e2ffd041ae32de28603b8191030ce8a305d2626b
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / capped-lifetime-for-cookie-set-in-js-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 </head>
7 <body>
8 <script>
9     description("Check that cookies created by JavaScript with max-age or expiry longer than a week get capped to a week.");
10     jsTestIsAsync = true;
11     testRunner.setUseITPDatabase(true);
12
13     if (internals)
14         internals.setResourceLoadStatisticsEnabled(true);
15
16     let passedTests = 0;
17     function checkThatCookieDoesNotExpireAfter(cookieData, maxAgeInSeconds) {
18         let now = new Date();
19         let maxExpiryDateInMilliseconds = now.getTime() + (maxAgeInSeconds * 1000);
20
21         if (maxExpiryDateInMilliseconds > cookieData["expires"])
22             ++passedTests;
23         else
24             testFailed("Cookie named " + cookieData["name"] + " expires in more than " + maxAgeInSeconds + " seconds.");
25     }
26
27     const twoDaysInSeconds = 2 * 24 * 60 * 60;
28     const shortLivedCookieMaxAge = { name : "shortLivedCookieMaxAge", lifetime : "Max-Age=" + twoDaysInSeconds + ";" };
29     document.cookie = shortLivedCookieMaxAge.name + "=foobar; " + shortLivedCookieMaxAge.lifetime + " path=/";
30
31     const twoDaysAsExpiresDate = createExpiresDateFromMaxAge(twoDaysInSeconds);
32     const shortLivedCookieExpires = { name : "shortLivedCookieExpires", lifetime : "Expires=" + twoDaysAsExpiresDate + ";" };
33     document.cookie = shortLivedCookieExpires.name + "=foobar; " + shortLivedCookieExpires.lifetime + " path=/";
34
35     const oneWeekInSeconds = 7 * 24 * 60 * 60;
36     const twoWeeksInSeconds = 2 * oneWeekInSeconds;
37     const longLivedCookieMaxAge = { name : "longLivedCookieMaxAge", lifetime : "Max-Age=" + twoWeeksInSeconds + ";" };
38     document.cookie = longLivedCookieMaxAge.name + "=foobar; " + longLivedCookieMaxAge.lifetime + " path=/";
39
40     const twoWeeksAsExpiresDate = createExpiresDateFromMaxAge(twoWeeksInSeconds);
41     const longLivedCookieExpires = { name : "longLivedCookieExpires", lifetime : "Expires=" + twoWeeksAsExpiresDate + ";" };
42     document.cookie = longLivedCookieExpires.name + "=foobar; " + longLivedCookieExpires.lifetime + " path=/";
43
44     const overTwoDaysInSeconds = twoDaysInSeconds + 30;
45     const overOneWeekInSeconds = oneWeekInSeconds + 30;
46     if (internals) {
47         let cookies = internals.getCookies();
48         if (!cookies.length)
49             testFailed("No cookies found.");
50         for (let cookie of cookies) {
51             switch (cookie.name) {
52                 case shortLivedCookieMaxAge.name:
53                     checkThatCookieDoesNotExpireAfter(cookie, overTwoDaysInSeconds);
54                     break;
55                 case shortLivedCookieExpires.name:
56                     checkThatCookieDoesNotExpireAfter(cookie, overTwoDaysInSeconds);
57                     break;
58                 case longLivedCookieMaxAge.name:
59                     checkThatCookieDoesNotExpireAfter(cookie, overOneWeekInSeconds);
60                     break;
61                 case longLivedCookieExpires.name:
62                     checkThatCookieDoesNotExpireAfter(cookie, overOneWeekInSeconds);
63                     break;
64             }
65         }
66
67         resetCookiesForCurrentOrigin();
68
69         if (passedTests === 4) {
70             testPassed("The two short-lived cookies don't expire after more than " + overTwoDaysInSeconds + " seconds.");
71             testPassed("The two long-lived cookies don't expire after more than " + overOneWeekInSeconds + " seconds.");
72         } else
73             testFailed("At least one cookie's expiry attribute was beyond the test thresholds.");
74     } else
75         testFailed("No internals object.");
76
77     if (internals)
78         internals.setResourceLoadStatisticsEnabled(false);
79
80     finishJSTest();
81 </script>
82 </body>
83 </html>