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.
9 var test_generator
= do_run_test();
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
);
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
);
80 Ci
.nsICookie
.SAMESITE_NONE
,
81 Ci
.nsICookie
.SCHEME_HTTPS
83 do_timeout(2100, continue_test
);
86 Assert
.equal(countCookies("captchart.com", "captchart.com"), 50);
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(
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
138 for (let cookie
of Services
.cookies
.cookies
) {
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
;
150 Services
.cookies
.countCookiesFromHost(aBaseDomain
),
153 Assert
.equal(Services
.cookies
.countCookiesFromHost(aHost
), cookies
.length
);
155 for (let cookie
of Services
.cookies
.getCookiesFromHost(aHost
, {})) {
157 cookie
.host
.length
>= aBaseDomain
.length
&&
158 cookie
.host
.slice(cookie
.host
.length
- aBaseDomain
.length
) == aBaseDomain
161 for (let i
= 0; i
< cookies
.length
; ++i
) {
162 if (cookies
[i
].host
== cookie
.host
&& cookies
[i
].name
== cookie
.name
) {
164 cookies
.splice(i
, 1);
170 do_throw("cookie " + cookie
.name
+ " not found in master cookies");
174 "cookie host " + cookie
.host
+ " not within domain " + aBaseDomain
179 Assert
.equal(cookies
.length
, 0);