1 // Copyright (c) 2012 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 "base/json/json_reader.h" 6 7 #include <stddef.h> 8 9 #include <memory> 10 11 #if !defined(__ANDROID__) && !defined(__ANDROID_HOST__) 12 #include "base/base_paths.h" 13 #include "base/path_service.h" 14 #endif 15 16 #include "base/files/file_util.h" 17 #include "base/logging.h" 18 #include "base/macros.h" 19 #include "base/strings/string_piece.h" 20 #include "base/strings/utf_string_conversions.h" 21 #include "base/values.h" 22 #include "build/build_config.h" 23 #include "testing/gtest/include/gtest/gtest.h" 24 25 namespace base { 26 27 TEST(JSONReaderTest, Reading) { 28 { 29 // some whitespace checking 30 std::unique_ptr<Value> root = JSONReader().ReadToValue(" null "); 31 ASSERT_TRUE(root); 32 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); 33 } 34 35 { 36 // Invalid JSON string 37 EXPECT_FALSE(JSONReader().ReadToValue("nu")); 38 } 39 40 { 41 // Simple bool 42 std::unique_ptr<Value> root = JSONReader().ReadToValue("true "); 43 ASSERT_TRUE(root); 44 EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); 45 } 46 47 { 48 // Embedded comment 49 std::unique_ptr<Value> root = JSONReader().ReadToValue("/* comment */null"); 50 ASSERT_TRUE(root); 51 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); 52 root = JSONReader().ReadToValue("40 /* comment */"); 53 ASSERT_TRUE(root); 54 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); 55 root = JSONReader().ReadToValue("true // comment"); 56 ASSERT_TRUE(root); 57 EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); 58 root = JSONReader().ReadToValue("/* comment */\"sample string\""); 59 ASSERT_TRUE(root); 60 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 61 std::string value; 62 EXPECT_TRUE(root->GetAsString(&value)); 63 EXPECT_EQ("sample string", value); 64 std::unique_ptr<ListValue> list = ListValue::From( 65 JSONReader().ReadToValue("[1, /* comment, 2 ] */ \n 3]")); 66 ASSERT_TRUE(list); 67 EXPECT_EQ(2u, list->GetSize()); 68 int int_val = 0; 69 EXPECT_TRUE(list->GetInteger(0, &int_val)); 70 EXPECT_EQ(1, int_val); 71 EXPECT_TRUE(list->GetInteger(1, &int_val)); 72 EXPECT_EQ(3, int_val); 73 list = ListValue::From(JSONReader().ReadToValue("[1, /*a*/2, 3]")); 74 ASSERT_TRUE(list); 75 EXPECT_EQ(3u, list->GetSize()); 76 root = JSONReader().ReadToValue("/* comment **/42"); 77 ASSERT_TRUE(root); 78 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); 79 EXPECT_TRUE(root->GetAsInteger(&int_val)); 80 EXPECT_EQ(42, int_val); 81 root = JSONReader().ReadToValue( 82 "/* comment **/\n" 83 "// */ 43\n" 84 "44"); 85 ASSERT_TRUE(root); 86 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); 87 EXPECT_TRUE(root->GetAsInteger(&int_val)); 88 EXPECT_EQ(44, int_val); 89 } 90 91 { 92 // Test number formats 93 std::unique_ptr<Value> root = JSONReader().ReadToValue("43"); 94 ASSERT_TRUE(root); 95 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); 96 int int_val = 0; 97 EXPECT_TRUE(root->GetAsInteger(&int_val)); 98 EXPECT_EQ(43, int_val); 99 } 100 101 { 102 // According to RFC4627, oct, hex, and leading zeros are invalid JSON. 103 EXPECT_FALSE(JSONReader().ReadToValue("043")); 104 EXPECT_FALSE(JSONReader().ReadToValue("0x43")); 105 EXPECT_FALSE(JSONReader().ReadToValue("00")); 106 } 107 108 { 109 // Test 0 (which needs to be special cased because of the leading zero 110 // clause). 111 std::unique_ptr<Value> root = JSONReader().ReadToValue("0"); 112 ASSERT_TRUE(root); 113 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); 114 int int_val = 1; 115 EXPECT_TRUE(root->GetAsInteger(&int_val)); 116 EXPECT_EQ(0, int_val); 117 } 118 119 { 120 // Numbers that overflow ints should succeed, being internally promoted to 121 // storage as doubles 122 std::unique_ptr<Value> root = JSONReader().ReadToValue("2147483648"); 123 ASSERT_TRUE(root); 124 double double_val; 125 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 126 double_val = 0.0; 127 EXPECT_TRUE(root->GetAsDouble(&double_val)); 128 EXPECT_DOUBLE_EQ(2147483648.0, double_val); 129 root = JSONReader().ReadToValue("-2147483649"); 130 ASSERT_TRUE(root); 131 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 132 double_val = 0.0; 133 EXPECT_TRUE(root->GetAsDouble(&double_val)); 134 EXPECT_DOUBLE_EQ(-2147483649.0, double_val); 135 } 136 137 { 138 // Parse a double 139 std::unique_ptr<Value> root = JSONReader().ReadToValue("43.1"); 140 ASSERT_TRUE(root); 141 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 142 double double_val = 0.0; 143 EXPECT_TRUE(root->GetAsDouble(&double_val)); 144 EXPECT_DOUBLE_EQ(43.1, double_val); 145 146 root = JSONReader().ReadToValue("4.3e-1"); 147 ASSERT_TRUE(root); 148 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 149 double_val = 0.0; 150 EXPECT_TRUE(root->GetAsDouble(&double_val)); 151 EXPECT_DOUBLE_EQ(.43, double_val); 152 153 root = JSONReader().ReadToValue("2.1e0"); 154 ASSERT_TRUE(root); 155 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 156 double_val = 0.0; 157 EXPECT_TRUE(root->GetAsDouble(&double_val)); 158 EXPECT_DOUBLE_EQ(2.1, double_val); 159 160 root = JSONReader().ReadToValue("2.1e+0001"); 161 ASSERT_TRUE(root); 162 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 163 double_val = 0.0; 164 EXPECT_TRUE(root->GetAsDouble(&double_val)); 165 EXPECT_DOUBLE_EQ(21.0, double_val); 166 167 root = JSONReader().ReadToValue("0.01"); 168 ASSERT_TRUE(root); 169 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 170 double_val = 0.0; 171 EXPECT_TRUE(root->GetAsDouble(&double_val)); 172 EXPECT_DOUBLE_EQ(0.01, double_val); 173 174 root = JSONReader().ReadToValue("1.00"); 175 ASSERT_TRUE(root); 176 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 177 double_val = 0.0; 178 EXPECT_TRUE(root->GetAsDouble(&double_val)); 179 EXPECT_DOUBLE_EQ(1.0, double_val); 180 } 181 182 { 183 // Fractional parts must have a digit before and after the decimal point. 184 EXPECT_FALSE(JSONReader().ReadToValue("1.")); 185 EXPECT_FALSE(JSONReader().ReadToValue(".1")); 186 EXPECT_FALSE(JSONReader().ReadToValue("1.e10")); 187 } 188 189 { 190 // Exponent must have a digit following the 'e'. 191 EXPECT_FALSE(JSONReader().ReadToValue("1e")); 192 EXPECT_FALSE(JSONReader().ReadToValue("1E")); 193 EXPECT_FALSE(JSONReader().ReadToValue("1e1.")); 194 EXPECT_FALSE(JSONReader().ReadToValue("1e1.0")); 195 } 196 197 { 198 // INF/-INF/NaN are not valid 199 EXPECT_FALSE(JSONReader().ReadToValue("1e1000")); 200 EXPECT_FALSE(JSONReader().ReadToValue("-1e1000")); 201 EXPECT_FALSE(JSONReader().ReadToValue("NaN")); 202 EXPECT_FALSE(JSONReader().ReadToValue("nan")); 203 EXPECT_FALSE(JSONReader().ReadToValue("inf")); 204 } 205 206 { 207 // Invalid number formats 208 EXPECT_FALSE(JSONReader().ReadToValue("4.3.1")); 209 EXPECT_FALSE(JSONReader().ReadToValue("4e3.1")); 210 } 211 212 { 213 // Test string parser 214 std::unique_ptr<Value> root = JSONReader().ReadToValue("\"hello world\""); 215 ASSERT_TRUE(root); 216 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 217 std::string str_val; 218 EXPECT_TRUE(root->GetAsString(&str_val)); 219 EXPECT_EQ("hello world", str_val); 220 } 221 222 { 223 // Empty string 224 std::unique_ptr<Value> root = JSONReader().ReadToValue("\"\""); 225 ASSERT_TRUE(root); 226 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 227 std::string str_val; 228 EXPECT_TRUE(root->GetAsString(&str_val)); 229 EXPECT_EQ("", str_val); 230 } 231 232 { 233 // Test basic string escapes 234 std::unique_ptr<Value> root = 235 JSONReader().ReadToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\""); 236 ASSERT_TRUE(root); 237 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 238 std::string str_val; 239 EXPECT_TRUE(root->GetAsString(&str_val)); 240 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", str_val); 241 } 242 243 { 244 // Test hex and unicode escapes including the null character. 245 std::unique_ptr<Value> root = 246 JSONReader().ReadToValue("\"\\x41\\x00\\u1234\""); 247 ASSERT_TRUE(root); 248 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 249 std::string str_val; 250 EXPECT_TRUE(root->GetAsString(&str_val)); 251 EXPECT_EQ(std::wstring(L"A\0\x1234", 3), UTF8ToWide(str_val)); 252 } 253 254 { 255 // Test invalid strings 256 EXPECT_FALSE(JSONReader().ReadToValue("\"no closing quote")); 257 EXPECT_FALSE(JSONReader().ReadToValue("\"\\z invalid escape char\"")); 258 EXPECT_FALSE(JSONReader().ReadToValue("\"\\xAQ invalid hex code\"")); 259 EXPECT_FALSE(JSONReader().ReadToValue("not enough hex chars\\x1\"")); 260 EXPECT_FALSE(JSONReader().ReadToValue("\"not enough escape chars\\u123\"")); 261 EXPECT_FALSE( 262 JSONReader().ReadToValue("\"extra backslash at end of input\\\"")); 263 } 264 265 { 266 // Basic array 267 std::unique_ptr<ListValue> list = 268 ListValue::From(JSONReader::Read("[true, false, null]")); 269 ASSERT_TRUE(list); 270 EXPECT_EQ(3U, list->GetSize()); 271 272 // Test with trailing comma. Should be parsed the same as above. 273 std::unique_ptr<Value> root2 = 274 JSONReader::Read("[true, false, null, ]", JSON_ALLOW_TRAILING_COMMAS); 275 EXPECT_TRUE(list->Equals(root2.get())); 276 } 277 278 { 279 // Empty array 280 std::unique_ptr<ListValue> list = ListValue::From(JSONReader::Read("[]")); 281 ASSERT_TRUE(list); 282 EXPECT_EQ(0U, list->GetSize()); 283 } 284 285 { 286 // Nested arrays 287 std::unique_ptr<ListValue> list = ListValue::From( 288 JSONReader::Read("[[true], [], [false, [], [null]], null]")); 289 ASSERT_TRUE(list); 290 EXPECT_EQ(4U, list->GetSize()); 291 292 // Lots of trailing commas. 293 std::unique_ptr<Value> root2 = 294 JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]", 295 JSON_ALLOW_TRAILING_COMMAS); 296 EXPECT_TRUE(list->Equals(root2.get())); 297 } 298 299 { 300 // Invalid, missing close brace. 301 EXPECT_FALSE(JSONReader::Read("[[true], [], [false, [], [null]], null")); 302 303 // Invalid, too many commas 304 EXPECT_FALSE(JSONReader::Read("[true,, null]")); 305 EXPECT_FALSE(JSONReader::Read("[true,, null]", JSON_ALLOW_TRAILING_COMMAS)); 306 307 // Invalid, no commas 308 EXPECT_FALSE(JSONReader::Read("[true null]")); 309 310 // Invalid, trailing comma 311 EXPECT_FALSE(JSONReader::Read("[true,]")); 312 } 313 314 { 315 // Valid if we set |allow_trailing_comma| to true. 316 std::unique_ptr<ListValue> list = ListValue::From( 317 JSONReader::Read("[true,]", JSON_ALLOW_TRAILING_COMMAS)); 318 ASSERT_TRUE(list); 319 EXPECT_EQ(1U, list->GetSize()); 320 Value* tmp_value = nullptr; 321 ASSERT_TRUE(list->Get(0, &tmp_value)); 322 EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN)); 323 bool bool_value = false; 324 EXPECT_TRUE(tmp_value->GetAsBoolean(&bool_value)); 325 EXPECT_TRUE(bool_value); 326 } 327 328 { 329 // Don't allow empty elements, even if |allow_trailing_comma| is 330 // true. 331 EXPECT_FALSE(JSONReader::Read("[,]", JSON_ALLOW_TRAILING_COMMAS)); 332 EXPECT_FALSE(JSONReader::Read("[true,,]", JSON_ALLOW_TRAILING_COMMAS)); 333 EXPECT_FALSE(JSONReader::Read("[,true,]", JSON_ALLOW_TRAILING_COMMAS)); 334 EXPECT_FALSE(JSONReader::Read("[true,,false]", JSON_ALLOW_TRAILING_COMMAS)); 335 } 336 337 { 338 // Test objects 339 std::unique_ptr<DictionaryValue> dict_val = 340 DictionaryValue::From(JSONReader::Read("{}")); 341 ASSERT_TRUE(dict_val); 342 343 dict_val = DictionaryValue::From(JSONReader::Read( 344 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }")); 345 ASSERT_TRUE(dict_val); 346 double double_val = 0.0; 347 EXPECT_TRUE(dict_val->GetDouble("number", &double_val)); 348 EXPECT_DOUBLE_EQ(9.87654321, double_val); 349 Value* null_val = nullptr; 350 ASSERT_TRUE(dict_val->Get("null", &null_val)); 351 EXPECT_TRUE(null_val->IsType(Value::TYPE_NULL)); 352 std::string str_val; 353 EXPECT_TRUE(dict_val->GetString("S", &str_val)); 354 EXPECT_EQ("str", str_val); 355 356 std::unique_ptr<Value> root2 = JSONReader::Read( 357 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", 358 JSON_ALLOW_TRAILING_COMMAS); 359 ASSERT_TRUE(root2); 360 EXPECT_TRUE(dict_val->Equals(root2.get())); 361 362 // Test newline equivalence. 363 root2 = JSONReader::Read( 364 "{\n" 365 " \"number\":9.87654321,\n" 366 " \"null\":null,\n" 367 " \"\\x53\":\"str\",\n" 368 "}\n", 369 JSON_ALLOW_TRAILING_COMMAS); 370 ASSERT_TRUE(root2); 371 EXPECT_TRUE(dict_val->Equals(root2.get())); 372 373 root2 = JSONReader::Read( 374 "{\r\n" 375 " \"number\":9.87654321,\r\n" 376 " \"null\":null,\r\n" 377 " \"\\x53\":\"str\",\r\n" 378 "}\r\n", 379 JSON_ALLOW_TRAILING_COMMAS); 380 ASSERT_TRUE(root2); 381 EXPECT_TRUE(dict_val->Equals(root2.get())); 382 } 383 384 { 385 // Test nesting 386 std::unique_ptr<DictionaryValue> dict_val = 387 DictionaryValue::From(JSONReader::Read( 388 "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}")); 389 ASSERT_TRUE(dict_val); 390 DictionaryValue* inner_dict = nullptr; 391 ASSERT_TRUE(dict_val->GetDictionary("inner", &inner_dict)); 392 ListValue* inner_array = nullptr; 393 ASSERT_TRUE(inner_dict->GetList("array", &inner_array)); 394 EXPECT_EQ(1U, inner_array->GetSize()); 395 bool bool_value = true; 396 EXPECT_TRUE(dict_val->GetBoolean("false", &bool_value)); 397 EXPECT_FALSE(bool_value); 398 inner_dict = nullptr; 399 EXPECT_TRUE(dict_val->GetDictionary("d", &inner_dict)); 400 401 std::unique_ptr<Value> root2 = JSONReader::Read( 402 "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", 403 JSON_ALLOW_TRAILING_COMMAS); 404 EXPECT_TRUE(dict_val->Equals(root2.get())); 405 } 406 407 { 408 // Test keys with periods 409 std::unique_ptr<DictionaryValue> dict_val = DictionaryValue::From( 410 JSONReader::Read("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}")); 411 ASSERT_TRUE(dict_val); 412 int integer_value = 0; 413 EXPECT_TRUE( 414 dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value)); 415 EXPECT_EQ(3, integer_value); 416 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("c", &integer_value)); 417 EXPECT_EQ(2, integer_value); 418 DictionaryValue* inner_dict = nullptr; 419 ASSERT_TRUE( 420 dict_val->GetDictionaryWithoutPathExpansion("d.e.f", &inner_dict)); 421 EXPECT_EQ(1U, inner_dict->size()); 422 EXPECT_TRUE( 423 inner_dict->GetIntegerWithoutPathExpansion("g.h.i.j", &integer_value)); 424 EXPECT_EQ(1, integer_value); 425 426 dict_val = 427 DictionaryValue::From(JSONReader::Read("{\"a\":{\"b\":2},\"a.b\":1}")); 428 ASSERT_TRUE(dict_val); 429 EXPECT_TRUE(dict_val->GetInteger("a.b", &integer_value)); 430 EXPECT_EQ(2, integer_value); 431 EXPECT_TRUE( 432 dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value)); 433 EXPECT_EQ(1, integer_value); 434 } 435 436 { 437 // Invalid, no closing brace 438 EXPECT_FALSE(JSONReader::Read("{\"a\": true")); 439 440 // Invalid, keys must be quoted 441 EXPECT_FALSE(JSONReader::Read("{foo:true}")); 442 443 // Invalid, trailing comma 444 EXPECT_FALSE(JSONReader::Read("{\"a\":true,}")); 445 446 // Invalid, too many commas 447 EXPECT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}")); 448 EXPECT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", 449 JSON_ALLOW_TRAILING_COMMAS)); 450 451 // Invalid, no separator 452 EXPECT_FALSE(JSONReader::Read("{\"a\" \"b\"}")); 453 454 // Invalid, lone comma. 455 EXPECT_FALSE(JSONReader::Read("{,}")); 456 EXPECT_FALSE(JSONReader::Read("{,}", JSON_ALLOW_TRAILING_COMMAS)); 457 EXPECT_FALSE( 458 JSONReader::Read("{\"a\":true,,}", JSON_ALLOW_TRAILING_COMMAS)); 459 EXPECT_FALSE(JSONReader::Read("{,\"a\":true}", JSON_ALLOW_TRAILING_COMMAS)); 460 EXPECT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", 461 JSON_ALLOW_TRAILING_COMMAS)); 462 } 463 464 { 465 // Test stack overflow 466 std::string evil(1000000, '['); 467 evil.append(std::string(1000000, ']')); 468 EXPECT_FALSE(JSONReader::Read(evil)); 469 } 470 471 { 472 // A few thousand adjacent lists is fine. 473 std::string not_evil("["); 474 not_evil.reserve(15010); 475 for (int i = 0; i < 5000; ++i) 476 not_evil.append("[],"); 477 not_evil.append("[]]"); 478 std::unique_ptr<ListValue> list = 479 ListValue::From(JSONReader::Read(not_evil)); 480 ASSERT_TRUE(list); 481 EXPECT_EQ(5001U, list->GetSize()); 482 } 483 484 { 485 // Test utf8 encoded input 486 std::unique_ptr<Value> root = 487 JSONReader().ReadToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\""); 488 ASSERT_TRUE(root); 489 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 490 std::string str_val; 491 EXPECT_TRUE(root->GetAsString(&str_val)); 492 EXPECT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val)); 493 494 std::unique_ptr<DictionaryValue> dict_val = 495 DictionaryValue::From(JSONReader().ReadToValue( 496 "{\"path\": \"/tmp/\xc3\xa0\xc3\xa8\xc3\xb2.png\"}")); 497 ASSERT_TRUE(dict_val); 498 EXPECT_TRUE(dict_val->GetString("path", &str_val)); 499 EXPECT_EQ("/tmp/\xC3\xA0\xC3\xA8\xC3\xB2.png", str_val); 500 } 501 502 { 503 // Test invalid utf8 encoded input 504 EXPECT_FALSE(JSONReader().ReadToValue("\"345\xb0\xa1\xb0\xa2\"")); 505 EXPECT_FALSE(JSONReader().ReadToValue("\"123\xc0\x81\"")); 506 EXPECT_FALSE(JSONReader().ReadToValue("\"abc\xc0\xae\"")); 507 } 508 509 { 510 // Test utf16 encoded strings. 511 std::unique_ptr<Value> root = JSONReader().ReadToValue("\"\\u20ac3,14\""); 512 ASSERT_TRUE(root); 513 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 514 std::string str_val; 515 EXPECT_TRUE(root->GetAsString(&str_val)); 516 EXPECT_EQ( 517 "\xe2\x82\xac" 518 "3,14", 519 str_val); 520 521 root = JSONReader().ReadToValue("\"\\ud83d\\udca9\\ud83d\\udc6c\""); 522 ASSERT_TRUE(root); 523 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 524 str_val.clear(); 525 EXPECT_TRUE(root->GetAsString(&str_val)); 526 EXPECT_EQ("\xf0\x9f\x92\xa9\xf0\x9f\x91\xac", str_val); 527 } 528 529 { 530 // Test invalid utf16 strings. 531 const char* const cases[] = { 532 "\"\\u123\"", // Invalid scalar. 533 "\"\\ud83d\"", // Invalid scalar. 534 "\"\\u$%@!\"", // Invalid scalar. 535 "\"\\uzz89\"", // Invalid scalar. 536 "\"\\ud83d\\udca\"", // Invalid lower surrogate. 537 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. 538 "\"\\ud83foo\"", // No lower surrogate. 539 "\"\\ud83\\foo\"" // No lower surrogate. 540 }; 541 std::unique_ptr<Value> root; 542 for (size_t i = 0; i < arraysize(cases); ++i) { 543 root = JSONReader().ReadToValue(cases[i]); 544 EXPECT_FALSE(root) << cases[i]; 545 } 546 } 547 548 { 549 // Test literal root objects. 550 std::unique_ptr<Value> root = JSONReader::Read("null"); 551 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); 552 553 root = JSONReader::Read("true"); 554 ASSERT_TRUE(root); 555 bool bool_value; 556 EXPECT_TRUE(root->GetAsBoolean(&bool_value)); 557 EXPECT_TRUE(bool_value); 558 559 root = JSONReader::Read("10"); 560 ASSERT_TRUE(root); 561 int integer_value; 562 EXPECT_TRUE(root->GetAsInteger(&integer_value)); 563 EXPECT_EQ(10, integer_value); 564 565 root = JSONReader::Read("\"root\""); 566 ASSERT_TRUE(root); 567 std::string str_val; 568 EXPECT_TRUE(root->GetAsString(&str_val)); 569 EXPECT_EQ("root", str_val); 570 } 571 } 572 573 #if !defined(__ANDROID__) && !defined(__ANDROID_HOST__) 574 TEST(JSONReaderTest, ReadFromFile) { 575 FilePath path; 576 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &path)); 577 path = path.AppendASCII("json"); 578 ASSERT_TRUE(base::PathExists(path)); 579 580 std::string input; 581 ASSERT_TRUE(ReadFileToString(path.AppendASCII("bom_feff.json"), &input)); 582 583 JSONReader reader; 584 std::unique_ptr<Value> root(reader.ReadToValue(input)); 585 ASSERT_TRUE(root) << reader.GetErrorMessage(); 586 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 587 } 588 #endif // !__ANDROID__ && !__ANDROID_HOST__ 589 590 // Tests that the root of a JSON object can be deleted safely while its 591 // children outlive it. 592 TEST(JSONReaderTest, StringOptimizations) { 593 std::unique_ptr<Value> dict_literal_0; 594 std::unique_ptr<Value> dict_literal_1; 595 std::unique_ptr<Value> dict_string_0; 596 std::unique_ptr<Value> dict_string_1; 597 std::unique_ptr<Value> list_value_0; 598 std::unique_ptr<Value> list_value_1; 599 600 { 601 std::unique_ptr<Value> root = JSONReader::Read( 602 "{" 603 " \"test\": {" 604 " \"foo\": true," 605 " \"bar\": 3.14," 606 " \"baz\": \"bat\"," 607 " \"moo\": \"cow\"" 608 " }," 609 " \"list\": [" 610 " \"a\"," 611 " \"b\"" 612 " ]" 613 "}", 614 JSON_DETACHABLE_CHILDREN); 615 ASSERT_TRUE(root); 616 617 DictionaryValue* root_dict = nullptr; 618 ASSERT_TRUE(root->GetAsDictionary(&root_dict)); 619 620 DictionaryValue* dict = nullptr; 621 ListValue* list = nullptr; 622 623 ASSERT_TRUE(root_dict->GetDictionary("test", &dict)); 624 ASSERT_TRUE(root_dict->GetList("list", &list)); 625 626 ASSERT_TRUE(dict->Remove("foo", &dict_literal_0)); 627 ASSERT_TRUE(dict->Remove("bar", &dict_literal_1)); 628 ASSERT_TRUE(dict->Remove("baz", &dict_string_0)); 629 ASSERT_TRUE(dict->Remove("moo", &dict_string_1)); 630 631 ASSERT_EQ(2u, list->GetSize()); 632 ASSERT_TRUE(list->Remove(0, &list_value_0)); 633 ASSERT_TRUE(list->Remove(0, &list_value_1)); 634 } 635 636 bool b = false; 637 double d = 0; 638 std::string s; 639 640 EXPECT_TRUE(dict_literal_0->GetAsBoolean(&b)); 641 EXPECT_TRUE(b); 642 643 EXPECT_TRUE(dict_literal_1->GetAsDouble(&d)); 644 EXPECT_EQ(3.14, d); 645 646 EXPECT_TRUE(dict_string_0->GetAsString(&s)); 647 EXPECT_EQ("bat", s); 648 649 EXPECT_TRUE(dict_string_1->GetAsString(&s)); 650 EXPECT_EQ("cow", s); 651 652 EXPECT_TRUE(list_value_0->GetAsString(&s)); 653 EXPECT_EQ("a", s); 654 EXPECT_TRUE(list_value_1->GetAsString(&s)); 655 EXPECT_EQ("b", s); 656 } 657 658 // A smattering of invalid JSON designed to test specific portions of the 659 // parser implementation against buffer overflow. Best run with DCHECKs so 660 // that the one in NextChar fires. 661 TEST(JSONReaderTest, InvalidSanity) { 662 const char* const kInvalidJson[] = { 663 "/* test *", "{\"foo\"", "{\"foo\":", " [", "\"\\u123g\"", "{\n\"eh:\n}", 664 }; 665 666 for (size_t i = 0; i < arraysize(kInvalidJson); ++i) { 667 JSONReader reader; 668 LOG(INFO) << "Sanity test " << i << ": <" << kInvalidJson[i] << ">"; 669 EXPECT_FALSE(reader.ReadToValue(kInvalidJson[i])); 670 EXPECT_NE(JSONReader::JSON_NO_ERROR, reader.error_code()); 671 EXPECT_NE("", reader.GetErrorMessage()); 672 } 673 } 674 675 TEST(JSONReaderTest, IllegalTrailingNull) { 676 const char json[] = { '"', 'n', 'u', 'l', 'l', '"', '\0' }; 677 std::string json_string(json, sizeof(json)); 678 JSONReader reader; 679 EXPECT_FALSE(reader.ReadToValue(json_string)); 680 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, reader.error_code()); 681 } 682 683 } // namespace base 684