OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/json/json_reader.h" | 5 #include "base/json/json_reader.h" |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 EXPECT_EQ("\xe2\x82\xac""3,14", str_val); | 498 EXPECT_EQ("\xe2\x82\xac""3,14", str_val); |
499 | 499 |
500 root.reset(JSONReader().ReadToValue("\"\\ud83d\\udca9\\ud83d\\udc6c\"")); | 500 root.reset(JSONReader().ReadToValue("\"\\ud83d\\udca9\\ud83d\\udc6c\"")); |
501 ASSERT_TRUE(root.get()); | 501 ASSERT_TRUE(root.get()); |
502 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); | 502 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); |
503 str_val.clear(); | 503 str_val.clear(); |
504 EXPECT_TRUE(root->GetAsString(&str_val)); | 504 EXPECT_TRUE(root->GetAsString(&str_val)); |
505 EXPECT_EQ("\xf0\x9f\x92\xa9\xf0\x9f\x91\xac", str_val); | 505 EXPECT_EQ("\xf0\x9f\x92\xa9\xf0\x9f\x91\xac", str_val); |
506 | 506 |
507 // Test invalid utf16 strings. | 507 // Test invalid utf16 strings. |
508 const char* cases[] = { | 508 const char* const cases[] = { |
509 "\"\\u123\"", // Invalid scalar. | 509 "\"\\u123\"", // Invalid scalar. |
510 "\"\\ud83d\"", // Invalid scalar. | 510 "\"\\ud83d\"", // Invalid scalar. |
511 "\"\\u$%@!\"", // Invalid scalar. | 511 "\"\\u$%@!\"", // Invalid scalar. |
512 "\"\\uzz89\"", // Invalid scalar. | 512 "\"\\uzz89\"", // Invalid scalar. |
513 "\"\\ud83d\\udca\"", // Invalid lower surrogate. | 513 "\"\\ud83d\\udca\"", // Invalid lower surrogate. |
514 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. | 514 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. |
515 "\"\\ud83foo\"", // No lower surrogate. | 515 "\"\\ud83foo\"", // No lower surrogate. |
516 "\"\\ud83\\foo\"" // No lower surrogate. | 516 "\"\\ud83\\foo\"" // No lower surrogate. |
517 }; | 517 }; |
518 for (size_t i = 0; i < arraysize(cases); ++i) { | 518 for (size_t i = 0; i < arraysize(cases); ++i) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 EXPECT_TRUE(list_value_0->GetAsString(&s)); | 620 EXPECT_TRUE(list_value_0->GetAsString(&s)); |
621 EXPECT_EQ("a", s); | 621 EXPECT_EQ("a", s); |
622 EXPECT_TRUE(list_value_1->GetAsString(&s)); | 622 EXPECT_TRUE(list_value_1->GetAsString(&s)); |
623 EXPECT_EQ("b", s); | 623 EXPECT_EQ("b", s); |
624 } | 624 } |
625 | 625 |
626 // A smattering of invalid JSON designed to test specific portions of the | 626 // A smattering of invalid JSON designed to test specific portions of the |
627 // parser implementation against buffer overflow. Best run with DCHECKs so | 627 // parser implementation against buffer overflow. Best run with DCHECKs so |
628 // that the one in NextChar fires. | 628 // that the one in NextChar fires. |
629 TEST(JSONReaderTest, InvalidSanity) { | 629 TEST(JSONReaderTest, InvalidSanity) { |
630 const char* invalid_json[] = { | 630 const char* const invalid_json[] = { |
631 "/* test *", | 631 "/* test *", |
632 "{\"foo\"", | 632 "{\"foo\"", |
633 "{\"foo\":", | 633 "{\"foo\":", |
634 " [", | 634 " [", |
635 "\"\\u123g\"", | 635 "\"\\u123g\"", |
636 "{\n\"eh:\n}", | 636 "{\n\"eh:\n}", |
637 }; | 637 }; |
638 | 638 |
639 for (size_t i = 0; i < arraysize(invalid_json); ++i) { | 639 for (size_t i = 0; i < arraysize(invalid_json); ++i) { |
640 JSONReader reader; | 640 JSONReader reader; |
641 LOG(INFO) << "Sanity test " << i << ": <" << invalid_json[i] << ">"; | 641 LOG(INFO) << "Sanity test " << i << ": <" << invalid_json[i] << ">"; |
642 EXPECT_FALSE(reader.ReadToValue(invalid_json[i])); | 642 EXPECT_FALSE(reader.ReadToValue(invalid_json[i])); |
643 EXPECT_NE(JSONReader::JSON_NO_ERROR, reader.error_code()); | 643 EXPECT_NE(JSONReader::JSON_NO_ERROR, reader.error_code()); |
644 EXPECT_NE("", reader.GetErrorMessage()); | 644 EXPECT_NE("", reader.GetErrorMessage()); |
645 } | 645 } |
646 } | 646 } |
647 | 647 |
648 TEST(JSONReaderTest, IllegalTrailingNull) { | 648 TEST(JSONReaderTest, IllegalTrailingNull) { |
649 const char json[] = { '"', 'n', 'u', 'l', 'l', '"', '\0' }; | 649 const char json[] = { '"', 'n', 'u', 'l', 'l', '"', '\0' }; |
650 std::string json_string(json, sizeof(json)); | 650 std::string json_string(json, sizeof(json)); |
651 JSONReader reader; | 651 JSONReader reader; |
652 EXPECT_FALSE(reader.ReadToValue(json_string)); | 652 EXPECT_FALSE(reader.ReadToValue(json_string)); |
653 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, reader.error_code()); | 653 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, reader.error_code()); |
654 } | 654 } |
655 | 655 |
656 } // namespace base | 656 } // namespace base |
OLD | NEW |