tor-browser

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

test_storagePressure.js (3180B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 /**
      7 * This test is mainly to verify that the storage pressure event is fired when
      8 * the eviction process is not able to free some space when a quota client
      9 * attempts to write over the global limit or when the global limit is reduced
     10 * below the global usage.
     11 */
     12 
     13 const { TestUtils } = ChromeUtils.importESModule(
     14  "resource://testing-common/TestUtils.sys.mjs"
     15 );
     16 
     17 loadScript("dom/quota/test/common/file.js");
     18 
     19 async function awaitStoragePressure() {
     20  const [subject] = await TestUtils.topicObserved(
     21    "QuotaManager::StoragePressure"
     22  );
     23  const usage = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
     24  return usage;
     25 }
     26 
     27 async function testSteps() {
     28  const globalLimitKB = 2;
     29 
     30  const principal = getPrincipal("https://example.com");
     31 
     32  info("Setting limits");
     33 
     34  setGlobalLimit(globalLimitKB);
     35 
     36  info("Initializing");
     37 
     38  let request = init();
     39  await requestFinished(request);
     40 
     41  info("Initializing temporary storage");
     42 
     43  request = initTemporaryStorage();
     44  await requestFinished(request);
     45 
     46  info("Persisting and filling an origin");
     47 
     48  // We need to persist the origin first to omit the group limit checks.
     49  // Otherwise, we would have to fill five separate origins.
     50  request = persist(principal);
     51  await requestFinished(request);
     52 
     53  let database = getSimpleDatabase(principal);
     54 
     55  request = database.open("data");
     56  await requestFinished(request);
     57 
     58  try {
     59    request = database.write(getBuffer(globalLimitKB * 1024));
     60    await requestFinished(request);
     61 
     62    ok(true, "Should not have thrown");
     63  } catch (ex) {
     64    ok(false, "Should not have thrown");
     65  }
     66 
     67  info("Testing storage pressure by writing over the global limit");
     68 
     69  info("Storing one more byte to get the storage pressure event while writing");
     70 
     71  let promiseStoragePressure = awaitStoragePressure();
     72 
     73  try {
     74    request = database.write(getBuffer(1));
     75    await requestFinished(request);
     76 
     77    ok(false, "Should have thrown");
     78  } catch (e) {
     79    ok(true, "Should have thrown");
     80    Assert.strictEqual(
     81      e.resultCode,
     82      NS_ERROR_FILE_NO_DEVICE_SPACE,
     83      "Threw right result code"
     84    );
     85  }
     86 
     87  info("Checking the storage pressure event");
     88 
     89  let usage = await promiseStoragePressure;
     90  Assert.equal(usage, globalLimitKB * 1024, "Got correct usage");
     91 
     92  info("Testing storage pressure by reducing the global limit");
     93 
     94  info(
     95    "Reducing the global limit to get the storage pressuse event while the" +
     96      " temporary storage is being initialized"
     97  );
     98 
     99  setGlobalLimit(globalLimitKB - 1);
    100 
    101  request = reset();
    102  await requestFinished(request);
    103 
    104  info("Initializing");
    105 
    106  request = init();
    107  await requestFinished(request);
    108 
    109  promiseStoragePressure = awaitStoragePressure();
    110 
    111  info("Initializing temporary storage");
    112 
    113  request = initTemporaryStorage();
    114  await requestFinished(request);
    115 
    116  info("Checking the storage pressure event");
    117 
    118  usage = await promiseStoragePressure;
    119  Assert.equal(usage, globalLimitKB * 1024, "Got correct usage");
    120 
    121  info("Resetting limits");
    122 
    123  resetGlobalLimit();
    124 
    125  request = reset();
    126  await requestFinished(request);
    127 }