tor-browser

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

test_no_remote_registration.js (6452B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
      2 * vim: sw=2 ts=2 sts=2 tw=78 expandtab :
      3 * This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 var manifests = [do_get_file("data/test_no_remote_registration.manifest")];
      8 registerManifests(manifests);
      9 
     10 function ProtocolHandler(aScheme) {
     11  this.scheme = aScheme;
     12 }
     13 
     14 ProtocolHandler.prototype = {
     15  allowPort: () => false,
     16  newChannel() {
     17    throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
     18  },
     19  QueryInterface: ChromeUtils.generateQI(["nsIProtocolHandler"]),
     20 };
     21 
     22 var testProtocols = [
     23  // It doesn't matter if it has this flag - the only flag we accept is
     24  // URI_IS_LOCAL_RESOURCE.
     25  {
     26    scheme: "moz-protocol-ui-resource",
     27    flags: Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE,
     28    shouldRegister: false,
     29  },
     30  // It doesn't matter if it has this flag - the only flag we accept is
     31  // URI_IS_LOCAL_RESOURCE.
     32  {
     33    scheme: "moz-protocol-local-file",
     34    flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_FILE,
     35    shouldRegister: false,
     36  },
     37  // This clearly is non-local
     38  {
     39    scheme: "moz-protocol-loadable-by-anyone",
     40    flags: Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
     41    shouldRegister: false,
     42  },
     43  // This should always be last (unless we add more flags that are OK)
     44  {
     45    scheme: "moz-protocol-local-resource",
     46    flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE,
     47    shouldRegister: true,
     48  },
     49 ];
     50 function run_test() {
     51  const { newAppInfo } = ChromeUtils.importESModule(
     52    "resource://testing-common/AppInfo.sys.mjs"
     53  );
     54  let XULAppInfo = newAppInfo({
     55    name: "XPCShell",
     56    ID: "{39885e5f-f6b4-4e2a-87e5-6259ecf79011}",
     57    version: "5",
     58    platformVersion: "1.9",
     59  });
     60 
     61  const uuidGenerator = Services.uuid;
     62 
     63  let XULAppInfoFactory = {
     64    // These two are used when we register all our factories (and unregister)
     65    CID: uuidGenerator.generateUUID(),
     66    scheme: "XULAppInfo",
     67    contractID: "@mozilla.org/xre/app-info;1",
     68    createInstance(iid) {
     69      return XULAppInfo.QueryInterface(iid);
     70    },
     71  };
     72 
     73  for (let protocol of testProtocols) {
     74    Services.io.registerProtocolHandler(
     75      protocol.scheme,
     76      new ProtocolHandler(protocol.scheme),
     77      protocol.flags,
     78      -1
     79    );
     80  }
     81 
     82  let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
     83 
     84  // Register the XULAppInfoFactory
     85  // Make sure the class ID has not already been registered
     86  let old_factory = { CID: "", factory: null };
     87  if (!registrar.isCIDRegistered(XULAppInfoFactory.CID)) {
     88    // Check to see if a contract was already registered and
     89    // register it if it is not. Otherwise, store the previous one
     90    // to be restored later and register the new one.
     91    if (registrar.isContractIDRegistered(XULAppInfoFactory.contractID)) {
     92      dump(
     93        XULAppInfoFactory.scheme +
     94          " is already registered. Storing currently registered object for restoration later."
     95      );
     96      old_factory.CID = registrar.contractIDToCID(XULAppInfoFactory.contractID);
     97      old_factory.factory = Components.manager.getClassObject(
     98        Cc[XULAppInfoFactory.contractID],
     99        Ci.nsIFactory
    100      );
    101    } else {
    102      dump(
    103        XULAppInfoFactory.scheme + " has never been registered. Registering..."
    104      );
    105    }
    106 
    107    registrar.registerFactory(
    108      XULAppInfoFactory.CID,
    109      "test-" + XULAppInfoFactory.scheme,
    110      XULAppInfoFactory.contractID,
    111      XULAppInfoFactory
    112    );
    113  } else {
    114    do_throw("CID " + XULAppInfoFactory.CID + " has already been registered!");
    115  }
    116 
    117  // Check for new chrome
    118  let cr = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(
    119    Ci.nsIChromeRegistry
    120  );
    121  cr.checkForNewChrome();
    122 
    123  // See if our various things were able to register
    124  let registrationTypes = ["content", "locale", "skin", "override", "resource"];
    125  for (let i = 0; i < testProtocols.length; i++) {
    126    let protocol = testProtocols[i];
    127    for (let j = 0; j < registrationTypes.length; j++) {
    128      let type = registrationTypes[j];
    129      dump(
    130        "Testing protocol '" + protocol.scheme + "' with type '" + type + "'\n"
    131      );
    132      let expectedURI = protocol.scheme + "://foo/";
    133      let sourceURI = "chrome://" + protocol.scheme + "/" + type + "/";
    134      switch (type) {
    135        case "content":
    136          expectedURI += protocol.scheme + ".xul";
    137          break;
    138        case "locale":
    139          expectedURI += protocol.scheme + ".dtd";
    140          break;
    141        case "skin":
    142          expectedURI += protocol.scheme + ".css";
    143          break;
    144        case "override":
    145          sourceURI =
    146            "chrome://good-package/content/override-" +
    147            protocol.scheme +
    148            ".xul";
    149          break;
    150        case "resource":
    151          sourceURI = "resource://" + protocol.scheme + "/";
    152          break;
    153      }
    154      try {
    155        sourceURI = Services.io.newURI(sourceURI);
    156        let uri;
    157        if (type == "resource") {
    158          // resources go about a slightly different way than everything else
    159          let rph = Services.io
    160            .getProtocolHandler("resource")
    161            .QueryInterface(Ci.nsIResProtocolHandler);
    162          // this throws for packages that are not registered
    163          uri = rph.resolveURI(sourceURI);
    164        } else {
    165          // this throws for packages that are not registered
    166          uri = cr.convertChromeURL(sourceURI).spec;
    167        }
    168 
    169        if (protocol.shouldRegister) {
    170          Assert.equal(expectedURI, uri);
    171        } else {
    172          // Overrides will not throw, so we'll get to here.  We want to make
    173          // sure that the two strings are not the same in this situation.
    174          Assert.notEqual(expectedURI, uri);
    175        }
    176      } catch (e) {
    177        if (protocol.shouldRegister) {
    178          dump(e + "\n");
    179          do_throw(
    180            "Should have registered our URI for protocol " + protocol.scheme
    181          );
    182        }
    183      }
    184    }
    185  }
    186 
    187  // Unregister our protocol handlers so we do not leak
    188  for (let protocol of testProtocols) {
    189    Services.io.unregisterProtocolHandler(protocol.scheme);
    190  }
    191 
    192  // Unregister XULAppInfoFactory
    193  registrar.unregisterFactory(XULAppInfoFactory.CID, XULAppInfoFactory);
    194  if (old_factory.factory != null) {
    195    registrar.registerFactory(
    196      old_factory.CID,
    197      "",
    198      XULAppInfoFactory.contractID,
    199      null
    200    );
    201  }
    202 }