Skip to content

Commit

Permalink
Bug 1907849: part 7) Add Worker tests to <DOMWindowTimers-setTimeout-…
Browse files Browse the repository at this point in the history
…setInterval.html>. r=smaug

Fixes w3c/trusted-types#568.

Differential Revision: https://phabricator.services.mozilla.com/D232323
  • Loading branch information
mbrodesser-Igalia committed Dec 18, 2024
1 parent cc05414 commit 3fa0198
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
[DOMWindowTimers-setTimeout-setInterval.html]
expected:
if (os == "mac") and not debug: [OK, TIMEOUT]
TIMEOUT
[window.setTimeout assigned via default policy (successful Script transformation).]
expected:
if (os == "mac") and not debug: [PASS, TIMEOUT]

[window.setInterval assigned via default policy (successful Script transformation).]
expected:
if (os == "mac") and not debug: [PASS, TIMEOUT]

[DedicatedWorkerGlobalScope.setTimeout assigned via default policy (successful Script transformation).]
expected: TIMEOUT

[DedicatedWorkerGlobalScope.setInterval assigned via default policy (successful Script transformation).]
expected: TIMEOUT

[SharedWorkerGlobalScope.setTimeout assigned via default policy (successful Script transformation).]
expected: TIMEOUT

[SharedWorkerGlobalScope.setInterval assigned via default policy (successful Script transformation).]
expected: TIMEOUT
2 changes: 1 addition & 1 deletion testing/web-platform/tests/lint.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ SET TIMEOUT: shadow-dom/Document-prototype-currentScript.html
SET TIMEOUT: shadow-dom/scroll-to-the-fragment-in-shadow-tree.html
SET TIMEOUT: shadow-dom/slotchange-event.html
SET TIMEOUT: trusted-types/block-string-assignment-to-DOMWindowTimers-setTimeout-setInterval.html
SET TIMEOUT: trusted-types/DOMWindowTimers-setTimeout-setInterval.html
SET TIMEOUT: trusted-types/support/DOMWindowTimers-setTimeout-setInterval.js
SET TIMEOUT: user-timing/*
SET TIMEOUT: web-animations/crashtests/reparent-animating-element-002.html
SET TIMEOUT: web-animations/timing-model/animations/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,29 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timer-initialisation-steps">
</head>
<body>
<script>
async_test(t => {
window.timeoutTrustedTest = t;
let policy = createScript_policy(window, 'timeout');
let script = policy.createScript("window.timeoutTrustedTest.done();");
setTimeout(script);
}, "window.setTimeout assigned via policy (successful Script transformation).");
<!-- "META: global=worker"
(https://web-platform-tests.org/writing-tests/testharness.html#tests-for-other-or-multiple-globals-any-js)
can't be used here because constructing the worker would require a trusted type.
-->
<script src="support/DOMWindowTimers-setTimeout-setInterval.js"></script>
<script>
let test_setup_policy = trustedTypes.createPolicy("p", {
createScriptURL: x => x
});

async_test(t => {
window.intervalTrustedTest = t;
let policy = createScript_policy(window, 'script');
let script = policy.createScript("window.intervalTrustedTest.done();");
setInterval(script);
}, "window.setInterval assigned via policy (successful Script transformation).");
fetch_tests_from_worker(new Worker(test_setup_policy.createScriptURL(
"support/DOMWindowTimers-setTimeout-setInterval-worker.js")));

trustedTypes.createPolicy("default", {createScript: (s, _, sink) => {
if (s === "timeoutStringTest") {
assert_equals(sink, 'Window setTimeout');
} else if (s === "intervalStringTest") {
assert_equals(sink, 'Window setInterval');
}
return "window." + s + ".done()";
}});

async_test(t => {
window.timeoutStringTest = t;
let script = "timeoutStringTest";
setTimeout(script);
}, "window.setTimeout assigned via default policy (successful Script transformation).");

async_test(t => {
window.intervalStringTest = t;
let script = "intervalStringTest";
setInterval(script);
}, "window.setInterval assigned via default policy (successful Script transformation).");

async_test(t => {
window.timeoutFunctionTest = t;
let script = () => window.timeoutFunctionTest.done();
setTimeout(script);
}, "window.setTimeout assigned with a function handler shouldn't go through default policy.");

async_test(t => {
window.intervalFunctionTest = t;
let script = () => window.intervalFunctionTest.done();
setInterval(script);
}, "window.setInterval assigned with a function handler shouldn't go through default policy.");
</script>
fetch_tests_from_worker(new SharedWorker(test_setup_policy.createScriptURL(
"support/DOMWindowTimers-setTimeout-setInterval-worker.js")));
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let test_setup_policy = trustedTypes.createPolicy("p", {
createScriptURL: x => x
});

importScripts(test_setup_policy.createScriptURL("/resources/testharness.js"));

importScripts(test_setup_policy.createScriptURL("helper.sub.js"));
importScripts(test_setup_policy.createScriptURL(
"DOMWindowTimers-setTimeout-setInterval.js"));

done();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Content-Security-Policy: require-trusted-types-for 'script';
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// `globalThis.toString()` is of the form "[Object <someName>]".
const globalThisStr = globalThis.toString().split(" ")[1].slice(0, -1);

async_test(t => {
globalThis.timeoutTrustedTest = t;
let policy = createScript_policy(globalThis, 'timeout');
let script = policy.createScript("globalThis.timeoutTrustedTest.done();");
globalThis.setTimeout(script);
}, `${globalThisStr}.setTimeout assigned via policy (successful Script transformation).`);

async_test(t => {
globalThis.intervalTrustedTest = t;
let policy = createScript_policy(globalThis, 'script');
let script = policy.createScript("globalThis.intervalTrustedTest.done();");
globalThis.setInterval(script);
}, `${globalThisStr}.setInterval assigned via policy (successful Script transformation).`);

globalThis.trustedTypes.createPolicy("default", {createScript: (s, _, sink) => {
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timer-initialisation-steps,
// step 9.6.1.1.
const expectedSink = globalThisStr.includes("Window") ? "Window" : "Worker";

if (s === "timeoutStringTest") {
assert_equals(sink, `${expectedSink} setTimeout`);
} else if (s === "intervalStringTest") {
assert_equals(sink, `${expectedSink} setInterval`);
}
return "globalThis." + s + ".done()";
}});

async_test(t => {
globalThis.timeoutStringTest = t;
let script = "timeoutStringTest";
globalThis.setTimeout(script);
}, `${globalThisStr}.setTimeout assigned via default policy (successful Script transformation).`);

async_test(t => {
globalThis.intervalStringTest = t;
let script = "intervalStringTest";
globalThis.setInterval(script);
}, `${globalThisStr}.setInterval assigned via default policy (successful Script transformation).`);

async_test(t => {
globalThis.timeoutFunctionTest = t;
let script = () => globalThis.timeoutFunctionTest.done();
setTimeout(script);
}, `${globalThisStr}.setTimeout assigned with a function handler shouldn't go through default policy.`);

async_test(t => {
globalThis.intervalFunctionTest = t;
let script = () => globalThis.intervalFunctionTest.done();
setInterval(script);
}, `${globalThisStr}.setInterval assigned with a function handler shouldn't go through default policy.`);

0 comments on commit 3fa0198

Please sign in to comment.