tor-browser

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

test_tls_handshake_timing.js (4102B)


      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 var { setTimeout } = ChromeUtils.importESModule(
     12  "resource://gre/modules/Timer.sys.mjs"
     13 );
     14 
     15 const { NodeHTTPSServer } = ChromeUtils.importESModule(
     16  "resource://testing-common/NodeServer.sys.mjs"
     17 );
     18 
     19 let h2Port;
     20 let h3Port;
     21 add_setup(function test_setup() {
     22  do_get_profile();
     23  h2Port = Services.env.get("MOZHTTP2_PORT");
     24  Assert.notEqual(h2Port, null);
     25  Assert.notEqual(h2Port, "");
     26  h3Port = Services.env.get("MOZHTTP3_PORT");
     27  Assert.notEqual(h3Port, null);
     28  Assert.notEqual(h3Port, "");
     29 
     30  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
     31    Ci.nsIX509CertDB
     32  );
     33 
     34  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
     35 
     36  Services.prefs.setBoolPref("network.http.http3.enable", true);
     37  Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
     38 });
     39 
     40 registerCleanupFunction(() => {
     41  Services.prefs.clearUserPref("network.http.speculative-parallel-limit");
     42 });
     43 
     44 function makeChan(url) {
     45  let chan = NetUtil.newChannel({
     46    uri: url,
     47    loadUsingSystemPrincipal: true,
     48    contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
     49  }).QueryInterface(Ci.nsIHttpChannel);
     50  return chan;
     51 }
     52 
     53 function channelOpenPromise(chan, flags) {
     54  return new Promise(resolve => {
     55    function finish(req, buffer) {
     56      resolve([req, buffer]);
     57    }
     58    chan.asyncOpen(new ChannelListener(finish, null, flags));
     59  });
     60 }
     61 
     62 async function do_test_timing(url) {
     63  // Make sure all connections are closed before testing.
     64  Services.obs.notifyObservers(null, "net:cancel-all-connections");
     65  // Make sure 0RTT is not involved.
     66  let nssComponent = Cc["@mozilla.org/psm;1"].getService(Ci.nsINSSComponent);
     67  await nssComponent.asyncClearSSLExternalAndInternalSessionCache();
     68  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     69  await new Promise(resolve => setTimeout(resolve, 1000));
     70 
     71  let chan = makeChan(url);
     72  let timedChannel = chan.QueryInterface(Ci.nsITimedChannel);
     73  await channelOpenPromise(chan);
     74  info(`secureConnectionStartTime=${timedChannel.secureConnectionStartTime}`);
     75  info(`connectEndTime=${timedChannel.connectEndTime}`);
     76  Assert.greater(timedChannel.secureConnectionStartTime, 0);
     77  Assert.greater(timedChannel.connectEndTime, 0);
     78  let handshakeTime =
     79    timedChannel.connectEndTime - timedChannel.secureConnectionStartTime;
     80  Assert.greater(handshakeTime, 0);
     81  info(`handshakeTime=${handshakeTime}`);
     82  info("perfMetrics", { handshakeTime });
     83 }
     84 
     85 add_task(async function test_http2() {
     86  Services.prefs.setIntPref("network.http.speculative-parallel-limit", 6);
     87  await do_test_timing(`https://foo.example.com:${h2Port}/server-timing`);
     88 
     89  Services.prefs.setIntPref("network.http.speculative-parallel-limit", 0);
     90  await do_test_timing(`https://foo.example.com:${h2Port}/server-timing`);
     91 });
     92 
     93 add_task(async function test_http1() {
     94  let server = new NodeHTTPSServer();
     95  await server.start();
     96  registerCleanupFunction(async () => {
     97    await server.stop();
     98  });
     99 
    100  await server.registerPathHandler("/test", (req, resp) => {
    101    const output = "done";
    102    resp.setHeader("Content-Length", output.length);
    103    resp.writeHead(200);
    104    resp.end(output);
    105  });
    106 
    107  Services.prefs.setIntPref("network.http.speculative-parallel-limit", 6);
    108  await do_test_timing(`https://localhost:${server.port()}/test`);
    109 
    110  Services.prefs.setIntPref("network.http.speculative-parallel-limit", 0);
    111  await do_test_timing(`https://localhost:${server.port()}/test`);
    112 });
    113 
    114 add_task(async function test_http3() {
    115  await http3_setup_tests("h3", true);
    116 
    117  Services.prefs.setIntPref("network.http.speculative-parallel-limit", 6);
    118  await do_test_timing(`https://foo.example.com/`);
    119 
    120  Services.prefs.setIntPref("network.http.speculative-parallel-limit", 0);
    121  await do_test_timing(`https://foo.example.com/`);
    122 });