Skip to content

Commit

Permalink
Refactor tests for sandbox iframes controlled by service worker
Browse files Browse the repository at this point in the history
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
makotoshimazu authored and chromium-wpt-export-bot committed Feb 19, 2018
1 parent e6bda36 commit 2836fb3
Show file tree
Hide file tree
Showing 4 changed files with 411 additions and 249 deletions.
@@ -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>
@@ -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)
Expand Up @@ -10,6 +10,7 @@ self.addEventListener('message', function(event) {
client_urls = client_urls.sort();
event.data.port.postMessage(
{clients: client_urls, requests: requests});
requests = [];
}));
});

Expand Down

0 comments on commit 2836fb3

Please sign in to comment.