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

Side by Side Diff: mojo/public/js/bindings/tests/validation_test_input_parser.js

Issue 411553003: Validate incoming JS Message Headers: test message parser (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Restored parser comment changes Created 6 years, 5 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 | Annotate | Revision Log
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 // Support for parsing binary sequences encoded as readable strings
6 // or ".data" files. The input format is described here:
7 // mojo/public/cpp/bindings/tests/validation_test_input_parser.h
8
9 define([
10 "mojo/public/js/bindings/buffer"
11 ], function(buffer) {
12
13 // Files and Lines represent the raw text from an input string
14 // or ".data" file.
15
16 function InputError(message, line) {
17 this.message = message;
18 this.line = line;
19 }
20
21 InputError.prototype.toString = function() {
22 var s = 'Error: ' + this.message;
23 if (this.line)
24 s += ', at line ' +
25 (this.line.number + 1) + ': "' + this.line.contents + '"';
26 return s;
27 }
28
29 function File(contents) {
30 this.contents = contents;
31 this.index = 0;
32 this.lineNumber = 0;
33 }
34
35 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()".
36 return this.index >= this.contents.length;
37 }
38
39 File.prototype.nextLine = function() {
40 if (this.isEmpty())
41 return null;
42 var start = this.index;
43 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
44 if (end == -1)
45 end = this.contents.length;
46 this.index = end + 1;
47 return new Line(this.contents.substring(start, end), this.lineNumber++);
48 }
49
50 function Line(contents, number) {
51 var i = contents.indexOf('//');
52 var s = (i == -1) ? contents.trim() : contents.substring(0, i).trim();
53 this.contents = contents;
54 this.items = (s.length > 0) ? s.split(/\s+/) : [];
55 this.index = 0;
56 this.number = number;
57 }
58
59 Line.prototype.isEmpty = function() {
60 return this.index >= this.items.length;
61 }
62
63 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
64 u1: 1, u2: 2, u4: 4, u8: 8, s1: 1, s2: 2, s4: 4, s8: 8, b: 1, f: 4, d: 8,
65 dist4: 4, dist8: 8, anchr: 0, handles: 0
66 };
67
68 function isValidItemType(type) {
69 return itemTypeSizes[type] !== undefined;
70 }
71
72 Line.prototype.nextItem = function() {
73 if (this.isEmpty())
74 return null;
75
76 var itemString = this.items[this.index++];
77 var type = 'u1';
78 var value = itemString;
79
80 if (itemString.charAt(0) == '[') {
81 var i = itemString.indexOf(']');
82 if (i != -1 && i + 1 < itemString.length) {
83 type = itemString.substring(1, i);
84 value = itemString.substring(i + 1);
85 } else {
86 throw new InputError('invalid item', this);
87 }
88 }
89 if (!isValidItemType(type))
90 throw new InputError('invalid item type', this);
91
92 return new Item(this, type, value);
93 }
94
95 // The text for Each whitespace delimited binary data "item"
96 // 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
97
98 function Item(line, type, value) {
99 this.line = line;
100 this.type = type;
101 this.value = value;
102 this.size = itemTypeSizes[type];
103 }
104
105 Item.prototype.isFloat = function() {
106 return this.type == 'f' || this.type == 'd';
107 }
108
109 Item.prototype.isInteger = function() {
110 return ['u1', 'u2', 'u4', 'u8',
111 's1', 's2', 's4', 's8'].indexOf(this.type) != -1;
112 }
113
114 Item.prototype.isNumber = function() {
115 return this.isFloat() || this.isInteger();
116 }
117
118 Item.prototype.isByte = function() {
119 return this.type == 'b';
120 }
121
122 Item.prototype.isDistance = function() {
123 return this.type == 'dist4' || this.type == 'dist8';
124 }
125
126 Item.prototype.isAnchor = function() {
127 return this.type == 'anchr';
128 }
129
130 Item.prototype.isHandles = function() {
131 return this.type == 'handles';
132 }
133
134 // A TestMessage represents the complete binary message loaded from
135 // an input string or ".data" file. The parseTestMessage() function
136 // below constructs a TestMessage from a File.
137
138 function TestMessage(byteLength) {
139 this.index = 0;
140 this.buffer = new buffer.Buffer(byteLength);
141 this.distances = {};
142 this.handleCount = 0;
143 }
144
145 function checkItemNumberValue(item, n, min, max) {
146 if (n < min || n > max)
147 throw new InputError('invalid item value', item.line);
148 }
149
150 TestMessage.prototype.addNumber = function(item) {
151 var n = item.isInteger() ? parseInt(item.value) : parseFloat(item.value);
152 if (Number.isNaN(n))
153 throw new InputError("can't parse item value", item.line);
154
155 switch(item.type) {
156 case 'u1':
yzshen1 2014/07/23 20:31:10 2 more spaces of indent, please.
hansmuller 2014/07/24 00:36:37 Done.
157 checkItemNumberValue(item, n, 0, 0xFF);
158 this.buffer.setUint8(this.index, n);
159 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().
160 break;
161 case 'u2':
162 checkItemNumberValue(item, n, 0, 0xFFFF);
163 this.buffer.setUint16(this.index, n);
164 this.index += 2;
165 break;
166 case 'u4':
167 checkItemNumberValue(item, n, 0, 0xFFFFFFFF);
168 this.buffer.setUint32(this.index, n);
169 this.index += 4;
170 break;
171 case 'u8':
172 checkItemNumberValue(item, n, 0, Number.MAX_SAFE_INTEGER);
173 this.buffer.setUint64(this.index, n);
174 this.index += 8;
175 break;
176 case 's1':
177 checkItemNumberValue(item, n, -128, 127);
178 this.buffer.setInt8(this.index, n);
179 this.index += 1;
180 break;
181 case 's2':
182 checkItemNumberValue(item, n, -32768, 32767);
183 this.buffer.setInt16(this.index, n);
184 this.index += 2;
185 break;
186 case 's4':
187 checkItemNumberValue(item, n, -2147483648, 2147483647);
188 this.buffer.setInt32(this.index, n);
189 this.index += 4;
190 break;
191 case 's8':
192 checkItemNumberValue(item, n,
193 Number.MIN_SAFE_INTEGER,
194 Number.MAX_SAFE_INTEGER);
195 this.buffer.setInt64(this.index, n);
196 this.index += 8;
197 break;
198 case 'f':
199 this.buffer.setFloat32(this.index, n);
200 this.index += 4;
201 break;
202 case 'd':
203 this.buffer.setFloat64(this.index, n);
204 this.index += 8;
205 break;
206
207 default:
208 throw new InputError('unrecognized item type', item.line);
209 }
210 }
211
212 TestMessage.prototype.addByte = function(item) {
213 if (!/^[01]{8}$/.test(item.value))
214 throw new InputError('invalid byte item value', item.line);
215 function b(i) {
216 return (item.value.charAt(7 - i) == '1') ? 1 << i : 0;
217 }
218 var n = b(0) | b(1) | b(2) | b(3) | b(4) | b(5) | b(6) | b(7);
219 this.buffer.setUint8(this.index, n);
220 this.index += 1;
221 }
222
223 TestMessage.prototype.addDistance = function(item) {
224 if (this.distances[item.value])
225 throw new InputError('duplicate distance item', item.line);
226 this.distances[item.value] = {index: this.index, item: item};
227 this.index += item.type == 'dist4' ? 4 : 8;
228 }
229
230 TestMessage.prototype.addAnchor = function(item) {
231 var dist = this.distances[item.value];
232 if (!dist)
233 throw new InputError('unmatched anchor item', item.line);
234 delete this.distances[item.value];
235
236 var n = this.index - dist.index;
237 // TODO(hansmuller): validate n
238
239 if (dist.item.type == 'dist4')
240 this.buffer.setUint32(dist.index, n);
241 else if (dist.item.type == 'dist8')
242 this.buffer.setUint64(dist.index, n);
243 else
244 throw new InputError('unrecognzed distance item type', dist.item.line);
245 }
246
247 TestMessage.prototype.addHandles = function(item) {
248 this.handleCount = parseInt(item.value);
249 if (Number.isNaN(this.handleCount))
250 throw new InputError("can't parse handleCount", item.line);
251 }
252
253 TestMessage.prototype.addItem = function(item) {
254 if (item.isNumber())
255 this.addNumber(item);
256 else if (item.isByte())
257 this.addByte(item);
258 else if (item.isDistance())
259 this.addDistance(item);
260 else if (item.isAnchor())
261 this.addAnchor(item);
262 else if (item.isHandles())
263 this.addHandles(item);
264 else
265 throw new InputError('unrecognized item type', item.line);
266 }
267
268 TestMessage.prototype.unanchoredDistances = function() {
269 var names = null;
270 for (var name in this.distances) {
271 if (this.distances.hasOwnProperty(name))
272 names = (names === null) ? name : names + ' ' + name;
273 }
274 return names;
275 }
276
277 function parseTestMessage(file) {
278 var items = [];
279 var messageLength = 0;
280 while(!file.isEmpty()) {
281 var line = file.nextLine();
282 while (!line.isEmpty()) {
283 var item = line.nextItem();
284 if (item.isHandles() && items.length > 0)
285 throw new InputError('handles item is not first');
286 messageLength += item.size;
287 items.push(item);
288 }
289 }
290
291 var msg = new TestMessage(messageLength);
292 for (var i = 0; i < items.length; i++)
293 msg.addItem(items[i]);
294
295 if (messageLength != msg.index)
296 throw new InputError('failed to compute message length');
297 var names = msg.unanchoredDistances();
298 if (names)
299 throw new InputError('no anchors for ' + names, 0);
300
301 return msg;
302 }
303
304 // Verify that the TestMessage (et al) data file loading code is OK.
305
306 function checkMessageFileParser() {
307 function ParserTestFailure(message, file) {
308 this.message = message;
309 this.file = file;
310 }
311
312 ParserTestFailure.prototype.toString = function() {
313 return 'Error: ' + this.message + ' for "' + this.file.contents + '"';
314 }
315
316 function checkData(data, expectedData, file) {
317 if (data.byteLength != expectedData.byteLength) {
318 var s = "message length (" + data.byteLength + ") doesn't match " +
319 "expected length: " + expectedData.byteLength;
yzshen1 2014/07/23 20:31:10 wrong indent.
hansmuller 2014/07/24 00:36:37 Done.
320 throw new ParserTestFailure(s, file);
321 }
322
323 for (var i = 0; i < data.byteLength; i++) {
324 if (data.getUint8(i) != expectedData.getUint8(i)) {
325 var s = 'message data mismatch at byte offset ' + i;
326 throw new ParserTestFailure(s, file);
327 }
328 }
329 }
330
331 function testFloatItems() {
332 var file = new File('[f]+.3e9 [d]-10.03');
333 var msg = parseTestMessage(file);
334 var expectedData = new buffer.Buffer(12);
335 expectedData.setFloat32(0, +.3e9);
336 expectedData.setFloat64(4, -10.03);
337 checkData(msg.buffer, expectedData, file);
338 }
339
340 function testUnsignedIntegerItems() {
341 var file = new File('[u1]0x10// hello world !! \n\r \t [u2]65535 \n' +
342 '[u4]65536 [u8]0xFFFFFFFFFFFFF 0 0Xff');
343 var msg = parseTestMessage(file);
344 var expectedData = new buffer.Buffer(17);
345 expectedData.setUint8(0, 0x10);
346 expectedData.setUint16(1, 65535);
347 expectedData.setUint32(3, 65536);
348 expectedData.setUint64(7, 0xFFFFFFFFFFFFF);
349 expectedData.setUint8(15, 0);
350 expectedData.setUint8(16, 0xff);
351 checkData(msg.buffer, expectedData, file);
352 }
353
354 function testSignedIntegerItems() {
355 var file = new File('[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40');
356 var msg = parseTestMessage(file);
357 var expectedData = new buffer.Buffer(15);
358 expectedData.setInt64(0, -0x800);
359 expectedData.setInt8(8, -128);
360 expectedData.setInt16(9, 0);
361 expectedData.setInt32(11, -40);
362 checkData(msg.buffer, expectedData, file);
363 }
364
365 function testByteItems() {
366 var file =
367 new File('[b]00001011 [b]10000000 // hello world\n [b]00000000');
368 var msg = parseTestMessage(file);
369 var expectedData = new buffer.Buffer(3);
370 expectedData.setUint8(0, 11);
371 expectedData.setUint8(1, 128);
372 expectedData.setUint8(2, 0);
373 checkData(msg.buffer, expectedData, file);
374 }
375
376 function testAnchors() {
377 var file = new File('[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar');
378 var msg = parseTestMessage(file);
379 var expectedData = new buffer.Buffer(14);
380 expectedData.setUint32(0, 14);
381 expectedData.setUint8(4, 0);
382 expectedData.setUint64(5, 9);
383 expectedData.setUint8(13, 0);
384 checkData(msg.buffer, expectedData, file);
385 }
386
387 function testHandles() {
388 var file = new File('// This message has handles! \n[handles]50 [u8]2');
389 var msg = parseTestMessage(file);
390 var expectedData = new buffer.Buffer(8);
391 expectedData.setUint64(0, 2);
392
393 if (msg.handleCount != 50) {
394 var s = 'wrong handle count (' + msg.handleCount + ')';
395 throw new ParserTestFailure(s, file);
396 }
397 checkData(msg.buffer, expectedData, file);
398 }
399
400 function testEmptyInput() {
401 var file = new File('');
402 var msg = parseTestMessage(file);
403 if (msg.buffer.byteLength != 0)
404 throw new ParserTestFailure('expected empty message', file);
405 }
406
407 function testBlankInput() {
408 var file =
409 new File(' \t // hello world \n\r \t// the answer is 42 ');
410 var msg = parseTestMessage(file);
411 if (msg.buffer.byteLength != 0)
412 throw new ParserTestFailure('expected empty message', file);
413 }
414
415 function testInvalidInput() {
416 function parserShouldFail(fileContents) {
417 var file = new File(fileContents);
418 try {
419 parseTestMessage(file);
420 } catch (e) {
421 if (e instanceof InputError)
422 return;
423 throw new ParserTestFailure(
424 'unexpected exception ' + e.toString(), file);
425 }
426 throw new ParserTestFailure("didn't detect invalid input", file);
427 }
428
429 ['/ hello world',
430 '[u1]x',
431 '[u2]-1000',
432 '[u1]0x100',
433 '[s2]-0x8001',
434 '[b]1',
435 '[b]1111111k',
436 '[dist4]unmatched',
437 '[anchr]hello [dist8]hello',
438 '[dist4]a [dist4]a [anchr]a',
439 // '[dist4]a [anchr]a [dist4]a [anchr]a',
440 '0 [handles]50'
441 ].forEach(parserShouldFail);
442 }
443
444 try {
445 testFloatItems();
446 testUnsignedIntegerItems();
447 testSignedIntegerItems();
448 testByteItems();
449 testInvalidInput();
450 testEmptyInput();
451 testBlankInput();
452 testHandles();
453 testAnchors();
454 } catch (e) {
455 return e;
456 }
457 return null;
458 }
459
460 var exports = {};
461 exports.checkMessageFileParser = checkMessageFileParser;
462 return exports;
463 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698