Created
May 31, 2012 00:05
-
-
Save fzzzy/2839696 to your computer and use it in GitHub Desktop.
tcpsocket wip 0.7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/b2g/Makefile.in b/b2g/Makefile.in | |
--- a/b2g/Makefile.in | |
+++ b/b2g/Makefile.in | |
@@ -6,10 +6,12 @@ | |
topsrcdir = @top_srcdir@ | |
srcdir = @srcdir@ | |
VPATH = @srcdir@ | |
include $(DEPTH)/config/autoconf.mk | |
DIRS = chrome components locales app | |
+TEST_DIRS += test | |
+ | |
include $(topsrcdir)/config/rules.mk | |
include $(topsrcdir)/testing/testsuite-targets.mk | |
diff --git a/b2g/chrome/content/shell.js b/b2g/chrome/content/shell.js | |
--- a/b2g/chrome/content/shell.js | |
+++ b/b2g/chrome/content/shell.js | |
@@ -49,17 +49,17 @@ | |
// until we have a proper security model, add some rights to | |
// the pre-installed web applications | |
// XXX never grant 'content-camera' to non-gaia apps | |
function addPermissions(urls) { | |
let permissions = [ | |
'indexedDB', 'indexedDB-unlimited', 'webapps-manage', 'offline-app', 'pin-app', | |
'websettings-read', 'websettings-readwrite', | |
'content-camera', 'webcontacts-manage', 'wifi-manage', 'desktop-notification', | |
- 'geolocation', 'device-storage' | |
+ 'geolocation', 'device-storage', 'tcp-socket' | |
]; | |
urls.forEach(function(url) { | |
url = url.trim(); | |
let uri = Services.io.newURI(url, null, null); | |
let allow = Ci.nsIPermissionManager.ALLOW_ACTION; | |
permissions.forEach(function(permission) { | |
Services.perms.add(uri, permission, allow); | |
diff --git a/b2g/components/B2GComponents.manifest b/b2g/components/B2GComponents.manifest | |
--- a/b2g/components/B2GComponents.manifest | |
+++ b/b2g/components/B2GComponents.manifest | |
@@ -24,8 +24,14 @@ | |
component {397a7fdf-2254-47be-b74e-76625a1a66d5} MozKeyboard.js | |
contract @mozilla.org/b2g-keyboard;1 {397a7fdf-2254-47be-b74e-76625a1a66d5} | |
category JavaScript-navigator-property mozKeyboard @mozilla.org/b2g-keyboard;1 | |
# DirectoryProvider.js | |
component {9181eb7c-6f87-11e1-90b1-4f59d80dd2e5} DirectoryProvider.js | |
contract @mozilla.org/browser/directory-provider;1 {9181eb7c-6f87-11e1-90b1-4f59d80dd2e5} | |
category xpcom-directory-providers browser-directory-provider @mozilla.org/browser/directory-provider;1 | |
+ | |
+# TCPSocket.js | |
+component {cda91b22-6472-11e1-aa11-834fec09cd0a} TCPSocket.js | |
+contract @mozilla.org/tcp-socket;1 {cda91b22-6472-11e1-aa11-834fec09cd0a} | |
+category JavaScript-global-property MozTCPSocket @mozilla.org/tcp-socket;1 | |
+ | |
diff --git a/b2g/components/Makefile.in b/b2g/components/Makefile.in | |
--- a/b2g/components/Makefile.in | |
+++ b/b2g/components/Makefile.in | |
@@ -9,18 +9,26 @@ | |
include $(DEPTH)/config/autoconf.mk | |
MODULE = B2GComponents | |
XPIDL_MODULE = B2GComponents | |
XPIDLSRCS = \ | |
b2g.idl \ | |
+ TCPSocket.idl \ | |
$(NULL) | |
+EXTRA_COMPONENTS = \ | |
+ TCPSocket.js \ | |
+ $(NULL) | |
+ | |
+# Only put things in _PP_ if they need to be preprocessed. Preprocessing means | |
+# we can't just symlink to the source file so you have to run make every time | |
+# you update the files. | |
EXTRA_PP_COMPONENTS = \ | |
B2GComponents.manifest \ | |
CameraContent.js \ | |
AlertsService.js \ | |
ContentPermissionPrompt.js \ | |
MozKeyboard.js \ | |
DirectoryProvider.js \ | |
$(NULL) | |
diff --git a/b2g/components/TCPSocket.idl b/b2g/components/TCPSocket.idl | |
new file mode 100644 | |
--- /dev/null | |
+++ b/b2g/components/TCPSocket.idl | |
@@ -0,0 +1,213 @@ | |
+/* This Source Code Form is subject to the terms of the Mozilla Public | |
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file, | |
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
+ | |
+#include "domstubs.idl" | |
+#include "nsIDOMEvent.idl" | |
+ | |
+// Bug 731746 - Allow chrome JS object to implement nsIDOMEventTarget | |
+// nsITCPSocket should be an nsIEventTarget but js objects | |
+// cannot be an nsIEventTarget yet | |
+// #include "nsIEventTarget.idl" | |
+ | |
+/** | |
+ * MozTCPSocket exposes a TCP client socket (no server sockets yet) | |
+ * to highly privileged apps. It provides a buffered, non-blocking | |
+ * interface for sending. For receiving, it uses an asynchronous, | |
+ * event handler based interface. | |
+ */ | |
+ | |
+// Bug 723206 - Constructors implemented in JS from IDL should be | |
+// allowed to have arguments | |
+// | |
+// Once bug 723206 will be fixed, this method could be replaced by | |
+// arguments when instantiating a TCPSocket object. For example it will | |
+// be possible to do (similarly to the WebSocket API): | |
+// var s = new MozTCPSocket(host, port); | |
+ | |
+[scriptable, uuid(b82e17da-6476-11e1-8813-57a2ffe9e42c)] | |
+interface nsIDOMTCPSocket : nsISupports | |
+{ | |
+ /** | |
+ * Create and return a socket object which will attempt to connect to | |
+ * the given host and port. | |
+ * | |
+ * @param host The hostname of the server to connect to. | |
+ * @param port The port to connect to. | |
+ * @param options An object specifying one or more parameters which | |
+ * determine the details of the socket. | |
+ * | |
+ * useSSL: true to create an SSL socket. Defaults to false. | |
+ * | |
+ * binaryType: "arraybuffer" to use UInt8 array | |
+ * instances in the ondata callback and as the argument | |
+ * to send. Defaults to "string", to use JavaScript strings. | |
+ * | |
+ * @return The new TCPSocket instance. | |
+ */ | |
+ nsIDOMTCPSocket open(in DOMString host, in unsigned short port, [optional] in jsval options); | |
+ | |
+ /** | |
+ * The host of this socket object. | |
+ */ | |
+ readonly attribute DOMString host; | |
+ | |
+ /** | |
+ * The port of this socket object. | |
+ */ | |
+ readonly attribute unsigned short port; | |
+ | |
+ /** | |
+ * True if this socket object is an SSL socket. | |
+ */ | |
+ readonly attribute boolean ssl; | |
+ | |
+ /** | |
+ * The number of bytes which have previously been buffered by calls to | |
+ * send on this socket. | |
+ */ | |
+ readonly attribute unsigned long bufferedAmount; | |
+ | |
+ /** | |
+ * Pause reading incoming data and invocations of the ondata handler until | |
+ * resume is called. | |
+ */ | |
+ void suspend(); | |
+ | |
+ /** | |
+ * Resume reading incoming data and invoking ondata as usual. | |
+ */ | |
+ void resume(); | |
+ | |
+ /** | |
+ * Close the socket. | |
+ */ | |
+ void close(); | |
+ | |
+ /** | |
+ * Write data to the socket. | |
+ * | |
+ * @param data The data to write to the socket. If | |
+ * binaryType: "arraybuffer" was passed in the options | |
+ * object, then this object should be an Uint8Array instance. | |
+ * If binaryType: "string" was passed, or if no binaryType | |
+ * option was specified, then this object should be an | |
+ * an ordinary JavaScript string. | |
+ * | |
+ * @return Send returns true or false as a hint to the caller that | |
+ * they may either continue sending more data immediately, or | |
+ * may want to wait until the other side has read some of the | |
+ * data which has already been written to the socket before | |
+ * buffering more. If send returns true, then less than 64k | |
+ * has been buffered and it's safe to immediately write more. | |
+ * If send returns false, then more than 64k has been buffered, | |
+ * and the caller may wish to wait until the ondrain event | |
+ * handler has been called before buffering more data by more | |
+ * calls to send. | |
+ */ | |
+ boolean send(in jsval data); | |
+ | |
+ /** | |
+ * The readyState attribute indicates which state the socket is currently | |
+ * in. The state will be either CONNECTING, OPEN, CLOSING, or CLOSED. | |
+ */ | |
+ readonly attribute DOMString readyState; | |
+ readonly attribute DOMString CONNECTING; | |
+ readonly attribute DOMString OPEN; | |
+ readonly attribute DOMString CLOSING; | |
+ readonly attribute DOMString CLOSED; | |
+ | |
+ /** | |
+ * The binaryType attribute indicates which mode this socket uses for | |
+ * sending and receiving data. If the binaryType: "arraybuffer" option | |
+ * was passed to the open method that created this socket, binaryType | |
+ * will be "arraybuffer". Otherwise, it will be "string". | |
+ */ | |
+ readonly attribute DOMString binaryType; | |
+ | |
+ /** | |
+ * The onopen event handler is called when the connection to the server | |
+ * has been established. If the connection is refused, onerror will be | |
+ * called, instead. | |
+ */ | |
+ attribute nsIDOMEventListener onopen; | |
+ | |
+ /** | |
+ * After send has buffered more than 64k of data, it returns false to | |
+ * indicate that the client should pause before sending more data, to | |
+ * avoid accumulating large buffers. This is only advisory, and the client | |
+ * is free to ignore it and buffer as much data as desired, but if reducing | |
+ * the size of buffers is important (especially for a streaming application) | |
+ * ondrain will be called once the previously-buffered data has been written | |
+ * to the network, at which point the client can resume calling send again. | |
+ */ | |
+ attribute nsIDOMEventListener ondrain; | |
+ | |
+ /** | |
+ * The ondata handler will be called repeatedly and asynchronously after | |
+ * onopen has been called, every time some data was available from the server | |
+ * and was read. If binaryType: "arraybuffer" was passed to open, the data | |
+ * attribute of the event object will be an Uint8Array. If not, it will be a | |
+ * normal JavaScript string. | |
+ * | |
+ * At any time, the client may choose to pause reading and receiving ondata | |
+ * callbacks, by calling the socket's suspend() method. Further invocations | |
+ * of ondata will be paused until resume() is called. | |
+ */ | |
+ attribute nsIDOMEventListener ondata; | |
+ | |
+ /** | |
+ * The onerror handler will be called when there is an error. The data | |
+ * attribute of the event passed to the onerror handler will have a | |
+ * description of the kind of error. | |
+ * | |
+ * If onerror is called before onopen, the error was connection refused, | |
+ * and onclose will not be called. If onerror is called after onopen, | |
+ * the connection was lost, and onclose will be called after onerror. | |
+ */ | |
+ attribute nsIDOMEventListener onerror; | |
+ | |
+ /** | |
+ * The onclose handler is called once the underlying network socket | |
+ * has been closed, either by the server, or by the client calling | |
+ * close. | |
+ * | |
+ * If onerror was not called before onclose, then either side cleanly | |
+ * closed the connection. | |
+ */ | |
+ attribute nsIDOMEventListener onclose; | |
+}; | |
+ | |
+/** | |
+ * nsITCPSocketEvent is the event object which is passed as the | |
+ * first argument to all the event handler callbacks. It contains | |
+ * the socket that was associated with the event, the type of event, | |
+ * and the data associated with the event (if any). | |
+ */ | |
+ | |
+[scriptable, uuid(0f2abcca-b483-4539-a3e8-345707f75c44)] | |
+interface nsITCPSocketEvent : nsIDOMEvent { | |
+ /** | |
+ * The socket object which produced this event. | |
+ */ | |
+ readonly attribute nsIDOMTCPSocket socket; | |
+ | |
+ /** | |
+ * The type of this event. | |
+ */ | |
+ readonly attribute DOMString type; | |
+ | |
+ /** | |
+ * The data related to this event, if any. In the ondata callback, | |
+ * data will be the bytes read from the network; if the binaryType | |
+ * of the socket was "arraybuffer", this value will be of type Uint8Array; | |
+ * otherwise, it will be a normal JavaScript string. | |
+ * | |
+ * In the onerror callback, data will be a string with a description | |
+ * of the error. | |
+ * | |
+ * In the other callbacks, data will be an empty string. | |
+ */ | |
+ readonly attribute jsval data; | |
+}; | |
+ | |
diff --git a/b2g/components/TCPSocket.js b/b2g/components/TCPSocket.js | |
new file mode 100644 | |
--- /dev/null | |
+++ b/b2g/components/TCPSocket.js | |
@@ -0,0 +1,340 @@ | |
+/* This Source Code Form is subject to the terms of the Mozilla Public | |
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file, | |
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
+ | |
+"use strict"; | |
+ | |
+const Cc = Components.classes; | |
+const Ci = Components.interfaces; | |
+const Cu = Components.utils; | |
+const Cr = Components.results; | |
+const CC = Components.Constructor; | |
+ | |
+Cu.import("resource://gre/modules/XPCOMUtils.jsm"); | |
+Cu.import("resource://gre/modules/Services.jsm"); | |
+ | |
+let debug = true; | |
+function LOG(msg) { | |
+ if (debug) | |
+ dump("TCPSocket: " + msg + "\n"); | |
+} | |
+ | |
+/* | |
+ * nsITCPSocketEvent object | |
+ */ | |
+function TCPSocketEvent(type, sock, data) { | |
+ this.type = type; | |
+ this.socket = sock; | |
+ this.data = data; | |
+} | |
+ | |
+TCPSocketEvent.prototype = { | |
+ classID: Components.ID("{f29a577b-e831-431e-a540-1c4856721c82}"), | |
+ | |
+ QueryInterface: XPCOMUtils.generateQI([Ci.nsITCPSocketEvent]), | |
+ | |
+ classInfo: XPCOMUtils.generateCI({ | |
+ classID: Components.ID("{f29a577b-e831-431e-a540-1c4856721c82}"), | |
+ contractID: "@mozilla.org/tcp-socket-event;1", | |
+ classDescription: "TCP Socket Event", | |
+ interfaces: [Ci.nsITCPSocketEvent], | |
+ flags: Ci.nsIClassInfo.DOM_OBJECT | |
+ }) | |
+}; | |
+ | |
+ | |
+/* | |
+ * nsIDOMTCPSocket object | |
+ */ | |
+function createTransport(host, port, sslMode) { | |
+ let options, optlen; | |
+ if (sslMode) { | |
+ options = [sslMode]; | |
+ optlen = 1; | |
+ } else { | |
+ options = null; | |
+ optlen = 0; | |
+ } | |
+ return Cc["@mozilla.org/network/socket-transport-service;1"] | |
+ .getService(Ci.nsISocketTransportService) | |
+ .createTransport(options, optlen, host, port, null); | |
+} | |
+ | |
+const InputStreamPump = CC( | |
+ "@mozilla.org/network/input-stream-pump;1", "nsIInputStreamPump", "init"), | |
+ Pipe = CC("@mozilla.org/pipe;1", "nsIPipe", "init"), | |
+ AsyncStreamCopier = CC( | |
+ "@mozilla.org/network/async-stream-copier;1", "nsIAsyncStreamCopier", "init"), | |
+ ScriptableInputStream = CC( | |
+ "@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream", "init"), | |
+ BinaryInputStream = CC( | |
+ "@mozilla.org/binaryinputstream;1", "nsIBinaryInputStream", "setInputStream"), | |
+ BinaryOutputStream = CC( | |
+ "@mozilla.org/binaryoutputstream;1", "nsIBinaryOutputStream", "setOutputStream"); | |
+ | |
+const kCONNECTING = 'connecting'; | |
+const kOPEN = 'open'; | |
+const kCLOSING = 'closing'; | |
+const kCLOSED = 'closed'; | |
+ | |
+function TCPSocket() { | |
+ this.readyState = kCLOSED; | |
+ | |
+ this.onopen = null; | |
+ this.ondrain = null; | |
+ this.ondata = null; | |
+ this.onerror = null; | |
+ this.onclose = null; | |
+ | |
+ this.binaryType = "string"; | |
+ | |
+ this.host = ""; | |
+ this.port = 0; | |
+ this.ssl = false; | |
+}; | |
+ | |
+TCPSocket.prototype = { | |
+ CONNECTING: kCONNECTING, | |
+ OPEN: kOPEN, | |
+ CLOSING: kCLOSING, | |
+ CLOSED: kCLOSED, | |
+ _binaryType: null, | |
+ _hasPrivileges: null, | |
+ _transport: null, | |
+ _request: null, | |
+ _inputStream: null, | |
+ _inputStreamPump: null, | |
+ _scriptableInputStream: null, | |
+ _binaryInputStream: null, | |
+ _unbufferedOutputStream: null, | |
+ _outputStreamPipe: null, | |
+ _binaryOutputStream: null, | |
+ _waitingOnOutput: false, | |
+ | |
+ callListener: function ts_callListener(type, data) { | |
+ if (!this[type]) | |
+ return; | |
+ | |
+ this[type].handleEvent(new TCPSocketEvent(type, this, data || "")); | |
+ }, | |
+ | |
+ get bufferedAmount() { | |
+ return this._outputStreamPipe.inputStream.available(); | |
+ }, | |
+ | |
+ init: function ts_init(aWindow) { | |
+ // When the TCPSocket property is initialized for each window, | |
+ // we check to see if the tcp-socket permission is set for this | |
+ // domain. If not, open will refuse to create and open new sockets. | |
+ let principal = aWindow.document.nodePrincipal; | |
+ this._hasPrivileges = ( | |
+ Services.perms.testExactPermission(principal.URI, "tcp-socket") | |
+ === Ci.nsIPermissionManager.ALLOW_ACTION); | |
+ }, | |
+ | |
+ // nsIDOMTCPSocket | |
+ open: function ts_open(host, port, options) { | |
+ // in the testing case, init won't be called and | |
+ // hasPrivileges will be null. We want to proceed to test. | |
+ if (this._hasPrivileges !== true && this._hasPrivileges !== null) { | |
+ throw new Error("TCPSocket does not have permission in this context.\n"); | |
+ } | |
+ let that = new TCPSocket(); | |
+ | |
+ LOG("startup called\n"); | |
+ LOG("Host info: " + host + ":" + port + "\n"); | |
+ | |
+ that.readyState = kCONNECTING; | |
+ that.host = host; | |
+ that.port = port; | |
+ if (options !== undefined) { | |
+ if (options.useSSL) { | |
+ that.ssl = 'ssl'; | |
+ } else { | |
+ that.ssl = false; | |
+ } | |
+ that._binaryType = options.binaryType || that._binaryType; | |
+ } else { | |
+ that.ssl = false; | |
+ } | |
+ LOG("SSL: " + that.ssl + "\n"); | |
+ | |
+ let transport = that._transport = createTransport(host, port, that.ssl); | |
+ | |
+ that._inputStream = transport.openInputStream(0, 0, 0); | |
+ that._unbufferedOutputStream = transport.openOutputStream( | |
+ Ci.nsITransport.OPEN_UNBUFFERED, 0, 0); | |
+ | |
+ // call onTransportStatus when connected | |
+ transport.setEventSink(that, Services.tm.currentThread); | |
+ | |
+ // If the other side is not listening, we will | |
+ // get an onInputStreamReady callback where available | |
+ // raises to indicate the connection was refused. | |
+ that._inputStream.asyncWait( | |
+ that, that._inputStream.WAIT_CLOSURE_ONLY, 0, Services.tm.currentThread); | |
+ | |
+ if (that._binaryType === "string") { | |
+ that._scriptableInputStream = new ScriptableInputStream(that._inputStream); | |
+ } else { | |
+ that._binaryInputStream = new BinaryInputStream(that._inputStream); | |
+ } | |
+ | |
+ that._outputStreamPipe = Pipe( | |
+ true, true, 65536, 65536, null); | |
+ | |
+ that._binaryOutputStream = new BinaryOutputStream( | |
+ that._outputStreamPipe.outputStream); | |
+ | |
+ that._outputStreamCopier = AsyncStreamCopier( | |
+ that._outputStreamPipe.inputStream, | |
+ that._unbufferedOutputStream, | |
+ // (nsSocketTransport uses gSocketTransportService) | |
+ Cc["@mozilla.org/network/socket-transport-service;1"] | |
+ .getService(Ci.nsIEventTarget), | |
+ /* source buffered */ true, /* sink buffered */ false, | |
+ 65536, /* close source*/ true, /* close sink */ true); | |
+ | |
+ that._outputStreamCopier.asyncCopy(null, null); | |
+ | |
+ return that; | |
+ }, | |
+ | |
+ close: function ts_close() { | |
+ if (this.readyState === kCLOSED) | |
+ return; | |
+ | |
+ LOG("close called\n"); | |
+ this.readyState = kCLOSING; | |
+ | |
+ if (this.bufferedAmount === 0) { | |
+ // Works around a bug where calling this._outputStreamPipe.outputStream.close() | |
+ // when not having written anything into the pipe does not actually close the | |
+ // underlying socket | |
+ this._transport.close(Cr.NS_OK); | |
+ } else { | |
+ this._outputStreamPipe.outputStream.close(); | |
+ this._inputStream.close(); | |
+ } | |
+ }, | |
+ | |
+ send: function ts_send(data) { | |
+ if (this.readyState !== kOPEN) { | |
+ throw new Error("Socket not open."); | |
+ } else if (data === undefined) { | |
+ throw new Error("Data is undefined."); | |
+ } | |
+ | |
+ if (this._binaryType === "arraybuffer") { | |
+ this._binaryOutputStream.writeByteArray(data, data.length); | |
+ } else { | |
+ this._binaryOutputStream.writeBytes(data, data.length); | |
+ } | |
+ | |
+ if (this.bufferedAmount > 65535) { | |
+ // If we buffered more than some arbitrary amount of data, | |
+ // (65535 right now) we should tell the caller so they can | |
+ // wait until ondrain is called, once all the buffered data | |
+ // has been written to the socket. | |
+ if (!this._waitingOnOutput) { | |
+ // If this is the first time send has caused buffering, | |
+ // we need to wait until the copier finishes copying all data | |
+ // from the pipe and then call ondrain to let the user know they | |
+ // can write more data. We wait on the output end of the pipe, | |
+ // which is being copied in another thread to the async socket, | |
+ // and when it is done being copied, we will get an | |
+ // onOutputStreamReady event in this thread. | |
+ this._waitingOnOutput = true; | |
+ this._outputStreamPipe.outputStream.asyncWait( | |
+ this, 0, 0, Services.tm.currentThread); | |
+ } | |
+ return false; | |
+ } | |
+ return true; | |
+ }, | |
+ | |
+ suspend: function ts_suspend() { | |
+ if (this._request) { | |
+ this._request.suspend(); | |
+ } | |
+ }, | |
+ | |
+ resume: function ts_resume() { | |
+ if (this._request) { | |
+ this._request.resume(); | |
+ } | |
+ }, | |
+ // nsITransportEventSink (Triggered by transport.setEventSink) | |
+ onTransportStatus: function ts_onTransportStatus( | |
+ transport, status, progress, max) { | |
+ | |
+ if (status === Ci.nsISocketTransport.STATUS_CONNECTED_TO) { | |
+ this.readyState = kOPEN; | |
+ this.callListener("onopen"); | |
+ | |
+ this._inputStreamPump = new InputStreamPump( | |
+ this._inputStream, -1, -1, 0, 0, false | |
+ ).asyncRead(this, null); | |
+ } | |
+ }, | |
+ // nsIAsyncInputStream (Triggered by _inputStream.asyncWait) | |
+ // Only used for detecting connection refused | |
+ onInputStreamReady: function ts_onInputStreamReady(input) { | |
+ try { | |
+ input.available(); | |
+ } catch (e) { | |
+ this.callListener("onerror", "Connection refused"); | |
+ } | |
+ }, | |
+ // nsIAsyncOutputStream (Triggered by | |
+ // _outputStreamPipe.outputStream.asyncWait) | |
+ // Used to know when the pipe's buffer is empty | |
+ onOutputStreamReady: function ts_onOutputStreamReady(stream) { | |
+ this._waitingOnOutput = false; | |
+ this.callListener("ondrain"); | |
+ }, | |
+ // nsIRequestObserver | |
+ onStartRequest: function ts_onStartRequest(request, context) { | |
+ this._request = request; | |
+ }, | |
+ // nsIRequestObserver | |
+ onStopRequest: function ts_onStopRequest(request, context, status) { | |
+ this.readyState = kCLOSED; | |
+ this._request = null; | |
+ | |
+ if (status) { | |
+ this.callListener("onerror", "Error " + status); | |
+ } | |
+ | |
+ this.callListener("onclose"); | |
+ }, | |
+ // nsIStreamListener | |
+ onDataAvailable: function ts_onDataAvailable(request, context, inputStream, offset, count) { | |
+ if (this._binaryType === "arraybuffer") { | |
+ let ua = new Uint8Array(count); | |
+ | |
+ ua.set(this._binaryInputStream.readByteArray(count)); | |
+ this.callListener("ondata", ua); | |
+ } else { | |
+ this.callListener("ondata", this._scriptableInputStream.read(count)); | |
+ } | |
+ }, | |
+ | |
+ classID: Components.ID("{cda91b22-6472-11e1-aa11-834fec09cd0a}"), | |
+ | |
+ classInfo: XPCOMUtils.generateCI({ | |
+ classID: Components.ID("{cda91b22-6472-11e1-aa11-834fec09cd0a}"), | |
+ contractID: "@mozilla.org/tcp-socket;1", | |
+ classDescription: "TCP Socket Helper", | |
+ interfaces: [Ci.nsIDOMTCPSocket], | |
+ flags: Ci.nsIClassInfo.DOM_OBJECT, | |
+ }), | |
+ | |
+ QueryInterface: XPCOMUtils.generateQI([ | |
+ Ci.nsIDOMTCPSocket, | |
+ ]) | |
+} | |
+ | |
+ | |
+const NSGetFactory = XPCOMUtils.generateNSGetFactory([TCPSocket]); | |
+ | |
diff --git a/b2g/test/Makefile.in b/b2g/test/Makefile.in | |
new file mode 100644 | |
--- /dev/null | |
+++ b/b2g/test/Makefile.in | |
@@ -0,0 +1,17 @@ | |
+# This Source Code Form is subject to the terms of the Mozilla Public | |
+# License, v. 2.0. If a copy of the MPL was not distributed with this | |
+# file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
+ | |
+DEPTH = ../.. | |
+topsrcdir = @top_srcdir@ | |
+srcdir = @srcdir@ | |
+VPATH = @srcdir@ | |
+relativesrcdir = b2g/test | |
+ | |
+include $(DEPTH)/config/autoconf.mk | |
+ | |
+MODULE = test_b2g | |
+ | |
+XPCSHELL_TESTS = unit | |
+ | |
+include $(topsrcdir)/config/rules.mk | |
diff --git a/b2g/test/unit/test_tcpsocket.js b/b2g/test/unit/test_tcpsocket.js | |
new file mode 100644 | |
--- /dev/null | |
+++ b/b2g/test/unit/test_tcpsocket.js | |
@@ -0,0 +1,345 @@ | |
+ | |
+ | |
+/** | |
+ * Test TCPSocket.js by creating an XPCOM-style server socket, then sending | |
+ * data in both directions and making sure each side receives their data | |
+ * correctly and with the proper events. | |
+ * | |
+ * This test is derived from netwerk/test/unit/test_socks.js, except we don't | |
+ * involve a subprocess. | |
+ * | |
+ * Future work: | |
+ * - SSL. see https://bugzilla.mozilla.org/show_bug.cgi?id=466524 | |
+ * https://bugzilla.mozilla.org/show_bug.cgi?id=662180 | |
+ * Alternatively, mochitests could be used. | |
+ * - Testing overflow logic. | |
+ * | |
+ **/ | |
+ | |
+const Cc = Components.classes; | |
+const Ci = Components.interfaces; | |
+const Cr = Components.results; | |
+const Cu = Components.utils; | |
+const CC = Components.Constructor; | |
+ | |
+// Some binary data to send. | |
+const DATA_ARRAY = [0, 255, 254, 0, 1, 2, 3, 0, 255, 255, 254, 0], | |
+ TYPED_DATA_ARRAY = new Uint8Array(DATA_ARRAY), | |
+ HELLO_WORLD = "hlo wrld. ", | |
+ BIG_ARRAY = new Array(524288); | |
+ | |
+for (var i_big = 0, j_big = 0; i_big < BIG_ARRAY.length; i_big++) { | |
+ BIG_ARRAY[i_big] = HELLO_WORLD.charCodeAt(j_big++); | |
+ if (j_big >= HELLO_WORLD.length) { | |
+ j_big = 0; | |
+ } | |
+} | |
+ | |
+const BIG_TYPED_ARRAY = new Uint8Array(BIG_ARRAY); | |
+ | |
+const ServerSocket = CC("@mozilla.org/network/server-socket;1", | |
+ "nsIServerSocket", | |
+ "init"), | |
+ InputStreamPump = CC("@mozilla.org/network/input-stream-pump;1", | |
+ "nsIInputStreamPump", | |
+ "init"), | |
+ BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", | |
+ "nsIBinaryInputStream", | |
+ "setInputStream"), | |
+ BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1", | |
+ "nsIBinaryOutputStream", | |
+ "setOutputStream"); | |
+ | |
+ | |
+// The "open" method is the constructor-ish method, but for realism, we do not | |
+// make it magically happen. | |
+const nsIDOMTCPSocket = CC("@mozilla.org/tcp-socket;1", | |
+ "nsIDOMTCPSocket"), | |
+ TCPSocket = new nsIDOMTCPSocket(); | |
+ | |
+/** | |
+ * Spin up a listening socket and associate at most one live, accepted socket | |
+ * with ourselves. | |
+ */ | |
+function TestServer() { | |
+ this.listener = ServerSocket(-1, true, -1); | |
+ print('server: listening on', this.listener.port); | |
+ this.listener.asyncListen(this); | |
+ | |
+ this.binaryInput = null; | |
+ this.input = null; | |
+ this.binaryOutput = null; | |
+ this.output = null; | |
+ | |
+ this.onaccept = null; | |
+ this.ondata = null; | |
+ this.onclose = null; | |
+} | |
+TestServer.prototype = { | |
+ onSocketAccepted: function(socket, trans) { | |
+ if (this.input) | |
+ do_throw("More than one live connection!?"); | |
+ | |
+ print('server: got client connection'); | |
+ this.input = trans.openInputStream(0, 0, 0); | |
+ this.binaryInput = new BinaryInputStream(this.input); | |
+ this.output = trans.openOutputStream(0, 0, 0); | |
+ this.binaryOutput = new BinaryOutputStream(this.output); | |
+ | |
+ new InputStreamPump(this.input, -1, -1, 0, 0, false).asyncRead(this, null); | |
+ | |
+ if (this.onaccept) | |
+ this.onaccept(); | |
+ else | |
+ do_throw("Received unexpected connection!"); | |
+ }, | |
+ | |
+ onStopListening: function(socket) { | |
+ }, | |
+ | |
+ | |
+ onDataAvailable: function(request, context, inputStream, offset, count) { | |
+print('onDataAvailable', request, context, inputStream, offset, count); | |
+ var readData = this.binaryInput.readByteArray(count); | |
+ if (this.ondata) { | |
+ try { | |
+ this.ondata(readData); | |
+ } | |
+ catch(ex) { | |
+ // re-throw if this is from do_throw | |
+ if (ex === Cr.NS_ERROR_ABORT) | |
+ throw ex; | |
+ // log if there was a test problem | |
+ do_print('Caught exception: ' + ex + '\n' + ex.stack); | |
+ do_throw('test is broken; bad ondata handler; see above'); | |
+ } | |
+ } | |
+ else | |
+ do_throw('Received ' + count + ' bytes of unexpected data!'); | |
+ | |
+ }, | |
+ | |
+ waitForData: function(expectedData, successFunc) { | |
+ this.expectedData = expectedData; | |
+ }, | |
+ | |
+ onStartRequest: function(request, context) { | |
+ }, | |
+ | |
+ onStopRequest: function(request, context, status) { | |
+print('onStopRequest', request, context, status); | |
+ if (this.onclose) | |
+ this.onclose(); | |
+ else | |
+ do_throw("Received unexpected close!"); | |
+ }, | |
+ | |
+ close: function() | |
+ { | |
+ this.binaryInput.close(); | |
+ this.binaryOutput.close(); | |
+ }, | |
+ | |
+ /** | |
+ * Forget about the socket we knew about before. | |
+ */ | |
+ reset: function() { | |
+ this.binaryInput = this.input = this.binaryOutput = this.output = null; | |
+ }, | |
+}; | |
+ | |
+function makeSuccessCase(name) { | |
+ return function() { | |
+ do_print('got expected: ' + name); | |
+ run_next_test(); | |
+ }; | |
+} | |
+function makeJointSuccess(names) { | |
+ let funcs = {}, successCount = 0; | |
+ names.forEach(function(name) { | |
+ funcs[name] = function() { | |
+ do_print('got expected: ' + name); | |
+ if (++successCount === names.length) | |
+ run_next_test(); | |
+ }; | |
+ }); | |
+ return funcs; | |
+}; | |
+function makeFailureCase(name) { | |
+ return function() { | |
+ let argstr; | |
+ if (arguments.length) { | |
+ argstr = '(args: ' + | |
+ Array.map(arguments, function(x) { return x + ""; }).join(" ") + ')'; | |
+ } | |
+ else { | |
+ argstr = '(no arguments)'; | |
+ } | |
+ do_throw('got unexpected: ' + name + ' ' + argstr); | |
+ }; | |
+} | |
+function makeExpectData(name, expectedData, fromEvent) { | |
+ let dataBuffer = fromEvent ? null : [], done = false; | |
+ return function(receivedData) { | |
+ if (fromEvent) { | |
+ receivedData = receivedData.data; | |
+ if (dataBuffer) { | |
+ let newBuffer = new Uint8Array(dataBuffer.length + receivedData.length); | |
+ newBuffer.set(dataBuffer, 0); | |
+ newBuffer.set(receivedData, dataBuffer.length); | |
+ dataBuffer = newBuffer; | |
+ } | |
+ else { | |
+ dataBuffer = receivedData; | |
+ } | |
+ } | |
+ else { | |
+ dataBuffer = dataBuffer.concat(receivedData); | |
+ } | |
+ do_print('received ' + receivedData.length + ' bytes'); | |
+ | |
+ if (done) | |
+ do_throw('Received data event when already done!'); | |
+ | |
+ if (dataBuffer.length >= expectedData.length) { | |
+ // check the bytes are equivalent | |
+ for (let i = 0; i < expectedData.length; i++) { | |
+ do_check_eq(dataBuffer[i], expectedData[i]); | |
+ } | |
+ if (dataBuffer.length > expectedData.length) | |
+ do_throw('Received ' + dataBuffer.length + ' bytes but only expected ' + | |
+ expectedData.length + ' bytes.'); | |
+ | |
+ done = true; | |
+ run_next_test(); | |
+ } | |
+ }; | |
+} | |
+ | |
+var server = null, sock = null, failure_drain = null; | |
+ | |
+function connectSock() { | |
+ server.reset(); | |
+ var yayFuncs = makeJointSuccess(['serveropen', 'clientopen']); | |
+ | |
+ sock = TCPSocket.open( | |
+ '127.0.0.1', server.listener.port, | |
+ { binaryType: 'arraybuffer' }); | |
+ | |
+ sock.onopen = yayFuncs.clientopen; | |
+ sock.ondrain = null; | |
+ sock.ondata = makeFailureCase('data'); | |
+ sock.onerror = makeFailureCase('error'); | |
+ sock.onclose = makeFailureCase('close'); | |
+ | |
+ server.onaccept = yayFuncs.serveropen; | |
+ server.ondata = makeFailureCase('serverdata'); | |
+ server.onclose = makeFailureCase('serverclose'); | |
+} | |
+function sendData() { | |
+ server.ondata = makeExpectData('serverdata', DATA_ARRAY); | |
+ if (!sock.send(TYPED_DATA_ARRAY)) { | |
+ do_throw("send should not have buffered such a small amount of data"); | |
+ } | |
+} | |
+function sendBig() { | |
+ var yays = makeJointSuccess(['serverdata', 'clientdrain']) | |
+ server.ondata = yays.serverdata; | |
+ sock.ondrain = function(evt) { | |
+ if (sock.bufferedAmount) { | |
+ do_throw("sock.bufferedAmount was > 0 in ondrain"); | |
+ } | |
+ yays.clientdrain(evt); | |
+ } | |
+ if (sock.send(BIG_TYPED_ARRAY)) { | |
+ do_throw("expected sock.send to return false on large buffer send"); | |
+ } | |
+} | |
+function receiveData() { | |
+ server.ondata = makeFailureCase('serverdata'); | |
+ sock.ondata = makeExpectData('data', DATA_ARRAY, true); | |
+ | |
+ server.binaryOutput.writeByteArray(DATA_ARRAY, DATA_ARRAY.length); | |
+} | |
+function serverCloses() { | |
+ // we don't really care about the server's close event, but we do want to | |
+ // make sure it happened for sequencing purposes. | |
+ var yayFuncs = makeJointSuccess(['clientclose', 'serverclose']); | |
+ sock.ondata = makeFailureCase('data'); | |
+ sock.onclose = yayFuncs.clientclose; | |
+ server.onclose = yayFuncs.serverclose; | |
+ | |
+ server.close(); | |
+} | |
+function clientCloses() { | |
+ // we want to make sure the server heard the close and also that the client's | |
+ // onclose event fired for consistency. | |
+ var yayFuncs = makeJointSuccess(['clientclose', 'serverclose']); | |
+ server.onclose = yayFuncs.serverclose; | |
+ sock.onclose = yayFuncs.clientclose; | |
+ | |
+ sock.close(); | |
+} | |
+ | |
+/** | |
+ * Send a large amount of data and immediately call close | |
+ */ | |
+ | |
+function bufferedClose() { | |
+ var yays = makeJointSuccess(['serverdata', 'clientclose', 'serverclose']); | |
+ server.ondata = yays.serverdata; | |
+ server.onclose = yays.serverclose; | |
+ sock.onclose = yays.clientclose; | |
+ sock.send(BIG_TYPED_ARRAY); | |
+ sock.close(); | |
+} | |
+ | |
+/** | |
+ * Connect to a port we know is not listening and so an error is assured. | |
+ */ | |
+function badConnect() { | |
+ // There's probably nothing listening on tcp port 2. | |
+ sock = TCPSocket.open('127.0.0.1', 2); | |
+ | |
+ sock.onopen = makeFailureCase('open'); | |
+ sock.ondata = makeFailureCase('data'); | |
+ sock.onclose = makeFailureCase('close'); | |
+ | |
+ sock.onerror = makeSuccessCase('error'); | |
+ | |
+} | |
+function cleanup() { | |
+ sock.close(); | |
+ makeSuccessCase('cleanup')(); | |
+} | |
+ | |
+// - connect, data and events work both ways | |
+add_test(connectSock); | |
+add_test(sendData); | |
+add_test(sendBig); | |
+add_test(receiveData); | |
+// - server closes on us | |
+add_test(serverCloses); | |
+ | |
+// - connect, we close on the server | |
+add_test(connectSock); | |
+add_test(clientCloses); | |
+ | |
+// - connect, buffer, close | |
+add_test(connectSock); | |
+add_test(bufferedClose); | |
+ | |
+// - get an error on an attempt to connect to a non-listening port | |
+add_test(badConnect); | |
+add_test(cleanup); | |
+ | |
+function run_test() { | |
+ server = new TestServer(); | |
+ | |
+ run_next_test(); | |
+ | |
+ do_timeout(10000, function() { | |
+ do_throw( | |
+ "The test should never take this long unless the system is hosed."); | |
+ }); | |
+} | |
diff --git a/b2g/test/unit/xpcshell.ini b/b2g/test/unit/xpcshell.ini | |
new file mode 100644 | |
--- /dev/null | |
+++ b/b2g/test/unit/xpcshell.ini | |
@@ -0,0 +1,5 @@ | |
+[DEFAULT] | |
+head = | |
+tail = | |
+ | |
+[test_tcpsocket.js] | |
diff --git a/testing/xpcshell/xpcshell.ini b/testing/xpcshell/xpcshell.ini | |
--- a/testing/xpcshell/xpcshell.ini | |
+++ b/testing/xpcshell/xpcshell.ini | |
@@ -117,8 +117,10 @@ | |
[include:modules/libpref/test/unit_ipc/xpcshell.ini] | |
[include:netwerk/test/unit_ipc/xpcshell.ini] | |
[include:netwerk/cookie/test/unit_ipc/xpcshell.ini] | |
[include:toolkit/components/contentprefs/tests/unit_ipc/xpcshell.ini] | |
[include:uriloader/exthandler/tests/unit_ipc/xpcshell.ini] | |
[include:modules/libmar/tests/unit/xpcshell.ini] | |
skip-if = os == "android" | |
+ | |
+[include:b2g/test/unit/xpcshell.ini] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment