summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_manifest.js
blob: e627512d9bb2fdffe9cd140bad5b98c7e5feb06c (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

ChromeUtils.defineESModuleGetters(this, {
  AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
});

AddonTestUtils.init(this);
AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "1",
  "42"
);

async function testManifest(manifest, expectedError) {
  ExtensionTestUtils.failOnSchemaWarnings(false);
  let normalized = await ExtensionTestUtils.normalizeManifest(manifest);
  ExtensionTestUtils.failOnSchemaWarnings(true);

  if (expectedError) {
    ok(
      expectedError.test(normalized.error),
      `Should have an error for ${JSON.stringify(manifest)}, got ${
        normalized.error
      }`
    );
  } else {
    ok(
      !normalized.error,
      `Should not have an error ${JSON.stringify(manifest)}, ${
        normalized.error
      }`
    );
  }
  return normalized.errors;
}

async function testIconPaths(icon, manifest, expectedError) {
  let normalized = await ExtensionTestUtils.normalizeManifest(manifest);

  if (expectedError) {
    ok(
      expectedError.test(normalized.error),
      `Should have an error for ${JSON.stringify(icon)}`
    );
  } else {
    ok(!normalized.error, `Should not have an error ${JSON.stringify(icon)}`);
  }
}

add_setup(async () => {
  await AddonTestUtils.promiseStartupManager();
});

add_task(async function test_manifest() {
  let badpaths = ["", " ", "\t", "http://foo.com/icon.png"];
  for (let path of badpaths) {
    await testIconPaths(
      path,
      {
        icons: path,
      },
      /Error processing icons/
    );

    await testIconPaths(
      path,
      {
        icons: {
          16: path,
        },
      },
      /Error processing icons/
    );
  }

  let paths = [
    "icon.png",
    "/icon.png",
    "./icon.png",
    "path to an icon.png",
    " icon.png",
  ];
  for (let path of paths) {
    // manifest.icons is an object
    await testIconPaths(
      path,
      {
        icons: path,
      },
      /Error processing icons/
    );

    await testIconPaths(path, {
      icons: {
        16: path,
      },
    });
  }
});

add_task(async function test_manifest_warnings_on_unexpected_props() {
  let extension = await ExtensionTestUtils.loadExtension({
    manifest: {
      background: {
        scripts: ["bg.js"],
        wrong_prop: true,
      },
    },
    files: {
      "bg.js": "",
    },
  });

  ExtensionTestUtils.failOnSchemaWarnings(false);
  await extension.startup();
  ExtensionTestUtils.failOnSchemaWarnings(true);

  // Retrieve the warning message collected by the Extension class
  // packagingWarning method.
  const { warnings } = extension.extension;
  equal(warnings.length, 1, "Got the expected number of manifest warnings");

  const expectedMessage =
    "Reading manifest: Warning processing background.wrong_prop";
  ok(
    warnings[0].startsWith(expectedMessage),
    "Got the expected warning message format"
  );

  await extension.unload();
});

add_task(async function test_mv2_scripting_permission_always_enabled() {
  let warnings = await testManifest({
    manifest_version: 2,
    permissions: ["scripting"],
  });

  Assert.deepEqual(warnings, [], "Got no warnings");
});

add_task(
  {
    pref_set: [["extensions.manifestV3.enabled", true]],
  },
  async function test_mv3_scripting_permission_always_enabled() {
    let warnings = await testManifest({
      manifest_version: 3,
      permissions: ["scripting"],
    });

    Assert.deepEqual(warnings, [], "Got no warnings");
  }
);

