5c84fb894f99b327c1773e2007dc10d91c2a8799
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / capped-lifetime-for-cookie-set-in-js-with-link-fragment-from-prevalent-resource-database.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4     <script src="/js-test-resources/js-test.js"></script>
5     <script src="/cookies/resources/cookie-utilities.js"></script>
6     <script src="resources/util.js"></script>
7 </head>
8 <body onload="setTimeout('runTest()', 0)">
9 <script>
10     description("Check that cookies created by JavaScript after a cross-site navigation with link fragment get capped to 24 hours.");
11     jsTestIsAsync = true;
12
13     function testCookies() {
14         let passedTests = 0;
15         function checkThatCookieDoesNotExpireAfter(cookieData, maxAgeInSeconds) {
16             let now = new Date();
17             let maxExpiryDateInMilliseconds = now.getTime() + (maxAgeInSeconds * 1000);
18
19             if (maxExpiryDateInMilliseconds > cookieData["expires"])
20                 ++passedTests;
21             else
22                 testFailed("Cookie named " + cookieData["name"] + " expires in more than " + maxAgeInSeconds + " seconds.");
23         }
24
25         const twelveHoursInSeconds = 12 * 60 * 60;
26         const shortLivedCookieMaxAge = { name : "shortLivedCookieMaxAge", lifetime : "Max-Age=" + twelveHoursInSeconds + ";" };
27         document.cookie = shortLivedCookieMaxAge.name + "=foobar; " + shortLivedCookieMaxAge.lifetime + " path=/";
28
29         const twelveHoursAsExpiresDate = createExpiresDateFromMaxAge(twelveHoursInSeconds);
30         const shortLivedCookieExpires = { name : "shortLivedCookieExpires", lifetime : "Expires=" + twelveHoursAsExpiresDate + ";" };
31         document.cookie = shortLivedCookieExpires.name + "=foobar; " + shortLivedCookieExpires.lifetime + " path=/";
32
33         const oneDayInSeconds = 2 * twelveHoursInSeconds;
34         const twoDaysInSeconds = 2 * oneDayInSeconds;
35         const longLivedCookieMaxAge = { name : "longLivedCookieMaxAge", lifetime : "Max-Age=" + twoDaysInSeconds + ";" };
36         document.cookie = longLivedCookieMaxAge.name + "=foobar; " + longLivedCookieMaxAge.lifetime + " path=/";
37
38         const twoDaysAsExpiresDate = createExpiresDateFromMaxAge(twoDaysInSeconds);
39         const longLivedCookieExpires = { name : "longLivedCookieExpires", lifetime : "Expires=" + twoDaysAsExpiresDate + ";" };
40         document.cookie = longLivedCookieExpires.name + "=foobar; " + longLivedCookieExpires.lifetime + " path=/";
41
42         const overTwelveHoursInSeconds = twelveHoursInSeconds + 30;
43         const overOneDayInSeconds = oneDayInSeconds + 30;
44         if (internals) {
45             let cookies = internals.getCookies();
46             if (!cookies.length)
47                 testFailed("No cookies found.");
48             for (let cookie of cookies) {
49                 switch (cookie.name) {
50                     case shortLivedCookieMaxAge.name:
51                         checkThatCookieDoesNotExpireAfter(cookie, overTwelveHoursInSeconds);
52                         break;
53                     case shortLivedCookieExpires.name:
54                         checkThatCookieDoesNotExpireAfter(cookie, overTwelveHoursInSeconds);
55                         break;
56                     case longLivedCookieMaxAge.name:
57                         checkThatCookieDoesNotExpireAfter(cookie, overOneDayInSeconds);
58                         break;
59                     case longLivedCookieExpires.name:
60                         checkThatCookieDoesNotExpireAfter(cookie, overOneDayInSeconds);
61                         break;
62                 }
63             }
64
65             resetCookiesForCurrentOrigin();
66
67             if (passedTests === 4) {
68                 testPassed("The two short-lived cookies don't expire after more than " + overTwelveHoursInSeconds + " seconds.");
69                 testPassed("The two long-lived cookies don't expire after more than " + overOneDayInSeconds + " seconds.");
70             } else
71                 testFailed("At least one cookie's expiry attribute was beyond the test thresholds.");
72         } else
73             testFailed("No internals object.");
74
75         setEnableFeature(false, finishJSTest);
76     }
77
78     function navigateCrossOrigin() {
79         document.location.href = destinationOrigin + "/resourceLoadStatistics/capped-lifetime-for-cookie-set-in-js-with-link-fragment-from-prevalent-resource.html#link=fragment";
80     }
81
82     const destinationOrigin = "http://localhost:8000";
83     const prevalentResourceOrigin = "http://127.0.0.1:8000";
84     function runTest() {
85         if (document.location.origin === prevalentResourceOrigin) {
86             testRunner.setUseITPDatabase(true);
87             setEnableFeature(true, function () {
88                 testRunner.setStatisticsPrevalentResource(prevalentResourceOrigin, true, function() {
89                     if (!testRunner.isStatisticsPrevalentResource(prevalentResourceOrigin))
90                         testFailed("Host did not get set as prevalent resource.");
91                     testRunner.statisticsUpdateCookieBlocking(navigateCrossOrigin);
92                 });
93
94             });
95         } else {
96             testCookies();
97         }
98     }
99 </script>
100 </body>
101 </html>