Resource Load Statistics: Block cookies for prevalent resources without user interaction
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / remove-partitioning-in-redirect.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4     <meta charset="UTF-8">
5     <script src="/js-test-resources/js-test.js"></script>
6 </head>
7 <body onload="runTest()">
8 <script>
9     description("Tests that partitioning is removed mid-flight in redirects.");
10     jsTestIsAsync = true;
11
12     function setEnableFeature(enable) {
13         if (!enable)
14             testRunner.statisticsResetToConsistentState();
15         internals.setResourceLoadStatisticsEnabled(enable);
16         testRunner.setCookieStoragePartitioningEnabled(enable);
17     }
18
19     if (document.location.hash === "") {
20         setEnableFeature(true);
21         if (testRunner.isStatisticsPrevalentResource("http://localhost"))
22             testFailed("Localhost was classified as prevalent resource before the test starts.");
23         // Make sure the network process is up-to-date.
24         testRunner.statisticsSetShouldPartitionCookiesForHost("localhost", false);
25         testRunner.dumpChildFramesAsText();
26         document.location.hash = "step1";
27     }
28
29     const partitionHost = "127.0.0.1:8000";
30     const thirdPartyOrigin = "http://localhost:8000";
31     const resourcePath = "/resourceLoadStatistics/resources";
32     const thirdPartyBaseUrl = thirdPartyOrigin + resourcePath;
33     const firstPartyCookieName = "firstPartyCookie";
34     const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=value";
35     const partitionedCookieName = "partitionedCookie";
36     const subPathToSetPartitionedCookie = "/set-cookie.php?name=" + partitionedCookieName + "&value=value";
37     const returnUrl = "http://" + partitionHost + "/resourceLoadStatistics/remove-partitioning-in-redirect.html";
38     const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + partitionedCookieName;
39     const redirectChainUrl = thirdPartyOrigin + resourcePath + "/redirect.php?redirectTo=http://" + partitionHost + resourcePath + subPathToGetCookies;
40
41     function openIframe(url, onLoadHandler) {
42         const element = document.createElement("iframe");
43         element.src = url;
44         if (onLoadHandler) {
45             element.onload = onLoadHandler;
46         }
47         document.body.appendChild(element);
48     }
49
50     function runTest() {
51         switch (document.location.hash) {
52             case "#step1":
53                 // Set first-party cookies for 127.0.0.1 and localhost.
54                 document.cookie = firstPartyCookieName + "=127.0.0.1;path='/'";
55                 document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + returnUrl + "#step2";
56                 break;
57             case "#step2":
58                 // Check that the cookie gets sent for localhost under 127.0.0.1 since localhost is not prevalent.
59                 document.location.hash = "step3";
60                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive first-party cookie.", runTest);
61                 break;
62             case "#step3":
63                 document.location.hash = "step4";
64                 // Load an iframe in a redirect chain that starts with localhost and ends with 127.0.0.1. Expect a cookie for 127.0.0.1.
65                 openIframe(redirectChainUrl + "&message=Redirect case 1, should receive first-party cookie for 127.0.0.1.", runTest);
66                 break;
67             case "#step4":
68                 // Set localhost as prevalent and with non-recent user interaction to put it in the partitioning category.
69                 document.location.hash = "step5";
70                 testRunner.setStatisticsPrevalentResource("http://localhost", true);
71                 testRunner.setStatisticsHasHadNonRecentUserInteraction("http://localhost");
72                 testRunner.statisticsUpdateCookiePartitioning();
73                 if (!testRunner.isStatisticsPrevalentResource("http://localhost"))
74                     testFailed("Host did not get set as prevalent resource.");
75                 runTest();
76                 break;
77             case "#step5":
78                 // Set partitioned cookie for localhost under 127.0.0.1.
79                 document.location.hash = "step6";
80                 openIframe(thirdPartyBaseUrl + subPathToSetPartitionedCookie + "&message=Set partitioned, third-party cookie.", runTest);
81                 break;
82             case "#step6":
83                 // Check that partitioned cookie gets sent for localhost under 127.0.0.1 since localhost is partitioned.
84                 document.location.hash = "step7";
85                 openIframe(thirdPartyBaseUrl + subPathToGetCookies +  "&message=Should receive partitioned cookie.", runTest);
86                 break;
87             case "#step7":
88                 // Load an iframe in a redirect chain that starts with localhost and ends with 127.0.0.1. Expect a cookie for 127.0.0.1.
89                 document.location.hash = "step8";
90                 openIframe(redirectChainUrl + "&message=Redirect case 2, should receive first-party cookie for 127.0.0.1.", runTest);
91                 break;
92             case "#step8":
93                 setEnableFeature(false);
94                 finishJSTest();
95                 break;
96         }
97     }
98 </script>
99 </body>
100 </html>