add_task(async function test_simpler_version_format() {
  const TEST_CASES = [
    // Valid cases
    { version: "0", expectWarning: false },
    { version: "0.0", expectWarning: false },
    { version: "0.0.0", expectWarning: false },
    { version: "0.0.0.0", expectWarning: false },
    { version: "0.0.0.1", expectWarning: false },
    { version: "0.0.0.999999999", expectWarning: false },
    { version: "0.0.1.0", expectWarning: false },
    { version: "0.0.999999999", expectWarning: false },
    { version: "0.1.0.0", expectWarning: false },
    { version: "0.999999999", expectWarning: false },
    { version: "1", expectWarning: false },
    { version: "1.0", expectWarning: false },
    { version: "1.0.0", expectWarning: false },
    { version: "1.0.0.0", expectWarning: false },
    { version: "1.2.3.4", expectWarning: false },
    { version: "999999999", expectWarning: false },
    {
      version: "999999999.999999999.999999999.999999999",
      expectWarning: false,
    },
    // Invalid cases
    { version: ".", expectWarning: true },
    { version: ".999999999", expectWarning: true },
    { version: "0.0.0.0.0", expectWarning: true },
    { version: "0.0.0.00001", expectWarning: true },
    { version: "0.0.0.0010", expectWarning: true },
    { version: "0.0.00001", expectWarning: true },
    { version: "0.0.001", expectWarning: true },
    { version: "0.0.01.0", expectWarning: true },
    { version: "0.01.0", expectWarning: true },
    { version: "00001", expectWarning: true },
    { version: "0001", expectWarning: true },
    { version: "001", expectWarning: true },
    { version: "01", expectWarning: true },
    { version: "01.0", expectWarning: true },
    { version: "099999", expectWarning: true },
    { version: "0999999999", expectWarning: true },
    { version: "1.00000", expectWarning: true },
    { version: "1.1.-1", expectWarning: true },
    { version: "1.1000000000", expectWarning: true },
    { version: "1.1pre1aa", expectWarning: true },
    { version: "1.2.1000000000", expectWarning: true },
    { version: "1.2.3.4-a", expectWarning: true },
    { version: "1.2.3.4.5", expectWarning: true },
    { version: "1000000000", expectWarning: true },
    { version: "1000000000.0.0.0", expectWarning: true },
    { version: "999999999.", expectWarning: true },
  ];

  for (const { version, expectWarning } of TEST_CASES) {
    const normalized = await ExtensionTestUtils.normalizeManifest({ version });

    if (expectWarning) {
      Assert.deepEqual(
        normalized.errors,
        [
          `Warning processing version: version must be a version string ` +
            `consisting of at most 4 integers of at most 9 digits without ` +
            `leading zeros, and separated with dots`,
        ],
        `expected warning for version: ${version}`
      );
    } else {
      Assert.deepEqual(
        normalized.errors,
        [],
        `expected no warning for version: ${version}`
      );
    }
  }
});

add_task(async function test_applications() {
  const id = "some@id";
  const updateURL = "https://example.com/updates/";

  let extension = await ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version: 2,
      applications: {
        gecko: { id, update_url: updateURL },
      },
    },
    useAddonManager: "temporary",
  });

  ExtensionTestUtils.failOnSchemaWarnings(false);
  await extension.startup();
  ExtensionTestUtils.failOnSchemaWarnings(true);

  Assert.deepEqual(extension.extension.warnings, [], "expected no warnings");

  const addon = await AddonManager.getAddonByID(extension.id);
  ok(addon, "got an add-on");
  equal(addon.id, id, "got expected ID");
  equal(addon.updateURL, updateURL, "got expected update URL");

  await extension.unload();
});

add_task(
  {
    pref_set: [["extensions.manifestV3.enabled", true]],
  },
  async function test_applications_key_mv3() {
    let warnings = await testManifest({
      manifest_version: 3,
      applications: {},
    });

    Assert.deepEqual(
      warnings,
      [`Property "applications" is unsupported in Manifest Version 3`],
      `Manifest v3 with "applications" key logs an error.`
    );
  }
);

