Deny third-party cookie creation for prevalent resources without interaction
[WebKit-https.git] / LayoutTests / http / tests / loading / resourceLoadStatistics / third-party-cookie-with-and-without-user-interaction.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4     <meta charset="UTF-8">
5     <title>Test for Partitioned Cookies With and Without User Interaction</title>
6     <script src="/js-test-resources/js-test.js"></script>
7 </head>
8 <body>
9 <script>
10     const partitionHost = "127.0.0.1:8000";
11     const thirdPartyHostname = "localhost";
12     const thirdPartyOrigin = "http://" + thirdPartyHostname + ":8000";
13     const thirdPartyBaseUrl = thirdPartyOrigin + "/loading/resourceLoadStatistics/resources";
14     const firstPartyCookieName = "firstPartyCookie";
15     const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=value";
16     const thirdPartyCookieName = "thirdPartyCookie";
17     const subPathToSetThirdPartyCookie = "/set-cookie.php?name=" + thirdPartyCookieName + "&value=value";
18     const fragmentWithReturnUrl = "http://127.0.0.1:8000/loading/resourceLoadStatistics/third-party-cookie-with-and-without-user-interaction.html";
19     const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + thirdPartyCookieName;
20
21     function setEnableFeature(enable) {
22         if (!enable) {
23             testRunner.statisticsResetToConsistentState();
24         }
25         testRunner.setStatisticsNotifyPagesWhenDataRecordsWereScanned(enable);
26         internals.setResourceLoadStatisticsEnabled(enable);
27         testRunner.setCookieStoragePartitioningEnabled(enable);
28     }
29
30     function finishTest() {
31         setEnableFeature(false);
32         testRunner.notifyDone();
33     }
34
35     function openIframe(url, onLoadHandler) {
36         const element = document.createElement("iframe");
37         element.src = url;
38         if (onLoadHandler) {
39             element.onload = onLoadHandler;
40         }
41         document.body.appendChild(element);
42     }
43
44     function setUserInteractionAndContinue() {
45         testRunner.setStatisticsHasHadUserInteraction(thirdPartyOrigin, true);
46         if (!testRunner.isStatisticsHasHadUserInteraction(thirdPartyOrigin))
47             testFailed("Third party did not get logged for user interaction.");
48         runTest();
49     }
50
51     function runTest() {
52         switch (document.location.hash) {
53             case "#step1":
54                 document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + fragmentWithReturnUrl + "#step2";
55                 break;
56             case "#step2":
57                 document.location.hash = "step3";
58                 openIframe(thirdPartyBaseUrl + subPathToSetThirdPartyCookie + "&message=Setting third party cookie.", runTest);
59                 break;
60             case "#step3":
61                 document.location.hash = "step4";
62                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive no cookies.", runTest);
63                 break;
64             case "#step4":
65                 document.location.hash = "step5";
66                 testRunner.statisticsSetShouldPartitionCookiesForHost(thirdPartyHostname, true);
67                 openIframe(thirdPartyBaseUrl + subPathToSetThirdPartyCookie + "&message=Setting partitioned, third party cookie.", runTest);
68                 break;
69             case "#step5":
70                 document.location.hash = "step6";
71                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive partitioned, third party cookie.", setUserInteractionAndContinue);
72                 break;
73             case "#step6":
74                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After user interaction, should receive non-partitioned, first party cookie.", finishTest);
75                 break;
76         }
77     }
78
79     if (document.location.host === partitionHost && document.location.hash == "" && window.testRunner && window.internals) {
80         setEnableFeature(true);
81
82         // Start test with third party as non-prevalent
83         testRunner.setStatisticsPrevalentResource(thirdPartyHostname, false);
84         testRunner.setStatisticsHasHadUserInteraction(thirdPartyOrigin, false);
85         testRunner.statisticsSetShouldPartitionCookiesForHost(thirdPartyHostname, false);
86
87         testRunner.waitUntilDone();
88         testRunner.dumpChildFramesAsText();
89         document.location.hash = "step1";
90     }
91
92     runTest();
93 </script>
94 </body>
95 </html>