test_dbgsocket_connection_drop.js (2504B)
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 "use strict"; 11 12 const { 13 RawPacket, 14 } = require("resource://devtools/shared/transport/packets.js"); 15 16 function run_test() { 17 info("Starting test at " + new Date().toTimeString()); 18 initTestDevToolsServer(); 19 20 add_task(test_socket_conn_drops_after_invalid_header); 21 add_task(test_socket_conn_drops_after_invalid_header_2); 22 add_task(test_socket_conn_drops_after_too_large_length); 23 add_task(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_large_length() { 36 // Packet length is limited (semi-arbitrarily) to 1 TiB (2^40) 37 return test_helper("4305724038957487634549823475894325:"); 38 } 39 40 function test_socket_conn_drops_after_too_long_header() { 41 // The packet header is currently limited to no more than 200 bytes 42 let rawPacket = "4305724038957487634549823475894325"; 43 for (let i = 0; i < 8; i++) { 44 rawPacket += rawPacket; 45 } 46 return test_helper(rawPacket + ":"); 47 } 48 49 var test_helper = async function (payload) { 50 const AuthenticatorType = DevToolsServer.Authenticators.get("PROMPT"); 51 const authenticator = new AuthenticatorType.Server(); 52 authenticator.allowConnection = () => { 53 return DevToolsServer.AuthenticationResult.ALLOW; 54 }; 55 const socketOptions = { 56 authenticator, 57 portOrPath: -1, 58 }; 59 60 const listener = new SocketListener(DevToolsServer, socketOptions); 61 listener.open(); 62 63 const transport = await DevToolsClient.socketConnect({ 64 host: "127.0.0.1", 65 port: listener.port, 66 }); 67 return new Promise(resolve => { 68 transport.hooks = { 69 onPacket() { 70 this.onPacket = function () { 71 do_throw(new Error("This connection should be dropped.")); 72 transport.close(); 73 }; 74 75 // Inject the payload directly into the stream. 76 transport._outgoing.push(new RawPacket(transport, payload)); 77 transport._flushOutgoing(); 78 }, 79 onTransportClosed() { 80 Assert.ok(true); 81 resolve(); 82 }, 83 }; 84 transport.ready(); 85 }); 86 };