Bug 1807268 - Re-enable verifyShowClipboardSuggestionsToggleTest UI test r=jajohnson
[gecko.git] / netwerk / test / unit / test_network_connectivity_service.js
blob7604c263c6a34e9ecd5e1cf8defadf7c112cd40e
1 /* Any copyright is dedicated to the Public Domain.
2  * http://creativecommons.org/publicdomain/zero/1.0/
3  */
5 "use strict";
7 const { HttpServer } = ChromeUtils.importESModule(
8   "resource://testing-common/httpd.sys.mjs"
9 );
11 /**
12  * Waits for an observer notification to fire.
13  *
14  * @param {String} topicName The notification topic.
15  * @returns {Promise} A promise that fulfills when the notification is fired.
16  */
17 function promiseObserverNotification(topicName, matchFunc) {
18   return new Promise(resolve => {
19     Services.obs.addObserver(function observe(subject, topic, data) {
20       let matches = typeof matchFunc != "function" || matchFunc(subject, data);
21       if (!matches) {
22         return;
23       }
24       Services.obs.removeObserver(observe, topic);
25       resolve({ subject, data });
26     }, topicName);
27   });
30 registerCleanupFunction(() => {
31   Services.prefs.clearUserPref("network.connectivity-service.DNSv4.domain");
32   Services.prefs.clearUserPref("network.connectivity-service.DNSv6.domain");
33   Services.prefs.clearUserPref("network.captive-portal-service.testMode");
34   Services.prefs.clearUserPref("network.connectivity-service.IPv4.url");
35   Services.prefs.clearUserPref("network.connectivity-service.IPv6.url");
36 });
38 let httpserver = null;
39 let httpserverv6 = null;
40 ChromeUtils.defineLazyGetter(this, "URL", function () {
41   return "http://localhost:" + httpserver.identity.primaryPort + "/content";
42 });
44 ChromeUtils.defineLazyGetter(this, "URLv6", function () {
45   return "http://[::1]:" + httpserverv6.identity.primaryPort + "/content";
46 });
48 function contentHandler(metadata, response) {
49   response.setHeader("Content-Type", "text/plain");
50   response.setHeader("Cache-Control", "no-cache");
52   const responseBody = "anybody";
53   response.bodyOutputStream.write(responseBody, responseBody.length);
56 const kDNSv6Domain =
57   mozinfo.os == "linux" || mozinfo.os == "android"
58     ? "ip6-localhost"
59     : "localhost";
61 add_task(async function testDNS() {
62   let ncs = Cc[
63     "@mozilla.org/network/network-connectivity-service;1"
64   ].getService(Ci.nsINetworkConnectivityService);
66   // Set the endpoints, trigger a DNS recheck, and wait for it to complete.
67   Services.prefs.setCharPref(
68     "network.connectivity-service.DNSv4.domain",
69     "example.org"
70   );
71   Services.prefs.setCharPref(
72     "network.connectivity-service.DNSv6.domain",
73     kDNSv6Domain
74   );
75   ncs.recheckDNS();
76   await promiseObserverNotification(
77     "network:connectivity-service:dns-checks-complete"
78   );
80   equal(
81     ncs.DNSv4,
82     Ci.nsINetworkConnectivityService.OK,
83     "Check DNSv4 support (expect OK)"
84   );
85   equal(
86     ncs.DNSv6,
87     Ci.nsINetworkConnectivityService.OK,
88     "Check DNSv6 support (expect OK)"
89   );
91   // Set the endpoints to non-exitant domains, trigger a DNS recheck, and wait for it to complete.
92   Services.prefs.setCharPref(
93     "network.connectivity-service.DNSv4.domain",
94     "does-not-exist.example"
95   );
96   Services.prefs.setCharPref(
97     "network.connectivity-service.DNSv6.domain",
98     "does-not-exist.example"
99   );
100   let observerNotification = promiseObserverNotification(
101     "network:connectivity-service:dns-checks-complete"
102   );
103   ncs.recheckDNS();
104   await observerNotification;
106   equal(
107     ncs.DNSv4,
108     Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
109     "Check DNSv4 support (expect N/A)"
110   );
111   equal(
112     ncs.DNSv6,
113     Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
114     "Check DNSv6 support (expect N/A)"
115   );
117   // Set the endpoints back to the proper domains, and simulate a captive portal
118   // event.
119   Services.prefs.setCharPref(
120     "network.connectivity-service.DNSv4.domain",
121     "example.org"
122   );
123   Services.prefs.setCharPref(
124     "network.connectivity-service.DNSv6.domain",
125     kDNSv6Domain
126   );
127   observerNotification = promiseObserverNotification(
128     "network:connectivity-service:dns-checks-complete"
129   );
130   Services.obs.notifyObservers(null, "network:captive-portal-connectivity");
131   // This will cause the state to go to UNKNOWN for a bit, until the check is completed.
132   equal(
133     ncs.DNSv4,
134     Ci.nsINetworkConnectivityService.UNKNOWN,
135     "Check DNSv4 support (expect UNKNOWN)"
136   );
137   equal(
138     ncs.DNSv6,
139     Ci.nsINetworkConnectivityService.UNKNOWN,
140     "Check DNSv6 support (expect UNKNOWN)"
141   );
143   await observerNotification;
145   equal(
146     ncs.DNSv4,
147     Ci.nsINetworkConnectivityService.OK,
148     "Check DNSv4 support (expect OK)"
149   );
150   equal(
151     ncs.DNSv6,
152     Ci.nsINetworkConnectivityService.OK,
153     "Check DNSv6 support (expect OK)"
154   );
156   httpserver = new HttpServer();
157   httpserver.registerPathHandler("/content", contentHandler);
158   httpserver.start(-1);
160   httpserverv6 = new HttpServer();
161   httpserverv6.registerPathHandler("/contentt", contentHandler);
162   httpserverv6._start(-1, "[::1]");
164   // Before setting the pref, this status is unknown in automation
165   equal(
166     ncs.IPv4,
167     Ci.nsINetworkConnectivityService.UNKNOWN,
168     "Check IPv4 support (expect UNKNOWN)"
169   );
170   equal(
171     ncs.IPv6,
172     Ci.nsINetworkConnectivityService.UNKNOWN,
173     "Check IPv6 support (expect UNKNOWN)"
174   );
176   Services.prefs.setBoolPref("network.captive-portal-service.testMode", true);
177   Services.prefs.setCharPref("network.connectivity-service.IPv4.url", URL);
178   Services.prefs.setCharPref("network.connectivity-service.IPv6.url", URLv6);
179   observerNotification = promiseObserverNotification(
180     "network:connectivity-service:ip-checks-complete"
181   );
182   ncs.recheckIPConnectivity();
183   await observerNotification;
185   equal(
186     ncs.IPv4,
187     Ci.nsINetworkConnectivityService.OK,
188     "Check IPv4 support (expect OK)"
189   );
190   equal(
191     ncs.IPv6,
192     Ci.nsINetworkConnectivityService.OK,
193     "Check IPv6 support (expect OK)"
194   );
196   // check that the CPS status is NOT_AVAILABLE when the endpoint is down.
197   await new Promise(resolve => httpserver.stop(resolve));
198   await new Promise(resolve => httpserverv6.stop(resolve));
199   observerNotification = promiseObserverNotification(
200     "network:connectivity-service:ip-checks-complete"
201   );
202   Services.obs.notifyObservers(null, "network:captive-portal-connectivity");
203   await observerNotification;
205   equal(
206     ncs.IPv4,
207     Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
208     "Check IPv4 support (expect NOT_AVAILABLE)"
209   );
210   equal(
211     ncs.IPv6,
212     Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
213     "Check IPv6 support (expect NOT_AVAILABLE)"
214   );