tor-browser

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

test_schema_13_db.js (2601B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test cookie database schema 13
      5 "use strict";
      6 
      7 add_task(async function test_schema_13_db() {
      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  // Assert schema 13 cookie db was created
     15  Assert.ok(do_get_cookie_file(profile).exists());
     16  let dbConnection = Services.storage.openDatabase(do_get_cookie_file(profile));
     17  let version = dbConnection.schemaVersion;
     18  dbConnection.close();
     19  Assert.greaterOrEqual(version, 13);
     20 
     21  // Close the profile.
     22  await promise_close_profile();
     23 
     24  // Remove the cookie file in order to create another database file.
     25  do_get_cookie_file(profile).remove(false);
     26 
     27  // Open CookieDatabaseConnection to manipulate DB without using services.
     28  let schema13db = new CookieDatabaseConnection(
     29    do_get_cookie_file(profile),
     30    13
     31  );
     32 
     33  let now = Date.now() * 1000;
     34  let futureExpiry = Math.round(now / 1e6 + 1000);
     35 
     36  let N = 20;
     37  // Populate db with N unexpired, unique, partially partitioned cookies.
     38  for (let i = 0; i < N; ++i) {
     39    let cookie = new Cookie(
     40      "test" + i,
     41      "Some data",
     42      "foo.com",
     43      "/",
     44      futureExpiry,
     45      now,
     46      now,
     47      false,
     48      false,
     49      false,
     50      false,
     51      {},
     52      Ci.nsICookie.SAMESITE_UNSET,
     53      Ci.nsICookie.SCHEME_UNSET,
     54      !!(i % 2) // isPartitioned
     55    );
     56    schema13db.insertCookie(cookie);
     57  }
     58  schema13db.close();
     59  schema13db = null;
     60 
     61  // Reload profile.
     62  await promise_load_profile();
     63 
     64  // Assert inserted cookies are in the db and correctly handled by services.
     65  Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), N);
     66 
     67  // Open connection to manipulated db
     68  dbConnection = Services.storage.openDatabase(do_get_cookie_file(profile));
     69  // Check that schema is still correct after profile reload / db opening
     70  Assert.greaterOrEqual(dbConnection.schemaVersion, 13);
     71 
     72  // Count cookies with isPartitionedAttributeSet set to 1 (true)
     73  let stmt = dbConnection.createStatement(
     74    "SELECT COUNT(1) FROM moz_cookies WHERE isPartitionedAttributeSet = 1"
     75  );
     76  let success = stmt.executeStep();
     77  Assert.ok(success);
     78 
     79  // Assert the correct number of partitioned cookies was inserted / read
     80  let isPartitionedAttributeSetCount = stmt.getInt32(0);
     81  stmt.finalize();
     82  Assert.equal(isPartitionedAttributeSetCount, N / 2);
     83 
     84  // Cleanup
     85  Services.cookies.removeAll();
     86  stmt.finalize();
     87  dbConnection.close();
     88  do_close_profile();
     89 });