1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/unit/test_about_networking.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 1.4 +/* -*- Mode: Javasript; indent-tab-mode: nil; js-indent-level: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +Cu.import("resource://testing-common/httpd.js"); 1.10 + 1.11 +const gDashboard = Cc['@mozilla.org/network/dashboard;1'] 1.12 + .getService(Ci.nsIDashboard); 1.13 + 1.14 +const gServerSocket = Components.classes["@mozilla.org/network/server-socket;1"] 1.15 + .createInstance(Components.interfaces.nsIServerSocket); 1.16 +const gHttpServer = new HttpServer(); 1.17 + 1.18 +add_test(function test_http() { 1.19 + gDashboard.requestHttpConnections(function(data) { 1.20 + let found = false; 1.21 + for (let i = 0; i < data.connections.length; i++) { 1.22 + if (data.connections[i].host == "localhost") { 1.23 + found = true; 1.24 + break; 1.25 + } 1.26 + } 1.27 + do_check_eq(found, true); 1.28 + 1.29 + run_next_test(); 1.30 + }); 1.31 +}); 1.32 + 1.33 +add_test(function test_dns() { 1.34 + gDashboard.requestDNSInfo(function(data) { 1.35 + let found = false; 1.36 + for (let i = 0; i < data.entries.length; i++) { 1.37 + if (data.entries[i].hostname == "localhost") { 1.38 + found = true; 1.39 + break; 1.40 + } 1.41 + } 1.42 + do_check_eq(found, true); 1.43 + 1.44 + do_test_pending(); 1.45 + gHttpServer.stop(do_test_finished); 1.46 + 1.47 + run_next_test(); 1.48 + }); 1.49 +}); 1.50 + 1.51 +add_test(function test_sockets() { 1.52 + let sts = Cc["@mozilla.org/network/socket-transport-service;1"] 1.53 + .getService(Ci.nsISocketTransportService); 1.54 + let threadManager = Cc["@mozilla.org/thread-manager;1"].getService(); 1.55 + 1.56 + let transport = sts.createTransport(null, 0, "127.0.0.1", 1.57 + gServerSocket.port, null); 1.58 + let listener = { 1.59 + onTransportStatus: function(aTransport, aStatus, aProgress, aProgressMax) { 1.60 + if (aStatus == Ci.nsISocketTransport.STATUS_CONNECTED_TO) { 1.61 + gDashboard.requestSockets(function(data) { 1.62 + gServerSocket.close(); 1.63 + let found = false; 1.64 + for (let i = 0; i < data.sockets.length; i++) { 1.65 + if (data.sockets[i].host == "127.0.0.1") { 1.66 + found = true; 1.67 + break; 1.68 + } 1.69 + } 1.70 + do_check_eq(found, true); 1.71 + 1.72 + run_next_test(); 1.73 + }); 1.74 + } 1.75 + } 1.76 + }; 1.77 + transport.setEventSink(listener, threadManager.currentThread); 1.78 + 1.79 + transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0); 1.80 +}); 1.81 + 1.82 +function run_test() { 1.83 + let ioService = Cc["@mozilla.org/network/io-service;1"] 1.84 + .getService(Ci.nsIIOService); 1.85 + 1.86 + gHttpServer.start(-1); 1.87 + 1.88 + let uri = ioService.newURI("http://localhost:" + gHttpServer.identity.primaryPort, 1.89 + null, null); 1.90 + let channel = ioService.newChannelFromURI(uri); 1.91 + 1.92 + channel.open(); 1.93 + 1.94 + gServerSocket.init(-1, true, -1); 1.95 + 1.96 + run_next_test(); 1.97 +} 1.98 +