Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | Cu.import("resource://gre/modules/Log.jsm"); |
michael@0 | 6 | Cu.import("resource://services-common/utils.js"); |
michael@0 | 7 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 8 | Cu.import("resource://testing-common/services-common/logging.js"); |
michael@0 | 9 | |
michael@0 | 10 | let btoa = Cu.import("resource://gre/modules/Log.jsm").btoa; |
michael@0 | 11 | let atob = Cu.import("resource://gre/modules/Log.jsm").atob; |
michael@0 | 12 | |
michael@0 | 13 | function do_check_empty(obj) { |
michael@0 | 14 | do_check_attribute_count(obj, 0); |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | function do_check_attribute_count(obj, c) { |
michael@0 | 18 | do_check_eq(c, Object.keys(obj).length); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | function do_check_throws(aFunc, aResult, aStack) { |
michael@0 | 22 | if (!aStack) { |
michael@0 | 23 | try { |
michael@0 | 24 | // We might not have a 'Components' object. |
michael@0 | 25 | aStack = Components.stack.caller; |
michael@0 | 26 | } catch (e) {} |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | try { |
michael@0 | 30 | aFunc(); |
michael@0 | 31 | } catch (e) { |
michael@0 | 32 | do_check_eq(e.result, aResult, aStack); |
michael@0 | 33 | return; |
michael@0 | 34 | } |
michael@0 | 35 | do_throw("Expected result " + aResult + ", none thrown.", aStack); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | |
michael@0 | 39 | /** |
michael@0 | 40 | * Test whether specified function throws exception with expected |
michael@0 | 41 | * result. |
michael@0 | 42 | * |
michael@0 | 43 | * @param func |
michael@0 | 44 | * Function to be tested. |
michael@0 | 45 | * @param message |
michael@0 | 46 | * Message of expected exception. <code>null</code> for no throws. |
michael@0 | 47 | */ |
michael@0 | 48 | function do_check_throws_message(aFunc, aResult) { |
michael@0 | 49 | try { |
michael@0 | 50 | aFunc(); |
michael@0 | 51 | } catch (e) { |
michael@0 | 52 | do_check_eq(e.message, aResult); |
michael@0 | 53 | return; |
michael@0 | 54 | } |
michael@0 | 55 | do_throw("Expected an error, none thrown."); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * Print some debug message to the console. All arguments will be printed, |
michael@0 | 60 | * separated by spaces. |
michael@0 | 61 | * |
michael@0 | 62 | * @param [arg0, arg1, arg2, ...] |
michael@0 | 63 | * Any number of arguments to print out |
michael@0 | 64 | * @usage _("Hello World") -> prints "Hello World" |
michael@0 | 65 | * @usage _(1, 2, 3) -> prints "1 2 3" |
michael@0 | 66 | */ |
michael@0 | 67 | let _ = function(some, debug, text, to) print(Array.slice(arguments).join(" ")); |
michael@0 | 68 | |
michael@0 | 69 | function httpd_setup (handlers, port=-1) { |
michael@0 | 70 | let server = new HttpServer(); |
michael@0 | 71 | for (let path in handlers) { |
michael@0 | 72 | server.registerPathHandler(path, handlers[path]); |
michael@0 | 73 | } |
michael@0 | 74 | try { |
michael@0 | 75 | server.start(port); |
michael@0 | 76 | } catch (ex) { |
michael@0 | 77 | _("=========================================="); |
michael@0 | 78 | _("Got exception starting HTTP server on port " + port); |
michael@0 | 79 | _("Error: " + CommonUtils.exceptionStr(ex)); |
michael@0 | 80 | _("Is there a process already listening on port " + port + "?"); |
michael@0 | 81 | _("=========================================="); |
michael@0 | 82 | do_throw(ex); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | // Set the base URI for convenience. |
michael@0 | 86 | let i = server.identity; |
michael@0 | 87 | server.baseURI = i.primaryScheme + "://" + i.primaryHost + ":" + i.primaryPort; |
michael@0 | 88 | |
michael@0 | 89 | return server; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | function httpd_handler(statusCode, status, body) { |
michael@0 | 93 | return function handler(request, response) { |
michael@0 | 94 | _("Processing request"); |
michael@0 | 95 | // Allow test functions to inspect the request. |
michael@0 | 96 | request.body = readBytesFromInputStream(request.bodyInputStream); |
michael@0 | 97 | handler.request = request; |
michael@0 | 98 | |
michael@0 | 99 | response.setStatusLine(request.httpVersion, statusCode, status); |
michael@0 | 100 | if (body) { |
michael@0 | 101 | response.bodyOutputStream.write(body, body.length); |
michael@0 | 102 | } |
michael@0 | 103 | }; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | /* |
michael@0 | 107 | * Read bytes string from an nsIInputStream. If 'count' is omitted, |
michael@0 | 108 | * all available input is read. |
michael@0 | 109 | */ |
michael@0 | 110 | function readBytesFromInputStream(inputStream, count) { |
michael@0 | 111 | return CommonUtils.readBytesFromInputStream(inputStream, count); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | /* |
michael@0 | 115 | * Ensure exceptions from inside callbacks leads to test failures. |
michael@0 | 116 | */ |
michael@0 | 117 | function ensureThrows(func) { |
michael@0 | 118 | return function() { |
michael@0 | 119 | try { |
michael@0 | 120 | func.apply(this, arguments); |
michael@0 | 121 | } catch (ex) { |
michael@0 | 122 | do_throw(ex); |
michael@0 | 123 | } |
michael@0 | 124 | }; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * Proxy auth helpers. |
michael@0 | 129 | */ |
michael@0 | 130 | |
michael@0 | 131 | /** |
michael@0 | 132 | * Fake a PAC to prompt a channel replacement. |
michael@0 | 133 | */ |
michael@0 | 134 | let PACSystemSettings = { |
michael@0 | 135 | CID: Components.ID("{5645d2c1-d6d8-4091-b117-fe7ee4027db7}"), |
michael@0 | 136 | contractID: "@mozilla.org/system-proxy-settings;1", |
michael@0 | 137 | |
michael@0 | 138 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory, |
michael@0 | 139 | Ci.nsISystemProxySettings]), |
michael@0 | 140 | |
michael@0 | 141 | createInstance: function createInstance(outer, iid) { |
michael@0 | 142 | if (outer) { |
michael@0 | 143 | throw Cr.NS_ERROR_NO_AGGREGATION; |
michael@0 | 144 | } |
michael@0 | 145 | return this.QueryInterface(iid); |
michael@0 | 146 | }, |
michael@0 | 147 | |
michael@0 | 148 | lockFactory: function lockFactory(lock) { |
michael@0 | 149 | throw Cr.NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 150 | }, |
michael@0 | 151 | |
michael@0 | 152 | // Replace this URI for each test to avoid caching. We want to ensure that |
michael@0 | 153 | // each test gets a completely fresh setup. |
michael@0 | 154 | mainThreadOnly: true, |
michael@0 | 155 | PACURI: null, |
michael@0 | 156 | getProxyForURI: function getProxyForURI(aURI) { |
michael@0 | 157 | throw Cr.NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 158 | } |
michael@0 | 159 | }; |
michael@0 | 160 | |
michael@0 | 161 | function installFakePAC() { |
michael@0 | 162 | _("Installing fake PAC."); |
michael@0 | 163 | Cm.nsIComponentRegistrar |
michael@0 | 164 | .registerFactory(PACSystemSettings.CID, |
michael@0 | 165 | "Fake system proxy-settings", |
michael@0 | 166 | PACSystemSettings.contractID, |
michael@0 | 167 | PACSystemSettings); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | function uninstallFakePAC() { |
michael@0 | 171 | _("Uninstalling fake PAC."); |
michael@0 | 172 | let CID = PACSystemSettings.CID; |
michael@0 | 173 | Cm.nsIComponentRegistrar.unregisterFactory(CID, PACSystemSettings); |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | // Many tests do service.startOver() and don't expect the provider type to |
michael@0 | 177 | // change (whereas by default, a startOver will do exactly that so FxA is |
michael@0 | 178 | // subsequently used). The tests that know how to deal with |
michael@0 | 179 | // the Firefox Accounts identity hack things to ensure that still works. |
michael@0 | 180 | function ensureStartOverKeepsIdentity() { |
michael@0 | 181 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 182 | Services.prefs.setBoolPref("services.sync-testing.startOverKeepIdentity", true); |
michael@0 | 183 | do_register_cleanup(function() { |
michael@0 | 184 | Services.prefs.clearUserPref("services.sync-testing.startOverKeepIdentity"); |
michael@0 | 185 | }); |
michael@0 | 186 | } |
michael@0 | 187 | ensureStartOverKeepsIdentity(); |