tor-browser

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

browser_Manifest_install.js (3785B)


      1 "use strict";
      2 
      3 const { Manifests } = ChromeUtils.importESModule(
      4  "resource://gre/modules/Manifest.sys.mjs"
      5 );
      6 
      7 const defaultURL = new URL(
      8  "http://example.org/browser/dom/manifest/test/resource.sjs"
      9 );
     10 defaultURL.searchParams.set("Content-Type", "application/manifest+json");
     11 
     12 const manifestMock = JSON.stringify({
     13  short_name: "hello World",
     14  scope: "/browser/",
     15 });
     16 const manifestUrl = `${defaultURL}&body=${manifestMock}`;
     17 
     18 function makeTestURL() {
     19  const url = new URL(defaultURL);
     20  const body = `<link rel="manifest" href='${manifestUrl}'>`;
     21  url.searchParams.set("Content-Type", "text/html; charset=utf-8");
     22  url.searchParams.set("body", encodeURIComponent(body));
     23  return url.href;
     24 }
     25 
     26 function generateHash(aString, hashAlg) {
     27  const cryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(
     28    Ci.nsICryptoHash
     29  );
     30  cryptoHash.init(hashAlg);
     31  const stringStream = Cc[
     32    "@mozilla.org/io/string-input-stream;1"
     33  ].createInstance(Ci.nsIStringInputStream);
     34  stringStream.setByteStringData(aString);
     35  cryptoHash.updateFromStream(stringStream, -1);
     36  // base64 allows the '/' char, but we can't use it for filenames.
     37  return cryptoHash.finish(true).replace(/\//g, "-");
     38 }
     39 
     40 const MANIFESTS_DIR = PathUtils.join(PathUtils.profileDir, "manifests");
     41 
     42 add_setup(async function () {
     43  await SpecialPowers.pushPrefEnv({
     44    set: [["dom.security.https_first", false]],
     45  });
     46 });
     47 
     48 add_task(async function () {
     49  const tabOptions = { gBrowser, url: makeTestURL() };
     50 
     51  const filenameMD5 = generateHash(manifestUrl, Ci.nsICryptoHash.MD5) + ".json";
     52  const filenameSHA =
     53    generateHash(manifestUrl, Ci.nsICryptoHash.SHA256) + ".json";
     54  const manifestMD5Path = PathUtils.join(MANIFESTS_DIR, filenameMD5);
     55  const manifestSHAPath = PathUtils.join(MANIFESTS_DIR, filenameSHA);
     56 
     57  await BrowserTestUtils.withNewTab(tabOptions, async function (browser) {
     58    let tmpManifest = await Manifests.getManifest(browser, manifestUrl);
     59    is(tmpManifest.installed, false, "We haven't installed this manifest yet");
     60 
     61    await tmpManifest.install();
     62 
     63    // making sure the manifest is actually installed before proceeding
     64    await tmpManifest._store._save();
     65    await IOUtils.move(tmpManifest.path, manifestMD5Path);
     66 
     67    let exists = await IOUtils.exists(tmpManifest.path);
     68    is(
     69      exists,
     70      false,
     71      "Manually moved manifest from SHA256 based path to MD5 based path"
     72    );
     73    Manifests.manifestObjs.delete(manifestUrl);
     74 
     75    let manifest = await Manifests.getManifest(browser, manifestUrl);
     76    await manifest.install(browser);
     77    is(manifest.name, "hello World", "Manifest has correct name");
     78    is(manifest.installed, true, "Manifest is installed");
     79    is(manifest.url, manifestUrl, "has correct url");
     80    is(manifest.browser, browser, "has correct browser");
     81    is(manifest.path, manifestSHAPath, "has correct path");
     82 
     83    exists = await IOUtils.exists(manifestMD5Path);
     84    is(exists, false, "MD5 based manifest removed");
     85 
     86    manifest = await Manifests.getManifest(browser, manifestUrl);
     87    is(manifest.installed, true, "New instances are installed");
     88 
     89    manifest = await Manifests.getManifest(browser);
     90    is(manifest.installed, true, "Will find manifest without being given url");
     91 
     92    let foundManifest = Manifests.findManifestUrl(
     93      "http://example.org/browser/dom/"
     94    );
     95    is(foundManifest, manifestUrl, "Finds manifests within scope");
     96 
     97    foundManifest = Manifests.findManifestUrl("http://example.org/");
     98    is(foundManifest, null, "Does not find manifests outside scope");
     99  });
    100  // Get the cached one now
    101  await BrowserTestUtils.withNewTab(tabOptions, async browser => {
    102    const manifest = await Manifests.getManifest(browser, manifestUrl);
    103    is(manifest.browser, browser, "has updated browser object");
    104  });
    105 });