tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_NetUtil.js (3072B)


      1 /*
      2 Any copyright is dedicated to the Public Domain.
      3 http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 "use strict";
      6 
      7 function test() {
      8  waitForExplicitFinish();
      9 
     10  // We overload this test to include verifying that httpd.js is
     11  // importable as a testing-only JS module.
     12  ChromeUtils.importESModule("resource://testing-common/httpd.sys.mjs");
     13 
     14  nextTest();
     15 }
     16 
     17 function nextTest() {
     18  if (tests.length) {
     19    executeSoon(tests.shift());
     20  } else {
     21    executeSoon(finish);
     22  }
     23 }
     24 
     25 var tests = [test_asyncFetchBadCert];
     26 
     27 function test_asyncFetchBadCert() {
     28  // Try a load from an untrusted cert, with errors supressed
     29  NetUtil.asyncFetch(
     30    {
     31      uri: "https://untrusted.example.com",
     32      loadUsingSystemPrincipal: true,
     33    },
     34    function (aInputStream, aStatusCode, aRequest) {
     35      ok(!Components.isSuccessCode(aStatusCode), "request failed");
     36      ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
     37 
     38      // Now try again with a channel whose notificationCallbacks doesn't suprress errors
     39      let channel = NetUtil.newChannel({
     40        uri: "https://untrusted.example.com",
     41        loadUsingSystemPrincipal: true,
     42      });
     43      channel.notificationCallbacks = {
     44        QueryInterface: ChromeUtils.generateQI([
     45          "nsIProgressEventSink",
     46          "nsIInterfaceRequestor",
     47        ]),
     48        getInterface(aIID) {
     49          return this.QueryInterface(aIID);
     50        },
     51        onProgress() {},
     52        onStatus() {},
     53      };
     54      NetUtil.asyncFetch(
     55        channel,
     56        function (aInputStream, aStatusCode, aRequest) {
     57          ok(!Components.isSuccessCode(aStatusCode), "request failed");
     58          ok(
     59            aRequest instanceof Ci.nsIHttpChannel,
     60            "request is an nsIHttpChannel"
     61          );
     62 
     63          // Now try a valid request
     64          NetUtil.asyncFetch(
     65            {
     66              uri: "https://example.com",
     67              loadUsingSystemPrincipal: true,
     68            },
     69            function (aInputStream, aStatusCode, aRequest) {
     70              info("aStatusCode for valid request: " + aStatusCode);
     71              ok(Components.isSuccessCode(aStatusCode), "request succeeded");
     72              ok(
     73                aRequest instanceof Ci.nsIHttpChannel,
     74                "request is an nsIHttpChannel"
     75              );
     76              ok(aRequest.requestSucceeded, "HTTP request succeeded");
     77 
     78              nextTest();
     79            }
     80          );
     81        }
     82      );
     83    }
     84  );
     85 }
     86 
     87 function WindowListener(aURL, aCallback) {
     88  this.callback = aCallback;
     89  this.url = aURL;
     90 }
     91 WindowListener.prototype = {
     92  onOpenWindow(aXULWindow) {
     93    var domwindow = aXULWindow.docShell.domWindow;
     94    var self = this;
     95    domwindow.addEventListener(
     96      "load",
     97      function () {
     98        if (domwindow.document.location.href != self.url) {
     99          return;
    100        }
    101 
    102        // Allow other window load listeners to execute before passing to callback
    103        executeSoon(function () {
    104          self.callback(domwindow);
    105        });
    106      },
    107      { once: true }
    108    );
    109  },
    110  onCloseWindow() {},
    111 };