netwerk/test/browser/browser_NetUtil.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 /*
     2 Any copyright is dedicated to the Public Domain.
     3 http://creativecommons.org/publicdomain/zero/1.0/
     4 */
     6 Components.utils.import("resource://gre/modules/NetUtil.jsm");
     8 function test() {
     9   waitForExplicitFinish();
    11   // We overload this test to include verifying that httpd.js is
    12   // importable as a testing-only JS module.
    13   Components.utils.import("resource://testing-common/httpd.js", {});
    15   nextTest();
    16 }
    18 function nextTest() {
    19   if (tests.length)
    20     executeSoon(tests.shift());
    21   else
    22     executeSoon(finish);
    23 }
    25 var tests = [
    26   test_asyncFetchBadCert,
    27 ];
    29 var gCertErrorDialogShown = 0;
    31 // We used to show a dialog box by default when we encountered an SSL
    32 // certificate error. Now we treat these errors just like other
    33 // networking errors; the dialog is no longer shown.
    34 function test_asyncFetchBadCert() {
    35   let listener = new WindowListener("chrome://pippki/content/certerror.xul", function (domwindow) {
    36     gCertErrorDialogShown++;
    38     // Close the dialog
    39     domwindow.document.documentElement.cancelDialog();
    40   });
    42   Services.wm.addListener(listener);
    44   // Try a load from an untrusted cert, with errors supressed
    45   NetUtil.asyncFetch("https://untrusted.example.com", function (aInputStream, aStatusCode, aRequest) {
    46     ok(!Components.isSuccessCode(aStatusCode), "request failed");
    47     ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
    49     is(gCertErrorDialogShown, 0, "cert error dialog was not shown");
    51     // Now try again with a channel whose notificationCallbacks doesn't suprress errors
    52     let channel = NetUtil.newChannel("https://untrusted.example.com");
    53     channel.notificationCallbacks = {
    54       QueryInterface: XPCOMUtils.generateQI([Ci.nsIProgressEventSink,
    55                                              Ci.nsIInterfaceRequestor]),
    56       getInterface: function (aIID) this.QueryInterface(aIID),
    57       onProgress: function () {},
    58       onStatus: function () {}
    59     };
    60     NetUtil.asyncFetch(channel, function (aInputStream, aStatusCode, aRequest) {
    61       ok(!Components.isSuccessCode(aStatusCode), "request failed");
    62       ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
    64       is(gCertErrorDialogShown, 0, "cert error dialog was not shown");
    66       // Now try a valid request
    67       NetUtil.asyncFetch("https://example.com", function (aInputStream, aStatusCode, aRequest) {
    68         info("aStatusCode for valid request: " + aStatusCode);
    69         ok(Components.isSuccessCode(aStatusCode), "request succeeded");
    70         ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
    71         ok(aRequest.requestSucceeded, "HTTP request succeeded");
    73         is(gCertErrorDialogShown, 0, "cert error dialog was not shown");
    75         Services.wm.removeListener(listener);
    76         nextTest();
    77       });
    78     });
    80   });
    81 }
    83 function WindowListener(aURL, aCallback) {
    84   this.callback = aCallback;
    85   this.url = aURL;
    86 }
    87 WindowListener.prototype = {
    88   onOpenWindow: function(aXULWindow) {
    89     var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
    90                               .getInterface(Ci.nsIDOMWindow);
    91     var self = this;
    92     domwindow.addEventListener("load", function() {
    93       domwindow.removeEventListener("load", arguments.callee, false);
    95       if (domwindow.document.location.href != self.url)
    96         return;
    98       // Allow other window load listeners to execute before passing to callback
    99       executeSoon(function() {
   100         self.callback(domwindow);
   101       });
   102     }, false);
   103   },
   104   onCloseWindow: function(aXULWindow) {},
   105   onWindowTitleChange: function(aXULWindow, aNewTitle) {}
   106 }

mercurial