tor-browser

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

test_http3_error_before_connect.js (3659B)


      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 "use strict";
      5 
      6 let httpsUri;
      7 
      8 registerCleanupFunction(async () => {
      9  Services.prefs.clearUserPref("network.http.http3.enable");
     10  Services.prefs.clearUserPref("network.dns.localDomains");
     11  Services.prefs.clearUserPref("network.dns.disableIPv6");
     12  Services.prefs.clearUserPref(
     13    "network.http.http3.alt-svc-mapping-for-testing"
     14  );
     15  Services.prefs.clearUserPref("network.http.http3.backup_timer_delay");
     16  dump("cleanup done\n");
     17 });
     18 
     19 function makeChan() {
     20  let chan = NetUtil.newChannel({
     21    uri: httpsUri,
     22    loadUsingSystemPrincipal: true,
     23  }).QueryInterface(Ci.nsIHttpChannel);
     24  chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
     25  return chan;
     26 }
     27 
     28 add_task(async function test_setup() {
     29  let h2Port = Services.env.get("MOZHTTP2_PORT");
     30  Assert.notEqual(h2Port, null);
     31  let h3Port = Services.env.get("MOZHTTP3_PORT_NO_RESPONSE");
     32  Assert.notEqual(h3Port, null);
     33  Assert.notEqual(h3Port, "");
     34 
     35  Services.prefs.setBoolPref("network.http.http3.enable", true);
     36  Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
     37  Services.prefs.setBoolPref("network.dns.disableIPv6", true);
     38  // Set AltSvc to point to not existing HTTP3 server on port 443
     39  Services.prefs.setCharPref(
     40    "network.http.http3.alt-svc-mapping-for-testing",
     41    "foo.example.com;h3=:" + h3Port
     42  );
     43  Services.prefs.setIntPref("network.http.http3.backup_timer_delay", 0);
     44 
     45  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
     46    Ci.nsIX509CertDB
     47  );
     48  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
     49 
     50  httpsUri = "https://foo.example.com:" + h2Port + "/";
     51 });
     52 
     53 add_task(async function test_fatal_stream_error() {
     54  let result = 1;
     55  // We need to loop here because we need to wait for AltSvc storage to
     56  // to be started.
     57  // We also do not have a way to verify that HTTP3 has been tried, because
     58  // the fallback is automatic, so try a couple of times.
     59  do {
     60    // We need to close HTTP2 connections, otherwise our connection pooling
     61    // will dispatch the request over already existing HTTP2 connection.
     62    Services.obs.notifyObservers(null, "net:prune-all-connections");
     63    let chan = makeChan();
     64    let listener = new CheckOnlyHttp2Listener();
     65    await altsvcSetupPromise(chan, listener);
     66    result++;
     67  } while (result < 5);
     68 });
     69 
     70 let CheckOnlyHttp2Listener = function () {};
     71 
     72 CheckOnlyHttp2Listener.prototype = {
     73  onStartRequest: function testOnStartRequest() {},
     74 
     75  onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
     76    read_stream(stream, cnt);
     77  },
     78 
     79  onStopRequest: function testOnStopRequest(request, status) {
     80    Assert.equal(status, Cr.NS_OK);
     81    let httpVersion = "";
     82    try {
     83      httpVersion = request.protocolVersion;
     84    } catch (e) {}
     85    Assert.equal(httpVersion, "h2");
     86 
     87    let routed = "NA";
     88    try {
     89      routed = request.getRequestHeader("Alt-Used");
     90    } catch (e) {}
     91    dump("routed is " + routed + "\n");
     92    Assert.ok(routed === "0" || routed === "NA");
     93    this.finish();
     94  },
     95 };
     96 
     97 add_task(async function test_no_http3_after_error() {
     98  let chan = makeChan();
     99  let listener = new CheckOnlyHttp2Listener();
    100  await altsvcSetupPromise(chan, listener);
    101 });
    102 
    103 // also after all connections are closed.
    104 add_task(async function test_no_http3_after_error2() {
    105  Services.obs.notifyObservers(null, "net:prune-all-connections");
    106  let chan = makeChan();
    107  let listener = new CheckOnlyHttp2Listener();
    108  await altsvcSetupPromise(chan, listener);
    109 });