tor-browser

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

test_http3_prio_helpers.js (3221B)


      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 // uses head_http3.js, which uses http2-ca.pem
      6 "use strict";
      7 
      8 /* exported inChildProcess, test_flag_priority */
      9 function inChildProcess() {
     10  return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
     11 }
     12 
     13 let Http3Listener = function (
     14  closure,
     15  expected_priority,
     16  expected_incremental,
     17  context
     18 ) {
     19  this._closure = closure;
     20  this._expected_priority = expected_priority;
     21  this._expected_incremental = expected_incremental;
     22  this._context = context;
     23 };
     24 
     25 // string -> [string, bool]
     26 // "u=3,i" -> ["u=3", true]
     27 function parse_priority_response_header(priority) {
     28  const priority_array = priority.split(",");
     29 
     30  // parse for urgency string
     31  const urgency = priority_array.find(element => element.includes("u="));
     32 
     33  // parse for incremental bool
     34  const incremental = !!priority_array.find(element => element == "i");
     35 
     36  return [urgency ? urgency : null, incremental];
     37 }
     38 
     39 Http3Listener.prototype = {
     40  resumed: false,
     41 
     42  onStartRequest: function testOnStartRequest(request) {
     43    Assert.equal(request.status, Cr.NS_OK);
     44    Assert.equal(request.responseStatus, 200);
     45 
     46    let secinfo = request.securityInfo;
     47    Assert.equal(secinfo.resumed, this.resumed);
     48    Assert.notEqual(secinfo.serverCert, null);
     49 
     50    // check priority urgency and incremental from response header
     51    let priority_urgency = null;
     52    let incremental = null;
     53    try {
     54      const prh = request.getResponseHeader("priority-mirror");
     55      [priority_urgency, incremental] = parse_priority_response_header(prh);
     56    } catch (e) {
     57      console.log("Failed to get priority-mirror from response header");
     58    }
     59    Assert.equal(priority_urgency, this._expected_priority, this._context);
     60    Assert.equal(incremental, this._expected_incremental, this._context);
     61  },
     62 
     63  onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
     64    read_stream(stream, cnt);
     65  },
     66 
     67  onStopRequest: function testOnStopRequest(request) {
     68    let httpVersion = "";
     69    try {
     70      httpVersion = request.protocolVersion;
     71    } catch (e) {}
     72    Assert.equal(httpVersion, "h3");
     73 
     74    try {
     75      this._closure();
     76    } catch (ex) {
     77      do_throw("Error in closure function: " + ex);
     78    }
     79  },
     80 };
     81 
     82 function make_channel(url) {
     83  var request = NetUtil.newChannel({
     84    uri: url,
     85    loadUsingSystemPrincipal: true,
     86  });
     87  request.QueryInterface(Ci.nsIHttpChannel);
     88  return request;
     89 }
     90 
     91 async function test_flag_priority(
     92  context,
     93  flag,
     94  expected_priority,
     95  incremental,
     96  expected_incremental
     97 ) {
     98  var chan = make_channel("https://foo.example.com/priority_mirror");
     99  var cos = chan.QueryInterface(Ci.nsIClassOfService);
    100 
    101  // configure the channel with flags
    102  if (flag != null) {
    103    cos.addClassFlags(flag);
    104  }
    105 
    106  // configure the channel with incremental
    107  if (incremental != null) {
    108    cos.incremental = incremental;
    109  }
    110 
    111  await new Promise(resolve =>
    112    chan.asyncOpen(
    113      new Http3Listener(
    114        resolve,
    115        expected_priority,
    116        expected_incremental,
    117        context
    118      )
    119    )
    120  );
    121 }