chrome/test/unit/test_no_remote_registration.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
     7 let manifests = [
     8   do_get_file("data/test_no_remote_registration.manifest"),
     9 ];
    10 registerManifests(manifests);
    12 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    14 let XULAppInfo = {
    15   vendor: "Mozilla",
    16   name: "XPCShell",
    17   ID: "{39885e5f-f6b4-4e2a-87e5-6259ecf79011}",
    18   version: "5",
    19   appBuildID: "2007010101",
    20   platformVersion: "1.9",
    21   platformBuildID: "2007010101",
    22   inSafeMode: false,
    23   logConsoleErrors: true,
    24   OS: "XPCShell",
    25   XPCOMABI: "noarch-spidermonkey",
    26   QueryInterface: XPCOMUtils.generateQI([
    27     Ci.nsIXULAppInfo,
    28     Ci.nsIXULRuntime,
    29   ])
    30 };
    32 let XULAppInfoFactory = {
    33   // These two are used when we register all our factories (and unregister)
    34   CID: XULAPPINFO_CID,
    35   scheme: "XULAppInfo",
    36   contractID: XULAPPINFO_CONTRACTID,
    37   createInstance: function (outer, iid) {
    38     if (outer != null)
    39       throw Cr.NS_ERROR_NO_AGGREGATION;
    40     return XULAppInfo.QueryInterface(iid);
    41   }
    42 };
    44 function ProtocolHandler(aScheme, aFlags)
    45 {
    46   this.scheme = aScheme;
    47   this.protocolFlags = aFlags;
    48   this.contractID = "@mozilla.org/network/protocol;1?name=" + aScheme;
    49 }
    51 ProtocolHandler.prototype =
    52 {
    53   defaultPort: -1,
    54   allowPort: function() false,
    55   newURI: function(aSpec, aCharset, aBaseURI)
    56   {
    57     let uri = Cc["@mozilla.org/network/standard-url;1"].
    58               createInstance(Ci.nsIURI);
    59     uri.spec = aSpec;
    60     if (!uri.scheme) {
    61       // We got a partial uri, so let's resolve it with the base one
    62       uri.spec = aBaseURI.resolve(aSpec);
    63     }
    64     return uri;
    65   },
    66   newChannel: function() { throw Cr.NS_ERROR_NOT_IMPLEMENTED },
    67   QueryInterface: XPCOMUtils.generateQI([
    68     Ci.nsIProtocolHandler
    69   ])
    70 };
    72 let testProtocols = [
    73   // It doesn't matter if it has this flag - the only flag we accept is
    74   // URI_IS_LOCAL_RESOURCE.
    75   {scheme: "moz-protocol-ui-resource",
    76    flags: Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE,
    77    CID: Components.ID("{d6dedc93-558f-44fe-90f4-3b4bba8a0b14}"),
    78    shouldRegister: false
    79   },
    80   // It doesn't matter if it has this flag - the only flag we accept is
    81   // URI_IS_LOCAL_RESOURCE.
    82   {scheme: "moz-protocol-local-file",
    83    flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_FILE,
    84    CID: Components.ID("{ee30d594-0a2d-4f47-89cc-d4cde320ab69}"),
    85    shouldRegister: false
    86   },
    87   // This clearly is non-local
    88   {scheme: "moz-protocol-loadable-by-anyone",
    89    flags: Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
    90    CID: Components.ID("{c3735f23-3b0c-4a33-bfa0-79436dcd40b2}"),
    91    shouldRegister: false
    92   },
    93   // This should always be last (unless we add more flags that are OK)
    94   {scheme: "moz-protocol-local-resource",
    95    flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE,
    96    CID: Components.ID("{b79e977c-f840-469a-b413-0125cc1b62a5}"),
    97    shouldRegister: true
    98   },
    99 ];
   100 function run_test()
   101 {
   102   // Create factories
   103   let factories = [];
   104   for (let i = 0; i < testProtocols.length; i++) {
   105     factories[i] = {
   106       scheme: testProtocols[i].scheme,
   107       flags: testProtocols[i].flags,
   108       CID: testProtocols[i].CID,
   109       contractID: "@mozilla.org/network/protocol;1?name=" + testProtocols[i].scheme,
   110       createInstance: function(aOuter, aIID)
   111       {
   112         if (aOuter != null)
   113           throw Cr.NS_ERROR_NO_AGGREGATION;
   114         let handler = new ProtocolHandler(this.scheme, this.flags, this.CID);
   115         return handler.QueryInterface(aIID);
   116       }
   117     };
   118   }
   119   // Add our XULAppInfo factory
   120   factories.push(XULAppInfoFactory);
   122   // Register our factories
   123   for (let i = 0; i < factories.length; i++) {
   124     let factory = factories[i];
   125     Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
   126               .registerFactory(factory.CID, "test-" + factory.scheme,
   127                                factory.contractID, factory);
   128   }
   130   // Check for new chrome
   131   let cr = Cc["@mozilla.org/chrome/chrome-registry;1"].
   132            getService(Ci.nsIChromeRegistry);
   133   cr.checkForNewChrome();
   135   // See if our various things were able to register
   136   let registrationTypes = [
   137     "content",
   138     "locale",
   139     "skin",
   140     "override",
   141     "resource",
   142   ];
   143   for (let i = 0; i < testProtocols.length; i++) {
   144     let protocol = testProtocols[i];
   145     for (let j = 0; j < registrationTypes.length; j++) {
   146       let type = registrationTypes[j];
   147       dump("Testing protocol '" + protocol.scheme + "' with type '" + type +
   148            "'\n");
   149       let expectedURI = protocol.scheme + "://foo/";
   150       let sourceURI = "chrome://" + protocol.scheme + "/" + type + "/";
   151       switch (type) {
   152         case "content":
   153           expectedURI += protocol.scheme + ".xul";
   154           break;
   155         case "locale":
   156           expectedURI += protocol.scheme + ".dtd";
   157           break;
   158         case "skin":
   159           expectedURI += protocol.scheme + ".css";
   160           break;
   161         case "override":
   162           sourceURI = "chrome://good-package/content/override-" +
   163                       protocol.scheme + ".xul";
   164           break;
   165         case "resource":
   166           sourceURI = "resource://" + protocol.scheme + "/";
   167           break;
   168       };
   169       try {
   170         let ios = Cc["@mozilla.org/network/io-service;1"].
   171                   getService(Ci.nsIIOService);
   172         sourceURI = ios.newURI(sourceURI, null, null);
   173         let uri;
   174         if (type == "resource") {
   175           // resources go about a slightly different way than everything else
   176           let rph = ios.getProtocolHandler("resource").
   177                     QueryInterface(Ci.nsIResProtocolHandler);
   178           // this throws for packages that are not registered
   179           uri = rph.resolveURI(sourceURI);
   180         }
   181         else {
   182           // this throws for packages that are not registered
   183           uri = cr.convertChromeURL(sourceURI).spec;
   184         }
   186         if (protocol.shouldRegister) {
   187           do_check_eq(expectedURI, uri);
   188         }
   189         else {
   190           // Overrides will not throw, so we'll get to here.  We want to make
   191           // sure that the two strings are not the same in this situation.
   192           do_check_neq(expectedURI, uri);
   193         }
   194       }
   195       catch (e) {
   196         if (protocol.shouldRegister) {
   197           dump(e + "\n");
   198           do_throw("Should have registered our URI for protocol " +
   199                    protocol.scheme);
   200         }
   201       }
   202     }
   203   }
   205   // Unregister our factories so we do not leak
   206   for (let i = 0; i < factories.length; i++) {
   207     let factory = factories[i];
   208     Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
   209               .unregisterFactory(factory.CID, factory);
   210   }
   211 }

mercurial