summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_system_update_custom.js
blob: 3aa61fed519d9c6eb4d08ef692cf60a5ca8bfa38 (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
489
490
491
492
// Tests that system add-on upgrades work.

createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "2");

let distroDir = FileUtils.getDir("ProfD", ["sysfeatures", "empty"], true);
registerDirectory("XREAppFeat", distroDir);

AddonTestUtils.usePrivilegedSignatures = id => "system";

add_task(initSystemAddonDirs);

/**
 * Defines the set of initial conditions to run each test against. Each should
 * define the following properties:
 *
 * setup:        A task to setup the profile into the initial state.
 * initialState: The initial expected system add-on state after setup has run.
 */
const TEST_CONDITIONS = {
  // Runs tests with no updated or default system add-ons initially installed
  blank: {
    setup() {
      clearSystemAddonUpdatesDir();
      distroDir.leafName = "empty";
    },
    initialState: [
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
    ],
  },
  // Runs tests with default system add-ons installed
  withAppSet: {
    setup() {
      clearSystemAddonUpdatesDir();
      distroDir.leafName = "prefilled";
    },
    initialState: [
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: "2.0" },
      { isUpgrade: false, version: "2.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
    ],
  },

  // Runs tests with updated system add-ons installed
  withProfileSet: {
    async setup() {
      await buildPrefilledUpdatesDir();
      distroDir.leafName = "empty";
    },
    initialState: [
      { isUpgrade: false, version: null },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
    ],
  },

  // Runs tests with both default and updated system add-ons installed
  withBothSets: {
    async setup() {
      await buildPrefilledUpdatesDir();
      distroDir.leafName = "hidden";
    },
    initialState: [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
    ],
  },
};

// Test that the update check is performed as part of the regular add-on update
// check
add_task(async function test_addon_update() {
  Services.prefs.setBoolPref(PREF_SYSTEM_ADDON_UPDATE_ENABLED, true);
  await setupSystemAddonConditions(TEST_CONDITIONS.blank, distroDir);

  let updateXML = buildSystemAddonUpdates([
    {
      id: "system2@tests.mozilla.org",
      version: "2.0",
      path: "system2_2.xpi",
      xpi: await getSystemAddonXPI(2, "2.0"),
    },
    {
      id: "system3@tests.mozilla.org",
      version: "2.0",
      path: "system3_2.xpi",
      xpi: await getSystemAddonXPI(3, "2.0"),
    },
  ]);

  const promiseInstallsEnded = createInstallsEndedPromise(2);

  await Promise.all([
    updateAllSystemAddons(updateXML),
    promiseWebExtensionStartup("system2@tests.mozilla.org"),
    promiseWebExtensionStartup("system3@tests.mozilla.org"),
  ]);

  await promiseInstallsEnded;

  await verifySystemAddonState(
    TEST_CONDITIONS.blank.initialState,
    [
      { isUpgrade: false, version: null },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
    ],
    false,
    distroDir
  );

  await promiseShutdownManager();
});

// Disabling app updates should block system add-on updates
add_task(async function test_app_update_disabled() {
  await setupSystemAddonConditions(TEST_CONDITIONS.blank, distroDir);

  Services.prefs.setBoolPref(PREF_SYSTEM_ADDON_UPDATE_ENABLED, false);
  let updateXML = buildSystemAddonUpdates([
    {
      id: "system2@tests.mozilla.org",
      version: "2.0",
      path: "system2_2.xpi",
      xpi: await getSystemAddonXPI(2, "2.0"),
    },
    {
      id: "system3@tests.mozilla.org",
      version: "2.0",
      path: "system3_2.xpi",
      xpi: await getSystemAddonXPI(3, "2.0"),
    },
  ]);
  await updateAllSystemAddons(updateXML);
  Services.prefs.clearUserPref(PREF_SYSTEM_ADDON_UPDATE_ENABLED);

  await verifySystemAddonState(
    TEST_CONDITIONS.blank.initialState,
    undefined,
    false,
    distroDir
  );

  await promiseShutdownManager();
});

// Safe mode should block system add-on updates
add_task(async function test_safe_mode() {
  gAppInfo.inSafeMode = true;

  await setupSystemAddonConditions(TEST_CONDITIONS.blank, distroDir);

  Services.prefs.setBoolPref(PREF_SYSTEM_ADDON_UPDATE_ENABLED, false);
  let updateXML = buildSystemAddonUpdates([
    {
      id: "system2@tests.mozilla.org",
      version: "2.0",
      path: "system2_2.xpi",
      xpi: await getSystemAddonXPI(2, "2.0"),
    },
    {
      id: "system3@tests.mozilla.org",
      version: "2.0",
      path: "system3_2.xpi",
      xpi: await getSystemAddonXPI(3, "2.0"),
    },
  ]);
  await updateAllSystemAddons(updateXML);
  Services.prefs.clearUserPref(PREF_SYSTEM_ADDON_UPDATE_ENABLED);

  await verifySystemAddonState(
    TEST_CONDITIONS.blank.initialState,
    undefined,
    false,
    distroDir
  );

  await promiseShutdownManager();

  gAppInfo.inSafeMode = false;
});

