Source/WebCore:
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / capped-lifetime-for-cookie-set-in-js-without-link-decoration-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 without link decoration don't get capped to 24 hours.");
11     jsTestIsAsync = true;
12
13     function testCookies() {
14         let passedTests = 0;
15
16         function checkThatCookieDoesExpireAfter(cookieData, maxAgeInSeconds) {
17             let now = new Date();
18             let maxExpiryDateInMilliseconds = now.getTime() + (maxAgeInSeconds * 1000);
19
20             if (maxExpiryDateInMilliseconds < cookieData["expires"])
21                 ++passedTests;
22             else
23                 testFailed("Cookie named " + cookieData["name"] + " expires in less than " + maxAgeInSeconds + " seconds.");
24         }
25
26         const oneDayInSeconds = 24 * 60 * 60;
27         const twoDaysInSeconds = 2 * oneDayInSeconds;
28         const longLivedCookieMaxAge = { name : "longLivedCookieMaxAge", lifetime : "Max-Age=" + twoDaysInSeconds + ";" };
29         document.cookie = longLivedCookieMaxAge.name + "=foobar; " + longLivedCookieMaxAge.lifetime + " path=/";
30
31         const twoDaysAsExpiresDate = createExpiresDateFromMaxAge(twoDaysInSeconds);
32         const longLivedCookieExpires = { name : "longLivedCookieExpires", lifetime : "Expires=" + twoDaysAsExpiresDate + ";" };
33         document.cookie = longLivedCookieExpires.name + "=foobar; " + longLivedCookieExpires.lifetime + " path=/";
34
35         const overOneDayInSeconds = oneDayInSeconds + 30;
36         if (internals) {
37             let cookies = internals.getCookies();
38             if (!cookies.length)
39                 testFailed("No cookies found.");
40             for (let cookie of cookies) {
41                 switch (cookie.name) {
42                     case longLivedCookieMaxAge.name:
43                         checkThatCookieDoesExpireAfter(cookie, overOneDayInSeconds);
44                         break;
45                     case longLivedCookieExpires.name:
46                         checkThatCookieDoesExpireAfter(cookie, overOneDayInSeconds);
47                         break;
48                 }
49             }
50
51             resetCookiesForCurrentOrigin();
52
53             if (passedTests === 2) {
54                 testPassed("The two long-lived cookies expire after more than " + overOneDayInSeconds + " seconds.");
55             } else
56                 testFailed("At least one cookie's expiry attribute was below the test thresholds.");
57         } else
58             testFailed("No internals object.");
59             
60         setEnableFeature(false, finishJSTest);
61     }
62
63     function navigateCrossOrigin() {
64         document.location.href = destinationOrigin + "/resourceLoadStatistics/capped-lifetime-for-cookie-set-in-js-without-link-decoration-from-prevalent-resource.html";
65     }
66
67     const destinationOrigin = "http://localhost:8000";
68     const prevalentResourceOrigin = "http://127.0.0.1:8000";
69     function runTest() {
70         if (document.location.origin === prevalentResourceOrigin) {
71             testRunner.setUseITPDatabase(true);
72             setEnableFeature(true, function () {
73                 testRunner.setStatisticsPrevalentResource(prevalentResourceOrigin, true, function() {
74                     if (!testRunner.isStatisticsPrevalentResource(prevalentResourceOrigin))
75                         testFailed("Host did not get set as prevalent resource.");
76                     testRunner.statisticsUpdateCookieBlocking(navigateCrossOrigin);
77                 });
78
79             });
80         } else {
81             testCookies();
82         }
83     }
84 </script>
85 </body>
86 </html>