OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/policy/configuration_policy_provider_test.h" | 5 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 " }" | 105 " }" |
106 " }" | 106 " }" |
107 " }" | 107 " }" |
108 " }" | 108 " }" |
109 " }" | 109 " }" |
110 " }" | 110 " }" |
111 " }" | 111 " }" |
112 " }" | 112 " }" |
113 "}"; | 113 "}"; |
114 | 114 |
115 namespace test_policy_definitions { | 115 namespace test_keys { |
116 | 116 |
117 const char kKeyString[] = "StringPolicy"; | 117 const char kKeyString[] = "StringPolicy"; |
118 const char kKeyBoolean[] = "BooleanPolicy"; | 118 const char kKeyBoolean[] = "BooleanPolicy"; |
119 const char kKeyInteger[] = "IntegerPolicy"; | 119 const char kKeyInteger[] = "IntegerPolicy"; |
120 const char kKeyStringList[] = "StringListPolicy"; | 120 const char kKeyStringList[] = "StringListPolicy"; |
121 const char kKeyDictionary[] = "DictionaryPolicy"; | 121 const char kKeyDictionary[] = "DictionaryPolicy"; |
122 | 122 |
123 static const PolicyDefinitionList::Entry kEntries[] = { | 123 } // namespace test_keys |
124 { kKeyString, base::Value::TYPE_STRING }, | |
125 { kKeyBoolean, base::Value::TYPE_BOOLEAN }, | |
126 { kKeyInteger, base::Value::TYPE_INTEGER }, | |
127 { kKeyStringList, base::Value::TYPE_LIST }, | |
128 { kKeyDictionary, base::Value::TYPE_DICTIONARY }, | |
129 }; | |
130 | |
131 const PolicyDefinitionList kList = { | |
132 kEntries, kEntries + arraysize(kEntries) | |
133 }; | |
134 | |
135 } // namespace test_policy_definitions | |
136 | 124 |
137 PolicyTestBase::PolicyTestBase() {} | 125 PolicyTestBase::PolicyTestBase() {} |
138 | 126 |
139 PolicyTestBase::~PolicyTestBase() {} | 127 PolicyTestBase::~PolicyTestBase() {} |
140 | 128 |
141 void PolicyTestBase::SetUp() { | 129 void PolicyTestBase::SetUp() { |
142 std::string error; | 130 std::string error; |
143 chrome_schema_ = Schema::Parse(kTestChromeSchema, &error); | 131 chrome_schema_ = Schema::Parse(kTestChromeSchema, &error); |
144 ASSERT_TRUE(chrome_schema_.valid()) << error; | 132 ASSERT_TRUE(chrome_schema_.valid()) << error; |
145 schema_registry_.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_CHROME, ""), | 133 schema_registry_.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_CHROME, ""), |
(...skipping 27 matching lines...) Expand all Loading... |
173 | 161 |
174 ConfigurationPolicyProviderTest::~ConfigurationPolicyProviderTest() {} | 162 ConfigurationPolicyProviderTest::~ConfigurationPolicyProviderTest() {} |
175 | 163 |
176 void ConfigurationPolicyProviderTest::SetUp() { | 164 void ConfigurationPolicyProviderTest::SetUp() { |
177 PolicyTestBase::SetUp(); | 165 PolicyTestBase::SetUp(); |
178 | 166 |
179 test_harness_.reset((*GetParam())()); | 167 test_harness_.reset((*GetParam())()); |
180 test_harness_->SetUp(); | 168 test_harness_->SetUp(); |
181 | 169 |
182 Schema extension_schema = | 170 Schema extension_schema = |
183 chrome_schema_.GetKnownProperty(test_policy_definitions::kKeyDictionary); | 171 chrome_schema_.GetKnownProperty(test_keys::kKeyDictionary); |
184 ASSERT_TRUE(extension_schema.valid()); | 172 ASSERT_TRUE(extension_schema.valid()); |
185 schema_registry_.RegisterComponent( | 173 schema_registry_.RegisterComponent( |
186 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 174 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
187 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), | 175 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), |
188 extension_schema); | 176 extension_schema); |
189 schema_registry_.RegisterComponent( | 177 schema_registry_.RegisterComponent( |
190 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 178 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
191 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), | 179 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), |
192 extension_schema); | 180 extension_schema); |
193 schema_registry_.RegisterComponent( | 181 schema_registry_.RegisterComponent( |
194 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 182 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
195 "cccccccccccccccccccccccccccccccc"), | 183 "cccccccccccccccccccccccccccccccc"), |
196 extension_schema); | 184 extension_schema); |
197 | 185 |
198 provider_.reset(test_harness_->CreateProvider( | 186 provider_.reset(test_harness_->CreateProvider(&schema_registry_, |
199 &schema_registry_, | 187 loop_.message_loop_proxy())); |
200 loop_.message_loop_proxy(), | |
201 &test_policy_definitions::kList)); | |
202 provider_->Init(&schema_registry_); | 188 provider_->Init(&schema_registry_); |
203 // Some providers do a reload on init. Make sure any notifications generated | 189 // Some providers do a reload on init. Make sure any notifications generated |
204 // are fired now. | 190 // are fired now. |
205 loop_.RunUntilIdle(); | 191 loop_.RunUntilIdle(); |
206 | 192 |
207 const PolicyBundle kEmptyBundle; | 193 const PolicyBundle kEmptyBundle; |
208 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); | 194 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); |
209 } | 195 } |
210 | 196 |
211 void ConfigurationPolicyProviderTest::TearDown() { | 197 void ConfigurationPolicyProviderTest::TearDown() { |
(...skipping 27 matching lines...) Expand all Loading... |
239 TEST_P(ConfigurationPolicyProviderTest, Empty) { | 225 TEST_P(ConfigurationPolicyProviderTest, Empty) { |
240 provider_->RefreshPolicies(); | 226 provider_->RefreshPolicies(); |
241 loop_.RunUntilIdle(); | 227 loop_.RunUntilIdle(); |
242 const PolicyBundle kEmptyBundle; | 228 const PolicyBundle kEmptyBundle; |
243 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); | 229 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); |
244 } | 230 } |
245 | 231 |
246 TEST_P(ConfigurationPolicyProviderTest, StringValue) { | 232 TEST_P(ConfigurationPolicyProviderTest, StringValue) { |
247 const char kTestString[] = "string_value"; | 233 const char kTestString[] = "string_value"; |
248 base::StringValue expected_value(kTestString); | 234 base::StringValue expected_value(kTestString); |
249 CheckValue(test_policy_definitions::kKeyString, | 235 CheckValue(test_keys::kKeyString, |
250 expected_value, | 236 expected_value, |
251 base::Bind(&PolicyProviderTestHarness::InstallStringPolicy, | 237 base::Bind(&PolicyProviderTestHarness::InstallStringPolicy, |
252 base::Unretained(test_harness_.get()), | 238 base::Unretained(test_harness_.get()), |
253 test_policy_definitions::kKeyString, | 239 test_keys::kKeyString, |
254 kTestString)); | 240 kTestString)); |
255 } | 241 } |
256 | 242 |
257 TEST_P(ConfigurationPolicyProviderTest, BooleanValue) { | 243 TEST_P(ConfigurationPolicyProviderTest, BooleanValue) { |
258 base::FundamentalValue expected_value(true); | 244 base::FundamentalValue expected_value(true); |
259 CheckValue(test_policy_definitions::kKeyBoolean, | 245 CheckValue(test_keys::kKeyBoolean, |
260 expected_value, | 246 expected_value, |
261 base::Bind(&PolicyProviderTestHarness::InstallBooleanPolicy, | 247 base::Bind(&PolicyProviderTestHarness::InstallBooleanPolicy, |
262 base::Unretained(test_harness_.get()), | 248 base::Unretained(test_harness_.get()), |
263 test_policy_definitions::kKeyBoolean, | 249 test_keys::kKeyBoolean, |
264 true)); | 250 true)); |
265 } | 251 } |
266 | 252 |
267 TEST_P(ConfigurationPolicyProviderTest, IntegerValue) { | 253 TEST_P(ConfigurationPolicyProviderTest, IntegerValue) { |
268 base::FundamentalValue expected_value(42); | 254 base::FundamentalValue expected_value(42); |
269 CheckValue(test_policy_definitions::kKeyInteger, | 255 CheckValue(test_keys::kKeyInteger, |
270 expected_value, | 256 expected_value, |
271 base::Bind(&PolicyProviderTestHarness::InstallIntegerPolicy, | 257 base::Bind(&PolicyProviderTestHarness::InstallIntegerPolicy, |
272 base::Unretained(test_harness_.get()), | 258 base::Unretained(test_harness_.get()), |
273 test_policy_definitions::kKeyInteger, | 259 test_keys::kKeyInteger, |
274 42)); | 260 42)); |
275 } | 261 } |
276 | 262 |
277 TEST_P(ConfigurationPolicyProviderTest, StringListValue) { | 263 TEST_P(ConfigurationPolicyProviderTest, StringListValue) { |
278 base::ListValue expected_value; | 264 base::ListValue expected_value; |
279 expected_value.Set(0U, base::Value::CreateStringValue("first")); | 265 expected_value.Set(0U, base::Value::CreateStringValue("first")); |
280 expected_value.Set(1U, base::Value::CreateStringValue("second")); | 266 expected_value.Set(1U, base::Value::CreateStringValue("second")); |
281 CheckValue(test_policy_definitions::kKeyStringList, | 267 CheckValue(test_keys::kKeyStringList, |
282 expected_value, | 268 expected_value, |
283 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, | 269 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, |
284 base::Unretained(test_harness_.get()), | 270 base::Unretained(test_harness_.get()), |
285 test_policy_definitions::kKeyStringList, | 271 test_keys::kKeyStringList, |
286 &expected_value)); | 272 &expected_value)); |
287 } | 273 } |
288 | 274 |
289 TEST_P(ConfigurationPolicyProviderTest, DictionaryValue) { | 275 TEST_P(ConfigurationPolicyProviderTest, DictionaryValue) { |
290 base::DictionaryValue expected_value; | 276 base::DictionaryValue expected_value; |
291 expected_value.SetBoolean("bool", true); | 277 expected_value.SetBoolean("bool", true); |
292 expected_value.SetDouble("double", 123.456); | 278 expected_value.SetDouble("double", 123.456); |
293 expected_value.SetInteger("int", 123); | 279 expected_value.SetInteger("int", 123); |
294 expected_value.SetString("string", "omg"); | 280 expected_value.SetString("string", "omg"); |
295 | 281 |
296 base::ListValue* list = new base::ListValue(); | 282 base::ListValue* list = new base::ListValue(); |
297 list->Set(0U, base::Value::CreateStringValue("first")); | 283 list->Set(0U, base::Value::CreateStringValue("first")); |
298 list->Set(1U, base::Value::CreateStringValue("second")); | 284 list->Set(1U, base::Value::CreateStringValue("second")); |
299 expected_value.Set("array", list); | 285 expected_value.Set("array", list); |
300 | 286 |
301 base::DictionaryValue* dict = new base::DictionaryValue(); | 287 base::DictionaryValue* dict = new base::DictionaryValue(); |
302 dict->SetString("sub", "value"); | 288 dict->SetString("sub", "value"); |
303 list = new base::ListValue(); | 289 list = new base::ListValue(); |
304 base::DictionaryValue* sub = new base::DictionaryValue(); | 290 base::DictionaryValue* sub = new base::DictionaryValue(); |
305 sub->SetInteger("aaa", 111); | 291 sub->SetInteger("aaa", 111); |
306 sub->SetInteger("bbb", 222); | 292 sub->SetInteger("bbb", 222); |
307 list->Append(sub); | 293 list->Append(sub); |
308 sub = new base::DictionaryValue(); | 294 sub = new base::DictionaryValue(); |
309 sub->SetString("ccc", "333"); | 295 sub->SetString("ccc", "333"); |
310 sub->SetString("ddd", "444"); | 296 sub->SetString("ddd", "444"); |
311 list->Append(sub); | 297 list->Append(sub); |
312 dict->Set("sublist", list); | 298 dict->Set("sublist", list); |
313 expected_value.Set("dictionary", dict); | 299 expected_value.Set("dictionary", dict); |
314 | 300 |
315 CheckValue(test_policy_definitions::kKeyDictionary, | 301 CheckValue(test_keys::kKeyDictionary, |
316 expected_value, | 302 expected_value, |
317 base::Bind(&PolicyProviderTestHarness::InstallDictionaryPolicy, | 303 base::Bind(&PolicyProviderTestHarness::InstallDictionaryPolicy, |
318 base::Unretained(test_harness_.get()), | 304 base::Unretained(test_harness_.get()), |
319 test_policy_definitions::kKeyDictionary, | 305 test_keys::kKeyDictionary, |
320 &expected_value)); | 306 &expected_value)); |
321 } | 307 } |
322 | 308 |
323 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { | 309 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { |
324 PolicyBundle bundle; | 310 PolicyBundle bundle; |
325 EXPECT_TRUE(provider_->policies().Equals(bundle)); | 311 EXPECT_TRUE(provider_->policies().Equals(bundle)); |
326 | 312 |
327 // OnUpdatePolicy is called even when there are no changes. | 313 // OnUpdatePolicy is called even when there are no changes. |
328 MockConfigurationPolicyObserver observer; | 314 MockConfigurationPolicyObserver observer; |
329 provider_->AddObserver(&observer); | 315 provider_->AddObserver(&observer); |
330 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 316 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
331 provider_->RefreshPolicies(); | 317 provider_->RefreshPolicies(); |
332 loop_.RunUntilIdle(); | 318 loop_.RunUntilIdle(); |
333 Mock::VerifyAndClearExpectations(&observer); | 319 Mock::VerifyAndClearExpectations(&observer); |
334 | 320 |
335 EXPECT_TRUE(provider_->policies().Equals(bundle)); | 321 EXPECT_TRUE(provider_->policies().Equals(bundle)); |
336 | 322 |
337 // OnUpdatePolicy is called when there are changes. | 323 // OnUpdatePolicy is called when there are changes. |
338 test_harness_->InstallStringPolicy(test_policy_definitions::kKeyString, | 324 test_harness_->InstallStringPolicy(test_keys::kKeyString, "value"); |
339 "value"); | |
340 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 325 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
341 provider_->RefreshPolicies(); | 326 provider_->RefreshPolicies(); |
342 loop_.RunUntilIdle(); | 327 loop_.RunUntilIdle(); |
343 Mock::VerifyAndClearExpectations(&observer); | 328 Mock::VerifyAndClearExpectations(&observer); |
344 | 329 |
345 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 330 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
346 .Set(test_policy_definitions::kKeyString, | 331 .Set(test_keys::kKeyString, |
347 test_harness_->policy_level(), | 332 test_harness_->policy_level(), |
348 test_harness_->policy_scope(), | 333 test_harness_->policy_scope(), |
349 base::Value::CreateStringValue("value"), | 334 base::Value::CreateStringValue("value"), |
350 NULL); | 335 NULL); |
351 EXPECT_TRUE(provider_->policies().Equals(bundle)); | 336 EXPECT_TRUE(provider_->policies().Equals(bundle)); |
352 provider_->RemoveObserver(&observer); | 337 provider_->RemoveObserver(&observer); |
353 } | 338 } |
354 | 339 |
355 TEST(ConfigurationPolicyProviderTest, FixDeprecatedPolicies) { | 340 TEST(ConfigurationPolicyProviderTest, FixDeprecatedPolicies) { |
356 PolicyMap policy_map; | 341 PolicyMap policy_map; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 for (int i = 0; i < 2; ++i) { | 392 for (int i = 0; i < 2; ++i) { |
408 base::DictionaryValue* dict = new base::DictionaryValue(); | 393 base::DictionaryValue* dict = new base::DictionaryValue(); |
409 dict->SetInteger("subdictindex", i); | 394 dict->SetInteger("subdictindex", i); |
410 dict->Set("subdict", policy_dict.DeepCopy()); | 395 dict->Set("subdict", policy_dict.DeepCopy()); |
411 list->Append(dict); | 396 list->Append(dict); |
412 } | 397 } |
413 policy_dict.Set("list", list); | 398 policy_dict.Set("list", list); |
414 policy_dict.Set("dict", policy_dict.DeepCopy()); | 399 policy_dict.Set("dict", policy_dict.DeepCopy()); |
415 | 400 |
416 // Install these policies as a Chrome policy. | 401 // Install these policies as a Chrome policy. |
417 test_harness_->InstallDictionaryPolicy( | 402 test_harness_->InstallDictionaryPolicy(test_keys::kKeyDictionary, |
418 test_policy_definitions::kKeyDictionary, &policy_dict); | 403 &policy_dict); |
419 // Install them as 3rd party policies too. | 404 // Install them as 3rd party policies too. |
420 base::DictionaryValue policy_3rdparty; | 405 base::DictionaryValue policy_3rdparty; |
421 policy_3rdparty.Set("extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | 406 policy_3rdparty.Set("extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
422 policy_dict.DeepCopy()); | 407 policy_dict.DeepCopy()); |
423 policy_3rdparty.Set("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", | 408 policy_3rdparty.Set("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
424 policy_dict.DeepCopy()); | 409 policy_dict.DeepCopy()); |
425 // Install invalid 3rd party policies that shouldn't be loaded. These also | 410 // Install invalid 3rd party policies that shouldn't be loaded. These also |
426 // help detecting memory leaks in the code paths that detect invalid input. | 411 // help detecting memory leaks in the code paths that detect invalid input. |
427 policy_3rdparty.Set("invalid-domain.component", policy_dict.DeepCopy()); | 412 policy_3rdparty.Set("invalid-domain.component", policy_dict.DeepCopy()); |
428 policy_3rdparty.Set("extensions.cccccccccccccccccccccccccccccccc", | 413 policy_3rdparty.Set("extensions.cccccccccccccccccccccccccccccccc", |
429 base::Value::CreateStringValue("invalid-value")); | 414 base::Value::CreateStringValue("invalid-value")); |
430 test_harness_->Install3rdPartyPolicy(&policy_3rdparty); | 415 test_harness_->Install3rdPartyPolicy(&policy_3rdparty); |
431 | 416 |
432 provider_->RefreshPolicies(); | 417 provider_->RefreshPolicies(); |
433 loop_.RunUntilIdle(); | 418 loop_.RunUntilIdle(); |
434 | 419 |
435 PolicyMap expected_policy; | 420 PolicyMap expected_policy; |
436 expected_policy.Set(test_policy_definitions::kKeyDictionary, | 421 expected_policy.Set(test_keys::kKeyDictionary, |
437 test_harness_->policy_level(), | 422 test_harness_->policy_level(), |
438 test_harness_->policy_scope(), | 423 test_harness_->policy_scope(), |
439 policy_dict.DeepCopy(), | 424 policy_dict.DeepCopy(), |
440 NULL); | 425 NULL); |
441 PolicyBundle expected_bundle; | 426 PolicyBundle expected_bundle; |
442 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 427 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
443 .CopyFrom(expected_policy); | 428 .CopyFrom(expected_policy); |
444 expected_policy.Clear(); | 429 expected_policy.Clear(); |
445 expected_policy.LoadFrom(&policy_dict, | 430 expected_policy.LoadFrom(&policy_dict, |
446 test_harness_->policy_level(), | 431 test_harness_->policy_level(), |
447 test_harness_->policy_scope()); | 432 test_harness_->policy_scope()); |
448 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 433 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
449 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) | 434 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) |
450 .CopyFrom(expected_policy); | 435 .CopyFrom(expected_policy); |
451 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 436 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
452 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) | 437 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) |
453 .CopyFrom(expected_policy); | 438 .CopyFrom(expected_policy); |
454 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); | 439 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); |
455 } | 440 } |
456 | 441 |
457 } // namespace policy | 442 } // namespace policy |
OLD | NEW |