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 () => {
20 async
function http3_setup_tests(http3version
, reload
) {
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();
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);
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");
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(
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({
66 loadUsingSystemPrincipal
: true,
67 }).QueryInterface(Ci
.nsIHttpChannel
);
68 chan
.loadFlags
= Ci
.nsIChannel
.LOAD_INITIAL_DOCUMENT_URI
;
72 let CheckHttp3Listener = function () {};
74 CheckHttp3Listener
.prototype = {
78 onStartRequest
: function testOnStartRequest() {},
80 onDataAvailable
: function testOnDataAvailable(request
, stream
, off
, cnt
) {
81 read_stream(stream
, cnt
);
84 onStopRequest
: function testOnStopRequest(request
) {
87 routed
= request
.getRequestHeader("Alt-Used");
89 dump("routed is " + routed
+ "\n");
91 if (routed
== this.expectedRoute
) {
94 httpVersion
= request
.protocolVersion
;
96 Assert
.equal(httpVersion
, this.http3version
);
99 dump("try again to get alt svc mapping\n");
105 async
function setup_altsvc(uri
, expectedRoute
, http3version
) {
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
) {
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");