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
3 // found in the LICENSE file.
5 #include "chrome/browser/policy/configuration_policy_provider_test.h"
8 #include "base/callback.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/values.h"
11 #include "chrome/browser/policy/configuration_policy_provider.h"
12 #include "chrome/browser/policy/external_data_fetcher.h"
13 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
14 #include "chrome/browser/policy/policy_bundle.h"
15 #include "chrome/browser/policy/policy_map.h"
16 #include "policy/policy_constants.h"
17 #include "testing/gmock/include/gmock/gmock.h"
19 using ::testing::Mock;
24 namespace test_policy_definitions {
26 const char kKeyString[] = "StringPolicy";
27 const char kKeyBoolean[] = "BooleanPolicy";
28 const char kKeyInteger[] = "IntegerPolicy";
29 const char kKeyStringList[] = "StringListPolicy";
30 const char kKeyDictionary[] = "DictionaryPolicy";
32 static const PolicyDefinitionList::Entry kEntries[] = {
33 { kKeyString, base::Value::TYPE_STRING },
34 { kKeyBoolean, base::Value::TYPE_BOOLEAN },
35 { kKeyInteger, base::Value::TYPE_INTEGER },
36 { kKeyStringList, base::Value::TYPE_LIST },
37 { kKeyDictionary, base::Value::TYPE_DICTIONARY },
40 const PolicyDefinitionList kList = {
41 kEntries, kEntries + arraysize(kEntries)
44 } // namespace test_policy_definitions
46 PolicyTestBase::PolicyTestBase() {}
48 PolicyTestBase::~PolicyTestBase() {}
50 void PolicyTestBase::TearDown() {
54 PolicyProviderTestHarness::PolicyProviderTestHarness(PolicyLevel level,
56 : level_(level), scope_(scope) {}
58 PolicyProviderTestHarness::~PolicyProviderTestHarness() {}
60 PolicyLevel PolicyProviderTestHarness::policy_level() const {
64 PolicyScope PolicyProviderTestHarness::policy_scope() const {
68 void PolicyProviderTestHarness::Install3rdPartyPolicy(
69 const base::DictionaryValue* policies) {
73 ConfigurationPolicyProviderTest::ConfigurationPolicyProviderTest() {}
75 ConfigurationPolicyProviderTest::~ConfigurationPolicyProviderTest() {}
77 void ConfigurationPolicyProviderTest::SetUp() {
78 PolicyTestBase::SetUp();
80 test_harness_.reset((*GetParam())());
81 test_harness_->SetUp();
83 provider_.reset(test_harness_->CreateProvider(
84 loop_.message_loop_proxy(), &test_policy_definitions::kList));
86 // Some providers do a reload on init. Make sure any notifications generated
90 const PolicyBundle kEmptyBundle;
91 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle));
94 void ConfigurationPolicyProviderTest::TearDown() {
95 // Give providers the chance to clean up after themselves on the file thread.
96 provider_->Shutdown();
99 PolicyTestBase::TearDown();
102 void ConfigurationPolicyProviderTest::CheckValue(
103 const char* policy_name,
104 const base::Value& expected_value,
105 base::Closure install_value) {
106 // Install the value, reload policy and check the provider for the value.
108 provider_->RefreshPolicies();
109 loop_.RunUntilIdle();
110 PolicyBundle expected_bundle;
111 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
113 test_harness_->policy_level(),
114 test_harness_->policy_scope(),
115 expected_value.DeepCopy(),
117 EXPECT_TRUE(provider_->policies().Equals(expected_bundle));
118 // TODO(joaodasilva): set the policy in the POLICY_DOMAIN_EXTENSIONS too,
119 // and extend the |expected_bundle|, once all providers are ready.
122 TEST_P(ConfigurationPolicyProviderTest, Empty) {
123 provider_->RefreshPolicies();
124 loop_.RunUntilIdle();
125 const PolicyBundle kEmptyBundle;
126 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle));
129 TEST_P(ConfigurationPolicyProviderTest, StringValue) {
130 const char kTestString[] = "string_value";
131 base::StringValue expected_value(kTestString);
132 CheckValue(test_policy_definitions::kKeyString,
134 base::Bind(&PolicyProviderTestHarness::InstallStringPolicy,
135 base::Unretained(test_harness_.get()),
136 test_policy_definitions::kKeyString,
140 TEST_P(ConfigurationPolicyProviderTest, BooleanValue) {
141 base::FundamentalValue expected_value(true);
142 CheckValue(test_policy_definitions::kKeyBoolean,
144 base::Bind(&PolicyProviderTestHarness::InstallBooleanPolicy,
145 base::Unretained(test_harness_.get()),
146 test_policy_definitions::kKeyBoolean,
150 TEST_P(ConfigurationPolicyProviderTest, IntegerValue) {
151 base::FundamentalValue expected_value(42);
152 CheckValue(test_policy_definitions::kKeyInteger,
154 base::Bind(&PolicyProviderTestHarness::InstallIntegerPolicy,
155 base::Unretained(test_harness_.get()),
156 test_policy_definitions::kKeyInteger,
160 TEST_P(ConfigurationPolicyProviderTest, StringListValue) {
161 base::ListValue expected_value;
162 expected_value.Set(0U, base::Value::CreateStringValue("first"));
163 expected_value.Set(1U, base::Value::CreateStringValue("second"));
164 CheckValue(test_policy_definitions::kKeyStringList,
166 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy,
167 base::Unretained(test_harness_.get()),
168 test_policy_definitions::kKeyStringList,
172 TEST_P(ConfigurationPolicyProviderTest, DictionaryValue) {
173 base::DictionaryValue expected_value;
174 expected_value.SetBoolean("bool", true);
175 expected_value.SetInteger("int", 123);
176 expected_value.SetString("str", "omg");
178 base::ListValue* list = new base::ListValue();
179 list->Set(0U, base::Value::CreateStringValue("first"));
180 list->Set(1U, base::Value::CreateStringValue("second"));
181 expected_value.Set("list", list);
183 base::DictionaryValue* dict = new base::DictionaryValue();
184 dict->SetString("sub", "value");
185 list = new base::ListValue();
186 base::DictionaryValue* sub = new base::DictionaryValue();
187 sub->SetInteger("aaa", 111);
188 sub->SetInteger("bbb", 222);
190 sub = new base::DictionaryValue();
191 sub->SetString("ccc", "333");
192 sub->SetString("ddd", "444");
194 dict->Set("sublist", list);
195 expected_value.Set("dict", dict);
197 CheckValue(test_policy_definitions::kKeyDictionary,
199 base::Bind(&PolicyProviderTestHarness::InstallDictionaryPolicy,
200 base::Unretained(test_harness_.get()),
201 test_policy_definitions::kKeyDictionary,
205 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) {
207 EXPECT_TRUE(provider_->policies().Equals(bundle));
209 // OnUpdatePolicy is called even when there are no changes.
210 MockConfigurationPolicyObserver observer;
211 provider_->AddObserver(&observer);
212 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1);
213 provider_->RefreshPolicies();
214 loop_.RunUntilIdle();
215 Mock::VerifyAndClearExpectations(&observer);
217 EXPECT_TRUE(provider_->policies().Equals(bundle));
219 // OnUpdatePolicy is called when there are changes.
220 test_harness_->InstallStringPolicy(test_policy_definitions::kKeyString,
222 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1);
223 provider_->RefreshPolicies();
224 loop_.RunUntilIdle();
225 Mock::VerifyAndClearExpectations(&observer);
227 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
228 .Set(test_policy_definitions::kKeyString,
229 test_harness_->policy_level(),
230 test_harness_->policy_scope(),
231 base::Value::CreateStringValue("value"),
233 EXPECT_TRUE(provider_->policies().Equals(bundle));
234 provider_->RemoveObserver(&observer);
237 TEST(ConfigurationPolicyProviderTest, FixDeprecatedPolicies) {
238 PolicyMap policy_map;
239 policy_map.Set(key::kProxyServerMode,
240 POLICY_LEVEL_MANDATORY,
242 base::Value::CreateIntegerValue(3),
245 // Both these policies should be ignored, since there's a higher priority
247 policy_map.Set(key::kProxyMode,
248 POLICY_LEVEL_RECOMMENDED,
250 base::Value::CreateStringValue("pac_script"),
252 policy_map.Set(key::kProxyPacUrl,
253 POLICY_LEVEL_RECOMMENDED,
255 base::Value::CreateStringValue("http://example.com/wpad.dat"),
258 MockConfigurationPolicyProvider provider;
260 provider.UpdateChromePolicy(policy_map);
262 PolicyBundle expected_bundle;
263 base::DictionaryValue* expected_value = new base::DictionaryValue();
264 expected_value->SetInteger(key::kProxyServerMode, 3);
265 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
266 .Set(key::kProxySettings,
267 POLICY_LEVEL_MANDATORY,
271 EXPECT_TRUE(provider.policies().Equals(expected_bundle));
275 Configuration3rdPartyPolicyProviderTest::
276 Configuration3rdPartyPolicyProviderTest() {}
278 Configuration3rdPartyPolicyProviderTest::
279 ~Configuration3rdPartyPolicyProviderTest() {}
281 TEST_P(Configuration3rdPartyPolicyProviderTest, Load3rdParty) {
282 base::DictionaryValue policy_dict;
283 policy_dict.SetBoolean("bool", true);
284 policy_dict.SetDouble("double", 123.456);
285 policy_dict.SetInteger("int", 789);
286 policy_dict.SetString("str", "string value");
288 base::ListValue* list = new base::ListValue();
289 for (int i = 0; i < 2; ++i) {
290 base::DictionaryValue* dict = new base::DictionaryValue();
291 dict->SetInteger("subdictindex", i);
292 dict->Set("subdict", policy_dict.DeepCopy());
295 policy_dict.Set("list", list);
296 policy_dict.Set("dict", policy_dict.DeepCopy());
298 // Install these policies as a Chrome policy.
299 test_harness_->InstallDictionaryPolicy(
300 test_policy_definitions::kKeyDictionary, &policy_dict);
301 // Install them as 3rd party policies too.
302 base::DictionaryValue policy_3rdparty;
303 policy_3rdparty.Set("extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
304 policy_dict.DeepCopy());
305 policy_3rdparty.Set("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
306 policy_dict.DeepCopy());
307 // Install invalid 3rd party policies that shouldn't be loaded. These also
308 // help detecting memory leaks in the code paths that detect invalid input.
309 policy_3rdparty.Set("invalid-domain.component", policy_dict.DeepCopy());
310 policy_3rdparty.Set("extensions.cccccccccccccccccccccccccccccccc",
311 base::Value::CreateStringValue("invalid-value"));
312 test_harness_->Install3rdPartyPolicy(&policy_3rdparty);
314 provider_->RefreshPolicies();
315 loop_.RunUntilIdle();
317 PolicyMap expected_policy;
318 expected_policy.Set(test_policy_definitions::kKeyDictionary,
319 test_harness_->policy_level(),
320 test_harness_->policy_scope(),
321 policy_dict.DeepCopy(),
323 PolicyBundle expected_bundle;
324 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
325 .CopyFrom(expected_policy);
326 expected_policy.Clear();
327 expected_policy.LoadFrom(&policy_dict,
328 test_harness_->policy_level(),
329 test_harness_->policy_scope());
330 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS,
331 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
332 .CopyFrom(expected_policy);
333 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS,
334 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))
335 .CopyFrom(expected_policy);
336 EXPECT_TRUE(provider_->policies().Equals(expected_bundle));
339 } // namespace policy