1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/test/browser/browser_updatessl.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,370 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.6 + */ 1.7 + 1.8 +let tempScope = {}; 1.9 +Components.utils.import("resource://gre/modules/addons/AddonUpdateChecker.jsm", tempScope); 1.10 +let AddonUpdateChecker = tempScope.AddonUpdateChecker; 1.11 + 1.12 +const updaterdf = RELATIVE_DIR + "browser_updatessl.rdf"; 1.13 +const redirect = RELATIVE_DIR + "redirect.sjs?"; 1.14 +const SUCCESS = 0; 1.15 +const DOWNLOAD_ERROR = AddonUpdateChecker.ERROR_DOWNLOAD_ERROR; 1.16 + 1.17 +const HTTP = "http://example.com/"; 1.18 +const HTTPS = "https://example.com/"; 1.19 +const NOCERT = "https://nocert.example.com/"; 1.20 +const SELFSIGNED = "https://self-signed.example.com/"; 1.21 +const UNTRUSTED = "https://untrusted.example.com/"; 1.22 +const EXPIRED = "https://expired.example.com/"; 1.23 + 1.24 +const PREF_UPDATE_REQUIREBUILTINCERTS = "extensions.update.requireBuiltInCerts"; 1.25 + 1.26 +var gTests = []; 1.27 +var gStart = 0; 1.28 +var gLast = 0; 1.29 + 1.30 +var HTTPObserver = { 1.31 + observeActivity: function(aChannel, aType, aSubtype, aTimestamp, aSizeData, 1.32 + aStringData) { 1.33 + aChannel.QueryInterface(Ci.nsIChannel); 1.34 + 1.35 + dump("*** HTTP Activity 0x" + aType.toString(16) + " 0x" + aSubtype.toString(16) + 1.36 + " " + aChannel.URI.spec + "\n"); 1.37 + } 1.38 +}; 1.39 + 1.40 +function test() { 1.41 + gStart = Date.now(); 1.42 + requestLongerTimeout(4); 1.43 + waitForExplicitFinish(); 1.44 + 1.45 + let observerService = Cc["@mozilla.org/network/http-activity-distributor;1"]. 1.46 + getService(Ci.nsIHttpActivityDistributor); 1.47 + observerService.addObserver(HTTPObserver); 1.48 + 1.49 + registerCleanupFunction(function() { 1.50 + observerService.removeObserver(HTTPObserver); 1.51 + }); 1.52 + 1.53 + run_next_test(); 1.54 +} 1.55 + 1.56 +function end_test() { 1.57 + Services.prefs.clearUserPref(PREF_UPDATE_REQUIREBUILTINCERTS); 1.58 + 1.59 + var cos = Cc["@mozilla.org/security/certoverride;1"]. 1.60 + getService(Ci.nsICertOverrideService); 1.61 + cos.clearValidityOverride("nocert.example.com", -1); 1.62 + cos.clearValidityOverride("self-signed.example.com", -1); 1.63 + cos.clearValidityOverride("untrusted.example.com", -1); 1.64 + cos.clearValidityOverride("expired.example.com", -1); 1.65 + 1.66 + info("All tests completed in " + (Date.now() - gStart) + "ms"); 1.67 + finish(); 1.68 +} 1.69 + 1.70 +function add_update_test(mainURL, redirectURL, expectedStatus) { 1.71 + gTests.push([mainURL, redirectURL, expectedStatus]); 1.72 +} 1.73 + 1.74 +function run_update_tests(callback) { 1.75 + function run_next_update_test() { 1.76 + if (gTests.length == 0) { 1.77 + callback(); 1.78 + return; 1.79 + } 1.80 + gLast = Date.now(); 1.81 + 1.82 + let [mainURL, redirectURL, expectedStatus] = gTests.shift(); 1.83 + if (redirectURL) { 1.84 + var url = mainURL + redirect + redirectURL + updaterdf; 1.85 + var message = "Should have seen the right result for an update check redirected from " + 1.86 + mainURL + " to " + redirectURL; 1.87 + } 1.88 + else { 1.89 + url = mainURL + updaterdf; 1.90 + message = "Should have seen the right result for an update check from " + 1.91 + mainURL; 1.92 + } 1.93 + 1.94 + AddonUpdateChecker.checkForUpdates("addon1@tests.mozilla.org", 1.95 + null, url, { 1.96 + onUpdateCheckComplete: function(updates) { 1.97 + is(updates.length, 1, "Should be the right number of results"); 1.98 + is(SUCCESS, expectedStatus, message); 1.99 + info("Update test ran in " + (Date.now() - gLast) + "ms"); 1.100 + run_next_update_test(); 1.101 + }, 1.102 + 1.103 + onUpdateCheckError: function(status) { 1.104 + is(status, expectedStatus, message); 1.105 + info("Update test ran in " + (Date.now() - gLast) + "ms"); 1.106 + run_next_update_test(); 1.107 + } 1.108 + }); 1.109 + } 1.110 + 1.111 + run_next_update_test(); 1.112 +} 1.113 + 1.114 +// Add overrides for the bad certificates 1.115 +function addCertOverrides() { 1.116 + addCertOverride("nocert.example.com", Ci.nsICertOverrideService.ERROR_MISMATCH); 1.117 + addCertOverride("self-signed.example.com", Ci.nsICertOverrideService.ERROR_UNTRUSTED); 1.118 + addCertOverride("untrusted.example.com", Ci.nsICertOverrideService.ERROR_UNTRUSTED); 1.119 + addCertOverride("expired.example.com", Ci.nsICertOverrideService.ERROR_TIME); 1.120 +} 1.121 + 1.122 +// Runs tests with built-in certificates required and no certificate exceptions. 1.123 +add_test(function() { 1.124 + // Tests that a simple update.rdf retrieval works as expected. 1.125 + add_update_test(HTTP, null, SUCCESS); 1.126 + add_update_test(HTTPS, null, DOWNLOAD_ERROR); 1.127 + add_update_test(NOCERT, null, DOWNLOAD_ERROR); 1.128 + add_update_test(SELFSIGNED, null, DOWNLOAD_ERROR); 1.129 + add_update_test(UNTRUSTED, null, DOWNLOAD_ERROR); 1.130 + add_update_test(EXPIRED, null, DOWNLOAD_ERROR); 1.131 + 1.132 + // Tests that redirecting from http to other servers works as expected 1.133 + add_update_test(HTTP, HTTP, SUCCESS); 1.134 + add_update_test(HTTP, HTTPS, SUCCESS); 1.135 + add_update_test(HTTP, NOCERT, DOWNLOAD_ERROR); 1.136 + add_update_test(HTTP, SELFSIGNED, DOWNLOAD_ERROR); 1.137 + add_update_test(HTTP, UNTRUSTED, DOWNLOAD_ERROR); 1.138 + add_update_test(HTTP, EXPIRED, DOWNLOAD_ERROR); 1.139 + 1.140 + // Tests that redirecting from valid https to other servers works as expected 1.141 + add_update_test(HTTPS, HTTP, DOWNLOAD_ERROR); 1.142 + add_update_test(HTTPS, HTTPS, DOWNLOAD_ERROR); 1.143 + add_update_test(HTTPS, NOCERT, DOWNLOAD_ERROR); 1.144 + add_update_test(HTTPS, SELFSIGNED, DOWNLOAD_ERROR); 1.145 + add_update_test(HTTPS, UNTRUSTED, DOWNLOAD_ERROR); 1.146 + add_update_test(HTTPS, EXPIRED, DOWNLOAD_ERROR); 1.147 + 1.148 + // Tests that redirecting from nocert https to other servers works as expected 1.149 + add_update_test(NOCERT, HTTP, DOWNLOAD_ERROR); 1.150 + add_update_test(NOCERT, HTTPS, DOWNLOAD_ERROR); 1.151 + add_update_test(NOCERT, NOCERT, DOWNLOAD_ERROR); 1.152 + add_update_test(NOCERT, SELFSIGNED, DOWNLOAD_ERROR); 1.153 + add_update_test(NOCERT, UNTRUSTED, DOWNLOAD_ERROR); 1.154 + add_update_test(NOCERT, EXPIRED, DOWNLOAD_ERROR); 1.155 + 1.156 + // Tests that redirecting from self-signed https to other servers works as expected 1.157 + add_update_test(SELFSIGNED, HTTP, DOWNLOAD_ERROR); 1.158 + add_update_test(SELFSIGNED, HTTPS, DOWNLOAD_ERROR); 1.159 + add_update_test(SELFSIGNED, NOCERT, DOWNLOAD_ERROR); 1.160 + add_update_test(SELFSIGNED, SELFSIGNED, DOWNLOAD_ERROR); 1.161 + add_update_test(SELFSIGNED, UNTRUSTED, DOWNLOAD_ERROR); 1.162 + add_update_test(SELFSIGNED, EXPIRED, DOWNLOAD_ERROR); 1.163 + 1.164 + // Tests that redirecting from untrusted https to other servers works as expected 1.165 + add_update_test(UNTRUSTED, HTTP, DOWNLOAD_ERROR); 1.166 + add_update_test(UNTRUSTED, HTTPS, DOWNLOAD_ERROR); 1.167 + add_update_test(UNTRUSTED, NOCERT, DOWNLOAD_ERROR); 1.168 + add_update_test(UNTRUSTED, SELFSIGNED, DOWNLOAD_ERROR); 1.169 + add_update_test(UNTRUSTED, UNTRUSTED, DOWNLOAD_ERROR); 1.170 + add_update_test(UNTRUSTED, EXPIRED, DOWNLOAD_ERROR); 1.171 + 1.172 + // Tests that redirecting from expired https to other servers works as expected 1.173 + add_update_test(EXPIRED, HTTP, DOWNLOAD_ERROR); 1.174 + add_update_test(EXPIRED, HTTPS, DOWNLOAD_ERROR); 1.175 + add_update_test(EXPIRED, NOCERT, DOWNLOAD_ERROR); 1.176 + add_update_test(EXPIRED, SELFSIGNED, DOWNLOAD_ERROR); 1.177 + add_update_test(EXPIRED, UNTRUSTED, DOWNLOAD_ERROR); 1.178 + add_update_test(EXPIRED, EXPIRED, DOWNLOAD_ERROR); 1.179 + 1.180 + run_update_tests(run_next_test); 1.181 +}); 1.182 + 1.183 +// Runs tests without requiring built-in certificates and no certificate 1.184 +// exceptions. 1.185 +add_test(function() { 1.186 + Services.prefs.setBoolPref(PREF_UPDATE_REQUIREBUILTINCERTS, false); 1.187 + 1.188 + // Tests that a simple update.rdf retrieval works as expected. 1.189 + add_update_test(HTTP, null, SUCCESS); 1.190 + add_update_test(HTTPS, null, SUCCESS); 1.191 + add_update_test(NOCERT, null, DOWNLOAD_ERROR); 1.192 + add_update_test(SELFSIGNED, null, DOWNLOAD_ERROR); 1.193 + add_update_test(UNTRUSTED, null, DOWNLOAD_ERROR); 1.194 + add_update_test(EXPIRED, null, DOWNLOAD_ERROR); 1.195 + 1.196 + // Tests that redirecting from http to other servers works as expected 1.197 + add_update_test(HTTP, HTTP, SUCCESS); 1.198 + add_update_test(HTTP, HTTPS, SUCCESS); 1.199 + add_update_test(HTTP, NOCERT, DOWNLOAD_ERROR); 1.200 + add_update_test(HTTP, SELFSIGNED, DOWNLOAD_ERROR); 1.201 + add_update_test(HTTP, UNTRUSTED, DOWNLOAD_ERROR); 1.202 + add_update_test(HTTP, EXPIRED, DOWNLOAD_ERROR); 1.203 + 1.204 + // Tests that redirecting from valid https to other servers works as expected 1.205 + add_update_test(HTTPS, HTTP, DOWNLOAD_ERROR); 1.206 + add_update_test(HTTPS, HTTPS, SUCCESS); 1.207 + add_update_test(HTTPS, NOCERT, DOWNLOAD_ERROR); 1.208 + add_update_test(HTTPS, SELFSIGNED, DOWNLOAD_ERROR); 1.209 + add_update_test(HTTPS, UNTRUSTED, DOWNLOAD_ERROR); 1.210 + add_update_test(HTTPS, EXPIRED, DOWNLOAD_ERROR); 1.211 + 1.212 + // Tests that redirecting from nocert https to other servers works as expected 1.213 + add_update_test(NOCERT, HTTP, DOWNLOAD_ERROR); 1.214 + add_update_test(NOCERT, HTTPS, DOWNLOAD_ERROR); 1.215 + add_update_test(NOCERT, NOCERT, DOWNLOAD_ERROR); 1.216 + add_update_test(NOCERT, SELFSIGNED, DOWNLOAD_ERROR); 1.217 + add_update_test(NOCERT, UNTRUSTED, DOWNLOAD_ERROR); 1.218 + add_update_test(NOCERT, EXPIRED, DOWNLOAD_ERROR); 1.219 + 1.220 + // Tests that redirecting from self-signed https to other servers works as expected 1.221 + add_update_test(SELFSIGNED, HTTP, DOWNLOAD_ERROR); 1.222 + add_update_test(SELFSIGNED, HTTPS, DOWNLOAD_ERROR); 1.223 + add_update_test(SELFSIGNED, NOCERT, DOWNLOAD_ERROR); 1.224 + add_update_test(SELFSIGNED, SELFSIGNED, DOWNLOAD_ERROR); 1.225 + add_update_test(SELFSIGNED, UNTRUSTED, DOWNLOAD_ERROR); 1.226 + add_update_test(SELFSIGNED, EXPIRED, DOWNLOAD_ERROR); 1.227 + 1.228 + // Tests that redirecting from untrusted https to other servers works as expected 1.229 + add_update_test(UNTRUSTED, HTTP, DOWNLOAD_ERROR); 1.230 + add_update_test(UNTRUSTED, HTTPS, DOWNLOAD_ERROR); 1.231 + add_update_test(UNTRUSTED, NOCERT, DOWNLOAD_ERROR); 1.232 + add_update_test(UNTRUSTED, SELFSIGNED, DOWNLOAD_ERROR); 1.233 + add_update_test(UNTRUSTED, UNTRUSTED, DOWNLOAD_ERROR); 1.234 + add_update_test(UNTRUSTED, EXPIRED, DOWNLOAD_ERROR); 1.235 + 1.236 + // Tests that redirecting from expired https to other servers works as expected 1.237 + add_update_test(EXPIRED, HTTP, DOWNLOAD_ERROR); 1.238 + add_update_test(EXPIRED, HTTPS, DOWNLOAD_ERROR); 1.239 + add_update_test(EXPIRED, NOCERT, DOWNLOAD_ERROR); 1.240 + add_update_test(EXPIRED, SELFSIGNED, DOWNLOAD_ERROR); 1.241 + add_update_test(EXPIRED, UNTRUSTED, DOWNLOAD_ERROR); 1.242 + add_update_test(EXPIRED, EXPIRED, DOWNLOAD_ERROR); 1.243 + 1.244 + run_update_tests(run_next_test); 1.245 +}); 1.246 + 1.247 +// Runs tests with built-in certificates required and all certificate exceptions. 1.248 +add_test(function() { 1.249 + Services.prefs.clearUserPref(PREF_UPDATE_REQUIREBUILTINCERTS); 1.250 + addCertOverrides(); 1.251 + 1.252 + // Tests that a simple update.rdf retrieval works as expected. 1.253 + add_update_test(HTTP, null, SUCCESS); 1.254 + add_update_test(HTTPS, null, DOWNLOAD_ERROR); 1.255 + add_update_test(NOCERT, null, DOWNLOAD_ERROR); 1.256 + add_update_test(SELFSIGNED, null, DOWNLOAD_ERROR); 1.257 + add_update_test(UNTRUSTED, null, DOWNLOAD_ERROR); 1.258 + add_update_test(EXPIRED, null, DOWNLOAD_ERROR); 1.259 + 1.260 + // Tests that redirecting from http to other servers works as expected 1.261 + add_update_test(HTTP, HTTP, SUCCESS); 1.262 + add_update_test(HTTP, HTTPS, SUCCESS); 1.263 + add_update_test(HTTP, NOCERT, SUCCESS); 1.264 + add_update_test(HTTP, SELFSIGNED, SUCCESS); 1.265 + add_update_test(HTTP, UNTRUSTED, SUCCESS); 1.266 + add_update_test(HTTP, EXPIRED, SUCCESS); 1.267 + 1.268 + // Tests that redirecting from valid https to other servers works as expected 1.269 + add_update_test(HTTPS, HTTP, DOWNLOAD_ERROR); 1.270 + add_update_test(HTTPS, HTTPS, DOWNLOAD_ERROR); 1.271 + add_update_test(HTTPS, NOCERT, DOWNLOAD_ERROR); 1.272 + add_update_test(HTTPS, SELFSIGNED, DOWNLOAD_ERROR); 1.273 + add_update_test(HTTPS, UNTRUSTED, DOWNLOAD_ERROR); 1.274 + add_update_test(HTTPS, EXPIRED, DOWNLOAD_ERROR); 1.275 + 1.276 + // Tests that redirecting from nocert https to other servers works as expected 1.277 + add_update_test(NOCERT, HTTP, DOWNLOAD_ERROR); 1.278 + add_update_test(NOCERT, HTTPS, DOWNLOAD_ERROR); 1.279 + add_update_test(NOCERT, NOCERT, DOWNLOAD_ERROR); 1.280 + add_update_test(NOCERT, SELFSIGNED, DOWNLOAD_ERROR); 1.281 + add_update_test(NOCERT, UNTRUSTED, DOWNLOAD_ERROR); 1.282 + add_update_test(NOCERT, EXPIRED, DOWNLOAD_ERROR); 1.283 + 1.284 + // Tests that redirecting from self-signed https to other servers works as expected 1.285 + add_update_test(SELFSIGNED, HTTP, DOWNLOAD_ERROR); 1.286 + add_update_test(SELFSIGNED, HTTPS, DOWNLOAD_ERROR); 1.287 + add_update_test(SELFSIGNED, NOCERT, DOWNLOAD_ERROR); 1.288 + add_update_test(SELFSIGNED, SELFSIGNED, DOWNLOAD_ERROR); 1.289 + add_update_test(SELFSIGNED, UNTRUSTED, DOWNLOAD_ERROR); 1.290 + add_update_test(SELFSIGNED, EXPIRED, DOWNLOAD_ERROR); 1.291 + 1.292 + // Tests that redirecting from untrusted https to other servers works as expected 1.293 + add_update_test(UNTRUSTED, HTTP, DOWNLOAD_ERROR); 1.294 + add_update_test(UNTRUSTED, HTTPS, DOWNLOAD_ERROR); 1.295 + add_update_test(UNTRUSTED, NOCERT, DOWNLOAD_ERROR); 1.296 + add_update_test(UNTRUSTED, SELFSIGNED, DOWNLOAD_ERROR); 1.297 + add_update_test(UNTRUSTED, UNTRUSTED, DOWNLOAD_ERROR); 1.298 + add_update_test(UNTRUSTED, EXPIRED, DOWNLOAD_ERROR); 1.299 + 1.300 + // Tests that redirecting from expired https to other servers works as expected 1.301 + add_update_test(EXPIRED, HTTP, DOWNLOAD_ERROR); 1.302 + add_update_test(EXPIRED, HTTPS, DOWNLOAD_ERROR); 1.303 + add_update_test(EXPIRED, NOCERT, DOWNLOAD_ERROR); 1.304 + add_update_test(EXPIRED, SELFSIGNED, DOWNLOAD_ERROR); 1.305 + add_update_test(EXPIRED, UNTRUSTED, DOWNLOAD_ERROR); 1.306 + add_update_test(EXPIRED, EXPIRED, DOWNLOAD_ERROR); 1.307 + 1.308 + run_update_tests(run_next_test); 1.309 +}); 1.310 + 1.311 +// Runs tests without requiring built-in certificates and all certificate 1.312 +// exceptions. 1.313 +add_test(function() { 1.314 + Services.prefs.setBoolPref(PREF_UPDATE_REQUIREBUILTINCERTS, false); 1.315 + 1.316 + // Tests that a simple update.rdf retrieval works as expected. 1.317 + add_update_test(HTTP, null, SUCCESS); 1.318 + add_update_test(HTTPS, null, SUCCESS); 1.319 + add_update_test(NOCERT, null, SUCCESS); 1.320 + add_update_test(SELFSIGNED, null, SUCCESS); 1.321 + add_update_test(UNTRUSTED, null, SUCCESS); 1.322 + add_update_test(EXPIRED, null, SUCCESS); 1.323 + 1.324 + // Tests that redirecting from http to other servers works as expected 1.325 + add_update_test(HTTP, HTTP, SUCCESS); 1.326 + add_update_test(HTTP, HTTPS, SUCCESS); 1.327 + add_update_test(HTTP, NOCERT, SUCCESS); 1.328 + add_update_test(HTTP, SELFSIGNED, SUCCESS); 1.329 + add_update_test(HTTP, UNTRUSTED, SUCCESS); 1.330 + add_update_test(HTTP, EXPIRED, SUCCESS); 1.331 + 1.332 + // Tests that redirecting from valid https to other servers works as expected 1.333 + add_update_test(HTTPS, HTTP, DOWNLOAD_ERROR); 1.334 + add_update_test(HTTPS, HTTPS, SUCCESS); 1.335 + add_update_test(HTTPS, NOCERT, SUCCESS); 1.336 + add_update_test(HTTPS, SELFSIGNED, SUCCESS); 1.337 + add_update_test(HTTPS, UNTRUSTED, SUCCESS); 1.338 + add_update_test(HTTPS, EXPIRED, SUCCESS); 1.339 + 1.340 + // Tests that redirecting from nocert https to other servers works as expected 1.341 + add_update_test(NOCERT, HTTP, DOWNLOAD_ERROR); 1.342 + add_update_test(NOCERT, HTTPS, SUCCESS); 1.343 + add_update_test(NOCERT, NOCERT, SUCCESS); 1.344 + add_update_test(NOCERT, SELFSIGNED, SUCCESS); 1.345 + add_update_test(NOCERT, UNTRUSTED, SUCCESS); 1.346 + add_update_test(NOCERT, EXPIRED, SUCCESS); 1.347 + 1.348 + // Tests that redirecting from self-signed https to other servers works as expected 1.349 + add_update_test(SELFSIGNED, HTTP, DOWNLOAD_ERROR); 1.350 + add_update_test(SELFSIGNED, HTTPS, SUCCESS); 1.351 + add_update_test(SELFSIGNED, NOCERT, SUCCESS); 1.352 + add_update_test(SELFSIGNED, SELFSIGNED, SUCCESS); 1.353 + add_update_test(SELFSIGNED, UNTRUSTED, SUCCESS); 1.354 + add_update_test(SELFSIGNED, EXPIRED, SUCCESS); 1.355 + 1.356 + // Tests that redirecting from untrusted https to other servers works as expected 1.357 + add_update_test(UNTRUSTED, HTTP, DOWNLOAD_ERROR); 1.358 + add_update_test(UNTRUSTED, HTTPS, SUCCESS); 1.359 + add_update_test(UNTRUSTED, NOCERT, SUCCESS); 1.360 + add_update_test(UNTRUSTED, SELFSIGNED, SUCCESS); 1.361 + add_update_test(UNTRUSTED, UNTRUSTED, SUCCESS); 1.362 + add_update_test(UNTRUSTED, EXPIRED, SUCCESS); 1.363 + 1.364 + // Tests that redirecting from expired https to other servers works as expected 1.365 + add_update_test(EXPIRED, HTTP, DOWNLOAD_ERROR); 1.366 + add_update_test(EXPIRED, HTTPS, SUCCESS); 1.367 + add_update_test(EXPIRED, NOCERT, SUCCESS); 1.368 + add_update_test(EXPIRED, SELFSIGNED, SUCCESS); 1.369 + add_update_test(EXPIRED, UNTRUSTED, SUCCESS); 1.370 + add_update_test(EXPIRED, EXPIRED, SUCCESS); 1.371 + 1.372 + run_update_tests(run_next_test); 1.373 +});