tor-browser

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

test_proxyconnect_https.js (2248B)


      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
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 /* import-globals-from head_cache.js */
      8 /* import-globals-from head_cookies.js */
      9 /* import-globals-from head_channels.js */
     10 
     11 const { TestUtils } = ChromeUtils.importESModule(
     12  "resource://testing-common/TestUtils.sys.mjs"
     13 );
     14 
     15 const { NodeHTTPSProxyServer, NodeHTTPSServer } = ChromeUtils.importESModule(
     16  "resource://testing-common/NodeServer.sys.mjs"
     17 );
     18 
     19 // We don't normally allow localhost channels to be proxied, but this
     20 // is easier than updating all the certs and/or domains.
     21 Services.prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
     22 registerCleanupFunction(() => {
     23  Services.prefs.clearUserPref("network.proxy.allow_hijacking_localhost");
     24 });
     25 
     26 function makeChan(uri) {
     27  let chan = NetUtil.newChannel({
     28    uri,
     29    loadUsingSystemPrincipal: true,
     30  }).QueryInterface(Ci.nsIHttpChannel);
     31  chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
     32  return chan;
     33 }
     34 
     35 let gotTransport = false;
     36 var upgradeListener = {
     37  onTransportAvailable: () => {
     38    gotTransport = true;
     39  },
     40  QueryInterface: ChromeUtils.generateQI(["nsIHttpUpgradeListener"]),
     41 };
     42 
     43 add_task(async function test_connect_only_https() {
     44  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
     45    Ci.nsIX509CertDB
     46  );
     47  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
     48  addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
     49 
     50  let proxy = new NodeHTTPSProxyServer();
     51  await proxy.start();
     52  let server = new NodeHTTPSServer();
     53  await server.start();
     54  registerCleanupFunction(async () => {
     55    await proxy.stop();
     56    await server.stop();
     57  });
     58 
     59  let chan = makeChan(`https://localhost:${server.port()}/test`);
     60  var internal = chan.QueryInterface(Ci.nsIHttpChannelInternal);
     61  internal.HTTPUpgrade("webrtc", upgradeListener);
     62  internal.setConnectOnly(false);
     63  await new Promise(resolve => {
     64    chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
     65  });
     66 
     67  await TestUtils.waitForCondition(() => gotTransport);
     68  Assert.ok(gotTransport);
     69 
     70  await proxy.stop();
     71  await server.stop();
     72 });