tor-browser

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

test_ping_aboutnetworking.js (3051B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 "use strict";
      7 
      8 const gDashboard = Cc["@mozilla.org/network/dashboard;1"].getService(
      9  Ci.nsIDashboard
     10 );
     11 
     12 function connectionFailed(status) {
     13  let status_ok = [
     14    "NS_NET_STATUS_RESOLVING_HOST",
     15    "NS_NET_STATUS_RESOLVED_HOST",
     16    "NS_NET_STATUS_CONNECTING_TO",
     17    "NS_NET_STATUS_CONNECTED_TO",
     18  ];
     19  for (let i = 0; i < status_ok.length; i++) {
     20    if (status == status_ok[i]) {
     21      return false;
     22    }
     23  }
     24 
     25  return true;
     26 }
     27 
     28 function test_sockets(serverSocket) {
     29  // TODO: enable this test in bug 1581892.
     30  if (mozinfo.socketprocess_networking) {
     31    info("skip test_sockets");
     32    do_test_finished();
     33    return;
     34  }
     35 
     36  do_test_pending();
     37  gDashboard.requestSockets(function (data) {
     38    let index = -1;
     39    info("requestSockets: " + JSON.stringify(data.sockets));
     40    for (let i = 0; i < data.sockets.length; i++) {
     41      if (data.sockets[i].host == "127.0.0.1") {
     42        index = i;
     43        break;
     44      }
     45    }
     46    Assert.notEqual(index, -1);
     47    Assert.equal(data.sockets[index].port, serverSocket.port);
     48    Assert.equal(data.sockets[index].type, "TCP");
     49 
     50    do_test_finished();
     51  });
     52 }
     53 
     54 function run_test() {
     55  var ps = Services.prefs;
     56  // disable network changed events to avoid the the risk of having the dns
     57  // cache getting flushed behind our back
     58  ps.setBoolPref("network.notify.changed", false);
     59  // Localhost is hardcoded to loopback and isn't cached, disable that with this pref
     60  ps.setBoolPref("network.proxy.allow_hijacking_localhost", true);
     61  ps.setBoolPref(
     62    "network.proxy.testing_localhost_is_secure_when_hijacked",
     63    false
     64  );
     65 
     66  registerCleanupFunction(function () {
     67    ps.clearUserPref("network.notify.changed");
     68    ps.clearUserPref("network.proxy.allow_hijacking_localhost");
     69    ps.clearUserPref("network.proxy.testing_localhost_is_secure_when_hijacked");
     70  });
     71 
     72  let serverSocket = Cc["@mozilla.org/network/server-socket;1"].createInstance(
     73    Ci.nsIServerSocket
     74  );
     75  serverSocket.init(-1, true, -1);
     76 
     77  do_test_pending();
     78  gDashboard.requestConnection(
     79    "localhost",
     80    serverSocket.port,
     81    "tcp",
     82    15,
     83    function (connInfo) {
     84      if (connInfo.status == "NS_NET_STATUS_CONNECTED_TO") {
     85        do_test_pending();
     86        gDashboard.requestDNSInfo(function (data) {
     87          let found = false;
     88          info("requestDNSInfo: " + JSON.stringify(data.entries));
     89          for (let i = 0; i < data.entries.length; i++) {
     90            if (data.entries[i].hostname == "localhost") {
     91              found = true;
     92              break;
     93            }
     94          }
     95          Assert.equal(found, true);
     96 
     97          do_test_finished();
     98          test_sockets(serverSocket);
     99        });
    100 
    101        do_test_finished();
    102      }
    103      if (connectionFailed(connInfo.status)) {
    104        do_throw(connInfo.status);
    105      }
    106    }
    107  );
    108 }