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 "components/policy/core/common/schema_internal.h" | 7 #include "components/policy/core/common/schema_internal.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace policy { | 10 namespace policy { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 | 54 |
55 bool ParseFails(const std::string& content) { | 55 bool ParseFails(const std::string& content) { |
56 std::string error; | 56 std::string error; |
57 Schema schema = Schema::Parse(content, &error); | 57 Schema schema = Schema::Parse(content, &error); |
58 if (schema.valid()) | 58 if (schema.valid()) |
59 return false; | 59 return false; |
60 EXPECT_FALSE(error.empty()); | 60 EXPECT_FALSE(error.empty()); |
61 return true; | 61 return true; |
62 } | 62 } |
63 | 63 |
64 std::string schemaObjectWrapper(const std::string &subschema) { | |
65 return "{" | |
66 " \"type\": \"object\"," | |
67 " \"properties\": {" | |
68 " \"SomePropertyName\":" + subschema + | |
69 " }" | |
70 "}"; | |
71 } | |
72 | |
64 } // namespace | 73 } // namespace |
65 | 74 |
66 TEST(SchemaTest, MinimalSchema) { | 75 TEST(SchemaTest, MinimalSchema) { |
67 EXPECT_FALSE(ParseFails("{ \"type\": \"object\" }")); | 76 EXPECT_FALSE(ParseFails("{ \"type\": \"object\" }")); |
68 } | 77 } |
69 | 78 |
70 TEST(SchemaTest, InvalidSchemas) { | 79 TEST(SchemaTest, InvalidSchemas) { |
71 EXPECT_TRUE(ParseFails("")); | 80 EXPECT_TRUE(ParseFails("")); |
72 EXPECT_TRUE(ParseFails("omg")); | 81 EXPECT_TRUE(ParseFails("omg")); |
73 EXPECT_TRUE(ParseFails("\"omg\"")); | 82 EXPECT_TRUE(ParseFails("\"omg\"")); |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 { "abab", base::Value::TYPE_STRING }, | 316 { "abab", base::Value::TYPE_STRING }, |
308 { "bb", base::Value::TYPE_NULL }, | 317 { "bb", base::Value::TYPE_NULL }, |
309 }; | 318 }; |
310 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedKeys); ++i) { | 319 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedKeys); ++i) { |
311 Schema sub = schema.GetKnownProperty(kExpectedKeys[i].expected_key); | 320 Schema sub = schema.GetKnownProperty(kExpectedKeys[i].expected_key); |
312 ASSERT_TRUE(sub.valid()); | 321 ASSERT_TRUE(sub.valid()); |
313 EXPECT_EQ(kExpectedKeys[i].expected_type, sub.type()); | 322 EXPECT_EQ(kExpectedKeys[i].expected_type, sub.type()); |
314 } | 323 } |
315 } | 324 } |
316 | 325 |
317 TEST(SchemaTest, Wrap) { | 326 TEST(SchemaTest, Wrap) { |
Joao da Silva
2014/01/16 10:56:34
Add enums and ranges to this test too
binjin
2014/01/17 14:29:01
Done.
| |
318 const internal::SchemaNode kSchemas[] = { | 327 const internal::SchemaNode kSchemas[] = { |
319 { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node | 328 { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node |
320 { base::Value::TYPE_BOOLEAN, -1 }, // 1 | 329 { base::Value::TYPE_BOOLEAN, -1 }, // 1 |
321 { base::Value::TYPE_INTEGER, -1 }, // 2 | 330 { base::Value::TYPE_INTEGER, -1 }, // 2 |
322 { base::Value::TYPE_DOUBLE, -1 }, // 3 | 331 { base::Value::TYPE_DOUBLE, -1 }, // 3 |
323 { base::Value::TYPE_STRING, -1 }, // 4 | 332 { base::Value::TYPE_STRING, -1 }, // 4 |
324 { base::Value::TYPE_LIST, 4 }, // 5: list of strings. | 333 { base::Value::TYPE_LIST, 4 }, // 5: list of strings. |
325 { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings. | 334 { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings. |
326 }; | 335 }; |
327 | 336 |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
675 | 684 |
676 Schema list = schema.GetKnownProperty("list"); | 685 Schema list = schema.GetKnownProperty("list"); |
677 ASSERT_TRUE(list.valid()); | 686 ASSERT_TRUE(list.valid()); |
678 ASSERT_EQ(base::Value::TYPE_LIST, list.type()); | 687 ASSERT_EQ(base::Value::TYPE_LIST, list.type()); |
679 | 688 |
680 Schema items = list.GetItems(); | 689 Schema items = list.GetItems(); |
681 ASSERT_TRUE(items.valid()); | 690 ASSERT_TRUE(items.valid()); |
682 ASSERT_EQ(base::Value::TYPE_BOOLEAN, items.type()); | 691 ASSERT_EQ(base::Value::TYPE_BOOLEAN, items.type()); |
683 } | 692 } |
684 | 693 |
694 TEST(SchemaTest, EnumerationRestriction) { | |
695 // Enum attribute is a list. | |
696 EXPECT_TRUE(ParseFails(schemaObjectWrapper( | |
697 "{" | |
698 " \"type\": \"string\"," | |
699 " \"enum\": 12" | |
700 "}"))); | |
701 | |
702 // Empty enum attributes is not allowed. | |
703 EXPECT_TRUE(ParseFails(schemaObjectWrapper( | |
704 "{" | |
705 " \"type\": \"integer\"," | |
706 " \"enum\": []" | |
707 "}"))); | |
708 | |
709 // Enum elements type should be same as stated. | |
710 EXPECT_TRUE(ParseFails(schemaObjectWrapper( | |
711 "{" | |
712 " \"type\": \"string\"," | |
713 " \"enum\": [1, 2, 3]" | |
714 "}"))); | |
715 | |
716 EXPECT_FALSE(ParseFails(schemaObjectWrapper( | |
717 "{" | |
718 " \"type\": \"integer\"," | |
719 " \"enum\": [1, 2, 3]" | |
720 "}"))); | |
721 | |
722 EXPECT_FALSE(ParseFails(schemaObjectWrapper( | |
723 "{" | |
724 " \"type\": \"string\"," | |
725 " \"enum\": [\"1\", \"2\", \"3\"]" | |
726 "}"))); | |
727 } | |
728 | |
729 TEST(SchemaTest, RangedRestriction) { | |
730 EXPECT_TRUE(ParseFails(schemaObjectWrapper( | |
731 "{" | |
732 " \"type\": \"integer\"," | |
733 " \"minimum\": 10," | |
734 " \"maximum\": 5" | |
735 "}"))); | |
736 | |
737 EXPECT_FALSE(ParseFails(schemaObjectWrapper( | |
738 "{" | |
739 " \"type\": \"integer\"," | |
740 " \"minimum\": 10," | |
741 " \"maximum\": 20" | |
742 "}"))); | |
743 } | |
685 | 744 |
686 } // namespace policy | 745 } // namespace policy |
OLD | NEW |