tor-browser

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

test_http3_421.js (4515B)


      1 "use strict";
      2 
      3 let h3Route;
      4 let httpsOrigin;
      5 let h3AltSvc;
      6 let prefs;
      7 
      8 let tests = [test_https_alt_svc, test_response_421, testsDone];
      9 let current_test = 0;
     10 
     11 function run_next_test() {
     12  if (current_test < tests.length) {
     13    dump("starting test number " + current_test + "\n");
     14    tests[current_test]();
     15    current_test++;
     16  }
     17 }
     18 
     19 function run_test() {
     20  let h2Port = Services.env.get("MOZHTTP2_PORT");
     21  Assert.notEqual(h2Port, null);
     22  Assert.notEqual(h2Port, "");
     23  let h3Port = Services.env.get("MOZHTTP3_PORT");
     24  Assert.notEqual(h3Port, null);
     25  Assert.notEqual(h3Port, "");
     26  h3AltSvc = ":" + h3Port;
     27 
     28  h3Route = "foo.example.com:" + h3Port;
     29  do_get_profile();
     30  prefs = Services.prefs;
     31 
     32  prefs.setBoolPref("network.http.http3.enable", true);
     33  prefs.setCharPref("network.dns.localDomains", "foo.example.com");
     34  // We always resolve elements of localDomains as it's hardcoded without the
     35  // following pref:
     36  prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
     37 
     38  // The certificate for the http3server server is for foo.example.com and
     39  // is signed by http2-ca.pem so add that cert to the trust list as a
     40  // signing cert.
     41  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
     42    Ci.nsIX509CertDB
     43  );
     44  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
     45  httpsOrigin = "https://foo.example.com:" + h2Port + "/";
     46 
     47  run_next_test();
     48 }
     49 
     50 function makeChan(uri) {
     51  let chan = NetUtil.newChannel({
     52    uri,
     53    loadUsingSystemPrincipal: true,
     54  }).QueryInterface(Ci.nsIHttpChannel);
     55  chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
     56  return chan;
     57 }
     58 
     59 let Http3Listener = function () {};
     60 
     61 Http3Listener.prototype = {
     62  onDataAvailableFired: false,
     63  buffer: "",
     64  routed: "",
     65  httpVersion: "",
     66 
     67  onStartRequest: function testOnStartRequest(request) {
     68    Assert.ok(request instanceof Ci.nsIHttpChannel);
     69    Assert.equal(request.status, Cr.NS_OK);
     70    Assert.equal(request.responseStatus, 200);
     71  },
     72 
     73  onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
     74    this.onDataAvailableFired = true;
     75    this.buffer = this.buffer.concat(read_stream(stream, cnt));
     76  },
     77 
     78  onStopRequest: function testOnStopRequest(request, status) {
     79    Assert.equal(status, Cr.NS_OK);
     80    Assert.equal(this.onDataAvailableFired, true);
     81    this.routed = "NA";
     82    try {
     83      this.routed = request.getRequestHeader("Alt-Used");
     84    } catch (e) {}
     85    dump("routed is " + this.routed + "\n");
     86 
     87    this.httpVersion = "";
     88    try {
     89      this.httpVersion = request.protocolVersion;
     90    } catch (e) {}
     91    dump("httpVersion is " + this.httpVersion + "\n");
     92  },
     93 };
     94 
     95 let WaitForHttp3Listener = function () {};
     96 
     97 WaitForHttp3Listener.prototype = new Http3Listener();
     98 
     99 WaitForHttp3Listener.prototype.uri = "";
    100 
    101 WaitForHttp3Listener.prototype.onStopRequest = function testOnStopRequest(
    102  request,
    103  status
    104 ) {
    105  Http3Listener.prototype.onStopRequest.call(this, request, status);
    106 
    107  if (this.routed == h3Route) {
    108    Assert.equal(this.httpVersion, "h3");
    109    run_next_test();
    110  } else {
    111    dump("poll later for alt svc mapping\n");
    112    do_test_pending();
    113    do_timeout(500, () => {
    114      doTest(this.uri);
    115    });
    116  }
    117 
    118  do_test_finished();
    119 };
    120 
    121 function doTest(uri) {
    122  let chan = makeChan(uri);
    123  let listener = new WaitForHttp3Listener();
    124  listener.uri = uri;
    125  chan.setRequestHeader("x-altsvc", h3AltSvc, false);
    126  chan.asyncOpen(listener);
    127 }
    128 
    129 // Test Alt-Svc for http3.
    130 // H2 server returns alt-svc=h3=:h3port
    131 function test_https_alt_svc() {
    132  dump("test_https_alt_svc()\n");
    133 
    134  do_test_pending();
    135  doTest(httpsOrigin + "http3-test");
    136 }
    137 
    138 let Resp421Listener = function () {};
    139 
    140 Resp421Listener.prototype = new Http3Listener();
    141 
    142 Resp421Listener.prototype.onStopRequest = function testOnStopRequest(
    143  request,
    144  status
    145 ) {
    146  Http3Listener.prototype.onStopRequest.call(this, request, status);
    147 
    148  Assert.equal(this.routed, "0");
    149  Assert.equal(this.httpVersion, "h2");
    150  Assert.ok(this.buffer.match("You Win! [(]by requesting/Response421[)]"));
    151 
    152  run_next_test();
    153  do_test_finished();
    154 };
    155 
    156 function test_response_421() {
    157  dump("test_response_421()\n");
    158 
    159  let listener = new Resp421Listener();
    160  let chan = makeChan(httpsOrigin + "Response421");
    161  chan.asyncOpen(listener);
    162  do_test_pending();
    163 }
    164 
    165 function testsDone() {
    166  prefs.clearUserPref("network.http.http3.enable");
    167  prefs.clearUserPref("network.dns.localDomains");
    168  prefs.clearUserPref("network.proxy.allow_hijacking_localhost");
    169  dump("testDone\n");
    170  do_test_pending();
    171  do_test_finished();
    172 }