Bug 1552176 - Unit test for TRRMode for individual channels r=dragana

Differential Revision: https://phabricator.services.mozilla.com/D48362

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Valentin Gosu 2019-11-02 20:42:25 +00:00
Родитель 27252b4558
Коммит 236b69b1c0
1 изменённых файлов: 126 добавлений и 10 удалений

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

@ -1,6 +1,6 @@
"use strict";
const { NodeServer } = ChromeUtils.import("resource://testing-common/httpd.js");
const { NodeServer, HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
const dns = Cc["@mozilla.org/network/dns-service;1"].getService(
Ci.nsIDNSService
);
@ -144,7 +144,6 @@ class DNSListener {
return this.promise.then.apply(this.promise, arguments);
}
}
add_task(async function test0_nodeExecute() {
// This test checks that moz-http2.js running in node is working.
// This should always be the first test in this file (except for setup)
@ -156,6 +155,126 @@ add_task(async function test0_nodeExecute() {
);
});
function makeChan(url, mode) {
let chan = NetUtil.newChannel({
uri: url,
loadUsingSystemPrincipal: true,
}).QueryInterface(Ci.nsIHttpChannel);
chan.setTRRMode(mode);
return chan;
}
add_task(async function test_trr_flags() {
let httpserv = new HttpServer();
httpserv.registerPathHandler("/", function handler(metadata, response) {
let content = "ok";
response.setHeader("Content-Length", `${content.length}`);
response.bodyOutputStream.write(content, content.length);
});
httpserv.start(-1);
const URL = `http://example.com:${httpserv.identity.primaryPort}/`;
dns.clearCache(true);
Services.prefs.setCharPref(
"network.trr.uri",
`https://localhost:${h2Port}/doh?responseIP=127.9.0.9`
);
Services.prefs.setIntPref("network.trr.mode", 0);
dns.clearCache(true);
let chan = makeChan(URL, Ci.nsIRequest.TRR_DEFAULT_MODE);
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
equal(chan.getTRRMode(), Ci.nsIRequest.TRR_DEFAULT_MODE);
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_DISABLED_MODE);
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
equal(chan.getTRRMode(), Ci.nsIRequest.TRR_DISABLED_MODE);
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_FIRST_MODE);
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
equal(chan.getTRRMode(), Ci.nsIRequest.TRR_FIRST_MODE);
dns.clearCache(true);
chan = makeChan(
`http://example.com:${httpserv.identity.primaryPort}/`,
Ci.nsIRequest.TRR_ONLY_MODE
);
// Should fail as it tries to connect to local but unavailable IP
await new Promise(resolve =>
chan.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE))
);
equal(chan.getTRRMode(), Ci.nsIRequest.TRR_ONLY_MODE);
dns.clearCache(true);
Services.prefs.setCharPref(
"network.trr.uri",
`https://localhost:${h2Port}/doh?responseIP=127.9.2.9`
);
Services.prefs.setIntPref("network.trr.mode", 2);
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_DEFAULT_MODE);
// Does get the IP from TRR, but failure means it falls back to DNS.
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_DISABLED_MODE);
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
dns.clearCache(true);
// Does get the IP from TRR, but failure means it falls back to DNS.
chan = makeChan(URL, Ci.nsIRequest.TRR_FIRST_MODE);
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_ONLY_MODE);
await new Promise(resolve =>
chan.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE))
);
dns.clearCache(true);
Services.prefs.setCharPref(
"network.trr.uri",
`https://localhost:${h2Port}/doh?responseIP=127.9.3.9`
);
Services.prefs.setIntPref("network.trr.mode", 3);
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_DEFAULT_MODE);
await new Promise(resolve =>
chan.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE))
);
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_DISABLED_MODE);
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_FIRST_MODE);
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_ONLY_MODE);
await new Promise(resolve =>
chan.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE))
);
dns.clearCache(true);
Services.prefs.setIntPref("network.trr.mode", 5);
Services.prefs.setCharPref(
"network.trr.uri",
`https://localhost:${h2Port}/doh?responseIP=1.1.1.1`
);
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_DEFAULT_MODE);
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_DISABLED_MODE);
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_FIRST_MODE);
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
dns.clearCache(true);
chan = makeChan(URL, Ci.nsIRequest.TRR_ONLY_MODE);
await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
await new Promise(resolve => httpserv.stop(resolve));
});
// verify basic A record
add_task(async function test1() {
dns.clearCache(true);
@ -1009,9 +1128,9 @@ add_task(async function test_connection_closed_no_bootstrap_localhost() {
});
add_task(async function test_connection_closed_no_bootstrap_no_excluded() {
// This test exists to document what happens when we're in TRR only mode
// and we don't set a bootstrap address. We use DNS to resolve the
// initial URI, but if the connection fails, we don't fallback to DNS
// This test makes sure that even in mode 3 without a bootstrap address
// we are able to restart the TRR connection if it drops - the TRR service
// channel will use regular DNS to resolve the TRR address.
dns.clearCache(true);
Services.prefs.setIntPref("network.trr.mode", 3); // TRR-only
Services.prefs.setCharPref("network.trr.excluded-domains", "");
@ -1031,11 +1150,8 @@ add_task(async function test_connection_closed_no_bootstrap_no_excluded() {
!Components.isSuccessCode(inStatus),
`${inStatus} should be an error code`
);
[, , inStatus] = await new DNSListener("bar2.example.com", undefined, false);
Assert.ok(
!Components.isSuccessCode(inStatus),
`${inStatus} should be an error code`
);
dns.clearCache(true);
await new DNSListener("bar2.example.com", "3.3.3.3");
});
add_task(async function test_connection_closed_trr_first() {