summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_signed_verify.js
blob: 0b5b30d89fed8f3f1c3467184bc35e4e6ffc23c4 (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
// Enable signature checks for these tests
gUseRealCertChecks = true;
// Disable update security
Services.prefs.setBoolPref(PREF_EM_CHECK_UPDATE_SECURITY, false);

const DATA = "data/signing_checks/";
const GOOD = [
  ["signed_bootstrap_2.xpi", AddonManager.SIGNEDSTATE_SIGNED],
  ["signed_nonbootstrap_2.xpi", AddonManager.SIGNEDSTATE_SIGNED]
];
const BAD = [
  ["unsigned_bootstrap_2.xpi", AddonManager.SIGNEDSTATE_MISSING],
  ["signed_bootstrap_badid_2.xpi", AddonManager.SIGNEDSTATE_BROKEN],
  ["unsigned_nonbootstrap_2.xpi", AddonManager.SIGNEDSTATE_MISSING],
  ["signed_nonbootstrap_badid_2.xpi", AddonManager.SIGNEDSTATE_BROKEN],
];
const ID = "test@tests.mozilla.org";

const profileDir = gProfD.clone();
profileDir.append("extensions");

function verifySignatures() {
  return new Promise(resolve => {
    let observer = (subject, topic, data) => {
      Services.obs.removeObserver(observer, "xpi-signature-changed");
      resolve(JSON.parse(data));
    }
    Services.obs.addObserver(observer, "xpi-signature-changed", false);

    do_print("Verifying signatures");
    let XPIscope = Components.utils.import("resource://gre/modules/addons/XPIProvider.jsm");
    XPIscope.XPIProvider.verifySignatures();
  });
}

function run_test() {
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "4", "4");

  run_next_test();
}

function verify_no_change([startFile, startState], [endFile, endState]) {
  add_task(function*() {
    do_print("A switch from " + startFile + " to " + endFile + " should cause no change.");

    // Install the first add-on
    manuallyInstall(do_get_file(DATA + startFile), profileDir, ID);
    startupManager();

    let addon = yield promiseAddonByID(ID);
    do_check_neq(addon, null);
    let wasAppDisabled = addon.appDisabled;
    do_check_neq(addon.appDisabled, addon.isActive);
    do_check_eq(addon.pendingOperations, AddonManager.PENDING_NONE);
    do_check_eq(addon.signedState, startState);

    // Swap in the files from the next add-on
    manuallyUninstall(profileDir, ID);
    manuallyInstall(do_get_file(DATA + endFile), profileDir, ID);

    let events = {
      [ID]: []
    };

    if (startState != endState)
      events[ID].unshift(["onPropertyChanged", ["signedState"]]);

    prepare_test(events);

    // Trigger the check
    let changes = yield verifySignatures();
    do_check_eq(changes.enabled.length, 0);
    do_check_eq(changes.disabled.length, 0);

    do_check_eq(addon.appDisabled, wasAppDisabled);
    do_check_neq(addon.appDisabled, addon.isActive);
    do_check_eq(addon.pendingOperations, AddonManager.PENDING_NONE);
    do_check_eq(addon.signedState, endState);

    // Remove the add-on and restart to let it go away
    manuallyUninstall(profileDir, ID);
    yield promiseRestartManager();
    yield promiseShutdownManager();
  });
}

