- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / configuration_policy_provider_test.cc
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.
4
5 #include "chrome/browser/policy/configuration_policy_provider_test.h"
6
7 #include "base/bind.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"
18
19 using ::testing::Mock;
20 using ::testing::_;
21
22 namespace policy {
23
24 namespace test_policy_definitions {
25
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";
31
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 },
38 };
39
40 const PolicyDefinitionList kList = {
41   kEntries, kEntries + arraysize(kEntries)
42 };
43
44 }  // namespace test_policy_definitions
45
46 PolicyTestBase::PolicyTestBase() {}
47
48 PolicyTestBase::~PolicyTestBase() {}
49
50 void PolicyTestBase::TearDown() {
51   loop_.RunUntilIdle();
52 }
53
54 PolicyProviderTestHarness::PolicyProviderTestHarness(PolicyLevel level,
55                                                      PolicyScope scope)
56     : level_(level), scope_(scope) {}
57
58 PolicyProviderTestHarness::~PolicyProviderTestHarness() {}
59
60 PolicyLevel PolicyProviderTestHarness::policy_level() const {
61   return level_;
62 }
63
64 PolicyScope PolicyProviderTestHarness::policy_scope() const {
65   return scope_;
66 }
67
68 void PolicyProviderTestHarness::Install3rdPartyPolicy(
69     const base::DictionaryValue* policies) {
70   FAIL();
71 }
72
73 ConfigurationPolicyProviderTest::ConfigurationPolicyProviderTest() {}
74
75 ConfigurationPolicyProviderTest::~ConfigurationPolicyProviderTest() {}
76
77 void ConfigurationPolicyProviderTest::SetUp() {
78   PolicyTestBase::SetUp();
79
80   test_harness_.reset((*GetParam())());
81   test_harness_->SetUp();
82
83   provider_.reset(test_harness_->CreateProvider(
84       loop_.message_loop_proxy(), &test_policy_definitions::kList));
85   provider_->Init();
86   // Some providers do a reload on init. Make sure any notifications generated
87   // are fired now.
88   loop_.RunUntilIdle();
89
90   const PolicyBundle kEmptyBundle;
91   EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle));
92 }
93
94 void ConfigurationPolicyProviderTest::TearDown() {
95   // Give providers the chance to clean up after themselves on the file thread.
96   provider_->Shutdown();
97   provider_.reset();
98
99   PolicyTestBase::TearDown();
100 }
101
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.
107   install_value.Run();
108   provider_->RefreshPolicies();
109   loop_.RunUntilIdle();
110   PolicyBundle expected_bundle;
111   expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
112       .Set(policy_name,
113            test_harness_->policy_level(),
114            test_harness_->policy_scope(),
115            expected_value.DeepCopy(),
116            NULL);
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.
120 }
121
122 TEST_P(ConfigurationPolicyProviderTest, Empty) {
123   provider_->RefreshPolicies();
124   loop_.RunUntilIdle();
125   const PolicyBundle kEmptyBundle;
126   EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle));
127 }
128
129 TEST_P(ConfigurationPolicyProviderTest, StringValue) {
130   const char kTestString[] = "string_value";
131   base::StringValue expected_value(kTestString);
132   CheckValue(test_policy_definitions::kKeyString,
133              expected_value,
134              base::Bind(&PolicyProviderTestHarness::InstallStringPolicy,
135                         base::Unretained(test_harness_.get()),
136                         test_policy_definitions::kKeyString,
137                         kTestString));
138 }
139
140 TEST_P(ConfigurationPolicyProviderTest, BooleanValue) {
141   base::FundamentalValue expected_value(true);
142   CheckValue(test_policy_definitions::kKeyBoolean,
143              expected_value,
144              base::Bind(&PolicyProviderTestHarness::InstallBooleanPolicy,
145                         base::Unretained(test_harness_.get()),
146                         test_policy_definitions::kKeyBoolean,
147                         true));
148 }
149
150 TEST_P(ConfigurationPolicyProviderTest, IntegerValue) {
151   base::FundamentalValue expected_value(42);
152   CheckValue(test_policy_definitions::kKeyInteger,
153              expected_value,
154              base::Bind(&PolicyProviderTestHarness::InstallIntegerPolicy,
155                         base::Unretained(test_harness_.get()),
156                         test_policy_definitions::kKeyInteger,
157                         42));
158 }
159
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,
165              expected_value,
166              base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy,
167                         base::Unretained(test_harness_.get()),
168                         test_policy_definitions::kKeyStringList,
169                         &expected_value));
170 }
171
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");
177
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);
182
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);
189   list->Append(sub);
190   sub = new base::DictionaryValue();
191   sub->SetString("ccc", "333");
192   sub->SetString("ddd", "444");
193   list->Append(sub);
194   dict->Set("sublist", list);
195   expected_value.Set("dict", dict);
196
197   CheckValue(test_policy_definitions::kKeyDictionary,
198              expected_value,
199              base::Bind(&PolicyProviderTestHarness::InstallDictionaryPolicy,
200                         base::Unretained(test_harness_.get()),
201                         test_policy_definitions::kKeyDictionary,
202                         &expected_value));
203 }
204
205 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) {
206   PolicyBundle bundle;
207   EXPECT_TRUE(provider_->policies().Equals(bundle));
208
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);
216
217   EXPECT_TRUE(provider_->policies().Equals(bundle));
218
219   // OnUpdatePolicy is called when there are changes.
220   test_harness_->InstallStringPolicy(test_policy_definitions::kKeyString,
221                                      "value");
222   EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1);
223   provider_->RefreshPolicies();
224   loop_.RunUntilIdle();
225   Mock::VerifyAndClearExpectations(&observer);
226
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"),
232            NULL);
233   EXPECT_TRUE(provider_->policies().Equals(bundle));
234   provider_->RemoveObserver(&observer);
235 }
236
237 TEST(ConfigurationPolicyProviderTest, FixDeprecatedPolicies) {
238   PolicyMap policy_map;
239   policy_map.Set(key::kProxyServerMode,
240                  POLICY_LEVEL_MANDATORY,
241                  POLICY_SCOPE_USER,
242                  base::Value::CreateIntegerValue(3),
243                  NULL);
244
245   // Both these policies should be ignored, since there's a higher priority
246   // policy available.
247   policy_map.Set(key::kProxyMode,
248                  POLICY_LEVEL_RECOMMENDED,
249                  POLICY_SCOPE_USER,
250                  base::Value::CreateStringValue("pac_script"),
251                  NULL);
252   policy_map.Set(key::kProxyPacUrl,
253                  POLICY_LEVEL_RECOMMENDED,
254                  POLICY_SCOPE_USER,
255                  base::Value::CreateStringValue("http://example.com/wpad.dat"),
256                  NULL);
257
258   MockConfigurationPolicyProvider provider;
259   provider.Init();
260   provider.UpdateChromePolicy(policy_map);
261
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,
268            POLICY_SCOPE_USER,
269            expected_value,
270            NULL);
271   EXPECT_TRUE(provider.policies().Equals(expected_bundle));
272   provider.Shutdown();
273 }
274
275 Configuration3rdPartyPolicyProviderTest::
276     Configuration3rdPartyPolicyProviderTest() {}
277
278 Configuration3rdPartyPolicyProviderTest::
279     ~Configuration3rdPartyPolicyProviderTest() {}
280
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");
287
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());
293     list->Append(dict);
294   }
295   policy_dict.Set("list", list);
296   policy_dict.Set("dict", policy_dict.DeepCopy());
297
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);
313
314   provider_->RefreshPolicies();
315   loop_.RunUntilIdle();
316
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(),
322                       NULL);
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));
337 }
338
339 }  // namespace policy