// Tests that a set that matches the default set does nothing
add_task(async function test_match_default() {
  await setupSystemAddonConditions(TEST_CONDITIONS.withAppSet, distroDir);

  let installXML = buildSystemAddonUpdates([
    {
      id: "system2@tests.mozilla.org",
      version: "2.0",
      path: "system2_2.xpi",
      xpi: await getSystemAddonXPI(2, "2.0"),
    },
    {
      id: "system3@tests.mozilla.org",
      version: "2.0",
      path: "system3_2.xpi",
      xpi: await getSystemAddonXPI(3, "2.0"),
    },
  ]);
  await installSystemAddons(installXML);

  // Shouldn't have installed an updated set
  await verifySystemAddonState(
    TEST_CONDITIONS.withAppSet.initialState,
    undefined,
    false,
    distroDir
  );

  await promiseShutdownManager();
});

// Tests that a set that matches the hidden default set works
add_task(async function test_match_default_revert() {
  await setupSystemAddonConditions(TEST_CONDITIONS.withBothSets, distroDir);

  let installXML = buildSystemAddonUpdates([
    {
      id: "system1@tests.mozilla.org",
      version: "1.0",
      path: "system1_1.xpi",
      xpi: await getSystemAddonXPI(1, "1.0"),
    },
    {
      id: "system2@tests.mozilla.org",
      version: "1.0",
      path: "system2_1.xpi",
      xpi: await getSystemAddonXPI(2, "1.0"),
    },
  ]);
  await installSystemAddons(installXML);

  // This should revert to the default set instead of installing new versions
  // into an updated set.
  await verifySystemAddonState(
    TEST_CONDITIONS.withBothSets.initialState,
    [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
    ],
    false,
    distroDir
  );

  await promiseShutdownManager();
});

// Tests that a set that matches the current set works
add_task(async function test_match_current() {
  await setupSystemAddonConditions(TEST_CONDITIONS.withBothSets, distroDir);

  let installXML = buildSystemAddonUpdates([
    {
      id: "system2@tests.mozilla.org",
      version: "2.0",
      path: "system2_2.xpi",
      xpi: await getSystemAddonXPI(2, "2.0"),
    },
    {
      id: "system3@tests.mozilla.org",
      version: "2.0",
      path: "system3_2.xpi",
      xpi: await getSystemAddonXPI(3, "2.0"),
    },
  ]);
  await installSystemAddons(installXML);

  // This should remain with the current set instead of creating a new copy
  let set = JSON.parse(Services.prefs.getCharPref(PREF_SYSTEM_ADDON_SET));
  Assert.equal(set.directory, "prefilled");

  await verifySystemAddonState(
    TEST_CONDITIONS.withBothSets.initialState,
    undefined,
    false,
    distroDir
  );

  await promiseShutdownManager();
});

// Tests that a set with a minor change doesn't re-download existing files
add_task(async function test_no_download() {
  await setupSystemAddonConditions(TEST_CONDITIONS.withBothSets, distroDir);

  // The missing file here is unneeded since there is a local version already
  let installXML = buildSystemAddonUpdates([
    { id: "system2@tests.mozilla.org", version: "2.0", path: "missing.xpi" },
    {
      id: "system4@tests.mozilla.org",
      version: "1.0",
      path: "system4_1.xpi",
      xpi: await getSystemAddonXPI(4, "1.0"),
    },
  ]);

  const promiseInstallsEnded = createInstallsEndedPromise(2);

  await Promise.all([
    installSystemAddons(installXML),
    promiseWebExtensionStartup("system4@tests.mozilla.org"),
  ]);

  // NOTE: verifySystemAddonState does call AddonTestUtils.promiseShutdownManager
  // internally, and so we need to wait for the system addons to be fully
  // installed and the system addon location's stating dir to have been released.
  info("Wait for system addon installs to be completed");
  await promiseInstallsEnded;
  info("Wait for system addons stating dir to be released");
  await waitForSystemAddonStagingDirReleased();

  await verifySystemAddonState(
    TEST_CONDITIONS.withBothSets.initialState,
    [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: true, version: "1.0" },
      { isUpgrade: false, version: null },
    ],
    false,
    distroDir
  );

  await promiseShutdownManager();
});

