Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | const Cc = Components.classes; |
michael@0 | 2 | const Ci = Components.interfaces; |
michael@0 | 3 | const Cr = Components.results; |
michael@0 | 4 | const Cu = Components.utils; |
michael@0 | 5 | const CC = Components.Constructor; |
michael@0 | 6 | |
michael@0 | 7 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 8 | |
michael@0 | 9 | const ServerSocket = CC("@mozilla.org/network/server-socket;1", |
michael@0 | 10 | "nsIServerSocket", |
michael@0 | 11 | "init"), |
michael@0 | 12 | InputStreamPump = CC("@mozilla.org/network/input-stream-pump;1", |
michael@0 | 13 | "nsIInputStreamPump", |
michael@0 | 14 | "init"), |
michael@0 | 15 | BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", |
michael@0 | 16 | "nsIBinaryInputStream", |
michael@0 | 17 | "setInputStream"), |
michael@0 | 18 | BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1", |
michael@0 | 19 | "nsIBinaryOutputStream", |
michael@0 | 20 | "setOutputStream"), |
michael@0 | 21 | TCPSocket = new (CC("@mozilla.org/tcp-socket;1", |
michael@0 | 22 | "nsIDOMTCPSocket"))(); |
michael@0 | 23 | |
michael@0 | 24 | var server = null, sock = null; |
michael@0 | 25 | |
michael@0 | 26 | /** |
michael@0 | 27 | * Spin up a listening socket and associate at most one live, accepted socket |
michael@0 | 28 | * with ourselves. |
michael@0 | 29 | */ |
michael@0 | 30 | function TestServer() { |
michael@0 | 31 | this.listener = ServerSocket(-1, true, -1); |
michael@0 | 32 | do_print('server: listening on', this.listener.port); |
michael@0 | 33 | this.listener.asyncListen(this); |
michael@0 | 34 | |
michael@0 | 35 | this.binaryInput = null; |
michael@0 | 36 | this.input = null; |
michael@0 | 37 | this.binaryOutput = null; |
michael@0 | 38 | this.output = null; |
michael@0 | 39 | |
michael@0 | 40 | this.onaccept = null; |
michael@0 | 41 | this.ondata = null; |
michael@0 | 42 | this.onclose = null; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | TestServer.prototype = { |
michael@0 | 46 | onSocketAccepted: function(socket, trans) { |
michael@0 | 47 | if (this.input) |
michael@0 | 48 | do_throw("More than one live connection!?"); |
michael@0 | 49 | |
michael@0 | 50 | do_print('server: got client connection'); |
michael@0 | 51 | this.input = trans.openInputStream(0, 0, 0); |
michael@0 | 52 | this.binaryInput = new BinaryInputStream(this.input); |
michael@0 | 53 | this.output = trans.openOutputStream(0, 0, 0); |
michael@0 | 54 | this.binaryOutput = new BinaryOutputStream(this.output); |
michael@0 | 55 | |
michael@0 | 56 | new InputStreamPump(this.input, -1, -1, 0, 0, false).asyncRead(this, null); |
michael@0 | 57 | |
michael@0 | 58 | if (this.onaccept) |
michael@0 | 59 | this.onaccept(); |
michael@0 | 60 | else |
michael@0 | 61 | do_throw("Received unexpected connection!"); |
michael@0 | 62 | }, |
michael@0 | 63 | |
michael@0 | 64 | onStopListening: function(socket) { |
michael@0 | 65 | }, |
michael@0 | 66 | |
michael@0 | 67 | onDataAvailable: function(request, context, inputStream, offset, count) { |
michael@0 | 68 | var readData = this.binaryInput.readByteArray(count); |
michael@0 | 69 | if (this.ondata) { |
michael@0 | 70 | try { |
michael@0 | 71 | this.ondata(readData); |
michael@0 | 72 | } catch(ex) { |
michael@0 | 73 | // re-throw if this is from do_throw |
michael@0 | 74 | if (ex === Cr.NS_ERROR_ABORT) |
michael@0 | 75 | throw ex; |
michael@0 | 76 | // log if there was a test problem |
michael@0 | 77 | do_print('Caught exception: ' + ex + '\n' + ex.stack); |
michael@0 | 78 | do_throw('test is broken; bad ondata handler; see above'); |
michael@0 | 79 | } |
michael@0 | 80 | } else { |
michael@0 | 81 | do_throw('Received ' + count + ' bytes of unexpected data!'); |
michael@0 | 82 | } |
michael@0 | 83 | }, |
michael@0 | 84 | |
michael@0 | 85 | onStartRequest: function(request, context) { |
michael@0 | 86 | }, |
michael@0 | 87 | |
michael@0 | 88 | onStopRequest: function(request, context, status) { |
michael@0 | 89 | if (this.onclose) |
michael@0 | 90 | this.onclose(); |
michael@0 | 91 | else |
michael@0 | 92 | do_throw("Received unexpected close!"); |
michael@0 | 93 | }, |
michael@0 | 94 | |
michael@0 | 95 | close: function() { |
michael@0 | 96 | this.binaryInput.close(); |
michael@0 | 97 | this.binaryOutput.close(); |
michael@0 | 98 | }, |
michael@0 | 99 | |
michael@0 | 100 | /** |
michael@0 | 101 | * Forget about the socket we knew about before. |
michael@0 | 102 | */ |
michael@0 | 103 | reset: function() { |
michael@0 | 104 | this.binaryInput = null; |
michael@0 | 105 | this.input = null; |
michael@0 | 106 | this.binaryOutput = null; |
michael@0 | 107 | this.output = null; |
michael@0 | 108 | }, |
michael@0 | 109 | }; |
michael@0 | 110 | |
michael@0 | 111 | function run_test() { |
michael@0 | 112 | Services.prefs.setBoolPref('dom.mozTCPSocket.enabled', true); |
michael@0 | 113 | |
michael@0 | 114 | do_test_pending(); |
michael@0 | 115 | |
michael@0 | 116 | server = new TestServer(); |
michael@0 | 117 | server.reset(); |
michael@0 | 118 | sock = TCPSocket.open( |
michael@0 | 119 | '127.0.0.1', server.listener.port, |
michael@0 | 120 | { binaryType: 'arraybuffer' }); |
michael@0 | 121 | |
michael@0 | 122 | var encoder = new TextEncoder(); |
michael@0 | 123 | var ok = encoder.encode("OKBYE"); |
michael@0 | 124 | |
michael@0 | 125 | var expected = ['O', 'K', 'B', 'Y', 'E'] |
michael@0 | 126 | .map(function(c) { return c.charCodeAt(0); }); |
michael@0 | 127 | var seenData = []; |
michael@0 | 128 | |
michael@0 | 129 | server.onaccept = function() {}; |
michael@0 | 130 | server.ondata = function(data) { |
michael@0 | 131 | do_print(data + ":" + data.length); |
michael@0 | 132 | |
michael@0 | 133 | seenData = seenData.concat(data); |
michael@0 | 134 | |
michael@0 | 135 | if (seenData.length == expected.length) { |
michael@0 | 136 | do_print(expected); |
michael@0 | 137 | do_check_eq(seenData.length, expected.length); |
michael@0 | 138 | for (var i = 0; i < seenData.length; i++) { |
michael@0 | 139 | do_check_eq(seenData[i], expected[i]); |
michael@0 | 140 | } |
michael@0 | 141 | sock.close(); |
michael@0 | 142 | server.close(); |
michael@0 | 143 | do_test_finished(); |
michael@0 | 144 | } |
michael@0 | 145 | }; |
michael@0 | 146 | server.onclose = function() {}; |
michael@0 | 147 | |
michael@0 | 148 | sock.onopen = function() { |
michael@0 | 149 | sock.send(ok.buffer, 0, 2); |
michael@0 | 150 | sock.send(ok.buffer, 2, 3); |
michael@0 | 151 | }; |
michael@0 | 152 | } |