extensions/cookie/test/unit/test_schema_2_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_2_migration.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,207 @@
     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 2 (Gecko 1.9.3) to the current
     1.8 +// 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 2 database.
    1.29 +  let schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
    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 +    schema2db.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 +    schema2db.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 +    schema2db.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 +    schema2db.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 +    schema2db.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 +    schema2db.insertCookie(cookie);
    1.77 +  }
    1.78 +
    1.79 +  // Close it.
    1.80 +  schema2db.close();
    1.81 +  schema2db = 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 2 statements on it.
   1.102 +  schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
   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", "foo.com", "/",
   1.107 +                            futureExpiry, now, now + i, false, false, false);
   1.108 +
   1.109 +    schema2db.insertCookie(cookie);
   1.110 +  }
   1.111 +  for (let i = 80; i < 100; ++i) {
   1.112 +    let cookie = new Cookie("oh" + i, "hai", "cat.com", "/",
   1.113 +                            futureExpiry, now, now + i, false, false, false);
   1.114 +
   1.115 +    schema2db.insertCookie(cookie);
   1.116 +  }
   1.117 +
   1.118 +  // Attempt to add a cookie with the same (name, host, path) values as another
   1.119 +  // cookie. This should succeed since we have a REPLACE clause for conflict on
   1.120 +  // the unique index.
   1.121 +  let cookie = new Cookie("oh", "hai", "baz.com", "/",
   1.122 +                          futureExpiry, now, now + 100, false, false, false);
   1.123 +
   1.124 +  schema2db.insertCookie(cookie);
   1.125 +
   1.126 +  // Check that there is, indeed, a singular cookie for baz.com.
   1.127 +  do_check_eq(do_count_cookies_in_db(schema2db.db, "baz.com"), 1);
   1.128 +
   1.129 +  // Close it.
   1.130 +  schema2db.close();
   1.131 +  schema2db = null;
   1.132 +
   1.133 +  // Back up the database, so we can test both asynchronous and synchronous
   1.134 +  // loading separately.
   1.135 +  let file = do_get_cookie_file(profile);
   1.136 +  let copy = profile.clone();
   1.137 +  copy.append("cookies.sqlite.copy");
   1.138 +  file.copyTo(null, copy.leafName);
   1.139 +
   1.140 +  // Load the database asynchronously, forcing a purge of the newly-added
   1.141 +  // cookies. (Their baseDomain column will be NULL.)
   1.142 +  do_load_profile(test_generator);
   1.143 +  yield;
   1.144 +
   1.145 +  // Test the expected set of cookies.
   1.146 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20);
   1.147 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20);
   1.148 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0);
   1.149 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0);
   1.150 +
   1.151 +  do_close_profile(test_generator);
   1.152 +  yield;
   1.153 +
   1.154 +  // Open the database and prove that they were deleted.
   1.155 +  schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
   1.156 +  do_check_eq(do_count_cookies_in_db(schema2db.db), 40);
   1.157 +  do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20);
   1.158 +  do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20);
   1.159 +  schema2db.close();
   1.160 +
   1.161 +  // Copy the database back.
   1.162 +  file.remove(false);
   1.163 +  copy.copyTo(null, file.leafName);
   1.164 +
   1.165 +  // Load the database host-at-a-time.
   1.166 +  do_load_profile();
   1.167 +
   1.168 +  // Test the expected set of cookies.
   1.169 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20);
   1.170 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20);
   1.171 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0);
   1.172 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0);
   1.173 +
   1.174 +  do_close_profile(test_generator);
   1.175 +  yield;
   1.176 +
   1.177 +  // Open the database and prove that they were deleted.
   1.178 +  schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
   1.179 +  do_check_eq(do_count_cookies_in_db(schema2db.db), 40);
   1.180 +  do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20);
   1.181 +  do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20);
   1.182 +  schema2db.close();
   1.183 +
   1.184 +  // Copy the database back.
   1.185 +  file.remove(false);
   1.186 +  copy.copyTo(null, file.leafName);
   1.187 +
   1.188 +  // Load the database synchronously, in its entirety.
   1.189 +  do_load_profile();
   1.190 +  do_check_eq(do_count_cookies(), 40);
   1.191 +
   1.192 +  // Test the expected set of cookies.
   1.193 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20);
   1.194 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20);
   1.195 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0);
   1.196 +  do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0);
   1.197 +
   1.198 +  do_close_profile(test_generator);
   1.199 +  yield;
   1.200 +
   1.201 +  // Open the database and prove that they were deleted.
   1.202 +  schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
   1.203 +  do_check_eq(do_count_cookies_in_db(schema2db.db), 40);
   1.204 +  do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20);
   1.205 +  do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20);
   1.206 +  schema2db.close();
   1.207 +
   1.208 +  finish_test();
   1.209 +}
   1.210 +

mercurial