michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: let tempScope = {}; michael@0: Components.utils.import("resource://gre/modules/addons/AddonUpdateChecker.jsm", tempScope); michael@0: let AddonUpdateChecker = tempScope.AddonUpdateChecker; michael@0: michael@0: const updaterdf = RELATIVE_DIR + "browser_updatessl.rdf"; michael@0: const redirect = RELATIVE_DIR + "redirect.sjs?"; michael@0: const SUCCESS = 0; michael@0: const DOWNLOAD_ERROR = AddonUpdateChecker.ERROR_DOWNLOAD_ERROR; michael@0: michael@0: const HTTP = "http://example.com/"; michael@0: const HTTPS = "https://example.com/"; michael@0: const NOCERT = "https://nocert.example.com/"; michael@0: const SELFSIGNED = "https://self-signed.example.com/"; michael@0: const UNTRUSTED = "https://untrusted.example.com/"; michael@0: const EXPIRED = "https://expired.example.com/"; michael@0: michael@0: const PREF_UPDATE_REQUIREBUILTINCERTS = "extensions.update.requireBuiltInCerts"; michael@0: michael@0: var gTests = []; michael@0: var gStart = 0; michael@0: var gLast = 0; michael@0: michael@0: var HTTPObserver = { michael@0: observeActivity: function(aChannel, aType, aSubtype, aTimestamp, aSizeData, michael@0: aStringData) { michael@0: aChannel.QueryInterface(Ci.nsIChannel); michael@0: michael@0: dump("*** HTTP Activity 0x" + aType.toString(16) + " 0x" + aSubtype.toString(16) + michael@0: " " + aChannel.URI.spec + "\n"); michael@0: } michael@0: }; michael@0: michael@0: function test() { michael@0: gStart = Date.now(); michael@0: requestLongerTimeout(4); michael@0: waitForExplicitFinish(); michael@0: michael@0: let observerService = Cc["@mozilla.org/network/http-activity-distributor;1"]. michael@0: getService(Ci.nsIHttpActivityDistributor); michael@0: observerService.addObserver(HTTPObserver); michael@0: michael@0: registerCleanupFunction(function() { michael@0: observerService.removeObserver(HTTPObserver); michael@0: }); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function end_test() { michael@0: Services.prefs.clearUserPref(PREF_UPDATE_REQUIREBUILTINCERTS); michael@0: michael@0: var cos = Cc["@mozilla.org/security/certoverride;1"]. michael@0: getService(Ci.nsICertOverrideService); michael@0: cos.clearValidityOverride("nocert.example.com", -1); michael@0: cos.clearValidityOverride("self-signed.example.com", -1); michael@0: cos.clearValidityOverride("untrusted.example.com", -1); michael@0: cos.clearValidityOverride("expired.example.com", -1); michael@0: michael@0: info("All tests completed in " + (Date.now() - gStart) + "ms"); michael@0: finish(); michael@0: } michael@0: michael@0: function add_update_test(mainURL, redirectURL, expectedStatus) { michael@0: gTests.push([mainURL, redirectURL, expectedStatus]); michael@0: } michael@0: michael@0: function run_update_tests(callback) { michael@0: function run_next_update_test() { michael@0: if (gTests.length == 0) { michael@0: callback(); michael@0: return; michael@0: } michael@0: gLast = Date.now(); michael@0: michael@0: let [mainURL, redirectURL, expectedStatus] = gTests.shift(); michael@0: if (redirectURL) { michael@0: var url = mainURL + redirect + redirectURL + updaterdf; michael@0: var message = "Should have seen the right result for an update check redirected from " + michael@0: mainURL + " to " + redirectURL; michael@0: } michael@0: else { michael@0: url = mainURL + updaterdf; michael@0: message = "Should have seen the right result for an update check from " + michael@0: mainURL; michael@0: } michael@0: michael@0: AddonUpdateChecker.checkForUpdates("addon1@tests.mozilla.org", michael@0: null, url, { michael@0: onUpdateCheckComplete: function(updates) { michael@0: is(updates.length, 1, "Should be the right number of results"); michael@0: is(SUCCESS, expectedStatus, message); michael@0: info("Update test ran in " + (Date.now() - gLast) + "ms"); michael@0: run_next_update_test(); michael@0: }, michael@0: michael@0: onUpdateCheckError: function(status) { michael@0: is(status, expectedStatus, message); michael@0: info("Update test ran in " + (Date.now() - gLast) + "ms"); michael@0: run_next_update_test(); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: run_next_update_test(); michael@0: } michael@0: michael@0: // Add overrides for the bad certificates michael@0: function addCertOverrides() { michael@0: addCertOverride("nocert.example.com", Ci.nsICertOverrideService.ERROR_MISMATCH); michael@0: addCertOverride("self-signed.example.com", Ci.nsICertOverrideService.ERROR_UNTRUSTED); michael@0: addCertOverride("untrusted.example.com", Ci.nsICertOverrideService.ERROR_UNTRUSTED); michael@0: addCertOverride("expired.example.com", Ci.nsICertOverrideService.ERROR_TIME); michael@0: } michael@0: michael@0: // Runs tests with built-in certificates required and no certificate exceptions. michael@0: add_test(function() { michael@0: // Tests that a simple update.rdf retrieval works as expected. michael@0: add_update_test(HTTP, null, SUCCESS); michael@0: add_update_test(HTTPS, null, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, null, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, null, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, null, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, null, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from http to other servers works as expected michael@0: add_update_test(HTTP, HTTP, SUCCESS); michael@0: add_update_test(HTTP, HTTPS, SUCCESS); michael@0: add_update_test(HTTP, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(HTTP, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(HTTP, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(HTTP, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from valid https to other servers works as expected michael@0: add_update_test(HTTPS, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from nocert https to other servers works as expected michael@0: add_update_test(NOCERT, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from self-signed https to other servers works as expected michael@0: add_update_test(SELFSIGNED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from untrusted https to other servers works as expected michael@0: add_update_test(UNTRUSTED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from expired https to other servers works as expected michael@0: add_update_test(EXPIRED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: run_update_tests(run_next_test); michael@0: }); michael@0: michael@0: // Runs tests without requiring built-in certificates and no certificate michael@0: // exceptions. michael@0: add_test(function() { michael@0: Services.prefs.setBoolPref(PREF_UPDATE_REQUIREBUILTINCERTS, false); michael@0: michael@0: // Tests that a simple update.rdf retrieval works as expected. michael@0: add_update_test(HTTP, null, SUCCESS); michael@0: add_update_test(HTTPS, null, SUCCESS); michael@0: add_update_test(NOCERT, null, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, null, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, null, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, null, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from http to other servers works as expected michael@0: add_update_test(HTTP, HTTP, SUCCESS); michael@0: add_update_test(HTTP, HTTPS, SUCCESS); michael@0: add_update_test(HTTP, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(HTTP, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(HTTP, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(HTTP, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from valid https to other servers works as expected michael@0: add_update_test(HTTPS, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, HTTPS, SUCCESS); michael@0: add_update_test(HTTPS, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from nocert https to other servers works as expected michael@0: add_update_test(NOCERT, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from self-signed https to other servers works as expected michael@0: add_update_test(SELFSIGNED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from untrusted https to other servers works as expected michael@0: add_update_test(UNTRUSTED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from expired https to other servers works as expected michael@0: add_update_test(EXPIRED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: run_update_tests(run_next_test); michael@0: }); michael@0: michael@0: // Runs tests with built-in certificates required and all certificate exceptions. michael@0: add_test(function() { michael@0: Services.prefs.clearUserPref(PREF_UPDATE_REQUIREBUILTINCERTS); michael@0: addCertOverrides(); michael@0: michael@0: // Tests that a simple update.rdf retrieval works as expected. michael@0: add_update_test(HTTP, null, SUCCESS); michael@0: add_update_test(HTTPS, null, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, null, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, null, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, null, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, null, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from http to other servers works as expected michael@0: add_update_test(HTTP, HTTP, SUCCESS); michael@0: add_update_test(HTTP, HTTPS, SUCCESS); michael@0: add_update_test(HTTP, NOCERT, SUCCESS); michael@0: add_update_test(HTTP, SELFSIGNED, SUCCESS); michael@0: add_update_test(HTTP, UNTRUSTED, SUCCESS); michael@0: add_update_test(HTTP, EXPIRED, SUCCESS); michael@0: michael@0: // Tests that redirecting from valid https to other servers works as expected michael@0: add_update_test(HTTPS, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from nocert https to other servers works as expected michael@0: add_update_test(NOCERT, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from self-signed https to other servers works as expected michael@0: add_update_test(SELFSIGNED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from untrusted https to other servers works as expected michael@0: add_update_test(UNTRUSTED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: // Tests that redirecting from expired https to other servers works as expected michael@0: add_update_test(EXPIRED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, HTTPS, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, NOCERT, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, SELFSIGNED, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, UNTRUSTED, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, EXPIRED, DOWNLOAD_ERROR); michael@0: michael@0: run_update_tests(run_next_test); michael@0: }); michael@0: michael@0: // Runs tests without requiring built-in certificates and all certificate michael@0: // exceptions. michael@0: add_test(function() { michael@0: Services.prefs.setBoolPref(PREF_UPDATE_REQUIREBUILTINCERTS, false); michael@0: michael@0: // Tests that a simple update.rdf retrieval works as expected. michael@0: add_update_test(HTTP, null, SUCCESS); michael@0: add_update_test(HTTPS, null, SUCCESS); michael@0: add_update_test(NOCERT, null, SUCCESS); michael@0: add_update_test(SELFSIGNED, null, SUCCESS); michael@0: add_update_test(UNTRUSTED, null, SUCCESS); michael@0: add_update_test(EXPIRED, null, SUCCESS); michael@0: michael@0: // Tests that redirecting from http to other servers works as expected michael@0: add_update_test(HTTP, HTTP, SUCCESS); michael@0: add_update_test(HTTP, HTTPS, SUCCESS); michael@0: add_update_test(HTTP, NOCERT, SUCCESS); michael@0: add_update_test(HTTP, SELFSIGNED, SUCCESS); michael@0: add_update_test(HTTP, UNTRUSTED, SUCCESS); michael@0: add_update_test(HTTP, EXPIRED, SUCCESS); michael@0: michael@0: // Tests that redirecting from valid https to other servers works as expected michael@0: add_update_test(HTTPS, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(HTTPS, HTTPS, SUCCESS); michael@0: add_update_test(HTTPS, NOCERT, SUCCESS); michael@0: add_update_test(HTTPS, SELFSIGNED, SUCCESS); michael@0: add_update_test(HTTPS, UNTRUSTED, SUCCESS); michael@0: add_update_test(HTTPS, EXPIRED, SUCCESS); michael@0: michael@0: // Tests that redirecting from nocert https to other servers works as expected michael@0: add_update_test(NOCERT, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(NOCERT, HTTPS, SUCCESS); michael@0: add_update_test(NOCERT, NOCERT, SUCCESS); michael@0: add_update_test(NOCERT, SELFSIGNED, SUCCESS); michael@0: add_update_test(NOCERT, UNTRUSTED, SUCCESS); michael@0: add_update_test(NOCERT, EXPIRED, SUCCESS); michael@0: michael@0: // Tests that redirecting from self-signed https to other servers works as expected michael@0: add_update_test(SELFSIGNED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(SELFSIGNED, HTTPS, SUCCESS); michael@0: add_update_test(SELFSIGNED, NOCERT, SUCCESS); michael@0: add_update_test(SELFSIGNED, SELFSIGNED, SUCCESS); michael@0: add_update_test(SELFSIGNED, UNTRUSTED, SUCCESS); michael@0: add_update_test(SELFSIGNED, EXPIRED, SUCCESS); michael@0: michael@0: // Tests that redirecting from untrusted https to other servers works as expected michael@0: add_update_test(UNTRUSTED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(UNTRUSTED, HTTPS, SUCCESS); michael@0: add_update_test(UNTRUSTED, NOCERT, SUCCESS); michael@0: add_update_test(UNTRUSTED, SELFSIGNED, SUCCESS); michael@0: add_update_test(UNTRUSTED, UNTRUSTED, SUCCESS); michael@0: add_update_test(UNTRUSTED, EXPIRED, SUCCESS); michael@0: michael@0: // Tests that redirecting from expired https to other servers works as expected michael@0: add_update_test(EXPIRED, HTTP, DOWNLOAD_ERROR); michael@0: add_update_test(EXPIRED, HTTPS, SUCCESS); michael@0: add_update_test(EXPIRED, NOCERT, SUCCESS); michael@0: add_update_test(EXPIRED, SELFSIGNED, SUCCESS); michael@0: add_update_test(EXPIRED, UNTRUSTED, SUCCESS); michael@0: add_update_test(EXPIRED, EXPIRED, SUCCESS); michael@0: michael@0: run_update_tests(run_next_test); michael@0: });