summaryrefslogtreecommitdiff
path: root/toolkit/mozapps/extensions/test/xpcshell/test_system_reset.js
blob: 31b7e5783f3912d9adeb5c09ff3edc074a603fc1 (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
// Tests that we reset to the default system add-ons correctly when switching
// application versions
const PREF_SYSTEM_ADDON_SET = "extensions.systemAddonSet";

BootstrapMonitor.init();

const updatesDir = FileUtils.getDir("ProfD", ["features"]);

// Build the test sets
var dir = FileUtils.getDir("ProfD", ["sysfeatures", "app1"], true);
do_get_file("data/system_addons/system1_1.xpi").copyTo(dir, "system1@tests.mozilla.org.xpi");
do_get_file("data/system_addons/system2_1.xpi").copyTo(dir, "system2@tests.mozilla.org.xpi");

dir = FileUtils.getDir("ProfD", ["sysfeatures", "app2"], true);
do_get_file("data/system_addons/system1_2.xpi").copyTo(dir, "system1@tests.mozilla.org.xpi");
do_get_file("data/system_addons/system3_1.xpi").copyTo(dir, "system3@tests.mozilla.org.xpi");

dir = FileUtils.getDir("ProfD", ["sysfeatures", "app3"], true);
do_get_file("data/system_addons/system1_1_badcert.xpi").copyTo(dir, "system1@tests.mozilla.org.xpi");
do_get_file("data/system_addons/system3_1.xpi").copyTo(dir, "system3@tests.mozilla.org.xpi");

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

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

function makeUUID() {
  let uuidGen = AM_Cc["@mozilla.org/uuid-generator;1"].
                getService(AM_Ci.nsIUUIDGenerator);
  return uuidGen.generateUUID().toString();
}

function* check_installed(conditions) {
  for (let i = 0; i < conditions.length; i++) {
    let condition = conditions[i];
    let id = "system" + (i + 1) + "@tests.mozilla.org";
    let addon = yield promiseAddonByID(id);

    if (!("isUpgrade" in condition) || !("version" in condition)) {
      throw Error("condition must contain isUpgrade and version");
    }
    let isUpgrade = conditions[i].isUpgrade;
    let version = conditions[i].version;

    let expectedDir = isUpgrade ? updatesDir : distroDir;

    if (version) {
      // Add-on should be installed
      do_check_neq(addon, null);
      do_check_eq(addon.version, version);
      do_check_true(addon.isActive);
      do_check_false(addon.foreignInstall);
      do_check_true(addon.hidden);
      do_check_true(addon.isSystem);
      do_check_false(hasFlag(addon.permissions, AddonManager.PERM_CAN_UPGRADE));
      if (isUpgrade) {
        do_check_true(hasFlag(addon.permissions, AddonManager.PERM_CAN_UNINSTALL));
      } else {
        do_check_false(hasFlag(addon.permissions, AddonManager.PERM_CAN_UNINSTALL));
      }

      // Verify the add-ons file is in the right place
      let file = expectedDir.clone();
      file.append(id + ".xpi");
      do_check_true(file.exists());
      do_check_true(file.isFile());

      let uri = addon.getResourceURI(null);
      do_check_true(uri instanceof AM_Ci.nsIFileURL);
      do_check_eq(uri.file.path, file.path);

      if (isUpgrade) {
        do_check_eq(addon.signedState, AddonManager.SIGNEDSTATE_SYSTEM);
      }

      // Verify the add-on actually started
      BootstrapMonitor.checkAddonStarted(id, version);
    }
    else {
      if (isUpgrade) {
        // Add-on should not be installed
        do_check_eq(addon, null);
      }
      else {
        // Either add-on should not be installed or it shouldn't be active
        do_check_true(!addon || !addon.isActive);
      }

      BootstrapMonitor.checkAddonNotStarted(id);

      if (addon)
        BootstrapMonitor.checkAddonInstalled(id);
      else
        BootstrapMonitor.checkAddonNotInstalled(id);
    }
  }
}

// Test with a missing features directory
add_task(function* test_missing_app_dir() {
  startupManager();

  let conditions = [
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
  ];

  yield check_installed(conditions);

  do_check_false(updatesDir.exists());

  yield promiseShutdownManager();
});

// Add some features in a new version
add_task(function* test_new_version() {
  gAppInfo.version = "1";
  distroDir.leafName = "app1";
  startupManager();

  let conditions = [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: null },
  ];

  yield check_installed(conditions);

  do_check_false(updatesDir.exists());

  yield promiseShutdownManager();
});

// Another new version swaps one feature and upgrades another
add_task(function* test_upgrade() {
  gAppInfo.version = "2";
  distroDir.leafName = "app2";
  startupManager();

  let conditions = [
      { isUpgrade: false, version: "2.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: "1.0" },
  ];

  yield check_installed(conditions);

  do_check_false(updatesDir.exists());

  yield promiseShutdownManager();
});

// Downgrade
add_task(function* test_downgrade() {
  gAppInfo.version = "1";
  distroDir.leafName = "app1";
  startupManager();

  let conditions = [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: null },
  ];

  yield check_installed(conditions);

  do_check_false(updatesDir.exists());

  yield promiseShutdownManager();
});

