summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_about_networking.js
blob: efcd5910ea3de29c83bb8ee86e8a86464c2edea9 (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
/* -*- 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/. */

Cu.import("resource://testing-common/httpd.js");
Cu.import("resource://gre/modules/NetUtil.jsm");

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

const gServerSocket = Components.classes["@mozilla.org/network/server-socket;1"]
                             .createInstance(Components.interfaces.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;
      }
    }
    do_check_eq(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;
      }
    }
    do_check_eq(found, true);

    do_test_pending();
    gHttpServer.stop(do_test_finished);

    run_next_test();
  });
});

add_test(function test_sockets() {
  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(null, 0, "127.0.0.1",
                                      gServerSocket.port, null);
  let listener = {
    onTransportStatus: function(aTransport, aStatus, aProgress, aProgressMax) {
      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;
            }
          }
          do_check_eq(found, true);

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

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

function run_test() {
  let ioService = Cc["@mozilla.org/network/io-service;1"]
    .getService(Ci.nsIIOService);

  gHttpServer.start(-1);

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

  channel.open2();

  gServerSocket.init(-1, true, -1);

  run_next_test();
}