Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: mojo/public/js/tests/validation_unittest.js

Issue 2745293005: Moving mojo/validation test into LayoutTests (Closed)
Patch Set: . Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 define([
6 "console",
7 "file",
8 "gin/test/expect",
9 "mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom",
10 "mojo/public/js/bindings",
11 "mojo/public/js/buffer",
12 "mojo/public/js/codec",
13 "mojo/public/js/core",
14 "mojo/public/js/tests/validation_test_input_parser",
15 "mojo/public/js/validator",
16 ], function(console,
17 file,
18 expect,
19 testInterface,
20 bindings,
21 buffer,
22 codec,
23 core,
24 parser,
25 validator) {
26
27 var noError = validator.validationError.NONE;
28
29 function checkTestMessageParser() {
30 function TestMessageParserFailure(message, input) {
31 this.message = message;
32 this.input = input;
33 }
34
35 TestMessageParserFailure.prototype.toString = function() {
36 return 'Error: ' + this.message + ' for "' + this.input + '"';
37 };
38
39 function checkData(data, expectedData, input) {
40 if (data.byteLength != expectedData.byteLength) {
41 var s = "message length (" + data.byteLength + ") doesn't match " +
42 "expected length: " + expectedData.byteLength;
43 throw new TestMessageParserFailure(s, input);
44 }
45
46 for (var i = 0; i < data.byteLength; i++) {
47 if (data.getUint8(i) != expectedData.getUint8(i)) {
48 var s = 'message data mismatch at byte offset ' + i;
49 throw new TestMessageParserFailure(s, input);
50 }
51 }
52 }
53
54 function testFloatItems() {
55 var input = '[f]+.3e9 [d]-10.03';
56 var msg = parser.parseTestMessage(input);
57 var expectedData = new buffer.Buffer(12);
58 expectedData.setFloat32(0, +.3e9);
59 expectedData.setFloat64(4, -10.03);
60 checkData(msg.buffer, expectedData, input);
61 }
62
63 function testUnsignedIntegerItems() {
64 var input = '[u1]0x10// hello world !! \n\r \t [u2]65535 \n' +
65 '[u4]65536 [u8]0xFFFFFFFFFFFFF 0 0Xff';
66 var msg = parser.parseTestMessage(input);
67 var expectedData = new buffer.Buffer(17);
68 expectedData.setUint8(0, 0x10);
69 expectedData.setUint16(1, 65535);
70 expectedData.setUint32(3, 65536);
71 expectedData.setUint64(7, 0xFFFFFFFFFFFFF);
72 expectedData.setUint8(15, 0);
73 expectedData.setUint8(16, 0xff);
74 checkData(msg.buffer, expectedData, input);
75 }
76
77 function testSignedIntegerItems() {
78 var input = '[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40';
79 var msg = parser.parseTestMessage(input);
80 var expectedData = new buffer.Buffer(15);
81 expectedData.setInt64(0, -0x800);
82 expectedData.setInt8(8, -128);
83 expectedData.setInt16(9, 0);
84 expectedData.setInt32(11, -40);
85 checkData(msg.buffer, expectedData, input);
86 }
87
88 function testByteItems() {
89 var input = '[b]00001011 [b]10000000 // hello world\n [b]00000000';
90 var msg = parser.parseTestMessage(input);
91 var expectedData = new buffer.Buffer(3);
92 expectedData.setUint8(0, 11);
93 expectedData.setUint8(1, 128);
94 expectedData.setUint8(2, 0);
95 checkData(msg.buffer, expectedData, input);
96 }
97
98 function testAnchors() {
99 var input = '[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar';
100 var msg = parser.parseTestMessage(input);
101 var expectedData = new buffer.Buffer(14);
102 expectedData.setUint32(0, 14);
103 expectedData.setUint8(4, 0);
104 expectedData.setUint64(5, 9);
105 expectedData.setUint8(13, 0);
106 checkData(msg.buffer, expectedData, input);
107 }
108
109 function testHandles() {
110 var input = '// This message has handles! \n[handles]50 [u8]2';
111 var msg = parser.parseTestMessage(input);
112 var expectedData = new buffer.Buffer(8);
113 expectedData.setUint64(0, 2);
114
115 if (msg.handleCount != 50) {
116 var s = 'wrong handle count (' + msg.handleCount + ')';
117 throw new TestMessageParserFailure(s, input);
118 }
119 checkData(msg.buffer, expectedData, input);
120 }
121
122 function testEmptyInput() {
123 var msg = parser.parseTestMessage('');
124 if (msg.buffer.byteLength != 0)
125 throw new TestMessageParserFailure('expected empty message', '');
126 }
127
128 function testBlankInput() {
129 var input = ' \t // hello world \n\r \t// the answer is 42 ';
130 var msg = parser.parseTestMessage(input);
131 if (msg.buffer.byteLength != 0)
132 throw new TestMessageParserFailure('expected empty message', input);
133 }
134
135 function testInvalidInput() {
136 function parserShouldFail(input) {
137 try {
138 parser.parseTestMessage(input);
139 } catch (e) {
140 if (e instanceof parser.InputError)
141 return;
142 throw new TestMessageParserFailure(
143 'unexpected exception ' + e.toString(), input);
144 }
145 throw new TestMessageParserFailure("didn't detect invalid input", file);
146 }
147
148 ['/ hello world',
149 '[u1]x',
150 '[u2]-1000',
151 '[u1]0x100',
152 '[s2]-0x8001',
153 '[b]1',
154 '[b]1111111k',
155 '[dist4]unmatched',
156 '[anchr]hello [dist8]hello',
157 '[dist4]a [dist4]a [anchr]a',
158 // '[dist4]a [anchr]a [dist4]a [anchr]a',
159 '0 [handles]50'
160 ].forEach(parserShouldFail);
161 }
162
163 try {
164 testFloatItems();
165 testUnsignedIntegerItems();
166 testSignedIntegerItems();
167 testByteItems();
168 testInvalidInput();
169 testEmptyInput();
170 testBlankInput();
171 testHandles();
172 testAnchors();
173 } catch (e) {
174 return e.toString();
175 }
176 return null;
177 }
178
179 function getMessageTestFiles(prefix) {
180 var sourceRoot = file.getSourceRootDirectory();
181 expect(sourceRoot).not.toBeNull();
182
183 var testDir = sourceRoot +
184 "/mojo/public/interfaces/bindings/tests/data/validation/";
185 var testFiles = file.getFilesInDirectory(testDir);
186 expect(testFiles).not.toBeNull();
187 expect(testFiles.length).toBeGreaterThan(0);
188
189 // The matching ".data" pathnames with the extension removed.
190 return testFiles.filter(function(s) {
191 return s.substr(-5) == ".data" && s.indexOf(prefix) == 0;
192 }).map(function(s) {
193 return testDir + s.slice(0, -5);
194 });
195 }
196
197 function readTestMessage(filename) {
198 var contents = file.readFileToString(filename + ".data");
199 expect(contents).not.toBeNull();
200 return parser.parseTestMessage(contents);
201 }
202
203 function readTestExpected(filename) {
204 var contents = file.readFileToString(filename + ".expected");
205 expect(contents).not.toBeNull();
206 return contents.trim();
207 }
208
209 function checkValidationResult(testFile, err) {
210 var actualResult = (err === noError) ? "PASS" : err;
211 var expectedResult = readTestExpected(testFile);
212 if (actualResult != expectedResult)
213 console.log("[Test message validation failed: " + testFile + " ]");
214 expect(actualResult).toEqual(expectedResult);
215 }
216
217 function testMessageValidation(prefix, filters) {
218 var testFiles = getMessageTestFiles(prefix);
219 expect(testFiles.length).toBeGreaterThan(0);
220
221 for (var i = 0; i < testFiles.length; i++) {
222 // TODO(hansmuller) Temporarily skipping array pointer overflow tests
223 // because JS numbers are limited to 53 bits.
224 // TODO(rudominer): Temporarily skipping 'no-such-method',
225 // 'invalid_request_flags', and 'invalid_response_flags' until additional
226 // logic in *RequestValidator and *ResponseValidator is ported from
227 // cpp to js.
228 // TODO(crbug/640298): Implement max recursion depth for JS.
229 // TODO(crbug/628104): Support struct map keys for JS.
230 if (testFiles[i].indexOf("overflow") != -1 ||
231 testFiles[i].indexOf("conformance_mthd19") != -1 ||
232 testFiles[i].indexOf("conformance_mthd20") != -1 ||
233 testFiles[i].indexOf("no_such_method") != -1 ||
234 testFiles[i].indexOf("invalid_request_flags") != -1 ||
235 testFiles[i].indexOf("invalid_response_flags") != -1) {
236 console.log("[Skipping " + testFiles[i] + "]");
237 continue;
238 }
239
240 var testMessage = readTestMessage(testFiles[i]);
241 var handles = new Array(testMessage.handleCount);
242 var message = new codec.Message(testMessage.buffer, handles);
243 var messageValidator = new validator.Validator(message);
244
245 var err = messageValidator.validateMessageHeader();
246 for (var j = 0; err === noError && j < filters.length; ++j)
247 err = filters[j](messageValidator);
248
249 checkValidationResult(testFiles[i], err);
250 }
251 }
252
253 function testConformanceMessageValidation() {
254 testMessageValidation("conformance_", [
255 testInterface.ConformanceTestInterface.validateRequest]);
256 }
257
258 function testBoundsCheckMessageValidation() {
259 testMessageValidation("boundscheck_", [
260 testInterface.BoundsCheckTestInterface.validateRequest]);
261 }
262
263 function testResponseConformanceMessageValidation() {
264 testMessageValidation("resp_conformance_", [
265 testInterface.ConformanceTestInterface.validateResponse]);
266 }
267
268 function testResponseBoundsCheckMessageValidation() {
269 testMessageValidation("resp_boundscheck_", [
270 testInterface.BoundsCheckTestInterface.validateResponse]);
271 }
272
273 function testIntegratedMessageValidation(testFilesPattern, endpoint) {
274 var testFiles = getMessageTestFiles(testFilesPattern);
275 expect(testFiles.length).toBeGreaterThan(0);
276
277 var testMessagePipe = core.createMessagePipe();
278 expect(testMessagePipe.result).toBe(core.RESULT_OK);
279
280 endpoint.bind(testMessagePipe.handle1);
281 var testingController = endpoint.enableTestingMode();
282
283 var validationError;
284 testingController.setInvalidIncomingMessageHandler(function(error) {
285 validationError = error;
286 });
287
288 for (var i = 0; i < testFiles.length; i++) {
289 validationError = noError;
290 var testMessage = readTestMessage(testFiles[i]);
291 var handles = new Array(testMessage.handleCount);
292
293 var writeMessageValue = core.writeMessage(
294 testMessagePipe.handle0,
295 new Uint8Array(testMessage.buffer.arrayBuffer),
296 new Array(testMessage.handleCount),
297 core.WRITE_MESSAGE_FLAG_NONE);
298 expect(writeMessageValue).toBe(core.RESULT_OK);
299
300 testingController.waitForNextMessage();
301 checkValidationResult(testFiles[i], validationError);
302 }
303
304 expect(core.close(testMessagePipe.handle0)).toBe(core.RESULT_OK);
305 }
306
307 function testIntegratedMessageHeaderValidation() {
308 testIntegratedMessageValidation(
309 "integration_msghdr",
310 new bindings.Binding(testInterface.IntegrationTestInterface, {}));
311 testIntegratedMessageValidation(
312 "integration_msghdr",
313 new testInterface.IntegrationTestInterfacePtr().ptr);
314 }
315
316 function testIntegratedRequestMessageValidation() {
317 testIntegratedMessageValidation(
318 "integration_intf_rqst",
319 new bindings.Binding(testInterface.IntegrationTestInterface, {}));
320 }
321
322 function testIntegratedResponseMessageValidation() {
323 testIntegratedMessageValidation(
324 "integration_intf_resp",
325 new testInterface.IntegrationTestInterfacePtr().ptr);
326 }
327
328 expect(checkTestMessageParser()).toBeNull();
329 testConformanceMessageValidation();
330 testBoundsCheckMessageValidation();
331 testResponseConformanceMessageValidation();
332 testResponseBoundsCheckMessageValidation();
333 testIntegratedMessageHeaderValidation();
334 testIntegratedResponseMessageValidation();
335 testIntegratedRequestMessageValidation();
336
337 this.result = "PASS";
338 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698