| 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" |
| 11 #include "chrome/browser/policy/configuration_policy_provider.h" | 11 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 12 #include "chrome/browser/policy/external_data_fetcher.h" | 12 #include "chrome/browser/policy/external_data_fetcher.h" |
| 13 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 13 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 14 #include "chrome/browser/policy/policy_bundle.h" | 14 #include "chrome/browser/policy/policy_bundle.h" |
| 15 #include "chrome/browser/policy/policy_map.h" | 15 #include "chrome/browser/policy/policy_map.h" |
| 16 #include "policy/policy_constants.h" | 16 #include "policy/policy_constants.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 | 18 |
| 19 using ::testing::Mock; | 19 using ::testing::Mock; |
| 20 using ::testing::_; | 20 using ::testing::_; |
| 21 | 21 |
| 22 namespace { |
| 23 // Builds a DictionaryValue for testing. |
| 24 // On Windows this matches the Chrome policy test schema. |
| 25 void BuildTestDictionaryValue(base::DictionaryValue& value) { |
| 26 value.SetBoolean("bool", true); |
| 27 value.SetInteger("int", 123); |
| 28 value.SetString("str", "omg"); |
| 29 |
| 30 base::ListValue* list = new base::ListValue(); |
| 31 list->Set(0U, base::Value::CreateStringValue("first")); |
| 32 list->Set(1U, base::Value::CreateStringValue("second")); |
| 33 value.Set("list", list); |
| 34 |
| 35 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 36 dict->SetString("sub", "value"); |
| 37 list = new base::ListValue(); |
| 38 base::DictionaryValue* sub = new base::DictionaryValue(); |
| 39 sub->SetInteger("aaa", 111); |
| 40 sub->SetInteger("bbb", 222); |
| 41 list->Append(sub); |
| 42 sub = new base::DictionaryValue(); |
| 43 sub->SetString("ccc", "333"); |
| 44 sub->SetString("ddd", "444"); |
| 45 list->Append(sub); |
| 46 dict->Set("sublist", list); |
| 47 value.Set("dict", dict); |
| 48 } |
| 49 } // namespace |
| 50 |
| 22 namespace policy { | 51 namespace policy { |
| 23 | 52 |
| 24 namespace test_policy_definitions { | 53 namespace test_policy_definitions { |
| 25 | 54 |
| 26 const char kKeyString[] = "StringPolicy"; | 55 const char kKeyString[] = "StringPolicy"; |
| 27 const char kKeyBoolean[] = "BooleanPolicy"; | 56 const char kKeyBoolean[] = "BooleanPolicy"; |
| 28 const char kKeyInteger[] = "IntegerPolicy"; | 57 const char kKeyInteger[] = "IntegerPolicy"; |
| 29 const char kKeyStringList[] = "StringListPolicy"; | 58 const char kKeyStringList[] = "StringListPolicy"; |
| 30 const char kKeyDictionary[] = "DictionaryPolicy"; | 59 const char kKeyDictionary[] = "DictionaryPolicy"; |
| 31 | 60 |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 CheckValue(test_policy_definitions::kKeyStringList, | 193 CheckValue(test_policy_definitions::kKeyStringList, |
| 165 expected_value, | 194 expected_value, |
| 166 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, | 195 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, |
| 167 base::Unretained(test_harness_.get()), | 196 base::Unretained(test_harness_.get()), |
| 168 test_policy_definitions::kKeyStringList, | 197 test_policy_definitions::kKeyStringList, |
| 169 &expected_value)); | 198 &expected_value)); |
| 170 } | 199 } |
| 171 | 200 |
| 172 TEST_P(ConfigurationPolicyProviderTest, DictionaryValue) { | 201 TEST_P(ConfigurationPolicyProviderTest, DictionaryValue) { |
| 173 base::DictionaryValue expected_value; | 202 base::DictionaryValue expected_value; |
| 174 expected_value.SetBoolean("bool", true); | 203 BuildTestDictionaryValue(expected_value); |
| 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, | 204 CheckValue(test_policy_definitions::kKeyDictionary, |
| 198 expected_value, | 205 expected_value, |
| 199 base::Bind(&PolicyProviderTestHarness::InstallDictionaryPolicy, | 206 base::Bind(&PolicyProviderTestHarness::InstallDictionaryPolicy, |
| 200 base::Unretained(test_harness_.get()), | 207 base::Unretained(test_harness_.get()), |
| 201 test_policy_definitions::kKeyDictionary, | 208 test_policy_definitions::kKeyDictionary, |
| 202 &expected_value)); | 209 &expected_value)); |
| 203 } | 210 } |
| 204 | 211 |
| 205 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { | 212 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { |
| 206 PolicyBundle bundle; | 213 PolicyBundle bundle; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 provider.Shutdown(); | 279 provider.Shutdown(); |
| 273 } | 280 } |
| 274 | 281 |
| 275 Configuration3rdPartyPolicyProviderTest:: | 282 Configuration3rdPartyPolicyProviderTest:: |
| 276 Configuration3rdPartyPolicyProviderTest() {} | 283 Configuration3rdPartyPolicyProviderTest() {} |
| 277 | 284 |
| 278 Configuration3rdPartyPolicyProviderTest:: | 285 Configuration3rdPartyPolicyProviderTest:: |
| 279 ~Configuration3rdPartyPolicyProviderTest() {} | 286 ~Configuration3rdPartyPolicyProviderTest() {} |
| 280 | 287 |
| 281 TEST_P(Configuration3rdPartyPolicyProviderTest, Load3rdParty) { | 288 TEST_P(Configuration3rdPartyPolicyProviderTest, Load3rdParty) { |
| 289 // Install a Chrome policy. |
| 290 base::DictionaryValue chrome_policy_dict; |
| 291 BuildTestDictionaryValue(chrome_policy_dict); |
| 292 test_harness_->InstallDictionaryPolicy( |
| 293 test_policy_definitions::kKeyDictionary, &chrome_policy_dict); |
| 294 |
| 295 // Install 3rd party policies. |
| 282 base::DictionaryValue policy_dict; | 296 base::DictionaryValue policy_dict; |
| 283 policy_dict.SetBoolean("bool", true); | 297 policy_dict.SetBoolean("bool", true); |
| 284 policy_dict.SetDouble("double", 123.456); | 298 policy_dict.SetDouble("double", 123.456); |
| 285 policy_dict.SetInteger("int", 789); | 299 policy_dict.SetInteger("int", 789); |
| 286 policy_dict.SetString("str", "string value"); | 300 policy_dict.SetString("str", "string value"); |
| 287 | 301 |
| 288 base::ListValue* list = new base::ListValue(); | 302 base::ListValue* list = new base::ListValue(); |
| 289 for (int i = 0; i < 2; ++i) { | 303 for (int i = 0; i < 2; ++i) { |
| 290 base::DictionaryValue* dict = new base::DictionaryValue(); | 304 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 291 dict->SetInteger("subdictindex", i); | 305 dict->SetInteger("subdictindex", i); |
| 292 dict->Set("subdict", policy_dict.DeepCopy()); | 306 dict->Set("subdict", policy_dict.DeepCopy()); |
| 293 list->Append(dict); | 307 list->Append(dict); |
| 294 } | 308 } |
| 295 policy_dict.Set("list", list); | 309 policy_dict.Set("list", list); |
| 296 policy_dict.Set("dict", policy_dict.DeepCopy()); | 310 policy_dict.Set("dict", policy_dict.DeepCopy()); |
| 297 | 311 |
| 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; | 312 base::DictionaryValue policy_3rdparty; |
| 303 policy_3rdparty.Set("extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | 313 policy_3rdparty.Set("extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 304 policy_dict.DeepCopy()); | 314 policy_dict.DeepCopy()); |
| 305 policy_3rdparty.Set("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", | 315 policy_3rdparty.Set("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
| 306 policy_dict.DeepCopy()); | 316 policy_dict.DeepCopy()); |
| 307 // Install invalid 3rd party policies that shouldn't be loaded. These also | 317 // 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. | 318 // help detecting memory leaks in the code paths that detect invalid input. |
| 309 policy_3rdparty.Set("invalid-domain.component", policy_dict.DeepCopy()); | 319 policy_3rdparty.Set("invalid-domain.component", policy_dict.DeepCopy()); |
| 310 policy_3rdparty.Set("extensions.cccccccccccccccccccccccccccccccc", | 320 policy_3rdparty.Set("extensions.cccccccccccccccccccccccccccccccc", |
| 311 base::Value::CreateStringValue("invalid-value")); | 321 base::Value::CreateStringValue("invalid-value")); |
| 312 test_harness_->Install3rdPartyPolicy(&policy_3rdparty); | 322 test_harness_->Install3rdPartyPolicy(&policy_3rdparty); |
| 313 | 323 |
| 314 provider_->RefreshPolicies(); | 324 provider_->RefreshPolicies(); |
| 315 loop_.RunUntilIdle(); | 325 loop_.RunUntilIdle(); |
| 316 | 326 |
| 317 PolicyMap expected_policy; | 327 PolicyMap expected_policy; |
| 318 expected_policy.Set(test_policy_definitions::kKeyDictionary, | 328 expected_policy.Set(test_policy_definitions::kKeyDictionary, |
| 319 test_harness_->policy_level(), | 329 test_harness_->policy_level(), |
| 320 test_harness_->policy_scope(), | 330 test_harness_->policy_scope(), |
| 321 policy_dict.DeepCopy(), | 331 chrome_policy_dict.DeepCopy(), |
| 322 NULL); | 332 NULL); |
| 323 PolicyBundle expected_bundle; | 333 PolicyBundle expected_bundle; |
| 324 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 334 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
| 325 .CopyFrom(expected_policy); | 335 .CopyFrom(expected_policy); |
| 326 expected_policy.Clear(); | 336 expected_policy.Clear(); |
| 327 expected_policy.LoadFrom(&policy_dict, | 337 expected_policy.LoadFrom(&policy_dict, |
| 328 test_harness_->policy_level(), | 338 test_harness_->policy_level(), |
| 329 test_harness_->policy_scope()); | 339 test_harness_->policy_scope()); |
| 330 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 340 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
| 331 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) | 341 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) |
| 332 .CopyFrom(expected_policy); | 342 .CopyFrom(expected_policy); |
| 333 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 343 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
| 334 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) | 344 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) |
| 335 .CopyFrom(expected_policy); | 345 .CopyFrom(expected_policy); |
| 336 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); | 346 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); |
| 337 } | 347 } |
| 338 | 348 |
| 339 } // namespace policy | 349 } // namespace policy |
| OLD | NEW |