services/common/tests/unit/head_helpers.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial