tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_schema_3_migration.js (3616B)


      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 "use strict";
      7 
      8 var test_generator = do_run_test();
      9 
     10 function run_test() {
     11  do_test_pending();
     12  test_generator.next();
     13 }
     14 
     15 function finish_test() {
     16  executeSoon(function () {
     17    test_generator.return();
     18    do_test_finished();
     19  });
     20 }
     21 
     22 function* do_run_test() {
     23  // Set up a profile.
     24  let profile = do_get_profile();
     25 
     26  // Start the cookieservice, to force creation of a database.
     27  // Get the sessionCookies to join the initialization in cookie thread
     28  Services.cookies.sessionCookies;
     29 
     30  // Close the profile.
     31  do_close_profile(test_generator);
     32  yield;
     33 
     34  // Remove the cookie file in order to create another database file.
     35  do_get_cookie_file(profile).remove(false);
     36 
     37  // Create a schema 3 database.
     38  let schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3);
     39 
     40  let now = Date.now() * 1000;
     41  let futureExpiry = Math.round(now / 1e6 + 1000);
     42  let pastExpiry = Math.round(now / 1e6 - 1000);
     43 
     44  // Populate it, with:
     45  // 1) Unexpired, unique cookies.
     46  for (let i = 0; i < 20; ++i) {
     47    let cookie = new Cookie(
     48      "oh" + i,
     49      "hai",
     50      "foo.com",
     51      "/",
     52      futureExpiry,
     53      now,
     54      now + i,
     55      false,
     56      false,
     57      false
     58    );
     59 
     60    schema3db.insertCookie(cookie);
     61  }
     62 
     63  // 2) Expired, unique cookies.
     64  for (let i = 20; i < 40; ++i) {
     65    let cookie = new Cookie(
     66      "oh" + i,
     67      "hai",
     68      "bar.com",
     69      "/",
     70      pastExpiry,
     71      now,
     72      now + i,
     73      false,
     74      false,
     75      false
     76    );
     77 
     78    schema3db.insertCookie(cookie);
     79  }
     80 
     81  // 3) Many copies of the same cookie, some of which have expired and
     82  // some of which have not.
     83  for (let i = 40; i < 45; ++i) {
     84    let cookie = new Cookie(
     85      "oh",
     86      "hai",
     87      "baz.com",
     88      "/",
     89      futureExpiry + i,
     90      now,
     91      now + i,
     92      false,
     93      false,
     94      false
     95    );
     96 
     97    schema3db.insertCookie(cookie);
     98  }
     99  for (let i = 45; i < 50; ++i) {
    100    let cookie = new Cookie(
    101      "oh",
    102      "hai",
    103      "baz.com",
    104      "/",
    105      pastExpiry - i,
    106      now,
    107      now + i,
    108      false,
    109      false,
    110      false
    111    );
    112 
    113    schema3db.insertCookie(cookie);
    114  }
    115  for (let i = 50; i < 55; ++i) {
    116    let cookie = new Cookie(
    117      "oh",
    118      "hai",
    119      "baz.com",
    120      "/",
    121      futureExpiry - i,
    122      now,
    123      now + i,
    124      false,
    125      false,
    126      false
    127    );
    128 
    129    schema3db.insertCookie(cookie);
    130  }
    131  for (let i = 55; i < 60; ++i) {
    132    let cookie = new Cookie(
    133      "oh",
    134      "hai",
    135      "baz.com",
    136      "/",
    137      pastExpiry + i,
    138      now,
    139      now + i,
    140      false,
    141      false,
    142      false
    143    );
    144 
    145    schema3db.insertCookie(cookie);
    146  }
    147 
    148  // Close it.
    149  schema3db.close();
    150  schema3db = null;
    151 
    152  // Load the database, forcing migration to the current schema version. Then
    153  // test the expected set of cookies:
    154  do_load_profile();
    155 
    156  // 1) All unexpired, unique cookies exist.
    157  Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 20);
    158 
    159  // 2) All expired, unique cookies exist.
    160  Assert.equal(Services.cookies.countCookiesFromHost("bar.com"), 20);
    161 
    162  // 3) Only one cookie remains, and it's the one with the highest expiration
    163  // time.
    164  Assert.equal(Services.cookies.countCookiesFromHost("baz.com"), 1);
    165  let cookies = Services.cookies.getCookiesFromHost("baz.com", {});
    166  let cookie = cookies[0];
    167  Assert.equal(cookie.expiry, (futureExpiry + 44) * 1000);
    168 
    169  finish_test();
    170 }