Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Bug 1170040 - Move marionette-client into gaia #30375

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .jshintignore
Expand Up @@ -65,7 +65,9 @@ tv_apps/tv-deck/bower_components/**
tv_apps/tv-epg/bower_components/**
tests/atoms/remote_date.js
tests/atoms/screenshot.js
tests/jsmarionette/client/marionette-client/docs/**
tests/jsmarionette/plugins/marionette-js-logger/lib/log_grabber.js
tests/jsmarionette/runner/marionette-js-runner/test/fixtures/**
tests/jsmarionette/runner/marionette-js-runner/test/bin/fixtures/**
tests/jsmarionette/runner/marionette-js-runner/venv/**
tests/jsmarionette/runner/mozilla-profile-builder/test/fixtures/**
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -27,10 +27,11 @@
"istanbul": "^0.3.2",
"jsdoc": "^3.3.0",
"jshint": "2.7.0",
"json-wire-protocol": "file:./tests/jsmarionette/client/json-wire-protocol",
"load-grunt-tasks": "0.4.0",
"mail-fakeservers": "0.0.24",
"marionette-apps": "file:./tests/jsmarionette/plugins/marionette-apps",
"marionette-client": "1.7.5",
"marionette-client": "file:./tests/jsmarionette/client/marionette-client",
"marionette-device-host": "1.0.0",
"marionette-content-script": "file:./tests/jsmarionette/plugins/marionette-content-script",
"marionette-file-manager": "file:./tests/jsmarionette/plugins/marionette-file-manager",
Expand Down Expand Up @@ -58,6 +59,8 @@
"rimraf": "2.2.5",
"sinon": "1.7.3",
"slugid": "^1.0.2",
"socket-retry-connect": "file:./tests/jsmarionette/client/socket-retry-connect",
"sockit-to-me": "file:./tests/jsmarionette/client/sockit-to-me",
"superagent-promise": ">=0.2.0",
"taskcluster-client": "0.21.4",
"taskcluster-npm-cache": "1.1.14",
Expand Down
11 changes: 11 additions & 0 deletions tests/jsmarionette/client/json-wire-protocol/HISTORY.md
@@ -0,0 +1,11 @@
# 1.0.0
- node only version with complete change of module interface.

# 0.2.0
- expose separator constant value.

# 0.1.0
- browser support ( intended for xpcshell primarily though )

# 0.0.1
- initial release node only
29 changes: 29 additions & 0 deletions tests/jsmarionette/client/json-wire-protocol/README.md
@@ -0,0 +1,29 @@
# JSON wire protocol parser

Lets you parse stuff like this: 10:{"a": "x"}...

More details/examples soon see test/ for usage.

## License

The MIT License (MIT)

Copyright (c) 2013 Sahaja James Lal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
135 changes: 135 additions & 0 deletions tests/jsmarionette/client/json-wire-protocol/lib/index.js
@@ -0,0 +1,135 @@
'use strict';
var Transform = require('stream').Transform;

var SEPARATOR = ':';
var SEPARATOR_CODE = SEPARATOR.charCodeAt(0);

/**
* First ocurrence of where string occurs in a buffer.
*
* NOTE: this is not UTF8 safe generally we expect to find the correct
* char fairly quickly unless the buffer is incorrectly formatted.
*
* @param {Buffer} buffer haystack.
* @param {String} code needle.
* @return {Numeric} -1 if not found index otherwise.
*/
function indexInBuffer(buffer, code) {
if (buffer.length === 0)
return -1;

var index = 0;
var length = buffer.length;

do {
if (buffer[index] === code) return index;
} while (
++index && index + 1 < length
);

return -1;
}

/**
* converts an object to a string representation suitable for storage on disk.
* Its very important to note that the length in the string refers to the utf8
* size of the json content in bytes (as utf8) not the JS string length.
*
* @param {Object} object to stringify.
* @return {String} serialized object.
*/
function stringify(object) {
var json = JSON.stringify(object);
var len = Buffer.byteLength(json);

return len + SEPARATOR + json;
}

/**
* attempts to parse a given buffer or string.
*
* @param {Uint8Array|Buffer} input in byteLength:{json..} format.
* @return {Objec} JS object.
*/
function parse(input) {
var stream = new Stream();
var result;

stream.once('data', function(data) {
result = data;
});

stream.write(input);

if (!result) {
throw new Error(
'no command available from parsing:' + input
);
}

return result;
}

function Stream() {
this._pendingLength = null;
this._buffer = new Buffer(0);

Transform.call(this, { objectMode: true });
}

