services/sync/tests/unit/test_addon_utils.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial