Resource Load Statistics: Block cookies for prevalent resources without user interaction
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / non-prevalent-resources-can-access-cookies-in-a-third-party-context.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4     <meta charset="UTF-8">
5     <title>Test to ensure non-prevalent resources can access their cookies in a third party context</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 + "/resourceLoadStatistics/resources";
14     const firstPartyCookieName = "firstPartyCookie";
15     const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=value";
16     const partitionedCookieName = "partitionedCookie";
17     const subPathToSetPartitionedCookie = "/set-cookie.php?name=" + partitionedCookieName + "&value=value";
18     const fragmentWithReturnUrl = "http://127.0.0.1:8000/resourceLoadStatistics/non-prevalent-resources-can-access-cookies-in-a-third-party-context.html";
19     const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + partitionedCookieName;
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                 // Set a first-party cookie for localhost.
55                 document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + fragmentWithReturnUrl + "#step2";
56                 break;
57             case "#step2":
58                 // Load localhost under 127.0.0.1 and check that it gets its cookie.
59                 document.location.hash = "step3";
60                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive one cookie.", runTest);
61                 break;
62             case "#step3":
63                 // Setup localhost for partitioning and set a partitioned cookie for localhost under 127.0.0.1.
64                 document.location.hash = "step4";
65                 testRunner.statisticsSetShouldPartitionCookiesForHost(thirdPartyHostname, true);
66                 openIframe(thirdPartyBaseUrl + subPathToSetPartitionedCookie + "&message=Setting partitioned, third party cookie.", runTest);
67                 break;
68             case "#step4":
69                 // Load localhost under 127.0.0.1 and check that it gets only the partitioned cookie. End by aquiring user interaction for localhost.
70                 document.location.hash = "step5";
71                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive one partitioned, third party cookie.", setUserInteractionAndContinue);
72                 break;
73             case "#step5":
74                 // Load localhost under 127.0.0.1 and check that it gets its non-partitioned cookie after user interaction.
75                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After user interaction, should receive one non-partitioned, first party cookie.", finishTest);
76                 break;
77         }
78     }
79
80     if (document.location.host === partitionHost && document.location.hash == "" && window.testRunner && window.internals) {
81         setEnableFeature(true);
82
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>