Stream.prototype = {
__proto__: Transform.prototype,

_transform: function(chunk, encoding, cb) {
if (!this._pendingLength) {
var idx = indexInBuffer(chunk, SEPARATOR_CODE);

// Nothing to do just buffer it...
if (idx === -1) {
this._buffer = Buffer.concat([this._buffer, chunk]);
return cb();
}

// number of bytes in the json segment...
var length = Buffer.concat([this._buffer, chunk.slice(0, idx)]);
this._pendingLength = parseInt(length.toString(), 10);
this._buffer = new Buffer(0);

// We have transitioned to a pending length state so do another pass.
return this._transform(chunk.slice(idx + 1), encoding, cb);
}

// Total length too small nothing to do...
if (this._buffer.length + chunk.length < this._pendingLength) {
this._buffer = Buffer.concat([this._buffer, chunk]);
return cb();
}

var buffer = Buffer.concat([this._buffer, chunk]);
var remainder = null;

if (buffer.length > this._pendingLength) {
remainder = buffer.slice(this._pendingLength);
buffer = buffer.slice(0, this._pendingLength);
}

// Reset internal state and push current json string.
this._pendingLength = null;
this._buffer = new Buffer(0);
this.push(JSON.parse(buffer.toString()));

// If we have any remaining data we need to keep processing...
if (remainder) {
return this._transform(remainder, encoding, cb);
}

// Otherwise we are done yay....
return cb();
}

};

exports.parse = parse;
exports.stringify = stringify;
exports.Stream = Stream;
exports.separator = SEPARATOR;
7 changes: 7 additions & 0 deletions tests/jsmarionette/client/json-wire-protocol/package.json
@@ -0,0 +1,7 @@
{
"name": "json-wire-protocol",
"version": "1.0.0",
"author": "James Lal",
"main": "lib/index.js",
"description": "JSON wire protocol parser/writer (used in mozilla debugger protocol)"
}
139 changes: 139 additions & 0 deletions tests/jsmarionette/client/json-wire-protocol/test/index-test.js
@@ -0,0 +1,139 @@
'use strict';
var assert = require('assert');
var jsonWireProtocol = require('../');

suite('json wire protocol', function() {
var subject;
var TWO_BYTE = 'ž';

function createBytes(content) {
return new Buffer(content);
}

test('.separator', function() {
assert.ok(jsonWireProtocol.separator);
});

suite('#stringify', function() {
test('ASCII only', function() {
var input = { a: 'abcdefg' };
var expected = JSON.stringify(input);

expected = expected.length + ':' + expected;
var output = jsonWireProtocol.stringify(input);

assert.deepEqual(expected, output);
});

test('invalid strings', function() {
var invalid = 'xfoobar!';

assert.throws(function() {
jsonWireProtocol.parse(invalid);
});
});

test('with multibyte chars', function() {
var input = { a: TWO_BYTE + TWO_BYTE + TWO_BYTE };
var expected = JSON.stringify(input);

expected = '14:' + expected;
assert.deepEqual(jsonWireProtocol.stringify(input), expected);
});
});

suite('#parse', function() {
test('working string', function() {
var input = { woot: TWO_BYTE };
var string = jsonWireProtocol.stringify(input);
assert.deepEqual(
input,
jsonWireProtocol.parse(createBytes(string))
);
});
});

suite('.Stream', function() {
setup(function() {
subject = new jsonWireProtocol.Stream();
});

suite('#write', function() {

suite('multiple commands over two buffers', function() {
var commandA = { a: 'cool' };
var commandB = { b: 'woot' };
var commandC = { c: 'wtfman' };
var commandD = { d: TWO_BYTE };

var all = [
commandA,
commandB,
commandC
]
.map(jsonWireProtocol.stringify)
.join('');
var half = all.length / 2;

var bufferA = createBytes(all.slice(0, half));
var bufferB = createBytes(all.slice(half) +
jsonWireProtocol.stringify(commandD));

var parsed;

setup(function() {
parsed = [];

subject.on('data', function(result) {
parsed.push(result);
});

subject.write(bufferA);
subject.write(bufferB);
});

test('result after writing to stream', function() {
assert.deepEqual(
parsed,
[commandA, commandB, commandC, commandD]
);
});
});

test('multiple chunks until length', function(done) {
var expected = { longer: TWO_BYTE + 'a' + TWO_BYTE };
var string = jsonWireProtocol.stringify(expected);

subject.on('data', function(result) {
assert.deepEqual(result, expected);
done();
});

for (var i = 0; i < string.length; i++) {
subject.write(createBytes(string[i]));
}
});

suite('entire buffer', function() {
var buffer;
/* 13 ascii bytes */
var string = '{"one":"foo"}';

setup(function() {
buffer = createBytes('13:' + string);
});

test('read entire buffer', function(done) {
var expected = { one: 'foo' };
subject.once('data', function(data) {
assert.deepEqual(data, expected);
done();
});

subject.write(buffer);
});
});
});
});

});