1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/network/tests/unit/test_multisend.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,152 @@ 1.4 +const Cc = Components.classes; 1.5 +const Ci = Components.interfaces; 1.6 +const Cr = Components.results; 1.7 +const Cu = Components.utils; 1.8 +const CC = Components.Constructor; 1.9 + 1.10 +Cu.import("resource://gre/modules/Services.jsm"); 1.11 + 1.12 +const ServerSocket = CC("@mozilla.org/network/server-socket;1", 1.13 + "nsIServerSocket", 1.14 + "init"), 1.15 + InputStreamPump = CC("@mozilla.org/network/input-stream-pump;1", 1.16 + "nsIInputStreamPump", 1.17 + "init"), 1.18 + BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", 1.19 + "nsIBinaryInputStream", 1.20 + "setInputStream"), 1.21 + BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1", 1.22 + "nsIBinaryOutputStream", 1.23 + "setOutputStream"), 1.24 + TCPSocket = new (CC("@mozilla.org/tcp-socket;1", 1.25 + "nsIDOMTCPSocket"))(); 1.26 + 1.27 +var server = null, sock = null; 1.28 + 1.29 +/** 1.30 + * Spin up a listening socket and associate at most one live, accepted socket 1.31 + * with ourselves. 1.32 + */ 1.33 +function TestServer() { 1.34 + this.listener = ServerSocket(-1, true, -1); 1.35 + do_print('server: listening on', this.listener.port); 1.36 + this.listener.asyncListen(this); 1.37 + 1.38 + this.binaryInput = null; 1.39 + this.input = null; 1.40 + this.binaryOutput = null; 1.41 + this.output = null; 1.42 + 1.43 + this.onaccept = null; 1.44 + this.ondata = null; 1.45 + this.onclose = null; 1.46 +} 1.47 + 1.48 +TestServer.prototype = { 1.49 + onSocketAccepted: function(socket, trans) { 1.50 + if (this.input) 1.51 + do_throw("More than one live connection!?"); 1.52 + 1.53 + do_print('server: got client connection'); 1.54 + this.input = trans.openInputStream(0, 0, 0); 1.55 + this.binaryInput = new BinaryInputStream(this.input); 1.56 + this.output = trans.openOutputStream(0, 0, 0); 1.57 + this.binaryOutput = new BinaryOutputStream(this.output); 1.58 + 1.59 + new InputStreamPump(this.input, -1, -1, 0, 0, false).asyncRead(this, null); 1.60 + 1.61 + if (this.onaccept) 1.62 + this.onaccept(); 1.63 + else 1.64 + do_throw("Received unexpected connection!"); 1.65 + }, 1.66 + 1.67 + onStopListening: function(socket) { 1.68 + }, 1.69 + 1.70 + onDataAvailable: function(request, context, inputStream, offset, count) { 1.71 + var readData = this.binaryInput.readByteArray(count); 1.72 + if (this.ondata) { 1.73 + try { 1.74 + this.ondata(readData); 1.75 + } catch(ex) { 1.76 + // re-throw if this is from do_throw 1.77 + if (ex === Cr.NS_ERROR_ABORT) 1.78 + throw ex; 1.79 + // log if there was a test problem 1.80 + do_print('Caught exception: ' + ex + '\n' + ex.stack); 1.81 + do_throw('test is broken; bad ondata handler; see above'); 1.82 + } 1.83 + } else { 1.84 + do_throw('Received ' + count + ' bytes of unexpected data!'); 1.85 + } 1.86 + }, 1.87 + 1.88 + onStartRequest: function(request, context) { 1.89 + }, 1.90 + 1.91 + onStopRequest: function(request, context, status) { 1.92 + if (this.onclose) 1.93 + this.onclose(); 1.94 + else 1.95 + do_throw("Received unexpected close!"); 1.96 + }, 1.97 + 1.98 + close: function() { 1.99 + this.binaryInput.close(); 1.100 + this.binaryOutput.close(); 1.101 + }, 1.102 + 1.103 + /** 1.104 + * Forget about the socket we knew about before. 1.105 + */ 1.106 + reset: function() { 1.107 + this.binaryInput = null; 1.108 + this.input = null; 1.109 + this.binaryOutput = null; 1.110 + this.output = null; 1.111 + }, 1.112 +}; 1.113 + 1.114 +function run_test() { 1.115 + Services.prefs.setBoolPref('dom.mozTCPSocket.enabled', true); 1.116 + 1.117 + do_test_pending(); 1.118 + 1.119 + server = new TestServer(); 1.120 + server.reset(); 1.121 + sock = TCPSocket.open( 1.122 + '127.0.0.1', server.listener.port, 1.123 + { binaryType: 'arraybuffer' }); 1.124 + 1.125 + var encoder = new TextEncoder(); 1.126 + var ok = encoder.encode("OKBYE"); 1.127 + 1.128 + var expected = ['O', 'K', 'B', 'Y', 'E'] 1.129 + .map(function(c) { return c.charCodeAt(0); }); 1.130 + var seenData = []; 1.131 + 1.132 + server.onaccept = function() {}; 1.133 + server.ondata = function(data) { 1.134 + do_print(data + ":" + data.length); 1.135 + 1.136 + seenData = seenData.concat(data); 1.137 + 1.138 + if (seenData.length == expected.length) { 1.139 + do_print(expected); 1.140 + do_check_eq(seenData.length, expected.length); 1.141 + for (var i = 0; i < seenData.length; i++) { 1.142 + do_check_eq(seenData[i], expected[i]); 1.143 + } 1.144 + sock.close(); 1.145 + server.close(); 1.146 + do_test_finished(); 1.147 + } 1.148 + }; 1.149 + server.onclose = function() {}; 1.150 + 1.151 + sock.onopen = function() { 1.152 + sock.send(ok.buffer, 0, 2); 1.153 + sock.send(ok.buffer, 2, 3); 1.154 + }; 1.155 +} 1.156 \ No newline at end of file