netwerk/test/unit/test_about_networking.js

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* -*- Mode: Javasript; indent-tab-mode: nil; js-indent-level: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 Cu.import("resource://testing-common/httpd.js");
michael@0 7
michael@0 8 const gDashboard = Cc['@mozilla.org/network/dashboard;1']
michael@0 9 .getService(Ci.nsIDashboard);
michael@0 10
michael@0 11 const gServerSocket = Components.classes["@mozilla.org/network/server-socket;1"]
michael@0 12 .createInstance(Components.interfaces.nsIServerSocket);
michael@0 13 const gHttpServer = new HttpServer();
michael@0 14
michael@0 15 add_test(function test_http() {
michael@0 16 gDashboard.requestHttpConnections(function(data) {
michael@0 17 let found = false;
michael@0 18 for (let i = 0; i < data.connections.length; i++) {
michael@0 19 if (data.connections[i].host == "localhost") {
michael@0 20 found = true;
michael@0 21 break;
michael@0 22 }
michael@0 23 }
michael@0 24 do_check_eq(found, true);
michael@0 25
michael@0 26 run_next_test();
michael@0 27 });
michael@0 28 });
michael@0 29
michael@0 30 add_test(function test_dns() {
michael@0 31 gDashboard.requestDNSInfo(function(data) {
michael@0 32 let found = false;
michael@0 33 for (let i = 0; i < data.entries.length; i++) {
michael@0 34 if (data.entries[i].hostname == "localhost") {
michael@0 35 found = true;
michael@0 36 break;
michael@0 37 }
michael@0 38 }
michael@0 39 do_check_eq(found, true);
michael@0 40
michael@0 41 do_test_pending();
michael@0 42 gHttpServer.stop(do_test_finished);
michael@0 43
michael@0 44 run_next_test();
michael@0 45 });
michael@0 46 });
michael@0 47
michael@0 48 add_test(function test_sockets() {
michael@0 49 let sts = Cc["@mozilla.org/network/socket-transport-service;1"]
michael@0 50 .getService(Ci.nsISocketTransportService);
michael@0 51 let threadManager = Cc["@mozilla.org/thread-manager;1"].getService();
michael@0 52
michael@0 53 let transport = sts.createTransport(null, 0, "127.0.0.1",
michael@0 54 gServerSocket.port, null);
michael@0 55 let listener = {
michael@0 56 onTransportStatus: function(aTransport, aStatus, aProgress, aProgressMax) {
michael@0 57 if (aStatus == Ci.nsISocketTransport.STATUS_CONNECTED_TO) {
michael@0 58 gDashboard.requestSockets(function(data) {
michael@0 59 gServerSocket.close();
michael@0 60 let found = false;
michael@0 61 for (let i = 0; i < data.sockets.length; i++) {
michael@0 62 if (data.sockets[i].host == "127.0.0.1") {
michael@0 63 found = true;
michael@0 64 break;
michael@0 65 }
michael@0 66 }
michael@0 67 do_check_eq(found, true);
michael@0 68
michael@0 69 run_next_test();
michael@0 70 });
michael@0 71 }
michael@0 72 }
michael@0 73 };
michael@0 74 transport.setEventSink(listener, threadManager.currentThread);
michael@0 75
michael@0 76 transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
michael@0 77 });
michael@0 78
michael@0 79 function run_test() {
michael@0 80 let ioService = Cc["@mozilla.org/network/io-service;1"]
michael@0 81 .getService(Ci.nsIIOService);
michael@0 82
michael@0 83 gHttpServer.start(-1);
michael@0 84
michael@0 85 let uri = ioService.newURI("http://localhost:" + gHttpServer.identity.primaryPort,
michael@0 86 null, null);
michael@0 87 let channel = ioService.newChannelFromURI(uri);
michael@0 88
michael@0 89 channel.open();
michael@0 90
michael@0 91 gServerSocket.init(-1, true, -1);
michael@0 92
michael@0 93 run_next_test();
michael@0 94 }
michael@0 95

mercurial