1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
7 const { HttpServer } = ChromeUtils.importESModule(
8 "resource://testing-common/httpd.sys.mjs"
12 * Waits for an observer notification to fire.
14 * @param {String} topicName The notification topic.
15 * @returns {Promise} A promise that fulfills when the notification is fired.
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);
24 Services.obs.removeObserver(observe, topic);
25 resolve({ subject, data });
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");
38 let httpserver = null;
39 let httpserverv6 = null;
40 ChromeUtils.defineLazyGetter(this, "URL", function () {
41 return "http://localhost:" + httpserver.identity.primaryPort + "/content";
44 ChromeUtils.defineLazyGetter(this, "URLv6", function () {
45 return "http://[::1]:" + httpserverv6.identity.primaryPort + "/content";
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);
57 mozinfo.os == "linux" || mozinfo.os == "android"
61 add_task(async function testDNS() {
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",
71 Services.prefs.setCharPref(
72 "network.connectivity-service.DNSv6.domain",
76 await promiseObserverNotification(
77 "network:connectivity-service:dns-checks-complete"
82 Ci.nsINetworkConnectivityService.OK,
83 "Check DNSv4 support (expect OK)"
87 Ci.nsINetworkConnectivityService.OK,
88 "Check DNSv6 support (expect OK)"
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"
96 Services.prefs.setCharPref(
97 "network.connectivity-service.DNSv6.domain",
98 "does-not-exist.example"
100 let observerNotification = promiseObserverNotification(
101 "network:connectivity-service:dns-checks-complete"
104 await observerNotification;
108 Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
109 "Check DNSv4 support (expect N/A)"
113 Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
114 "Check DNSv6 support (expect N/A)"
117 // Set the endpoints back to the proper domains, and simulate a captive portal
119 Services.prefs.setCharPref(
120 "network.connectivity-service.DNSv4.domain",
123 Services.prefs.setCharPref(
124 "network.connectivity-service.DNSv6.domain",
127 observerNotification = promiseObserverNotification(
128 "network:connectivity-service:dns-checks-complete"
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.
134 Ci.nsINetworkConnectivityService.UNKNOWN,
135 "Check DNSv4 support (expect UNKNOWN)"
139 Ci.nsINetworkConnectivityService.UNKNOWN,
140 "Check DNSv6 support (expect UNKNOWN)"
143 await observerNotification;
147 Ci.nsINetworkConnectivityService.OK,
148 "Check DNSv4 support (expect OK)"
152 Ci.nsINetworkConnectivityService.OK,
153 "Check DNSv6 support (expect OK)"
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
167 Ci.nsINetworkConnectivityService.UNKNOWN,
168 "Check IPv4 support (expect UNKNOWN)"
172 Ci.nsINetworkConnectivityService.UNKNOWN,
173 "Check IPv6 support (expect UNKNOWN)"
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"
182 ncs.recheckIPConnectivity();
183 await observerNotification;
187 Ci.nsINetworkConnectivityService.OK,
188 "Check IPv4 support (expect OK)"
192 Ci.nsINetworkConnectivityService.OK,
193 "Check IPv6 support (expect OK)"
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"
202 Services.obs.notifyObservers(null, "network:captive-portal-connectivity");
203 await observerNotification;
207 Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
208 "Check IPv4 support (expect NOT_AVAILABLE)"
212 Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
213 "Check IPv6 support (expect NOT_AVAILABLE)"