summaryrefslogtreecommitdiffstats
path: root/dom/manifest/test/browser_Manifest_install.js
blob: aad43e02639184e86213858be1dd21b95d482c45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"use strict";

const { Manifests } = ChromeUtils.importESModule(
  "resource://gre/modules/Manifest.sys.mjs"
);

const defaultURL = new URL(
  "http://example.org/browser/dom/manifest/test/resource.sjs"
);
defaultURL.searchParams.set("Content-Type", "application/manifest+json");

const manifestMock = JSON.stringify({
  short_name: "hello World",
  scope: "/browser/",
});
const manifestUrl = `${defaultURL}&body=${manifestMock}`;

function makeTestURL() {
  const url = new URL(defaultURL);
  const body = `<link rel="manifest" href='${manifestUrl}'>`;
  url.searchParams.set("Content-Type", "text/html; charset=utf-8");
  url.searchParams.set("body", encodeURIComponent(body));
  return url.href;
}

function generateHash(aString, hashAlg) {
  const cryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(
    Ci.nsICryptoHash
  );
  cryptoHash.init(hashAlg);
  const stringStream = Cc[
    "@mozilla.org/io/string-input-stream;1"
  ].createInstance(Ci.nsIStringInputStream);
  stringStream.data = aString;
  cryptoHash.updateFromStream(stringStream, -1);
  // base64 allows the '/' char, but we can't use it for filenames.
  return cryptoHash.finish(true).replace(/\//g, "-");
}

const MANIFESTS_DIR = PathUtils.join(PathUtils.profileDir, "manifests");

add_task(async function () {
  const tabOptions = { gBrowser, url: makeTestURL() };

  const filenameMD5 = generateHash(manifestUrl, Ci.nsICryptoHash.MD5) + ".json";
  const filenameSHA =
    generateHash(manifestUrl, Ci.nsICryptoHash.SHA256) + ".json";
  const manifestMD5Path = PathUtils.join(MANIFESTS_DIR, filenameMD5);
  const manifestSHAPath = PathUtils.join(MANIFESTS_DIR, filenameSHA);

  await BrowserTestUtils.withNewTab(tabOptions, async function (browser) {
    let tmpManifest = await Manifests.getManifest(browser, manifestUrl);
    is(tmpManifest.installed, false, "We haven't installed this manifest yet");

    await tmpManifest.install();

    // making sure the manifest is actually installed before proceeding
    await tmpManifest._store._save();
    await IOUtils.move(tmpManifest.path, manifestMD5Path);

    let exists = await IOUtils.exists(tmpManifest.path);
    is(
      exists,
      false,
      "Manually moved manifest from SHA256 based path to MD5 based path"
    );
    Manifests.manifestObjs.delete(manifestUrl);

    let manifest = await Manifests.getManifest(browser, manifestUrl);
    await manifest.install(browser);
    is(manifest.name, "hello World", "Manifest has correct name");
    is(manifest.installed, true, "Manifest is installed");
    is(manifest.url, manifestUrl, "has correct url");
    is(manifest.browser, browser, "has correct browser");
    is(manifest.path, manifestSHAPath, "has correct path");

    exists = await IOUtils.exists(manifestMD5Path);
    is(exists, false, "MD5 based manifest removed");

    manifest = await Manifests.getManifest(browser, manifestUrl);
    is(manifest.installed, true, "New instances are installed");

    manifest = await Manifests.getManifest(browser);
    is(manifest.installed, true, "Will find manifest without being given url");

    let foundManifest = Manifests.findManifestUrl(
      "http://example.org/browser/dom/"
    );
    is(foundManifest, manifestUrl, "Finds manifests within scope");

    foundManifest = Manifests.findManifestUrl("http://example.org/");
    is(foundManifest, null, "Does not find manifests outside scope");
  });
  // Get the cached one now
  await BrowserTestUtils.withNewTab(tabOptions, async browser => {
    const manifest = await Manifests.getManifest(browser, manifestUrl);
    is(manifest.browser, browser, "has updated browser object");
  });
});