Bug 1796551 [wpt PR 36570] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / netwerk / test / unit / test_http3_alt_svc.js
blobcb3e186961beacf8feebba4f8463663cd916de6d
1 "use strict";
3 let httpsOrigin;
4 let h3AltSvc;
5 let h3Route;
6 let prefs;
8 let tests = [test_https_alt_svc, testsDone];
10 let current_test = 0;
12 function run_next_test() {
13   if (current_test < tests.length) {
14     dump("starting test number " + current_test + "\n");
15     tests[current_test]();
16     current_test++;
17   }
20 function run_test() {
21   let env = Cc["@mozilla.org/process/environment;1"].getService(
22     Ci.nsIEnvironment
23   );
24   let h2Port = env.get("MOZHTTP2_PORT");
25   Assert.notEqual(h2Port, null);
26   Assert.notEqual(h2Port, "");
27   let h3Port = env.get("MOZHTTP3_PORT");
28   Assert.notEqual(h3Port, null);
29   Assert.notEqual(h3Port, "");
30   h3AltSvc = ":" + h3Port;
32   h3Route = "foo.example.com:" + h3Port;
33   do_get_profile();
34   prefs = Services.prefs;
36   prefs.setBoolPref("network.http.http3.enable", true);
37   prefs.setCharPref("network.dns.localDomains", "foo.example.com");
38   // We always resolve elements of localDomains as it's hardcoded without the
39   // following pref:
40   prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
42   // The certificate for the http3server server is for foo.example.com and
43   // is signed by http2-ca.pem so add that cert to the trust list as a
44   // signing cert.
45   let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
46     Ci.nsIX509CertDB
47   );
48   addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
49   httpsOrigin = "https://foo.example.com:" + h2Port + "/";
51   run_next_test();
54 function createPrincipal(url) {
55   var ssm = Services.scriptSecurityManager;
56   try {
57     return ssm.createContentPrincipal(Services.io.newURI(url), {});
58   } catch (e) {
59     return null;
60   }
63 function makeChan(uri) {
64   let chan = NetUtil.newChannel({
65     uri,
66     loadingPrincipal: createPrincipal(uri),
67     securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT,
68     contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
69   }).QueryInterface(Ci.nsIHttpChannel);
70   chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
71   return chan;
74 let WaitForHttp3Listener = function() {};
76 WaitForHttp3Listener.prototype = {
77   onDataAvailableFired: false,
78   expectedRoute: "",
80   onStartRequest: function testOnStartRequest(request) {
81     Assert.ok(request instanceof Ci.nsIHttpChannel);
82     Assert.equal(request.responseStatus, 200);
83   },
85   onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
86     this.onDataAvailableFired = true;
87     read_stream(stream, cnt);
88   },
90   onStopRequest: function testOnStopRequest(request, status) {
91     let routed = "NA";
92     try {
93       routed = request.getRequestHeader("Alt-Used");
94     } catch (e) {}
95     dump("routed is " + routed + "\n");
97     if (routed == this.expectedRoute) {
98       let httpVersion = "";
99       try {
100         httpVersion = request.protocolVersion;
101       } catch (e) {}
102       Assert.equal(httpVersion, "h3-29");
103       run_next_test();
104     } else {
105       dump("poll later for alt svc mapping\n");
106       do_test_pending();
107       do_timeout(500, () => {
108         doTest(this.uri, this.expectedRoute, this.h3AltSvc);
109       });
110     }
112     do_test_finished();
113   },
116 function doTest(uri, expectedRoute, altSvc) {
117   let chan = makeChan(uri);
118   let listener = new WaitForHttp3Listener();
119   listener.uri = uri;
120   listener.expectedRoute = expectedRoute;
121   listener.h3AltSvc = altSvc;
122   chan.setRequestHeader("x-altsvc", altSvc, false);
123   chan.asyncOpen(listener);
126 // Test Alt-Svc for http3.
127 // H2 server returns alt-svc=h2=foo2.example.com:8000,h3-29=:h3port,h3-30=foo2.example.com:8443
128 function test_https_alt_svc() {
129   dump("test_https_alt_svc()\n");
130   do_test_pending();
131   doTest(httpsOrigin + "http3-test2", h3Route, h3AltSvc);
134 function testsDone() {
135   prefs.clearUserPref("network.http.http3.enable");
136   prefs.clearUserPref("network.dns.localDomains");
137   prefs.clearUserPref("network.proxy.allow_hijacking_localhost");
138   dump("testDone\n");