test_coaleasing_h2_and_h3_connection.js (3627B)
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 "use strict"; 6 7 var { setTimeout } = ChromeUtils.importESModule( 8 "resource://gre/modules/Timer.sys.mjs" 9 ); 10 11 const { NodeHTTPSServer } = ChromeUtils.importESModule( 12 "resource://testing-common/NodeServer.sys.mjs" 13 ); 14 15 let h2Port; 16 let h3Port; 17 18 add_setup(async function setup() { 19 h2Port = Services.env.get("MOZHTTP2_PORT"); 20 Assert.notEqual(h2Port, null); 21 Assert.notEqual(h2Port, ""); 22 23 h3Port = Services.env.get("MOZHTTP3_PORT"); 24 Assert.notEqual(h3Port, null); 25 Assert.notEqual(h3Port, ""); 26 27 Services.prefs.setBoolPref("network.http.http3.enable", true); 28 Services.prefs.setIntPref("network.http.speculative-parallel-limit", 6); 29 Services.prefs.setBoolPref("network.http.altsvc.oe", true); 30 31 // Set to allow the cert presented by our H2 server 32 do_get_profile(); 33 34 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService( 35 Ci.nsIX509CertDB 36 ); 37 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u"); 38 }); 39 40 registerCleanupFunction(async () => { 41 Services.prefs.clearUserPref("network.http.http3.enable"); 42 Services.prefs.clearUserPref("network.dns.localDomains"); 43 Services.prefs.clearUserPref("network.http.speculative-parallel-limit"); 44 Services.prefs.clearUserPref("network.http.altsvc.oe"); 45 }); 46 47 function makeChan(url) { 48 let chan = NetUtil.newChannel({ 49 uri: url, 50 loadUsingSystemPrincipal: true, 51 contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 52 }).QueryInterface(Ci.nsIHttpChannel); 53 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI; 54 return chan; 55 } 56 57 function channelOpenPromise(chan, flags) { 58 return new Promise(resolve => { 59 function finish(req, buffer) { 60 resolve([req, buffer]); 61 } 62 chan.asyncOpen(new ChannelListener(finish, null, flags)); 63 }); 64 } 65 66 add_task(async function testNotCoaleasingH2Connection() { 67 const host = "foo.example.com"; 68 Services.prefs.setCharPref("network.dns.localDomains", host); 69 70 let server = new NodeHTTPSServer(); 71 await server.start(); 72 registerCleanupFunction(async () => { 73 await server.stop(); 74 }); 75 76 await server.execute(`global.h3Port = "${h3Port}";`); 77 await server.registerPathHandler("/altsvc", (req, resp) => { 78 const body = "done"; 79 resp.setHeader("Content-Length", body.length); 80 resp.setHeader("Alt-Svc", `h3=:${global.h3Port}`); 81 resp.writeHead(200); 82 resp.write(body); 83 resp.end(""); 84 }); 85 86 let chan = makeChan(`https://${host}:${server.port()}/altsvc`); 87 let [req] = await channelOpenPromise(chan); 88 Assert.equal(req.protocolVersion, "http/1.1"); 89 90 // Some delay to make sure the H3 speculative connection is created. 91 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout 92 await new Promise(resolve => setTimeout(resolve, 1000)); 93 94 // To clear the altsvc cache. 95 Services.obs.notifyObservers(null, "last-pb-context-exited"); 96 97 // Add another alt-svc header to route to moz-http2.js. 98 Services.prefs.setCharPref( 99 "network.http.http3.alt-svc-mapping-for-testing", 100 `${host};h2=:${h2Port}` 101 ); 102 103 let start = new Date().getTime(); 104 chan = makeChan(`https://${host}:${server.port()}/server-timing`); 105 chan.QueryInterface(Ci.nsIHttpChannelInternal).beConservative = true; 106 [req] = await channelOpenPromise(chan); 107 Assert.equal(req.protocolVersion, "h2"); 108 109 // The time this request takes should be way more less than the 110 // neqo idle timeout (30s). 111 let duration = (new Date().getTime() - start) / 1000; 112 Assert.less(duration, 10); 113 });