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
3 // found in the LICENSE file.
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "build/build_config.h"
11 #include "components/policy/core/common/policy_details.h"
12 #include "components/policy/core/common/schema.h"
13 #include "policy/policy_constants.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 // This unittest tests the code generated by
17 // chrome/tools/build/generate_policy_source.py.
23 #if defined(OS_CHROMEOS)
24 // Checks if two schemas are the same or not. Note that this function doesn't
25 // consider restrictions on integers and strings nor pattern properties.
26 bool IsSameSchema(Schema a
, Schema b
) {
27 if (a
.valid() != b
.valid())
31 if (a
.type() != b
.type())
33 if (a
.type() == base::Value::TYPE_LIST
)
34 return IsSameSchema(a
.GetItems(), b
.GetItems());
35 if (a
.type() != base::Value::TYPE_DICTIONARY
)
37 Schema::Iterator a_it
= a
.GetPropertiesIterator();
38 Schema::Iterator b_it
= b
.GetPropertiesIterator();
39 while (!a_it
.IsAtEnd()) {
42 if (strcmp(a_it
.key(), b_it
.key()) != 0)
44 if (!IsSameSchema(a_it
.schema(), b_it
.schema()))
51 return IsSameSchema(a
.GetAdditionalProperties(), b
.GetAdditionalProperties());
57 TEST(GeneratePolicySource
, ChromeSchemaData
) {
58 Schema schema
= Schema::Wrap(GetChromeSchemaData());
59 ASSERT_TRUE(schema
.valid());
60 EXPECT_EQ(base::Value::TYPE_DICTIONARY
, schema
.type());
62 Schema subschema
= schema
.GetAdditionalProperties();
63 EXPECT_FALSE(subschema
.valid());
65 subschema
= schema
.GetProperty("no such policy exists");
66 EXPECT_FALSE(subschema
.valid());
68 subschema
= schema
.GetProperty(key::kSearchSuggestEnabled
);
69 ASSERT_TRUE(subschema
.valid());
70 EXPECT_EQ(base::Value::TYPE_BOOLEAN
, subschema
.type());
72 subschema
= schema
.GetProperty(key::kDefaultCookiesSetting
);
73 ASSERT_TRUE(subschema
.valid());
74 EXPECT_EQ(base::Value::TYPE_INTEGER
, subschema
.type());
76 subschema
= schema
.GetProperty(key::kProxyMode
);
77 ASSERT_TRUE(subschema
.valid());
78 EXPECT_EQ(base::Value::TYPE_STRING
, subschema
.type());
80 subschema
= schema
.GetProperty(key::kURLBlacklist
);
81 ASSERT_TRUE(subschema
.valid());
82 EXPECT_EQ(base::Value::TYPE_LIST
, subschema
.type());
83 ASSERT_TRUE(subschema
.GetItems().valid());
84 EXPECT_EQ(base::Value::TYPE_STRING
, subschema
.GetItems().type());
86 #if !defined(OS_ANDROID) && !defined(OS_IOS)
87 subschema
= schema
.GetProperty(key::kExtensionSettings
);
88 ASSERT_TRUE(subschema
.valid());
89 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, subschema
.type());
90 EXPECT_FALSE(subschema
.GetAdditionalProperties().valid());
91 EXPECT_FALSE(subschema
.GetProperty("no such extension id exists").valid());
92 EXPECT_TRUE(subschema
.GetPatternProperties("*").empty());
93 EXPECT_TRUE(subschema
.GetPatternProperties("no such extension id").empty());
94 EXPECT_TRUE(subschema
.GetPatternProperties("^[a-p]{32}$").empty());
95 EXPECT_TRUE(subschema
.GetPatternProperties(
96 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").empty());
97 EXPECT_TRUE(subschema
.GetPatternProperties(
98 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").empty());
99 SchemaList schema_list
=
100 subschema
.GetPatternProperties("abcdefghijklmnopabcdefghijklmnop");
101 ASSERT_EQ(1u, schema_list
.size());
102 subschema
= schema_list
[0];
103 ASSERT_TRUE(subschema
.valid());
104 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, subschema
.type());
105 subschema
= subschema
.GetProperty("installation_mode");
106 ASSERT_TRUE(subschema
.valid());
107 ASSERT_EQ(base::Value::TYPE_STRING
, subschema
.type());
109 subschema
= schema
.GetProperty(key::kExtensionSettings
).GetProperty("*");
110 ASSERT_TRUE(subschema
.valid());
111 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, subschema
.type());
112 subschema
= subschema
.GetProperty("installation_mode");
113 ASSERT_TRUE(subschema
.valid());
114 ASSERT_EQ(base::Value::TYPE_STRING
, subschema
.type());
117 subschema
= schema
.GetProperty(key::kProxySettings
);
118 ASSERT_TRUE(subschema
.valid());
119 EXPECT_EQ(base::Value::TYPE_DICTIONARY
, subschema
.type());
120 EXPECT_FALSE(subschema
.GetAdditionalProperties().valid());
121 EXPECT_FALSE(subschema
.GetProperty("no such proxy key exists").valid());
122 ASSERT_TRUE(subschema
.GetProperty(key::kProxyMode
).valid());
123 ASSERT_TRUE(subschema
.GetProperty(key::kProxyServer
).valid());
124 ASSERT_TRUE(subschema
.GetProperty(key::kProxyServerMode
).valid());
125 ASSERT_TRUE(subschema
.GetProperty(key::kProxyPacUrl
).valid());
126 ASSERT_TRUE(subschema
.GetProperty(key::kProxyBypassList
).valid());
128 // Verify that all the Chrome policies are there.
129 for (Schema::Iterator it
= schema
.GetPropertiesIterator();
130 !it
.IsAtEnd(); it
.Advance()) {
131 EXPECT_TRUE(it
.key());
132 EXPECT_FALSE(std::string(it
.key()).empty());
133 EXPECT_TRUE(GetChromePolicyDetails(it
.key()));
136 // The properties are iterated in order.
137 const char* kExpectedProperties
[] = {
138 key::kProxyBypassList
,
142 key::kProxyServerMode
,
145 const char** next
= kExpectedProperties
;
146 for (Schema::Iterator
it(subschema
.GetPropertiesIterator());
147 !it
.IsAtEnd(); it
.Advance(), ++next
) {
148 ASSERT_TRUE(*next
!= NULL
);
149 EXPECT_STREQ(*next
, it
.key());
150 ASSERT_TRUE(it
.schema().valid());
151 EXPECT_EQ(base::Value::TYPE_STRING
, it
.schema().type());
153 EXPECT_TRUE(*next
== NULL
);
155 #if defined(OS_CHROMEOS)
156 subschema
= schema
.GetKnownProperty(key::kPowerManagementIdleSettings
);
157 ASSERT_TRUE(subschema
.valid());
159 EXPECT_TRUE(IsSameSchema(subschema
.GetKnownProperty("AC"),
160 subschema
.GetKnownProperty("Battery")));
162 subschema
= schema
.GetKnownProperty(key::kDeviceLoginScreenPowerManagement
);
163 ASSERT_TRUE(subschema
.valid());
165 EXPECT_TRUE(IsSameSchema(subschema
.GetKnownProperty("AC"),
166 subschema
.GetKnownProperty("Battery")));
170 TEST(GeneratePolicySource
, PolicyDetails
) {
171 EXPECT_FALSE(GetChromePolicyDetails(""));
172 EXPECT_FALSE(GetChromePolicyDetails("no such policy"));
173 EXPECT_FALSE(GetChromePolicyDetails("SearchSuggestEnable"));
174 EXPECT_FALSE(GetChromePolicyDetails("searchSuggestEnabled"));
175 EXPECT_FALSE(GetChromePolicyDetails("SSearchSuggestEnabled"));
177 const PolicyDetails
* details
=
178 GetChromePolicyDetails(key::kSearchSuggestEnabled
);
179 ASSERT_TRUE(details
);
180 EXPECT_FALSE(details
->is_deprecated
);
181 EXPECT_FALSE(details
->is_device_policy
);
182 EXPECT_EQ(6, details
->id
);
183 EXPECT_EQ(0u, details
->max_external_data_size
);
186 details
= GetChromePolicyDetails(key::kJavascriptEnabled
);
187 ASSERT_TRUE(details
);
188 EXPECT_TRUE(details
->is_deprecated
);
189 EXPECT_FALSE(details
->is_device_policy
);
190 EXPECT_EQ(9, details
->id
);
191 EXPECT_EQ(0u, details
->max_external_data_size
);
194 #if defined(OS_CHROMEOS)
195 details
= GetChromePolicyDetails(key::kDevicePolicyRefreshRate
);
196 ASSERT_TRUE(details
);
197 EXPECT_FALSE(details
->is_deprecated
);
198 EXPECT_TRUE(details
->is_device_policy
);
199 EXPECT_EQ(90, details
->id
);
200 EXPECT_EQ(0u, details
->max_external_data_size
);
203 // TODO(bartfab): add a test that verifies a max_external_data_size larger
204 // than 0, once a type 'external' policy is added.
207 #if defined(OS_CHROMEOS)
208 TEST(GeneratePolicySource
, SetEnterpriseDefaults
) {
209 PolicyMap policy_map
;
211 // If policy not configured yet, set the enterprise default.
212 SetEnterpriseUsersDefaults(&policy_map
);
214 const base::Value
* multiprof_behavior
=
215 policy_map
.GetValue(key::kChromeOsMultiProfileUserBehavior
);
216 base::StringValue
expected("primary-only");
217 EXPECT_TRUE(expected
.Equals(multiprof_behavior
));
219 // If policy already configured, it's not changed to enterprise defaults.
220 policy_map
.Set(key::kChromeOsMultiProfileUserBehavior
,
221 POLICY_LEVEL_MANDATORY
,
224 new base::StringValue("test_value"),
226 SetEnterpriseUsersDefaults(&policy_map
);
228 policy_map
.GetValue(key::kChromeOsMultiProfileUserBehavior
);
229 expected
= base::StringValue("test_value");
230 EXPECT_TRUE(expected
.Equals(multiprof_behavior
));
234 } // namespace policy