PageRenderTime 32ms CodeModel.GetById 8ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/extensions/cookie/test/unit/test_domain_eviction.js

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