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.

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

mercurial