// Fake a mid-cycle install
add_task(function* test_updated() {
  // Create a random dir to install into
  let dirname = makeUUID();
  FileUtils.getDir("ProfD", ["features", dirname], true);
  updatesDir.append(dirname);

  // Copy in the system add-ons
    let file = do_get_file("data/system_addons/system2_2.xpi");
  file.copyTo(updatesDir, "system2@tests.mozilla.org.xpi");
  file = do_get_file("data/system_addons/system3_2.xpi");
  file.copyTo(updatesDir, "system3@tests.mozilla.org.xpi");

  // Inject it into the system set
  let addonSet = {
    schema: 1,
    directory: updatesDir.leafName,
    addons: {
      "system2@tests.mozilla.org": {
        version: "2.0"
      },
      "system3@tests.mozilla.org": {
        version: "2.0"
      },
    }
  };
  Services.prefs.setCharPref(PREF_SYSTEM_ADDON_SET, JSON.stringify(addonSet));

  startupManager(false);

  let conditions = [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: true, version: "2.0" },
  ];

  yield check_installed(conditions);

  yield promiseShutdownManager();
});

// Entering safe mode should disable the updated system add-ons and use the
// default system add-ons
add_task(function* safe_mode_disabled() {
  gAppInfo.inSafeMode = true;
  startupManager(false);

  let conditions = [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: null },
  ];

  yield check_installed(conditions);

  yield promiseShutdownManager();
});

// Leaving safe mode should re-enable the updated system add-ons
add_task(function* normal_mode_enabled() {
  gAppInfo.inSafeMode = false;
  startupManager(false);

  let conditions = [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: true, version: "2.0" },
  ];

  yield check_installed(conditions);

  yield promiseShutdownManager();
});

// An additional add-on in the directory should be ignored
add_task(function* test_skips_additional() {
  // Copy in the system add-ons
  let file = do_get_file("data/system_addons/system4_1.xpi");
  file.copyTo(updatesDir, "system4@tests.mozilla.org.xpi");

  startupManager(false);

  let conditions = [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: true, version: "2.0" },
  ];

  yield check_installed(conditions);

  yield promiseShutdownManager();
});

// Missing add-on should revert to the default set
add_task(function* test_revert() {
  manuallyUninstall(updatesDir, "system2@tests.mozilla.org");

  // With the add-on physically gone from disk we won't see uninstall events
  BootstrapMonitor.clear("system2@tests.mozilla.org");

  startupManager(false);

  // With system add-on 2 gone the updated set is now invalid so it reverts to
  // the default set which is system add-ons 1 and 2.
  let conditions = [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: null },
  ];

  yield check_installed(conditions);

  yield promiseShutdownManager();
});

// Putting it back will make the set work again
add_task(function* test_reuse() {
  let file = do_get_file("data/system_addons/system2_2.xpi");
  file.copyTo(updatesDir, "system2@tests.mozilla.org.xpi");

  startupManager(false);

  let conditions = [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: true, version: "2.0" },
  ];

  yield check_installed(conditions);

  yield promiseShutdownManager();
});

// Making the pref corrupt should revert to the default set
add_task(function* test_corrupt_pref() {
  Services.prefs.setCharPref(PREF_SYSTEM_ADDON_SET, "foo");

  startupManager(false);

  let conditions = [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: null },
  ];

  yield check_installed(conditions);

  yield promiseShutdownManager();
});

// An add-on with a bad certificate should cause us to use the default set
add_task(function* test_bad_profile_cert() {
  let file = do_get_file("data/system_addons/system1_1_badcert.xpi");
  file.copyTo(updatesDir, "system1@tests.mozilla.org.xpi");

  // Inject it into the system set
  let addonSet = {
    schema: 1,
    directory: updatesDir.leafName,
    addons: {
      "system1@tests.mozilla.org": {
        version: "2.0"
      },
      "system2@tests.mozilla.org": {
        version: "1.0"
      },
      "system3@tests.mozilla.org": {
        version: "1.0"
      },
    }
  };
  Services.prefs.setCharPref(PREF_SYSTEM_ADDON_SET, JSON.stringify(addonSet));

  startupManager(false);

  let conditions = [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: null },
  ];

  yield check_installed(conditions);

  yield promiseShutdownManager();
});

// Switching to app defaults that contain a bad certificate should still work
add_task(function* test_bad_app_cert() {
  gAppInfo.version = "3";
  distroDir.leafName = "app3";
  startupManager();

  // Add-on will still be present
  let addon = yield promiseAddonByID("system1@tests.mozilla.org");
  do_check_neq(addon, null);
  do_check_eq(addon.signedState, AddonManager.SIGNEDSTATE_NOT_REQUIRED);

  let conditions = [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: "1.0" },
  ];

  yield check_installed(conditions);

  yield promiseShutdownManager();
});

// A failed upgrade should revert to the default set.
add_task(function* test_updated() {
  // Create a random dir to install into
  let dirname = makeUUID();
  FileUtils.getDir("ProfD", ["features", dirname], true);
  updatesDir.append(dirname);

  // Copy in the system add-ons
  let file = do_get_file("data/system_addons/system2_2.xpi");
  file.copyTo(updatesDir, "system2@tests.mozilla.org.xpi");
  file = do_get_file("data/system_addons/system_failed_update.xpi");
  file.copyTo(updatesDir, "system_failed_update@tests.mozilla.org.xpi");

  // Inject it into the system set
  let addonSet = {
    schema: 1,
    directory: updatesDir.leafName,
    addons: {
      "system2@tests.mozilla.org": {
        version: "2.0"
      },
      "system_failed_update@tests.mozilla.org": {
        version: "1.0"
      },
    }
  };
  Services.prefs.setCharPref(PREF_SYSTEM_ADDON_SET, JSON.stringify(addonSet));

  startupManager(false);

  let conditions = [
      { isUpgrade: false, version: "1.0" },
  ];

  yield check_installed(conditions);

  yield promiseShutdownManager();
});