tor-browser

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

test_schema_12_migration.js (3754B)


      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 12 to the current version,
      5 // presently 13, which added the "partitioned" cookie attribute.
      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 12 database.
     38  let schema12db = new CookieDatabaseConnection(
     39    do_get_cookie_file(profile),
     40    12
     41  );
     42 
     43  let now = Date.now() * 1000;
     44  let futureExpiry = Math.round(now / 1e6 + 1000);
     45  let pastExpiry = Math.round(now / 1e6 - 1000);
     46 
     47  // Populate it, with:
     48  // 1) Unexpired, unique cookies.
     49  for (let i = 0; i < 20; ++i) {
     50    let cookie = new Cookie(
     51      "oh" + i,
     52      "hai",
     53      "foo.com",
     54      "/",
     55      futureExpiry,
     56      now,
     57      now,
     58      false,
     59      false,
     60      false
     61    );
     62 
     63    schema12db.insertCookie(cookie);
     64  }
     65 
     66  // 2) Expired, unique cookies.
     67  for (let i = 20; i < 40; ++i) {
     68    let cookie = new Cookie(
     69      "oh" + i,
     70      "hai",
     71      "bar.com",
     72      "/",
     73      pastExpiry,
     74      now,
     75      now,
     76      false,
     77      false,
     78      false
     79    );
     80 
     81    schema12db.insertCookie(cookie);
     82  }
     83 
     84  // 3) Many copies of the same cookie, some of which have expired and
     85  // some of which have not.
     86  for (let i = 40; i < 45; ++i) {
     87    let cookie = new Cookie(
     88      "oh",
     89      "hai",
     90      "baz.com",
     91      "/",
     92      futureExpiry + i,
     93      now,
     94      now,
     95      false,
     96      false,
     97      false
     98    );
     99 
    100    try {
    101      schema12db.insertCookie(cookie);
    102    } catch (e) {}
    103  }
    104  for (let i = 45; i < 50; ++i) {
    105    let cookie = new Cookie(
    106      "oh",
    107      "hai",
    108      "baz.com",
    109      "/",
    110      pastExpiry - i,
    111      now,
    112      now,
    113      false,
    114      false,
    115      false
    116    );
    117 
    118    try {
    119      schema12db.insertCookie(cookie);
    120    } catch (e) {}
    121  }
    122  for (let i = 50; i < 55; ++i) {
    123    let cookie = new Cookie(
    124      "oh",
    125      "hai",
    126      "baz.com",
    127      "/",
    128      futureExpiry - i,
    129      now,
    130      now,
    131      false,
    132      false,
    133      false
    134    );
    135 
    136    try {
    137      schema12db.insertCookie(cookie);
    138    } catch (e) {}
    139  }
    140  for (let i = 55; i < 60; ++i) {
    141    let cookie = new Cookie(
    142      "oh",
    143      "hai",
    144      "baz.com",
    145      "/",
    146      pastExpiry + i,
    147      now,
    148      now,
    149      false,
    150      false,
    151      false
    152    );
    153 
    154    try {
    155      schema12db.insertCookie(cookie);
    156    } catch (e) {}
    157  }
    158 
    159  // Close it.
    160  schema12db.close();
    161  schema12db = null;
    162 
    163  // Load the database, forcing migration to the current schema version. Then
    164  // test the expected set of cookies:
    165  do_load_profile();
    166 
    167  // 1) All unexpired, unique cookies exist.
    168  Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 20);
    169 
    170  // 2) All expired, unique cookies exist.
    171  Assert.equal(Services.cookies.countCookiesFromHost("bar.com"), 20);
    172 
    173  // 3) Only one cookie remains, and it's the one with the highest expiration
    174  // time.
    175  Assert.equal(Services.cookies.countCookiesFromHost("baz.com"), 1);
    176  let cookies = Services.cookies.getCookiesFromHost("baz.com", {});
    177  let cookie = cookies[0];
    178  Assert.equal(cookie.expiry, (futureExpiry + 40) * 1000);
    179 
    180  finish_test();
    181 }