Bug 1807268 - Re-enable verifyShowClipboardSuggestionsToggleTest UI test r=jajohnson
[gecko.git] / netwerk / test / unit / test_trr_with_proxy.js
blobd7f520595cba7129042e68d4b42fc7ee79c74067
1 /* This test checks that a TRRServiceChannel can connect to the server with
2    a proxy.
3    Steps:
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.
13 "use strict";
15 var { setTimeout } = ChromeUtils.importESModule(
16   "resource://gre/modules/Timer.sys.mjs"
19 /* import-globals-from trr_common.js */
21 let filter;
22 let systemProxySettings;
23 let trrProxy;
24 const pps = Cc["@mozilla.org/network/protocol-proxy-service;1"].getService();
26 function setup() {
27   h2Port = trr_test_setup();
28   SetParentalControlEnabled(false);
31 setup();
32 registerCleanupFunction(async () => {
33   trr_clear_prefs();
34   Services.prefs.clearUserPref("network.proxy.type");
35   Services.prefs.clearUserPref("network.proxy.autoconfig_url");
36   Services.prefs.clearUserPref("network.trr.async_connInfo");
37   if (trrProxy) {
38     await trrProxy.stop();
39   }
40 });
42 class ProxyFilter {
43   constructor(type, host, port, flags) {
44     this._type = type;
45     this._host = host;
46     this._port = port;
47     this._flags = flags;
48     this.QueryInterface = ChromeUtils.generateQI(["nsIProtocolProxyFilter"]);
49   }
50   applyFilter(uri, pi, cb) {
51     cb.onProxyFilterResult(
52       pps.newProxyInfo(
53         this._type,
54         this._host,
55         this._port,
56         "",
57         "",
58         this._flags,
59         1000,
60         null
61       )
62     );
63   }
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);
82   if (delay) {
83     await new Promise(resolve => do_timeout(delay, resolve));
84   }
86   await new TRRDNSListener("bar.example.com", "2.2.2.2");
88   // A non-zero request count indicates that TRR requests are being routed
89   // through the proxy.
90   Assert.ok(
91     (await trrProxy.request_count()) >= 1,
92     `Request count should be at least 1`
93   );
95   // clean up
96   Services.prefs.clearUserPref("network.proxy.type");
97   Services.prefs.clearUserPref("network.proxy.autoconfig_url");
98   if (filter) {
99     pps.unregisterFilter(filter);
100     filter = null;
101   }
102   if (systemProxySettings) {
103     MockRegistrar.unregister(systemProxySettings);
104     systemProxySettings = null;
105   }
107   await trrProxy.stop();
108   trrProxy = null;
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);
116   }
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);
125     });
126     httpserv.start(-1);
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 => {
133         let listener = {
134           QueryInterface: ChromeUtils.generateQI(["nsIConsoleListener"]),
135           observe(msg) {
136             if (msg == `PAC file installed from ${pacUri}`) {
137               Services.console.unregisterListener(listener);
138               resolve();
139             }
140           },
141         };
142         Services.console.registerListener(listener);
143       });
144     }
146     await consoleMessageObserved();
147   }
149   async function setupProxyFilter(proxyPort) {
150     filter = new ProxyFilter("https", "foo.example.com", proxyPort, 0);
151     pps.registerFilter(filter, 10);
152   }
154   async function setupSystemProxySettings(proxyPort) {
155     systemProxySettings = {
156       QueryInterface: ChromeUtils.generateQI(["nsISystemProxySettings"]),
157       mainThreadOnly: true,
158       PACURI: null,
159       getProxyForURI: () => {
160         return `HTTPS foo.example.com:${proxyPort}`;
161       },
162     };
164     MockRegistrar.register(
165       "@mozilla.org/system-proxy-settings;1",
166       systemProxySettings
167     );
169     Services.prefs.setIntPref(
170       "network.proxy.type",
171       Ci.nsIProtocolProxyService.PROXYCONFIG_SYSTEM
172     );
174     // simulate that system proxy setting is changed.
175     pps.notifyProxyConfigChangedInternal();
176   }
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");