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

Side by Side Diff: third_party/WebKit/Source/platform/inspector_protocol/ParserTest.cpp

Issue 2251343003: [DevTools] Generate separate copies of inspector_protocol. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: win compile Created 4 years, 3 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 (c) 2016 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 #include "platform/inspector_protocol/InspectorProtocol.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7
8 namespace blink {
9 namespace protocol {
10
11 TEST(ParserTest, Reading)
12 {
13 protocol::Value* tmpValue;
14 std::unique_ptr<protocol::Value> root;
15 std::unique_ptr<protocol::Value> root2;
16 String16 strVal;
17 int intVal = 0;
18
19 // some whitespace checking
20 root = parseJSON(" null ");
21 ASSERT_TRUE(root.get());
22 EXPECT_EQ(Value::TypeNull, root->type());
23
24 // Invalid JSON string
25 root = parseJSON("nu");
26 EXPECT_FALSE(root.get());
27
28 // Simple bool
29 root = parseJSON("true ");
30 ASSERT_TRUE(root.get());
31 EXPECT_EQ(Value::TypeBoolean, root->type());
32
33 // Embedded comment
34 root = parseJSON("40 /*/");
35 EXPECT_FALSE(root.get());
36 root = parseJSON("/* comment */null");
37 ASSERT_TRUE(root.get());
38 EXPECT_EQ(Value::TypeNull, root->type());
39 root = parseJSON("40 /* comment */");
40 ASSERT_TRUE(root.get());
41 EXPECT_EQ(Value::TypeInteger, root->type());
42 EXPECT_TRUE(root->asInteger(&intVal));
43 EXPECT_EQ(40, intVal);
44 root = parseJSON("/**/ 40 /* multi-line\n comment */ // more comment");
45 ASSERT_TRUE(root.get());
46 EXPECT_EQ(Value::TypeInteger, root->type());
47 EXPECT_TRUE(root->asInteger(&intVal));
48 EXPECT_EQ(40, intVal);
49 root = parseJSON("true // comment");
50 ASSERT_TRUE(root.get());
51 EXPECT_EQ(Value::TypeBoolean, root->type());
52 root = parseJSON("/* comment */\"sample string\"");
53 ASSERT_TRUE(root.get());
54 EXPECT_TRUE(root->asString(&strVal));
55 EXPECT_EQ("sample string", strVal);
56 root = parseJSON("[1, /* comment, 2 ] */ \n 3]");
57 ASSERT_TRUE(root.get());
58 protocol::ListValue* list = ListValue::cast(root.get());
59 ASSERT_TRUE(list);
60 EXPECT_EQ(2u, list->size());
61 tmpValue = list->at(0);
62 ASSERT_TRUE(tmpValue);
63 EXPECT_TRUE(tmpValue->asInteger(&intVal));
64 EXPECT_EQ(1, intVal);
65 tmpValue = list->at(1);
66 ASSERT_TRUE(tmpValue);
67 EXPECT_TRUE(tmpValue->asInteger(&intVal));
68 EXPECT_EQ(3, intVal);
69 root = parseJSON("[1, /*a*/2, 3]");
70 ASSERT_TRUE(root.get());
71 list = ListValue::cast(root.get());
72 ASSERT_TRUE(list);
73 EXPECT_EQ(3u, list->size());
74 root = parseJSON("/* comment **/42");
75 ASSERT_TRUE(root.get());
76 EXPECT_EQ(Value::TypeInteger, root->type());
77 EXPECT_TRUE(root->asInteger(&intVal));
78 EXPECT_EQ(42, intVal);
79 root = parseJSON(
80 "/* comment **/\n"
81 "// */ 43\n"
82 "44");
83 ASSERT_TRUE(root.get());
84 EXPECT_EQ(Value::TypeInteger, root->type());
85 EXPECT_TRUE(root->asInteger(&intVal));
86 EXPECT_EQ(44, intVal);
87
88 // Test number formats
89 root = parseJSON("43");
90 ASSERT_TRUE(root.get());
91 EXPECT_EQ(Value::TypeInteger, root->type());
92 EXPECT_TRUE(root->asInteger(&intVal));
93 EXPECT_EQ(43, intVal);
94
95 // According to RFC4627, oct, hex, and leading zeros are invalid JSON.
96 root = parseJSON("043");
97 EXPECT_FALSE(root.get());
98 root = parseJSON("0x43");
99 EXPECT_FALSE(root.get());
100 root = parseJSON("00");
101 EXPECT_FALSE(root.get());
102
103 // Test 0 (which needs to be special cased because of the leading zero
104 // clause).
105 root = parseJSON("0");
106 ASSERT_TRUE(root.get());
107 EXPECT_EQ(Value::TypeInteger, root->type());
108 intVal = 1;
109 EXPECT_TRUE(root->asInteger(&intVal));
110 EXPECT_EQ(0, intVal);
111
112 // Numbers that overflow ints should succeed, being internally promoted to
113 // storage as doubles
114 root = parseJSON("2147483648");
115 ASSERT_TRUE(root.get());
116 double doubleVal;
117 EXPECT_EQ(Value::TypeDouble, root->type());
118 doubleVal = 0.0;
119 EXPECT_TRUE(root->asDouble(&doubleVal));
120 EXPECT_DOUBLE_EQ(2147483648.0, doubleVal);
121 root = parseJSON("-2147483649");
122 ASSERT_TRUE(root.get());
123 EXPECT_EQ(Value::TypeDouble, root->type());
124 doubleVal = 0.0;
125 EXPECT_TRUE(root->asDouble(&doubleVal));
126 EXPECT_DOUBLE_EQ(-2147483649.0, doubleVal);
127
128 // Parse a double
129 root = parseJSON("43.1");
130 ASSERT_TRUE(root.get());
131 EXPECT_EQ(Value::TypeDouble, root->type());
132 doubleVal = 0.0;
133 EXPECT_TRUE(root->asDouble(&doubleVal));
134 EXPECT_DOUBLE_EQ(43.1, doubleVal);
135
136 root = parseJSON("4.3e-1");
137 ASSERT_TRUE(root.get());
138 EXPECT_EQ(Value::TypeDouble, root->type());
139 doubleVal = 0.0;
140 EXPECT_TRUE(root->asDouble(&doubleVal));
141 EXPECT_DOUBLE_EQ(.43, doubleVal);
142
143 root = parseJSON("2.1e0");
144 ASSERT_TRUE(root.get());
145 EXPECT_EQ(Value::TypeDouble, root->type());
146 doubleVal = 0.0;
147 EXPECT_TRUE(root->asDouble(&doubleVal));
148 EXPECT_DOUBLE_EQ(2.1, doubleVal);
149
150 root = parseJSON("2.1e+0001");
151 ASSERT_TRUE(root.get());
152 EXPECT_EQ(Value::TypeInteger, root->type());
153 doubleVal = 0.0;
154 EXPECT_TRUE(root->asDouble(&doubleVal));
155 EXPECT_DOUBLE_EQ(21.0, doubleVal);
156
157 root = parseJSON("0.01");
158 ASSERT_TRUE(root.get());
159 EXPECT_EQ(Value::TypeDouble, root->type());
160 doubleVal = 0.0;
161 EXPECT_TRUE(root->asDouble(&doubleVal));
162 EXPECT_DOUBLE_EQ(0.01, doubleVal);
163
164 root = parseJSON("1.00");
165 ASSERT_TRUE(root.get());
166 EXPECT_EQ(Value::TypeInteger, root->type());
167 doubleVal = 0.0;
168 EXPECT_TRUE(root->asDouble(&doubleVal));
169 EXPECT_DOUBLE_EQ(1.0, doubleVal);
170
171 // Fractional parts must have a digit before and after the decimal point.
172 root = parseJSON("1.");
173 EXPECT_FALSE(root.get());
174 root = parseJSON(".1");
175 EXPECT_FALSE(root.get());
176 root = parseJSON("1.e10");
177 EXPECT_FALSE(root.get());
178
179 // Exponent must have a digit following the 'e'.
180 root = parseJSON("1e");
181 EXPECT_FALSE(root.get());
182 root = parseJSON("1E");
183 EXPECT_FALSE(root.get());
184 root = parseJSON("1e1.");
185 EXPECT_FALSE(root.get());
186 root = parseJSON("1e1.0");
187 EXPECT_FALSE(root.get());
188
189 // INF/-INF/NaN are not valid
190 root = parseJSON("NaN");
191 EXPECT_FALSE(root.get());
192 root = parseJSON("nan");
193 EXPECT_FALSE(root.get());
194 root = parseJSON("inf");
195 EXPECT_FALSE(root.get());
196
197 // Invalid number formats
198 root = parseJSON("4.3.1");
199 EXPECT_FALSE(root.get());
200 root = parseJSON("4e3.1");
201 EXPECT_FALSE(root.get());
202
203 // Test string parser
204 root = parseJSON("\"hello world\"");
205 ASSERT_TRUE(root.get());
206 EXPECT_EQ(Value::TypeString, root->type());
207 EXPECT_TRUE(root->asString(&strVal));
208 EXPECT_EQ("hello world", strVal);
209
210 // Empty string
211 root = parseJSON("\"\"");
212 ASSERT_TRUE(root.get());
213 EXPECT_EQ(Value::TypeString, root->type());
214 EXPECT_TRUE(root->asString(&strVal));
215 EXPECT_EQ("", strVal);
216
217 // Test basic string escapes
218 root = parseJSON("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"");
219 ASSERT_TRUE(root.get());
220 EXPECT_EQ(Value::TypeString, root->type());
221 EXPECT_TRUE(root->asString(&strVal));
222 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", strVal);
223
224 // Test hex and unicode escapes including the null character.
225 root = parseJSON("\"\\x41\\x00\\u1234\"");
226 EXPECT_FALSE(root.get());
227
228 // Test invalid strings
229 root = parseJSON("\"no closing quote");
230 EXPECT_FALSE(root.get());
231 root = parseJSON("\"\\z invalid escape char\"");
232 EXPECT_FALSE(root.get());
233 root = parseJSON("\"not enough escape chars\\u123\"");
234 EXPECT_FALSE(root.get());
235 root = parseJSON("\"extra backslash at end of input\\\"");
236 EXPECT_FALSE(root.get());
237
238 // Basic array
239 root = parseJSON("[true, false, null]");
240 ASSERT_TRUE(root.get());
241 EXPECT_EQ(Value::TypeArray, root->type());
242 list = ListValue::cast(root.get());
243 ASSERT_TRUE(list);
244 EXPECT_EQ(3U, list->size());
245
246 // Empty array
247 root = parseJSON("[]");
248 ASSERT_TRUE(root.get());
249 EXPECT_EQ(Value::TypeArray, root->type());
250 list = ListValue::cast(root.get());
251 ASSERT_TRUE(list);
252 EXPECT_EQ(0U, list->size());
253
254 // Nested arrays
255 root = parseJSON("[[true], [], [false, [], [null]], null]");
256 ASSERT_TRUE(root.get());
257 EXPECT_EQ(Value::TypeArray, root->type());
258 list = ListValue::cast(root.get());
259 ASSERT_TRUE(list);
260 EXPECT_EQ(4U, list->size());
261
262 // Invalid, missing close brace.
263 root = parseJSON("[[true], [], [false, [], [null]], null");
264 EXPECT_FALSE(root.get());
265
266 // Invalid, too many commas
267 root = parseJSON("[true,, null]");
268 EXPECT_FALSE(root.get());
269
270 // Invalid, no commas
271 root = parseJSON("[true null]");
272 EXPECT_FALSE(root.get());
273
274 // Invalid, trailing comma
275 root = parseJSON("[true,]");
276 EXPECT_FALSE(root.get());
277
278 root = parseJSON("[true]");
279 ASSERT_TRUE(root.get());
280 EXPECT_EQ(Value::TypeArray, root->type());
281 list = ListValue::cast(root.get());
282 ASSERT_TRUE(list);
283 EXPECT_EQ(1U, list->size());
284 tmpValue = list->at(0);
285 ASSERT_TRUE(tmpValue);
286 EXPECT_EQ(Value::TypeBoolean, tmpValue->type());
287 bool boolValue = false;
288 EXPECT_TRUE(tmpValue->asBoolean(&boolValue));
289 EXPECT_TRUE(boolValue);
290
291 // Don't allow empty elements.
292 root = parseJSON("[,]");
293 EXPECT_FALSE(root.get());
294 root = parseJSON("[true,,]");
295 EXPECT_FALSE(root.get());
296 root = parseJSON("[,true,]");
297 EXPECT_FALSE(root.get());
298 root = parseJSON("[true,,false]");
299 EXPECT_FALSE(root.get());
300
301 // Test objects
302 root = parseJSON("{}");
303 ASSERT_TRUE(root.get());
304 EXPECT_EQ(Value::TypeObject, root->type());
305
306 root = parseJSON("{\"number\":9.87654321, \"null\":null , \"S\" : \"str\" }" );
307 ASSERT_TRUE(root.get());
308 EXPECT_EQ(Value::TypeObject, root->type());
309 protocol::DictionaryValue* objectVal = DictionaryValue::cast(root.get());
310 ASSERT_TRUE(objectVal);
311 doubleVal = 0.0;
312 EXPECT_TRUE(objectVal->getDouble("number", &doubleVal));
313 EXPECT_DOUBLE_EQ(9.87654321, doubleVal);
314 protocol::Value* nullVal = objectVal->get("null");
315 ASSERT_TRUE(nullVal);
316 EXPECT_EQ(Value::TypeNull, nullVal->type());
317 EXPECT_TRUE(objectVal->getString("S", &strVal));
318 EXPECT_EQ("str", strVal);
319
320 // Test newline equivalence.
321 root2 = parseJSON(
322 "{\n"
323 " \"number\":9.87654321,\n"
324 " \"null\":null,\n"
325 " \"S\":\"str\"\n"
326 "}\n");
327 ASSERT_TRUE(root2.get());
328 EXPECT_EQ(root->toJSONString(), root2->toJSONString());
329
330 root2 = parseJSON(
331 "{\r\n"
332 " \"number\":9.87654321,\r\n"
333 " \"null\":null,\r\n"
334 " \"S\":\"str\"\r\n"
335 "}\r\n");
336 ASSERT_TRUE(root2.get());
337 EXPECT_EQ(root->toJSONString(), root2->toJSONString());
338
339 // Test nesting
340 root = parseJSON("{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}");
341 ASSERT_TRUE(root.get());
342 EXPECT_EQ(Value::TypeObject, root->type());
343 objectVal = DictionaryValue::cast(root.get());
344 ASSERT_TRUE(objectVal);
345 protocol::DictionaryValue* innerObject = objectVal->getObject("inner");
346 ASSERT_TRUE(innerObject);
347 protocol::ListValue* innerArray = innerObject->getArray("array");
348 ASSERT_TRUE(innerArray);
349 EXPECT_EQ(1U, innerArray->size());
350 boolValue = true;
351 EXPECT_TRUE(objectVal->getBoolean("false", &boolValue));
352 EXPECT_FALSE(boolValue);
353 innerObject = objectVal->getObject("d");
354 EXPECT_TRUE(innerObject);
355
356 // Test keys with periods
357 root = parseJSON("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}");
358 ASSERT_TRUE(root.get());
359 EXPECT_EQ(Value::TypeObject, root->type());
360 objectVal = DictionaryValue::cast(root.get());
361 ASSERT_TRUE(objectVal);
362 int integerValue = 0;
363 EXPECT_TRUE(objectVal->getInteger("a.b", &integerValue));
364 EXPECT_EQ(3, integerValue);
365 EXPECT_TRUE(objectVal->getInteger("c", &integerValue));
366 EXPECT_EQ(2, integerValue);
367 innerObject = objectVal->getObject("d.e.f");
368 ASSERT_TRUE(innerObject);
369 EXPECT_EQ(1U, innerObject->size());
370 EXPECT_TRUE(innerObject->getInteger("g.h.i.j", &integerValue));
371 EXPECT_EQ(1, integerValue);
372
373 root = parseJSON("{\"a\":{\"b\":2},\"a.b\":1}");
374 ASSERT_TRUE(root.get());
375 EXPECT_EQ(Value::TypeObject, root->type());
376 objectVal = DictionaryValue::cast(root.get());
377 ASSERT_TRUE(objectVal);
378 innerObject = objectVal->getObject("a");
379 ASSERT_TRUE(innerObject);
380 EXPECT_TRUE(innerObject->getInteger("b", &integerValue));
381 EXPECT_EQ(2, integerValue);
382 EXPECT_TRUE(objectVal->getInteger("a.b", &integerValue));
383 EXPECT_EQ(1, integerValue);
384
385 // Invalid, no closing brace
386 root = parseJSON("{\"a\": true");
387 EXPECT_FALSE(root.get());
388
389 // Invalid, keys must be quoted
390 root = parseJSON("{foo:true}");
391 EXPECT_FALSE(root.get());
392
393 // Invalid, trailing comma
394 root = parseJSON("{\"a\":true,}");
395 EXPECT_FALSE(root.get());
396
397 // Invalid, too many commas
398 root = parseJSON("{\"a\":true,,\"b\":false}");
399 EXPECT_FALSE(root.get());
400
401 // Invalid, no separator
402 root = parseJSON("{\"a\" \"b\"}");
403 EXPECT_FALSE(root.get());
404
405 // Invalid, lone comma.
406 root = parseJSON("{,}");
407 EXPECT_FALSE(root.get());
408 root = parseJSON("{\"a\":true,,}");
409 EXPECT_FALSE(root.get());
410 root = parseJSON("{,\"a\":true}");
411 EXPECT_FALSE(root.get());
412 root = parseJSON("{\"a\":true,,\"b\":false}");
413 EXPECT_FALSE(root.get());
414
415 // Test stack overflow
416 String16Builder evil;
417 evil.reserveCapacity(2000000);
418 for (int i = 0; i < 1000000; ++i)
419 evil.append('[');
420 for (int i = 0; i < 1000000; ++i)
421 evil.append(']');
422 root = parseJSON(evil.toString());
423 EXPECT_FALSE(root.get());
424
425 // A few thousand adjacent lists is fine.
426 String16Builder notEvil;
427 notEvil.reserveCapacity(15010);
428 notEvil.append('[');
429 for (int i = 0; i < 5000; ++i)
430 notEvil.append("[],");
431 notEvil.append("[]]");
432 root = parseJSON(notEvil.toString());
433 ASSERT_TRUE(root.get());
434 EXPECT_EQ(Value::TypeArray, root->type());
435 list = ListValue::cast(root.get());
436 ASSERT_TRUE(list);
437 EXPECT_EQ(5001U, list->size());
438
439 // Test utf8 encoded input
440 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\"");
441 ASSERT_FALSE(root.get());
442
443 // Test utf16 encoded strings.
444 root = parseJSON("\"\\u20ac3,14\"");
445 ASSERT_TRUE(root.get());
446 EXPECT_EQ(Value::TypeString, root->type());
447 EXPECT_TRUE(root->asString(&strVal));
448 UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34};
449 EXPECT_EQ(String16(tmp2, 5), strVal);
450
451 root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\"");
452 ASSERT_TRUE(root.get());
453 EXPECT_EQ(Value::TypeString, root->type());
454 EXPECT_TRUE(root->asString(&strVal));
455 UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c};
456 EXPECT_EQ(String16(tmp3, 4), strVal);
457
458 // Test literal root objects.
459 root = parseJSON("null");
460 EXPECT_EQ(Value::TypeNull, root->type());
461
462 root = parseJSON("true");
463 ASSERT_TRUE(root.get());
464 EXPECT_TRUE(root->asBoolean(&boolValue));
465 EXPECT_TRUE(boolValue);
466
467 root = parseJSON("10");
468 ASSERT_TRUE(root.get());
469 EXPECT_TRUE(root->asInteger(&integerValue));
470 EXPECT_EQ(10, integerValue);
471
472 root = parseJSON("\"root\"");
473 ASSERT_TRUE(root.get());
474 EXPECT_TRUE(root->asString(&strVal));
475 EXPECT_EQ("root", strVal);
476 }
477
478 TEST(ParserTest, InvalidSanity)
479 {
480 const char* const invalidJson[] = {
481 "/* test *",
482 "{\"foo\"",
483 "{\"foo\":",
484 " [",
485 "\"\\u123g\"",
486 "{\n\"eh:\n}",
487 "////",
488 "*/**/",
489 "/**/",
490 "/*/",
491 "//**/"
492 };
493
494 for (size_t i = 0; i < 11; ++i) {
495 std::unique_ptr<protocol::Value> result = parseJSON(invalidJson[i]);
496 EXPECT_FALSE(result.get());
497 }
498 }
499
500 } // namespace protocol
501 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698