File: head_http3.js

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (149 lines) | stat: -rw-r--r-- 4,556 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* import-globals-from head_channels.js */
/* import-globals-from head_cookies.js */

const { HTTP3Server: HeadHTTP3Server } = ChromeUtils.importESModule(
  "resource://testing-common/NodeServer.sys.mjs"
);

const SERVER_PATH = Services.env.get("MOZ_HTTP3_SERVER_PATH");
const CERT_DB_PATH = Services.env.get("MOZ_HTTP3_CERT_DB_PATH");

async function with_http3_server(selectPort) {
  const server = new HeadHTTP3Server();
  const h3Port = await server.start(SERVER_PATH, CERT_DB_PATH);
  registerCleanupFunction(() => server.stop()); // returns a Promise; cleanup will await it
  return selectPort(server, h3Port);
}

async function create_h3_server() {
  return with_http3_server((_, h3Port) => h3Port);
}

async function create_masque_proxy_server() {
  return with_http3_server(server => ({
    masqueProxyPort: server.masque_proxy_port(),
    noResponsePort: server.no_response_port(),
  }));
}

async function http3_setup_tests(http3version, reload) {
  do_get_profile();

  let h3Port;
  let isAndroid = mozinfo.os == "android";
  // On Android, we don't have a way to start the server on the host on demand.
  if (reload && !isAndroid) {
    h3Port = await create_h3_server();
  } else {
    h3Port = Services.env.get("MOZHTTP3_PORT");
  }

  Assert.notEqual(h3Port, null);
  Assert.notEqual(h3Port, "");

  let h3Route = "foo.example.com:" + h3Port;

  Services.prefs.setBoolPref("network.http.http3.enable", true);
  if (isAndroid) {
    const overrideService = Cc[
      "@mozilla.org/network/native-dns-override;1"
    ].getService(Ci.nsINativeDNSResolverOverride);
    // To connect to the http3Server running on the host, we need to set this
    // special IP address.
    overrideService.addIPOverride("foo.example.com", "10.0.2.2");
  } else {
    Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
  }
  Services.prefs.setBoolPref("network.dns.disableIPv6", true);
  Services.prefs.setCharPref(
    "network.http.http3.alt-svc-mapping-for-testing",
    `foo.example.com;${http3version}=:${h3Port}`
  );

  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
    Ci.nsIX509CertDB
  );
  // `../unit/` so that unit_ipc tests can use as well
  addCertFromFile(certdb, "../unit/http2-ca.pem", "CTu,u,u");

  await setup_altsvc("https://foo.example.com/", h3Route, http3version);
}

function makeChan(uri) {
  let chan = NetUtil.newChannel({
    uri,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
  chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
  return chan;
}

let CheckHttp3Listener = function () {};

CheckHttp3Listener.prototype = {
  expectedRoute: "",
  http3version: "",

  onStartRequest: function testOnStartRequest() {},

  onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
    read_stream(stream, cnt);
  },

  onStopRequest: function testOnStopRequest(request) {
    let routed = "NA";
    try {
      routed = request.getRequestHeader("Alt-Used");
    } catch (e) {}
    dump("routed is " + routed + "\n");

    if (routed == this.expectedRoute) {
      let httpVersion = "";
      try {
        httpVersion = request.protocolVersion;
      } catch (e) {}
      Assert.equal(httpVersion, this.http3version);
      this.finish(true);
    } else {
      dump("try again to get alt svc mapping\n");
      this.finish(false);
    }
  },
};

async function setup_altsvc(uri, expectedRoute, http3version) {
  let result = false;
  do {
    let chan = makeChan(uri);
    let listener = new CheckHttp3Listener();
    listener.expectedRoute = expectedRoute;
    listener.http3version = http3version;
    result = await altsvcSetupPromise(chan, listener);
    dump("results=" + result);
  } while (result === false);
}

function altsvcSetupPromise(chan, listener) {
  return new Promise(resolve => {
    function finish(result) {
      resolve(result);
    }
    listener.finish = finish;
    chan.asyncOpen(listener);
  });
}

function http3_clear_prefs() {
  Services.prefs.clearUserPref("network.http.http3.enable");
  Services.prefs.clearUserPref("network.dns.localDomains");
  Services.prefs.clearUserPref("network.dns.disableIPv6");
  Services.prefs.clearUserPref(
    "network.http.http3.alt-svc-mapping-for-testing"
  );
  Services.prefs.clearUserPref("network.http.http3.support_version1");
  dump("cleanup done\n");
}