tor-browser

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

test_schema_14_migration.js (2418B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test cookie database schema 14
      5 "use strict";
      6 
      7 add_task(async function test_schema_14_migration() {
      8  // Set up a profile.
      9  let profile = do_get_profile();
     10 
     11  // Start the cookieservice, to force creation of a database.
     12  Services.cookies.sessionCookies;
     13 
     14  // Close the profile.
     15  await promise_close_profile();
     16 
     17  // Remove the cookie file in order to create another database file.
     18  do_get_cookie_file(profile).remove(false);
     19 
     20  // Create a schema 13 database.
     21  let schema13db = new CookieDatabaseConnection(
     22    do_get_cookie_file(profile),
     23    13
     24  );
     25 
     26  let now = Math.round(Date.now() / 1000);
     27 
     28  let N = 20;
     29  // Populate db with N unexpired, unique cookies.
     30  for (let i = 0; i < N; ++i) {
     31    let cookie = new Cookie(
     32      "test" + i,
     33      "Some data",
     34      "foo.com",
     35      "/",
     36      now + (i % 2 ? 34560000 * 2 : 0),
     37      now,
     38      now,
     39      false,
     40      false,
     41      false,
     42      false,
     43      {},
     44      Ci.nsICookie.SAMESITE_UNSET,
     45      Ci.nsICookie.SCHEME_UNSET,
     46      !!(i % 2) // isPartitioned
     47    );
     48    schema13db.insertCookie(cookie);
     49  }
     50 
     51  schema13db.close();
     52  schema13db = null;
     53 
     54  // Check if we have the right entries
     55  {
     56    const dbConnection = Services.storage.openDatabase(
     57      do_get_cookie_file(profile)
     58    );
     59    const stmt = dbConnection.createStatement(
     60      "SELECT count(name) FROM moz_cookies WHERE expiry > unixepoch() + 34560000"
     61    );
     62    const success = stmt.executeStep();
     63    Assert.ok(success);
     64 
     65    const count = stmt.getInt32(0);
     66    Assert.equal(count, 10);
     67    stmt.finalize();
     68    dbConnection.close();
     69  }
     70 
     71  // Reload profile.
     72  await promise_load_profile();
     73 
     74  // Assert inserted cookies are in the db and correctly handled by services.
     75  Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), N);
     76 
     77  // Check if the time was reset
     78  {
     79    const dbConnection = Services.storage.openDatabase(
     80      do_get_cookie_file(profile)
     81    );
     82    const stmt = dbConnection.createStatement(
     83      "SELECT COUNT(name) FROM moz_cookies WHERE expiry <= (unixepoch() + 34560000) * 1000"
     84    );
     85    const success = stmt.executeStep();
     86    Assert.ok(success);
     87 
     88    const count = stmt.getInt32(0);
     89    Assert.equal(count, 20);
     90    stmt.finalize();
     91    dbConnection.close();
     92  }
     93 
     94  // Cleanup
     95  Services.cookies.removeAll();
     96  do_close_profile();
     97 });