tor-browser

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

test_about_networking.js (3327B)


      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 { HttpServer } = ChromeUtils.importESModule(
      9  "resource://testing-common/httpd.sys.mjs"
     10 );
     11 
     12 const gDashboard = Cc["@mozilla.org/network/dashboard;1"].getService(
     13  Ci.nsIDashboard
     14 );
     15 
     16 const gServerSocket = Cc["@mozilla.org/network/server-socket;1"].createInstance(
     17  Ci.nsIServerSocket
     18 );
     19 const gHttpServer = new HttpServer();
     20 
     21 add_test(function test_http() {
     22  gDashboard.requestHttpConnections(function (data) {
     23    let found = false;
     24    for (let i = 0; i < data.connections.length; i++) {
     25      if (data.connections[i].host == "localhost") {
     26        found = true;
     27        break;
     28      }
     29    }
     30    Assert.equal(found, true);
     31 
     32    run_next_test();
     33  });
     34 });
     35 
     36 add_test(function test_dns() {
     37  gDashboard.requestDNSInfo(function (data) {
     38    let found = false;
     39    for (let i = 0; i < data.entries.length; i++) {
     40      if (data.entries[i].hostname == "localhost") {
     41        found = true;
     42        break;
     43      }
     44    }
     45    Assert.equal(found, true);
     46 
     47    do_test_pending();
     48    gHttpServer.stop(do_test_finished);
     49 
     50    run_next_test();
     51  });
     52 });
     53 
     54 add_test(function test_sockets() {
     55  // TODO: enable this test in bug 1581892.
     56  if (mozinfo.socketprocess_networking) {
     57    info("skip test_sockets");
     58    run_next_test();
     59    return;
     60  }
     61 
     62  let sts = Cc["@mozilla.org/network/socket-transport-service;1"].getService(
     63    Ci.nsISocketTransportService
     64  );
     65  let threadManager = Cc["@mozilla.org/thread-manager;1"].getService();
     66 
     67  let transport = sts.createTransport(
     68    [],
     69    "127.0.0.1",
     70    gServerSocket.port,
     71    null,
     72    null
     73  );
     74  let listener = {
     75    onTransportStatus(aTransport, aStatus) {
     76      if (aStatus == Ci.nsISocketTransport.STATUS_CONNECTED_TO) {
     77        gDashboard.requestSockets(function (data) {
     78          gServerSocket.close();
     79          let found = false;
     80          for (let i = 0; i < data.sockets.length; i++) {
     81            if (data.sockets[i].host == "127.0.0.1") {
     82              found = true;
     83              break;
     84            }
     85          }
     86          Assert.equal(found, true);
     87 
     88          run_next_test();
     89        });
     90      }
     91    },
     92  };
     93  transport.setEventSink(listener, threadManager.currentThread);
     94 
     95  transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
     96 });
     97 
     98 function run_test() {
     99  Services.prefs.setBoolPref(
    100    "network.cookieJarSettings.unblocked_for_testing",
    101    true
    102  );
    103 
    104  // We always resolve localhost as it's hardcoded without the following pref:
    105  Services.prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
    106  Services.prefs.setBoolPref(
    107    "network.proxy.testing_localhost_is_secure_when_hijacked",
    108    false
    109  );
    110 
    111  gHttpServer.start(-1);
    112 
    113  let uri = Services.io.newURI(
    114    "http://localhost:" + gHttpServer.identity.primaryPort
    115  );
    116  let channel = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true });
    117 
    118  channel.open();
    119 
    120  gServerSocket.init(-1, true, -1);
    121  Services.prefs.clearUserPref("network.proxy.allow_hijacking_localhost");
    122  Services.prefs.clearUserPref(
    123    "network.proxy.testing_localhost_is_secure_when_hijacked"
    124  );
    125 
    126  run_next_test();
    127 }