head_http3.js (4556B)
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 /* import-globals-from head_channels.js */ 6 /* import-globals-from head_cookies.js */ 7 8 const { HTTP3Server: HeadHTTP3Server } = ChromeUtils.importESModule( 9 "resource://testing-common/NodeServer.sys.mjs" 10 ); 11 12 const SERVER_PATH = Services.env.get("MOZ_HTTP3_SERVER_PATH"); 13 const CERT_DB_PATH = Services.env.get("MOZ_HTTP3_CERT_DB_PATH"); 14 15 async function with_http3_server(selectPort) { 16 const server = new HeadHTTP3Server(); 17 const h3Port = await server.start(SERVER_PATH, CERT_DB_PATH); 18 registerCleanupFunction(() => server.stop()); // returns a Promise; cleanup will await it 19 return selectPort(server, h3Port); 20 } 21 22 async function create_h3_server() { 23 return with_http3_server((_, h3Port) => h3Port); 24 } 25 26 async function create_masque_proxy_server() { 27 return with_http3_server(server => ({ 28 masqueProxyPort: server.masque_proxy_port(), 29 noResponsePort: server.no_response_port(), 30 })); 31 } 32 33 async function http3_setup_tests(http3version, reload) { 34 do_get_profile(); 35 36 let h3Port; 37 let isAndroid = mozinfo.os == "android"; 38 // On Android, we don't have a way to start the server on the host on demand. 39 if (reload && !isAndroid) { 40 h3Port = await create_h3_server(); 41 } else { 42 h3Port = Services.env.get("MOZHTTP3_PORT"); 43 } 44 45 Assert.notEqual(h3Port, null); 46 Assert.notEqual(h3Port, ""); 47 48 let h3Route = "foo.example.com:" + h3Port; 49 50 Services.prefs.setBoolPref("network.http.http3.enable", true); 51 if (isAndroid) { 52 const overrideService = Cc[ 53 "@mozilla.org/network/native-dns-override;1" 54 ].getService(Ci.nsINativeDNSResolverOverride); 55 // To connect to the http3Server running on the host, we need to set this 56 // special IP address. 57 overrideService.addIPOverride("foo.example.com", "10.0.2.2"); 58 } else { 59 Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com"); 60 } 61 Services.prefs.setBoolPref("network.dns.disableIPv6", true); 62 Services.prefs.setCharPref( 63 "network.http.http3.alt-svc-mapping-for-testing", 64 `foo.example.com;${http3version}=:${h3Port}` 65 ); 66 67 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService( 68 Ci.nsIX509CertDB 69 ); 70 // `../unit/` so that unit_ipc tests can use as well 71 addCertFromFile(certdb, "../unit/http2-ca.pem", "CTu,u,u"); 72 73 await setup_altsvc("https://foo.example.com/", h3Route, http3version); 74 } 75 76 function makeChan(uri) { 77 let chan = NetUtil.newChannel({ 78 uri, 79 loadUsingSystemPrincipal: true, 80 }).QueryInterface(Ci.nsIHttpChannel); 81 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI; 82 return chan; 83 } 84 85 let CheckHttp3Listener = function () {}; 86 87 CheckHttp3Listener.prototype = { 88 expectedRoute: "", 89 http3version: "", 90 91 onStartRequest: function testOnStartRequest() {}, 92 93 onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) { 94 read_stream(stream, cnt); 95 }, 96 97 onStopRequest: function testOnStopRequest(request) { 98 let routed = "NA"; 99 try { 100 routed = request.getRequestHeader("Alt-Used"); 101 } catch (e) {} 102 dump("routed is " + routed + "\n"); 103 104 if (routed == this.expectedRoute) { 105 let httpVersion = ""; 106 try { 107 httpVersion = request.protocolVersion; 108 } catch (e) {} 109 Assert.equal(httpVersion, this.http3version); 110 this.finish(true); 111 } else { 112 dump("try again to get alt svc mapping\n"); 113 this.finish(false); 114 } 115 }, 116 }; 117 118 async function setup_altsvc(uri, expectedRoute, http3version) { 119 let result = false; 120 do { 121 let chan = makeChan(uri); 122 let listener = new CheckHttp3Listener(); 123 listener.expectedRoute = expectedRoute; 124 listener.http3version = http3version; 125 result = await altsvcSetupPromise(chan, listener); 126 dump("results=" + result); 127 } while (result === false); 128 } 129 130 function altsvcSetupPromise(chan, listener) { 131 return new Promise(resolve => { 132 function finish(result) { 133 resolve(result); 134 } 135 listener.finish = finish; 136 chan.asyncOpen(listener); 137 }); 138 } 139 140 function http3_clear_prefs() { 141 Services.prefs.clearUserPref("network.http.http3.enable"); 142 Services.prefs.clearUserPref("network.dns.localDomains"); 143 Services.prefs.clearUserPref("network.dns.disableIPv6"); 144 Services.prefs.clearUserPref( 145 "network.http.http3.alt-svc-mapping-for-testing" 146 ); 147 Services.prefs.clearUserPref("network.http.http3.support_version1"); 148 dump("cleanup done\n"); 149 }