| 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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 root = parseJSON("1e"); | 183 root = parseJSON("1e"); |
| 184 EXPECT_FALSE(root.get()); | 184 EXPECT_FALSE(root.get()); |
| 185 root = parseJSON("1E"); | 185 root = parseJSON("1E"); |
| 186 EXPECT_FALSE(root.get()); | 186 EXPECT_FALSE(root.get()); |
| 187 root = parseJSON("1e1."); | 187 root = parseJSON("1e1."); |
| 188 EXPECT_FALSE(root.get()); | 188 EXPECT_FALSE(root.get()); |
| 189 root = parseJSON("1e1.0"); | 189 root = parseJSON("1e1.0"); |
| 190 EXPECT_FALSE(root.get()); | 190 EXPECT_FALSE(root.get()); |
| 191 | 191 |
| 192 // INF/-INF/NaN are not valid | 192 // INF/-INF/NaN are not valid |
| 193 root = parseJSON("1e1000"); | |
| 194 EXPECT_FALSE(root.get()); | |
| 195 root = parseJSON("-1e1000"); | |
| 196 EXPECT_FALSE(root.get()); | |
| 197 root = parseJSON("NaN"); | 193 root = parseJSON("NaN"); |
| 198 EXPECT_FALSE(root.get()); | 194 EXPECT_FALSE(root.get()); |
| 199 root = parseJSON("nan"); | 195 root = parseJSON("nan"); |
| 200 EXPECT_FALSE(root.get()); | 196 EXPECT_FALSE(root.get()); |
| 201 root = parseJSON("inf"); | 197 root = parseJSON("inf"); |
| 202 EXPECT_FALSE(root.get()); | 198 EXPECT_FALSE(root.get()); |
| 203 | 199 |
| 204 // Invalid number formats | 200 // Invalid number formats |
| 205 root = parseJSON("4.3.1"); | 201 root = parseJSON("4.3.1"); |
| 206 EXPECT_FALSE(root.get()); | 202 EXPECT_FALSE(root.get()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 227 EXPECT_EQ(Value::TypeString, root->type()); | 223 EXPECT_EQ(Value::TypeString, root->type()); |
| 228 EXPECT_TRUE(root->asString(&strVal)); | 224 EXPECT_TRUE(root->asString(&strVal)); |
| 229 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", strVal); | 225 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", strVal); |
| 230 | 226 |
| 231 // Test hex and unicode escapes including the null character. | 227 // Test hex and unicode escapes including the null character. |
| 232 root = parseJSON("\"\\x41\\x00\\u1234\""); | 228 root = parseJSON("\"\\x41\\x00\\u1234\""); |
| 233 ASSERT_TRUE(root.get()); | 229 ASSERT_TRUE(root.get()); |
| 234 EXPECT_EQ(Value::TypeString, root->type()); | 230 EXPECT_EQ(Value::TypeString, root->type()); |
| 235 EXPECT_TRUE(root->asString(&strVal)); | 231 EXPECT_TRUE(root->asString(&strVal)); |
| 236 UChar tmp1[] = {0x41, 0, 0x1234}; | 232 UChar tmp1[] = {0x41, 0, 0x1234}; |
| 237 EXPECT_EQ(String(tmp1, WTF_ARRAY_LENGTH(tmp1)), strVal); | 233 EXPECT_EQ(String16(tmp1, 3), strVal); |
| 238 | 234 |
| 239 // Test invalid strings | 235 // Test invalid strings |
| 240 root = parseJSON("\"no closing quote"); | 236 root = parseJSON("\"no closing quote"); |
| 241 EXPECT_FALSE(root.get()); | 237 EXPECT_FALSE(root.get()); |
| 242 root = parseJSON("\"\\z invalid escape char\""); | 238 root = parseJSON("\"\\z invalid escape char\""); |
| 243 EXPECT_FALSE(root.get()); | 239 EXPECT_FALSE(root.get()); |
| 244 root = parseJSON("\"\\xAQ invalid hex code\""); | 240 root = parseJSON("\"\\xAQ invalid hex code\""); |
| 245 EXPECT_FALSE(root.get()); | 241 EXPECT_FALSE(root.get()); |
| 246 root = parseJSON("not enough hex chars\\x1\""); | 242 root = parseJSON("not enough hex chars\\x1\""); |
| 247 EXPECT_FALSE(root.get()); | 243 EXPECT_FALSE(root.get()); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 root = parseJSON("{,}"); | 417 root = parseJSON("{,}"); |
| 422 EXPECT_FALSE(root.get()); | 418 EXPECT_FALSE(root.get()); |
| 423 root = parseJSON("{\"a\":true,,}"); | 419 root = parseJSON("{\"a\":true,,}"); |
| 424 EXPECT_FALSE(root.get()); | 420 EXPECT_FALSE(root.get()); |
| 425 root = parseJSON("{,\"a\":true}"); | 421 root = parseJSON("{,\"a\":true}"); |
| 426 EXPECT_FALSE(root.get()); | 422 EXPECT_FALSE(root.get()); |
| 427 root = parseJSON("{\"a\":true,,\"b\":false}"); | 423 root = parseJSON("{\"a\":true,,\"b\":false}"); |
| 428 EXPECT_FALSE(root.get()); | 424 EXPECT_FALSE(root.get()); |
| 429 | 425 |
| 430 // Test stack overflow | 426 // Test stack overflow |
| 431 StringBuilder evil; | 427 String16Builder evil; |
| 432 evil.reserveCapacity(2000000); | 428 evil.reserveCapacity(2000000); |
| 433 for (int i = 0; i < 1000000; ++i) | 429 for (int i = 0; i < 1000000; ++i) |
| 434 evil.append('['); | 430 evil.append('['); |
| 435 for (int i = 0; i < 1000000; ++i) | 431 for (int i = 0; i < 1000000; ++i) |
| 436 evil.append(']'); | 432 evil.append(']'); |
| 437 root = parseJSON(evil.toString()); | 433 root = parseJSON(evil.toString()); |
| 438 EXPECT_FALSE(root.get()); | 434 EXPECT_FALSE(root.get()); |
| 439 | 435 |
| 440 // A few thousand adjacent lists is fine. | 436 // A few thousand adjacent lists is fine. |
| 441 StringBuilder notEvil; | 437 String16Builder notEvil; |
| 442 notEvil.reserveCapacity(15010); | 438 notEvil.reserveCapacity(15010); |
| 443 notEvil.append('['); | 439 notEvil.append('['); |
| 444 for (int i = 0; i < 5000; ++i) | 440 for (int i = 0; i < 5000; ++i) |
| 445 notEvil.append("[],"); | 441 notEvil.append("[],"); |
| 446 notEvil.append("[]]"); | 442 notEvil.append("[]]"); |
| 447 root = parseJSON(notEvil.toString()); | 443 root = parseJSON(notEvil.toString()); |
| 448 ASSERT_TRUE(root.get()); | 444 ASSERT_TRUE(root.get()); |
| 449 EXPECT_EQ(Value::TypeArray, root->type()); | 445 EXPECT_EQ(Value::TypeArray, root->type()); |
| 450 list = ListValue::cast(root.get()); | 446 list = ListValue::cast(root.get()); |
| 451 ASSERT_TRUE(list); | 447 ASSERT_TRUE(list); |
| 452 EXPECT_EQ(5001U, list->size()); | 448 EXPECT_EQ(5001U, list->size()); |
| 453 | 449 |
| 454 // Test utf8 encoded input | 450 // Test utf8 encoded input |
| 455 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\""); | 451 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\""); |
| 456 ASSERT_TRUE(root.get()); | 452 ASSERT_TRUE(root.get()); |
| 457 EXPECT_EQ(Value::TypeString, root->type()); | 453 EXPECT_EQ(Value::TypeString, root->type()); |
| 458 EXPECT_TRUE(root->asString(&strVal)); | 454 EXPECT_TRUE(root->asString(&strVal)); |
| 459 UChar tmp4[] = {0x7f51, 0x9875}; | 455 UChar tmp4[] = {0x7f51, 0x9875}; |
| 460 EXPECT_EQ(String(tmp4, WTF_ARRAY_LENGTH(tmp4)), strVal); | 456 EXPECT_EQ(String16(tmp4, 2), strVal); |
| 461 | 457 |
| 462 root = parseJSON("{\"path\": \"/tmp/\\xc3\\xa0\\xc3\\xa8\\xc3\\xb2.png\"}"); | 458 root = parseJSON("{\"path\": \"/tmp/\\xc3\\xa0\\xc3\\xa8\\xc3\\xb2.png\"}"); |
| 463 ASSERT_TRUE(root.get()); | 459 ASSERT_TRUE(root.get()); |
| 464 EXPECT_EQ(Value::TypeObject, root->type()); | 460 EXPECT_EQ(Value::TypeObject, root->type()); |
| 465 objectVal = DictionaryValue::cast(root.get()); | 461 objectVal = DictionaryValue::cast(root.get()); |
| 466 ASSERT_TRUE(objectVal); | 462 ASSERT_TRUE(objectVal); |
| 467 EXPECT_TRUE(objectVal->getString("path", &strVal)); | 463 EXPECT_TRUE(objectVal->getString("path", &strVal)); |
| 468 UChar tmp5[] = {0x2f, 0x74, 0x6d, 0x70, 0x2f, 0xe0, 0xe8, 0xf2, 0x2e, 0x70,
0x6e, 0x67}; | 464 UChar tmp5[] = {0x2f, 0x74, 0x6d, 0x70, 0x2f, 0xe0, 0xe8, 0xf2, 0x2e, 0x70,
0x6e, 0x67}; |
| 469 EXPECT_EQ(String(tmp5, WTF_ARRAY_LENGTH(tmp5)), strVal); | 465 EXPECT_EQ(String16(tmp5, 12), strVal); |
| 470 | 466 |
| 471 // Test invalid utf8 encoded input | 467 // Test invalid utf8 encoded input |
| 472 root = parseJSON("\"345\\xb0\\xa1\\xb0\\xa2\""); | 468 root = parseJSON("\"345\\xb0\\xa1\\xb0\\xa2\""); |
| 473 ASSERT_FALSE(root.get()); | 469 ASSERT_FALSE(root.get()); |
| 474 root = parseJSON("\"123\\xc0\\x81\""); | 470 root = parseJSON("\"123\\xc0\\x81\""); |
| 475 ASSERT_FALSE(root.get()); | 471 ASSERT_FALSE(root.get()); |
| 476 root = parseJSON("\"abc\\xc0\\xae\""); | 472 root = parseJSON("\"abc\\xc0\\xae\""); |
| 477 ASSERT_FALSE(root.get()); | 473 ASSERT_FALSE(root.get()); |
| 478 | 474 |
| 479 // Test utf16 encoded strings. | 475 // Test utf16 encoded strings. |
| 480 root = parseJSON("\"\\u20ac3,14\""); | 476 root = parseJSON("\"\\u20ac3,14\""); |
| 481 ASSERT_TRUE(root.get()); | 477 ASSERT_TRUE(root.get()); |
| 482 EXPECT_EQ(Value::TypeString, root->type()); | 478 EXPECT_EQ(Value::TypeString, root->type()); |
| 483 EXPECT_TRUE(root->asString(&strVal)); | 479 EXPECT_TRUE(root->asString(&strVal)); |
| 484 UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34}; | 480 UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34}; |
| 485 EXPECT_EQ(String(tmp2, WTF_ARRAY_LENGTH(tmp2)), strVal); | 481 EXPECT_EQ(String16(tmp2, 5), strVal); |
| 486 | 482 |
| 487 root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\""); | 483 root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\""); |
| 488 ASSERT_TRUE(root.get()); | 484 ASSERT_TRUE(root.get()); |
| 489 EXPECT_EQ(Value::TypeString, root->type()); | 485 EXPECT_EQ(Value::TypeString, root->type()); |
| 490 EXPECT_TRUE(root->asString(&strVal)); | 486 EXPECT_TRUE(root->asString(&strVal)); |
| 491 UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c}; | 487 UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c}; |
| 492 EXPECT_EQ(String(tmp3, WTF_ARRAY_LENGTH(tmp3)), strVal); | 488 EXPECT_EQ(String16(tmp3, 4), strVal); |
| 493 | 489 |
| 494 // Test invalid utf16 strings. | 490 // Test invalid utf16 strings. |
| 495 const char* const cases[] = { | 491 const char* const cases[] = { |
| 496 "\"\\u123\"", // Invalid scalar. | 492 "\"\\u123\"", // Invalid scalar. |
| 497 "\"\\ud83d\"", // Invalid scalar. | 493 "\"\\ud83d\"", // Invalid scalar. |
| 498 "\"\\u$%@!\"", // Invalid scalar. | 494 "\"\\u$%@!\"", // Invalid scalar. |
| 499 "\"\\uzz89\"", // Invalid scalar. | 495 "\"\\uzz89\"", // Invalid scalar. |
| 500 "\"\\ud83d\\udca\"", // Invalid lower surrogate. | 496 "\"\\ud83d\\udca\"", // Invalid lower surrogate. |
| 501 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. | 497 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. |
| 502 "\"\\ud83foo\"", // No lower surrogate. | 498 "\"\\ud83foo\"", // No lower surrogate. |
| 503 "\"\\ud83\\foo\"" // No lower surrogate. | 499 "\"\\ud83\\foo\"" // No lower surrogate. |
| 504 }; | 500 }; |
| 505 for (size_t i = 0; i < WTF_ARRAY_LENGTH(cases); ++i) { | 501 for (size_t i = 0; i < 8; ++i) { |
| 506 root = parseJSON(cases[i]); | 502 root = parseJSON(cases[i]); |
| 507 EXPECT_FALSE(root.get()) << cases[i]; | 503 EXPECT_FALSE(root.get()) << cases[i]; |
| 508 } | 504 } |
| 509 | 505 |
| 510 // Test literal root objects. | 506 // Test literal root objects. |
| 511 root = parseJSON("null"); | 507 root = parseJSON("null"); |
| 512 EXPECT_EQ(Value::TypeNull, root->type()); | 508 EXPECT_EQ(Value::TypeNull, root->type()); |
| 513 | 509 |
| 514 root = parseJSON("true"); | 510 root = parseJSON("true"); |
| 515 ASSERT_TRUE(root.get()); | 511 ASSERT_TRUE(root.get()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 536 " [", | 532 " [", |
| 537 "\"\\u123g\"", | 533 "\"\\u123g\"", |
| 538 "{\n\"eh:\n}", | 534 "{\n\"eh:\n}", |
| 539 "////", | 535 "////", |
| 540 "*/**/", | 536 "*/**/", |
| 541 "/**/", | 537 "/**/", |
| 542 "/*/", | 538 "/*/", |
| 543 "//**/" | 539 "//**/" |
| 544 }; | 540 }; |
| 545 | 541 |
| 546 for (size_t i = 0; i < WTF_ARRAY_LENGTH(invalidJson); ++i) { | 542 for (size_t i = 0; i < 11; ++i) { |
| 547 OwnPtr<protocol::Value> result = parseJSON(invalidJson[i]); | 543 OwnPtr<protocol::Value> result = parseJSON(invalidJson[i]); |
| 548 EXPECT_FALSE(result.get()); | 544 EXPECT_FALSE(result.get()); |
| 549 } | 545 } |
| 550 } | 546 } |
| 551 | 547 |
| 552 } // namespace protocol | 548 } // namespace protocol |
| 553 } // namespace blink | 549 } // namespace blink |
| OLD | NEW |