Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<!DOCTYPE html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/navigation-support.js"></script>
</head>
<body>
<script>
"use strict";
function expectMessage(filter) {
return new Promise(resolve => {
function listener(e) {
if (filter(e)) {
window.removeEventListener("message", listener);
resolve();
}
}
window.addEventListener("message", listener);
});
}
function expectViolationAsMessage(sample) {
const filter = e => {
const result = (e.data.effectiveDirective == "require-trusted-types-for" &&
(!sample || e.data.sample.startsWith(sample)));
if (result) {
assert_true(true, "Expected violation as message: " + sample);
}
return result;
}
return new expectMessage(filter);
}
function expectLoadedAsMessage(uri) {
const filter = e => {
const result = (e.data.type == "DOMContentLoaded" &&
(!uri || e.data.uri.endsWith(uri)));
if (result) {
assert_true(true, "Expected loaded as message: " + uri);
}
return result;
}
return new expectMessage(filter);
}
function openWindow(test, uri) {
const win = window.open(uri);
test.add_cleanup(_ => win.close());
}
const kFormSubmission = "form-submission";
// When adding more elements, adapt all functions consuming the existing elements.
const kNavigationElements =
[
"anchor",
kFormSubmission,
];
function maybeAddFormSubmissionToSearchParams(navigationElement, searchParams) {
return (navigationElement == kFormSubmission) ?
[kFormSubmission + "=1", ...searchParams] : searchParams;
}
function joinToHref(searchParams, originAndPathName) {
if (searchParams.length > 0) {
return originAndPathName + "?" + searchParams.join("&");
}
return originAndPathName;
}
for (const navigationElement of kNavigationElements) {
promise_test(t => {
const page = joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement, []),
"navigation-support.html");
openWindow(t, `support/${page}`);
return Promise.all([
expectLoadedAsMessage(page),
expectViolationAsMessage("Location href"),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls in enforcing mode.`);
promise_test(t => {
const page = joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement, ["defaultpolicy=1"]),
"navigation-support.html");
openWindow(t, `support/${page}`);
return Promise.all([
expectLoadedAsMessage(page),
expectLoadedAsMessage(page + "&" + kNavigationAttempted + "&defaultpolicywashere"),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls w/ default policy in enforcing mode.`);
promise_test(t => {
const page = joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement, []),
"navigation-report-only-support.html");
openWindow(t, `support/${page}`);
return Promise.all([
expectLoadedAsMessage(page),
expectLoadedAsMessage(joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement,
[kNavigationAttempted, "continue"]), "navigation-support.html")),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls in report-only mode.`);
promise_test(t => {
const page = joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement, ["defaultpolicy=1"]),
"navigation-report-only-support.html");
openWindow(t, `support/${page}`);
return Promise.all([
expectLoadedAsMessage(page),
expectLoadedAsMessage(joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement,
["defaultpolicy=1", kNavigationAttempted, "defaultpolicywashere"]),
"navigation-support.html")),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls w/ default policy in report-only mode.`);
promise_test(t => {
const page = joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement, ["frame=1"]),
"navigation-support.html");
openWindow(t, `support/${page}`);
return Promise.all([
expectLoadedAsMessage(page),
expectViolationAsMessage("Location href"),
]);
}, `Navigate a frame via ${navigationElement} with javascript:-urls in enforcing mode.`);
promise_test(t => {
const page = joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement,
["defaultpolicy=1", "frame=1"]),
"navigation-support.html");
openWindow(t, `support/${page}`);
return Promise.all([
expectLoadedAsMessage(page),
expectLoadedAsMessage(page + "&" + kNavigationAttempted + "&defaultpolicywashere"),
]);
}, `Navigate a frame via ${navigationElement} with javascript:-urls w/ default policy in enforcing mode.`);
promise_test(t => {
const page = joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement, ["frame=1"]),
"navigation-report-only-support.html");
openWindow(t, `support/${page}`);
return Promise.all([
expectLoadedAsMessage(page),
expectLoadedAsMessage(joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement,
["frame=1", kNavigationAttempted, "continue"]),
"navigation-support.html")),
]);
}, `Navigate a frame via ${navigationElement} with javascript:-urls in report-only mode.`);
promise_test(t => {
const page = joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement,
["defaultpolicy=1", "frame=1"]),
"navigation-report-only-support.html");
openWindow(t, `support/${page}`);
return Promise.all([
expectLoadedAsMessage(page),
expectLoadedAsMessage(joinToHref(maybeAddFormSubmissionToSearchParams(navigationElement,
["defaultpolicy=1", "frame=1", kNavigationAttempted, "defaultpolicywashere"]),
"navigation-support.html")),
]);
}, `Navigate a frame via ${navigationElement} with javascript:-urls w/ default policy in report-only mode.`);
}
</script>
</body>