Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor tests for sandbox iframes controlled by service worker
This patch is to split sandboxed-iframe-fetch-event.https.html into small chunks. Each promise_test has smaller number of assertions, so that now we can see the results for each situation. Bug: 771815 Change-Id: I75cdd67f92bbb07d6b538b209e1f0819752c3bc8
- Loading branch information
1 parent
e6bda36
commit 2836fb3
Showing
4 changed files
with
411 additions
and
249 deletions.
There are no files selected for viewing
91 changes: 49 additions & 42 deletions
91
service-workers/service-worker/resources/sandboxed-iframe-fetch-event-iframe.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,63 @@ | ||
<script> | ||
function with_iframe(url) { | ||
return new Promise(function(resolve) { | ||
var frame = document.createElement('iframe'); | ||
frame.src = url; | ||
frame.onload = function() { resolve(frame); }; | ||
document.body.appendChild(frame); | ||
}); | ||
return new Promise(resolve => { | ||
let frame = document.createElement('iframe'); | ||
frame.src = url; | ||
frame.onload = () => { resolve(frame); }; | ||
document.body.appendChild(frame); | ||
}); | ||
} | ||
|
||
function with_sandboxed_iframe(url, sandbox) { | ||
return new Promise(function(resolve) { | ||
var frame = document.createElement('iframe'); | ||
frame.sandbox = sandbox; | ||
frame.src = url; | ||
frame.onload = function() { resolve(frame); }; | ||
document.body.appendChild(frame); | ||
}); | ||
return new Promise(resolve => { | ||
let frame = document.createElement('iframe'); | ||
frame.sandbox = sandbox; | ||
frame.src = url; | ||
frame.onload = () => { resolve(frame); }; | ||
document.body.appendChild(frame); | ||
}); | ||
} | ||
|
||
function fetch_in_worker() { | ||
return new Promise((resolve) => { | ||
var blob = new Blob([ | ||
"fetch('" + location.href + "_workerfetch', {mode: 'no-cors'})" + | ||
function fetch_from_worker(url) { | ||
return new Promise(resolve => { | ||
let blob = new Blob([ | ||
`fetch('${url}', {mode: 'no-cors'})` + | ||
" .then(() => { self.postMessage('OK'); });"]); | ||
var url = URL.createObjectURL(blob); | ||
var worker = new Worker(url); | ||
let worker_url = URL.createObjectURL(blob); | ||
let worker = new Worker(worker_url); | ||
worker.onmessage = resolve; | ||
}); | ||
} | ||
|
||
window.onmessage = function (e) { | ||
var id = e.data['id']; | ||
fetch(location.href + "_fetch", {mode: 'no-cors'}) | ||
.then(function() { | ||
return fetch_in_worker(); | ||
}) | ||
.then(function() { | ||
return with_iframe(location.href + "_iframe"); | ||
}) | ||
.then(function() { | ||
return with_sandboxed_iframe(location.href + "_script", | ||
"allow-scripts"); | ||
}) | ||
.then(function() { | ||
return with_sandboxed_iframe(location.href + "_script-origin", | ||
"allow-scripts allow-same-origin"); | ||
}) | ||
.then(function() { | ||
window.top.postMessage({id: id, result: 'done'}, '*'); | ||
}) | ||
.catch(function(e) { | ||
window.top.postMessage({id: id, result: 'error: ' + e.toString()}, '*'); | ||
}); | ||
function run_test(type) { | ||
const base_path = location.href; | ||
switch (type) { | ||
case 'fetch': | ||
return fetch(`${base_path}&test=fetch`, {mode: 'no-cors'}); | ||
case 'fetch-from-worker': | ||
return fetch_from_worker(`${base_path}&test=fetch-from-worker`); | ||
case 'iframe': | ||
return with_iframe(`${base_path}&test=iframe`); | ||
case 'sandboxed-iframe': | ||
return with_sandboxed_iframe(`${base_path}&test=sandboxed-iframe`, | ||
"allow-scripts"); | ||
case 'sandboxed-iframe-same-origin': | ||
return with_sandboxed_iframe( | ||
`${base_path}&test=sandboxed-iframe-same-origin`, | ||
"allow-scripts allow-same-origin"); | ||
default: | ||
return Promise.reject(`Unknown type: ${type}`); | ||
} | ||
} | ||
|
||
window.onmessage = event => { | ||
let id = event.data['id']; | ||
run_test(event.data['type']) | ||
.then(() => { | ||
window.top.postMessage({id: id, result: 'done'}, '*'); | ||
}) | ||
.catch(e => { | ||
window.top.postMessage({id: id, result: 'error: ' + e.toString()}, '*'); | ||
}); | ||
}; | ||
</script> |
13 changes: 13 additions & 0 deletions
13
service-workers/service-worker/resources/sandboxed-iframe-fetch-event-iframe.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import os.path | ||
|
||
def main(request, response): | ||
header = [('Content-Type', 'text/html')] | ||
if 'test' in request.GET: | ||
with open(os.path.join(os.path.dirname(__file__),'blank.html'), 'r') as f: | ||
body = f.read() | ||
return (header, body) | ||
|
||
with open(os.path.join(os.path.dirname(__file__), | ||
'sandboxed-iframe-fetch-event-iframe.html'), 'r') as f: | ||
body = f.read() | ||
return (header, body) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.