| 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/InspectorProtocol.h" | 5 #include "core/inspector/protocol/Protocol.h" |
| 6 |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 7 | 8 |
| 8 namespace blink { | 9 namespace blink { |
| 9 namespace protocol { | |
| 10 | 10 |
| 11 TEST(ParserTest, Reading) | 11 using protocol::parseJSON; |
| 12 using protocol::DictionaryValue; |
| 13 using protocol::ListValue; |
| 14 using protocol::Value; |
| 15 |
| 16 TEST(ProtocolParserTest, Reading) |
| 12 { | 17 { |
| 13 protocol::Value* tmpValue; | 18 Value* tmpValue; |
| 14 std::unique_ptr<protocol::Value> root; | 19 std::unique_ptr<Value> root; |
| 15 std::unique_ptr<protocol::Value> root2; | 20 std::unique_ptr<Value> root2; |
| 16 String16 strVal; | 21 String strVal; |
| 17 int intVal = 0; | 22 int intVal = 0; |
| 18 | 23 |
| 19 // some whitespace checking | 24 // some whitespace checking |
| 20 root = parseJSON(" null "); | 25 root = parseJSON(" null "); |
| 21 ASSERT_TRUE(root.get()); | 26 ASSERT_TRUE(root.get()); |
| 22 EXPECT_EQ(Value::TypeNull, root->type()); | 27 EXPECT_EQ(Value::TypeNull, root->type()); |
| 23 | 28 |
| 24 // Invalid JSON string | 29 // Invalid JSON string |
| 25 root = parseJSON("nu"); | 30 root = parseJSON("nu"); |
| 26 EXPECT_FALSE(root.get()); | 31 EXPECT_FALSE(root.get()); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 48 EXPECT_EQ(40, intVal); | 53 EXPECT_EQ(40, intVal); |
| 49 root = parseJSON("true // comment"); | 54 root = parseJSON("true // comment"); |
| 50 ASSERT_TRUE(root.get()); | 55 ASSERT_TRUE(root.get()); |
| 51 EXPECT_EQ(Value::TypeBoolean, root->type()); | 56 EXPECT_EQ(Value::TypeBoolean, root->type()); |
| 52 root = parseJSON("/* comment */\"sample string\""); | 57 root = parseJSON("/* comment */\"sample string\""); |
| 53 ASSERT_TRUE(root.get()); | 58 ASSERT_TRUE(root.get()); |
| 54 EXPECT_TRUE(root->asString(&strVal)); | 59 EXPECT_TRUE(root->asString(&strVal)); |
| 55 EXPECT_EQ("sample string", strVal); | 60 EXPECT_EQ("sample string", strVal); |
| 56 root = parseJSON("[1, /* comment, 2 ] */ \n 3]"); | 61 root = parseJSON("[1, /* comment, 2 ] */ \n 3]"); |
| 57 ASSERT_TRUE(root.get()); | 62 ASSERT_TRUE(root.get()); |
| 58 protocol::ListValue* list = ListValue::cast(root.get()); | 63 ListValue* list = ListValue::cast(root.get()); |
| 59 ASSERT_TRUE(list); | 64 ASSERT_TRUE(list); |
| 60 EXPECT_EQ(2u, list->size()); | 65 EXPECT_EQ(2u, list->size()); |
| 61 tmpValue = list->at(0); | 66 tmpValue = list->at(0); |
| 62 ASSERT_TRUE(tmpValue); | 67 ASSERT_TRUE(tmpValue); |
| 63 EXPECT_TRUE(tmpValue->asInteger(&intVal)); | 68 EXPECT_TRUE(tmpValue->asInteger(&intVal)); |
| 64 EXPECT_EQ(1, intVal); | 69 EXPECT_EQ(1, intVal); |
| 65 tmpValue = list->at(1); | 70 tmpValue = list->at(1); |
| 66 ASSERT_TRUE(tmpValue); | 71 ASSERT_TRUE(tmpValue); |
| 67 EXPECT_TRUE(tmpValue->asInteger(&intVal)); | 72 EXPECT_TRUE(tmpValue->asInteger(&intVal)); |
| 68 EXPECT_EQ(3, intVal); | 73 EXPECT_EQ(3, intVal); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 EXPECT_FALSE(root.get()); | 304 EXPECT_FALSE(root.get()); |
| 300 | 305 |
| 301 // Test objects | 306 // Test objects |
| 302 root = parseJSON("{}"); | 307 root = parseJSON("{}"); |
| 303 ASSERT_TRUE(root.get()); | 308 ASSERT_TRUE(root.get()); |
| 304 EXPECT_EQ(Value::TypeObject, root->type()); | 309 EXPECT_EQ(Value::TypeObject, root->type()); |
| 305 | 310 |
| 306 root = parseJSON("{\"number\":9.87654321, \"null\":null , \"S\" : \"str\" }"
); | 311 root = parseJSON("{\"number\":9.87654321, \"null\":null , \"S\" : \"str\" }"
); |
| 307 ASSERT_TRUE(root.get()); | 312 ASSERT_TRUE(root.get()); |
| 308 EXPECT_EQ(Value::TypeObject, root->type()); | 313 EXPECT_EQ(Value::TypeObject, root->type()); |
| 309 protocol::DictionaryValue* objectVal = DictionaryValue::cast(root.get()); | 314 DictionaryValue* objectVal = DictionaryValue::cast(root.get()); |
| 310 ASSERT_TRUE(objectVal); | 315 ASSERT_TRUE(objectVal); |
| 311 doubleVal = 0.0; | 316 doubleVal = 0.0; |
| 312 EXPECT_TRUE(objectVal->getDouble("number", &doubleVal)); | 317 EXPECT_TRUE(objectVal->getDouble("number", &doubleVal)); |
| 313 EXPECT_DOUBLE_EQ(9.87654321, doubleVal); | 318 EXPECT_DOUBLE_EQ(9.87654321, doubleVal); |
| 314 protocol::Value* nullVal = objectVal->get("null"); | 319 Value* nullVal = objectVal->get("null"); |
| 315 ASSERT_TRUE(nullVal); | 320 ASSERT_TRUE(nullVal); |
| 316 EXPECT_EQ(Value::TypeNull, nullVal->type()); | 321 EXPECT_EQ(Value::TypeNull, nullVal->type()); |
| 317 EXPECT_TRUE(objectVal->getString("S", &strVal)); | 322 EXPECT_TRUE(objectVal->getString("S", &strVal)); |
| 318 EXPECT_EQ("str", strVal); | 323 EXPECT_EQ("str", strVal); |
| 319 | 324 |
| 320 // Test newline equivalence. | 325 // Test newline equivalence. |
| 321 root2 = parseJSON( | 326 root2 = parseJSON( |
| 322 "{\n" | 327 "{\n" |
| 323 " \"number\":9.87654321,\n" | 328 " \"number\":9.87654321,\n" |
| 324 " \"null\":null,\n" | 329 " \"null\":null,\n" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 335 "}\r\n"); | 340 "}\r\n"); |
| 336 ASSERT_TRUE(root2.get()); | 341 ASSERT_TRUE(root2.get()); |
| 337 EXPECT_EQ(root->toJSONString(), root2->toJSONString()); | 342 EXPECT_EQ(root->toJSONString(), root2->toJSONString()); |
| 338 | 343 |
| 339 // Test nesting | 344 // Test nesting |
| 340 root = parseJSON("{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}"); | 345 root = parseJSON("{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}"); |
| 341 ASSERT_TRUE(root.get()); | 346 ASSERT_TRUE(root.get()); |
| 342 EXPECT_EQ(Value::TypeObject, root->type()); | 347 EXPECT_EQ(Value::TypeObject, root->type()); |
| 343 objectVal = DictionaryValue::cast(root.get()); | 348 objectVal = DictionaryValue::cast(root.get()); |
| 344 ASSERT_TRUE(objectVal); | 349 ASSERT_TRUE(objectVal); |
| 345 protocol::DictionaryValue* innerObject = objectVal->getObject("inner"); | 350 DictionaryValue* innerObject = objectVal->getObject("inner"); |
| 346 ASSERT_TRUE(innerObject); | 351 ASSERT_TRUE(innerObject); |
| 347 protocol::ListValue* innerArray = innerObject->getArray("array"); | 352 ListValue* innerArray = innerObject->getArray("array"); |
| 348 ASSERT_TRUE(innerArray); | 353 ASSERT_TRUE(innerArray); |
| 349 EXPECT_EQ(1U, innerArray->size()); | 354 EXPECT_EQ(1U, innerArray->size()); |
| 350 boolValue = true; | 355 boolValue = true; |
| 351 EXPECT_TRUE(objectVal->getBoolean("false", &boolValue)); | 356 EXPECT_TRUE(objectVal->getBoolean("false", &boolValue)); |
| 352 EXPECT_FALSE(boolValue); | 357 EXPECT_FALSE(boolValue); |
| 353 innerObject = objectVal->getObject("d"); | 358 innerObject = objectVal->getObject("d"); |
| 354 EXPECT_TRUE(innerObject); | 359 EXPECT_TRUE(innerObject); |
| 355 | 360 |
| 356 // Test keys with periods | 361 // Test keys with periods |
| 357 root = parseJSON("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}"); | 362 root = parseJSON("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}"); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 root = parseJSON("{,}"); | 411 root = parseJSON("{,}"); |
| 407 EXPECT_FALSE(root.get()); | 412 EXPECT_FALSE(root.get()); |
| 408 root = parseJSON("{\"a\":true,,}"); | 413 root = parseJSON("{\"a\":true,,}"); |
| 409 EXPECT_FALSE(root.get()); | 414 EXPECT_FALSE(root.get()); |
| 410 root = parseJSON("{,\"a\":true}"); | 415 root = parseJSON("{,\"a\":true}"); |
| 411 EXPECT_FALSE(root.get()); | 416 EXPECT_FALSE(root.get()); |
| 412 root = parseJSON("{\"a\":true,,\"b\":false}"); | 417 root = parseJSON("{\"a\":true,,\"b\":false}"); |
| 413 EXPECT_FALSE(root.get()); | 418 EXPECT_FALSE(root.get()); |
| 414 | 419 |
| 415 // Test stack overflow | 420 // Test stack overflow |
| 416 String16Builder evil; | 421 StringBuilder evil; |
| 417 evil.reserveCapacity(2000000); | 422 evil.reserveCapacity(2000000); |
| 418 for (int i = 0; i < 1000000; ++i) | 423 for (int i = 0; i < 1000000; ++i) |
| 419 evil.append('['); | 424 evil.append('['); |
| 420 for (int i = 0; i < 1000000; ++i) | 425 for (int i = 0; i < 1000000; ++i) |
| 421 evil.append(']'); | 426 evil.append(']'); |
| 422 root = parseJSON(evil.toString()); | 427 root = parseJSON(evil.toString()); |
| 423 EXPECT_FALSE(root.get()); | 428 EXPECT_FALSE(root.get()); |
| 424 | 429 |
| 425 // A few thousand adjacent lists is fine. | 430 // A few thousand adjacent lists is fine. |
| 426 String16Builder notEvil; | 431 StringBuilder notEvil; |
| 427 notEvil.reserveCapacity(15010); | 432 notEvil.reserveCapacity(15010); |
| 428 notEvil.append('['); | 433 notEvil.append('['); |
| 429 for (int i = 0; i < 5000; ++i) | 434 for (int i = 0; i < 5000; ++i) |
| 430 notEvil.append("[],"); | 435 notEvil.append("[],"); |
| 431 notEvil.append("[]]"); | 436 notEvil.append("[]]"); |
| 432 root = parseJSON(notEvil.toString()); | 437 root = parseJSON(notEvil.toString()); |
| 433 ASSERT_TRUE(root.get()); | 438 ASSERT_TRUE(root.get()); |
| 434 EXPECT_EQ(Value::TypeArray, root->type()); | 439 EXPECT_EQ(Value::TypeArray, root->type()); |
| 435 list = ListValue::cast(root.get()); | 440 list = ListValue::cast(root.get()); |
| 436 ASSERT_TRUE(list); | 441 ASSERT_TRUE(list); |
| 437 EXPECT_EQ(5001U, list->size()); | 442 EXPECT_EQ(5001U, list->size()); |
| 438 | 443 |
| 439 // Test utf8 encoded input | 444 // Test utf8 encoded input |
| 440 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\""); | 445 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\""); |
| 441 ASSERT_FALSE(root.get()); | 446 ASSERT_FALSE(root.get()); |
| 442 | 447 |
| 443 // Test utf16 encoded strings. | 448 // Test utf16 encoded strings. |
| 444 root = parseJSON("\"\\u20ac3,14\""); | 449 root = parseJSON("\"\\u20ac3,14\""); |
| 445 ASSERT_TRUE(root.get()); | 450 ASSERT_TRUE(root.get()); |
| 446 EXPECT_EQ(Value::TypeString, root->type()); | 451 EXPECT_EQ(Value::TypeString, root->type()); |
| 447 EXPECT_TRUE(root->asString(&strVal)); | 452 EXPECT_TRUE(root->asString(&strVal)); |
| 448 UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34}; | 453 UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34}; |
| 449 EXPECT_EQ(String16(tmp2, 5), strVal); | 454 EXPECT_EQ(String(tmp2, 5), strVal); |
| 450 | 455 |
| 451 root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\""); | 456 root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\""); |
| 452 ASSERT_TRUE(root.get()); | 457 ASSERT_TRUE(root.get()); |
| 453 EXPECT_EQ(Value::TypeString, root->type()); | 458 EXPECT_EQ(Value::TypeString, root->type()); |
| 454 EXPECT_TRUE(root->asString(&strVal)); | 459 EXPECT_TRUE(root->asString(&strVal)); |
| 455 UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c}; | 460 UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c}; |
| 456 EXPECT_EQ(String16(tmp3, 4), strVal); | 461 EXPECT_EQ(String(tmp3, 4), strVal); |
| 457 | 462 |
| 458 // Test literal root objects. | 463 // Test literal root objects. |
| 459 root = parseJSON("null"); | 464 root = parseJSON("null"); |
| 460 EXPECT_EQ(Value::TypeNull, root->type()); | 465 EXPECT_EQ(Value::TypeNull, root->type()); |
| 461 | 466 |
| 462 root = parseJSON("true"); | 467 root = parseJSON("true"); |
| 463 ASSERT_TRUE(root.get()); | 468 ASSERT_TRUE(root.get()); |
| 464 EXPECT_TRUE(root->asBoolean(&boolValue)); | 469 EXPECT_TRUE(root->asBoolean(&boolValue)); |
| 465 EXPECT_TRUE(boolValue); | 470 EXPECT_TRUE(boolValue); |
| 466 | 471 |
| 467 root = parseJSON("10"); | 472 root = parseJSON("10"); |
| 468 ASSERT_TRUE(root.get()); | 473 ASSERT_TRUE(root.get()); |
| 469 EXPECT_TRUE(root->asInteger(&integerValue)); | 474 EXPECT_TRUE(root->asInteger(&integerValue)); |
| 470 EXPECT_EQ(10, integerValue); | 475 EXPECT_EQ(10, integerValue); |
| 471 | 476 |
| 472 root = parseJSON("\"root\""); | 477 root = parseJSON("\"root\""); |
| 473 ASSERT_TRUE(root.get()); | 478 ASSERT_TRUE(root.get()); |
| 474 EXPECT_TRUE(root->asString(&strVal)); | 479 EXPECT_TRUE(root->asString(&strVal)); |
| 475 EXPECT_EQ("root", strVal); | 480 EXPECT_EQ("root", strVal); |
| 476 } | 481 } |
| 477 | 482 |
| 478 TEST(ParserTest, InvalidSanity) | 483 TEST(ProtocolParserTest, InvalidSanity) |
| 479 { | 484 { |
| 480 const char* const invalidJson[] = { | 485 const char* const invalidJson[] = { |
| 481 "/* test *", | 486 "/* test *", |
| 482 "{\"foo\"", | 487 "{\"foo\"", |
| 483 "{\"foo\":", | 488 "{\"foo\":", |
| 484 " [", | 489 " [", |
| 485 "\"\\u123g\"", | 490 "\"\\u123g\"", |
| 486 "{\n\"eh:\n}", | 491 "{\n\"eh:\n}", |
| 487 "////", | 492 "////", |
| 488 "*/**/", | 493 "*/**/", |
| 489 "/**/", | 494 "/**/", |
| 490 "/*/", | 495 "/*/", |
| 491 "//**/" | 496 "//**/" |
| 492 }; | 497 }; |
| 493 | 498 |
| 494 for (size_t i = 0; i < 11; ++i) { | 499 for (size_t i = 0; i < 11; ++i) { |
| 495 std::unique_ptr<protocol::Value> result = parseJSON(invalidJson[i]); | 500 std::unique_ptr<Value> result = parseJSON(invalidJson[i]); |
| 496 EXPECT_FALSE(result.get()); | 501 EXPECT_FALSE(result.get()); |
| 497 } | 502 } |
| 498 } | 503 } |
| 499 | 504 |
| 500 } // namespace protocol | |
| 501 } // namespace blink | 505 } // namespace blink |
| OLD | NEW |