OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/inspector_protocol/Parser.h" | 5 #include "platform/inspector_protocol/Parser.h" |
6 | 6 |
| 7 #include "platform/inspector_protocol/String16.h" |
7 #include "platform/inspector_protocol/Values.h" | 8 #include "platform/inspector_protocol/Values.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "wtf/text/StringBuilder.h" | |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 namespace protocol { | 12 namespace protocol { |
13 | 13 |
14 TEST(ParserTest, Reading) | 14 TEST(ParserTest, Reading) |
15 { | 15 { |
16 protocol::Value* tmpValue; | 16 protocol::Value* tmpValue; |
17 OwnPtr<protocol::Value> root; | 17 OwnPtr<protocol::Value> root; |
18 OwnPtr<protocol::Value> root2; | 18 OwnPtr<protocol::Value> root2; |
19 String strVal; | 19 String16 strVal; |
20 int intVal = 0; | 20 int intVal = 0; |
21 | 21 |
22 // some whitespace checking | 22 // some whitespace checking |
23 root = parseJSON(" null "); | 23 root = parseJSON(" null "); |
24 ASSERT_TRUE(root.get()); | 24 ASSERT_TRUE(root.get()); |
25 EXPECT_EQ(Value::TypeNull, root->type()); | 25 EXPECT_EQ(Value::TypeNull, root->type()); |
26 | 26 |
27 // Invalid JSON string | 27 // Invalid JSON string |
28 root = parseJSON("nu"); | 28 root = parseJSON("nu"); |
29 EXPECT_FALSE(root.get()); | 29 EXPECT_FALSE(root.get()); |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 EXPECT_EQ(Value::TypeString, root->type()); | 227 EXPECT_EQ(Value::TypeString, root->type()); |
228 EXPECT_TRUE(root->asString(&strVal)); | 228 EXPECT_TRUE(root->asString(&strVal)); |
229 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", strVal); | 229 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", strVal); |
230 | 230 |
231 // Test hex and unicode escapes including the null character. | 231 // Test hex and unicode escapes including the null character. |
232 root = parseJSON("\"\\x41\\x00\\u1234\""); | 232 root = parseJSON("\"\\x41\\x00\\u1234\""); |
233 ASSERT_TRUE(root.get()); | 233 ASSERT_TRUE(root.get()); |
234 EXPECT_EQ(Value::TypeString, root->type()); | 234 EXPECT_EQ(Value::TypeString, root->type()); |
235 EXPECT_TRUE(root->asString(&strVal)); | 235 EXPECT_TRUE(root->asString(&strVal)); |
236 UChar tmp1[] = {0x41, 0, 0x1234}; | 236 UChar tmp1[] = {0x41, 0, 0x1234}; |
237 EXPECT_EQ(String(tmp1, WTF_ARRAY_LENGTH(tmp1)), strVal); | 237 EXPECT_EQ(String16(tmp1, WTF_ARRAY_LENGTH(tmp1)), strVal); |
238 | 238 |
239 // Test invalid strings | 239 // Test invalid strings |
240 root = parseJSON("\"no closing quote"); | 240 root = parseJSON("\"no closing quote"); |
241 EXPECT_FALSE(root.get()); | 241 EXPECT_FALSE(root.get()); |
242 root = parseJSON("\"\\z invalid escape char\""); | 242 root = parseJSON("\"\\z invalid escape char\""); |
243 EXPECT_FALSE(root.get()); | 243 EXPECT_FALSE(root.get()); |
244 root = parseJSON("\"\\xAQ invalid hex code\""); | 244 root = parseJSON("\"\\xAQ invalid hex code\""); |
245 EXPECT_FALSE(root.get()); | 245 EXPECT_FALSE(root.get()); |
246 root = parseJSON("not enough hex chars\\x1\""); | 246 root = parseJSON("not enough hex chars\\x1\""); |
247 EXPECT_FALSE(root.get()); | 247 EXPECT_FALSE(root.get()); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 root = parseJSON("{,}"); | 421 root = parseJSON("{,}"); |
422 EXPECT_FALSE(root.get()); | 422 EXPECT_FALSE(root.get()); |
423 root = parseJSON("{\"a\":true,,}"); | 423 root = parseJSON("{\"a\":true,,}"); |
424 EXPECT_FALSE(root.get()); | 424 EXPECT_FALSE(root.get()); |
425 root = parseJSON("{,\"a\":true}"); | 425 root = parseJSON("{,\"a\":true}"); |
426 EXPECT_FALSE(root.get()); | 426 EXPECT_FALSE(root.get()); |
427 root = parseJSON("{\"a\":true,,\"b\":false}"); | 427 root = parseJSON("{\"a\":true,,\"b\":false}"); |
428 EXPECT_FALSE(root.get()); | 428 EXPECT_FALSE(root.get()); |
429 | 429 |
430 // Test stack overflow | 430 // Test stack overflow |
431 StringBuilder evil; | 431 String16Builder evil; |
432 evil.reserveCapacity(2000000); | 432 evil.reserveCapacity(2000000); |
433 for (int i = 0; i < 1000000; ++i) | 433 for (int i = 0; i < 1000000; ++i) |
434 evil.append('['); | 434 evil.append('['); |
435 for (int i = 0; i < 1000000; ++i) | 435 for (int i = 0; i < 1000000; ++i) |
436 evil.append(']'); | 436 evil.append(']'); |
437 root = parseJSON(evil.toString()); | 437 root = parseJSON(evil.toString()); |
438 EXPECT_FALSE(root.get()); | 438 EXPECT_FALSE(root.get()); |
439 | 439 |
440 // A few thousand adjacent lists is fine. | 440 // A few thousand adjacent lists is fine. |
441 StringBuilder notEvil; | 441 String16Builder notEvil; |
442 notEvil.reserveCapacity(15010); | 442 notEvil.reserveCapacity(15010); |
443 notEvil.append('['); | 443 notEvil.append('['); |
444 for (int i = 0; i < 5000; ++i) | 444 for (int i = 0; i < 5000; ++i) |
445 notEvil.append("[],"); | 445 notEvil.append("[],"); |
446 notEvil.append("[]]"); | 446 notEvil.append("[]]"); |
447 root = parseJSON(notEvil.toString()); | 447 root = parseJSON(notEvil.toString()); |
448 ASSERT_TRUE(root.get()); | 448 ASSERT_TRUE(root.get()); |
449 EXPECT_EQ(Value::TypeArray, root->type()); | 449 EXPECT_EQ(Value::TypeArray, root->type()); |
450 list = ListValue::cast(root.get()); | 450 list = ListValue::cast(root.get()); |
451 ASSERT_TRUE(list); | 451 ASSERT_TRUE(list); |
452 EXPECT_EQ(5001U, list->size()); | 452 EXPECT_EQ(5001U, list->size()); |
453 | 453 |
454 // Test utf8 encoded input | 454 // Test utf8 encoded input |
455 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\""); | 455 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\""); |
456 ASSERT_TRUE(root.get()); | 456 ASSERT_TRUE(root.get()); |
457 EXPECT_EQ(Value::TypeString, root->type()); | 457 EXPECT_EQ(Value::TypeString, root->type()); |
458 EXPECT_TRUE(root->asString(&strVal)); | 458 EXPECT_TRUE(root->asString(&strVal)); |
459 UChar tmp4[] = {0x7f51, 0x9875}; | 459 UChar tmp4[] = {0x7f51, 0x9875}; |
460 EXPECT_EQ(String(tmp4, WTF_ARRAY_LENGTH(tmp4)), strVal); | 460 EXPECT_EQ(String16(tmp4, WTF_ARRAY_LENGTH(tmp4)), strVal); |
461 | 461 |
462 root = parseJSON("{\"path\": \"/tmp/\\xc3\\xa0\\xc3\\xa8\\xc3\\xb2.png\"}"); | 462 root = parseJSON("{\"path\": \"/tmp/\\xc3\\xa0\\xc3\\xa8\\xc3\\xb2.png\"}"); |
463 ASSERT_TRUE(root.get()); | 463 ASSERT_TRUE(root.get()); |
464 EXPECT_EQ(Value::TypeObject, root->type()); | 464 EXPECT_EQ(Value::TypeObject, root->type()); |
465 objectVal = DictionaryValue::cast(root.get()); | 465 objectVal = DictionaryValue::cast(root.get()); |
466 ASSERT_TRUE(objectVal); | 466 ASSERT_TRUE(objectVal); |
467 EXPECT_TRUE(objectVal->getString("path", &strVal)); | 467 EXPECT_TRUE(objectVal->getString("path", &strVal)); |
468 UChar tmp5[] = {0x2f, 0x74, 0x6d, 0x70, 0x2f, 0xe0, 0xe8, 0xf2, 0x2e, 0x70,
0x6e, 0x67}; | 468 UChar tmp5[] = {0x2f, 0x74, 0x6d, 0x70, 0x2f, 0xe0, 0xe8, 0xf2, 0x2e, 0x70,
0x6e, 0x67}; |
469 EXPECT_EQ(String(tmp5, WTF_ARRAY_LENGTH(tmp5)), strVal); | 469 EXPECT_EQ(String16(tmp5, WTF_ARRAY_LENGTH(tmp5)), strVal); |
470 | 470 |
471 // Test invalid utf8 encoded input | 471 // Test invalid utf8 encoded input |
472 root = parseJSON("\"345\\xb0\\xa1\\xb0\\xa2\""); | 472 root = parseJSON("\"345\\xb0\\xa1\\xb0\\xa2\""); |
473 ASSERT_FALSE(root.get()); | 473 ASSERT_FALSE(root.get()); |
474 root = parseJSON("\"123\\xc0\\x81\""); | 474 root = parseJSON("\"123\\xc0\\x81\""); |
475 ASSERT_FALSE(root.get()); | 475 ASSERT_FALSE(root.get()); |
476 root = parseJSON("\"abc\\xc0\\xae\""); | 476 root = parseJSON("\"abc\\xc0\\xae\""); |
477 ASSERT_FALSE(root.get()); | 477 ASSERT_FALSE(root.get()); |
478 | 478 |
479 // Test utf16 encoded strings. | 479 // Test utf16 encoded strings. |
480 root = parseJSON("\"\\u20ac3,14\""); | 480 root = parseJSON("\"\\u20ac3,14\""); |
481 ASSERT_TRUE(root.get()); | 481 ASSERT_TRUE(root.get()); |
482 EXPECT_EQ(Value::TypeString, root->type()); | 482 EXPECT_EQ(Value::TypeString, root->type()); |
483 EXPECT_TRUE(root->asString(&strVal)); | 483 EXPECT_TRUE(root->asString(&strVal)); |
484 UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34}; | 484 UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34}; |
485 EXPECT_EQ(String(tmp2, WTF_ARRAY_LENGTH(tmp2)), strVal); | 485 EXPECT_EQ(String16(tmp2, WTF_ARRAY_LENGTH(tmp2)), strVal); |
486 | 486 |
487 root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\""); | 487 root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\""); |
488 ASSERT_TRUE(root.get()); | 488 ASSERT_TRUE(root.get()); |
489 EXPECT_EQ(Value::TypeString, root->type()); | 489 EXPECT_EQ(Value::TypeString, root->type()); |
490 EXPECT_TRUE(root->asString(&strVal)); | 490 EXPECT_TRUE(root->asString(&strVal)); |
491 UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c}; | 491 UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c}; |
492 EXPECT_EQ(String(tmp3, WTF_ARRAY_LENGTH(tmp3)), strVal); | 492 EXPECT_EQ(String16(tmp3, WTF_ARRAY_LENGTH(tmp3)), strVal); |
493 | 493 |
494 // Test invalid utf16 strings. | 494 // Test invalid utf16 strings. |
495 const char* const cases[] = { | 495 const char* const cases[] = { |
496 "\"\\u123\"", // Invalid scalar. | 496 "\"\\u123\"", // Invalid scalar. |
497 "\"\\ud83d\"", // Invalid scalar. | 497 "\"\\ud83d\"", // Invalid scalar. |
498 "\"\\u$%@!\"", // Invalid scalar. | 498 "\"\\u$%@!\"", // Invalid scalar. |
499 "\"\\uzz89\"", // Invalid scalar. | 499 "\"\\uzz89\"", // Invalid scalar. |
500 "\"\\ud83d\\udca\"", // Invalid lower surrogate. | 500 "\"\\ud83d\\udca\"", // Invalid lower surrogate. |
501 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. | 501 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. |
502 "\"\\ud83foo\"", // No lower surrogate. | 502 "\"\\ud83foo\"", // No lower surrogate. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 }; | 544 }; |
545 | 545 |
546 for (size_t i = 0; i < WTF_ARRAY_LENGTH(invalidJson); ++i) { | 546 for (size_t i = 0; i < WTF_ARRAY_LENGTH(invalidJson); ++i) { |
547 OwnPtr<protocol::Value> result = parseJSON(invalidJson[i]); | 547 OwnPtr<protocol::Value> result = parseJSON(invalidJson[i]); |
548 EXPECT_FALSE(result.get()); | 548 EXPECT_FALSE(result.get()); |
549 } | 549 } |
550 } | 550 } |
551 | 551 |
552 } // namespace protocol | 552 } // namespace protocol |
553 } // namespace blink | 553 } // namespace blink |
OLD | NEW |