summaryrefslogtreecommitdiffstats
path: root/devtools/shared/transport/tests/xpcshell/test_dbgsocket_connection_drop.js
blob: e08c2380fb4a8df11cb095fc30cee7534fab4df2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/**
 * Bug 755412 - checks if the server drops the connection on an improperly
 * framed packet, i.e. when the length header is invalid.
 */
"use strict";

const {
  RawPacket,
} = require("resource://devtools/shared/transport/packets.js");

function run_test() {
  info("Starting test at " + new Date().toTimeString());
  initTestDevToolsServer();

  add_task(test_socket_conn_drops_after_invalid_header);
  add_task(test_socket_conn_drops_after_invalid_header_2);
  add_task(test_socket_conn_drops_after_too_large_length);
  add_task(test_socket_conn_drops_after_too_long_header);
  run_next_test();
}

function test_socket_conn_drops_after_invalid_header() {
  return test_helper('fluff30:27:{"to":"root","type":"echo"}');
}

function test_socket_conn_drops_after_invalid_header_2() {
  return test_helper('27asd:{"to":"root","type":"echo"}');
}

function test_socket_conn_drops_after_too_large_length() {
  // Packet length is limited (semi-arbitrarily) to 1 TiB (2^40)
  return test_helper("4305724038957487634549823475894325:");
}

function test_socket_conn_drops_after_too_long_header() {
  // The packet header is currently limited to no more than 200 bytes
  let rawPacket = "4305724038957487634549823475894325";
  for (let i = 0; i < 8; i++) {
    rawPacket += rawPacket;
  }
  return test_helper(rawPacket + ":");
}

var test_helper = async function (payload) {
  const AuthenticatorType = DevToolsServer.Authenticators.get("PROMPT");
  const authenticator = new AuthenticatorType.Server();
  authenticator.allowConnection = () => {
    return DevToolsServer.AuthenticationResult.ALLOW;
  };
  const socketOptions = {
    authenticator,
    portOrPath: -1,
  };

  const listener = new SocketListener(DevToolsServer, socketOptions);
  listener.open();

  const transport = await DevToolsClient.socketConnect({
    host: "127.0.0.1",
    port: listener.port,
  });
  return new Promise(resolve => {
    transport.hooks = {
      onPacket(packet) {
        this.onPacket = function () {
          do_throw(new Error("This connection should be dropped."));
          transport.close();
        };

        // Inject the payload directly into the stream.
        transport._outgoing.push(new RawPacket(transport, payload));
        transport._flushOutgoing();
      },
      onTransportClosed(status) {
        Assert.ok(true);
        resolve();
      },
    };
    transport.ready();
  });
};