extensions/cookie/test/unit/test_schema_2_migration.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 // Test cookie database migration from version 2 (Gecko 1.9.3) to the current
     5 // version, presently 4 (Gecko 2.0).
     7 let test_generator = do_run_test();
     9 function run_test() {
    10   do_test_pending();
    11   test_generator.next();
    12 }
    14 function finish_test() {
    15   do_execute_soon(function() {
    16     test_generator.close();
    17     do_test_finished();
    18   });
    19 }
    21 function do_run_test() {
    22   // Set up a profile.
    23   let profile = do_get_profile();
    25   // Create a schema 2 database.
    26   let schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
    28   let now = Date.now() * 1000;
    29   let futureExpiry = Math.round(now / 1e6 + 1000);
    30   let pastExpiry = Math.round(now / 1e6 - 1000);
    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);
    38     schema2db.insertCookie(cookie);
    39   }
    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);
    46     schema2db.insertCookie(cookie);
    47   }
    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);
    55     schema2db.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);
    61     schema2db.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);
    67     schema2db.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);
    73     schema2db.insertCookie(cookie);
    74   }
    76   // Close it.
    77   schema2db.close();
    78   schema2db = null;
    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);
    85   // 2) All expired, unique cookies exist.
    86   do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20);
    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);
    95   do_close_profile(test_generator);
    96   yield;
    98   // Open the database so we can execute some more schema 2 statements on it.
    99   schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
   101   // Populate it with more cookies.
   102   for (let i = 60; i < 80; ++i) {
   103     let cookie = new Cookie("oh" + i, "hai", "foo.com", "/",
   104                             futureExpiry, now, now + i, false, false, false);
   106     schema2db.insertCookie(cookie);
   107   }
   108   for (let i = 80; i < 100; ++i) {
   109     let cookie = new Cookie("oh" + i, "hai", "cat.com", "/",
   110                             futureExpiry, now, now + i, false, false, false);
   112     schema2db.insertCookie(cookie);
   113   }
   115   // Attempt to add a cookie with the same (name, host, path) values as another
   116   // cookie. This should succeed since we have a REPLACE clause for conflict on
   117   // the unique index.
   118   let cookie = new Cookie("oh", "hai", "baz.com", "/",
   119                           futureExpiry, now, now + 100, false, false, false);
   121   schema2db.insertCookie(cookie);
   123   // Check that there is, indeed, a singular cookie for baz.com.
   124   do_check_eq(do_count_cookies_in_db(schema2db.db, "baz.com"), 1);
   126   // Close it.
   127   schema2db.close();
   128   schema2db = null;
   130   // Back up the database, so we can test both asynchronous and synchronous
   131   // loading separately.
   132   let file = do_get_cookie_file(profile);
   133   let copy = profile.clone();
   134   copy.append("cookies.sqlite.copy");
   135   file.copyTo(null, copy.leafName);
   137   // Load the database asynchronously, forcing a purge of the newly-added
   138   // cookies. (Their baseDomain column will be NULL.)
   139   do_load_profile(test_generator);
   140   yield;
   142   // Test the expected set of cookies.
   143   do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20);
   144   do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20);
   145   do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0);
   146   do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0);
   148   do_close_profile(test_generator);
   149   yield;
   151   // Open the database and prove that they were deleted.
   152   schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
   153   do_check_eq(do_count_cookies_in_db(schema2db.db), 40);
   154   do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20);
   155   do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20);
   156   schema2db.close();
   158   // Copy the database back.
   159   file.remove(false);
   160   copy.copyTo(null, file.leafName);
   162   // Load the database host-at-a-time.
   163   do_load_profile();
   165   // Test the expected set of cookies.
   166   do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20);
   167   do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20);
   168   do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0);
   169   do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0);
   171   do_close_profile(test_generator);
   172   yield;
   174   // Open the database and prove that they were deleted.
   175   schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
   176   do_check_eq(do_count_cookies_in_db(schema2db.db), 40);
   177   do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20);
   178   do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20);
   179   schema2db.close();
   181   // Copy the database back.
   182   file.remove(false);
   183   copy.copyTo(null, file.leafName);
   185   // Load the database synchronously, in its entirety.
   186   do_load_profile();
   187   do_check_eq(do_count_cookies(), 40);
   189   // Test the expected set of cookies.
   190   do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20);
   191   do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20);
   192   do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0);
   193   do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0);
   195   do_close_profile(test_generator);
   196   yield;
   198   // Open the database and prove that they were deleted.
   199   schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
   200   do_check_eq(do_count_cookies_in_db(schema2db.db), 40);
   201   do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20);
   202   do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20);
   203   schema2db.close();
   205   finish_test();
   206 }

mercurial