summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/webextensions/test/xpcshell/test_updatecheck.js
blob: e8529bb2cb3bb0258b60a5ace028fca31238730d (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// This verifies that AddonUpdateChecker works correctly

Components.utils.import("resource://gre/modules/addons/AddonUpdateChecker.jsm");

Components.utils.import("resource://testing-common/httpd.js");

var testserver = createHttpServer(4444);
testserver.registerDirectory("/data/", do_get_file("data"));

function checkUpdates(aId, aUpdateKey, aUpdateFile) {
  return new Promise((resolve, reject) => {
    AddonUpdateChecker.checkForUpdates(aId, aUpdateKey, `http://localhost:4444/data/${aUpdateFile}`, {
      onUpdateCheckComplete: resolve,

      onUpdateCheckError: function(status) {
        let error = new Error("Update check failed with status " + status);
        error.status = status;
        reject(error);
      }
    });
  });
}

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

  run_next_test();
}

// Test that a basic update check returns the expected available updates
add_task(function* () {
  for (let file of ["test_updatecheck.rdf", "test_updatecheck.json"]) {
    let updates = yield checkUpdates("updatecheck1@tests.mozilla.org", null, file);

    equal(updates.length, 5);
    let update = AddonUpdateChecker.getNewestCompatibleUpdate(updates);
    notEqual(update, null);
    equal(update.version, "3.0");
    update = AddonUpdateChecker.getCompatibilityUpdate(updates, "2");
    notEqual(update, null);
    equal(update.version, "2.0");
    equal(update.targetApplications[0].minVersion, "1");
    equal(update.targetApplications[0].maxVersion, "2");
  }
});

/*
 * Tests that the security checks are applied correctly
 *
 * Test     signature   updateHash  updateLink   expected
 *--------------------------------------------------------
 * 2        absent      absent      http         fail
 * 3        broken      absent      http         fail
 * 4        correct     absent      http         no update
 * 5        correct     sha1        http         update
 * 6        corrent     absent      https        update
 * 7        corrent     sha1        https        update
 * 8        corrent     md2         http         no update
 * 9        corrent     md2         https        update
 */

var updateKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDK426erD/H3XtsjvaB5+PJqbhj" +
                "Zc9EDI5OCJS8R3FIObJ9ZHJK1TXeaE7JWqt9WUmBWTEFvwS+FI9vWu8058N9CHhD" +
                "NyeP6i4LuUYjTURnn7Yw/IgzyIJ2oKsYa32RuxAyteqAWqPT/J63wBixIeCxmysf" +
                "awB/zH4KaPiY3vnrzQIDAQAB";

add_task(function* () {
  for (let file of ["test_updatecheck.rdf", "test_updatecheck.json"]) {
    try {
      yield checkUpdates("test_bug378216_5@tests.mozilla.org",
                         updateKey, file);
      throw "Expected the update check to fail";
    } catch (e) {}
  }
});

add_task(function* () {
  for (let file of ["test_updatecheck.rdf", "test_updatecheck.json"]) {
    try {
      yield checkUpdates("test_bug378216_7@tests.mozilla.org",
                         updateKey, file);

      throw "Expected the update check to fail";
    } catch (e) {}
  }
});

add_task(function* () {
  // Make sure that the JSON manifest is rejected when an update key is
  // required, but perform the remaining tests which aren't expected to fail
  // because of the update key, without requiring one for the JSON variant.

  try {
    let updates = yield checkUpdates("test_bug378216_8@tests.mozilla.org",
                                     updateKey, "test_updatecheck.json");

    throw "Expected the update check to fail";
  } catch (e) {}

  for (let [file, key] of [["test_updatecheck.rdf", updateKey],
                           ["test_updatecheck.json", null]]) {
    let updates = yield checkUpdates("test_bug378216_8@tests.mozilla.org",
                                     key, file);
    equal(updates.length, 1);
    ok(!("updateURL" in updates[0]));
  }
});

