tor-browser

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

test_http3_fallback_anonymous_conn.js (2812B)


      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_http3.js */
      8 
      9 const { NodeHTTP2Server } = ChromeUtils.importESModule(
     10  "resource://testing-common/NodeServer.sys.mjs"
     11 );
     12 
     13 function makeChan(uri) {
     14  let chan = NetUtil.newChannel({
     15    uri,
     16    loadUsingSystemPrincipal: true,
     17  }).QueryInterface(Ci.nsIHttpChannel);
     18  chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
     19  return chan;
     20 }
     21 
     22 function channelOpenPromise(chan, flags) {
     23  return new Promise(resolve => {
     24    function finish(req, buffer) {
     25      resolve([req, buffer]);
     26    }
     27    chan.asyncOpen(new ChannelListener(finish, null, flags));
     28  });
     29 }
     30 
     31 add_setup(async function setup() {
     32  Services.prefs.setCharPref("network.dns.localDomains", "alt1.example.com");
     33 
     34  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
     35    Ci.nsIX509CertDB
     36  );
     37  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
     38  addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
     39 
     40  // A dummy request to make sure AltSvcCache::mStorage is ready.
     41  let chan = makeChan(`https://localhost`);
     42  await channelOpenPromise(chan, CL_EXPECT_FAILURE);
     43 });
     44 
     45 add_task(async function test_fallback() {
     46  // We only need the `noResponsePort`, not the masque proxy.
     47  const { noResponsePort } = await create_masque_proxy_server();
     48 
     49  Services.prefs.setCharPref(
     50    "network.http.http3.alt-svc-mapping-for-testing",
     51    `alt1.example.com;h3=:${noResponsePort}`
     52  );
     53 
     54  let server = new NodeHTTP2Server();
     55  await server.start(noResponsePort);
     56 
     57  Assert.equal(server.port(), noResponsePort);
     58 
     59  await server.registerPathHandler("/request1", (req, resp) => {
     60    resp.writeHead(200);
     61    resp.end("response1");
     62  });
     63  await server.registerPathHandler("/request2", (req, resp) => {
     64    resp.writeHead(200);
     65    resp.end("response2");
     66  });
     67 
     68  registerCleanupFunction(async () => {
     69    await server.stop();
     70  });
     71 
     72  let chan = makeChan(
     73    `${server.protocol()}://alt1.example.com:${server.port()}/request1`
     74  );
     75  let [req, buf] = await channelOpenPromise(
     76    chan,
     77    CL_IGNORE_CL | CL_ALLOW_UNKNOWN_CL
     78  );
     79  Assert.equal(req.protocolVersion, "h2");
     80  Assert.equal(buf, "response1");
     81 
     82  let chan1 = makeChan(
     83    `${server.protocol()}://alt1.example.com:${server.port()}/request2`
     84  );
     85  chan1.loadFlags = Ci.nsIRequest.LOAD_ANONYMOUS;
     86  [req, buf] = await Promise.race([
     87    channelOpenPromise(chan1, CL_IGNORE_CL | CL_ALLOW_UNKNOWN_CL),
     88    // chan1 should be completed within a short time.
     89    new Promise(resolve => {
     90      do_timeout(3000, resolve);
     91    }),
     92  ]);
     93  Assert.equal(req.protocolVersion, "h2");
     94  Assert.equal(buf, "response2");
     95 
     96  await server.stop();
     97 });