tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_network_connectivity_service.js (6442B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/
      3 */
      4 
      5 "use strict";
      6 
      7 const { HttpServer } = ChromeUtils.importESModule(
      8  "resource://testing-common/httpd.sys.mjs"
      9 );
     10 
     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  });
     28 }
     29 
     30 Services.prefs.setBoolPref(
     31  "network.connectivity-service.wait_for_idle_startup",
     32  false
     33 );
     34 registerCleanupFunction(() => {
     35  Services.prefs.clearUserPref("network.connectivity-service.DNSv4.domain");
     36  Services.prefs.clearUserPref("network.connectivity-service.DNSv6.domain");
     37  Services.prefs.clearUserPref("network.captive-portal-service.testMode");
     38  Services.prefs.clearUserPref("network.connectivity-service.IPv4.url");
     39  Services.prefs.clearUserPref("network.connectivity-service.IPv6.url");
     40 });
     41 
     42 let httpserver = null;
     43 let httpserverv6 = null;
     44 ChromeUtils.defineLazyGetter(this, "URL", function () {
     45  return "http://localhost:" + httpserver.identity.primaryPort + "/content";
     46 });
     47 
     48 ChromeUtils.defineLazyGetter(this, "URLv6", function () {
     49  return "http://[::1]:" + httpserverv6.identity.primaryPort + "/content";
     50 });
     51 
     52 function contentHandler(metadata, response) {
     53  response.setHeader("Content-Type", "text/plain");
     54  response.setHeader("Cache-Control", "no-cache");
     55 
     56  const responseBody = "anybody";
     57  response.bodyOutputStream.write(responseBody, responseBody.length);
     58 }
     59 
     60 const kDNSv6Domain =
     61  mozinfo.os == "linux" || mozinfo.os == "android"
     62    ? "ip6-localhost"
     63    : "localhost";
     64 
     65 add_task(async function testDNS() {
     66  let ncs = Cc[
     67    "@mozilla.org/network/network-connectivity-service;1"
     68  ].getService(Ci.nsINetworkConnectivityService);
     69 
     70  // Set the endpoints, trigger a DNS recheck, and wait for it to complete.
     71  Services.prefs.setCharPref(
     72    "network.connectivity-service.DNSv4.domain",
     73    "example.org"
     74  );
     75  Services.prefs.setCharPref(
     76    "network.connectivity-service.DNSv6.domain",
     77    kDNSv6Domain
     78  );
     79  ncs.recheckDNS();
     80  await promiseObserverNotification(
     81    "network:connectivity-service:dns-checks-complete"
     82  );
     83 
     84  equal(
     85    ncs.DNSv4,
     86    Ci.nsINetworkConnectivityService.OK,
     87    "Check DNSv4 support (expect OK)"
     88  );
     89  equal(
     90    ncs.DNSv6,
     91    Ci.nsINetworkConnectivityService.OK,
     92    "Check DNSv6 support (expect OK)"
     93  );
     94 
     95  // Set the endpoints to non-exitant domains, trigger a DNS recheck, and wait for it to complete.
     96  Services.prefs.setCharPref(
     97    "network.connectivity-service.DNSv4.domain",
     98    "does-not-exist.example"
     99  );
    100  Services.prefs.setCharPref(
    101    "network.connectivity-service.DNSv6.domain",
    102    "does-not-exist.example"
    103  );
    104  let observerNotification = promiseObserverNotification(
    105    "network:connectivity-service:dns-checks-complete"
    106  );
    107  ncs.recheckDNS();
    108  await observerNotification;
    109 
    110  equal(
    111    ncs.DNSv4,
    112    Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
    113    "Check DNSv4 support (expect N/A)"
    114  );
    115  equal(
    116    ncs.DNSv6,
    117    Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
    118    "Check DNSv6 support (expect N/A)"
    119  );
    120 
    121  // Set the endpoints back to the proper domains, and simulate a captive portal
    122  // event.
    123  Services.prefs.setCharPref(
    124    "network.connectivity-service.DNSv4.domain",
    125    "example.org"
    126  );
    127  Services.prefs.setCharPref(
    128    "network.connectivity-service.DNSv6.domain",
    129    kDNSv6Domain
    130  );
    131  observerNotification = promiseObserverNotification(
    132    "network:connectivity-service:dns-checks-complete"
    133  );
    134  Services.obs.notifyObservers(null, "network:captive-portal-connectivity");
    135  // This will cause the state to go to UNKNOWN for a bit, until the check is completed.
    136  equal(
    137    ncs.DNSv4,
    138    Ci.nsINetworkConnectivityService.UNKNOWN,
    139    "Check DNSv4 support (expect UNKNOWN)"
    140  );
    141  equal(
    142    ncs.DNSv6,
    143    Ci.nsINetworkConnectivityService.UNKNOWN,
    144    "Check DNSv6 support (expect UNKNOWN)"
    145  );
    146 
    147  await observerNotification;
    148 
    149  equal(
    150    ncs.DNSv4,
    151    Ci.nsINetworkConnectivityService.OK,
    152    "Check DNSv4 support (expect OK)"
    153  );
    154  equal(
    155    ncs.DNSv6,
    156    Ci.nsINetworkConnectivityService.OK,
    157    "Check DNSv6 support (expect OK)"
    158  );
    159 
    160  httpserver = new HttpServer();
    161  httpserver.registerPathHandler("/content", contentHandler);
    162  httpserver.start(-1);
    163 
    164  httpserverv6 = new HttpServer();
    165  httpserverv6.registerPathHandler("/contentt", contentHandler);
    166  httpserverv6._start(-1, "[::1]");
    167 
    168  // Before setting the pref, this status is unknown in automation
    169  equal(
    170    ncs.IPv4,
    171    Ci.nsINetworkConnectivityService.UNKNOWN,
    172    "Check IPv4 support (expect UNKNOWN)"
    173  );
    174  equal(
    175    ncs.IPv6,
    176    Ci.nsINetworkConnectivityService.UNKNOWN,
    177    "Check IPv6 support (expect UNKNOWN)"
    178  );
    179 
    180  Services.prefs.setBoolPref("network.captive-portal-service.testMode", true);
    181  Services.prefs.setCharPref("network.connectivity-service.IPv4.url", URL);
    182  Services.prefs.setCharPref("network.connectivity-service.IPv6.url", URLv6);
    183  observerNotification = promiseObserverNotification(
    184    "network:connectivity-service:ip-checks-complete"
    185  );
    186  ncs.recheckIPConnectivity();
    187  await observerNotification;
    188 
    189  equal(
    190    ncs.IPv4,
    191    Ci.nsINetworkConnectivityService.OK,
    192    "Check IPv4 support (expect OK)"
    193  );
    194  equal(
    195    ncs.IPv6,
    196    Ci.nsINetworkConnectivityService.OK,
    197    "Check IPv6 support (expect OK)"
    198  );
    199 
    200  // check that the CPS status is NOT_AVAILABLE when the endpoint is down.
    201  await new Promise(resolve => httpserver.stop(resolve));
    202  await new Promise(resolve => httpserverv6.stop(resolve));
    203  observerNotification = promiseObserverNotification(
    204    "network:connectivity-service:ip-checks-complete"
    205  );
    206  Services.obs.notifyObservers(null, "network:captive-portal-connectivity");
    207  await observerNotification;
    208 
    209  equal(
    210    ncs.IPv4,
    211    Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
    212    "Check IPv4 support (expect NOT_AVAILABLE)"
    213  );
    214  equal(
    215    ncs.IPv6,
    216    Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
    217    "Check IPv6 support (expect NOT_AVAILABLE)"
    218  );
    219 });