Commit 2d090a27 authored by Hiroshige Hayashizaki's avatar Hiroshige Hayashizaki Committed by Commit Bot

[WPT/referrer-policy] Call getRequestURLs() for each subtest

Previously, getRequestURLs(), PolicyDelivery/Subresouce
object creation etc. were done once per test HTML file and
reused among multiple subtests.

This CL introduces invokeScenario() that does these things and
calls invokeScenario() for each subtest, and thus
calls getRequestURLs() for each subtest.

This removes hacky modifications to `subresource.url`
in 4K-referrer-length-related subtests to avoid cache hit.
Also this makes subresource URLs unique for
srcdoc-related subtests (previously the same image URL
was used).

Bug: 906850
Change-Id: I894a902338a90526678b70d01857103f46984189
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1723013
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/master@{#690288}
parent b8deeb50
...@@ -51,6 +51,41 @@ function stripUrlForUseAsReferrer(url) { ...@@ -51,6 +51,41 @@ function stripUrlForUseAsReferrer(url) {
return url.replace(/#.*$/, ""); return url.replace(/#.*$/, "");
} }
function invokeScenario(scenario, sourceContextList) {
const originTypeConversion = {
"same-origin-http": "same-http",
"same-origin-https": "same-https",
"cross-origin-http": "cross-http",
"cross-origin-https": "cross-https"
};
const urls = getRequestURLs(
scenario.subresource,
originTypeConversion[scenario.origin + '-' + scenario.target_protocol],
scenario.redirection);
const deliveryTypeConversion = {
"attr-referrer": "attr",
"rel-noreferrer": "rel-noref",
// Other delivery methods such as "http-rp" are ignored here because
// they are already applied to the main document by generator.py.
};
/** @type {PolicyDelivery} */
const delivery = {
deliveryType: deliveryTypeConversion[scenario.delivery_method],
key: "referrerPolicy",
value: scenario.referrer_policy};
/** @type {Subresource} */
const subresource = {
subresourceType: scenario.subresource,
url: urls.testUrl,
policyDeliveries: [delivery]
};
return invokeRequest(subresource, sourceContextList || []);
}
function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) { function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) {
// Pass and skip rest of the test if browser does not support fetch. // Pass and skip rest of the test if browser does not support fetch.
if (scenario.subresource == "fetch-request" && !window.fetch) { if (scenario.subresource == "fetch-request" && !window.fetch) {
...@@ -66,17 +101,6 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) { ...@@ -66,17 +101,6 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) {
// This check is A NOOP in release. // This check is A NOOP in release.
sanityChecker.checkScenario(scenario); sanityChecker.checkScenario(scenario);
const originTypeConversion = {
"same-origin-http": "same-http",
"same-origin-https": "same-https",
"cross-origin-http": "cross-http",
"cross-origin-https": "cross-https"
};
const urls = getRequestURLs(
scenario.subresource,
originTypeConversion[scenario.origin + '-' + scenario.target_protocol],
scenario.redirection);
const referrerUrlResolver = { const referrerUrlResolver = {
"omitted": function(sourceUrl) { "omitted": function(sourceUrl) {
return undefined; return undefined;
...@@ -89,15 +113,16 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) { ...@@ -89,15 +113,16 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) {
} }
}; };
const checkResult = (expectedReferrerUrl, result) => { const checkResult = (expectation, result) => {
// Check if the result is in valid format. NOOP in release. let currentURL = location.toString();
sanityChecker.checkSubresourceResult(scenario, urls.testUrl, result); const expectedReferrerUrl =
referrerUrlResolver[expectation](currentURL);
// Check the reported URL. // Check the reported URL.
assert_equals(result.referrer, assert_equals(result.referrer,
expectedReferrerUrl, expectedReferrerUrl,
"Reported Referrer URL is '" + "Reported Referrer URL is '" +
scenario.referrer_url + "'."); expectation + "'.");
assert_equals(result.headers.referer, assert_equals(result.headers.referer,
expectedReferrerUrl, expectedReferrerUrl,
"Reported Referrer URL from HTTP header is '" + "Reported Referrer URL from HTTP header is '" +
...@@ -105,30 +130,6 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) { ...@@ -105,30 +130,6 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) {
}; };
function runTest() { function runTest() {
const deliveryTypeConversion = {
"attr-referrer": "attr",
"rel-noreferrer": "rel-noref",
// Other delivery methods such as "http-rp" are ignored here because
// they are already applied to the main document by generator.py.
};
/** @type {PolicyDelivery} */
const delivery = {
deliveryType: deliveryTypeConversion[scenario.delivery_method],
key: "referrerPolicy",
value: scenario.referrer_policy};
/** @type {Subresource} */
const subresource = {
subresourceType: scenario.subresource,
url: urls.testUrl,
policyDeliveries: [delivery]
};
let currentURL = location.toString();
const expectedReferrer =
referrerUrlResolver[scenario.referrer_url](currentURL);
function historyBackPromise(t, scenario) { function historyBackPromise(t, scenario) {
history.back(); history.back();
return new Promise(resolve => { return new Promise(resolve => {
...@@ -148,43 +149,37 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) { ...@@ -148,43 +149,37 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) {
// Request in the top-level document. // Request in the top-level document.
promise_test(_ => { promise_test(_ => {
return invokeRequest(subresource, []) return invokeScenario(scenario)
.then(result => checkResult(expectedReferrer, result)); .then(result => checkResult(scenario.referrer_url, result));
}, testDescription); }, testDescription);
// `Referer` headers with length over 4k are culled down to an origin, so, let's test around // `Referer` headers with length over 4k are culled down to an origin, so,
// that boundary for tests that would otherwise return the complete URL. // let's test around that boundary for tests that would otherwise return
// the complete URL.
// Different subresource URLs are used because getRequestURLs() is called
// for each sub test which returns a unique URL.
if (scenario.referrer_url == "stripped-referrer") { if (scenario.referrer_url == "stripped-referrer") {
promise_test(t => { promise_test(t => {
history.pushState(null, null, "/"); history.pushState(null, null, "/");
history.replaceState(null, null, "A".repeat(4096 - location.href.length - 1)); history.replaceState(null, null, "A".repeat(4096 - location.href.length - 1));
const expectedReferrer = location.href; return invokeScenario(scenario)
// Ensure that we don't load the same URL as the previous test. .then(result => checkResult(scenario.referrer_url, result))
subresource.url += "&-1";
return invokeRequest(subresource, [])
.then(result => checkResult(location.href, result))
.finally(_ => historyBackPromise(t, scenario)); .finally(_ => historyBackPromise(t, scenario));
}, "`Referer` header with length < 4k is not stripped to an origin."); }, "`Referer` header with length < 4k is not stripped to an origin.");
promise_test(t => { promise_test(t => {
history.pushState(null, null, "/"); history.pushState(null, null, "/");
history.replaceState(null, null, "A".repeat(4096 - location.href.length)); history.replaceState(null, null, "A".repeat(4096 - location.href.length));
const expectedReferrer = location.href; return invokeScenario(scenario)
// Ensure that we don't load the same URL as the previous test. .then(result => checkResult(scenario.referrer_url, result))
subresource.url += "&0";
return invokeRequest(subresource, [])
.then(result => checkResult(expectedReferrer, result))
.finally(_ => historyBackPromise(t, scenario)); .finally(_ => historyBackPromise(t, scenario));
}, "`Referer` header with length == 4k is not stripped to an origin."); }, "`Referer` header with length == 4k is not stripped to an origin.");
promise_test(t => { promise_test(t => {
const originString = referrerUrlResolver["origin"](currentURL);
history.pushState(null, null, "/"); history.pushState(null, null, "/");
history.replaceState(null, null, "A".repeat(4096 - location.href.length + 1)); history.replaceState(null, null, "A".repeat(4096 - location.href.length + 1));
// Ensure that we don't load the same URL as the previous test. return invokeScenario(scenario)
subresource.url += "&+1"; .then(result => checkResult("origin", result))
return invokeRequest(subresource, [])
.then(result => checkResult(originString, result))
.finally(_ => historyBackPromise(t, scenario)); .finally(_ => historyBackPromise(t, scenario));
}, "`Referer` header with length > 4k is stripped to an origin."); }, "`Referer` header with length > 4k is stripped to an origin.");
} }
...@@ -202,8 +197,8 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) { ...@@ -202,8 +197,8 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) {
/** @type {Array<SourceContext>} */ /** @type {Array<SourceContext>} */
const sourceContextList = [{sourceContextType: "srcdoc"}]; const sourceContextList = [{sourceContextType: "srcdoc"}];
return invokeRequest(subresource, sourceContextList) return invokeScenario(scenario, sourceContextList)
.then(result => checkResult(expectedReferrer, result)); .then(result => checkResult(scenario.referrer_url, result));
}, testDescription + " (srcdoc iframe inherits parent)"); }, testDescription + " (srcdoc iframe inherits parent)");
// Request in a `srcdoc` frame with its own referrer policy to // Request in a `srcdoc` frame with its own referrer policy to
...@@ -214,16 +209,14 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) { ...@@ -214,16 +209,14 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) {
const overridingPolicy = const overridingPolicy =
scenario.referrer_policy === "no-referrer" ? "unsafe-url" scenario.referrer_policy === "no-referrer" ? "unsafe-url"
: "no-referrer"; : "no-referrer";
const overrridingExpectedReferrer = const overrridingExpectation =
referrerUrlResolver[overridingPolicy === "no-referrer" overridingPolicy === "no-referrer" ? "omitted"
? "omitted" : "stripped-referrer";
: "stripped-referrer"](location.toString());
/** @type {Subresource} */ const scenarioWithoutDelivery = Object.assign({}, scenario);
const subresourceWithoutDelivery = { // Omit policy deliveries applied to subresource requests.
subresourceType: scenario.subresource, // This is hacky method but will be removed soon.
url: urls.testUrl scenarioWithoutDelivery.delivery_method = null;
};
// <iframe srcdoc> with overriding <meta> referrerPolicy. // <iframe srcdoc> with overriding <meta> referrerPolicy.
/** @type {Array<SourceContext>} */ /** @type {Array<SourceContext>} */
...@@ -234,8 +227,8 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) { ...@@ -234,8 +227,8 @@ function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) {
value: overridingPolicy}] value: overridingPolicy}]
}]; }];
return invokeRequest(subresourceWithoutDelivery, sourceContextList) return invokeScenario(scenarioWithoutDelivery, sourceContextList)
.then(result => checkResult(overrridingExpectedReferrer, result)); .then(result => checkResult(overrridingExpectation, result));
}, testDescription + " (overridden by srcdoc iframe)"); }, testDescription + " (overridden by srcdoc iframe)");
} }
......
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