1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/server/tests/unit/test_dbgsocket_connection_drop.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,72 @@ 1.4 +/** 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.7 + */ 1.8 + 1.9 +/** 1.10 + * Bug 755412 - checks if the server drops the connection on an improperly 1.11 + * framed packet, i.e. when the length header is invalid. 1.12 + */ 1.13 + 1.14 +Cu.import("resource://gre/modules/devtools/dbg-server.jsm"); 1.15 +Cu.import("resource://gre/modules/devtools/dbg-client.jsm"); 1.16 + 1.17 +let port = 2929; 1.18 + 1.19 +function run_test() 1.20 +{ 1.21 + do_print("Starting test at " + new Date().toTimeString()); 1.22 + initTestDebuggerServer(); 1.23 + 1.24 + add_test(test_socket_conn_drops_after_invalid_header); 1.25 + add_test(test_socket_conn_drops_after_invalid_header_2); 1.26 + add_test(test_socket_conn_drops_after_too_long_header); 1.27 + run_next_test(); 1.28 +} 1.29 + 1.30 +function test_socket_conn_drops_after_invalid_header() { 1.31 + return test_helper('fluff30:27:{"to":"root","type":"echo"}'); 1.32 +} 1.33 + 1.34 +function test_socket_conn_drops_after_invalid_header_2() { 1.35 + return test_helper('27asd:{"to":"root","type":"echo"}'); 1.36 +} 1.37 + 1.38 +function test_socket_conn_drops_after_too_long_header() { 1.39 + return test_helper('4305724038957487634549823475894325'); 1.40 +} 1.41 + 1.42 + 1.43 +function test_helper(payload) { 1.44 + try_open_listener(); 1.45 + 1.46 + let transport = debuggerSocketConnect("127.0.0.1", port); 1.47 + transport.hooks = { 1.48 + onPacket: function(aPacket) { 1.49 + this.onPacket = function(aPacket) { 1.50 + do_throw(new Error("This connection should be dropped.")); 1.51 + transport.close(); 1.52 + } 1.53 + 1.54 + // Inject the payload directly into the stream. 1.55 + transport._outgoing += payload; 1.56 + transport._flushOutgoing(); 1.57 + }, 1.58 + onClosed: function(aStatus) { 1.59 + do_check_true(true); 1.60 + run_next_test(); 1.61 + }, 1.62 + }; 1.63 + transport.ready(); 1.64 +} 1.65 + 1.66 +function try_open_listener() 1.67 +{ 1.68 + try { 1.69 + do_check_true(DebuggerServer.openListener(port)); 1.70 + } catch (e) { 1.71 + // In case the port is unavailable, pick a random one between 2000 and 65000. 1.72 + port = Math.floor(Math.random() * (65000 - 2000 + 1)) + 2000; 1.73 + try_open_listener(); 1.74 + } 1.75 +}