/extensions/cookie/test/unit/test_schema_3_migration.js

http://github.com/zpao/v8monkey · JavaScript · 125 lines · 72 code · 29 blank · 24 comment · 7 complexity · 251c9e8ab67a4a0cd8d767e152759207 MD5 · raw file

  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. // Test cookie database migration from version 3 (prerelease Gecko 2.0) to the
  4. // current version, presently 4 (Gecko 2.0).
  5. let test_generator = do_run_test();
  6. function run_test() {
  7. do_test_pending();
  8. test_generator.next();
  9. }
  10. function finish_test() {
  11. do_execute_soon(function() {
  12. test_generator.close();
  13. do_test_finished();
  14. });
  15. }
  16. function do_run_test() {
  17. // Set up a profile.
  18. let profile = do_get_profile();
  19. // Create a schema 3 database.
  20. let schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3);
  21. let now = Date.now() * 1000;
  22. let futureExpiry = Math.round(now / 1e6 + 1000);
  23. let pastExpiry = Math.round(now / 1e6 - 1000);
  24. // Populate it, with:
  25. // 1) Unexpired, unique cookies.
  26. for (let i = 0; i < 20; ++i) {
  27. let cookie = new Cookie("oh" + i, "hai", "foo.com", "/",
  28. futureExpiry, now, now + i, false, false, false);
  29. schema3db.insertCookie(cookie);
  30. }
  31. // 2) Expired, unique cookies.
  32. for (let i = 20; i < 40; ++i) {
  33. let cookie = new Cookie("oh" + i, "hai", "bar.com", "/",
  34. pastExpiry, now, now + i, false, false, false);
  35. schema3db.insertCookie(cookie);
  36. }
  37. // 3) Many copies of the same cookie, some of which have expired and
  38. // some of which have not.
  39. for (let i = 40; i < 45; ++i) {
  40. let cookie = new Cookie("oh", "hai", "baz.com", "/",
  41. futureExpiry + i, now, now + i, false, false, false);
  42. schema3db.insertCookie(cookie);
  43. }
  44. for (let i = 45; i < 50; ++i) {
  45. let cookie = new Cookie("oh", "hai", "baz.com", "/",
  46. pastExpiry - i, now, now + i, false, false, false);
  47. schema3db.insertCookie(cookie);
  48. }
  49. for (let i = 50; i < 55; ++i) {
  50. let cookie = new Cookie("oh", "hai", "baz.com", "/",
  51. futureExpiry - i, now, now + i, false, false, false);
  52. schema3db.insertCookie(cookie);
  53. }
  54. for (let i = 55; i < 60; ++i) {
  55. let cookie = new Cookie("oh", "hai", "baz.com", "/",
  56. pastExpiry + i, now, now + i, false, false, false);
  57. schema3db.insertCookie(cookie);
  58. }
  59. // Close it.
  60. schema3db.close();
  61. schema3db = null;
  62. // Load the database, forcing migration to the current schema version. Then
  63. // test the expected set of cookies:
  64. // 1) All unexpired, unique cookies exist.
  65. do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20);
  66. // 2) All expired, unique cookies exist.
  67. do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20);
  68. // 3) Only one cookie remains, and it's the one with the highest expiration
  69. // time.
  70. do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 1);
  71. let enumerator = Services.cookiemgr.getCookiesFromHost("baz.com");
  72. let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
  73. do_check_eq(cookie.expiry, futureExpiry + 44);
  74. do_close_profile(test_generator);
  75. yield;
  76. // Open the database so we can execute some more schema 3 statements on it.
  77. schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3);
  78. // Populate it with more cookies.
  79. for (let i = 60; i < 80; ++i) {
  80. let cookie = new Cookie("oh" + i, "hai", "cat.com", "/",
  81. futureExpiry, now, now + i, false, false, false);
  82. schema3db.insertCookie(cookie);
  83. }
  84. // Close it.
  85. schema3db.close();
  86. schema3db = null;
  87. // Load the database. The cookies added immediately prior will have a NULL
  88. // creationTime column.
  89. do_load_profile();
  90. // Test the expected set of cookies.
  91. do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 20);
  92. let enumerator = Services.cookiemgr.getCookiesFromHost("cat.com");
  93. let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
  94. do_check_eq(cookie.creationTime, 0);
  95. finish_test();
  96. }