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