1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 "mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom",
9 "mojo/public/js/bindings/buffer",
10 "mojo/public/js/bindings/codec",
11 "mojo/public/js/bindings/tests/validation_test_input_parser",
12 "mojo/public/js/bindings/validator",
13 ], function(file, expect, testInterface, buffer, codec, parser, validator) {
15 function checkTestMessageParser() {
16 function TestMessageParserFailure(message, input) {
17 this.message = message;
21 TestMessageParserFailure.prototype.toString = function() {
22 return 'Error: ' + this.message + ' for "' + this.input + '"';
25 function checkData(data, expectedData, input) {
26 if (data.byteLength != expectedData.byteLength) {
27 var s = "message length (" + data.byteLength + ") doesn't match " +
28 "expected length: " + expectedData.byteLength;
29 throw new TestMessageParserFailure(s, input);
32 for (var i = 0; i < data.byteLength; i++) {
33 if (data.getUint8(i) != expectedData.getUint8(i)) {
34 var s = 'message data mismatch at byte offset ' + i;
35 throw new TestMessageParserFailure(s, input);
40 function testFloatItems() {
41 var input = '[f]+.3e9 [d]-10.03';
42 var msg = parser.parseTestMessage(input);
43 var expectedData = new buffer.Buffer(12);
44 expectedData.setFloat32(0, +.3e9);
45 expectedData.setFloat64(4, -10.03);
46 checkData(msg.buffer, expectedData, input);
49 function testUnsignedIntegerItems() {
50 var input = '[u1]0x10// hello world !! \n\r \t [u2]65535 \n' +
51 '[u4]65536 [u8]0xFFFFFFFFFFFFF 0 0Xff';
52 var msg = parser.parseTestMessage(input);
53 var expectedData = new buffer.Buffer(17);
54 expectedData.setUint8(0, 0x10);
55 expectedData.setUint16(1, 65535);
56 expectedData.setUint32(3, 65536);
57 expectedData.setUint64(7, 0xFFFFFFFFFFFFF);
58 expectedData.setUint8(15, 0);
59 expectedData.setUint8(16, 0xff);
60 checkData(msg.buffer, expectedData, input);
63 function testSignedIntegerItems() {
64 var input = '[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40';
65 var msg = parser.parseTestMessage(input);
66 var expectedData = new buffer.Buffer(15);
67 expectedData.setInt64(0, -0x800);
68 expectedData.setInt8(8, -128);
69 expectedData.setInt16(9, 0);
70 expectedData.setInt32(11, -40);
71 checkData(msg.buffer, expectedData, input);
74 function testByteItems() {
75 var input = '[b]00001011 [b]10000000 // hello world\n [b]00000000';
76 var msg = parser.parseTestMessage(input);
77 var expectedData = new buffer.Buffer(3);
78 expectedData.setUint8(0, 11);
79 expectedData.setUint8(1, 128);
80 expectedData.setUint8(2, 0);
81 checkData(msg.buffer, expectedData, input);
84 function testAnchors() {
85 var input = '[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar';
86 var msg = parser.parseTestMessage(input);
87 var expectedData = new buffer.Buffer(14);
88 expectedData.setUint32(0, 14);
89 expectedData.setUint8(4, 0);
90 expectedData.setUint64(5, 9);
91 expectedData.setUint8(13, 0);
92 checkData(msg.buffer, expectedData, input);
95 function testHandles() {
96 var input = '// This message has handles! \n[handles]50 [u8]2';
97 var msg = parser.parseTestMessage(input);
98 var expectedData = new buffer.Buffer(8);
99 expectedData.setUint64(0, 2);
101 if (msg.handleCount != 50) {
102 var s = 'wrong handle count (' + msg.handleCount + ')';
103 throw new TestMessageParserFailure(s, input);
105 checkData(msg.buffer, expectedData, input);
108 function testEmptyInput() {
109 var msg = parser.parseTestMessage('');
110 if (msg.buffer.byteLength != 0)
111 throw new TestMessageParserFailure('expected empty message', '');
114 function testBlankInput() {
115 var input = ' \t // hello world \n\r \t// the answer is 42 ';
116 var msg = parser.parseTestMessage(input);
117 if (msg.buffer.byteLength != 0)
118 throw new TestMessageParserFailure('expected empty message', input);
121 function testInvalidInput() {
122 function parserShouldFail(input) {
124 parser.parseTestMessage(input);
126 if (e instanceof parser.InputError)
128 throw new TestMessageParserFailure(
129 'unexpected exception ' + e.toString(), input);
131 throw new TestMessageParserFailure("didn't detect invalid input", file);
142 '[anchr]hello [dist8]hello',
143 '[dist4]a [dist4]a [anchr]a',
144 // '[dist4]a [anchr]a [dist4]a [anchr]a',
146 ].forEach(parserShouldFail);
151 testUnsignedIntegerItems();
152 testSignedIntegerItems();
165 function getMessageTestFiles() {
166 var sourceRoot = file.getSourceRootDirectory();
167 expect(sourceRoot).not.toBeNull();
169 var testDir = sourceRoot +
170 "/mojo/public/interfaces/bindings/tests/data/validation/";
171 var testFiles = file.getFilesInDirectory(testDir);
172 expect(testFiles).not.toBeNull();
173 expect(testFiles.length).toBeGreaterThan(0);
175 // The ".data" pathnames with the extension removed.
176 var testPathNames = testFiles.filter(function(s) {
177 return s.substr(-5) == ".data";
179 return testDir + s.slice(0, -5);
182 // For now, just checking the message header tests.
183 return testPathNames.filter(function(s) {
184 return s.indexOf("_msghdr_") != -1;
188 function readTestMessage(filename) {
189 var contents = file.readFileToString(filename + ".data");
190 expect(contents).not.toBeNull();
191 return parser.parseTestMessage(contents);
194 function readTestExpected(filename) {
195 var contents = file.readFileToString(filename + ".expected");
196 expect(contents).not.toBeNull();
197 return contents.trim();
200 function testValidateMessageHeader() {
201 var testFiles = getMessageTestFiles();
202 expect(testFiles.length).toBeGreaterThan(0);
204 for (var i = 0; i < testFiles.length; i++) {
205 var testMessage = readTestMessage(testFiles[i]);
206 // TODO(hansmuller): add the message handles.
207 var message = new codec.Message(testMessage.buffer);
208 var actualResult = new validator.Validator(message).validateMessage();
209 var expectedResult = readTestExpected(testFiles[i]);
210 expect(actualResult).toEqual(expectedResult);
214 testValidateMessageHeader();
215 expect(checkTestMessageParser()).toBeNull();
216 this.result = "PASS";