toolkit/devtools/server/tests/unit/test_dbgsocket.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 Cu.import("resource://gre/modules/devtools/dbg-server.jsm");
michael@0 5 Cu.import("resource://gre/modules/devtools/dbg-client.jsm");
michael@0 6
michael@0 7 let port = 2929;
michael@0 8
michael@0 9 function run_test()
michael@0 10 {
michael@0 11 do_print("Starting test at " + new Date().toTimeString());
michael@0 12 initTestDebuggerServer();
michael@0 13
michael@0 14 add_test(test_socket_conn);
michael@0 15 add_test(test_socket_shutdown);
michael@0 16 add_test(test_pipe_conn);
michael@0 17
michael@0 18 run_next_test();
michael@0 19 }
michael@0 20
michael@0 21 function really_long() {
michael@0 22 let ret = "0123456789";
michael@0 23 for (let i = 0; i < 18; i++) {
michael@0 24 ret += ret;
michael@0 25 }
michael@0 26 return ret;
michael@0 27 }
michael@0 28
michael@0 29 function test_socket_conn()
michael@0 30 {
michael@0 31 do_check_eq(DebuggerServer._socketConnections, 0);
michael@0 32 try_open_listener();
michael@0 33 do_print("Debugger server port is " + port);
michael@0 34 do_check_eq(DebuggerServer._socketConnections, 1);
michael@0 35 // Make sure opening the listener twice does nothing.
michael@0 36 do_check_true(DebuggerServer.openListener(port));
michael@0 37 do_check_eq(DebuggerServer._socketConnections, 1);
michael@0 38
michael@0 39 do_print("Starting long and unicode tests at " + new Date().toTimeString());
michael@0 40 let unicodeString = "(╯°□°)╯︵ ┻━┻";
michael@0 41 let transport = debuggerSocketConnect("127.0.0.1", port);
michael@0 42 transport.hooks = {
michael@0 43 onPacket: function(aPacket) {
michael@0 44 this.onPacket = function(aPacket) {
michael@0 45 do_check_eq(aPacket.unicode, unicodeString);
michael@0 46 transport.close();
michael@0 47 }
michael@0 48 // Verify that things work correctly when bigger than the output
michael@0 49 // transport buffers and when transporting unicode...
michael@0 50 transport.send({to: "root",
michael@0 51 type: "echo",
michael@0 52 reallylong: really_long(),
michael@0 53 unicode: unicodeString});
michael@0 54 do_check_eq(aPacket.from, "root");
michael@0 55 },
michael@0 56 onClosed: function(aStatus) {
michael@0 57 run_next_test();
michael@0 58 },
michael@0 59 };
michael@0 60 transport.ready();
michael@0 61 }
michael@0 62
michael@0 63 function test_socket_shutdown()
michael@0 64 {
michael@0 65 do_check_eq(DebuggerServer._socketConnections, 1);
michael@0 66 do_check_true(DebuggerServer.closeListener());
michael@0 67 do_check_eq(DebuggerServer._socketConnections, 0);
michael@0 68 // Make sure closing the listener twice does nothing.
michael@0 69 do_check_false(DebuggerServer.closeListener());
michael@0 70 do_check_eq(DebuggerServer._socketConnections, 0);
michael@0 71
michael@0 72 do_print("Connecting to a server socket at " + new Date().toTimeString());
michael@0 73 let transport = debuggerSocketConnect("127.0.0.1", port);
michael@0 74 transport.hooks = {
michael@0 75 onPacket: function(aPacket) {
michael@0 76 // Shouldn't reach this, should never connect.
michael@0 77 do_check_true(false);
michael@0 78 },
michael@0 79
michael@0 80 onClosed: function(aStatus) {
michael@0 81 do_print("test_socket_shutdown onClosed called at " + new Date().toTimeString());
michael@0 82 // The connection should be refused here, but on slow or overloaded
michael@0 83 // machines it may just time out.
michael@0 84 let expected = [ Cr.NS_ERROR_CONNECTION_REFUSED, Cr.NS_ERROR_NET_TIMEOUT ];
michael@0 85 do_check_neq(expected.indexOf(aStatus), -1);
michael@0 86 run_next_test();
michael@0 87 }
michael@0 88 };
michael@0 89
michael@0 90 // Hack to get more logging for bug 775924.
michael@0 91 transport.onDataAvailable = makeInfallible(function DT_onDataAvailable(aRequest, aContext,
michael@0 92 aStream, aOffset, aCount) {
michael@0 93 do_print("onDataAvailable. offset: "+aOffset+", count: "+aCount);
michael@0 94 let buf = NetUtil.readInputStreamToString(aStream, aStream.available());
michael@0 95 transport._incoming += buf;
michael@0 96 do_print("Read form stream("+buf.length+"): "+buf);
michael@0 97 while (transport._processIncoming()) {
michael@0 98 do_print("Look ma, I'm looping!");
michael@0 99 };
michael@0 100 }, "DebuggerTransport.prototype.onDataAvailable");
michael@0 101
michael@0 102 do_print("Initializing input stream at " + new Date().toTimeString());
michael@0 103 transport.ready();
michael@0 104 }
michael@0 105
michael@0 106 function test_pipe_conn()
michael@0 107 {
michael@0 108 let transport = DebuggerServer.connectPipe();
michael@0 109 transport.hooks = {
michael@0 110 onPacket: function(aPacket) {
michael@0 111 do_check_eq(aPacket.from, "root");
michael@0 112 transport.close();
michael@0 113 },
michael@0 114 onClosed: function(aStatus) {
michael@0 115 run_next_test();
michael@0 116 }
michael@0 117 };
michael@0 118
michael@0 119 transport.ready();
michael@0 120 }
michael@0 121
michael@0 122 function try_open_listener()
michael@0 123 {
michael@0 124 try {
michael@0 125 do_check_true(DebuggerServer.openListener(port));
michael@0 126 } catch (e) {
michael@0 127 // In case the port is unavailable, pick a random one between 2000 and 65000.
michael@0 128 port = Math.floor(Math.random() * (65000 - 2000 + 1)) + 2000;
michael@0 129 try_open_listener();
michael@0 130 }
michael@0 131 }
michael@0 132
michael@0 133 // Copied verbatim from dbg-transport.js.
michael@0 134 // Hack to get more logging for bug 775924.
michael@0 135 function makeInfallible(aHandler, aName) {
michael@0 136 if (!aName)
michael@0 137 aName = aHandler.name;
michael@0 138
michael@0 139 return function (/* arguments */) {
michael@0 140 try {
michael@0 141 return aHandler.apply(this, arguments);
michael@0 142 } catch (ex) {
michael@0 143 let msg = "Handler function ";
michael@0 144 if (aName) {
michael@0 145 msg += aName + " ";
michael@0 146 }
michael@0 147 msg += "threw an exception: " + DevToolsUtils.safeErrorString(ex);
michael@0 148 if (ex.stack) {
michael@0 149 msg += "\nCall stack:\n" + ex.stack;
michael@0 150 }
michael@0 151
michael@0 152 do_print(msg + "\n");
michael@0 153
michael@0 154 if (Cu.reportError) {
michael@0 155 /*
michael@0 156 * Note that the xpcshell test harness registers an observer for
michael@0 157 * console messages, so when we're running tests, this will cause
michael@0 158 * the test to quit.
michael@0 159 */
michael@0 160 Cu.reportError(msg);
michael@0 161 }
michael@0 162 return undefined;
michael@0 163 }
michael@0 164 }
michael@0 165 }

mercurial