tor-browser

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

test_http3_coalescing.js (3905B)


      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 var { setTimeout } = ChromeUtils.importESModule(
      8  "resource://gre/modules/Timer.sys.mjs"
      9 );
     10 
     11 let h3Port;
     12 
     13 const certOverrideService = Cc[
     14  "@mozilla.org/security/certoverride;1"
     15 ].getService(Ci.nsICertOverrideService);
     16 
     17 function setup() {
     18  h3Port = Services.env.get("MOZHTTP3_PORT");
     19  Assert.notEqual(h3Port, null);
     20  Assert.notEqual(h3Port, "");
     21  Services.prefs.setBoolPref("network.http.http3.enable", true);
     22 }
     23 
     24 setup();
     25 registerCleanupFunction(async () => {
     26  Services.prefs.clearUserPref("network.dns.upgrade_with_https_rr");
     27  Services.prefs.clearUserPref("network.dns.use_https_rr_as_altsvc");
     28  Services.prefs.clearUserPref("network.dns.echconfig.enabled");
     29  Services.prefs.clearUserPref(
     30    "network.dns.echconfig.fallback_to_origin_when_all_failed"
     31  );
     32  Services.prefs.clearUserPref("network.dns.httpssvc.reset_exclustion_list");
     33  Services.prefs.clearUserPref("network.http.http3.enable");
     34  Services.prefs.clearUserPref(
     35    "network.dns.httpssvc.http3_fast_fallback_timeout"
     36  );
     37  Services.prefs.clearUserPref("network.http.http3.pmtud");
     38  Services.prefs.clearUserPref(
     39    "network.http.http3.alt-svc-mapping-for-testing"
     40  );
     41  Services.prefs.clearUserPref("network.dns.localDomains");
     42  Services.prefs.clearUserPref("network.http.speculative-parallel-limit");
     43 });
     44 
     45 function makeChan(url) {
     46  let chan = NetUtil.newChannel({
     47    uri: url,
     48    loadUsingSystemPrincipal: true,
     49    contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
     50  }).QueryInterface(Ci.nsIHttpChannel);
     51  chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
     52  return chan;
     53 }
     54 
     55 function channelOpenPromise(chan, flags) {
     56  return new Promise(resolve => {
     57    function finish(req, buffer) {
     58      resolve([req, buffer]);
     59      certOverrideService.setDisableAllSecurityChecksAndLetAttackersInterceptMyData(
     60        false
     61      );
     62    }
     63    certOverrideService.setDisableAllSecurityChecksAndLetAttackersInterceptMyData(
     64      true
     65    );
     66    chan.asyncOpen(new ChannelListener(finish, null, flags));
     67  });
     68 }
     69 
     70 async function H3CoalescingTest(host1, host2) {
     71  Services.prefs.setCharPref(
     72    "network.http.http3.alt-svc-mapping-for-testing",
     73    `${host1};h3=:${h3Port}`
     74  );
     75  Services.prefs.setCharPref("network.dns.localDomains", host1);
     76 
     77  let chan = makeChan(`https://${host1}`);
     78  let [req] = await channelOpenPromise(chan, CL_ALLOW_UNKNOWN_CL);
     79  req.QueryInterface(Ci.nsIHttpChannel);
     80  Assert.equal(req.protocolVersion, "h3");
     81  let hash = req.getResponseHeader("x-http3-conn-hash");
     82 
     83  Services.prefs.setCharPref(
     84    "network.http.http3.alt-svc-mapping-for-testing",
     85    `${host2};h3=:${h3Port}`
     86  );
     87  Services.prefs.setCharPref("network.dns.localDomains", host2);
     88 
     89  chan = makeChan(`https://${host2}`);
     90  [req] = await channelOpenPromise(chan, CL_ALLOW_UNKNOWN_CL);
     91  req.QueryInterface(Ci.nsIHttpChannel);
     92  Assert.equal(req.protocolVersion, "h3");
     93  // The port used by the second connection should be the same as the first one.
     94  Assert.equal(req.getResponseHeader("x-http3-conn-hash"), hash);
     95 }
     96 
     97 add_task(async function testH3CoalescingWithSpeculativeConnection() {
     98  await http3_setup_tests("h3");
     99  Services.prefs.setIntPref("network.http.speculative-parallel-limit", 6);
    100  await H3CoalescingTest("foo.h3_coalescing.org", "bar.h3_coalescing.org");
    101 });
    102 
    103 add_task(async function testH3CoalescingWithoutSpeculativeConnection() {
    104  Services.prefs.setIntPref("network.http.speculative-parallel-limit", 0);
    105  Services.obs.notifyObservers(null, "net:cancel-all-connections");
    106  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    107  await new Promise(resolve => setTimeout(resolve, 1000));
    108  await H3CoalescingTest("baz.h3_coalescing.org", "qux.h3_coalescing.org");
    109 });