michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: Cu.import("resource://gre/modules/Preferences.jsm"); michael@0: Cu.import("resource://services-sync/addonutils.js"); michael@0: Cu.import("resource://services-sync/util.js"); michael@0: michael@0: const HTTP_PORT = 8888; michael@0: const SERVER_ADDRESS = "http://127.0.0.1:8888"; michael@0: michael@0: let prefs = new Preferences(); michael@0: michael@0: prefs.set("extensions.getAddons.get.url", michael@0: SERVER_ADDRESS + "/search/guid:%IDS%"); michael@0: michael@0: loadAddonTestFunctions(); michael@0: startupManager(); michael@0: michael@0: function createAndStartHTTPServer(port=HTTP_PORT) { michael@0: try { michael@0: let server = new HttpServer(); michael@0: michael@0: let bootstrap1XPI = ExtensionsTestPath("/addons/test_bootstrap1_1.xpi"); michael@0: michael@0: server.registerFile("/search/guid:missing-sourceuri%40tests.mozilla.org", michael@0: do_get_file("missing-sourceuri.xml")); michael@0: michael@0: server.registerFile("/search/guid:rewrite%40tests.mozilla.org", michael@0: do_get_file("rewrite-search.xml")); michael@0: michael@0: server.start(port); michael@0: michael@0: return server; michael@0: } catch (ex) { michael@0: _("Got exception starting HTTP server on port " + port); michael@0: _("Error: " + Utils.exceptionStr(ex)); michael@0: do_throw(ex); michael@0: } michael@0: } michael@0: michael@0: function run_test() { michael@0: initTestLogging("Trace"); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_test(function test_handle_empty_source_uri() { michael@0: _("Ensure that search results without a sourceURI are properly ignored."); michael@0: michael@0: let server = createAndStartHTTPServer(); michael@0: michael@0: const ID = "missing-sourceuri@tests.mozilla.org"; michael@0: michael@0: let cb = Async.makeSpinningCallback(); michael@0: AddonUtils.installAddons([{id: ID, requireSecureURI: false}], cb); michael@0: let result = cb.wait(); michael@0: michael@0: do_check_true("installedIDs" in result); michael@0: do_check_eq(0, result.installedIDs.length); michael@0: michael@0: server.stop(run_next_test); michael@0: }); michael@0: michael@0: add_test(function test_ignore_untrusted_source_uris() { michael@0: _("Ensures that source URIs from insecure schemes are rejected."); michael@0: michael@0: let ioService = Cc["@mozilla.org/network/io-service;1"] michael@0: .getService(Ci.nsIIOService); michael@0: michael@0: const bad = ["http://example.com/foo.xpi", michael@0: "ftp://example.com/foo.xpi", michael@0: "silly://example.com/foo.xpi"]; michael@0: michael@0: const good = ["https://example.com/foo.xpi"]; michael@0: michael@0: for (let s of bad) { michael@0: let sourceURI = ioService.newURI(s, null, null); michael@0: let addon = {sourceURI: sourceURI, name: "bad", id: "bad"}; michael@0: michael@0: try { michael@0: let cb = Async.makeSpinningCallback(); michael@0: AddonUtils.getInstallFromSearchResult(addon, cb, true); michael@0: cb.wait(); michael@0: } catch (ex) { michael@0: do_check_neq(null, ex); michael@0: do_check_eq(0, ex.message.indexOf("Insecure source URI")); michael@0: continue; michael@0: } michael@0: michael@0: // We should never get here if an exception is thrown. michael@0: do_check_true(false); michael@0: } michael@0: michael@0: let count = 0; michael@0: for (let s of good) { michael@0: let sourceURI = ioService.newURI(s, null, null); michael@0: let addon = {sourceURI: sourceURI, name: "good", id: "good"}; michael@0: michael@0: // Despite what you might think, we don't get an error in the callback. michael@0: // The install won't work because the underlying Addon instance wasn't michael@0: // proper. But, that just results in an AddonInstall that is missing michael@0: // certain values. We really just care that the callback is being invoked michael@0: // anyway. michael@0: let callback = function onInstall(error, install) { michael@0: do_check_null(error); michael@0: do_check_neq(null, install); michael@0: do_check_eq(sourceURI.spec, install.sourceURI.spec); michael@0: michael@0: count += 1; michael@0: michael@0: if (count >= good.length) { michael@0: run_next_test(); michael@0: } michael@0: }; michael@0: michael@0: AddonUtils.getInstallFromSearchResult(addon, callback, true); michael@0: } michael@0: }); michael@0: michael@0: add_test(function test_source_uri_rewrite() { michael@0: _("Ensure that a 'src=api' query string is rewritten to 'src=sync'"); michael@0: michael@0: // This tests for conformance with bug 708134 so server-side metrics aren't michael@0: // skewed. michael@0: michael@0: Svc.Prefs.set("addons.ignoreRepositoryChecking", true); michael@0: michael@0: // We resort to monkeypatching because of the API design. michael@0: let oldFunction = AddonUtils.__proto__.installAddonFromSearchResult; michael@0: michael@0: let installCalled = false; michael@0: AddonUtils.__proto__.installAddonFromSearchResult = michael@0: function testInstallAddon(addon, metadata, cb) { michael@0: michael@0: do_check_eq(SERVER_ADDRESS + "/require.xpi?src=sync", michael@0: addon.sourceURI.spec); michael@0: michael@0: installCalled = true; michael@0: michael@0: AddonUtils.getInstallFromSearchResult(addon, function (error, install) { michael@0: do_check_null(error); michael@0: do_check_eq(SERVER_ADDRESS + "/require.xpi?src=sync", michael@0: install.sourceURI.spec); michael@0: michael@0: cb(null, {id: addon.id, addon: addon, install: install}); michael@0: }, false); michael@0: }; michael@0: michael@0: let server = createAndStartHTTPServer(); michael@0: michael@0: let installCallback = Async.makeSpinningCallback(); michael@0: AddonUtils.installAddons([{id: "rewrite@tests.mozilla.org"}], installCallback); michael@0: michael@0: installCallback.wait(); michael@0: do_check_true(installCalled); michael@0: AddonUtils.__proto__.installAddonFromSearchResult = oldFunction; michael@0: michael@0: Svc.Prefs.reset("addons.ignoreRepositoryChecking"); michael@0: server.stop(run_next_test); michael@0: });