Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | // Test that domain eviction occurs when the cookies per base domain limit is |
michael@0 | 5 | // reached, and that expired cookies are evicted before live cookies. |
michael@0 | 6 | |
michael@0 | 7 | let test_generator = do_run_test(); |
michael@0 | 8 | |
michael@0 | 9 | function run_test() |
michael@0 | 10 | { |
michael@0 | 11 | do_test_pending(); |
michael@0 | 12 | do_run_generator(test_generator); |
michael@0 | 13 | } |
michael@0 | 14 | |
michael@0 | 15 | function continue_test() |
michael@0 | 16 | { |
michael@0 | 17 | do_run_generator(test_generator); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | function do_run_test() |
michael@0 | 21 | { |
michael@0 | 22 | // Set the base domain limit to 50 so we have a known value. |
michael@0 | 23 | Services.prefs.setIntPref("network.cookie.maxPerHost", 50); |
michael@0 | 24 | |
michael@0 | 25 | let futureExpiry = Math.floor(Date.now() / 1000 + 1000); |
michael@0 | 26 | |
michael@0 | 27 | // test eviction under the 50 cookies per base domain limit. this means |
michael@0 | 28 | // that cookies for foo.com and bar.foo.com should count toward this limit, |
michael@0 | 29 | // while cookies for baz.com should not. there are several tests we perform |
michael@0 | 30 | // to make sure the base domain logic is working correctly. |
michael@0 | 31 | |
michael@0 | 32 | // 1) simplest case: set 100 cookies for "foo.bar" and make sure 50 survive. |
michael@0 | 33 | setCookies("foo.bar", 100, futureExpiry); |
michael@0 | 34 | do_check_eq(countCookies("foo.bar", "foo.bar"), 50); |
michael@0 | 35 | |
michael@0 | 36 | // 2) set cookies for different subdomains of "foo.baz", and an unrelated |
michael@0 | 37 | // domain, and make sure all 50 within the "foo.baz" base domain are counted. |
michael@0 | 38 | setCookies("foo.baz", 10, futureExpiry); |
michael@0 | 39 | setCookies(".foo.baz", 10, futureExpiry); |
michael@0 | 40 | setCookies("bar.foo.baz", 10, futureExpiry); |
michael@0 | 41 | setCookies("baz.bar.foo.baz", 10, futureExpiry); |
michael@0 | 42 | setCookies("unrelated.domain", 50, futureExpiry); |
michael@0 | 43 | do_check_eq(countCookies("foo.baz", "baz.bar.foo.baz"), 40); |
michael@0 | 44 | setCookies("foo.baz", 20, futureExpiry); |
michael@0 | 45 | do_check_eq(countCookies("foo.baz", "baz.bar.foo.baz"), 50); |
michael@0 | 46 | |
michael@0 | 47 | // 3) ensure cookies are evicted by order of lastAccessed time, if the |
michael@0 | 48 | // limit on cookies per base domain is reached. |
michael@0 | 49 | setCookies("horse.radish", 10, futureExpiry); |
michael@0 | 50 | |
michael@0 | 51 | // Wait a while, to make sure the first batch of cookies is older than |
michael@0 | 52 | // the second (timer resolution varies on different platforms). |
michael@0 | 53 | do_timeout(100, continue_test); |
michael@0 | 54 | yield; |
michael@0 | 55 | |
michael@0 | 56 | setCookies("tasty.horse.radish", 50, futureExpiry); |
michael@0 | 57 | do_check_eq(countCookies("horse.radish", "horse.radish"), 50); |
michael@0 | 58 | |
michael@0 | 59 | let enumerator = Services.cookiemgr.enumerator; |
michael@0 | 60 | while (enumerator.hasMoreElements()) { |
michael@0 | 61 | let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); |
michael@0 | 62 | |
michael@0 | 63 | if (cookie.host == "horse.radish") |
michael@0 | 64 | do_throw("cookies not evicted by lastAccessed order"); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | // Test that expired cookies for a domain are evicted before live ones. |
michael@0 | 68 | let shortExpiry = Math.floor(Date.now() / 1000 + 2); |
michael@0 | 69 | setCookies("captchart.com", 49, futureExpiry); |
michael@0 | 70 | Services.cookiemgr.add("captchart.com", "", "test100", "eviction", |
michael@0 | 71 | false, false, false, shortExpiry); |
michael@0 | 72 | do_timeout(2100, continue_test); |
michael@0 | 73 | yield; |
michael@0 | 74 | |
michael@0 | 75 | do_check_eq(countCookies("captchart.com", "captchart.com"), 50); |
michael@0 | 76 | Services.cookiemgr.add("captchart.com", "", "test200", "eviction", |
michael@0 | 77 | false, false, false, futureExpiry); |
michael@0 | 78 | do_check_eq(countCookies("captchart.com", "captchart.com"), 50); |
michael@0 | 79 | |
michael@0 | 80 | let enumerator = Services.cookiemgr.getCookiesFromHost("captchart.com"); |
michael@0 | 81 | while (enumerator.hasMoreElements()) { |
michael@0 | 82 | let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); |
michael@0 | 83 | do_check_true(cookie.expiry == futureExpiry); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | do_finish_generator_test(test_generator); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | // set 'aNumber' cookies with host 'aHost', with distinct names. |
michael@0 | 90 | function |
michael@0 | 91 | setCookies(aHost, aNumber, aExpiry) |
michael@0 | 92 | { |
michael@0 | 93 | for (let i = 0; i < aNumber; ++i) |
michael@0 | 94 | Services.cookiemgr.add(aHost, "", "test" + i, "eviction", |
michael@0 | 95 | false, false, false, aExpiry); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | // count how many cookies are within domain 'aBaseDomain', using three |
michael@0 | 99 | // independent interface methods on nsICookieManager2: |
michael@0 | 100 | // 1) 'enumerator', an enumerator of all cookies; |
michael@0 | 101 | // 2) 'countCookiesFromHost', which returns the number of cookies within the |
michael@0 | 102 | // base domain of 'aHost', |
michael@0 | 103 | // 3) 'getCookiesFromHost', which returns an enumerator of 2). |
michael@0 | 104 | function |
michael@0 | 105 | countCookies(aBaseDomain, aHost) |
michael@0 | 106 | { |
michael@0 | 107 | let enumerator = Services.cookiemgr.enumerator; |
michael@0 | 108 | |
michael@0 | 109 | // count how many cookies are within domain 'aBaseDomain' using the cookie |
michael@0 | 110 | // enumerator. |
michael@0 | 111 | let cookies = []; |
michael@0 | 112 | while (enumerator.hasMoreElements()) { |
michael@0 | 113 | let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); |
michael@0 | 114 | |
michael@0 | 115 | if (cookie.host.length >= aBaseDomain.length && |
michael@0 | 116 | cookie.host.slice(cookie.host.length - aBaseDomain.length) == aBaseDomain) |
michael@0 | 117 | cookies.push(cookie); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | // confirm the count using countCookiesFromHost and getCookiesFromHost. |
michael@0 | 121 | let result = cookies.length; |
michael@0 | 122 | do_check_eq(Services.cookiemgr.countCookiesFromHost(aBaseDomain), |
michael@0 | 123 | cookies.length); |
michael@0 | 124 | do_check_eq(Services.cookiemgr.countCookiesFromHost(aHost), cookies.length); |
michael@0 | 125 | |
michael@0 | 126 | enumerator = Services.cookiemgr.getCookiesFromHost(aHost); |
michael@0 | 127 | while (enumerator.hasMoreElements()) { |
michael@0 | 128 | let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); |
michael@0 | 129 | |
michael@0 | 130 | if (cookie.host.length >= aBaseDomain.length && |
michael@0 | 131 | cookie.host.slice(cookie.host.length - aBaseDomain.length) == aBaseDomain) { |
michael@0 | 132 | let found = false; |
michael@0 | 133 | for (let i = 0; i < cookies.length; ++i) { |
michael@0 | 134 | if (cookies[i].host == cookie.host && cookies[i].name == cookie.name) { |
michael@0 | 135 | found = true; |
michael@0 | 136 | cookies.splice(i, 1); |
michael@0 | 137 | break; |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | if (!found) |
michael@0 | 142 | do_throw("cookie " + cookie.name + " not found in master enumerator"); |
michael@0 | 143 | |
michael@0 | 144 | } else { |
michael@0 | 145 | do_throw("cookie host " + cookie.host + " not within domain " + aBaseDomain); |
michael@0 | 146 | } |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | do_check_eq(cookies.length, 0); |
michael@0 | 150 | |
michael@0 | 151 | return result; |
michael@0 | 152 | } |
michael@0 | 153 |