Resource Load Statistics: Grandfather domains for existing data records
[WebKit-https.git] / LayoutTests / http / tests / loading / resourceLoadStatistics / grandfathering.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4     <meta charset="UTF-8">
5     <title>Test for Grandfathering</title>
6     <script src="../../resources/js-test-pre.js"></script>
7 </head>
8 <body>
9 <script>
10     const firstPartyOrigin = "http://localhost:8000";
11     const firstPartyBaseUrl = firstPartyOrigin + "/loading/resourceLoadStatistics/resources";
12     const firstPartyCookieName = "firstPartyCookie";
13     const thirdPartyCookieName = "thirdPartyCookie";
14     const cookieValue = "value";
15     const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=" + cookieValue;
16     const returnUrl = firstPartyOrigin + "/loading/resourceLoadStatistics/grandfathering.html";
17     const thirdPartyOrigin = "http://127.0.0.1:8000";
18     const thirdPartyBaseUrl = thirdPartyOrigin + "/loading/resourceLoadStatistics/resources";
19     const subPathToSetThirdPartyCookie = "/set-cookie.php?name=" + thirdPartyCookieName + "&value=" + cookieValue;
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 fireDataModificationHandlerAndContinue() {
36         testRunner.installStatisticsDidModifyDataRecordsCallback(function() {
37             runTest();
38         });
39         testRunner.statisticsFireDataModificationHandler();
40     }
41
42     function clearInMemoryAndPersistentStoreAndContinue() {
43         testRunner.installStatisticsDidScanDataRecordsCallback(function() {
44             runTest();
45         });
46         testRunner.statisticsClearInMemoryAndPersistentStore();
47     }
48
49     function runTest() {
50         switch (document.location.hash) {
51             case "#step1":
52                 // Set a first-party cookie for localhost.
53                 document.location.href = firstPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + returnUrl + "#step2";
54                 break;
55             case "#step2":
56                 document.location.hash = "step3";
57                 // Check that localhost's cookie is there.
58                 if (document.cookie !== firstPartyCookieName + "=" + cookieValue)
59                     testFailed("First-party cookie not set.");
60                 runTest();
61                 break;
62             case "#step3":
63                 document.location.hash = "step4";
64                 // Clear store to trigger grandfathering.
65                 clearInMemoryAndPersistentStoreAndContinue();
66                 break;
67             case "#step4":
68                 document.location.hash = "step5";
69                 // Set localhost as prevalent resource.
70                 testRunner.setStatisticsPrevalentResource("http://localhost", true);
71                 if (!testRunner.isStatisticsPrevalentResource("http://localhost"))
72                     testFailed("Localhost did not get set as prevalent resource.");
73                 runTest();
74                 break;
75             case "#step5":
76                 // Set a first-party cookie for 127.0.0.1.
77                 document.location.href = thirdPartyBaseUrl + subPathToSetThirdPartyCookie + "#" + returnUrl + "#step6";
78                 break;
79             case "#step6":
80                 document.location.hash = "step7";
81                 // Set 127.0.0.1 as prevalent resource
82                 testRunner.setStatisticsPrevalentResource("http://127.0.0.1", true);
83                 if (!testRunner.isStatisticsPrevalentResource("http://127.0.0.1"))
84                     testFailed("127.0.0.1 did not get set as prevalent resource.");
85                 runTest();
86                 break;
87             case "#step7":
88                 document.location.hash = "step8";
89                 // Trigger a purge.
90                 fireDataModificationHandlerAndContinue();
91                 break;
92             case "#step8":
93                 // Check that localhost's cookie is still there.
94                 if (document.cookie !== firstPartyCookieName + "=" + cookieValue)
95                     testFailed("First-party cookie purged.");
96                 else
97                     testPassed("Grandfathered cookie was not purged.");
98                 finishTest();
99                 break;
100             default:
101                 testFailed("Should not be reached.");
102         }
103     }
104
105     if (document.location.hash === "" && window.testRunner && window.internals) {
106         setEnableFeature(true);
107
108         testRunner.setStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(false);
109         testRunner.setStatisticsMinimumTimeBetweeenDataRecordsRemoval(0);
110
111         testRunner.waitUntilDone();
112         testRunner.dumpChildFramesAsText();
113         document.location.hash = "step1";
114     }
115
116     runTest();
117 </script>
118 </body>
119 </html>