add_task(function* () {
  for (let [file, key] of [["test_updatecheck.rdf", updateKey],
                           ["test_updatecheck.json", null]]) {
    let updates = yield checkUpdates("test_bug378216_9@tests.mozilla.org",
                                     key, file);
    equal(updates.length, 1);
    equal(updates[0].version, "2.0");
    ok("updateURL" in updates[0]);
  }
});

add_task(function* () {
  for (let [file, key] of [["test_updatecheck.rdf", updateKey],
                           ["test_updatecheck.json", null]]) {
    let updates = yield checkUpdates("test_bug378216_10@tests.mozilla.org",
                                     key, file);
    equal(updates.length, 1);
    equal(updates[0].version, "2.0");
    ok("updateURL" in updates[0]);
  }
});

add_task(function* () {
  for (let [file, key] of [["test_updatecheck.rdf", updateKey],
                           ["test_updatecheck.json", null]]) {
    let updates = yield checkUpdates("test_bug378216_11@tests.mozilla.org",
                                     key, file);
    equal(updates.length, 1);
    equal(updates[0].version, "2.0");
    ok("updateURL" in updates[0]);
  }
});

add_task(function* () {
  for (let [file, key] of [["test_updatecheck.rdf", updateKey],
                           ["test_updatecheck.json", null]]) {
    let updates = yield checkUpdates("test_bug378216_12@tests.mozilla.org",
                                     key, file);
    equal(updates.length, 1);
    do_check_false("updateURL" in updates[0]);
  }
});

add_task(function* () {
  for (let [file, key] of [["test_updatecheck.rdf", updateKey],
                           ["test_updatecheck.json", null]]) {
    let updates = yield checkUpdates("test_bug378216_13@tests.mozilla.org",
                                     key, file);
    equal(updates.length, 1);
    equal(updates[0].version, "2.0");
    ok("updateURL" in updates[0]);
  }
});

add_task(function* () {
  for (let file of ["test_updatecheck.rdf", "test_updatecheck.json"]) {
    let updates = yield checkUpdates("test_bug378216_14@tests.mozilla.org",
                                     null, file);
    equal(updates.length, 0);
  }
});

add_task(function* () {
  for (let file of ["test_updatecheck.rdf", "test_updatecheck.json"]) {
    try {
      yield checkUpdates("test_bug378216_15@tests.mozilla.org",
                         null, file);

      throw "Update check should have failed";
    } catch (e) {
      equal(e.status, AddonUpdateChecker.ERROR_PARSE_ERROR);
    }
  }
});

add_task(function* () {
  for (let file of ["test_updatecheck.rdf", "test_updatecheck.json"]) {
    let updates = yield checkUpdates("ignore-compat@tests.mozilla.org",
                                     null, file);
    equal(updates.length, 3);
    let update = AddonUpdateChecker.getNewestCompatibleUpdate(
      updates, null, null, true);
    notEqual(update, null);
    equal(update.version, 2);
  }
});

add_task(function* () {
  for (let file of ["test_updatecheck.rdf", "test_updatecheck.json"]) {
    let updates = yield checkUpdates("compat-override@tests.mozilla.org",
                                     null, file);
    equal(updates.length, 3);
    let overrides = [{
      type: "incompatible",
      minVersion: 1,
      maxVersion: 2,
      appID: "xpcshell@tests.mozilla.org",
      appMinVersion: 0.1,
      appMaxVersion: 0.2
    }, {
      type: "incompatible",
      minVersion: 2,
      maxVersion: 2,
      appID: "xpcshell@tests.mozilla.org",
      appMinVersion: 1,
      appMaxVersion: 2
    }];
    let update = AddonUpdateChecker.getNewestCompatibleUpdate(
      updates, null, null, true, false, overrides);
    notEqual(update, null);
    equal(update.version, 1);
  }
});

add_task(function* () {
  for (let file of ["test_updatecheck.rdf", "test_updatecheck.json"]) {
    let updates = yield checkUpdates("compat-strict-optin@tests.mozilla.org",
                                     null, file);
    equal(updates.length, 1);
    let update = AddonUpdateChecker.getNewestCompatibleUpdate(
      updates, null, null, true, false);
    equal(update, null);
  }
});