summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_json_parser.js
blob: aba25173d7ea46ec61c94fd213790b9569b5dc60 (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
100
101
102
103
104
105
106
107
108
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

add_task(async function test_json_parser() {
  const ID = "json@test.web.extension";

  let xpi = AddonTestUtils.createTempWebExtensionFile({
    files: {
      "manifest.json": String.raw`{
        // This is a manifest.
        "manifest_version": 2,
        "browser_specific_settings": {"gecko": {"id": "${ID}"}},
        "name": "This \" is // not a comment",
        "version": "0.1\\" // , "description": "This is not a description"
      }`,
    },
  });

  let expectedManifest = {
    manifest_version: 2,
    browser_specific_settings: { gecko: { id: ID } },
    name: 'This " is // not a comment',
    version: "0.1\\",
  };

  let fileURI = Services.io.newFileURI(xpi);
  let uri = NetUtil.newURI(`jar:${fileURI.spec}!/`);

  let extension = new ExtensionData(uri, false);

  await extension.parseManifest();

  Assert.deepEqual(
    extension.rawManifest,
    expectedManifest,
    "Manifest with correctly-filtered comments"
  );

  Services.obs.notifyObservers(xpi, "flush-cache-entry");
});

add_task(async function test_getExtensionVersionWithoutValidation() {
  let xpi = AddonTestUtils.createTempWebExtensionFile({
    files: {
      "manifest.json": String.raw`{
        // This is valid JSON but not a valid manifest.
        "version": ["This is not a valid version"]
      }`,
    },
  });
  let fileURI = Services.io.newFileURI(xpi);
  let uri = NetUtil.newURI(`jar:${fileURI.spec}!/`);
  let extension = new ExtensionData(uri, false);

  let rawVersion = await extension.getExtensionVersionWithoutValidation();
  Assert.deepEqual(
    rawVersion,
    ["This is not a valid version"],
    "Got the raw value of the 'version' key from an (invalid) manifest file"
  );

  // The manifest lacks several required properties and manifest_version is
  // invalid. The exact error here doesn't matter, as long as it shows that the
  // manifest is invalid.
  await Assert.rejects(
    extension.parseManifest(),
    /Unexpected params.manifestVersion value: undefined/,
    "parseManifest() should reject an invalid manifest"
  );

  Services.obs.notifyObservers(xpi, "flush-cache-entry");
});

add_task(
  {
    pref_set: [
      ["extensions.manifestV3.enabled", true],
      ["extensions.webextensions.warnings-as-errors", false],
    ],
  },
  async function test_applications_no_longer_valid_in_mv3() {
    let id = "some@id";
    let xpi = AddonTestUtils.createTempWebExtensionFile({
      files: {
        "manifest.json": JSON.stringify({
          manifest_version: 3,
          name: "some name",
          version: "0.1",
          applications: { gecko: { id } },
        }),
      },
    });

    let fileURI = Services.io.newFileURI(xpi);
    let uri = NetUtil.newURI(`jar:${fileURI.spec}!/`);

    let extension = new ExtensionData(uri, false);

    const { manifest } = await extension.parseManifest();
    ok(
      !Object.keys(manifest).includes("applications"),
      "expected no applications key in manifest"
    );

    Services.obs.notifyObservers(xpi, "flush-cache-entry");
  }
);