tor-browser

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

test_Sanitizer_interrupted_v2.js (4096B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 do_get_profile();
      7 
      8 // Test that interrupted sanitizations are properly tracked.
      9 
     10 add_task(async function () {
     11  const { Sanitizer } = ChromeUtils.importESModule(
     12    "resource:///modules/Sanitizer.sys.mjs"
     13  );
     14 
     15  Services.prefs.setBoolPref(Sanitizer.PREF_NEWTAB_SEGREGATION, false);
     16 
     17  registerCleanupFunction(() => {
     18    Services.prefs.clearUserPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN);
     19    Services.prefs.clearUserPref(
     20      Sanitizer.PREF_SHUTDOWN_BRANCH + "cookiesAndStorage"
     21    );
     22    Services.prefs.clearUserPref(Sanitizer.PREF_NEWTAB_SEGREGATION);
     23  });
     24  Services.prefs.setBoolPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, true);
     25  Services.prefs.setBoolPref(
     26    Sanitizer.PREF_SHUTDOWN_BRANCH + "cookiesAndStorage",
     27    true
     28  );
     29 
     30  await Sanitizer.onStartup();
     31  Assert.ok(Sanitizer.shouldSanitizeOnShutdown, "Should sanitize on shutdown");
     32 
     33  let pendingSanitizations = JSON.parse(
     34    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
     35  );
     36  Assert.equal(
     37    pendingSanitizations.length,
     38    1,
     39    "Should have 1 pending sanitization"
     40  );
     41  Assert.equal(
     42    pendingSanitizations[0].id,
     43    "shutdown",
     44    "Should be the shutdown sanitization"
     45  );
     46  Assert.ok(
     47    pendingSanitizations[0].itemsToClear.includes("cookiesAndStorage"),
     48    "Pref has been setup"
     49  );
     50  Assert.ok(
     51    !pendingSanitizations[0].options.isShutdown,
     52    "Shutdown option is not present"
     53  );
     54 
     55  // Check the preference listeners.
     56  Services.prefs.setBoolPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, false);
     57  pendingSanitizations = JSON.parse(
     58    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
     59  );
     60  Assert.equal(
     61    pendingSanitizations.length,
     62    0,
     63    "Should not have pending sanitizations"
     64  );
     65  Assert.ok(
     66    !Sanitizer.shouldSanitizeOnShutdown,
     67    "Should not sanitize on shutdown"
     68  );
     69  Services.prefs.setBoolPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, true);
     70  pendingSanitizations = JSON.parse(
     71    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
     72  );
     73  Assert.equal(
     74    pendingSanitizations.length,
     75    1,
     76    "Should have 1 pending sanitization"
     77  );
     78  Assert.equal(
     79    pendingSanitizations[0].id,
     80    "shutdown",
     81    "Should be the shutdown sanitization"
     82  );
     83 
     84  Assert.ok(
     85    pendingSanitizations[0].itemsToClear.includes("cookiesAndStorage"),
     86    "Pending sanitizations should include cookiesAndStorage"
     87  );
     88  Services.prefs.setBoolPref(
     89    Sanitizer.PREF_SHUTDOWN_BRANCH + "cookiesAndStorage",
     90    false
     91  );
     92  pendingSanitizations = JSON.parse(
     93    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
     94  );
     95  Assert.equal(
     96    pendingSanitizations.length,
     97    1,
     98    "Should have 1 pending sanitization"
     99  );
    100  Assert.ok(
    101    !pendingSanitizations[0].itemsToClear.includes("cookiesAndStorage"),
    102    "Pending sanitizations should have been updated"
    103  );
    104 
    105  // Check a sanitization properly rebuilds the pref.
    106  await Sanitizer.sanitize(["cookiesAndStorage"]);
    107  pendingSanitizations = JSON.parse(
    108    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
    109  );
    110  Assert.equal(
    111    pendingSanitizations.length,
    112    1,
    113    "Should have 1 pending sanitization"
    114  );
    115  Assert.equal(
    116    pendingSanitizations[0].id,
    117    "shutdown",
    118    "Should be the shutdown sanitization"
    119  );
    120 
    121  // Startup should run the pending one and setup a new shutdown sanitization.
    122  Services.prefs.setBoolPref(
    123    Sanitizer.PREF_SHUTDOWN_BRANCH + "cookiesAndStorage",
    124    false
    125  );
    126  await Sanitizer.onStartup();
    127  pendingSanitizations = JSON.parse(
    128    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
    129  );
    130  Assert.equal(
    131    pendingSanitizations.length,
    132    1,
    133    "Should have 1 pending sanitization"
    134  );
    135  Assert.equal(
    136    pendingSanitizations[0].id,
    137    "shutdown",
    138    "Should be the shutdown sanitization"
    139  );
    140  Assert.ok(
    141    !pendingSanitizations[0].itemsToClear.includes("cookiesAndStorage"),
    142    "Pref has been setup"
    143  );
    144 });