summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_local_install.js
blob: a26a4e283b4b7a8a33a6ad46ff313420f13c2174 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

const { AddonTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/AddonTestUtils.sys.mjs"
);

AddonTestUtils.initMochitest(this);

const XPI_INCOMPATIBLE_ID = "incompatible-xpi@tests.mozilla.org";
// NOTE: we are using an HTTP url on purpose here, the test case fails
// otherwise... We disable `AddonManager.checkUpdateSecurity` to allow
// retrieving updates from HTTP (which is restored in a
// `registerCleanupFunction()` or at the end of the task).
//
// eslint-disable-next-line @microsoft/sdl/no-insecure-url
const BASE_URL = "http://fake-updates.example.com";

const server = AddonTestUtils.createHttpServer({
  hosts: ["fake-updates.example.com"],
});

const UPDATE_ENTRY_COMPATIBLE = {
  // NOTE: this version must be the exact same one associated than the
  // initially incompatible XPI, otherwise it won't override the initial
  // compatibility range.
  // See the check in `AddonUpdateChecker.getCompatibilityUpdate` here:
  // https://searchfox.org/mozilla-central/rev/4044c340/toolkit/mozapps/extensions/internal/AddonUpdateChecker.sys.mjs#489
  version: "4.0",
  // An empty compatibility range will make this update to be overriding the
  // incompatible range in the xpi and makes the xpi version to be considered
  // compatible.
  applications: { gecko: {} },
};

const UPDATE_ENTRY_INCOMPATIBLE = {
  ...UPDATE_ENTRY_COMPATIBLE,
  // This update entry instead is including a compatibility range that would
  // makes the xpi version being installed to be considered still incompatible.
  applications: {
    gecko: {
      strict_min_version: "41",
      strict_max_version: "41.*",
    },
  },
};

AddonTestUtils.registerJSON(server, "/updates-still-incompatible.json", {
  addons: {
    [XPI_INCOMPATIBLE_ID]: {
      updates: [UPDATE_ENTRY_INCOMPATIBLE],
    },
  },
});

AddonTestUtils.registerJSON(server, "/updates-now-compatible.json", {
  addons: {
    [XPI_INCOMPATIBLE_ID]: {
      updates: [UPDATE_ENTRY_COMPATIBLE],
    },
  },
});

add_task(async function test_local_install_blocklisted() {
  let id = "amosigned-xpi@tests.mozilla.org";
  let version = "2.2";

  await AddonTestUtils.loadBlocklistRawData({
    extensionsMLBF: [
      {
        stash: { blocked: [`${id}:${version}`], unblocked: [] },
        stash_time: 0,
      },
    ],
  });
  let needsCleanupBlocklist = true;
  const cleanupBlocklist = async () => {
    if (!needsCleanupBlocklist) {
      return;
    }
    await AddonTestUtils.loadBlocklistRawData({
      extensionsMLBF: [
        {
          stash: { blocked: [], unblocked: [] },
          stash_time: 0,
        },
      ],
    });
    needsCleanupBlocklist = false;
  };
  registerCleanupFunction(cleanupBlocklist);

  const xpiFilePath = getTestFilePath("../xpinstall/amosigned.xpi");
  const xpiFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
  xpiFile.initWithPath(xpiFilePath);
  ok(xpiFile.exists(), "Expect the xpi file to exist");
  const xpiFileURI = Services.io.newFileURI(xpiFile);

  let install = await AddonManager.getInstallForURL(xpiFileURI.spec, {
    telemetryInfo: { source: "file-url" },
  });
  const promiseInstallFailed = BrowserUtils.promiseObserved(
    "addon-install-failed",
    subject => {
      return subject.wrappedJSObject.installs[0] == install;
    }
  );

  AddonManager.installAddonFromWebpage(
    "application/x-xpinstall",
    gBrowser.selectedBrowser,
    Services.scriptSecurityManager.getSystemPrincipal(),
    install
  );

  info("Wait for addon-install-failed to be notified");
  await promiseInstallFailed;
  Assert.equal(
    install.error,
    AddonManager.ERROR_BLOCKLISTED,
    "LocalInstall cancelled with the expected error"
  );

  await cleanupBlocklist();
});

