tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_addon_utils.js (4004B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { AddonUtils } = ChromeUtils.importESModule(
      7  "resource://services-sync/addonutils.sys.mjs"
      8 );
      9 
     10 const HTTP_PORT = 8888;
     11 const SERVER_ADDRESS = "http://127.0.0.1:8888";
     12 
     13 Services.prefs.setStringPref(
     14  "extensions.getAddons.get.url",
     15  SERVER_ADDRESS + "/search/guid:%IDS%"
     16 );
     17 
     18 AddonTestUtils.init(this);
     19 AddonTestUtils.createAppInfo(
     20  "xpcshell@tests.mozilla.org",
     21  "XPCShell",
     22  "1",
     23  "1.9.2"
     24 );
     25 
     26 add_task(async function setup() {
     27  await AddonTestUtils.promiseStartupManager();
     28 });
     29 
     30 function createAndStartHTTPServer(port = HTTP_PORT) {
     31  try {
     32    let server = new HttpServer();
     33 
     34    server.registerFile(
     35      "/search/guid:missing-sourceuri%40tests.mozilla.org",
     36      do_get_file("missing-sourceuri.json")
     37    );
     38 
     39    server.registerFile(
     40      "/search/guid:rewrite%40tests.mozilla.org",
     41      do_get_file("rewrite-search.json")
     42    );
     43 
     44    server.start(port);
     45 
     46    return server;
     47  } catch (ex) {
     48    _("Got exception starting HTTP server on port " + port);
     49    _("Error: " + Log.exceptionStr(ex));
     50    do_throw(ex);
     51  }
     52  return null; /* not hit, but keeps eslint happy! */
     53 }
     54 
     55 function run_test() {
     56  syncTestLogging();
     57 
     58  run_next_test();
     59 }
     60 
     61 add_task(async function test_handle_empty_source_uri() {
     62  _("Ensure that search results without a sourceURI are properly ignored.");
     63 
     64  let server = createAndStartHTTPServer();
     65 
     66  const ID = "missing-sourceuri@tests.mozilla.org";
     67 
     68  const result = await AddonUtils.installAddons([
     69    { id: ID, requireSecureURI: false },
     70  ]);
     71 
     72  Assert.ok("installedIDs" in result);
     73  Assert.equal(0, result.installedIDs.length);
     74 
     75  Assert.ok("skipped" in result);
     76  Assert.ok(result.skipped.includes(ID));
     77 
     78  await promiseStopServer(server);
     79 });
     80 
     81 add_test(function test_ignore_untrusted_source_uris() {
     82  _("Ensures that source URIs from insecure schemes are rejected.");
     83 
     84  const bad = [
     85    "http://example.com/foo.xpi",
     86    "ftp://example.com/foo.xpi",
     87    "silly://example.com/foo.xpi",
     88  ];
     89 
     90  const good = ["https://example.com/foo.xpi"];
     91 
     92  for (let s of bad) {
     93    let sourceURI = Services.io.newURI(s);
     94    let addon = { sourceURI, name: "bad", id: "bad" };
     95 
     96    let canInstall = AddonUtils.canInstallAddon(addon);
     97    Assert.ok(!canInstall, "Correctly rejected a bad URL");
     98  }
     99 
    100  for (let s of good) {
    101    let sourceURI = Services.io.newURI(s);
    102    let addon = { sourceURI, name: "good", id: "good" };
    103 
    104    let canInstall = AddonUtils.canInstallAddon(addon);
    105    Assert.ok(canInstall, "Correctly accepted a good URL");
    106  }
    107  run_next_test();
    108 });
    109 
    110 add_task(async function test_source_uri_rewrite() {
    111  _("Ensure that a 'src=api' query string is rewritten to 'src=sync'");
    112 
    113  // This tests for conformance with bug 708134 so server-side metrics aren't
    114  // skewed.
    115 
    116  // We resort to monkeypatching because of the API design.
    117  let oldFunction =
    118    Object.getPrototypeOf(AddonUtils).installAddonFromSearchResult;
    119 
    120  let installCalled = false;
    121  Object.getPrototypeOf(AddonUtils).installAddonFromSearchResult =
    122    async function testInstallAddon(addon) {
    123      Assert.equal(
    124        SERVER_ADDRESS + "/require.xpi?src=sync",
    125        addon.sourceURI.spec
    126      );
    127 
    128      installCalled = true;
    129 
    130      const install = await AddonUtils.getInstallFromSearchResult(addon);
    131      Assert.equal(
    132        SERVER_ADDRESS + "/require.xpi?src=sync",
    133        install.sourceURI.spec
    134      );
    135      Assert.deepEqual(
    136        install.installTelemetryInfo,
    137        { source: "sync" },
    138        "Got the expected installTelemetryInfo"
    139      );
    140 
    141      return { id: addon.id, addon, install };
    142    };
    143 
    144  let server = createAndStartHTTPServer();
    145 
    146  let installOptions = {
    147    id: "rewrite@tests.mozilla.org",
    148    requireSecureURI: false,
    149  };
    150  await AddonUtils.installAddons([installOptions]);
    151 
    152  Assert.ok(installCalled);
    153  Object.getPrototypeOf(AddonUtils).installAddonFromSearchResult = oldFunction;
    154 
    155  await promiseStopServer(server);
    156 });