Index: mojo/public/js/bindings/tests/validation_test_input_parser.js |
diff --git a/mojo/public/js/bindings/tests/validation_test_input_parser.js b/mojo/public/js/bindings/tests/validation_test_input_parser.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..063a5e8dfce7cb8e6ed8c108bb49dffdd1fcd842 |
--- /dev/null |
+++ b/mojo/public/js/bindings/tests/validation_test_input_parser.js |
@@ -0,0 +1,463 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Support for parsing binary sequences encoded as readable strings |
+// or ".data" files. The input format is described here: |
+// mojo/public/cpp/bindings/tests/validation_test_input_parser.h |
+ |
+define([ |
+ "mojo/public/js/bindings/buffer" |
+ ], function(buffer) { |
+ |
+ // Files and Lines represent the raw text from an input string |
+ // or ".data" file. |
+ |
+ function InputError(message, line) { |
+ this.message = message; |
+ this.line = line; |
+ } |
+ |
+ InputError.prototype.toString = function() { |
+ var s = 'Error: ' + this.message; |
+ if (this.line) |
+ s += ', at line ' + |
+ (this.line.number + 1) + ': "' + this.line.contents + '"'; |
+ return s; |
+ } |
+ |
+ function File(contents) { |
+ this.contents = contents; |
+ this.index = 0; |
+ this.lineNumber = 0; |
+ } |
+ |
+ File.prototype.isEmpty = function() { |
yzshen1
2014/07/23 20:31:10
nit: maybe "reachEnd" is more clear. (and similar
hansmuller
2014/07/24 00:36:37
OK, I used "endReached()".
|
+ return this.index >= this.contents.length; |
+ } |
+ |
+ File.prototype.nextLine = function() { |
+ if (this.isEmpty()) |
+ return null; |
+ var start = this.index; |
+ var end = this.contents.indexOf('\n', start); |
yzshen1
2014/07/23 20:31:10
Does \r matter here?
hansmuller
2014/07/24 00:36:37
Not really, since breaking the input up into "line
yzshen1
2014/07/24 01:15:10
It also matters where to end a comment, right?
On
|
+ if (end == -1) |
+ end = this.contents.length; |
+ this.index = end + 1; |
+ return new Line(this.contents.substring(start, end), this.lineNumber++); |
+ } |
+ |
+ function Line(contents, number) { |
+ var i = contents.indexOf('//'); |
+ var s = (i == -1) ? contents.trim() : contents.substring(0, i).trim(); |
+ this.contents = contents; |
+ this.items = (s.length > 0) ? s.split(/\s+/) : []; |
+ this.index = 0; |
+ this.number = number; |
+ } |
+ |
+ Line.prototype.isEmpty = function() { |
+ return this.index >= this.items.length; |
+ } |
+ |
+ var itemTypeSizes = { |
yzshen1
2014/07/23 20:31:10
Shall we name it kItemTypeSizes because it is cons
hansmuller
2014/07/24 00:36:37
According to the style guide I should write:
var I
|
+ u1: 1, u2: 2, u4: 4, u8: 8, s1: 1, s2: 2, s4: 4, s8: 8, b: 1, f: 4, d: 8, |
+ dist4: 4, dist8: 8, anchr: 0, handles: 0 |
+ }; |
+ |
+ function isValidItemType(type) { |
+ return itemTypeSizes[type] !== undefined; |
+ } |
+ |
+ Line.prototype.nextItem = function() { |
+ if (this.isEmpty()) |
+ return null; |
+ |
+ var itemString = this.items[this.index++]; |
+ var type = 'u1'; |
+ var value = itemString; |
+ |
+ if (itemString.charAt(0) == '[') { |
+ var i = itemString.indexOf(']'); |
+ if (i != -1 && i + 1 < itemString.length) { |
+ type = itemString.substring(1, i); |
+ value = itemString.substring(i + 1); |
+ } else { |
+ throw new InputError('invalid item', this); |
+ } |
+ } |
+ if (!isValidItemType(type)) |
+ throw new InputError('invalid item type', this); |
+ |
+ return new Item(this, type, value); |
+ } |
+ |
+ // The text for Each whitespace delimited binary data "item" |
+ // file is represented by an Item. |
yzshen1
2014/07/23 20:31:10
Is this line correct?
hansmuller
2014/07/24 00:36:37
That "...file is" was a typo, thanks for pointing
|
+ |
+ function Item(line, type, value) { |
+ this.line = line; |
+ this.type = type; |
+ this.value = value; |
+ this.size = itemTypeSizes[type]; |
+ } |
+ |
+ Item.prototype.isFloat = function() { |
+ return this.type == 'f' || this.type == 'd'; |
+ } |
+ |
+ Item.prototype.isInteger = function() { |
+ return ['u1', 'u2', 'u4', 'u8', |
+ 's1', 's2', 's4', 's8'].indexOf(this.type) != -1; |
+ } |
+ |
+ Item.prototype.isNumber = function() { |
+ return this.isFloat() || this.isInteger(); |
+ } |
+ |
+ Item.prototype.isByte = function() { |
+ return this.type == 'b'; |
+ } |
+ |
+ Item.prototype.isDistance = function() { |
+ return this.type == 'dist4' || this.type == 'dist8'; |
+ } |
+ |
+ Item.prototype.isAnchor = function() { |
+ return this.type == 'anchr'; |
+ } |
+ |
+ Item.prototype.isHandles = function() { |
+ return this.type == 'handles'; |
+ } |
+ |
+ // A TestMessage represents the complete binary message loaded from |
+ // an input string or ".data" file. The parseTestMessage() function |
+ // below constructs a TestMessage from a File. |
+ |
+ function TestMessage(byteLength) { |
+ this.index = 0; |
+ this.buffer = new buffer.Buffer(byteLength); |
+ this.distances = {}; |
+ this.handleCount = 0; |
+ } |
+ |
+ function checkItemNumberValue(item, n, min, max) { |
+ if (n < min || n > max) |
+ throw new InputError('invalid item value', item.line); |
+ } |
+ |
+ TestMessage.prototype.addNumber = function(item) { |
+ var n = item.isInteger() ? parseInt(item.value) : parseFloat(item.value); |
+ if (Number.isNaN(n)) |
+ throw new InputError("can't parse item value", item.line); |
+ |
+ switch(item.type) { |
+ case 'u1': |
yzshen1
2014/07/23 20:31:10
2 more spaces of indent, please.
hansmuller
2014/07/24 00:36:37
Done.
|
+ checkItemNumberValue(item, n, 0, 0xFF); |
+ this.buffer.setUint8(this.index, n); |
+ this.index += 1; |
yzshen1
2014/07/23 20:31:10
We could consider advancing this.index with item.s
hansmuller
2014/07/24 00:36:37
Good point. I moved it to addItem().
|
+ break; |
+ case 'u2': |
+ checkItemNumberValue(item, n, 0, 0xFFFF); |
+ this.buffer.setUint16(this.index, n); |
+ this.index += 2; |
+ break; |
+ case 'u4': |
+ checkItemNumberValue(item, n, 0, 0xFFFFFFFF); |
+ this.buffer.setUint32(this.index, n); |
+ this.index += 4; |
+ break; |
+ case 'u8': |
+ checkItemNumberValue(item, n, 0, Number.MAX_SAFE_INTEGER); |
+ this.buffer.setUint64(this.index, n); |
+ this.index += 8; |
+ break; |
+ case 's1': |
+ checkItemNumberValue(item, n, -128, 127); |
+ this.buffer.setInt8(this.index, n); |
+ this.index += 1; |
+ break; |
+ case 's2': |
+ checkItemNumberValue(item, n, -32768, 32767); |
+ this.buffer.setInt16(this.index, n); |
+ this.index += 2; |
+ break; |
+ case 's4': |
+ checkItemNumberValue(item, n, -2147483648, 2147483647); |
+ this.buffer.setInt32(this.index, n); |
+ this.index += 4; |
+ break; |
+ case 's8': |
+ checkItemNumberValue(item, n, |
+ Number.MIN_SAFE_INTEGER, |
+ Number.MAX_SAFE_INTEGER); |
+ this.buffer.setInt64(this.index, n); |
+ this.index += 8; |
+ break; |
+ case 'f': |
+ this.buffer.setFloat32(this.index, n); |
+ this.index += 4; |
+ break; |
+ case 'd': |
+ this.buffer.setFloat64(this.index, n); |
+ this.index += 8; |
+ break; |
+ |
+ default: |
+ throw new InputError('unrecognized item type', item.line); |
+ } |
+ } |
+ |
+ TestMessage.prototype.addByte = function(item) { |
+ if (!/^[01]{8}$/.test(item.value)) |
+ throw new InputError('invalid byte item value', item.line); |
+ function b(i) { |
+ return (item.value.charAt(7 - i) == '1') ? 1 << i : 0; |
+ } |
+ var n = b(0) | b(1) | b(2) | b(3) | b(4) | b(5) | b(6) | b(7); |
+ this.buffer.setUint8(this.index, n); |
+ this.index += 1; |
+ } |
+ |
+ TestMessage.prototype.addDistance = function(item) { |
+ if (this.distances[item.value]) |
+ throw new InputError('duplicate distance item', item.line); |
+ this.distances[item.value] = {index: this.index, item: item}; |
+ this.index += item.type == 'dist4' ? 4 : 8; |
+ } |
+ |
+ TestMessage.prototype.addAnchor = function(item) { |
+ var dist = this.distances[item.value]; |
+ if (!dist) |
+ throw new InputError('unmatched anchor item', item.line); |
+ delete this.distances[item.value]; |
+ |
+ var n = this.index - dist.index; |
+ // TODO(hansmuller): validate n |
+ |
+ if (dist.item.type == 'dist4') |
+ this.buffer.setUint32(dist.index, n); |
+ else if (dist.item.type == 'dist8') |
+ this.buffer.setUint64(dist.index, n); |
+ else |
+ throw new InputError('unrecognzed distance item type', dist.item.line); |
+ } |
+ |
+ TestMessage.prototype.addHandles = function(item) { |
+ this.handleCount = parseInt(item.value); |
+ if (Number.isNaN(this.handleCount)) |
+ throw new InputError("can't parse handleCount", item.line); |
+ } |
+ |
+ TestMessage.prototype.addItem = function(item) { |
+ if (item.isNumber()) |
+ this.addNumber(item); |
+ else if (item.isByte()) |
+ this.addByte(item); |
+ else if (item.isDistance()) |
+ this.addDistance(item); |
+ else if (item.isAnchor()) |
+ this.addAnchor(item); |
+ else if (item.isHandles()) |
+ this.addHandles(item); |
+ else |
+ throw new InputError('unrecognized item type', item.line); |
+ } |
+ |
+ TestMessage.prototype.unanchoredDistances = function() { |
+ var names = null; |
+ for (var name in this.distances) { |
+ if (this.distances.hasOwnProperty(name)) |
+ names = (names === null) ? name : names + ' ' + name; |
+ } |
+ return names; |
+ } |
+ |
+ function parseTestMessage(file) { |
+ var items = []; |
+ var messageLength = 0; |
+ while(!file.isEmpty()) { |
+ var line = file.nextLine(); |
+ while (!line.isEmpty()) { |
+ var item = line.nextItem(); |
+ if (item.isHandles() && items.length > 0) |
+ throw new InputError('handles item is not first'); |
+ messageLength += item.size; |
+ items.push(item); |
+ } |
+ } |
+ |
+ var msg = new TestMessage(messageLength); |
+ for (var i = 0; i < items.length; i++) |
+ msg.addItem(items[i]); |
+ |
+ if (messageLength != msg.index) |
+ throw new InputError('failed to compute message length'); |
+ var names = msg.unanchoredDistances(); |
+ if (names) |
+ throw new InputError('no anchors for ' + names, 0); |
+ |
+ return msg; |
+ } |
+ |
+ // Verify that the TestMessage (et al) data file loading code is OK. |
+ |
+ function checkMessageFileParser() { |
+ function ParserTestFailure(message, file) { |
+ this.message = message; |
+ this.file = file; |
+ } |
+ |
+ ParserTestFailure.prototype.toString = function() { |
+ return 'Error: ' + this.message + ' for "' + this.file.contents + '"'; |
+ } |
+ |
+ function checkData(data, expectedData, file) { |
+ if (data.byteLength != expectedData.byteLength) { |
+ var s = "message length (" + data.byteLength + ") doesn't match " + |
+ "expected length: " + expectedData.byteLength; |
yzshen1
2014/07/23 20:31:10
wrong indent.
hansmuller
2014/07/24 00:36:37
Done.
|
+ throw new ParserTestFailure(s, file); |
+ } |
+ |
+ for (var i = 0; i < data.byteLength; i++) { |
+ if (data.getUint8(i) != expectedData.getUint8(i)) { |
+ var s = 'message data mismatch at byte offset ' + i; |
+ throw new ParserTestFailure(s, file); |
+ } |
+ } |
+ } |
+ |
+ function testFloatItems() { |
+ var file = new File('[f]+.3e9 [d]-10.03'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new buffer.Buffer(12); |
+ expectedData.setFloat32(0, +.3e9); |
+ expectedData.setFloat64(4, -10.03); |
+ checkData(msg.buffer, expectedData, file); |
+ } |
+ |
+ function testUnsignedIntegerItems() { |
+ var file = new File('[u1]0x10// hello world !! \n\r \t [u2]65535 \n' + |
+ '[u4]65536 [u8]0xFFFFFFFFFFFFF 0 0Xff'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new buffer.Buffer(17); |
+ expectedData.setUint8(0, 0x10); |
+ expectedData.setUint16(1, 65535); |
+ expectedData.setUint32(3, 65536); |
+ expectedData.setUint64(7, 0xFFFFFFFFFFFFF); |
+ expectedData.setUint8(15, 0); |
+ expectedData.setUint8(16, 0xff); |
+ checkData(msg.buffer, expectedData, file); |
+ } |
+ |
+ function testSignedIntegerItems() { |
+ var file = new File('[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new buffer.Buffer(15); |
+ expectedData.setInt64(0, -0x800); |
+ expectedData.setInt8(8, -128); |
+ expectedData.setInt16(9, 0); |
+ expectedData.setInt32(11, -40); |
+ checkData(msg.buffer, expectedData, file); |
+ } |
+ |
+ function testByteItems() { |
+ var file = |
+ new File('[b]00001011 [b]10000000 // hello world\n [b]00000000'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new buffer.Buffer(3); |
+ expectedData.setUint8(0, 11); |
+ expectedData.setUint8(1, 128); |
+ expectedData.setUint8(2, 0); |
+ checkData(msg.buffer, expectedData, file); |
+ } |
+ |
+ function testAnchors() { |
+ var file = new File('[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new buffer.Buffer(14); |
+ expectedData.setUint32(0, 14); |
+ expectedData.setUint8(4, 0); |
+ expectedData.setUint64(5, 9); |
+ expectedData.setUint8(13, 0); |
+ checkData(msg.buffer, expectedData, file); |
+ } |
+ |
+ function testHandles() { |
+ var file = new File('// This message has handles! \n[handles]50 [u8]2'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new buffer.Buffer(8); |
+ expectedData.setUint64(0, 2); |
+ |
+ if (msg.handleCount != 50) { |
+ var s = 'wrong handle count (' + msg.handleCount + ')'; |
+ throw new ParserTestFailure(s, file); |
+ } |
+ checkData(msg.buffer, expectedData, file); |
+ } |
+ |
+ function testEmptyInput() { |
+ var file = new File(''); |
+ var msg = parseTestMessage(file); |
+ if (msg.buffer.byteLength != 0) |
+ throw new ParserTestFailure('expected empty message', file); |
+ } |
+ |
+ function testBlankInput() { |
+ var file = |
+ new File(' \t // hello world \n\r \t// the answer is 42 '); |
+ var msg = parseTestMessage(file); |
+ if (msg.buffer.byteLength != 0) |
+ throw new ParserTestFailure('expected empty message', file); |
+ } |
+ |
+ function testInvalidInput() { |
+ function parserShouldFail(fileContents) { |
+ var file = new File(fileContents); |
+ try { |
+ parseTestMessage(file); |
+ } catch (e) { |
+ if (e instanceof InputError) |
+ return; |
+ throw new ParserTestFailure( |
+ 'unexpected exception ' + e.toString(), file); |
+ } |
+ throw new ParserTestFailure("didn't detect invalid input", file); |
+ } |
+ |
+ ['/ hello world', |
+ '[u1]x', |
+ '[u2]-1000', |
+ '[u1]0x100', |
+ '[s2]-0x8001', |
+ '[b]1', |
+ '[b]1111111k', |
+ '[dist4]unmatched', |
+ '[anchr]hello [dist8]hello', |
+ '[dist4]a [dist4]a [anchr]a', |
+ // '[dist4]a [anchr]a [dist4]a [anchr]a', |
+ '0 [handles]50' |
+ ].forEach(parserShouldFail); |
+ } |
+ |
+ try { |
+ testFloatItems(); |
+ testUnsignedIntegerItems(); |
+ testSignedIntegerItems(); |
+ testByteItems(); |
+ testInvalidInput(); |
+ testEmptyInput(); |
+ testBlankInput(); |
+ testHandles(); |
+ testAnchors(); |
+ } catch (e) { |
+ return e; |
+ } |
+ return null; |
+ } |
+ |
+ var exports = {}; |
+ exports.checkMessageFileParser = checkMessageFileParser; |
+ return exports; |
+}); |