Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / head_http3.js
blob469a131fa73ac64d890bbb4b6cf3b7910936f5f9
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/. */
5 /* import-globals-from head_channels.js */
6 /* import-globals-from head_cookies.js */
7 /* import-globals-from head_servers.js */
9 async function create_h3_server() {
10 let h3ServerPath = Services.env.get("MOZ_HTTP3_SERVER_PATH");
11 let h3DBPath = Services.env.get("MOZ_HTTP3_CERT_DB_PATH");
12 let server = new HTTP3Server();
13 let h3Port = await server.start(h3ServerPath, h3DBPath);
14 registerCleanupFunction(async () => {
15 await server.stop();
16 });
17 return h3Port;
20 async function http3_setup_tests(http3version, reload) {
21 do_get_profile();
23 let h3Port;
24 let isAndroid = mozinfo.os == "android";
25 // On Android, we don't have a way to start the server on the host on demand.
26 if (reload && !isAndroid) {
27 h3Port = await create_h3_server();
28 } else {
29 h3Port = Services.env.get("MOZHTTP3_PORT");
32 Assert.notEqual(h3Port, null);
33 Assert.notEqual(h3Port, "");
35 let h3Route = "foo.example.com:" + h3Port;
37 Services.prefs.setBoolPref("network.http.http3.enable", true);
38 if (isAndroid) {
39 const overrideService = Cc[
40 "@mozilla.org/network/native-dns-override;1"
41 ].getService(Ci.nsINativeDNSResolverOverride);
42 // To connect to the http3Server running on the host, we need to set this
43 // special IP address.
44 overrideService.addIPOverride("foo.example.com", "10.0.2.2");
45 } else {
46 Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
48 Services.prefs.setBoolPref("network.dns.disableIPv6", true);
49 Services.prefs.setCharPref(
50 "network.http.http3.alt-svc-mapping-for-testing",
51 `foo.example.com;${http3version}=:${h3Port}`
54 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
55 Ci.nsIX509CertDB
57 // `../unit/` so that unit_ipc tests can use as well
58 addCertFromFile(certdb, "../unit/http2-ca.pem", "CTu,u,u");
60 await setup_altsvc("https://foo.example.com/", h3Route, http3version);
63 function makeChan(uri) {
64 let chan = NetUtil.newChannel({
65 uri,
66 loadUsingSystemPrincipal: true,
67 }).QueryInterface(Ci.nsIHttpChannel);
68 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
69 return chan;
72 let CheckHttp3Listener = function () {};
74 CheckHttp3Listener.prototype = {
75 expectedRoute: "",
76 http3version: "",
78 onStartRequest: function testOnStartRequest() {},
80 onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
81 read_stream(stream, cnt);
84 onStopRequest: function testOnStopRequest(request) {
85 let routed = "NA";
86 try {
87 routed = request.getRequestHeader("Alt-Used");
88 } catch (e) {}
89 dump("routed is " + routed + "\n");
91 if (routed == this.expectedRoute) {
92 let httpVersion = "";
93 try {
94 httpVersion = request.protocolVersion;
95 } catch (e) {}
96 Assert.equal(httpVersion, this.http3version);
97 this.finish(true);
98 } else {
99 dump("try again to get alt svc mapping\n");
100 this.finish(false);
105 async function setup_altsvc(uri, expectedRoute, http3version) {
106 let result = false;
107 do {
108 let chan = makeChan(uri);
109 let listener = new CheckHttp3Listener();
110 listener.expectedRoute = expectedRoute;
111 listener.http3version = http3version;
112 result = await altsvcSetupPromise(chan, listener);
113 dump("results=" + result);
114 } while (result === false);
117 function altsvcSetupPromise(chan, listener) {
118 return new Promise(resolve => {
119 function finish(result) {
120 resolve(result);
122 listener.finish = finish;
123 chan.asyncOpen(listener);
127 function http3_clear_prefs() {
128 Services.prefs.clearUserPref("network.http.http3.enable");
129 Services.prefs.clearUserPref("network.dns.localDomains");
130 Services.prefs.clearUserPref("network.dns.disableIPv6");
131 Services.prefs.clearUserPref(
132 "network.http.http3.alt-svc-mapping-for-testing"
134 Services.prefs.clearUserPref("network.http.http3.support_version1");
135 dump("cleanup done\n");