add_task(async function test_local_install_incompatible() {
  const xpiFilePath = getTestFilePath("../xpinstall/incompatible.xpi");
  const xpiFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
  xpiFile.initWithPath(xpiFilePath);
  ok(xpiFile.exists(), "Expect the xpi file to exist");
  const xpiFileURI = Services.io.newFileURI(xpiFile);

  const installTestExtension = async ({ expectIncompatible }) => {
    let install = await AddonManager.getInstallForURL(xpiFileURI.spec, {
      telemetryInfo: { source: "file-url" },
    });
    const promiseInstallDone = expectIncompatible
      ? BrowserUtils.promiseObserved(
          "addon-install-failed",
          subject => subject.wrappedJSObject.installs[0] == install
        )
      : BrowserUtils.promiseObserved(
          "webextension-permission-prompt",
          subject => subject.wrappedJSObject.info.addon == install.addon
        );

    AddonManager.installAddonFromWebpage(
      "application/x-xpinstall",
      gBrowser.selectedBrowser,
      Services.scriptSecurityManager.getSystemPrincipal(),
      install
    );

    if (expectIncompatible) {
      info("Wait for addon-install-failed to be notified");
      await promiseInstallDone;
      Assert.equal(
        install.error,
        AddonManager.ERROR_INCOMPATIBLE,
        "LocalInstall cancelled with the expected error"
      );
    } else {
      info("Wait for webextension-permission-prompt to be notified");
      await promiseInstallDone;
      Assert.equal(
        install.error,
        0,
        "no error expected on the LocalInstall instance"
      );
      Assert.equal(
        install.state,
        AddonManager.STATE_DOWNLOADED,
        "Got the expected LocalInstall state"
      );
      Assert.ok(
        install.addon.isCompatible,
        "updated Addon XPI is expected to be compatible"
      );
      Assert.equal(
        install.addon.version,
        "4.0",
        "Addon version expected to match the updated xpi file"
      );
      // Cancel the installation, before exiting the test.
      await install.cancel();
    }
  };

  info("Test incompatible xpi without a compatibility override");
  // Use a new tab to make sure the doorhanger will be gone when
  // the test tab is being removed (same when repeating the
  // test with expectIncompatible set to false).
  await BrowserTestUtils.withNewTab("about:blank", async () => {
    await installTestExtension({ expectIncompatible: true });
  });

  // Add the prefs to ignore signature checks for this test (allowed on all
  // channels while running in automation).
  SpecialPowers.pushPrefEnv({
    set: [
      ["extensions.update.url", `${BASE_URL}/updates.json`],
      ["xpinstall.signatures.required", false],
      ["extensions.ui.ignoreUnsigned", true],
    ],
  });
  AddonManager.checkUpdateSecurity = false;
  registerCleanupFunction(() => {
    AddonManager.checkUpdateSecurity = true;
  });

  info(
    "Test incompatible xpi with a compatibility override that is still incompatible"
  );
  // Add the prefs to provide a compatibility range override which is still
  // incompatible.
  SpecialPowers.pushPrefEnv({
    set: [
      ["extensions.update.url", `${BASE_URL}/updates-still-incompatible.json`],
    ],
  });
  await BrowserTestUtils.withNewTab("about:blank", async () => {
    await installTestExtension({ expectIncompatible: true });
  });
  SpecialPowers.popPrefEnv();

  info(
    "Test incompatible xpi with a compatibility override that makes it compatible"
  );
  // Add the prefs to provide a compatibility range override which is
  // compatible.
  SpecialPowers.pushPrefEnv({
    set: [["extensions.update.url", `${BASE_URL}/updates-now-compatible.json`]],
  });
  await BrowserTestUtils.withNewTab("about:blank", async () => {
    await installTestExtension({ expectIncompatible: false });
  });
  SpecialPowers.popPrefEnv();

  SpecialPowers.popPrefEnv();
  AddonManager.checkUpdateSecurity = true;
});