tor-browser

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

make_secondaryCacheValidity.js (4583B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 // TODO: Replace with a module for blob/file utils once available
      7 loadScript("dom/quota/test/common/file.js");
      8 
      9 const { IndexedDBUtils } = ChromeUtils.importESModule(
     10  "resource://testing-common/dom/indexedDB/test/modules/IndexedDBUtils.sys.mjs"
     11 );
     12 const { LocalStorageUtils } = ChromeUtils.importESModule(
     13  "resource://testing-common/dom/localstorage/test/modules/LocalStorageUtils.sys.mjs"
     14 );
     15 const { PrincipalUtils } = ChromeUtils.importESModule(
     16  "resource://testing-common/dom/quota/test/modules/PrincipalUtils.sys.mjs"
     17 );
     18 const { QuotaUtils } = ChromeUtils.importESModule(
     19  "resource://testing-common/dom/quota/test/modules/QuotaUtils.sys.mjs"
     20 );
     21 const { SimpleDBUtils } = ChromeUtils.importESModule(
     22  "resource://testing-common/dom/simpledb/test/modules/SimpleDBUtils.sys.mjs"
     23 );
     24 
     25 async function makeSecondaryCacheValidity() {
     26  const principal = PrincipalUtils.createPrincipal("http://example42.com");
     27 
     28  // IndexedDB
     29  {
     30    const request = indexedDB.openForPrincipal(principal, "myIndexedDB");
     31    // TODO: Use IndexedDBUtils once they support waiting for upgrade needed
     32    await openDBRequestUpgradeNeeded(request);
     33 
     34    const database = request.result;
     35 
     36    const objectStore = database.createObjectStore("Blobs", {});
     37 
     38    objectStore.add(getNullBlob(200), 42);
     39 
     40    await IndexedDBUtils.requestFinished(request);
     41 
     42    database.close();
     43  }
     44 
     45  // Cache API
     46  {
     47    async function sandboxScript() {
     48      const cache = await caches.open("myCache");
     49      const request = new Request("http://example.com/index.html");
     50      const response = new Response("hello world");
     51      await cache.put(request, response);
     52    }
     53 
     54    const sandbox = new Cu.Sandbox(principal, {
     55      wantGlobalProperties: ["caches", "fetch"],
     56    });
     57 
     58    const promise = new Promise(function (resolve, reject) {
     59      sandbox.resolve = resolve;
     60      sandbox.reject = reject;
     61    });
     62 
     63    Cu.evalInSandbox(
     64      sandboxScript.toSource() + " sandboxScript().then(resolve, reject);",
     65      sandbox
     66    );
     67    await promise;
     68  }
     69 
     70  // SimpleDB
     71  {
     72    const connection = SimpleDBUtils.createConnection(principal);
     73 
     74    const openRequest = connection.open("mySimpleDB");
     75    await SimpleDBUtils.requestFinished(openRequest);
     76 
     77    const writeRequest = connection.write(getBuffer(100));
     78    await SimpleDBUtils.requestFinished(writeRequest);
     79 
     80    const closeRequest = connection.close();
     81    await SimpleDBUtils.requestFinished(closeRequest);
     82  }
     83 
     84  // LocalStorage
     85  {
     86    const storage = LocalStorageUtils.createStorage(principal);
     87 
     88    storage.setItem("foo", "bar");
     89 
     90    storage.close();
     91  }
     92 
     93  // OPFS
     94  {
     95    async function sandboxScript() {
     96      const root = await storage.getDirectory();
     97 
     98      {
     99        const file = await root.getFileHandle("data.bin", { create: true });
    100 
    101        const writable = await file.createWritable();
    102        await writable.write(new Uint8Array(512));
    103        await writable.close();
    104      }
    105 
    106      {
    107        const directory = await root.getDirectoryHandle("dir", {
    108          create: true,
    109        });
    110 
    111        const file = await directory.getFileHandle("data.bin", {
    112          create: true,
    113        });
    114 
    115        const writable = await file.createWritable();
    116        await writable.write(new Uint8Array(128));
    117        await writable.close();
    118      }
    119    }
    120 
    121    const sandbox = new Cu.Sandbox(principal, {
    122      wantGlobalProperties: ["storage"],
    123      forceSecureContext: true,
    124    });
    125 
    126    const promise = new Promise(function (resolve, reject) {
    127      sandbox.resolve = resolve;
    128      sandbox.reject = reject;
    129    });
    130 
    131    Cu.evalInSandbox(
    132      sandboxScript.toSource() + " sandboxScript().then(resolve, reject);",
    133      sandbox
    134    );
    135    await promise;
    136  }
    137 
    138  // Force update of the metadata file, so it contains up to date cached usage
    139  // information.
    140 
    141  info("Shutting down storage");
    142 
    143  {
    144    const request = Services.qms.reset();
    145    await QuotaUtils.requestFinished(request);
    146  }
    147 
    148  info("Initializing storage");
    149 
    150  {
    151    const request = Services.qms.init();
    152    await QuotaUtils.requestFinished(request);
    153  }
    154 
    155  info("Initializing temporary storage");
    156 
    157  {
    158    const request = Services.qms.initTemporaryStorage();
    159    await QuotaUtils.requestFinished(request);
    160  }
    161 }
    162 
    163 async function testSteps() {
    164  add_task(
    165    {
    166      pref_set: [
    167        ["dom.quotaManager.loadQuotaFromCache", false],
    168        ["dom.storage.testing", true],
    169        ["dom.storage.client_validation", false],
    170      ],
    171    },
    172    makeSecondaryCacheValidity
    173  );
    174 }