1 /* This test checks that a TRRServiceChannel can connect to the server with
4 - Setup the proxy (PAC, proxy filter, and system proxy settings)
5 - Test when "network.trr.async_connInfo" is false. In this case, every
6 TRRServicChannel waits for the proxy info to be resolved.
7 - Test when "network.trr.async_connInfo" is true. In this case, every
8 TRRServicChannel uses an already created connection info to connect.
9 - The test test_trr_uri_change() is about checking if trr connection info
10 is updated correctly when trr uri changed.
15 var { setTimeout } = ChromeUtils.importESModule(
16 "resource://gre/modules/Timer.sys.mjs"
19 /* import-globals-from trr_common.js */
22 let systemProxySettings;
24 const pps = Cc["@mozilla.org/network/protocol-proxy-service;1"].getService();
27 h2Port = trr_test_setup();
28 SetParentalControlEnabled(false);
32 registerCleanupFunction(async () => {
34 Services.prefs.clearUserPref("network.proxy.type");
35 Services.prefs.clearUserPref("network.proxy.autoconfig_url");
36 Services.prefs.clearUserPref("network.trr.async_connInfo");
38 await trrProxy.stop();
43 constructor(type, host, port, flags) {
48 this.QueryInterface = ChromeUtils.generateQI(["nsIProtocolProxyFilter"]);
50 applyFilter(uri, pi, cb) {
51 cb.onProxyFilterResult(
66 async function doTest(proxySetup, delay) {
67 info("Verifying a basic A record");
68 Services.dns.clearCache(true);
69 // Close all previous connections.
70 Services.obs.notifyObservers(null, "net:cancel-all-connections");
71 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
72 await new Promise(resolve => setTimeout(resolve, 1000));
74 setModeAndURI(2, "doh?responseIP=2.2.2.2"); // TRR-first
76 trrProxy = new TRRProxy();
77 await trrProxy.start(h2Port);
78 info("port=" + trrProxy.port);
80 await proxySetup(trrProxy.port);
83 await new Promise(resolve => do_timeout(delay, resolve));
86 await new TRRDNSListener("bar.example.com", "2.2.2.2");
88 // A non-zero request count indicates that TRR requests are being routed
91 (await trrProxy.request_count()) >= 1,
92 `Request count should be at least 1`
96 Services.prefs.clearUserPref("network.proxy.type");
97 Services.prefs.clearUserPref("network.proxy.autoconfig_url");
99 pps.unregisterFilter(filter);
102 if (systemProxySettings) {
103 MockRegistrar.unregister(systemProxySettings);
104 systemProxySettings = null;
107 await trrProxy.stop();
111 add_task(async function test_trr_proxy() {
112 async function setupPACWithDataURL(proxyPort) {
113 var pac = `data:text/plain, function FindProxyForURL(url, host) { return "HTTPS foo.example.com:${proxyPort}";}`;
114 Services.prefs.setIntPref("network.proxy.type", 2);
115 Services.prefs.setCharPref("network.proxy.autoconfig_url", pac);
118 async function setupPACWithHttpURL(proxyPort) {
119 let httpserv = new HttpServer();
120 httpserv.registerPathHandler("/", function handler(metadata, response) {
121 response.setStatusLine(metadata.httpVersion, 200, "OK");
122 let content = `function FindProxyForURL(url, host) { return "HTTPS foo.example.com:${proxyPort}";}`;
123 response.setHeader("Content-Length", `${content.length}`);
124 response.bodyOutputStream.write(content, content.length);
127 Services.prefs.setIntPref("network.proxy.type", 2);
128 let pacUri = `http://127.0.0.1:${httpserv.identity.primaryPort}/`;
129 Services.prefs.setCharPref("network.proxy.autoconfig_url", pacUri);
131 function consoleMessageObserved() {
132 return new Promise(resolve => {
134 QueryInterface: ChromeUtils.generateQI(["nsIConsoleListener"]),
136 if (msg == `PAC file installed from ${pacUri}`) {
137 Services.console.unregisterListener(listener);
142 Services.console.registerListener(listener);
146 await consoleMessageObserved();
149 async function setupProxyFilter(proxyPort) {
150 filter = new ProxyFilter("https", "foo.example.com", proxyPort, 0);
151 pps.registerFilter(filter, 10);
154 async function setupSystemProxySettings(proxyPort) {
155 systemProxySettings = {
156 QueryInterface: ChromeUtils.generateQI(["nsISystemProxySettings"]),
157 mainThreadOnly: true,
159 getProxyForURI: () => {
160 return `HTTPS foo.example.com:${proxyPort}`;
164 MockRegistrar.register(
165 "@mozilla.org/system-proxy-settings;1",
169 Services.prefs.setIntPref(
170 "network.proxy.type",
171 Ci.nsIProtocolProxyService.PROXYCONFIG_SYSTEM
174 // simulate that system proxy setting is changed.
175 pps.notifyProxyConfigChangedInternal();
178 Services.prefs.setBoolPref("network.trr.async_connInfo", false);
179 await doTest(setupPACWithDataURL);
180 await doTest(setupPACWithDataURL, 1000);
181 await doTest(setupPACWithHttpURL);
182 await doTest(setupPACWithHttpURL, 1000);
183 await doTest(setupProxyFilter);
184 await doTest(setupProxyFilter, 1000);
185 await doTest(setupSystemProxySettings);
186 await doTest(setupSystemProxySettings, 1000);
188 Services.prefs.setBoolPref("network.trr.async_connInfo", true);
189 await doTest(setupPACWithDataURL);
190 await doTest(setupPACWithDataURL, 1000);
191 await doTest(setupPACWithHttpURL);
192 await doTest(setupPACWithHttpURL, 1000);
193 await doTest(setupProxyFilter);
194 await doTest(setupProxyFilter, 1000);
195 await doTest(setupSystemProxySettings);
196 await doTest(setupSystemProxySettings, 1000);
199 add_task(async function test_trr_uri_change() {
200 Services.prefs.setIntPref("network.proxy.type", 0);
201 Services.prefs.setBoolPref("network.trr.async_connInfo", true);
202 Services.dns.clearCache(true);
203 setModeAndURI(2, "doh?responseIP=2.2.2.2", "127.0.0.1");
205 await new TRRDNSListener("car.example.com", "127.0.0.1");
207 Services.dns.clearCache(true);
208 setModeAndURI(2, "doh?responseIP=2.2.2.2");
209 await new TRRDNSListener("car.example.net", "2.2.2.2");