tor-browser

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

test_h2proxy_connection_limit.js (2626B)


      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 // Summary:
      6 // Test whether the connection limit is honored when http2 proxy is used.
      7 //
      8 // Test step:
      9 // 1. Create 30 http requests.
     10 // 2. Check if the count of all sockets created by proxy is less than 6.
     11 
     12 "use strict";
     13 
     14 const { NodeHTTP2ProxyServer, NodeHTTP2Server, with_node_servers } =
     15  ChromeUtils.importESModule("resource://testing-common/NodeServer.sys.mjs");
     16 
     17 /* import-globals-from head_cache.js */
     18 /* import-globals-from head_cookies.js */
     19 /* import-globals-from head_channels.js */
     20 
     21 // We don't normally allow localhost channels to be proxied, but this
     22 // is easier than updating all the certs and/or domains.
     23 Services.prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
     24 registerCleanupFunction(() => {
     25  Services.prefs.clearUserPref("network.proxy.allow_hijacking_localhost");
     26 });
     27 
     28 function makeChan(uri) {
     29  let chan = NetUtil.newChannel({
     30    uri,
     31    loadUsingSystemPrincipal: true,
     32  }).QueryInterface(Ci.nsIHttpChannel);
     33  chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
     34  return chan;
     35 }
     36 
     37 add_task(async function test_connection_limit() {
     38  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
     39    Ci.nsIX509CertDB
     40  );
     41  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
     42  addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
     43 
     44  let proxy = new NodeHTTP2ProxyServer();
     45  await proxy.start();
     46  registerCleanupFunction(async () => {
     47    await proxy.stop();
     48  });
     49 
     50  const maxConnections = 6;
     51  Services.prefs.setIntPref(
     52    "network.http.max-persistent-connections-per-server",
     53    maxConnections
     54  );
     55  registerCleanupFunction(async () => {
     56    Services.prefs.clearUserPref(
     57      "network.http.max-persistent-connections-per-server"
     58    );
     59  });
     60 
     61  await with_node_servers([NodeHTTP2Server], async server => {
     62    await server.registerPathHandler("/test", (req, resp) => {
     63      resp.writeHead(200);
     64      resp.end("All good");
     65    });
     66 
     67    let promises = [];
     68    for (let i = 0; i < 30; ++i) {
     69      let chan = makeChan(`${server.origin()}/test`);
     70      promises.push(
     71        new Promise(resolve => {
     72          chan.asyncOpen(
     73            new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL)
     74          );
     75        })
     76      );
     77    }
     78    await Promise.all(promises);
     79    let count = await proxy.socketCount(server.port());
     80    Assert.lessOrEqual(
     81      count,
     82      maxConnections,
     83      "socket count should be less than maxConnections"
     84    );
     85  });
     86 });