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