Bug 1675016 - Add more test for HTTP3 failure r=necko-reviewers,kershaw

- Add 2 test: 1) server is not listening to the port and 2) server is not responding that will cause the connection to timeout and fall back to HTTP2
 - Adds a server that only reads packets but never sends any to simulate a handshake timing out

Differential Revision: https://phabricator.services.mozilla.com/D95816
This commit is contained in:
Dragana Damjanovic 2020-11-11 13:32:24 +00:00
Родитель 46030d2607
Коммит 25db47d6a9
6 изменённых файлов: 286 добавлений и 15 удалений

Просмотреть файл

@ -297,6 +297,23 @@ impl HttpServer for Server {
}
}
#[derive(Default)]
struct NonRespondingServer {}
impl ::std::fmt::Display for NonRespondingServer {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "NonRespondingServer")
}
}
impl HttpServer for NonRespondingServer {
fn process(&mut self, _dgram: Option<Datagram>) -> Output {
Output::None
}
fn process_events(&mut self) {}
}
fn emit_packet(socket: &UdpSocket, out_dgram: Datagram) {
let sent = socket
.send_to(&out_dgram, &out_dgram.destination())
@ -361,6 +378,12 @@ fn read_dgram(
}
}
enum ServerType {
Http3,
Http3Fail,
Http3NoResponse,
}
struct ServersRunner {
hosts: Vec<SocketAddr>,
poll: Poll,
@ -385,19 +408,21 @@ impl ServersRunner {
}
pub fn init(&mut self) {
self.add_new_socket(0, true);
self.add_new_socket(1, false);
self.add_new_socket(0, ServerType::Http3);
self.add_new_socket(1, ServerType::Http3Fail);
self.add_new_socket(3, ServerType::Http3NoResponse);
println!(
"HTTP3 server listening on ports {} and {}",
"HTTP3 server listening on ports {}, {} and {}",
self.hosts[0].port(),
self.hosts[1].port()
self.hosts[1].port(),
self.hosts[2].port()
);
self.poll
.register(&self.timer, TIMER_TOKEN, Ready::readable(), PollOpt::edge())
.unwrap();
}
fn add_new_socket(&mut self, count: usize, http3: bool) -> u16 {
fn add_new_socket(&mut self, count: usize, server_type: ServerType) -> u16 {
let addr = "127.0.0.1:0".parse().unwrap();
let socket = match UdpSocket::bind(&addr) {
@ -429,17 +454,17 @@ impl ServersRunner {
self.sockets.push(socket);
self.servers
.insert(local_addr, (self.create_server(http3), None));
.insert(local_addr, (self.create_server(server_type), None));
local_addr.port()
}
fn create_server(&self, http3: bool) -> Box<dyn HttpServer> {
fn create_server(&self, server_type: ServerType) -> Box<dyn HttpServer> {
let anti_replay = AntiReplay::new(Instant::now(), Duration::from_secs(10), 7, 14)
.expect("unable to setup anti-replay");
let cid_mgr = Rc::new(RefCell::new(FixedConnectionIdManager::new(10)));
if http3 {
Box::new(Http3TestServer::new(
match server_type {
ServerType::Http3 => Box::new(Http3TestServer::new(
Http3Server::new(
Instant::now(),
&[" HTTP2 Test Cert"],
@ -453,9 +478,8 @@ impl ServersRunner {
},
)
.expect("We cannot make a server!"),
))
} else {
Box::new(
)),
ServerType::Http3Fail => Box::new(
Server::new(
Instant::now(),
&[" HTTP2 Test Cert"],
@ -465,7 +489,8 @@ impl ServersRunner {
cid_mgr,
)
.expect("We cannot make a server!"),
)
),
ServerType::Http3NoResponse => Box::new(NonRespondingServer::default()),
}
}

Просмотреть файл

@ -0,0 +1,121 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
let httpsUri;
registerCleanupFunction(async () => {
Services.prefs.clearUserPref("network.http.http3.enabled");
Services.prefs.clearUserPref("network.dns.localDomains");
Services.prefs.clearUserPref("network.dns.disableIPv6");
Services.prefs.clearUserPref(
"network.http.http3.alt-svc-mapping-for-testing"
);
dump("cleanup done\n");
});
function chanPromise(chan, listener) {
return new Promise(resolve => {
function finish() {
resolve();
}
listener.finish = finish;
chan.asyncOpen(listener);
});
}
function makeChan() {
let chan = NetUtil.newChannel({
uri: httpsUri,
loadUsingSystemPrincipal: true,
}).QueryInterface(Ci.nsIHttpChannel);
chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
return chan;
}
add_task(async function test_setup() {
let env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
let h2Port = env.get("MOZHTTP2_PORT");
Assert.notEqual(h2Port, null);
let h3Port = env.get("MOZHTTP3_PORT_NO_RESPONSE");
Assert.notEqual(h3Port, null);
Assert.notEqual(h3Port, "");
Services.prefs.setBoolPref("network.http.http3.enabled", true);
Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
Services.prefs.setBoolPref("network.dns.disableIPv6", true);
// Set AltSvc to point to not existing HTTP3 server on port 443
Services.prefs.setCharPref(
"network.http.http3.alt-svc-mapping-for-testing",
"foo.example.com;h3-27=:" + h3Port
);
let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
Ci.nsIX509CertDB
);
addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
httpsUri = "https://foo.example.com:" + h2Port + "/";
});
add_task(async function test_fatal_stream_error() {
let result = 1;
// We need to loop here because we need to wait for AltSvc storage to
// to be started.
// We also do not have a way to verify that HTTP3 has been tried, because
// the fallback is automatic, so try a couple of times.
do {
// We need to close HTTP2 connections, otherwise our connection pooling
// will dispatch the request over already existing HTTP2 connection.
Services.obs.notifyObservers(null, "net:prune-all-connections");
let chan = makeChan();
let listener = new CheckOnlyHttp2Listener();
await altsvcSetupPromise(chan, listener);
result++;
} while (result < 5);
});
let CheckOnlyHttp2Listener = function() {};
CheckOnlyHttp2Listener.prototype = {
onStartRequest: function testOnStartRequest(request) {},
onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
read_stream(stream, cnt);
},
onStopRequest: function testOnStopRequest(request, status) {
Assert.equal(status, Cr.NS_OK);
let httpVersion = "";
try {
httpVersion = request.protocolVersion;
} catch (e) {}
Assert.equal(httpVersion, "h2");
let routed = "NA";
try {
routed = request.getRequestHeader("Alt-Used");
} catch (e) {}
dump("routed is " + routed + "\n");
Assert.ok(routed === "0" || routed === "NA");
this.finish();
},
};
add_task(async function test_no_http3_after_error() {
let chan = makeChan();
let listener = new CheckOnlyHttp2Listener();
await altsvcSetupPromise(chan, listener);
});
// also after all connections are closed.
add_task(async function test_no_http3_after_error2() {
Services.obs.notifyObservers(null, "net:prune-all-connections");
let chan = makeChan();
let listener = new CheckOnlyHttp2Listener();
await altsvcSetupPromise(chan, listener);
});

Просмотреть файл

@ -81,7 +81,6 @@ add_task(async function test_fatal_error() {
let h3Port = env.get("MOZHTTP3_PORT_FAILED");
Assert.notEqual(h3Port, null);
Assert.notEqual(h3Port, "");
let h3AltSvc = ":" + h3Port;
Services.prefs.setBoolPref("network.http.http3.enabled", true);
Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");

Просмотреть файл

@ -0,0 +1,121 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
let httpsUri;
registerCleanupFunction(async () => {
Services.prefs.clearUserPref("network.http.http3.enabled");
Services.prefs.clearUserPref("network.dns.localDomains");
Services.prefs.clearUserPref("network.dns.disableIPv6");
Services.prefs.clearUserPref(
"network.http.http3.alt-svc-mapping-for-testing"
);
dump("cleanup done\n");
});
function chanPromise(chan, listener) {
return new Promise(resolve => {
function finish() {
resolve();
}
listener.finish = finish;
chan.asyncOpen(listener);
});
}
function makeChan() {
let chan = NetUtil.newChannel({
uri: httpsUri,
loadUsingSystemPrincipal: true,
}).QueryInterface(Ci.nsIHttpChannel);
chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
return chan;
}
function altsvcSetupPromise(chan, listener) {
return new Promise(resolve => {
function finish(result) {
resolve(result);
}
listener.finish = finish;
chan.asyncOpen(listener);
});
}
add_task(async function test_fatal_error() {
let env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
let h2Port = env.get("MOZHTTP2_PORT");
Assert.notEqual(h2Port, null);
Services.prefs.setBoolPref("network.http.http3.enabled", true);
Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
Services.prefs.setBoolPref("network.dns.disableIPv6", true);
// Set AltSvc to point to not existing HTTP3 server on port 443
Services.prefs.setCharPref(
"network.http.http3.alt-svc-mapping-for-testing",
"foo.example.com;h3-27=:443"
);
let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
Ci.nsIX509CertDB
);
addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
httpsUri = "https://foo.example.com:" + h2Port + "/";
});
add_task(async function test_fatal_stream_error() {
let result = 1;
// We need to loop here because we need to wait for AltSvc storage to
// to be started.
// We also do not have a way to verify that HTTP3 has been tried, because
// the fallback is automatic, so try a couple of times.
do {
// We need to close HTTP2 connections, otherwise our connection pooling
// will dispatch the request over already existing HTTP2 connection.
Services.obs.notifyObservers(null, "net:prune-all-connections");
let chan = makeChan();
let listener = new CheckOnlyHttp2Listener();
await altsvcSetupPromise(chan, listener);
result++;
} while (result < 5);
});
let CheckOnlyHttp2Listener = function() {};
CheckOnlyHttp2Listener.prototype = {
onStartRequest: function testOnStartRequest(request) {},
onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
read_stream(stream, cnt);
},
onStopRequest: function testOnStopRequest(request, status) {
Assert.equal(status, Cr.NS_OK);
let httpVersion = "";
try {
httpVersion = request.protocolVersion;
} catch (e) {}
Assert.equal(httpVersion, "h2");
this.finish();
},
};
add_task(async function test_no_http3_after_error() {
let chan = makeChan();
let listener = new CheckOnlyHttp2Listener();
await altsvcSetupPromise(chan, listener);
});
// also after all connections are closed.
add_task(async function test_no_http3_after_error2() {
Services.obs.notifyObservers(null, "net:prune-all-connections");
let chan = makeChan();
let listener = new CheckOnlyHttp2Listener();
await altsvcSetupPromise(chan, listener);
});

Просмотреть файл

@ -479,5 +479,9 @@ skip-if = asan || tsan || os == 'win' || os =='android'
skip-if = asan || tsan || os == 'win' || os =='android'
[test_http3_large_post.js]
skip-if = asan || tsan || os == 'win' || os =='android'
[test_http3_error_before_connect.js]
skip-if = asan || tsan || os == 'win' || os =='android'
[test_http3_server_not_existing.js]
skip-if = asan || tsan || os == 'win' || os =='android'
[test_cookie_ipv6.js]

Просмотреть файл

@ -1412,11 +1412,12 @@ class XPCShellTests(object):
msg = process.stdout.readline()
if "server listening" in msg:
searchObj = re.search(
r"HTTP3 server listening on ports ([0-9]+) and ([0-9]+)", msg, 0
r"HTTP3 server listening on ports ([0-9]+), ([0-9]+) and ([0-9]+)", msg, 0
)
if searchObj:
self.env["MOZHTTP3_PORT"] = searchObj.group(1)
self.env["MOZHTTP3_PORT_FAILED"] = searchObj.group(2)
self.env["MOZHTTP3_PORT_NO_RESPONSE"] = searchObj.group(3)
except OSError as e:
# This occurs if the subprocess couldn't be started
self.log.error("Could not run the http3 server: %s" % (str(e)))