summaryrefslogtreecommitdiffstats
path: root/devtools/shared/transport/tests/unit/test_dbgsocket_connection_drop.js
blob: 9221939b15d43bf03cc216035cc3007274cfa471 (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
/**
 * 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.
 */

const { RawPacket } = require("devtools/shared/transport/packets");

function run_test() {
  do_print("Starting test at " + new Date().toTimeString());
  initTestDebuggerServer();

  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 = Task.async(function* (payload) {
  let AuthenticatorType = DebuggerServer.Authenticators.get("PROMPT");
  let authenticator = new AuthenticatorType.Server();
  authenticator.allowConnection = () => {
    return DebuggerServer.AuthenticationResult.ALLOW;
  };

  let listener = DebuggerServer.createListener();
  listener.portOrPath = -1;
  listener.authenticator = authenticator;
  listener.open();

  let transport = yield DebuggerClient.socketConnect({
    host: "127.0.0.1",
    port: listener.port
  });
  let closedDeferred = defer();
  transport.hooks = {
    onPacket: function (aPacket) {
      this.onPacket = function (aPacket) {
        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();
    },
    onClosed: function (aStatus) {
      do_check_true(true);
      closedDeferred.resolve();
    },
  };
  transport.ready();
  return closedDeferred.promise;
});