tor-browser

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

test_cookies_read.js (4068B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // test cookie database asynchronous read operation.
      5 
      6 "use strict";
      7 
      8 var CMAX = 1000; // # of cookies to create
      9 
     10 add_task(async () => {
     11  // Set up a profile.
     12  let profile = do_get_profile();
     13 
     14  // Allow all cookies.
     15  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
     16  Services.prefs.setBoolPref(
     17    "network.cookieJarSettings.unblocked_for_testing",
     18    true
     19  );
     20 
     21  // Bug 1617611 - Fix all the tests broken by "cookies SameSite=Lax by default"
     22  Services.prefs.setBoolPref("network.cookie.sameSite.laxByDefault", false);
     23 
     24  // Start the cookieservice, to force creation of a database.
     25  // Get the sessionCookies to join the initialization in cookie thread
     26  Services.cookies.sessionCookies;
     27 
     28  // Open a database connection now, after synchronous initialization has
     29  // completed. We may not be able to open one later once asynchronous writing
     30  // begins.
     31  Assert.ok(do_get_cookie_file(profile).exists());
     32 
     33  // Close the profile.
     34  await promise_close_profile();
     35 
     36  // Remove the cookie file in order to create another database file.
     37  do_get_cookie_file(profile).remove(false);
     38 
     39  let db = new CookieDatabaseConnection(do_get_cookie_file(profile), 12);
     40 
     41  // Reload profile.
     42  await promise_load_profile();
     43 
     44  let uri = NetUtil.newURI("http://foo.com/");
     45  let channel = NetUtil.newChannel({
     46    uri,
     47    loadUsingSystemPrincipal: true,
     48    contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
     49  });
     50  for (let i = 0; i < CMAX; ++i) {
     51    uri = NetUtil.newURI("http://" + i + ".com/");
     52    Services.cookies.setCookieStringFromHttp(
     53      uri,
     54      "oh=hai; max-age=1000",
     55      channel
     56    );
     57  }
     58 
     59  Assert.equal(do_count_cookies(), CMAX);
     60 
     61  // Wait until all CMAX cookies have been written out to the database.
     62  while (do_count_cookies_in_db(db.db) < CMAX) {
     63    await new Promise(resolve => executeSoon(resolve));
     64  }
     65 
     66  // Check the WAL file size. We set it to 16 pages of 32k, which means it
     67  // should be around 500k.
     68  let file = db.db.databaseFile;
     69  Assert.ok(file.exists());
     70  Assert.less(file.fileSize, 1e6);
     71  db.close();
     72 
     73  // fake a profile change
     74  await promise_close_profile();
     75  do_load_profile();
     76 
     77  // test a few random cookies
     78  Assert.equal(Services.cookies.countCookiesFromHost("999.com"), 1);
     79  Assert.equal(Services.cookies.countCookiesFromHost("abc.com"), 0);
     80  Assert.equal(Services.cookies.countCookiesFromHost("100.com"), 1);
     81  Assert.equal(Services.cookies.countCookiesFromHost("400.com"), 1);
     82  Assert.equal(Services.cookies.countCookiesFromHost("xyz.com"), 0);
     83 
     84  // force synchronous load of everything
     85  Assert.equal(do_count_cookies(), CMAX);
     86 
     87  // check that everything's precisely correct
     88  for (let i = 0; i < CMAX; ++i) {
     89    let host = i.toString() + ".com";
     90    Assert.equal(Services.cookies.countCookiesFromHost(host), 1);
     91  }
     92 
     93  // reload again, to make sure the additions were written correctly
     94  await promise_close_profile();
     95  do_load_profile();
     96 
     97  // remove some of the cookies, in both reverse and forward order
     98  for (let i = 100; i-- > 0; ) {
     99    let host = i.toString() + ".com";
    100    Services.cookies.remove(host, "oh", "/", {});
    101  }
    102  for (let i = CMAX - 100; i < CMAX; ++i) {
    103    let host = i.toString() + ".com";
    104    Services.cookies.remove(host, "oh", "/", {});
    105  }
    106 
    107  // check the count
    108  Assert.equal(do_count_cookies(), CMAX - 200);
    109 
    110  // reload again, to make sure the removals were written correctly
    111  await promise_close_profile();
    112  do_load_profile();
    113 
    114  // check the count
    115  Assert.equal(do_count_cookies(), CMAX - 200);
    116 
    117  // reload again, but wait for async read completion
    118  await promise_close_profile();
    119  await promise_load_profile();
    120 
    121  // check that everything's precisely correct
    122  Assert.equal(do_count_cookies(), CMAX - 200);
    123  for (let i = 100; i < CMAX - 100; ++i) {
    124    let host = i.toString() + ".com";
    125    Assert.equal(Services.cookies.countCookiesFromHost(host), 1);
    126  }
    127 
    128  Services.prefs.clearUserPref("network.cookie.sameSite.laxByDefault");
    129 });