| 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/String16.h" |
| 8 #include "platform/inspector_protocol/Values.h" | 8 #include "platform/inspector_protocol/Values.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 | 219 |
| 220 // Test basic string escapes | 220 // Test basic string escapes |
| 221 root = parseJSON("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\""); | 221 root = parseJSON("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\""); |
| 222 ASSERT_TRUE(root.get()); | 222 ASSERT_TRUE(root.get()); |
| 223 EXPECT_EQ(Value::TypeString, root->type()); | 223 EXPECT_EQ(Value::TypeString, root->type()); |
| 224 EXPECT_TRUE(root->asString(&strVal)); | 224 EXPECT_TRUE(root->asString(&strVal)); |
| 225 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", strVal); | 225 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", strVal); |
| 226 | 226 |
| 227 // Test hex and unicode escapes including the null character. | 227 // Test hex and unicode escapes including the null character. |
| 228 root = parseJSON("\"\\x41\\x00\\u1234\""); | 228 root = parseJSON("\"\\x41\\x00\\u1234\""); |
| 229 ASSERT_TRUE(root.get()); | 229 EXPECT_FALSE(root.get()); |
| 230 EXPECT_EQ(Value::TypeString, root->type()); | |
| 231 EXPECT_TRUE(root->asString(&strVal)); | |
| 232 UChar tmp1[] = {0x41, 0, 0x1234}; | |
| 233 EXPECT_EQ(String16(tmp1, 3), strVal); | |
| 234 | 230 |
| 235 // Test invalid strings | 231 // Test invalid strings |
| 236 root = parseJSON("\"no closing quote"); | 232 root = parseJSON("\"no closing quote"); |
| 237 EXPECT_FALSE(root.get()); | 233 EXPECT_FALSE(root.get()); |
| 238 root = parseJSON("\"\\z invalid escape char\""); | 234 root = parseJSON("\"\\z invalid escape char\""); |
| 239 EXPECT_FALSE(root.get()); | 235 EXPECT_FALSE(root.get()); |
| 240 root = parseJSON("\"\\xAQ invalid hex code\""); | |
| 241 EXPECT_FALSE(root.get()); | |
| 242 root = parseJSON("not enough hex chars\\x1\""); | |
| 243 EXPECT_FALSE(root.get()); | |
| 244 root = parseJSON("\"not enough escape chars\\u123\""); | 236 root = parseJSON("\"not enough escape chars\\u123\""); |
| 245 EXPECT_FALSE(root.get()); | 237 EXPECT_FALSE(root.get()); |
| 246 root = parseJSON("\"extra backslash at end of input\\\""); | 238 root = parseJSON("\"extra backslash at end of input\\\""); |
| 247 EXPECT_FALSE(root.get()); | 239 EXPECT_FALSE(root.get()); |
| 248 | 240 |
| 249 // Basic array | 241 // Basic array |
| 250 root = parseJSON("[true, false, null]"); | 242 root = parseJSON("[true, false, null]"); |
| 251 ASSERT_TRUE(root.get()); | 243 ASSERT_TRUE(root.get()); |
| 252 EXPECT_EQ(Value::TypeArray, root->type()); | 244 EXPECT_EQ(Value::TypeArray, root->type()); |
| 253 list = ListValue::cast(root.get()); | 245 list = ListValue::cast(root.get()); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 root = parseJSON("[,true,]"); | 299 root = parseJSON("[,true,]"); |
| 308 EXPECT_FALSE(root.get()); | 300 EXPECT_FALSE(root.get()); |
| 309 root = parseJSON("[true,,false]"); | 301 root = parseJSON("[true,,false]"); |
| 310 EXPECT_FALSE(root.get()); | 302 EXPECT_FALSE(root.get()); |
| 311 | 303 |
| 312 // Test objects | 304 // Test objects |
| 313 root = parseJSON("{}"); | 305 root = parseJSON("{}"); |
| 314 ASSERT_TRUE(root.get()); | 306 ASSERT_TRUE(root.get()); |
| 315 EXPECT_EQ(Value::TypeObject, root->type()); | 307 EXPECT_EQ(Value::TypeObject, root->type()); |
| 316 | 308 |
| 317 root = parseJSON("{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\
" }"); | 309 root = parseJSON("{\"number\":9.87654321, \"null\":null , \"S\" : \"str\" }"
); |
| 318 ASSERT_TRUE(root.get()); | 310 ASSERT_TRUE(root.get()); |
| 319 EXPECT_EQ(Value::TypeObject, root->type()); | 311 EXPECT_EQ(Value::TypeObject, root->type()); |
| 320 protocol::DictionaryValue* objectVal = DictionaryValue::cast(root.get()); | 312 protocol::DictionaryValue* objectVal = DictionaryValue::cast(root.get()); |
| 321 ASSERT_TRUE(objectVal); | 313 ASSERT_TRUE(objectVal); |
| 322 doubleVal = 0.0; | 314 doubleVal = 0.0; |
| 323 EXPECT_TRUE(objectVal->getNumber("number", &doubleVal)); | 315 EXPECT_TRUE(objectVal->getNumber("number", &doubleVal)); |
| 324 EXPECT_DOUBLE_EQ(9.87654321, doubleVal); | 316 EXPECT_DOUBLE_EQ(9.87654321, doubleVal); |
| 325 protocol::Value* nullVal = objectVal->get("null"); | 317 protocol::Value* nullVal = objectVal->get("null"); |
| 326 ASSERT_TRUE(nullVal); | 318 ASSERT_TRUE(nullVal); |
| 327 EXPECT_EQ(Value::TypeNull, nullVal->type()); | 319 EXPECT_EQ(Value::TypeNull, nullVal->type()); |
| 328 EXPECT_TRUE(objectVal->getString("S", &strVal)); | 320 EXPECT_TRUE(objectVal->getString("S", &strVal)); |
| 329 EXPECT_EQ("str", strVal); | 321 EXPECT_EQ("str", strVal); |
| 330 | 322 |
| 331 // Test newline equivalence. | 323 // Test newline equivalence. |
| 332 root2 = parseJSON( | 324 root2 = parseJSON( |
| 333 "{\n" | 325 "{\n" |
| 334 " \"number\":9.87654321,\n" | 326 " \"number\":9.87654321,\n" |
| 335 " \"null\":null,\n" | 327 " \"null\":null,\n" |
| 336 " \"\\x53\":\"str\"\n" | 328 " \"S\":\"str\"\n" |
| 337 "}\n"); | 329 "}\n"); |
| 338 ASSERT_TRUE(root2.get()); | 330 ASSERT_TRUE(root2.get()); |
| 339 EXPECT_EQ(root->toJSONString(), root2->toJSONString()); | 331 EXPECT_EQ(root->toJSONString(), root2->toJSONString()); |
| 340 | 332 |
| 341 root2 = parseJSON( | 333 root2 = parseJSON( |
| 342 "{\r\n" | 334 "{\r\n" |
| 343 " \"number\":9.87654321,\r\n" | 335 " \"number\":9.87654321,\r\n" |
| 344 " \"null\":null,\r\n" | 336 " \"null\":null,\r\n" |
| 345 " \"\\x53\":\"str\"\r\n" | 337 " \"S\":\"str\"\r\n" |
| 346 "}\r\n"); | 338 "}\r\n"); |
| 347 ASSERT_TRUE(root2.get()); | 339 ASSERT_TRUE(root2.get()); |
| 348 EXPECT_EQ(root->toJSONString(), root2->toJSONString()); | 340 EXPECT_EQ(root->toJSONString(), root2->toJSONString()); |
| 349 | 341 |
| 350 // Test nesting | 342 // Test nesting |
| 351 root = parseJSON("{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}"); | 343 root = parseJSON("{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}"); |
| 352 ASSERT_TRUE(root.get()); | 344 ASSERT_TRUE(root.get()); |
| 353 EXPECT_EQ(Value::TypeObject, root->type()); | 345 EXPECT_EQ(Value::TypeObject, root->type()); |
| 354 objectVal = DictionaryValue::cast(root.get()); | 346 objectVal = DictionaryValue::cast(root.get()); |
| 355 ASSERT_TRUE(objectVal); | 347 ASSERT_TRUE(objectVal); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 notEvil.append("[]]"); | 434 notEvil.append("[]]"); |
| 443 root = parseJSON(notEvil.toString()); | 435 root = parseJSON(notEvil.toString()); |
| 444 ASSERT_TRUE(root.get()); | 436 ASSERT_TRUE(root.get()); |
| 445 EXPECT_EQ(Value::TypeArray, root->type()); | 437 EXPECT_EQ(Value::TypeArray, root->type()); |
| 446 list = ListValue::cast(root.get()); | 438 list = ListValue::cast(root.get()); |
| 447 ASSERT_TRUE(list); | 439 ASSERT_TRUE(list); |
| 448 EXPECT_EQ(5001U, list->size()); | 440 EXPECT_EQ(5001U, list->size()); |
| 449 | 441 |
| 450 // Test utf8 encoded input | 442 // Test utf8 encoded input |
| 451 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\""); | 443 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\""); |
| 452 ASSERT_TRUE(root.get()); | |
| 453 EXPECT_EQ(Value::TypeString, root->type()); | |
| 454 EXPECT_TRUE(root->asString(&strVal)); | |
| 455 UChar tmp4[] = {0x7f51, 0x9875}; | |
| 456 EXPECT_EQ(String16(tmp4, 2), strVal); | |
| 457 | |
| 458 root = parseJSON("{\"path\": \"/tmp/\\xc3\\xa0\\xc3\\xa8\\xc3\\xb2.png\"}"); | |
| 459 ASSERT_TRUE(root.get()); | |
| 460 EXPECT_EQ(Value::TypeObject, root->type()); | |
| 461 objectVal = DictionaryValue::cast(root.get()); | |
| 462 ASSERT_TRUE(objectVal); | |
| 463 EXPECT_TRUE(objectVal->getString("path", &strVal)); | |
| 464 UChar tmp5[] = {0x2f, 0x74, 0x6d, 0x70, 0x2f, 0xe0, 0xe8, 0xf2, 0x2e, 0x70,
0x6e, 0x67}; | |
| 465 EXPECT_EQ(String16(tmp5, 12), strVal); | |
| 466 | |
| 467 // Test invalid utf8 encoded input | |
| 468 root = parseJSON("\"345\\xb0\\xa1\\xb0\\xa2\""); | |
| 469 ASSERT_FALSE(root.get()); | |
| 470 root = parseJSON("\"123\\xc0\\x81\""); | |
| 471 ASSERT_FALSE(root.get()); | |
| 472 root = parseJSON("\"abc\\xc0\\xae\""); | |
| 473 ASSERT_FALSE(root.get()); | 444 ASSERT_FALSE(root.get()); |
| 474 | 445 |
| 475 // Test utf16 encoded strings. | 446 // Test utf16 encoded strings. |
| 476 root = parseJSON("\"\\u20ac3,14\""); | 447 root = parseJSON("\"\\u20ac3,14\""); |
| 477 ASSERT_TRUE(root.get()); | 448 ASSERT_TRUE(root.get()); |
| 478 EXPECT_EQ(Value::TypeString, root->type()); | 449 EXPECT_EQ(Value::TypeString, root->type()); |
| 479 EXPECT_TRUE(root->asString(&strVal)); | 450 EXPECT_TRUE(root->asString(&strVal)); |
| 480 UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34}; | 451 UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34}; |
| 481 EXPECT_EQ(String16(tmp2, 5), strVal); | 452 EXPECT_EQ(String16(tmp2, 5), strVal); |
| 482 | 453 |
| 483 root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\""); | 454 root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\""); |
| 484 ASSERT_TRUE(root.get()); | 455 ASSERT_TRUE(root.get()); |
| 485 EXPECT_EQ(Value::TypeString, root->type()); | 456 EXPECT_EQ(Value::TypeString, root->type()); |
| 486 EXPECT_TRUE(root->asString(&strVal)); | 457 EXPECT_TRUE(root->asString(&strVal)); |
| 487 UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c}; | 458 UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c}; |
| 488 EXPECT_EQ(String16(tmp3, 4), strVal); | 459 EXPECT_EQ(String16(tmp3, 4), strVal); |
| 489 | 460 |
| 490 // Test invalid utf16 strings. | |
| 491 const char* const cases[] = { | |
| 492 "\"\\u123\"", // Invalid scalar. | |
| 493 "\"\\ud83d\"", // Invalid scalar. | |
| 494 "\"\\u$%@!\"", // Invalid scalar. | |
| 495 "\"\\uzz89\"", // Invalid scalar. | |
| 496 "\"\\ud83d\\udca\"", // Invalid lower surrogate. | |
| 497 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. | |
| 498 "\"\\ud83foo\"", // No lower surrogate. | |
| 499 "\"\\ud83\\foo\"" // No lower surrogate. | |
| 500 }; | |
| 501 for (size_t i = 0; i < 8; ++i) { | |
| 502 root = parseJSON(cases[i]); | |
| 503 EXPECT_FALSE(root.get()) << cases[i]; | |
| 504 } | |
| 505 | |
| 506 // Test literal root objects. | 461 // Test literal root objects. |
| 507 root = parseJSON("null"); | 462 root = parseJSON("null"); |
| 508 EXPECT_EQ(Value::TypeNull, root->type()); | 463 EXPECT_EQ(Value::TypeNull, root->type()); |
| 509 | 464 |
| 510 root = parseJSON("true"); | 465 root = parseJSON("true"); |
| 511 ASSERT_TRUE(root.get()); | 466 ASSERT_TRUE(root.get()); |
| 512 EXPECT_TRUE(root->asBoolean(&boolValue)); | 467 EXPECT_TRUE(root->asBoolean(&boolValue)); |
| 513 EXPECT_TRUE(boolValue); | 468 EXPECT_TRUE(boolValue); |
| 514 | 469 |
| 515 root = parseJSON("10"); | 470 root = parseJSON("10"); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 540 }; | 495 }; |
| 541 | 496 |
| 542 for (size_t i = 0; i < 11; ++i) { | 497 for (size_t i = 0; i < 11; ++i) { |
| 543 OwnPtr<protocol::Value> result = parseJSON(invalidJson[i]); | 498 OwnPtr<protocol::Value> result = parseJSON(invalidJson[i]); |
| 544 EXPECT_FALSE(result.get()); | 499 EXPECT_FALSE(result.get()); |
| 545 } | 500 } |
| 546 } | 501 } |
| 547 | 502 |
| 548 } // namespace protocol | 503 } // namespace protocol |
| 549 } // namespace blink | 504 } // namespace blink |
| OLD | NEW |