function verify_enables([startFile, startState], [endFile, endState]) {
  add_task(function*() {
    do_print("A switch from " + startFile + " to " + endFile + " should enable the add-on.");

    // Install the first add-on
    manuallyInstall(do_get_file(DATA + startFile), profileDir, ID);
    startupManager();

    let addon = yield promiseAddonByID(ID);
    do_check_neq(addon, null);
    do_check_false(addon.isActive);
    do_check_eq(addon.pendingOperations, AddonManager.PENDING_NONE);
    do_check_eq(addon.signedState, startState);

    // Swap in the files from the next add-on
    manuallyUninstall(profileDir, ID);
    manuallyInstall(do_get_file(DATA + endFile), profileDir, ID);

    let needsRestart = hasFlag(addon.operationsRequiringRestart, AddonManager.OP_NEEDS_RESTART_ENABLE);
    do_print(needsRestart);

    let events = {};
    if (!needsRestart) {
      events[ID] = [
        ["onPropertyChanged", ["appDisabled"]],
        ["onEnabling", false],
        "onEnabled"
      ];
    }
    else {
      events[ID] = [
        ["onPropertyChanged", ["appDisabled"]],
        "onEnabling"
      ];
    }

    if (startState != endState)
      events[ID].unshift(["onPropertyChanged", ["signedState"]]);

    prepare_test(events);

    // Trigger the check
    let changes = yield verifySignatures();
    do_check_eq(changes.enabled.length, 1);
    do_check_eq(changes.enabled[0], ID);
    do_check_eq(changes.disabled.length, 0);

    do_check_false(addon.appDisabled);
    if (needsRestart)
      do_check_neq(addon.pendingOperations, AddonManager.PENDING_NONE);
    else
      do_check_true(addon.isActive);
    do_check_eq(addon.signedState, endState);

    ensure_test_completed();

    // Remove the add-on and restart to let it go away
    manuallyUninstall(profileDir, ID);
    yield promiseRestartManager();
    yield promiseShutdownManager();
  });
}

function verify_disables([startFile, startState], [endFile, endState]) {
  add_task(function*() {
    do_print("A switch from " + startFile + " to " + endFile + " should disable the add-on.");

    // Install the first add-on
    manuallyInstall(do_get_file(DATA + startFile), profileDir, ID);
    startupManager();

    let addon = yield promiseAddonByID(ID);
    do_check_neq(addon, null);
    do_check_true(addon.isActive);
    do_check_eq(addon.pendingOperations, AddonManager.PENDING_NONE);
    do_check_eq(addon.signedState, startState);

    let needsRestart = hasFlag(addon.operationsRequiringRestart, AddonManager.OP_NEEDS_RESTART_DISABLE);

    // Swap in the files from the next add-on
    manuallyUninstall(profileDir, ID);
    manuallyInstall(do_get_file(DATA + endFile), profileDir, ID);

    let events = {};
    if (!needsRestart) {
      events[ID] = [
        ["onPropertyChanged", ["appDisabled"]],
        ["onDisabling", false],
        "onDisabled"
      ];
    }
    else {
      events[ID] = [
        ["onPropertyChanged", ["appDisabled"]],
        "onDisabling"
      ];
    }

    if (startState != endState)
      events[ID].unshift(["onPropertyChanged", ["signedState"]]);

    prepare_test(events);

    // Trigger the check
    let changes = yield verifySignatures();
    do_check_eq(changes.enabled.length, 0);
    do_check_eq(changes.disabled.length, 1);
    do_check_eq(changes.disabled[0], ID);

    do_check_true(addon.appDisabled);
    if (needsRestart)
      do_check_neq(addon.pendingOperations, AddonManager.PENDING_NONE);
    else
      do_check_false(addon.isActive);
    do_check_eq(addon.signedState, endState);

    ensure_test_completed();

    // Remove the add-on and restart to let it go away
    manuallyUninstall(profileDir, ID);
    yield promiseRestartManager();
    yield promiseShutdownManager();
  });
}

for (let start of GOOD) {
  for (let end of BAD) {
    verify_disables(start, end);
  }
}

for (let start of BAD) {
  for (let end of GOOD) {
    verify_enables(start, end);
  }
}

for (let start of GOOD) {
  for (let end of GOOD.filter(f => f != start)) {
    verify_no_change(start, end);
  }
}

for (let start of BAD) {
  for (let end of BAD.filter(f => f != start)) {
    verify_no_change(start, end);
  }
}