Commit fa104956 authored by Lukasz Anforowicz's avatar Lukasz Anforowicz Committed by Commit Bot

Fix how CORB async_test(s) work.

Some CORB tests run in parallel, because they use `async_test`.  See
https://web-platform-tests.org/writing-tests/testharness-api.html#asynchronous-tests:
"Keep in mind that other tests could start executing before an
Asynchronous Test is finished."

This CL fixes such tests, to make sure that they either migrate to
`promise_test` [1] or avoid using shared/global state.

[1] https://web-platform-tests.org/writing-tests/testharness-api.html#promise-tests
says "Unlike Asynchronous Tests, Promise Tests don’t start running until
after the previous Promise Test finishes. Under rare circumstances, the
next test may begin to execute before the returned promise has settled.
Use add_cleanup to register any necessary cleanup actions such as
resetting global state that need to happen consistently before the next
test starts."

Bug: 1011410
Change-Id: Ib369e6cd07aed2daaf6347ae5d6f777a2c0835db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1841898
Commit-Queue: Łukasz Anforowicz <lukasza@chromium.org>
Reviewed-by: default avatarPhilip Jägenstedt <foolip@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706131}
parent 9d0ff9f8
......@@ -4,6 +4,6 @@
// which found out that some script resources are served
// with text/html content-type and with a body that is
// both a valid html and a valid javascript.
window.polyglot = "html-js-polyglot.js";
window['html-js-polyglot.js'] = true;
//--></script></body></html>
......@@ -5,6 +5,6 @@
// which found out that some script resources are served
// with text/html content-type and with a body that is
// both a valid html and a valid javascript.
window.polyglot = "html-js-polyglot2.js";
window['html-js-polyglot2.js'] = true;
//]]>--></script>
......@@ -9,13 +9,13 @@
<script>
["html-js-polyglot.js", "html-js-polyglot2.js"].forEach(polyglot_name => {
async_test(function(t) {
window.polyglot = "not yet set by the script";
window[polyglot_name] = false;
var script = document.createElement("script");
script.onload = t.step_func_done(function(){
// Verify that the script response wasn't blocked - that script
// should have set window.polyglot to |polyglot_name|.
assert_equals(window.polyglot, polyglot_name);
// should have set window[polyglot_name] to true.
assert_true(window[polyglot_name]);
})
addEventListener("error",function(e) {
t.step(function() {
......
......@@ -53,25 +53,25 @@ mime_types = [
]
function test(mime_type, body) {
async_test(function(t) {
// The test below depends on a global/shared event handler - we need to ensure
// that no tests run in parallel - this is achieved by using `promise_test`
// instead of `async_test`. See also
// https://web-platform-tests.org/writing-tests/testharness-api.html#promise-tests
promise_test(t => new Promise(function(resolve, reject) {
var script = document.createElement("script")
// Without CORB, the JSON parser breaker would cause a syntax error when
// parsed as JavaScript, but with CORB there should be no errors (because
// CORB will replace the response body with an empty body).
script.onload = t.step_func_done(function(){})
addEventListener("error",function(e) {
t.step(function() {
assert_unreached("Empty body of a CORS-blocked response shouldn't trigger syntax errors.");
t.done();
})
});
script.onload = resolve;
addEventListener("error", t.unreached_func(
"Empty body of a CORS-blocked response shouldn't trigger syntax errors."))
// www1 is cross-origin, so the HTTP response is CORB-eligible.
var src_prefix = "http://{{domains[www1]}}:{{ports[http][0]}}/fetch/corb/resources/sniffable-resource.py";
script.src = src_prefix + "?type=" + mime_type + "&body=" + encodeURIComponent(body);
document.body.appendChild(script)
}, "CORB-blocks '" + mime_type + "' that starts with the following JSON parser breaker: " + body);
}), "CORB-blocks '" + mime_type + "' that starts with the following JSON parser breaker: " + body);
}
mime_types.forEach(function(type) {
......
......@@ -11,6 +11,7 @@
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<div id=log></div>
<script>
setup({allow_uncaught_exception : true});
......@@ -18,16 +19,17 @@ setup({allow_uncaught_exception : true});
function test(mime_type, is_blocking_expected) {
async_test(function(t) {
var script = document.createElement("script")
var script_has_run_token = "script_has_run" + token();
// With and without CORB there should be no error, but without CORB the
// original script body will be preserved and |window.script_has_run| will
// be set.
window.script_has_run = false;
window[script_has_run_token] = false;
script.onload = t.step_func_done(function(){
if (is_blocking_expected) {
assert_false(window.script_has_run);
assert_false(window[script_has_run_token]);
} else {
assert_true(window.script_has_run);
assert_true(window[script_has_run_token]);
}
});
addEventListener("error",function(e) {
......@@ -44,7 +46,7 @@ function test(mime_type, is_blocking_expected) {
// rather than cross-*site* URL below (e.g. s/hosts[alt]/domains/g).
// See also https://crbug.com/918660 for more context.
var src_prefix = "http://{{hosts[alt][www1]}}:{{ports[http][0]}}/fetch/corb/resources/sniffable-resource.py";
body = "window.script_has_run = true;"
body = `window['${script_has_run_token}'] = true;`
script.src = src_prefix + "?type=" + mime_type + "&body=" + encodeURIComponent(body);
document.body.appendChild(script)
}, "CORB-blocks '" + mime_type + "' that starts with the following JSON parser breaker: " + body);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment