blob: 84fdc979aed37df5c9d2110170e1ae8ca75dad2c [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 thirdPartyCookieName = "thirdPartyCookie";
const subPathToSetThirdPartyCookie = "/set-cookie.php?name=" + thirdPartyCookieName + "&value=value";
const partitionedThirdPartyCookieName = "partitionedThirdPartyCookie";
const subPathToSetPartitionedThirdPartyCookie = "/set-cookie.php?name=" + partitionedThirdPartyCookieName + "&value=value";
const fragmentWithReturnUrl = "http://127.0.0.1:8000/resourceLoadStatistics/third-party-cookie-with-and-without-user-interaction.html";
const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + thirdPartyCookieName + "&name3=" + partitionedThirdPartyCookieName;
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 first-party cookie for localhost, effectively granting it access to cookies in a third-party context.
document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + fragmentWithReturnUrl + "#step2";
break;
case "#step2":
// Set first-party cookie for localhost in a third-party context.
document.location.hash = "step3";
openIframe(thirdPartyBaseUrl + subPathToSetThirdPartyCookie + "&message=Setting cookie as third party.", runTest);
break;
case "#step3":
// Check that both cookies as sent.
document.location.hash = "step4";
openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive both the cookie set in a first-party context and in a third-party context.", runTest);
break;
case "#step4":
// Flag localhost for partitioning and set a partitioned cookie.
document.location.hash = "step5";
testRunner.statisticsSetShouldPartitionCookiesForHost(thirdPartyHostname, true, runTest);
break;
case "#step5":
// Flag localhost for partitioning and set a partitioned cookie.
document.location.hash = "step6";
openIframe(thirdPartyBaseUrl + subPathToSetPartitionedThirdPartyCookie + "&message=Setting partitioned, third party cookie.", runTest);
break;
case "#step6":
document.location.hash = "step7";
openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should only receive partitioned, third party cookie.", setUserInteractionAndContinue);
break;
case "#step7":
openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After user interaction, should receive non-partitioned cookies set in a first-party context and in a third-party context.", finishTest);
break;
}
}
if (document.location.host === partitionHost && document.location.hash == "" && window.testRunner && window.internals) {
setEnableFeature(true);
testRunner.waitUntilDone();
testRunner.dumpChildFramesAsText();
document.location.hash = "step1";
// Start test with third party as non-prevalent
testRunner.setStatisticsPrevalentResource(thirdPartyHostname, false);
testRunner.setStatisticsHasHadUserInteraction(thirdPartyOrigin, false);
testRunner.statisticsSetShouldPartitionCookiesForHost(thirdPartyHostname, false, runTest);
} else {
runTest();
}
</script>
</body>
</html>