Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_domain_eviction.js
blob58520c2daac205a323aa280bb61222ac41d8f914
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 // Test that domain eviction occurs when the cookies per base domain limit is
5 // reached, and that expired cookies are evicted before live cookies.
7 "use strict";
9 var test_generator = do_run_test();
11 function run_test() {
12 do_test_pending();
13 do_run_generator(test_generator);
16 function continue_test() {
17 do_run_generator(test_generator);
20 function* do_run_test() {
21 // Set quotaPerHost to maxPerHost - 1, so there is only one cookie
22 // will be evicted everytime.
23 Services.prefs.setIntPref("network.cookie.quotaPerHost", 49);
24 // Set the base domain limit to 50 so we have a known value.
25 Services.prefs.setIntPref("network.cookie.maxPerHost", 50);
27 let futureExpiry = Math.floor(Date.now() / 1000 + 1000);
29 // test eviction under the 50 cookies per base domain limit. this means
30 // that cookies for foo.com and bar.foo.com should count toward this limit,
31 // while cookies for baz.com should not. there are several tests we perform
32 // to make sure the base domain logic is working correctly.
34 // 1) simplest case: set 100 cookies for "foo.bar" and make sure 50 survive.
35 setCookies("foo.bar", 100, futureExpiry);
36 Assert.equal(countCookies("foo.bar", "foo.bar"), 50);
38 // 2) set cookies for different subdomains of "foo.baz", and an unrelated
39 // domain, and make sure all 50 within the "foo.baz" base domain are counted.
40 setCookies("foo.baz", 10, futureExpiry);
41 setCookies(".foo.baz", 10, futureExpiry);
42 setCookies("bar.foo.baz", 10, futureExpiry);
43 setCookies("baz.bar.foo.baz", 10, futureExpiry);
44 setCookies("unrelated.domain", 50, futureExpiry);
45 Assert.equal(countCookies("foo.baz", "baz.bar.foo.baz"), 40);
46 setCookies("foo.baz", 20, futureExpiry);
47 Assert.equal(countCookies("foo.baz", "baz.bar.foo.baz"), 50);
49 // 3) ensure cookies are evicted by order of lastAccessed time, if the
50 // limit on cookies per base domain is reached.
51 setCookies("horse.radish", 10, futureExpiry);
53 // Wait a while, to make sure the first batch of cookies is older than
54 // the second (timer resolution varies on different platforms).
55 do_timeout(100, continue_test);
56 yield;
58 setCookies("tasty.horse.radish", 50, futureExpiry);
59 Assert.equal(countCookies("horse.radish", "horse.radish"), 50);
61 for (let cookie of Services.cookies.cookies) {
62 if (cookie.host == "horse.radish") {
63 do_throw("cookies not evicted by lastAccessed order");
67 // Test that expired cookies for a domain are evicted before live ones.
68 let shortExpiry = Math.floor(Date.now() / 1000 + 2);
69 setCookies("captchart.com", 49, futureExpiry);
70 Services.cookies.add(
71 "captchart.com",
72 "",
73 "test100",
74 "eviction",
75 false,
76 false,
77 false,
78 shortExpiry,
79 {},
80 Ci.nsICookie.SAMESITE_NONE,
81 Ci.nsICookie.SCHEME_HTTPS
83 do_timeout(2100, continue_test);
84 yield;
86 Assert.equal(countCookies("captchart.com", "captchart.com"), 50);
87 Services.cookies.add(
88 "captchart.com",
89 "",
90 "test200",
91 "eviction",
92 false,
93 false,
94 false,
95 futureExpiry,
96 {},
97 Ci.nsICookie.SAMESITE_NONE,
98 Ci.nsICookie.SCHEME_HTTPS
100 Assert.equal(countCookies("captchart.com", "captchart.com"), 50);
102 for (let cookie of Services.cookies.getCookiesFromHost("captchart.com", {})) {
103 Assert.ok(cookie.expiry == futureExpiry);
106 do_finish_generator_test(test_generator);
109 // set 'aNumber' cookies with host 'aHost', with distinct names.
110 function setCookies(aHost, aNumber, aExpiry) {
111 for (let i = 0; i < aNumber; ++i) {
112 Services.cookies.add(
113 aHost,
115 "test" + i,
116 "eviction",
117 false,
118 false,
119 false,
120 aExpiry,
122 Ci.nsICookie.SAMESITE_NONE,
123 Ci.nsICookie.SCHEME_HTTPS
128 // count how many cookies are within domain 'aBaseDomain', using three
129 // independent interface methods on nsICookieManager:
130 // 1) 'cookies', an array of all cookies;
131 // 2) 'countCookiesFromHost', which returns the number of cookies within the
132 // base domain of 'aHost',
133 // 3) 'getCookiesFromHost', which returns an array of 2).
134 function countCookies(aBaseDomain, aHost) {
135 // count how many cookies are within domain 'aBaseDomain' using the cookies
136 // array.
137 let cookies = [];
138 for (let cookie of Services.cookies.cookies) {
139 if (
140 cookie.host.length >= aBaseDomain.length &&
141 cookie.host.slice(cookie.host.length - aBaseDomain.length) == aBaseDomain
143 cookies.push(cookie);
147 // confirm the count using countCookiesFromHost and getCookiesFromHost.
148 let result = cookies.length;
149 Assert.equal(
150 Services.cookies.countCookiesFromHost(aBaseDomain),
151 cookies.length
153 Assert.equal(Services.cookies.countCookiesFromHost(aHost), cookies.length);
155 for (let cookie of Services.cookies.getCookiesFromHost(aHost, {})) {
156 if (
157 cookie.host.length >= aBaseDomain.length &&
158 cookie.host.slice(cookie.host.length - aBaseDomain.length) == aBaseDomain
160 let found = false;
161 for (let i = 0; i < cookies.length; ++i) {
162 if (cookies[i].host == cookie.host && cookies[i].name == cookie.name) {
163 found = true;
164 cookies.splice(i, 1);
165 break;
169 if (!found) {
170 do_throw("cookie " + cookie.name + " not found in master cookies");
172 } else {
173 do_throw(
174 "cookie host " + cookie.host + " not within domain " + aBaseDomain
179 Assert.equal(cookies.length, 0);
181 return result;