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/file_util.h" | 8 #include "base/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 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 | 552 |
553 JSONReader reader; | 553 JSONReader reader; |
554 scoped_ptr<Value> root(reader.ReadToValue(input)); | 554 scoped_ptr<Value> root(reader.ReadToValue(input)); |
555 ASSERT_TRUE(root.get()) << reader.GetErrorMessage(); | 555 ASSERT_TRUE(root.get()) << reader.GetErrorMessage(); |
556 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 556 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
557 } | 557 } |
558 | 558 |
559 // Tests that the root of a JSON object can be deleted safely while its | 559 // Tests that the root of a JSON object can be deleted safely while its |
560 // children outlive it. | 560 // children outlive it. |
561 TEST(JSONReaderTest, StringOptimizations) { | 561 TEST(JSONReaderTest, StringOptimizations) { |
562 Value* dict_literals[2] = {0}; | 562 scoped_ptr<Value> dict_literal_0; |
563 Value* dict_strings[2] = {0}; | 563 scoped_ptr<Value> dict_literal_1; |
564 Value* list_values[2] = {0}; | 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; |
565 | 568 |
566 { | 569 { |
567 scoped_ptr<Value> root(JSONReader::Read( | 570 scoped_ptr<Value> root(JSONReader::Read( |
568 "{" | 571 "{" |
569 " \"test\": {" | 572 " \"test\": {" |
570 " \"foo\": true," | 573 " \"foo\": true," |
571 " \"bar\": 3.14," | 574 " \"bar\": 3.14," |
572 " \"baz\": \"bat\"," | 575 " \"baz\": \"bat\"," |
573 " \"moo\": \"cow\"" | 576 " \"moo\": \"cow\"" |
574 " }," | 577 " }," |
575 " \"list\": [" | 578 " \"list\": [" |
576 " \"a\"," | 579 " \"a\"," |
577 " \"b\"" | 580 " \"b\"" |
578 " ]" | 581 " ]" |
579 "}", JSON_DETACHABLE_CHILDREN)); | 582 "}", JSON_DETACHABLE_CHILDREN)); |
580 ASSERT_TRUE(root.get()); | 583 ASSERT_TRUE(root.get()); |
581 | 584 |
582 DictionaryValue* root_dict = NULL; | 585 DictionaryValue* root_dict = NULL; |
583 ASSERT_TRUE(root->GetAsDictionary(&root_dict)); | 586 ASSERT_TRUE(root->GetAsDictionary(&root_dict)); |
584 | 587 |
585 DictionaryValue* dict = NULL; | 588 DictionaryValue* dict = NULL; |
586 ListValue* list = NULL; | 589 ListValue* list = NULL; |
587 | 590 |
588 ASSERT_TRUE(root_dict->GetDictionary("test", &dict)); | 591 ASSERT_TRUE(root_dict->GetDictionary("test", &dict)); |
589 ASSERT_TRUE(root_dict->GetList("list", &list)); | 592 ASSERT_TRUE(root_dict->GetList("list", &list)); |
590 | 593 |
591 EXPECT_TRUE(dict->Remove("foo", &dict_literals[0])); | 594 EXPECT_TRUE(dict->Remove("foo", &dict_literal_0)); |
592 EXPECT_TRUE(dict->Remove("bar", &dict_literals[1])); | 595 EXPECT_TRUE(dict->Remove("bar", &dict_literal_1)); |
593 EXPECT_TRUE(dict->Remove("baz", &dict_strings[0])); | 596 EXPECT_TRUE(dict->Remove("baz", &dict_string_0)); |
594 EXPECT_TRUE(dict->Remove("moo", &dict_strings[1])); | 597 EXPECT_TRUE(dict->Remove("moo", &dict_string_1)); |
595 | 598 |
596 ASSERT_EQ(2u, list->GetSize()); | 599 ASSERT_EQ(2u, list->GetSize()); |
597 EXPECT_TRUE(list->Remove(0, &list_values[0])); | 600 EXPECT_TRUE(list->Remove(0, &list_value_0)); |
598 EXPECT_TRUE(list->Remove(0, &list_values[1])); | 601 EXPECT_TRUE(list->Remove(0, &list_value_1)); |
599 } | 602 } |
600 | 603 |
601 bool b = false; | 604 bool b = false; |
602 double d = 0; | 605 double d = 0; |
603 std::string s; | 606 std::string s; |
604 | 607 |
605 EXPECT_TRUE(dict_literals[0]->GetAsBoolean(&b)); | 608 EXPECT_TRUE(dict_literal_0->GetAsBoolean(&b)); |
606 EXPECT_TRUE(b); | 609 EXPECT_TRUE(b); |
607 | 610 |
608 EXPECT_TRUE(dict_literals[1]->GetAsDouble(&d)); | 611 EXPECT_TRUE(dict_literal_1->GetAsDouble(&d)); |
609 EXPECT_EQ(3.14, d); | 612 EXPECT_EQ(3.14, d); |
610 | 613 |
611 EXPECT_TRUE(dict_strings[0]->GetAsString(&s)); | 614 EXPECT_TRUE(dict_string_0->GetAsString(&s)); |
612 EXPECT_EQ("bat", s); | 615 EXPECT_EQ("bat", s); |
613 | 616 |
614 EXPECT_TRUE(dict_strings[1]->GetAsString(&s)); | 617 EXPECT_TRUE(dict_string_1->GetAsString(&s)); |
615 EXPECT_EQ("cow", s); | 618 EXPECT_EQ("cow", s); |
616 | 619 |
617 EXPECT_TRUE(list_values[0]->GetAsString(&s)); | 620 EXPECT_TRUE(list_value_0->GetAsString(&s)); |
618 EXPECT_EQ("a", s); | 621 EXPECT_EQ("a", s); |
619 EXPECT_TRUE(list_values[1]->GetAsString(&s)); | 622 EXPECT_TRUE(list_value_1->GetAsString(&s)); |
620 EXPECT_EQ("b", s); | 623 EXPECT_EQ("b", s); |
621 | |
622 delete dict_literals[0]; | |
623 delete dict_literals[1]; | |
624 delete dict_strings[0]; | |
625 delete dict_strings[1]; | |
626 delete list_values[0]; | |
627 delete list_values[1]; | |
628 } | 624 } |
629 | 625 |
630 // 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 |
631 // parser implementation against buffer overflow. Best run with DCHECKs so | 627 // parser implementation against buffer overflow. Best run with DCHECKs so |
632 // that the one in NextChar fires. | 628 // that the one in NextChar fires. |
633 TEST(JSONReaderTest, InvalidSanity) { | 629 TEST(JSONReaderTest, InvalidSanity) { |
634 const char* invalid_json[] = { | 630 const char* invalid_json[] = { |
635 "/* test *", | 631 "/* test *", |
636 "{\"foo\"", | 632 "{\"foo\"", |
637 "{\"foo\":", | 633 "{\"foo\":", |
(...skipping 13 matching lines...) Expand all Loading... |
651 | 647 |
652 TEST(JSONReaderTest, IllegalTrailingNull) { | 648 TEST(JSONReaderTest, IllegalTrailingNull) { |
653 const char json[] = { '"', 'n', 'u', 'l', 'l', '"', '\0' }; | 649 const char json[] = { '"', 'n', 'u', 'l', 'l', '"', '\0' }; |
654 std::string json_string(json, sizeof(json)); | 650 std::string json_string(json, sizeof(json)); |
655 JSONReader reader; | 651 JSONReader reader; |
656 EXPECT_FALSE(reader.ReadToValue(json_string)); | 652 EXPECT_FALSE(reader.ReadToValue(json_string)); |
657 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, reader.error_code()); | 653 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, reader.error_code()); |
658 } | 654 } |
659 | 655 |
660 } // namespace base | 656 } // namespace base |
OLD | NEW |