From 6a69a94e9ac1fefdd6cc45ebbbc51b54296eed0f Mon Sep 17 00:00:00 2001 From: Valentin Gosu Date: Tue, 7 Apr 2020 11:56:29 +0000 Subject: [PATCH] Bug 1625213 - Add simple e10s ESNI test r=dragana It seems there was no unit test exercising this code in e10s. This copies the simple test from test_esni_dns_fetch.js to make sure we have a little code coverage for the IPC code too. Differential Revision: https://phabricator.services.mozilla.com/D69327 --HG-- extra : moz-landing-system : lando --- .../test/unit_ipc/child_esni_dns_fetch1.js | 61 ++++++++++++++++ .../test/unit_ipc/test_esni_dns_fetch_wrap.js | 71 +++++++++++++++++++ netwerk/test/unit_ipc/xpcshell.ini | 2 + 3 files changed, 134 insertions(+) create mode 100644 netwerk/test/unit_ipc/child_esni_dns_fetch1.js create mode 100644 netwerk/test/unit_ipc/test_esni_dns_fetch_wrap.js diff --git a/netwerk/test/unit_ipc/child_esni_dns_fetch1.js b/netwerk/test/unit_ipc/child_esni_dns_fetch1.js new file mode 100644 index 000000000000..a8be85b1c26f --- /dev/null +++ b/netwerk/test/unit_ipc/child_esni_dns_fetch1.js @@ -0,0 +1,61 @@ +"use strict"; + +const dns = Cc["@mozilla.org/network/dns-service;1"].getService( + Ci.nsIDNSService +); +const threadManager = Cc["@mozilla.org/thread-manager;1"].getService( + Ci.nsIThreadManager +); +const mainThread = threadManager.currentThread; + +const defaultOriginAttributes = {}; + +let test_answer = "bXkgdm9pY2UgaXMgbXkgcGFzc3dvcmQ="; +let test_answer_addr = "127.0.0.1"; + +class DNSListener { + constructor() { + this.promise = new Promise(resolve => { + this.resolve = resolve; + }); + } + onLookupComplete(inRequest, inRecord, inStatus) { + let txtRec; + try { + txtRec = inRecord.QueryInterface(Ci.nsIDNSByTypeRecord); + } catch (e) {} + if (txtRec) { + this.resolve([inRequest, txtRec, inStatus, "onLookupByTypeComplete"]); + } else { + this.resolve([inRequest, inRecord, inStatus, "onLookupComplete"]); + } + } + // So we can await this as a promise. + then() { + return this.promise.then.apply(this.promise, arguments); + } +} + +DNSListener.prototype.QueryInterface = ChromeUtils.generateQI([ + Ci.nsIDNSListener, +]); + +add_task(async function testEsniRequest() { + // use the h2 server as DOH provider + let listenerEsni = new DNSListener(); + let request = dns.asyncResolveByType( + "_esni.example.com", + dns.RESOLVE_TYPE_TXT, + 0, + listenerEsni, + mainThread, + defaultOriginAttributes + ); + + let [inRequest, inRecord, inStatus, inType] = await listenerEsni; + Assert.equal(inStatus, Cr.NS_OK, "status OK"); + Assert.equal(inRequest, request, "correct request was used"); + Assert.equal(inType, "onLookupByTypeComplete", "check correct type"); + let answer = inRecord.getRecordsAsOneString(); + Assert.equal(answer, test_answer, "got correct answer"); +}); diff --git a/netwerk/test/unit_ipc/test_esni_dns_fetch_wrap.js b/netwerk/test/unit_ipc/test_esni_dns_fetch_wrap.js new file mode 100644 index 000000000000..024cf7efa12a --- /dev/null +++ b/netwerk/test/unit_ipc/test_esni_dns_fetch_wrap.js @@ -0,0 +1,71 @@ +"use strict"; + +let h2Port; +let prefs; + +function setup() { + let env = Cc["@mozilla.org/process/environment;1"].getService( + Ci.nsIEnvironment + ); + h2Port = env.get("MOZHTTP2_PORT"); + Assert.notEqual(h2Port, null); + Assert.notEqual(h2Port, ""); + + // Set to allow the cert presented by our H2 server + do_get_profile(); + prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch); + + prefs.setBoolPref("network.security.esni.enabled", false); + prefs.setBoolPref("network.http.spdy.enabled", true); + prefs.setBoolPref("network.http.spdy.enabled.http2", true); + // the TRR server is on 127.0.0.1 + prefs.setCharPref("network.trr.bootstrapAddress", "127.0.0.1"); + + // make all native resolve calls "secretly" resolve localhost instead + prefs.setBoolPref("network.dns.native-is-localhost", true); + + // 0 - off, 1 - race, 2 TRR first, 3 TRR only, 4 shadow + prefs.setIntPref("network.trr.mode", 2); // TRR first + prefs.setBoolPref("network.trr.wait-for-portal", false); + // don't confirm that TRR is working, just go! + prefs.setCharPref("network.trr.confirmationNS", "skip"); + + // So we can change the pref without clearing the cache to check a pushed + // record with a TRR path that fails. + prefs.setBoolPref("network.trr.clear-cache-on-pref-change", false); + + // The moz-http2 cert is for foo.example.com and is signed by http2-ca.pem + // so add that cert to the trust list as a signing cert. // the foo.example.com domain name. + const certdb = Cc["@mozilla.org/security/x509certdb;1"].getService( + Ci.nsIX509CertDB + ); + addCertFromFile(certdb, "../unit/http2-ca.pem", "CTu,u,u"); +} + +setup(); +registerCleanupFunction(() => { + prefs.clearUserPref("network.security.esni.enabled"); + prefs.clearUserPref("network.http.spdy.enabled"); + prefs.clearUserPref("network.http.spdy.enabled.http2"); + prefs.clearUserPref("network.dns.localDomains"); + prefs.clearUserPref("network.dns.native-is-localhost"); + prefs.clearUserPref("network.trr.mode"); + prefs.clearUserPref("network.trr.uri"); + prefs.clearUserPref("network.trr.credentials"); + prefs.clearUserPref("network.trr.wait-for-portal"); + prefs.clearUserPref("network.trr.allow-rfc1918"); + prefs.clearUserPref("network.trr.useGET"); + prefs.clearUserPref("network.trr.confirmationNS"); + prefs.clearUserPref("network.trr.bootstrapAddress"); + prefs.clearUserPref("network.trr.blacklist-duration"); + prefs.clearUserPref("network.trr.request-timeout"); + prefs.clearUserPref("network.trr.clear-cache-on-pref-change"); +}); + +function run_test() { + prefs.setCharPref( + "network.trr.uri", + "https://foo.example.com:" + h2Port + "/esni-dns" + ); + run_test_in_child("child_esni_dns_fetch1.js"); +} diff --git a/netwerk/test/unit_ipc/xpcshell.ini b/netwerk/test/unit_ipc/xpcshell.ini index 12aee430a8b8..725f8ee34076 100644 --- a/netwerk/test/unit_ipc/xpcshell.ini +++ b/netwerk/test/unit_ipc/xpcshell.ini @@ -60,6 +60,7 @@ support-files = !/netwerk/test/unit/test_alt-data_cross_process.js !/netwerk/test/unit/test_httpcancel.js child_cookie_header.js + child_esni_dns_fetch1.js [test_bug528292_wrap.js] [test_cookie_header_stripped.js] @@ -108,3 +109,4 @@ skip-if = true [test_multipart_streamconv_wrap.js] [test_alt-data_cross_process_wrap.js] [test_httpcancel_wrap.js] +[test_esni_dns_fetch_wrap.js]