summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_system_profile_location.js
blob: 9dbf8f707078f1f7875e5f791c31a747f0d4aec9 (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
"use strict";

/* globals browser */
let scopes = AddonManager.SCOPE_PROFILE | AddonManager.SCOPE_APPLICATION;
Services.prefs.setIntPref("extensions.enabledScopes", scopes);

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

AddonTestUtils.usePrivilegedSignatures = id => id.startsWith("system");

async function promiseInstallSystemProfileExtension(id, hidden) {
  let xpi = await AddonTestUtils.createTempWebExtensionFile({
    manifest: {
      browser_specific_settings: { gecko: { id } },
      hidden,
    },
    background() {
      browser.test.sendMessage("started");
    },
  });
  let wrapper = ExtensionTestUtils.expectExtension(id);

  const install = await AddonManager.getInstallForURL(`file://${xpi.path}`, {
    useSystemLocation: true, // KEY_APP_SYSTEM_PROFILE
  });

  install.install();

  await wrapper.awaitStartup();
  await wrapper.awaitMessage("started");
  return wrapper;
}

async function promiseUninstall(id) {
  await AddonManager.uninstallSystemProfileAddon(id);

  let addon = await promiseAddonByID(id);
  equal(addon, null, "Addon is gone after uninstall");
}

// Tests installing an extension into the app-system-profile location.
add_task(async function test_system_profile_location() {
  let id = "system@tests.mozilla.org";
  await AddonTestUtils.promiseStartupManager();
  let wrapper = await promiseInstallSystemProfileExtension(id);

  let addon = await promiseAddonByID(id);
  notEqual(addon, null, "Addon is installed");
  ok(addon.isActive, "Addon is active");
  ok(addon.isPrivileged, "Addon is privileged");
  ok(wrapper.extension.isAppProvided, "Addon is app provided");
  ok(!addon.hidden, "Addon is not hidden");
  equal(
    addon.signedState,
    AddonManager.SIGNEDSTATE_PRIVILEGED,
    "Addon is system signed"
  );

  // After a restart, the extension should start up normally.
  await promiseRestartManager();
  await wrapper.awaitStartup();
  await wrapper.awaitMessage("started");
  ok(true, "Extension in app-system-profile location ran after restart");

  addon = await promiseAddonByID(id);
  notEqual(addon, null, "Addon is installed");
  ok(addon.isActive, "Addon is active");

  // After a restart that causes a database rebuild, it should still work
  await promiseRestartManager("2");
  await wrapper.awaitStartup();
  await wrapper.awaitMessage("started");
  ok(true, "Extension in app-system-profile location ran after restart");

  addon = await promiseAddonByID(id);
  notEqual(addon, null, "Addon is installed");
  ok(addon.isActive, "Addon is active");

  // After a restart that changes the schema version, it should still work
  await promiseShutdownManager();
  Services.prefs.setIntPref("extensions.databaseSchema", 0);
  await promiseStartupManager();

  await wrapper.awaitStartup();
  await wrapper.awaitMessage("started");
  ok(true, "Extension in app-system-profile location ran after restart");

  addon = await promiseAddonByID(id);
  notEqual(addon, null, "Addon is installed");
  ok(addon.isActive, "Addon is active");

  await promiseUninstall(id);

  await AddonTestUtils.promiseShutdownManager();
});

// Tests installing a hidden extension in the app-system-profile location.
add_task(async function test_system_profile_location_hidden() {
  let id = "system-hidden@tests.mozilla.org";
  await AddonTestUtils.promiseStartupManager();
  await promiseInstallSystemProfileExtension(id, true);

  let addon = await promiseAddonByID(id);
  notEqual(addon, null, "Addon is installed");
  ok(addon.isActive, "Addon is active");
  ok(addon.isPrivileged, "Addon is privileged");
  ok(addon.hidden, "Addon is hidden");

  await promiseUninstall(id);

  await AddonTestUtils.promiseShutdownManager();
});

add_task(async function test_system_profile_location_installFile() {
  await AddonTestUtils.promiseStartupManager();
  let id = "system-fileinstall@test";
  let xpi = await AddonTestUtils.createTempWebExtensionFile({
    manifest: {
      browser_specific_settings: { gecko: { id } },
    },
    background() {
      browser.test.sendMessage("started");
    },
  });
  let wrapper = ExtensionTestUtils.expectExtension(id);

  const install = await AddonManager.getInstallForFile(xpi, null, null, true);
  install.install();

  await wrapper.awaitStartup();
  await wrapper.awaitMessage("started");

  await promiseUninstall(id);

  await AddonTestUtils.promiseShutdownManager();
});

add_task(async function test_system_profile_location_overridden() {
  await AddonTestUtils.promiseStartupManager();
  let id = "system-fileinstall@test";
  let xpi = await AddonTestUtils.createTempWebExtensionFile({
    manifest: {
      version: "1.0",
      browser_specific_settings: { gecko: { id } },
    },
  });

  let install = await AddonManager.getInstallForFile(xpi, null, null, true);
  await install.install();

  let addon = await promiseAddonByID(id);
  equal(addon.version, "1.0", "Addon is installed");

  // Install a user profile version on top of the system profile location.
  xpi = await AddonTestUtils.createTempWebExtensionFile({
    manifest: {
      version: "2.0",
      browser_specific_settings: { gecko: { id } },
    },
  });

  install = await AddonManager.getInstallForFile(xpi);
  await install.install();

  addon = await promiseAddonByID(id);
  equal(addon.version, "2.0", "Addon is upgraded");

  // Uninstall the system profile addon.
  await AddonManager.uninstallSystemProfileAddon(id);

  addon = await promiseAddonByID(id);
  equal(addon.version, "2.0", "Addon is still active");

  await addon.uninstall();
  await AddonTestUtils.promiseShutdownManager();
});

add_task(async function test_system_profile_location_require_system_cert() {
  await AddonTestUtils.promiseStartupManager();
  let id = "fail@test";
  let xpi = await AddonTestUtils.createTempWebExtensionFile({
    manifest: {
      browser_specific_settings: { gecko: { id } },
    },
  });
  const install = await AddonManager.getInstallForURL(`file://${xpi.path}`, {
    useSystemLocation: true, // KEY_APP_SYSTEM_PROFILE
  });

  await install.install();

  let addon = await promiseAddonByID(id);
  ok(!addon.isPrivileged, "Addon is not privileged");
  ok(!addon.isActive, "Addon is not active");
  equal(addon.signedState, AddonManager.SIGNEDSTATE_SIGNED, "Addon is signed");

  await addon.uninstall();
  await AddonTestUtils.promiseShutdownManager();
});