|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
3 */ |
|
4 |
|
5 const xpi = RELATIVE_DIR + "addons/browser_installssl.xpi"; |
|
6 const redirect = RELATIVE_DIR + "redirect.sjs?"; |
|
7 const SUCCESS = 0; |
|
8 const NETWORK_FAILURE = AddonManager.ERROR_NETWORK_FAILURE; |
|
9 |
|
10 const HTTP = "http://example.com/"; |
|
11 const HTTPS = "https://example.com/"; |
|
12 const NOCERT = "https://nocert.example.com/"; |
|
13 const SELFSIGNED = "https://self-signed.example.com/"; |
|
14 const UNTRUSTED = "https://untrusted.example.com/"; |
|
15 const EXPIRED = "https://expired.example.com/"; |
|
16 |
|
17 const PREF_INSTALL_REQUIREBUILTINCERTS = "extensions.install.requireBuiltInCerts"; |
|
18 |
|
19 var gTests = []; |
|
20 var gStart = 0; |
|
21 var gLast = 0; |
|
22 var gPendingInstall = null; |
|
23 |
|
24 function test() { |
|
25 gStart = Date.now(); |
|
26 requestLongerTimeout(4); |
|
27 waitForExplicitFinish(); |
|
28 |
|
29 registerCleanupFunction(function() { |
|
30 var cos = Cc["@mozilla.org/security/certoverride;1"]. |
|
31 getService(Ci.nsICertOverrideService); |
|
32 cos.clearValidityOverride("nocert.example.com", -1); |
|
33 cos.clearValidityOverride("self-signed.example.com", -1); |
|
34 cos.clearValidityOverride("untrusted.example.com", -1); |
|
35 cos.clearValidityOverride("expired.example.com", -1); |
|
36 |
|
37 try { |
|
38 Services.prefs.clearUserPref(PREF_INSTALL_REQUIREBUILTINCERTS); |
|
39 } |
|
40 catch (e) { |
|
41 } |
|
42 |
|
43 if (gPendingInstall) { |
|
44 gTests = []; |
|
45 ok(false, "Timed out in the middle of downloading " + gPendingInstall.sourceURI.spec); |
|
46 try { |
|
47 gPendingInstall.cancel(); |
|
48 } |
|
49 catch (e) { |
|
50 } |
|
51 } |
|
52 }); |
|
53 |
|
54 run_next_test(); |
|
55 } |
|
56 |
|
57 function end_test() { |
|
58 info("All tests completed in " + (Date.now() - gStart) + "ms"); |
|
59 finish(); |
|
60 } |
|
61 |
|
62 function add_install_test(mainURL, redirectURL, expectedStatus) { |
|
63 gTests.push([mainURL, redirectURL, expectedStatus]); |
|
64 } |
|
65 |
|
66 function run_install_tests(callback) { |
|
67 function run_next_install_test() { |
|
68 if (gTests.length == 0) { |
|
69 callback(); |
|
70 return; |
|
71 } |
|
72 gLast = Date.now(); |
|
73 |
|
74 let [mainURL, redirectURL, expectedStatus] = gTests.shift(); |
|
75 if (redirectURL) { |
|
76 var url = mainURL + redirect + redirectURL + xpi; |
|
77 var message = "Should have seen the right result for an install redirected from " + |
|
78 mainURL + " to " + redirectURL; |
|
79 } |
|
80 else { |
|
81 url = mainURL + xpi; |
|
82 message = "Should have seen the right result for an install from " + |
|
83 mainURL; |
|
84 } |
|
85 |
|
86 AddonManager.getInstallForURL(url, function(install) { |
|
87 gPendingInstall = install; |
|
88 install.addListener({ |
|
89 onDownloadEnded: function(install) { |
|
90 is(SUCCESS, expectedStatus, message); |
|
91 info("Install test ran in " + (Date.now() - gLast) + "ms"); |
|
92 // Don't proceed with the install |
|
93 install.cancel(); |
|
94 gPendingInstall = null; |
|
95 run_next_install_test(); |
|
96 return false; |
|
97 }, |
|
98 |
|
99 onDownloadFailed: function(install) { |
|
100 is(install.error, expectedStatus, message); |
|
101 info("Install test ran in " + (Date.now() - gLast) + "ms"); |
|
102 gPendingInstall = null; |
|
103 run_next_install_test(); |
|
104 } |
|
105 }); |
|
106 install.install(); |
|
107 }, "application/x-xpinstall"); |
|
108 } |
|
109 |
|
110 run_next_install_test(); |
|
111 } |
|
112 |
|
113 // Add overrides for the bad certificates |
|
114 function addCertOverrides() { |
|
115 addCertOverride("nocert.example.com", Ci.nsICertOverrideService.ERROR_MISMATCH); |
|
116 addCertOverride("self-signed.example.com", Ci.nsICertOverrideService.ERROR_UNTRUSTED); |
|
117 addCertOverride("untrusted.example.com", Ci.nsICertOverrideService.ERROR_UNTRUSTED); |
|
118 addCertOverride("expired.example.com", Ci.nsICertOverrideService.ERROR_TIME); |
|
119 } |
|
120 |
|
121 // Runs tests with built-in certificates required, no certificate exceptions |
|
122 // and no hashes |
|
123 add_test(function() { |
|
124 // Tests that a simple install works as expected. |
|
125 add_install_test(HTTP, null, SUCCESS); |
|
126 add_install_test(HTTPS, null, NETWORK_FAILURE); |
|
127 add_install_test(NOCERT, null, NETWORK_FAILURE); |
|
128 add_install_test(SELFSIGNED, null, NETWORK_FAILURE); |
|
129 add_install_test(UNTRUSTED, null, NETWORK_FAILURE); |
|
130 add_install_test(EXPIRED, null, NETWORK_FAILURE); |
|
131 |
|
132 // Tests that redirecting from http to other servers works as expected |
|
133 add_install_test(HTTP, HTTP, SUCCESS); |
|
134 add_install_test(HTTP, HTTPS, SUCCESS); |
|
135 add_install_test(HTTP, NOCERT, NETWORK_FAILURE); |
|
136 add_install_test(HTTP, SELFSIGNED, NETWORK_FAILURE); |
|
137 add_install_test(HTTP, UNTRUSTED, NETWORK_FAILURE); |
|
138 add_install_test(HTTP, EXPIRED, NETWORK_FAILURE); |
|
139 |
|
140 // Tests that redirecting from valid https to other servers works as expected |
|
141 add_install_test(HTTPS, HTTP, NETWORK_FAILURE); |
|
142 add_install_test(HTTPS, HTTPS, NETWORK_FAILURE); |
|
143 add_install_test(HTTPS, NOCERT, NETWORK_FAILURE); |
|
144 add_install_test(HTTPS, SELFSIGNED, NETWORK_FAILURE); |
|
145 add_install_test(HTTPS, UNTRUSTED, NETWORK_FAILURE); |
|
146 add_install_test(HTTPS, EXPIRED, NETWORK_FAILURE); |
|
147 |
|
148 // Tests that redirecting from nocert https to other servers works as expected |
|
149 add_install_test(NOCERT, HTTP, NETWORK_FAILURE); |
|
150 add_install_test(NOCERT, HTTPS, NETWORK_FAILURE); |
|
151 add_install_test(NOCERT, NOCERT, NETWORK_FAILURE); |
|
152 add_install_test(NOCERT, SELFSIGNED, NETWORK_FAILURE); |
|
153 add_install_test(NOCERT, UNTRUSTED, NETWORK_FAILURE); |
|
154 add_install_test(NOCERT, EXPIRED, NETWORK_FAILURE); |
|
155 |
|
156 // Tests that redirecting from self-signed https to other servers works as expected |
|
157 add_install_test(SELFSIGNED, HTTP, NETWORK_FAILURE); |
|
158 add_install_test(SELFSIGNED, HTTPS, NETWORK_FAILURE); |
|
159 add_install_test(SELFSIGNED, NOCERT, NETWORK_FAILURE); |
|
160 add_install_test(SELFSIGNED, SELFSIGNED, NETWORK_FAILURE); |
|
161 add_install_test(SELFSIGNED, UNTRUSTED, NETWORK_FAILURE); |
|
162 add_install_test(SELFSIGNED, EXPIRED, NETWORK_FAILURE); |
|
163 |
|
164 // Tests that redirecting from untrusted https to other servers works as expected |
|
165 add_install_test(UNTRUSTED, HTTP, NETWORK_FAILURE); |
|
166 add_install_test(UNTRUSTED, HTTPS, NETWORK_FAILURE); |
|
167 add_install_test(UNTRUSTED, NOCERT, NETWORK_FAILURE); |
|
168 add_install_test(UNTRUSTED, SELFSIGNED, NETWORK_FAILURE); |
|
169 add_install_test(UNTRUSTED, UNTRUSTED, NETWORK_FAILURE); |
|
170 add_install_test(UNTRUSTED, EXPIRED, NETWORK_FAILURE); |
|
171 |
|
172 // Tests that redirecting from expired https to other servers works as expected |
|
173 add_install_test(EXPIRED, HTTP, NETWORK_FAILURE); |
|
174 add_install_test(EXPIRED, HTTPS, NETWORK_FAILURE); |
|
175 add_install_test(EXPIRED, NOCERT, NETWORK_FAILURE); |
|
176 add_install_test(EXPIRED, SELFSIGNED, NETWORK_FAILURE); |
|
177 add_install_test(EXPIRED, UNTRUSTED, NETWORK_FAILURE); |
|
178 add_install_test(EXPIRED, EXPIRED, NETWORK_FAILURE); |
|
179 |
|
180 run_install_tests(run_next_test); |
|
181 }); |
|
182 |
|
183 // Runs tests without requiring built-in certificates, no certificate |
|
184 // exceptions and no hashes |
|
185 add_test(function() { |
|
186 Services.prefs.setBoolPref(PREF_INSTALL_REQUIREBUILTINCERTS, false); |
|
187 |
|
188 // Tests that a simple install works as expected. |
|
189 add_install_test(HTTP, null, SUCCESS); |
|
190 add_install_test(HTTPS, null, SUCCESS); |
|
191 add_install_test(NOCERT, null, NETWORK_FAILURE); |
|
192 add_install_test(SELFSIGNED, null, NETWORK_FAILURE); |
|
193 add_install_test(UNTRUSTED, null, NETWORK_FAILURE); |
|
194 add_install_test(EXPIRED, null, NETWORK_FAILURE); |
|
195 |
|
196 // Tests that redirecting from http to other servers works as expected |
|
197 add_install_test(HTTP, HTTP, SUCCESS); |
|
198 add_install_test(HTTP, HTTPS, SUCCESS); |
|
199 add_install_test(HTTP, NOCERT, NETWORK_FAILURE); |
|
200 add_install_test(HTTP, SELFSIGNED, NETWORK_FAILURE); |
|
201 add_install_test(HTTP, UNTRUSTED, NETWORK_FAILURE); |
|
202 add_install_test(HTTP, EXPIRED, NETWORK_FAILURE); |
|
203 |
|
204 // Tests that redirecting from valid https to other servers works as expected |
|
205 add_install_test(HTTPS, HTTP, NETWORK_FAILURE); |
|
206 add_install_test(HTTPS, HTTPS, SUCCESS); |
|
207 add_install_test(HTTPS, NOCERT, NETWORK_FAILURE); |
|
208 add_install_test(HTTPS, SELFSIGNED, NETWORK_FAILURE); |
|
209 add_install_test(HTTPS, UNTRUSTED, NETWORK_FAILURE); |
|
210 add_install_test(HTTPS, EXPIRED, NETWORK_FAILURE); |
|
211 |
|
212 // Tests that redirecting from nocert https to other servers works as expected |
|
213 add_install_test(NOCERT, HTTP, NETWORK_FAILURE); |
|
214 add_install_test(NOCERT, HTTPS, NETWORK_FAILURE); |
|
215 add_install_test(NOCERT, NOCERT, NETWORK_FAILURE); |
|
216 add_install_test(NOCERT, SELFSIGNED, NETWORK_FAILURE); |
|
217 add_install_test(NOCERT, UNTRUSTED, NETWORK_FAILURE); |
|
218 add_install_test(NOCERT, EXPIRED, NETWORK_FAILURE); |
|
219 |
|
220 // Tests that redirecting from self-signed https to other servers works as expected |
|
221 add_install_test(SELFSIGNED, HTTP, NETWORK_FAILURE); |
|
222 add_install_test(SELFSIGNED, HTTPS, NETWORK_FAILURE); |
|
223 add_install_test(SELFSIGNED, NOCERT, NETWORK_FAILURE); |
|
224 add_install_test(SELFSIGNED, SELFSIGNED, NETWORK_FAILURE); |
|
225 add_install_test(SELFSIGNED, UNTRUSTED, NETWORK_FAILURE); |
|
226 add_install_test(SELFSIGNED, EXPIRED, NETWORK_FAILURE); |
|
227 |
|
228 // Tests that redirecting from untrusted https to other servers works as expected |
|
229 add_install_test(UNTRUSTED, HTTP, NETWORK_FAILURE); |
|
230 add_install_test(UNTRUSTED, HTTPS, NETWORK_FAILURE); |
|
231 add_install_test(UNTRUSTED, NOCERT, NETWORK_FAILURE); |
|
232 add_install_test(UNTRUSTED, SELFSIGNED, NETWORK_FAILURE); |
|
233 add_install_test(UNTRUSTED, UNTRUSTED, NETWORK_FAILURE); |
|
234 add_install_test(UNTRUSTED, EXPIRED, NETWORK_FAILURE); |
|
235 |
|
236 // Tests that redirecting from expired https to other servers works as expected |
|
237 add_install_test(EXPIRED, HTTP, NETWORK_FAILURE); |
|
238 add_install_test(EXPIRED, HTTPS, NETWORK_FAILURE); |
|
239 add_install_test(EXPIRED, NOCERT, NETWORK_FAILURE); |
|
240 add_install_test(EXPIRED, SELFSIGNED, NETWORK_FAILURE); |
|
241 add_install_test(EXPIRED, UNTRUSTED, NETWORK_FAILURE); |
|
242 add_install_test(EXPIRED, EXPIRED, NETWORK_FAILURE); |
|
243 |
|
244 run_install_tests(run_next_test); |
|
245 }); |
|
246 |
|
247 // Runs tests with built-in certificates required, all certificate exceptions |
|
248 // and no hashes |
|
249 add_test(function() { |
|
250 Services.prefs.clearUserPref(PREF_INSTALL_REQUIREBUILTINCERTS); |
|
251 addCertOverrides(); |
|
252 |
|
253 // Tests that a simple install works as expected. |
|
254 add_install_test(HTTP, null, SUCCESS); |
|
255 add_install_test(HTTPS, null, NETWORK_FAILURE); |
|
256 add_install_test(NOCERT, null, NETWORK_FAILURE); |
|
257 add_install_test(SELFSIGNED, null, NETWORK_FAILURE); |
|
258 add_install_test(UNTRUSTED, null, NETWORK_FAILURE); |
|
259 add_install_test(EXPIRED, null, NETWORK_FAILURE); |
|
260 |
|
261 // Tests that redirecting from http to other servers works as expected |
|
262 add_install_test(HTTP, HTTP, SUCCESS); |
|
263 add_install_test(HTTP, HTTPS, SUCCESS); |
|
264 add_install_test(HTTP, NOCERT, SUCCESS); |
|
265 add_install_test(HTTP, SELFSIGNED, SUCCESS); |
|
266 add_install_test(HTTP, UNTRUSTED, SUCCESS); |
|
267 add_install_test(HTTP, EXPIRED, SUCCESS); |
|
268 |
|
269 // Tests that redirecting from valid https to other servers works as expected |
|
270 add_install_test(HTTPS, HTTP, NETWORK_FAILURE); |
|
271 add_install_test(HTTPS, HTTPS, NETWORK_FAILURE); |
|
272 add_install_test(HTTPS, NOCERT, NETWORK_FAILURE); |
|
273 add_install_test(HTTPS, SELFSIGNED, NETWORK_FAILURE); |
|
274 add_install_test(HTTPS, UNTRUSTED, NETWORK_FAILURE); |
|
275 add_install_test(HTTPS, EXPIRED, NETWORK_FAILURE); |
|
276 |
|
277 // Tests that redirecting from nocert https to other servers works as expected |
|
278 add_install_test(NOCERT, HTTP, NETWORK_FAILURE); |
|
279 add_install_test(NOCERT, HTTPS, NETWORK_FAILURE); |
|
280 add_install_test(NOCERT, NOCERT, NETWORK_FAILURE); |
|
281 add_install_test(NOCERT, SELFSIGNED, NETWORK_FAILURE); |
|
282 add_install_test(NOCERT, UNTRUSTED, NETWORK_FAILURE); |
|
283 add_install_test(NOCERT, EXPIRED, NETWORK_FAILURE); |
|
284 |
|
285 // Tests that redirecting from self-signed https to other servers works as expected |
|
286 add_install_test(SELFSIGNED, HTTP, NETWORK_FAILURE); |
|
287 add_install_test(SELFSIGNED, HTTPS, NETWORK_FAILURE); |
|
288 add_install_test(SELFSIGNED, NOCERT, NETWORK_FAILURE); |
|
289 add_install_test(SELFSIGNED, SELFSIGNED, NETWORK_FAILURE); |
|
290 add_install_test(SELFSIGNED, UNTRUSTED, NETWORK_FAILURE); |
|
291 add_install_test(SELFSIGNED, EXPIRED, NETWORK_FAILURE); |
|
292 |
|
293 // Tests that redirecting from untrusted https to other servers works as expected |
|
294 add_install_test(UNTRUSTED, HTTP, NETWORK_FAILURE); |
|
295 add_install_test(UNTRUSTED, HTTPS, NETWORK_FAILURE); |
|
296 add_install_test(UNTRUSTED, NOCERT, NETWORK_FAILURE); |
|
297 add_install_test(UNTRUSTED, SELFSIGNED, NETWORK_FAILURE); |
|
298 add_install_test(UNTRUSTED, UNTRUSTED, NETWORK_FAILURE); |
|
299 add_install_test(UNTRUSTED, EXPIRED, NETWORK_FAILURE); |
|
300 |
|
301 // Tests that redirecting from expired https to other servers works as expected |
|
302 add_install_test(EXPIRED, HTTP, NETWORK_FAILURE); |
|
303 add_install_test(EXPIRED, HTTPS, NETWORK_FAILURE); |
|
304 add_install_test(EXPIRED, NOCERT, NETWORK_FAILURE); |
|
305 add_install_test(EXPIRED, SELFSIGNED, NETWORK_FAILURE); |
|
306 add_install_test(EXPIRED, UNTRUSTED, NETWORK_FAILURE); |
|
307 add_install_test(EXPIRED, EXPIRED, NETWORK_FAILURE); |
|
308 |
|
309 run_install_tests(run_next_test); |
|
310 }); |
|
311 |
|
312 // Runs tests without requiring built-in certificates, all certificate |
|
313 // exceptions and no hashes |
|
314 add_test(function() { |
|
315 Services.prefs.setBoolPref(PREF_INSTALL_REQUIREBUILTINCERTS, false); |
|
316 |
|
317 // Tests that a simple install works as expected. |
|
318 add_install_test(HTTP, null, SUCCESS); |
|
319 add_install_test(HTTPS, null, SUCCESS); |
|
320 add_install_test(NOCERT, null, SUCCESS); |
|
321 add_install_test(SELFSIGNED, null, SUCCESS); |
|
322 add_install_test(UNTRUSTED, null, SUCCESS); |
|
323 add_install_test(EXPIRED, null, SUCCESS); |
|
324 |
|
325 // Tests that redirecting from http to other servers works as expected |
|
326 add_install_test(HTTP, HTTP, SUCCESS); |
|
327 add_install_test(HTTP, HTTPS, SUCCESS); |
|
328 add_install_test(HTTP, NOCERT, SUCCESS); |
|
329 add_install_test(HTTP, SELFSIGNED, SUCCESS); |
|
330 add_install_test(HTTP, UNTRUSTED, SUCCESS); |
|
331 add_install_test(HTTP, EXPIRED, SUCCESS); |
|
332 |
|
333 // Tests that redirecting from valid https to other servers works as expected |
|
334 add_install_test(HTTPS, HTTP, NETWORK_FAILURE); |
|
335 add_install_test(HTTPS, HTTPS, SUCCESS); |
|
336 add_install_test(HTTPS, NOCERT, SUCCESS); |
|
337 add_install_test(HTTPS, SELFSIGNED, SUCCESS); |
|
338 add_install_test(HTTPS, UNTRUSTED, SUCCESS); |
|
339 add_install_test(HTTPS, EXPIRED, SUCCESS); |
|
340 |
|
341 // Tests that redirecting from nocert https to other servers works as expected |
|
342 add_install_test(NOCERT, HTTP, NETWORK_FAILURE); |
|
343 add_install_test(NOCERT, HTTPS, SUCCESS); |
|
344 add_install_test(NOCERT, NOCERT, SUCCESS); |
|
345 add_install_test(NOCERT, SELFSIGNED, SUCCESS); |
|
346 add_install_test(NOCERT, UNTRUSTED, SUCCESS); |
|
347 add_install_test(NOCERT, EXPIRED, SUCCESS); |
|
348 |
|
349 // Tests that redirecting from self-signed https to other servers works as expected |
|
350 add_install_test(SELFSIGNED, HTTP, NETWORK_FAILURE); |
|
351 add_install_test(SELFSIGNED, HTTPS, SUCCESS); |
|
352 add_install_test(SELFSIGNED, NOCERT, SUCCESS); |
|
353 add_install_test(SELFSIGNED, SELFSIGNED, SUCCESS); |
|
354 add_install_test(SELFSIGNED, UNTRUSTED, SUCCESS); |
|
355 add_install_test(SELFSIGNED, EXPIRED, SUCCESS); |
|
356 |
|
357 // Tests that redirecting from untrusted https to other servers works as expected |
|
358 add_install_test(UNTRUSTED, HTTP, NETWORK_FAILURE); |
|
359 add_install_test(UNTRUSTED, HTTPS, SUCCESS); |
|
360 add_install_test(UNTRUSTED, NOCERT, SUCCESS); |
|
361 add_install_test(UNTRUSTED, SELFSIGNED, SUCCESS); |
|
362 add_install_test(UNTRUSTED, UNTRUSTED, SUCCESS); |
|
363 add_install_test(UNTRUSTED, EXPIRED, SUCCESS); |
|
364 |
|
365 // Tests that redirecting from expired https to other servers works as expected |
|
366 add_install_test(EXPIRED, HTTP, NETWORK_FAILURE); |
|
367 add_install_test(EXPIRED, HTTPS, SUCCESS); |
|
368 add_install_test(EXPIRED, NOCERT, SUCCESS); |
|
369 add_install_test(EXPIRED, SELFSIGNED, SUCCESS); |
|
370 add_install_test(EXPIRED, UNTRUSTED, SUCCESS); |
|
371 add_install_test(EXPIRED, EXPIRED, SUCCESS); |
|
372 |
|
373 run_install_tests(run_next_test); |
|
374 }); |