add_task(async function test_bss_gecko_android() {
  const addonId = "some@id";
  const isAndroid = AppConstants.platform == "android";

  const TEST_CASES = [
    {
      title: "gecko_android overrides gecko",
      browser_specific_settings: {
        gecko: {
          id: addonId,
          strict_min_version: "2",
          strict_max_version: "2",
        },
        gecko_android: {
          strict_min_version: "1",
          strict_max_version: "1",
        },
      },
      expectedError: isAndroid
        ? `Add-on ${addonId} is not compatible with application version. add-on minVersion: 1. add-on maxVersion: 1.`
        : `Add-on ${addonId} is not compatible with application version. add-on minVersion: 2. add-on maxVersion: 2.`,
    },
    {
      title:
        "strict_min_version in gecko_android overrides gecko.strict_min_version",
      browser_specific_settings: {
        gecko: {
          id: addonId,
          strict_min_version: "2",
          strict_max_version: "3",
        },
        gecko_android: {
          strict_min_version: "3",
        },
      },
      expectedError: isAndroid
        ? `Add-on ${addonId} is not compatible with application version. add-on minVersion: 3. add-on maxVersion: 3.`
        : `Add-on ${addonId} is not compatible with application version. add-on minVersion: 2. add-on maxVersion: 3.`,
    },
    {
      title:
        "strict_max_version in gecko_android overrides gecko.strict_max_version",
      browser_specific_settings: {
        gecko: {
          id: addonId,
          strict_min_version: "2",
          strict_max_version: "2",
        },
        gecko_android: {
          strict_max_version: "3",
        },
      },
      expectedError: isAndroid
        ? `Add-on ${addonId} is not compatible with application version. add-on minVersion: 2. add-on maxVersion: 3.`
        : `Add-on ${addonId} is not compatible with application version. add-on minVersion: 2. add-on maxVersion: 2.`,
    },
    {
      title: "no gecko_android",
      browser_specific_settings: {
        gecko: {
          id: addonId,
          strict_min_version: "2",
          strict_max_version: "2",
        },
      },
      expectedError: `Add-on ${addonId} is not compatible with application version. add-on minVersion: 2. add-on maxVersion: 2.`,
    },
    {
      title: "empty gecko_android",
      browser_specific_settings: {
        gecko: {
          id: addonId,
          strict_min_version: "2",
          strict_max_version: "2",
        },
        gecko_android: {},
      },
      expectedError: `Add-on ${addonId} is not compatible with application version. add-on minVersion: 2. add-on maxVersion: 2.`,
    },
    {
      title: "empty strict min/max versions in gecko_android",
      browser_specific_settings: {
        gecko: {
          id: addonId,
          strict_min_version: "2",
          strict_max_version: "2",
        },
        gecko_android: {
          strict_min_version: "",
          strict_max_version: "",
        },
      },
      expectedError: `Add-on ${addonId} is not compatible with application version. add-on minVersion: 2. add-on maxVersion: 2.`,
    },
    {
      title: "unsupported prop in gecko_android",
      browser_specific_settings: {
        gecko: {
          id: addonId,
          strict_min_version: "2",
          strict_max_version: "2",
        },
        gecko_android: {
          aPropThatIsNotSupported: "aPropThatIsNotSupported",
        },
      },
      expectedError: `Add-on ${addonId} is not compatible with application version. add-on minVersion: 2. add-on maxVersion: 2.`,
    },
    {
      title: "only strict min/max version in gecko_android",
      browser_specific_settings: {
        gecko: {
          id: addonId,
        },
        gecko_android: {
          strict_min_version: "3",
          strict_max_version: "4",
        },
      },
      expectedError: isAndroid
        ? `Add-on ${addonId} is not compatible with application version. add-on minVersion: 3. add-on maxVersion: 4.`
        : null,
    },
    {
      title: "only strict_min_version in gecko_android",
      browser_specific_settings: {
        gecko: {
          id: addonId,
        },
        gecko_android: {
          // The app version is set to `42` at the top of the file.
          strict_min_version: "100",
        },
      },
      expectedError: isAndroid
        ? `Add-on ${addonId} is not compatible with application version. add-on minVersion: 100.`
        : null,
    },
  ];

  for (const {
    title,
    browser_specific_settings,
    expectedError,
  } of TEST_CASES) {
    info(`verifying: ${title}`);

    // This task is mainly about verifying `bss.gecko_android` and some test
    // cases require a "valid" compatibility range by default, which would
    // break the assumption below (that the install is going to fail). This is
    // why we skip null errors, but only on non-Android builds.
    if (expectedError === null) {
      notEqual(
        AppConstants.platform,
        "android",
        `${title} - expected no error on a non-Android build`
      );
      continue;
    }

    const manifest = {
      manifest_version: 2,
      version: "1.0",
      browser_specific_settings,
    };

    const extension = ExtensionTestUtils.loadExtension({
      manifest,
      useAddonManager: "temporary",
    });

    await Assert.rejects(
      extension.startup(),
      new RegExp(expectedError),
      `${title} - expected error: ${expectedError}`
    );

    const addon = await AddonManager.getAddonByID(addonId);
    equal(addon, null, "add-on is not installed");
  }
});

add_task(
  { skip_if: AppConstants.platform !== "android" },
  async function test_bss_gecko_android_only() {
    const extension = ExtensionTestUtils.loadExtension({
      manifest: {
        manifest_version: 2,
        version: "1.0",
        browser_specific_settings: {
          gecko_android: {
            strict_min_version: "0",
          },
        },
      },
    });

    await extension.startup();

    const { manifest } = extension.extension;
    equal(
      manifest.browser_specific_settings.gecko_android.strict_min_version,
      "0",
      "expected strict min version"
    );

    await extension.unload();
  }
);