1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/cookie/test/unit/test_domain_eviction.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +// Test that domain eviction occurs when the cookies per base domain limit is 1.8 +// reached, and that expired cookies are evicted before live cookies. 1.9 + 1.10 +let test_generator = do_run_test(); 1.11 + 1.12 +function run_test() 1.13 +{ 1.14 + do_test_pending(); 1.15 + do_run_generator(test_generator); 1.16 +} 1.17 + 1.18 +function continue_test() 1.19 +{ 1.20 + do_run_generator(test_generator); 1.21 +} 1.22 + 1.23 +function do_run_test() 1.24 +{ 1.25 + // Set the base domain limit to 50 so we have a known value. 1.26 + Services.prefs.setIntPref("network.cookie.maxPerHost", 50); 1.27 + 1.28 + let futureExpiry = Math.floor(Date.now() / 1000 + 1000); 1.29 + 1.30 + // test eviction under the 50 cookies per base domain limit. this means 1.31 + // that cookies for foo.com and bar.foo.com should count toward this limit, 1.32 + // while cookies for baz.com should not. there are several tests we perform 1.33 + // to make sure the base domain logic is working correctly. 1.34 + 1.35 + // 1) simplest case: set 100 cookies for "foo.bar" and make sure 50 survive. 1.36 + setCookies("foo.bar", 100, futureExpiry); 1.37 + do_check_eq(countCookies("foo.bar", "foo.bar"), 50); 1.38 + 1.39 + // 2) set cookies for different subdomains of "foo.baz", and an unrelated 1.40 + // domain, and make sure all 50 within the "foo.baz" base domain are counted. 1.41 + setCookies("foo.baz", 10, futureExpiry); 1.42 + setCookies(".foo.baz", 10, futureExpiry); 1.43 + setCookies("bar.foo.baz", 10, futureExpiry); 1.44 + setCookies("baz.bar.foo.baz", 10, futureExpiry); 1.45 + setCookies("unrelated.domain", 50, futureExpiry); 1.46 + do_check_eq(countCookies("foo.baz", "baz.bar.foo.baz"), 40); 1.47 + setCookies("foo.baz", 20, futureExpiry); 1.48 + do_check_eq(countCookies("foo.baz", "baz.bar.foo.baz"), 50); 1.49 + 1.50 + // 3) ensure cookies are evicted by order of lastAccessed time, if the 1.51 + // limit on cookies per base domain is reached. 1.52 + setCookies("horse.radish", 10, futureExpiry); 1.53 + 1.54 + // Wait a while, to make sure the first batch of cookies is older than 1.55 + // the second (timer resolution varies on different platforms). 1.56 + do_timeout(100, continue_test); 1.57 + yield; 1.58 + 1.59 + setCookies("tasty.horse.radish", 50, futureExpiry); 1.60 + do_check_eq(countCookies("horse.radish", "horse.radish"), 50); 1.61 + 1.62 + let enumerator = Services.cookiemgr.enumerator; 1.63 + while (enumerator.hasMoreElements()) { 1.64 + let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); 1.65 + 1.66 + if (cookie.host == "horse.radish") 1.67 + do_throw("cookies not evicted by lastAccessed order"); 1.68 + } 1.69 + 1.70 + // Test that expired cookies for a domain are evicted before live ones. 1.71 + let shortExpiry = Math.floor(Date.now() / 1000 + 2); 1.72 + setCookies("captchart.com", 49, futureExpiry); 1.73 + Services.cookiemgr.add("captchart.com", "", "test100", "eviction", 1.74 + false, false, false, shortExpiry); 1.75 + do_timeout(2100, continue_test); 1.76 + yield; 1.77 + 1.78 + do_check_eq(countCookies("captchart.com", "captchart.com"), 50); 1.79 + Services.cookiemgr.add("captchart.com", "", "test200", "eviction", 1.80 + false, false, false, futureExpiry); 1.81 + do_check_eq(countCookies("captchart.com", "captchart.com"), 50); 1.82 + 1.83 + let enumerator = Services.cookiemgr.getCookiesFromHost("captchart.com"); 1.84 + while (enumerator.hasMoreElements()) { 1.85 + let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); 1.86 + do_check_true(cookie.expiry == futureExpiry); 1.87 + } 1.88 + 1.89 + do_finish_generator_test(test_generator); 1.90 +} 1.91 + 1.92 +// set 'aNumber' cookies with host 'aHost', with distinct names. 1.93 +function 1.94 +setCookies(aHost, aNumber, aExpiry) 1.95 +{ 1.96 + for (let i = 0; i < aNumber; ++i) 1.97 + Services.cookiemgr.add(aHost, "", "test" + i, "eviction", 1.98 + false, false, false, aExpiry); 1.99 +} 1.100 + 1.101 +// count how many cookies are within domain 'aBaseDomain', using three 1.102 +// independent interface methods on nsICookieManager2: 1.103 +// 1) 'enumerator', an enumerator of all cookies; 1.104 +// 2) 'countCookiesFromHost', which returns the number of cookies within the 1.105 +// base domain of 'aHost', 1.106 +// 3) 'getCookiesFromHost', which returns an enumerator of 2). 1.107 +function 1.108 +countCookies(aBaseDomain, aHost) 1.109 +{ 1.110 + let enumerator = Services.cookiemgr.enumerator; 1.111 + 1.112 + // count how many cookies are within domain 'aBaseDomain' using the cookie 1.113 + // enumerator. 1.114 + let cookies = []; 1.115 + while (enumerator.hasMoreElements()) { 1.116 + let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); 1.117 + 1.118 + if (cookie.host.length >= aBaseDomain.length && 1.119 + cookie.host.slice(cookie.host.length - aBaseDomain.length) == aBaseDomain) 1.120 + cookies.push(cookie); 1.121 + } 1.122 + 1.123 + // confirm the count using countCookiesFromHost and getCookiesFromHost. 1.124 + let result = cookies.length; 1.125 + do_check_eq(Services.cookiemgr.countCookiesFromHost(aBaseDomain), 1.126 + cookies.length); 1.127 + do_check_eq(Services.cookiemgr.countCookiesFromHost(aHost), cookies.length); 1.128 + 1.129 + enumerator = Services.cookiemgr.getCookiesFromHost(aHost); 1.130 + while (enumerator.hasMoreElements()) { 1.131 + let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); 1.132 + 1.133 + if (cookie.host.length >= aBaseDomain.length && 1.134 + cookie.host.slice(cookie.host.length - aBaseDomain.length) == aBaseDomain) { 1.135 + let found = false; 1.136 + for (let i = 0; i < cookies.length; ++i) { 1.137 + if (cookies[i].host == cookie.host && cookies[i].name == cookie.name) { 1.138 + found = true; 1.139 + cookies.splice(i, 1); 1.140 + break; 1.141 + } 1.142 + } 1.143 + 1.144 + if (!found) 1.145 + do_throw("cookie " + cookie.name + " not found in master enumerator"); 1.146 + 1.147 + } else { 1.148 + do_throw("cookie host " + cookie.host + " not within domain " + aBaseDomain); 1.149 + } 1.150 + } 1.151 + 1.152 + do_check_eq(cookies.length, 0); 1.153 + 1.154 + return result; 1.155 +} 1.156 +