services/sync/tests/unit/test_addon_utils.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 "use strict";
michael@0 5
michael@0 6 Cu.import("resource://gre/modules/Preferences.jsm");
michael@0 7 Cu.import("resource://services-sync/addonutils.js");
michael@0 8 Cu.import("resource://services-sync/util.js");
michael@0 9
michael@0 10 const HTTP_PORT = 8888;
michael@0 11 const SERVER_ADDRESS = "http://127.0.0.1:8888";
michael@0 12
michael@0 13 let prefs = new Preferences();
michael@0 14
michael@0 15 prefs.set("extensions.getAddons.get.url",
michael@0 16 SERVER_ADDRESS + "/search/guid:%IDS%");
michael@0 17
michael@0 18 loadAddonTestFunctions();
michael@0 19 startupManager();
michael@0 20
michael@0 21 function createAndStartHTTPServer(port=HTTP_PORT) {
michael@0 22 try {
michael@0 23 let server = new HttpServer();
michael@0 24
michael@0 25 let bootstrap1XPI = ExtensionsTestPath("/addons/test_bootstrap1_1.xpi");
michael@0 26
michael@0 27 server.registerFile("/search/guid:missing-sourceuri%40tests.mozilla.org",
michael@0 28 do_get_file("missing-sourceuri.xml"));
michael@0 29
michael@0 30 server.registerFile("/search/guid:rewrite%40tests.mozilla.org",
michael@0 31 do_get_file("rewrite-search.xml"));
michael@0 32
michael@0 33 server.start(port);
michael@0 34
michael@0 35 return server;
michael@0 36 } catch (ex) {
michael@0 37 _("Got exception starting HTTP server on port " + port);
michael@0 38 _("Error: " + Utils.exceptionStr(ex));
michael@0 39 do_throw(ex);
michael@0 40 }
michael@0 41 }
michael@0 42
michael@0 43 function run_test() {
michael@0 44 initTestLogging("Trace");
michael@0 45
michael@0 46 run_next_test();
michael@0 47 }
michael@0 48
michael@0 49 add_test(function test_handle_empty_source_uri() {
michael@0 50 _("Ensure that search results without a sourceURI are properly ignored.");
michael@0 51
michael@0 52 let server = createAndStartHTTPServer();
michael@0 53
michael@0 54 const ID = "missing-sourceuri@tests.mozilla.org";
michael@0 55
michael@0 56 let cb = Async.makeSpinningCallback();
michael@0 57 AddonUtils.installAddons([{id: ID, requireSecureURI: false}], cb);
michael@0 58 let result = cb.wait();
michael@0 59
michael@0 60 do_check_true("installedIDs" in result);
michael@0 61 do_check_eq(0, result.installedIDs.length);
michael@0 62
michael@0 63 server.stop(run_next_test);
michael@0 64 });
michael@0 65
michael@0 66 add_test(function test_ignore_untrusted_source_uris() {
michael@0 67 _("Ensures that source URIs from insecure schemes are rejected.");
michael@0 68
michael@0 69 let ioService = Cc["@mozilla.org/network/io-service;1"]
michael@0 70 .getService(Ci.nsIIOService);
michael@0 71
michael@0 72 const bad = ["http://example.com/foo.xpi",
michael@0 73 "ftp://example.com/foo.xpi",
michael@0 74 "silly://example.com/foo.xpi"];
michael@0 75
michael@0 76 const good = ["https://example.com/foo.xpi"];
michael@0 77
michael@0 78 for (let s of bad) {
michael@0 79 let sourceURI = ioService.newURI(s, null, null);
michael@0 80 let addon = {sourceURI: sourceURI, name: "bad", id: "bad"};
michael@0 81
michael@0 82 try {
michael@0 83 let cb = Async.makeSpinningCallback();
michael@0 84 AddonUtils.getInstallFromSearchResult(addon, cb, true);
michael@0 85 cb.wait();
michael@0 86 } catch (ex) {
michael@0 87 do_check_neq(null, ex);
michael@0 88 do_check_eq(0, ex.message.indexOf("Insecure source URI"));
michael@0 89 continue;
michael@0 90 }
michael@0 91
michael@0 92 // We should never get here if an exception is thrown.
michael@0 93 do_check_true(false);
michael@0 94 }
michael@0 95
michael@0 96 let count = 0;
michael@0 97 for (let s of good) {
michael@0 98 let sourceURI = ioService.newURI(s, null, null);
michael@0 99 let addon = {sourceURI: sourceURI, name: "good", id: "good"};
michael@0 100
michael@0 101 // Despite what you might think, we don't get an error in the callback.
michael@0 102 // The install won't work because the underlying Addon instance wasn't
michael@0 103 // proper. But, that just results in an AddonInstall that is missing
michael@0 104 // certain values. We really just care that the callback is being invoked
michael@0 105 // anyway.
michael@0 106 let callback = function onInstall(error, install) {
michael@0 107 do_check_null(error);
michael@0 108 do_check_neq(null, install);
michael@0 109 do_check_eq(sourceURI.spec, install.sourceURI.spec);
michael@0 110
michael@0 111 count += 1;
michael@0 112
michael@0 113 if (count >= good.length) {
michael@0 114 run_next_test();
michael@0 115 }
michael@0 116 };
michael@0 117
michael@0 118 AddonUtils.getInstallFromSearchResult(addon, callback, true);
michael@0 119 }
michael@0 120 });
michael@0 121
michael@0 122 add_test(function test_source_uri_rewrite() {
michael@0 123 _("Ensure that a 'src=api' query string is rewritten to 'src=sync'");
michael@0 124
michael@0 125 // This tests for conformance with bug 708134 so server-side metrics aren't
michael@0 126 // skewed.
michael@0 127
michael@0 128 Svc.Prefs.set("addons.ignoreRepositoryChecking", true);
michael@0 129
michael@0 130 // We resort to monkeypatching because of the API design.
michael@0 131 let oldFunction = AddonUtils.__proto__.installAddonFromSearchResult;
michael@0 132
michael@0 133 let installCalled = false;
michael@0 134 AddonUtils.__proto__.installAddonFromSearchResult =
michael@0 135 function testInstallAddon(addon, metadata, cb) {
michael@0 136
michael@0 137 do_check_eq(SERVER_ADDRESS + "/require.xpi?src=sync",
michael@0 138 addon.sourceURI.spec);
michael@0 139
michael@0 140 installCalled = true;
michael@0 141
michael@0 142 AddonUtils.getInstallFromSearchResult(addon, function (error, install) {
michael@0 143 do_check_null(error);
michael@0 144 do_check_eq(SERVER_ADDRESS + "/require.xpi?src=sync",
michael@0 145 install.sourceURI.spec);
michael@0 146
michael@0 147 cb(null, {id: addon.id, addon: addon, install: install});
michael@0 148 }, false);
michael@0 149 };
michael@0 150
michael@0 151 let server = createAndStartHTTPServer();
michael@0 152
michael@0 153 let installCallback = Async.makeSpinningCallback();
michael@0 154 AddonUtils.installAddons([{id: "rewrite@tests.mozilla.org"}], installCallback);
michael@0 155
michael@0 156 installCallback.wait();
michael@0 157 do_check_true(installCalled);
michael@0 158 AddonUtils.__proto__.installAddonFromSearchResult = oldFunction;
michael@0 159
michael@0 160 Svc.Prefs.reset("addons.ignoreRepositoryChecking");
michael@0 161 server.stop(run_next_test);
michael@0 162 });

mercurial