OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/policy/core/common/schema.h" | 5 #include "components/policy/core/common/schema.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "components/policy/core/common/schema_internal.h" | 8 #include "components/policy/core/common/schema_internal.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 | 114 |
115 void TestSchemaValidation(Schema schema, | 115 void TestSchemaValidation(Schema schema, |
116 const base::Value& value, | 116 const base::Value& value, |
117 SchemaOnErrorStrategy strategy, | 117 SchemaOnErrorStrategy strategy, |
118 bool expected_return_value) { | 118 bool expected_return_value) { |
119 std::string error; | 119 std::string error; |
120 static const char kNoErrorReturned[] = "No error returned."; | 120 static const char kNoErrorReturned[] = "No error returned."; |
121 | 121 |
122 // Test that Schema::Validate() works as expected. | 122 // Test that Schema::Validate() works as expected. |
123 error = kNoErrorReturned; | 123 error = kNoErrorReturned; |
124 bool returned = schema.Validate(value, strategy, &error); | 124 bool returned = schema.Validate(value, strategy, NULL, &error); |
125 EXPECT_EQ(returned, expected_return_value) << error; | 125 EXPECT_EQ(returned, expected_return_value) << error; |
126 | 126 |
127 // Test that Schema::Normalize() will return the same value as | 127 // Test that Schema::Normalize() will return the same value as |
128 // Schema::Validate(). | 128 // Schema::Validate(). |
129 error = kNoErrorReturned; | 129 error = kNoErrorReturned; |
130 scoped_ptr<base::Value> cloned_value(value.DeepCopy()); | 130 scoped_ptr<base::Value> cloned_value(value.DeepCopy()); |
131 returned = schema.Normalize(cloned_value.get(), strategy, &error); | 131 returned = schema.Normalize(cloned_value.get(), strategy, NULL, &error); |
132 EXPECT_EQ(returned, expected_return_value) << error; | 132 EXPECT_EQ(returned, expected_return_value) << error; |
133 | 133 |
134 // Test that Schema::Normalize() have actually dropped invalid and unknown | 134 // Test that Schema::Normalize() have actually dropped invalid and unknown |
135 // properties. | 135 // properties. |
136 if (expected_return_value) { | 136 if (expected_return_value) { |
137 EXPECT_TRUE(schema.Validate(*cloned_value.get(), SCHEMA_STRICT, &error)); | 137 EXPECT_TRUE( |
138 EXPECT_TRUE(schema.Normalize(cloned_value.get(), SCHEMA_STRICT, &error)); | 138 schema.Validate(*cloned_value.get(), SCHEMA_STRICT, NULL, &error)); |
139 EXPECT_TRUE( | |
140 schema.Normalize(cloned_value.get(), SCHEMA_STRICT, NULL, &error)); | |
139 } | 141 } |
140 } | 142 } |
141 | 143 |
144 void TestSchemaValidationWithPath(Schema schema, | |
145 const base::Value& value, | |
146 const std::string& expected_failure_path) { | |
147 std::string error_path = "NOT_SET"; | |
148 std::string error; | |
149 | |
150 bool returned = schema.Validate(value, SCHEMA_STRICT, &error_path, &error); | |
151 ASSERT_FALSE(returned) << error_path; | |
152 EXPECT_EQ(error_path, expected_failure_path); | |
153 } | |
154 | |
142 std::string SchemaObjectWrapper(const std::string& subschema) { | 155 std::string SchemaObjectWrapper(const std::string& subschema) { |
143 return "{" | 156 return "{" |
144 " \"type\": \"object\"," | 157 " \"type\": \"object\"," |
145 " \"properties\": {" | 158 " \"properties\": {" |
146 " \"SomePropertyName\":" + subschema + | 159 " \"SomePropertyName\":" + subschema + |
147 " }" | 160 " }" |
148 "}"; | 161 "}"; |
149 } | 162 } |
150 | 163 |
151 } // namespace | 164 } // namespace |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
643 | 656 |
644 bundle.SetInteger("IntegerWithRange", 4); | 657 bundle.SetInteger("IntegerWithRange", 4); |
645 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); | 658 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
646 bundle.SetInteger("IntegerWithRange", 3); | 659 bundle.SetInteger("IntegerWithRange", 3); |
647 | 660 |
648 // Unknown top level property. | 661 // Unknown top level property. |
649 bundle.SetString("boom", "bang"); | 662 bundle.SetString("boom", "bang"); |
650 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); | 663 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
651 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); | 664 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
652 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN, true); | 665 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN, true); |
666 TestSchemaValidationWithPath(schema, bundle, ""); | |
653 bundle.Remove("boom", NULL); | 667 bundle.Remove("boom", NULL); |
654 | 668 |
655 // Invalid top level property. | 669 // Invalid top level property. |
656 bundle.SetInteger("Boolean", 12345); | 670 bundle.SetInteger("Boolean", 12345); |
657 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); | 671 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
658 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); | 672 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
659 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID, true); | 673 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID, true); |
674 TestSchemaValidationWithPath(schema, bundle, "Boolean"); | |
660 bundle.SetBoolean("Boolean", true); | 675 bundle.SetBoolean("Boolean", true); |
661 | 676 |
662 // Tests on ObjectOfObject. | 677 // Tests on ObjectOfObject. |
663 { | 678 { |
664 Schema subschema = schema.GetProperty("ObjectOfObject"); | 679 Schema subschema = schema.GetProperty("ObjectOfObject"); |
665 ASSERT_TRUE(subschema.valid()); | 680 ASSERT_TRUE(subschema.valid()); |
666 base::DictionaryValue root; | 681 base::DictionaryValue root; |
667 | 682 |
668 // Unknown property. | 683 // Unknown property. |
669 root.SetBoolean("Object.three", false); | 684 root.SetBoolean("Object.three", false); |
670 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); | 685 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
671 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); | 686 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
672 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); | 687 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
673 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); | 688 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
674 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); | 689 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
690 TestSchemaValidationWithPath(subschema, root, "Object"); | |
675 root.Remove("Object.three", NULL); | 691 root.Remove("Object.three", NULL); |
676 | 692 |
677 // Invalid property. | 693 // Invalid property. |
678 root.SetInteger("Object.one", 12345); | 694 root.SetInteger("Object.one", 12345); |
679 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); | 695 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
680 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); | 696 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
681 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); | 697 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
682 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); | 698 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
683 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); | 699 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
700 TestSchemaValidationWithPath(subschema, root, "Object.one"); | |
684 root.Remove("Object.one", NULL); | 701 root.Remove("Object.one", NULL); |
685 } | 702 } |
686 | 703 |
687 // Tests on ArrayOfObjects. | 704 // Tests on ArrayOfObjects. |
688 { | 705 { |
689 Schema subschema = schema.GetProperty("ArrayOfObjects"); | 706 Schema subschema = schema.GetProperty("ArrayOfObjects"); |
690 ASSERT_TRUE(subschema.valid()); | 707 ASSERT_TRUE(subschema.valid()); |
691 base::ListValue root; | 708 base::ListValue root; |
692 | 709 |
693 // Unknown property. | 710 // Unknown property. |
694 base::DictionaryValue* dict_value = new base::DictionaryValue(); | 711 base::DictionaryValue* dict_value = new base::DictionaryValue(); |
695 dict_value->SetBoolean("three", true); | 712 dict_value->SetBoolean("three", true); |
696 root.Append(dict_value); // Pass ownership to root. | 713 root.Append(dict_value); // Pass ownership to root. |
697 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); | 714 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
698 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); | 715 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
699 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); | 716 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
700 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); | 717 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
701 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); | 718 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
719 TestSchemaValidationWithPath(subschema, root, "items[0]"); | |
702 root.Remove(root.GetSize() - 1, NULL); | 720 root.Remove(root.GetSize() - 1, NULL); |
703 | 721 |
704 // Invalid property. | 722 // Invalid property. |
705 dict_value = new base::DictionaryValue(); | 723 dict_value = new base::DictionaryValue(); |
706 dict_value->SetBoolean("two", true); | 724 dict_value->SetBoolean("two", true); |
707 root.Append(dict_value); // Pass ownership to root. | 725 root.Append(dict_value); // Pass ownership to root. |
708 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); | 726 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
709 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); | 727 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
710 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); | 728 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
711 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); | 729 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
712 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); | 730 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
731 TestSchemaValidationWithPath(subschema, root, "items[0].two"); | |
713 root.Remove(root.GetSize() - 1, NULL); | 732 root.Remove(root.GetSize() - 1, NULL); |
714 } | 733 } |
715 | 734 |
716 // Tests on ObjectOfArray. | 735 // Tests on ObjectOfArray. |
717 { | 736 { |
718 Schema subschema = schema.GetProperty("ObjectOfArray"); | 737 Schema subschema = schema.GetProperty("ObjectOfArray"); |
719 ASSERT_TRUE(subschema.valid()); | 738 ASSERT_TRUE(subschema.valid()); |
720 base::DictionaryValue root; | 739 base::DictionaryValue root; |
721 | 740 |
722 base::ListValue* list_value = new base::ListValue(); | 741 base::ListValue* list_value = new base::ListValue(); |
723 root.Set("List", list_value); // Pass ownership to root. | 742 root.Set("List", list_value); // Pass ownership to root. |
724 | 743 |
725 // No errors. | 744 // Test that there are not errors here. |
726 list_value->Append(new base::FundamentalValue(12345)); | 745 list_value->Append(new base::FundamentalValue(12345)); |
727 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); | 746 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); |
728 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); | 747 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
729 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); | 748 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
730 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); | 749 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
731 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); | 750 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
732 | 751 |
Joao da Silva
2014/01/24 09:58:39
What I meant is that we should test that there are
binjin
2014/01/24 10:03:25
I think TestSchemaValidation(subschema, root, SCHE
Joao da Silva
2014/01/27 10:35:52
OK, sgtm.
| |
733 // Invalid list item. | 752 // Invalid list item. |
734 list_value->Append(new base::StringValue("blabla")); | 753 list_value->Append(new base::StringValue("blabla")); |
735 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); | 754 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
736 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); | 755 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
737 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); | 756 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
738 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); | 757 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
739 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); | 758 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
759 TestSchemaValidationWithPath(subschema, root, "List.items[1]"); | |
740 } | 760 } |
741 | 761 |
742 // Tests on ArrayOfObjectOfArray. | 762 // Tests on ArrayOfObjectOfArray. |
743 { | 763 { |
744 Schema subschema = schema.GetProperty("ArrayOfObjectOfArray"); | 764 Schema subschema = schema.GetProperty("ArrayOfObjectOfArray"); |
745 ASSERT_TRUE(subschema.valid()); | 765 ASSERT_TRUE(subschema.valid()); |
746 base::ListValue root; | 766 base::ListValue root; |
747 | 767 |
748 base::ListValue* list_value = new base::ListValue(); | 768 base::ListValue* list_value = new base::ListValue(); |
749 base::DictionaryValue* dict_value = new base::DictionaryValue(); | 769 base::DictionaryValue* dict_value = new base::DictionaryValue(); |
750 dict_value->Set("List", list_value); // Pass ownership to dict_value. | 770 dict_value->Set("List", list_value); // Pass ownership to dict_value. |
751 root.Append(dict_value); // Pass ownership to root. | 771 root.Append(dict_value); // Pass ownership to root. |
752 | 772 |
753 // No errors. | 773 // Test that there are not errors here. |
754 list_value->Append(new base::StringValue("blabla")); | 774 list_value->Append(new base::StringValue("blabla")); |
755 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); | 775 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); |
756 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); | 776 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
757 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); | 777 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
758 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); | 778 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
759 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); | 779 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
760 | 780 |
761 // Invalid list item. | 781 // Invalid list item. |
762 list_value->Append(new base::FundamentalValue(12345)); | 782 list_value->Append(new base::FundamentalValue(12345)); |
763 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); | 783 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
764 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); | 784 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
765 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); | 785 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
766 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); | 786 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
767 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); | 787 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
788 TestSchemaValidationWithPath(subschema, root, "items[0].List.items[1]"); | |
768 } | 789 } |
769 | 790 |
770 // Test that integer to double promotion is allowed. | 791 // Test that integer to double promotion is allowed. |
771 bundle.SetInteger("Number", 31415); | 792 bundle.SetInteger("Number", 31415); |
772 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); | 793 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); |
773 } | 794 } |
774 | 795 |
775 TEST(SchemaTest, InvalidReferences) { | 796 TEST(SchemaTest, InvalidReferences) { |
776 // References to undeclared schemas fail. | 797 // References to undeclared schemas fail. |
777 EXPECT_TRUE(ParseFails( | 798 EXPECT_TRUE(ParseFails( |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1024 | 1045 |
1025 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( | 1046 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( |
1026 "{" | 1047 "{" |
1027 " \"type\": \"integer\"," | 1048 " \"type\": \"integer\"," |
1028 " \"minimum\": 10," | 1049 " \"minimum\": 10," |
1029 " \"maximum\": 20" | 1050 " \"maximum\": 20" |
1030 "}"))); | 1051 "}"))); |
1031 } | 1052 } |
1032 | 1053 |
1033 } // namespace policy | 1054 } // namespace policy |
OLD | NEW |