summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_about_networking.js
blob: b8b864c5a06e3fa502eafd6e0935807b7f81d683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const { HttpServer } = ChromeUtils.importESModule(
  "resource://testing-common/httpd.sys.mjs"
);

const gDashboard = Cc["@mozilla.org/network/dashboard;1"].getService(
  Ci.nsIDashboard
);

const gServerSocket = Cc["@mozilla.org/network/server-socket;1"].createInstance(
  Ci.nsIServerSocket
);
const gHttpServer = new HttpServer();

add_test(function test_http() {
  gDashboard.requestHttpConnections(function (data) {
    let found = false;
    for (let i = 0; i < data.connections.length; i++) {
      if (data.connections[i].host == "localhost") {
        found = true;
        break;
      }
    }
    Assert.equal(found, true);

    run_next_test();
  });
});

add_test(function test_dns() {
  gDashboard.requestDNSInfo(function (data) {
    let found = false;
    for (let i = 0; i < data.entries.length; i++) {
      if (data.entries[i].hostname == "localhost") {
        found = true;
        break;
      }
    }
    Assert.equal(found, true);

    do_test_pending();
    gHttpServer.stop(do_test_finished);

    run_next_test();
  });
});

add_test(function test_sockets() {
  // TODO: enable this test in bug 1581892.
  if (mozinfo.socketprocess_networking) {
    info("skip test_sockets");
    run_next_test();
    return;
  }

  let sts = Cc["@mozilla.org/network/socket-transport-service;1"].getService(
    Ci.nsISocketTransportService
  );
  let threadManager = Cc["@mozilla.org/thread-manager;1"].getService();

  let transport = sts.createTransport(
    [],
    "127.0.0.1",
    gServerSocket.port,
    null,
    null
  );
  let listener = {
    onTransportStatus(aTransport, aStatus) {
      if (aStatus == Ci.nsISocketTransport.STATUS_CONNECTED_TO) {
        gDashboard.requestSockets(function (data) {
          gServerSocket.close();
          let found = false;
          for (let i = 0; i < data.sockets.length; i++) {
            if (data.sockets[i].host == "127.0.0.1") {
              found = true;
              break;
            }
          }
          Assert.equal(found, true);

          run_next_test();
        });
      }
    },
  };
  transport.setEventSink(listener, threadManager.currentThread);

  transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
});

function run_test() {
  Services.prefs.setBoolPref(
    "network.cookieJarSettings.unblocked_for_testing",
    true
  );

  // We always resolve localhost as it's hardcoded without the following pref:
  Services.prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);

  gHttpServer.start(-1);

  let uri = Services.io.newURI(
    "http://localhost:" + gHttpServer.identity.primaryPort
  );
  let channel = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true });

  channel.open();

  gServerSocket.init(-1, true, -1);
  Services.prefs.clearUserPref("network.proxy.allow_hijacking_localhost");

  run_next_test();
}