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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/server/tests/unit/test_dbgsocket.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,165 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Cu.import("resource://gre/modules/devtools/dbg-server.jsm");
     1.8 +Cu.import("resource://gre/modules/devtools/dbg-client.jsm");
     1.9 +
    1.10 +let port = 2929;
    1.11 +
    1.12 +function run_test()
    1.13 +{
    1.14 +  do_print("Starting test at " + new Date().toTimeString());
    1.15 +  initTestDebuggerServer();
    1.16 +
    1.17 +  add_test(test_socket_conn);
    1.18 +  add_test(test_socket_shutdown);
    1.19 +  add_test(test_pipe_conn);
    1.20 +
    1.21 +  run_next_test();
    1.22 +}
    1.23 +
    1.24 +function really_long() {
    1.25 +  let ret = "0123456789";
    1.26 +  for (let i = 0; i < 18; i++) {
    1.27 +    ret += ret;
    1.28 +  }
    1.29 +  return ret;
    1.30 +}
    1.31 +
    1.32 +function test_socket_conn()
    1.33 +{
    1.34 +  do_check_eq(DebuggerServer._socketConnections, 0);
    1.35 +  try_open_listener();
    1.36 +  do_print("Debugger server port is " + port);
    1.37 +  do_check_eq(DebuggerServer._socketConnections, 1);
    1.38 +  // Make sure opening the listener twice does nothing.
    1.39 +  do_check_true(DebuggerServer.openListener(port));
    1.40 +  do_check_eq(DebuggerServer._socketConnections, 1);
    1.41 +
    1.42 +  do_print("Starting long and unicode tests at " + new Date().toTimeString());
    1.43 +  let unicodeString = "(╯°□°)╯︵ ┻━┻";
    1.44 +  let transport = debuggerSocketConnect("127.0.0.1", port);
    1.45 +  transport.hooks = {
    1.46 +    onPacket: function(aPacket) {
    1.47 +      this.onPacket = function(aPacket) {
    1.48 +        do_check_eq(aPacket.unicode, unicodeString);
    1.49 +        transport.close();
    1.50 +      }
    1.51 +      // Verify that things work correctly when bigger than the output
    1.52 +      // transport buffers and when transporting unicode...
    1.53 +      transport.send({to: "root",
    1.54 +                      type: "echo",
    1.55 +                      reallylong: really_long(),
    1.56 +                      unicode: unicodeString});
    1.57 +      do_check_eq(aPacket.from, "root");
    1.58 +    },
    1.59 +    onClosed: function(aStatus) {
    1.60 +      run_next_test();
    1.61 +    },
    1.62 +  };
    1.63 +  transport.ready();
    1.64 +}
    1.65 +
    1.66 +function test_socket_shutdown()
    1.67 +{
    1.68 +  do_check_eq(DebuggerServer._socketConnections, 1);
    1.69 +  do_check_true(DebuggerServer.closeListener());
    1.70 +  do_check_eq(DebuggerServer._socketConnections, 0);
    1.71 +  // Make sure closing the listener twice does nothing.
    1.72 +  do_check_false(DebuggerServer.closeListener());
    1.73 +  do_check_eq(DebuggerServer._socketConnections, 0);
    1.74 +
    1.75 +  do_print("Connecting to a server socket at " + new Date().toTimeString());
    1.76 +  let transport = debuggerSocketConnect("127.0.0.1", port);
    1.77 +  transport.hooks = {
    1.78 +    onPacket: function(aPacket) {
    1.79 +      // Shouldn't reach this, should never connect.
    1.80 +      do_check_true(false);
    1.81 +    },
    1.82 +
    1.83 +    onClosed: function(aStatus) {
    1.84 +      do_print("test_socket_shutdown onClosed called at " + new Date().toTimeString());
    1.85 +      // The connection should be refused here, but on slow or overloaded
    1.86 +      // machines it may just time out.
    1.87 +      let expected = [ Cr.NS_ERROR_CONNECTION_REFUSED, Cr.NS_ERROR_NET_TIMEOUT ];
    1.88 +      do_check_neq(expected.indexOf(aStatus), -1);
    1.89 +      run_next_test();
    1.90 +    }
    1.91 +  };
    1.92 +
    1.93 +  // Hack to get more logging for bug 775924.
    1.94 +  transport.onDataAvailable = makeInfallible(function DT_onDataAvailable(aRequest, aContext,
    1.95 +                                             aStream, aOffset, aCount) {
    1.96 +    do_print("onDataAvailable. offset: "+aOffset+", count: "+aCount);
    1.97 +    let buf = NetUtil.readInputStreamToString(aStream, aStream.available());
    1.98 +    transport._incoming += buf;
    1.99 +    do_print("Read form stream("+buf.length+"): "+buf);
   1.100 +    while (transport._processIncoming()) {
   1.101 +      do_print("Look ma, I'm looping!");
   1.102 +    };
   1.103 +  }, "DebuggerTransport.prototype.onDataAvailable");
   1.104 +
   1.105 +  do_print("Initializing input stream at " + new Date().toTimeString());
   1.106 +  transport.ready();
   1.107 +}
   1.108 +
   1.109 +function test_pipe_conn()
   1.110 +{
   1.111 +  let transport = DebuggerServer.connectPipe();
   1.112 +  transport.hooks = {
   1.113 +    onPacket: function(aPacket) {
   1.114 +      do_check_eq(aPacket.from, "root");
   1.115 +      transport.close();
   1.116 +    },
   1.117 +    onClosed: function(aStatus) {
   1.118 +      run_next_test();
   1.119 +    }
   1.120 +  };
   1.121 +
   1.122 +  transport.ready();
   1.123 +}
   1.124 +
   1.125 +function try_open_listener()
   1.126 +{
   1.127 +  try {
   1.128 +    do_check_true(DebuggerServer.openListener(port));
   1.129 +  } catch (e) {
   1.130 +    // In case the port is unavailable, pick a random one between 2000 and 65000.
   1.131 +    port = Math.floor(Math.random() * (65000 - 2000 + 1)) + 2000;
   1.132 +    try_open_listener();
   1.133 +  }
   1.134 +}
   1.135 +
   1.136 +// Copied verbatim from dbg-transport.js.
   1.137 +// Hack to get more logging for bug 775924.
   1.138 +function makeInfallible(aHandler, aName) {
   1.139 +  if (!aName)
   1.140 +    aName = aHandler.name;
   1.141 +
   1.142 +  return function (/* arguments */) {
   1.143 +    try {
   1.144 +      return aHandler.apply(this, arguments);
   1.145 +    } catch (ex) {
   1.146 +      let msg = "Handler function ";
   1.147 +      if (aName) {
   1.148 +        msg += aName + " ";
   1.149 +      }
   1.150 +      msg += "threw an exception: " + DevToolsUtils.safeErrorString(ex);
   1.151 +      if (ex.stack) {
   1.152 +        msg += "\nCall stack:\n" + ex.stack;
   1.153 +      }
   1.154 +
   1.155 +      do_print(msg + "\n");
   1.156 +
   1.157 +      if (Cu.reportError) {
   1.158 +        /*
   1.159 +         * Note that the xpcshell test harness registers an observer for
   1.160 +         * console messages, so when we're running tests, this will cause
   1.161 +         * the test to quit.
   1.162 +         */
   1.163 +        Cu.reportError(msg);
   1.164 +      }
   1.165 +      return undefined;
   1.166 +    }
   1.167 +  }
   1.168 +}

mercurial