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