test_http3_fatal_stream_error.js (4182B)
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 "use strict"; 5 6 let httpsUri; 7 8 registerCleanupFunction(async () => { 9 Services.prefs.clearUserPref("network.http.http3.enable"); 10 Services.prefs.clearUserPref("network.dns.localDomains"); 11 Services.prefs.clearUserPref("network.dns.disableIPv6"); 12 Services.prefs.clearUserPref( 13 "network.http.http3.alt-svc-mapping-for-testing" 14 ); 15 Services.prefs.clearUserPref("network.http.http3.backup_timer_delay"); 16 dump("cleanup done\n"); 17 }); 18 19 let Http3FailedListener = function () {}; 20 21 Http3FailedListener.prototype = { 22 onStartRequest: function testOnStartRequest() {}, 23 24 onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) { 25 this.amount += cnt; 26 read_stream(stream, cnt); 27 }, 28 29 onStopRequest: function testOnStopRequest(request, status) { 30 if (Components.isSuccessCode(status)) { 31 // This is still HTTP2 connection 32 let httpVersion = ""; 33 try { 34 httpVersion = request.protocolVersion; 35 } catch (e) {} 36 Assert.equal(httpVersion, "h2"); 37 this.finish(false); 38 } else { 39 Assert.equal(status, Cr.NS_ERROR_NET_PARTIAL_TRANSFER); 40 this.finish(true); 41 } 42 }, 43 }; 44 45 function makeChan() { 46 let chan = NetUtil.newChannel({ 47 uri: httpsUri, 48 loadUsingSystemPrincipal: true, 49 }).QueryInterface(Ci.nsIHttpChannel); 50 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI; 51 return chan; 52 } 53 54 function altsvcSetupPromise(chan, listener) { 55 return new Promise(resolve => { 56 function finish(result) { 57 resolve(result); 58 } 59 listener.finish = finish; 60 chan.asyncOpen(listener); 61 }); 62 } 63 64 add_task(async function test_fatal_error() { 65 let h2Port = Services.env.get("MOZHTTP2_PORT"); 66 Assert.notEqual(h2Port, null); 67 68 let h3Port = Services.env.get("MOZHTTP3_PORT_FAILED"); 69 Assert.notEqual(h3Port, null); 70 Assert.notEqual(h3Port, ""); 71 72 Services.prefs.setBoolPref("network.http.http3.enable", true); 73 Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com"); 74 Services.prefs.setBoolPref("network.dns.disableIPv6", true); 75 Services.prefs.setCharPref( 76 "network.http.http3.alt-svc-mapping-for-testing", 77 "foo.example.com;h3=:" + h3Port 78 ); 79 Services.prefs.setIntPref("network.http.http3.backup_timer_delay", 0); 80 81 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService( 82 Ci.nsIX509CertDB 83 ); 84 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u"); 85 86 httpsUri = "https://foo.example.com:" + h2Port + "/"; 87 }); 88 89 add_task(async function test_fatal_stream_error() { 90 let result = false; 91 // We need to loop here because we need to wait for AltSvc storage to 92 // to be started. 93 do { 94 // We need to close HTTP2 connections, otherwise our connection pooling 95 // will dispatch the request over already existing HTTP2 connection. 96 Services.obs.notifyObservers(null, "net:prune-all-connections"); 97 let chan = makeChan(); 98 let listener = new Http3FailedListener(); 99 result = await altsvcSetupPromise(chan, listener); 100 } while (result === false); 101 }); 102 103 let CheckOnlyHttp2Listener = function () {}; 104 105 CheckOnlyHttp2Listener.prototype = { 106 onStartRequest: function testOnStartRequest() {}, 107 108 onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) { 109 read_stream(stream, cnt); 110 }, 111 112 onStopRequest: function testOnStopRequest(request, status) { 113 Assert.equal(status, Cr.NS_OK); 114 let httpVersion = ""; 115 try { 116 httpVersion = request.protocolVersion; 117 } catch (e) {} 118 Assert.equal(httpVersion, "h2"); 119 this.finish(false); 120 }, 121 }; 122 123 add_task(async function test_no_http3_after_error() { 124 let chan = makeChan(); 125 let listener = new CheckOnlyHttp2Listener(); 126 await altsvcSetupPromise(chan, listener); 127 }); 128 129 // also after all connections are closed. 130 add_task(async function test_no_http3_after_error2() { 131 Services.obs.notifyObservers(null, "net:prune-all-connections"); 132 let chan = makeChan(); 133 let listener = new CheckOnlyHttp2Listener(); 134 await altsvcSetupPromise(chan, listener); 135 });