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 <cstring> |
5 #include <string> | 6 #include <string> |
6 | 7 |
7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
8 #include "base/values.h" | 9 #include "base/values.h" |
9 #include "build/build_config.h" | 10 #include "build/build_config.h" |
10 #include "components/policy/core/common/policy_details.h" | 11 #include "components/policy/core/common/policy_details.h" |
11 #include "components/policy/core/common/schema.h" | 12 #include "components/policy/core/common/schema.h" |
12 #include "policy/policy_constants.h" | 13 #include "policy/policy_constants.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 // This unittest tests the code generated by | 16 // This unittest tests the code generated by |
16 // chrome/tools/build/generate_policy_source.py. | 17 // chrome/tools/build/generate_policy_source.py. |
17 | 18 |
18 namespace policy { | 19 namespace policy { |
19 | 20 |
| 21 namespace { |
| 22 |
| 23 // Checks if two schemas are the same or not. Note that this function doesn't |
| 24 // consider restrictions on integers and strings nor pattern properties. |
| 25 bool IsSameSchema(Schema a, Schema b) { |
| 26 if (a.valid() != b.valid()) |
| 27 return false; |
| 28 if (!a.valid()) |
| 29 return true; |
| 30 if (a.type() != b.type()) |
| 31 return false; |
| 32 if (a.type() == base::Value::TYPE_LIST) |
| 33 return IsSameSchema(a.GetItems(), b.GetItems()); |
| 34 if (a.type() != base::Value::TYPE_DICTIONARY) |
| 35 return true; |
| 36 Schema::Iterator a_it = a.GetPropertiesIterator(); |
| 37 Schema::Iterator b_it = b.GetPropertiesIterator(); |
| 38 while (!a_it.IsAtEnd()) { |
| 39 if (b_it.IsAtEnd()) |
| 40 return false; |
| 41 if (strcmp(a_it.key(), b_it.key()) != 0) |
| 42 return false; |
| 43 if (!IsSameSchema(a_it.schema(), b_it.schema())) |
| 44 return false; |
| 45 a_it.Advance(); |
| 46 b_it.Advance(); |
| 47 } |
| 48 if (!b_it.IsAtEnd()) |
| 49 return false; |
| 50 return IsSameSchema(a.GetAdditionalProperties(), b.GetAdditionalProperties()); |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
20 TEST(GeneratePolicySource, ChromeSchemaData) { | 55 TEST(GeneratePolicySource, ChromeSchemaData) { |
21 Schema schema = Schema::Wrap(GetChromeSchemaData()); | 56 Schema schema = Schema::Wrap(GetChromeSchemaData()); |
22 ASSERT_TRUE(schema.valid()); | 57 ASSERT_TRUE(schema.valid()); |
23 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 58 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); |
24 | 59 |
25 Schema subschema = schema.GetAdditionalProperties(); | 60 Schema subschema = schema.GetAdditionalProperties(); |
26 EXPECT_FALSE(subschema.valid()); | 61 EXPECT_FALSE(subschema.valid()); |
27 | 62 |
28 subschema = schema.GetProperty("no such policy exists"); | 63 subschema = schema.GetProperty("no such policy exists"); |
29 EXPECT_FALSE(subschema.valid()); | 64 EXPECT_FALSE(subschema.valid()); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 }; | 111 }; |
77 const char** next = kExpectedProperties; | 112 const char** next = kExpectedProperties; |
78 for (Schema::Iterator it(subschema.GetPropertiesIterator()); | 113 for (Schema::Iterator it(subschema.GetPropertiesIterator()); |
79 !it.IsAtEnd(); it.Advance(), ++next) { | 114 !it.IsAtEnd(); it.Advance(), ++next) { |
80 ASSERT_TRUE(*next != NULL); | 115 ASSERT_TRUE(*next != NULL); |
81 EXPECT_STREQ(*next, it.key()); | 116 EXPECT_STREQ(*next, it.key()); |
82 ASSERT_TRUE(it.schema().valid()); | 117 ASSERT_TRUE(it.schema().valid()); |
83 EXPECT_EQ(base::Value::TYPE_STRING, it.schema().type()); | 118 EXPECT_EQ(base::Value::TYPE_STRING, it.schema().type()); |
84 } | 119 } |
85 EXPECT_TRUE(*next == NULL); | 120 EXPECT_TRUE(*next == NULL); |
| 121 |
| 122 #if defined(OS_CHROMEOS) |
| 123 subschema = schema.GetKnownProperty(key::kPowerManagementIdleSettings); |
| 124 ASSERT_TRUE(subschema.valid()); |
| 125 |
| 126 EXPECT_TRUE(IsSameSchema(subschema.GetKnownProperty("AC"), |
| 127 subschema.GetKnownProperty("Battery"))); |
| 128 |
| 129 subschema = schema.GetKnownProperty(key::kDeviceLoginScreenPowerManagement); |
| 130 ASSERT_TRUE(subschema.valid()); |
| 131 |
| 132 EXPECT_TRUE(IsSameSchema(subschema.GetKnownProperty("AC"), |
| 133 subschema.GetKnownProperty("Battery"))); |
| 134 #endif |
86 } | 135 } |
87 | 136 |
88 TEST(GeneratePolicySource, PolicyDetails) { | 137 TEST(GeneratePolicySource, PolicyDetails) { |
89 EXPECT_FALSE(GetChromePolicyDetails("")); | 138 EXPECT_FALSE(GetChromePolicyDetails("")); |
90 EXPECT_FALSE(GetChromePolicyDetails("no such policy")); | 139 EXPECT_FALSE(GetChromePolicyDetails("no such policy")); |
91 EXPECT_FALSE(GetChromePolicyDetails("SearchSuggestEnable")); | 140 EXPECT_FALSE(GetChromePolicyDetails("SearchSuggestEnable")); |
92 EXPECT_FALSE(GetChromePolicyDetails("searchSuggestEnabled")); | 141 EXPECT_FALSE(GetChromePolicyDetails("searchSuggestEnabled")); |
93 EXPECT_FALSE(GetChromePolicyDetails("SSearchSuggestEnabled")); | 142 EXPECT_FALSE(GetChromePolicyDetails("SSearchSuggestEnabled")); |
94 | 143 |
95 const PolicyDetails* details = | 144 const PolicyDetails* details = |
(...skipping 20 matching lines...) Expand all Loading... |
116 EXPECT_TRUE(details->is_device_policy); | 165 EXPECT_TRUE(details->is_device_policy); |
117 EXPECT_EQ(90, details->id); | 166 EXPECT_EQ(90, details->id); |
118 EXPECT_EQ(0u, details->max_external_data_size); | 167 EXPECT_EQ(0u, details->max_external_data_size); |
119 #endif | 168 #endif |
120 | 169 |
121 // TODO(bartfab): add a test that verifies a max_external_data_size larger | 170 // TODO(bartfab): add a test that verifies a max_external_data_size larger |
122 // than 0, once a type 'external' policy is added. | 171 // than 0, once a type 'external' policy is added. |
123 } | 172 } |
124 | 173 |
125 } // namespace policy | 174 } // namespace policy |
OLD | NEW |