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

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:4d127b0c702a
1 /**
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
4 */
5
6 /**
7 * Bug 755412 - checks if the server drops the connection on an improperly
8 * framed packet, i.e. when the length header is invalid.
9 */
10
11 Cu.import("resource://gre/modules/devtools/dbg-server.jsm");
12 Cu.import("resource://gre/modules/devtools/dbg-client.jsm");
13
14 let port = 2929;
15
16 function run_test()
17 {
18 do_print("Starting test at " + new Date().toTimeString());
19 initTestDebuggerServer();
20
21 add_test(test_socket_conn_drops_after_invalid_header);
22 add_test(test_socket_conn_drops_after_invalid_header_2);
23 add_test(test_socket_conn_drops_after_too_long_header);
24 run_next_test();
25 }
26
27 function test_socket_conn_drops_after_invalid_header() {
28 return test_helper('fluff30:27:{"to":"root","type":"echo"}');
29 }
30
31 function test_socket_conn_drops_after_invalid_header_2() {
32 return test_helper('27asd:{"to":"root","type":"echo"}');
33 }
34
35 function test_socket_conn_drops_after_too_long_header() {
36 return test_helper('4305724038957487634549823475894325');
37 }
38
39
40 function test_helper(payload) {
41 try_open_listener();
42
43 let transport = debuggerSocketConnect("127.0.0.1", port);
44 transport.hooks = {
45 onPacket: function(aPacket) {
46 this.onPacket = function(aPacket) {
47 do_throw(new Error("This connection should be dropped."));
48 transport.close();
49 }
50
51 // Inject the payload directly into the stream.
52 transport._outgoing += payload;
53 transport._flushOutgoing();
54 },
55 onClosed: function(aStatus) {
56 do_check_true(true);
57 run_next_test();
58 },
59 };
60 transport.ready();
61 }
62
63 function try_open_listener()
64 {
65 try {
66 do_check_true(DebuggerServer.openListener(port));
67 } catch (e) {
68 // In case the port is unavailable, pick a random one between 2000 and 65000.
69 port = Math.floor(Math.random() * (65000 - 2000 + 1)) + 2000;
70 try_open_listener();
71 }
72 }

mercurial