tor-browser

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

test_cache2_clear_with_usercontext_oa.js (2337B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const { HttpServer } = ChromeUtils.importESModule(
      8  "resource://testing-common/httpd.sys.mjs"
      9 );
     10 
     11 var httpserver = new HttpServer();
     12 
     13 function make_uri(urlStr) {
     14  return Services.io.newURI(urlStr);
     15 }
     16 
     17 function serverHandler(_metadata, response) {
     18  const body = "hello world";
     19  response.setHeader("Content-Type", "text/plain", false);
     20  response.bodyOutputStream.write(body, body.length);
     21 }
     22 
     23 add_setup(async function setup() {
     24  httpserver.registerPathHandler("/test", serverHandler);
     25  httpserver.start(-1);
     26 
     27  registerCleanupFunction(async function () {
     28    await httpserver.stop();
     29  });
     30 });
     31 
     32 async function test(oaLoad, oaClear, shouldExist) {
     33  let port = httpserver.identity.primaryPort;
     34  info("Starting test with port " + port);
     35 
     36  let url = `http://localhost:${port}/test`;
     37  let chan = makeHTTPChannel(url);
     38  chan.loadInfo.originAttributes = oaLoad;
     39  await new Promise(resolve => {
     40    chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
     41  });
     42 
     43  let cache_storage = getCacheStorage(
     44    "disk",
     45    Services.loadContextInfo.custom(false, oaLoad)
     46  );
     47  let exists = cache_storage.exists(make_uri(url), null);
     48  Assert.ok(exists, "Entry should be in cache");
     49 
     50  Services.cache2.clearOriginsByOriginAttributes(JSON.stringify(oaClear));
     51 
     52  // clearOriginsByOriginAttributes is async, so we block on cacheIOThread
     53  await new Promise(resolve => {
     54    syncWithCacheIOThread(resolve, true);
     55  });
     56 
     57  let existsAgain = cache_storage.exists(make_uri(url), null);
     58  Assert.equal(
     59    existsAgain,
     60    shouldExist,
     61    shouldExist ? "Entry should be in cache" : "Entry should not be in cache"
     62  );
     63 }
     64 
     65 add_task(async function test_clear_cache_with_usercontext_oa() {
     66  await test({ userContextId: 0 }, { userContextId: 0 }, false);
     67 });
     68 
     69 add_task(async function test_clear_cache_with_usercontext_oa() {
     70  await test({ userContextId: 0 }, { userContextId: 1 }, true);
     71 });
     72 
     73 add_task(
     74  async function test_clear_cache_with_usercontext_oa_across_partition() {
     75    await test(
     76      { userContextId: 0, partitionKey: "(https,example.com)" },
     77      { userContextId: 0 },
     78      true
     79    );
     80  }
     81 );