// Tests that a second update before a restart works
add_task(async function test_double_update() {
  await setupSystemAddonConditions(TEST_CONDITIONS.withAppSet, distroDir);

  let installXML = buildSystemAddonUpdates([
    {
      id: "system2@tests.mozilla.org",
      version: "2.0",
      path: "system2_2.xpi",
      xpi: await getSystemAddonXPI(2, "2.0"),
    },
    {
      id: "system3@tests.mozilla.org",
      version: "1.0",
      path: "system3_1.xpi",
      xpi: await getSystemAddonXPI(3, "1.0"),
    },
  ]);
  await Promise.all([
    installSystemAddons(installXML),
    promiseWebExtensionStartup("system2@tests.mozilla.org"),
    promiseWebExtensionStartup("system3@tests.mozilla.org"),
  ]);

  installXML = buildSystemAddonUpdates([
    {
      id: "system3@tests.mozilla.org",
      version: "2.0",
      path: "system3_2.xpi",
      xpi: await getSystemAddonXPI(3, "2.0"),
    },
    {
      id: "system4@tests.mozilla.org",
      version: "1.0",
      path: "system4_1.xpi",
      xpi: await getSystemAddonXPI(4, "1.0"),
    },
  ]);
  await Promise.all([
    installSystemAddons(installXML),
    promiseWebExtensionStartup("system3@tests.mozilla.org"),
    promiseWebExtensionStartup("system4@tests.mozilla.org"),
  ]);

  await verifySystemAddonState(
    TEST_CONDITIONS.withAppSet.initialState,
    [
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: "2.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: true, version: "1.0" },
      { isUpgrade: false, version: null },
    ],
    true,
    distroDir
  );

  await promiseShutdownManager();
});

// A second update after a restart will delete the original unused set
add_task(async function test_update_purges() {
  await setupSystemAddonConditions(TEST_CONDITIONS.withBothSets, distroDir);

  let installXML = buildSystemAddonUpdates([
    {
      id: "system2@tests.mozilla.org",
      version: "2.0",
      path: "system2_2.xpi",
      xpi: await getSystemAddonXPI(2, "2.0"),
    },
    {
      id: "system3@tests.mozilla.org",
      version: "1.0",
      path: "system3_1.xpi",
      xpi: await getSystemAddonXPI(3, "1.0"),
    },
  ]);
  await Promise.all([
    installSystemAddons(installXML),
    promiseWebExtensionStartup("system2@tests.mozilla.org"),
    promiseWebExtensionStartup("system3@tests.mozilla.org"),
  ]);

  await verifySystemAddonState(
    TEST_CONDITIONS.withBothSets.initialState,
    [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: true, version: "1.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
    ],
    false,
    distroDir
  );

  await installSystemAddons(buildSystemAddonUpdates(null));

  let dirs = await getSystemAddonDirectories();
  Assert.equal(dirs.length, 1);

  await promiseShutdownManager();
});

function createInstallsEndedPromise(expectedCount) {
  // Addon installation triggers the execution of an un-awaited promise. We need
  // to keep track of addon installs so that we can tell when these async
  // processes have finished.

  return new Promise(resolve => {
    let installsEnded = 0;

    const listener = {
      onInstallStarted() {},
      onInstallEnded() {
        installsEnded++;

        if (installsEnded === expectedCount) {
          AddonManager.removeInstallListener(listener);
          resolve();
        }
      },
      onInstallCancelled() {},
      onInstallFailed() {},
    };

    AddonManager.addInstallListener(listener);
  });
}

async function waitForSystemAddonStagingDirReleased() {
  // Wait for the staging dir to be released, which prevents unexpected test failure due to
  // AddonTestUtils.promiseShutdownManager being mocking an AddonManager shutdown by using
  // Cu.unload to unload all XPIProvider jsm modules, which would hit unexpected failures
  // if done while system addon updates are still running in the background (due to the
  // fact that the jsm global to have been already nuked while AddonInstall startInstall
  // method may still being executed asynchronously).

  const { XPIInternal } = ChromeUtils.import(
    "resource://gre/modules/addons/XPIProvider.jsm"
  );
  let systemAddonLocation = XPIInternal.XPIStates.getLocation(
    XPIInternal.KEY_APP_SYSTEM_ADDONS
  );
  await TestUtils.waitForCondition(() => {
    return systemAddonLocation.installer._stagingDirLock == 0;
  }, "Wait for staging dir to be released");
}