PageRenderTime 28ms CodeModel.GetById 8ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

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