blob: b8df5aee09c130c3da67b2400ca44cb84c11dae0 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test to ensure non-prevalent resources can access their cookies in a third party context</title>
<script src="/js-test-resources/js-test.js"></script>
</head>
<body>
<script>
const partitionHost = "127.0.0.1:8000";
const thirdPartyHostname = "localhost";
const thirdPartyOrigin = "http://" + thirdPartyHostname + ":8000";
const thirdPartyBaseUrl = thirdPartyOrigin + "/resourceLoadStatistics/resources";
const firstPartyCookieName = "firstPartyCookie";
const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=value";
const partitionedCookieName = "partitionedCookie";
const subPathToSetPartitionedCookie = "/set-cookie.php?name=" + partitionedCookieName + "&value=value";
const fragmentWithReturnUrl = "http://127.0.0.1:8000/resourceLoadStatistics/non-prevalent-resources-can-access-cookies-in-a-third-party-context.html";
const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + partitionedCookieName;
function setEnableFeature(enable) {
if (!enable) {
testRunner.statisticsResetToConsistentState();
}
testRunner.setStatisticsNotifyPagesWhenDataRecordsWereScanned(enable);
internals.setResourceLoadStatisticsEnabled(enable);
testRunner.setCookieStoragePartitioningEnabled(enable);
}
function finishTest() {
setEnableFeature(false);
testRunner.notifyDone();
}
function openIframe(url, onLoadHandler) {
const element = document.createElement("iframe");
element.src = url;
if (onLoadHandler) {
element.onload = onLoadHandler;
}
document.body.appendChild(element);
}
function setUserInteractionAndContinue() {
testRunner.setStatisticsHasHadUserInteraction(thirdPartyOrigin, true);
if (!testRunner.isStatisticsHasHadUserInteraction(thirdPartyOrigin))
testFailed("Third party did not get logged for user interaction.");
runTest();
}
function runTest() {
switch (document.location.hash) {
case "#step1":
// Set a first-party cookie for localhost.
document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + fragmentWithReturnUrl + "#step2";
break;
case "#step2":
// Load localhost under 127.0.0.1 and check that it gets its cookie.
document.location.hash = "step3";
openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive one cookie.", runTest);
break;
case "#step3":
// Setup localhost for partitioning.
document.location.hash = "step4";
testRunner.statisticsSetShouldPartitionCookiesForHost(thirdPartyHostname, true, runTest);
break;
case "#step4":
// Set a partitioned cookie for localhost under 127.0.0.1.
document.location.hash = "step5";
openIframe(thirdPartyBaseUrl + subPathToSetPartitionedCookie + "&message=Setting partitioned, third party cookie.", runTest);
break;
case "#step5":
// Load localhost under 127.0.0.1 and check that it gets only the partitioned cookie. End by acquiring user interaction for localhost.
document.location.hash = "step6";
openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive one partitioned, third party cookie.", setUserInteractionAndContinue);
break;
case "#step6":
// Load localhost under 127.0.0.1 and check that it gets its non-partitioned cookie after user interaction.
openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After user interaction, should receive one non-partitioned, first party cookie.", finishTest);
break;
}
}
if (document.location.host === partitionHost && document.location.hash == "" && window.testRunner && window.internals) {
setEnableFeature(true);
testRunner.setStatisticsPrevalentResource(thirdPartyHostname, false);
testRunner.setStatisticsHasHadUserInteraction(thirdPartyOrigin, false);
testRunner.waitUntilDone();
testRunner.dumpChildFramesAsText();
document.location.hash = "step1";
testRunner.statisticsSetShouldPartitionCookiesForHost(thirdPartyHostname, false, runTest);
} else {
runTest();
}
</script>
</body>
</html>