tor-browser

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

test_cookies_purge_counting.js (2230B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 add_setup(function test_setup() {
      5  // FOG needs a profile directory to put its data in.
      6  do_get_profile();
      7 
      8  // FOG needs to be initialized in order for data to flow.
      9  Services.fog.initializeFOG();
     10 });
     11 
     12 add_task(async function test_purge_counting() {
     13  let profile = do_get_profile();
     14  let dbFile = do_get_cookie_file(profile);
     15  Assert.ok(!dbFile.exists());
     16 
     17  let schema12db = new CookieDatabaseConnection(dbFile, 12);
     18 
     19  let now = Date.now() * 1000; // date in microseconds
     20  let pastExpiry = Math.round(now / 1e6 - 1000);
     21  let futureExpiry = Math.round(now / 1e6 + 1000);
     22 
     23  let manyHosts = 50;
     24  let manyCookies = 140;
     25  let totalCookies = manyHosts * manyCookies;
     26 
     27  // add many expired cookies for each host
     28  for (let hostNum = 0; hostNum < manyHosts; hostNum++) {
     29    let host = "cookie-host" + hostNum + ".com";
     30    for (let i = 0; i < manyCookies; i++) {
     31      let cookie = new Cookie(
     32        "cookie-name" + i,
     33        "cookie-value" + i,
     34        host,
     35        "/", // path
     36        pastExpiry,
     37        pastExpiry, // needed to get the cookie by the db init
     38        now,
     39        false,
     40        false,
     41        false
     42      );
     43      schema12db.insertCookie(cookie);
     44    }
     45  }
     46 
     47  let validCookies = Services.cookies.cookies.length; // includes expired cookies
     48  Assert.equal(validCookies, totalCookies);
     49 
     50  // add a valid cookie - this triggers the purge
     51  const cv = Services.cookies.add(
     52    "cookie-host0.com", // any host
     53    "/", // path
     54    "cookie-name-x",
     55    "cookie-value-x",
     56    false, // secure
     57    true, // http-only
     58    true, // isSession
     59    futureExpiry * 1000,
     60    {}, // OA
     61    Ci.nsICookie.SAMESITE_UNSET, // SameSite
     62    Ci.nsICookie.SCHEME_HTTPS
     63  );
     64  Assert.equal(cv.result, Ci.nsICookieValidation.eOK, "Valid cookie");
     65 
     66  // check that we purge all the expired cookies and not the unexpired
     67  validCookies = Services.cookies.cookies.length;
     68  Assert.equal(validCookies, 1);
     69 
     70  // check that the telemetry fired
     71  let cpm = await Glean.networking.cookiePurgeMax.testGetValue();
     72  Assert.equal(cpm.sum, totalCookies, "Purge the expected number of cookies");
     73 
     74  schema12db.close();
     75 });