extensions/cookie/test/unit/test_schema_3_migration.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/cookie/test/unit/test_schema_3_migration.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +// Test cookie database migration from version 3 (prerelease Gecko 2.0) to the
     1.8 +// current version, presently 4 (Gecko 2.0).
     1.9 +
    1.10 +let test_generator = do_run_test();
    1.11 +
    1.12 +function run_test() {
    1.13 +  do_test_pending();
    1.14 +  test_generator.next();
    1.15 +}
    1.16 +
    1.17 +function finish_test() {
    1.18 +  do_execute_soon(function() {
    1.19 +    test_generator.close();
    1.20 +    do_test_finished();
    1.21 +  });
    1.22 +}
    1.23 +
    1.24 +function do_run_test() {
    1.25 +  // Set up a profile.
    1.26 +  let profile = do_get_profile();
    1.27 +
    1.28 +  // Create a schema 3 database.
    1.29 +  let schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3);
    1.30 +
    1.31 +  let now = Date.now() * 1000;
    1.32 +  let futureExpiry = Math.round(now / 1e6 + 1000);
    1.33 +  let pastExpiry = Math.round(now / 1e6 - 1000);
    1.34 +
    1.35 +  // Populate it, with:
    1.36 +  // 1) Unexpired, unique cookies.
    1.37 +  for (let i = 0; i < 20; ++i) {
    1.38 +    let cookie = new Cookie("oh" + i, "hai", "foo.com", "/",
    1.39 +                            futureExpiry, now, now + i, false, false, false);
    1.40 +
    1.41 +    schema3db.insertCookie(cookie);
    1.42 +  }
    1.43 +
    1.44 +  // 2) Expired, unique cookies.
    1.45 +  for (let i = 20; i < 40; ++i) {
    1.46 +    let cookie = new Cookie("oh" + i, "hai", "bar.com", "/",
    1.47 +                            pastExpiry, now, now + i, false, false, false);
    1.48 +
    1.49 +    schema3db.insertCookie(cookie);
    1.50 +  }
    1.51 +
    1.52 +  // 3) Many copies of the same cookie, some of which have expired and
    1.53 +  // some of which have not.
    1.54 +  for (let i = 40; i < 45; ++i) {
    1.55 +    let cookie = new Cookie("oh", "hai", "baz.com", "/",
    1.56 +                            futureExpiry + i, now, now + i, false, false, false);
    1.57 +
    1.58 +    schema3db.insertCookie(cookie);
    1.59 +  }
    1.60 +  for (let i = 45; i < 50; ++i) {
    1.61 +    let cookie = new Cookie("oh", "hai", "baz.com", "/",
    1.62 +                            pastExpiry - i, now, now + i, false, false, false);
    1.63 +
    1.64 +    schema3db.insertCookie(cookie);
    1.65 +  }
    1.66 +  for (let i = 50; i < 55; ++i) {
    1.67 +    let cookie = new Cookie("oh", "hai", "baz.com", "/",
    1.68 +                            futureExpiry - i, now, now + i, false, false, false);
    1.69 +
    1.70 +    schema3db.insertCookie(cookie);
    1.71 +  }
    1.72 +  for (let i = 55; i < 60; ++i) {
    1.73 +    let cookie = new Cookie("oh", "hai", "baz.com", "/",
    1.74 +                            pastExpiry + i, now, now + i, false, false, false);
    1.75 +
    1.76 +    schema3db.insertCookie(cookie);
    1.77 +  }
    1.78 +
    1.79 +  // Close it.
    1.80 +  schema3db.close();
    1.81 +  schema3db = null;
    1.82 +
    1.83 +  // Load the database, forcing migration to the current schema version. Then
    1.84 +  // test the expected set of cookies:
    1.85 +  // 1) All unexpired, unique cookies exist.
    1.86 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20);
    1.87 +
    1.88 +  // 2) All expired, unique cookies exist.
    1.89 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20);
    1.90 +
    1.91 +  // 3) Only one cookie remains, and it's the one with the highest expiration
    1.92 +  // time.
    1.93 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 1);
    1.94 +  let enumerator = Services.cookiemgr.getCookiesFromHost("baz.com");
    1.95 +  let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
    1.96 +  do_check_eq(cookie.expiry, futureExpiry + 44);
    1.97 +
    1.98 +  do_close_profile(test_generator);
    1.99 +  yield;
   1.100 +
   1.101 +  // Open the database so we can execute some more schema 3 statements on it.
   1.102 +  schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3);
   1.103 +
   1.104 +  // Populate it with more cookies.
   1.105 +  for (let i = 60; i < 80; ++i) {
   1.106 +    let cookie = new Cookie("oh" + i, "hai", "cat.com", "/",
   1.107 +                            futureExpiry, now, now + i, false, false, false);
   1.108 +
   1.109 +    schema3db.insertCookie(cookie);
   1.110 +  }
   1.111 +
   1.112 +  // Close it.
   1.113 +  schema3db.close();
   1.114 +  schema3db = null;
   1.115 +
   1.116 +  // Load the database. The cookies added immediately prior will have a NULL
   1.117 +  // creationTime column.
   1.118 +  do_load_profile();
   1.119 +
   1.120 +  // Test the expected set of cookies.
   1.121 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 20);
   1.122 +  let enumerator = Services.cookiemgr.getCookiesFromHost("cat.com");
   1.123 +  let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
   1.124 +  do_check_eq(cookie.creationTime, 0);
   1.125 +
   1.126 +  finish_test();
   1.127 +}
   1.128 +

mercurial