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 "components/policy/core/common/policy_namespace.h" |
16 #include "policy/policy_constants.h" | 17 #include "policy/policy_constants.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
18 | 19 |
19 using ::testing::Mock; | 20 using ::testing::Mock; |
20 using ::testing::_; | 21 using ::testing::_; |
21 | 22 |
22 namespace policy { | 23 namespace policy { |
23 | 24 |
| 25 const char kTestChromeSchema[] = |
| 26 "{" |
| 27 " \"type\": \"object\"," |
| 28 " \"properties\": {" |
| 29 " \"StringPolicy\": { \"type\": \"string\" }," |
| 30 " \"BooleanPolicy\": { \"type\": \"boolean\" }," |
| 31 " \"IntegerPolicy\": { \"type\": \"integer\" }," |
| 32 " \"StringListPolicy\": {" |
| 33 " \"type\": \"array\"," |
| 34 " \"items\": { \"type\": \"string\" }" |
| 35 " }," |
| 36 " \"DictionaryPolicy\": {" |
| 37 " \"type\": \"object\"," |
| 38 " \"properties\": {" |
| 39 " \"bool\": { \"type\": \"boolean\" }," |
| 40 " \"double\": { \"type\": \"number\" }," |
| 41 " \"int\": { \"type\": \"integer\" }," |
| 42 " \"string\": { \"type\": \"string\" }," |
| 43 " \"array\": {" |
| 44 " \"type\": \"array\"," |
| 45 " \"items\": { \"type\": \"string\" }" |
| 46 " }," |
| 47 " \"dictionary\": {" |
| 48 " \"type\": \"object\"," |
| 49 " \"properties\": {" |
| 50 " \"sub\": { \"type\": \"string\" }," |
| 51 " \"sublist\": {" |
| 52 " \"type\": \"array\"," |
| 53 " \"items\": {" |
| 54 " \"type\": \"object\"," |
| 55 " \"properties\": {" |
| 56 " \"aaa\": { \"type\": \"integer\" }," |
| 57 " \"bbb\": { \"type\": \"integer\" }," |
| 58 " \"ccc\": { \"type\": \"string\" }," |
| 59 " \"ddd\": { \"type\": \"string\" }" |
| 60 " }" |
| 61 " }" |
| 62 " }" |
| 63 " }" |
| 64 " }," |
| 65 " \"list\": {" |
| 66 " \"type\": \"array\"," |
| 67 " \"items\": {" |
| 68 " \"type\": \"object\"," |
| 69 " \"properties\": {" |
| 70 " \"subdictindex\": { \"type\": \"integer\" }," |
| 71 " \"subdict\": {" |
| 72 " \"type\": \"object\"," |
| 73 " \"properties\": {" |
| 74 " \"bool\": { \"type\": \"boolean\" }," |
| 75 " \"double\": { \"type\": \"number\" }," |
| 76 " \"int\": { \"type\": \"integer\" }," |
| 77 " \"string\": { \"type\": \"string\" }" |
| 78 " }" |
| 79 " }" |
| 80 " }" |
| 81 " }" |
| 82 " }," |
| 83 " \"dict\": {" |
| 84 " \"type\": \"object\"," |
| 85 " \"properties\": {" |
| 86 " \"bool\": { \"type\": \"boolean\" }," |
| 87 " \"double\": { \"type\": \"number\" }," |
| 88 " \"int\": { \"type\": \"integer\" }," |
| 89 " \"string\": { \"type\": \"string\" }," |
| 90 " \"list\": {" |
| 91 " \"type\": \"array\"," |
| 92 " \"items\": {" |
| 93 " \"type\": \"object\"," |
| 94 " \"properties\": {" |
| 95 " \"subdictindex\": { \"type\": \"integer\" }," |
| 96 " \"subdict\": {" |
| 97 " \"type\": \"object\"," |
| 98 " \"properties\": {" |
| 99 " \"bool\": { \"type\": \"boolean\" }," |
| 100 " \"double\": { \"type\": \"number\" }," |
| 101 " \"int\": { \"type\": \"integer\" }," |
| 102 " \"string\": { \"type\": \"string\" }" |
| 103 " }" |
| 104 " }" |
| 105 " }" |
| 106 " }" |
| 107 " }" |
| 108 " }" |
| 109 " }" |
| 110 " }" |
| 111 " }" |
| 112 " }" |
| 113 "}"; |
| 114 |
24 namespace test_policy_definitions { | 115 namespace test_policy_definitions { |
25 | 116 |
26 const char kKeyString[] = "StringPolicy"; | 117 const char kKeyString[] = "StringPolicy"; |
27 const char kKeyBoolean[] = "BooleanPolicy"; | 118 const char kKeyBoolean[] = "BooleanPolicy"; |
28 const char kKeyInteger[] = "IntegerPolicy"; | 119 const char kKeyInteger[] = "IntegerPolicy"; |
29 const char kKeyStringList[] = "StringListPolicy"; | 120 const char kKeyStringList[] = "StringListPolicy"; |
30 const char kKeyDictionary[] = "DictionaryPolicy"; | 121 const char kKeyDictionary[] = "DictionaryPolicy"; |
31 | 122 |
32 static const PolicyDefinitionList::Entry kEntries[] = { | 123 static const PolicyDefinitionList::Entry kEntries[] = { |
33 { kKeyString, base::Value::TYPE_STRING }, | 124 { kKeyString, base::Value::TYPE_STRING }, |
34 { kKeyBoolean, base::Value::TYPE_BOOLEAN }, | 125 { kKeyBoolean, base::Value::TYPE_BOOLEAN }, |
35 { kKeyInteger, base::Value::TYPE_INTEGER }, | 126 { kKeyInteger, base::Value::TYPE_INTEGER }, |
36 { kKeyStringList, base::Value::TYPE_LIST }, | 127 { kKeyStringList, base::Value::TYPE_LIST }, |
37 { kKeyDictionary, base::Value::TYPE_DICTIONARY }, | 128 { kKeyDictionary, base::Value::TYPE_DICTIONARY }, |
38 }; | 129 }; |
39 | 130 |
40 const PolicyDefinitionList kList = { | 131 const PolicyDefinitionList kList = { |
41 kEntries, kEntries + arraysize(kEntries) | 132 kEntries, kEntries + arraysize(kEntries) |
42 }; | 133 }; |
43 | 134 |
44 } // namespace test_policy_definitions | 135 } // namespace test_policy_definitions |
45 | 136 |
46 PolicyTestBase::PolicyTestBase() {} | 137 PolicyTestBase::PolicyTestBase() {} |
47 | 138 |
48 PolicyTestBase::~PolicyTestBase() {} | 139 PolicyTestBase::~PolicyTestBase() {} |
49 | 140 |
| 141 void PolicyTestBase::SetUp() { |
| 142 std::string error; |
| 143 chrome_schema_ = Schema::Parse(kTestChromeSchema, &error); |
| 144 ASSERT_TRUE(chrome_schema_.valid()) << error; |
| 145 schema_registry_.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_CHROME, ""), |
| 146 chrome_schema_); |
| 147 } |
| 148 |
50 void PolicyTestBase::TearDown() { | 149 void PolicyTestBase::TearDown() { |
51 loop_.RunUntilIdle(); | 150 loop_.RunUntilIdle(); |
52 } | 151 } |
53 | 152 |
54 PolicyProviderTestHarness::PolicyProviderTestHarness(PolicyLevel level, | 153 PolicyProviderTestHarness::PolicyProviderTestHarness(PolicyLevel level, |
55 PolicyScope scope) | 154 PolicyScope scope) |
56 : level_(level), scope_(scope) {} | 155 : level_(level), scope_(scope) {} |
57 | 156 |
58 PolicyProviderTestHarness::~PolicyProviderTestHarness() {} | 157 PolicyProviderTestHarness::~PolicyProviderTestHarness() {} |
59 | 158 |
(...skipping 13 matching lines...) Expand all Loading... |
73 ConfigurationPolicyProviderTest::ConfigurationPolicyProviderTest() {} | 172 ConfigurationPolicyProviderTest::ConfigurationPolicyProviderTest() {} |
74 | 173 |
75 ConfigurationPolicyProviderTest::~ConfigurationPolicyProviderTest() {} | 174 ConfigurationPolicyProviderTest::~ConfigurationPolicyProviderTest() {} |
76 | 175 |
77 void ConfigurationPolicyProviderTest::SetUp() { | 176 void ConfigurationPolicyProviderTest::SetUp() { |
78 PolicyTestBase::SetUp(); | 177 PolicyTestBase::SetUp(); |
79 | 178 |
80 test_harness_.reset((*GetParam())()); | 179 test_harness_.reset((*GetParam())()); |
81 test_harness_->SetUp(); | 180 test_harness_->SetUp(); |
82 | 181 |
| 182 Schema extension_schema = |
| 183 chrome_schema_.GetKnownProperty(test_policy_definitions::kKeyDictionary); |
| 184 ASSERT_TRUE(extension_schema.valid()); |
| 185 schema_registry_.RegisterComponent( |
| 186 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
| 187 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), |
| 188 extension_schema); |
| 189 schema_registry_.RegisterComponent( |
| 190 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
| 191 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), |
| 192 extension_schema); |
| 193 schema_registry_.RegisterComponent( |
| 194 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
| 195 "cccccccccccccccccccccccccccccccc"), |
| 196 extension_schema); |
| 197 |
83 provider_.reset(test_harness_->CreateProvider( | 198 provider_.reset(test_harness_->CreateProvider( |
84 loop_.message_loop_proxy(), &test_policy_definitions::kList)); | 199 &schema_registry_, |
85 provider_->Init(); | 200 loop_.message_loop_proxy(), |
| 201 &test_policy_definitions::kList)); |
| 202 provider_->Init(&schema_registry_); |
86 // Some providers do a reload on init. Make sure any notifications generated | 203 // Some providers do a reload on init. Make sure any notifications generated |
87 // are fired now. | 204 // are fired now. |
88 loop_.RunUntilIdle(); | 205 loop_.RunUntilIdle(); |
89 | 206 |
90 const PolicyBundle kEmptyBundle; | 207 const PolicyBundle kEmptyBundle; |
91 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); | 208 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); |
92 } | 209 } |
93 | 210 |
94 void ConfigurationPolicyProviderTest::TearDown() { | 211 void ConfigurationPolicyProviderTest::TearDown() { |
95 // Give providers the chance to clean up after themselves on the file thread. | 212 // Give providers the chance to clean up after themselves on the file thread. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 expected_value, | 282 expected_value, |
166 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, | 283 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, |
167 base::Unretained(test_harness_.get()), | 284 base::Unretained(test_harness_.get()), |
168 test_policy_definitions::kKeyStringList, | 285 test_policy_definitions::kKeyStringList, |
169 &expected_value)); | 286 &expected_value)); |
170 } | 287 } |
171 | 288 |
172 TEST_P(ConfigurationPolicyProviderTest, DictionaryValue) { | 289 TEST_P(ConfigurationPolicyProviderTest, DictionaryValue) { |
173 base::DictionaryValue expected_value; | 290 base::DictionaryValue expected_value; |
174 expected_value.SetBoolean("bool", true); | 291 expected_value.SetBoolean("bool", true); |
| 292 expected_value.SetDouble("double", 123.456); |
175 expected_value.SetInteger("int", 123); | 293 expected_value.SetInteger("int", 123); |
176 expected_value.SetString("str", "omg"); | 294 expected_value.SetString("string", "omg"); |
177 | 295 |
178 base::ListValue* list = new base::ListValue(); | 296 base::ListValue* list = new base::ListValue(); |
179 list->Set(0U, base::Value::CreateStringValue("first")); | 297 list->Set(0U, base::Value::CreateStringValue("first")); |
180 list->Set(1U, base::Value::CreateStringValue("second")); | 298 list->Set(1U, base::Value::CreateStringValue("second")); |
181 expected_value.Set("list", list); | 299 expected_value.Set("array", list); |
182 | 300 |
183 base::DictionaryValue* dict = new base::DictionaryValue(); | 301 base::DictionaryValue* dict = new base::DictionaryValue(); |
184 dict->SetString("sub", "value"); | 302 dict->SetString("sub", "value"); |
185 list = new base::ListValue(); | 303 list = new base::ListValue(); |
186 base::DictionaryValue* sub = new base::DictionaryValue(); | 304 base::DictionaryValue* sub = new base::DictionaryValue(); |
187 sub->SetInteger("aaa", 111); | 305 sub->SetInteger("aaa", 111); |
188 sub->SetInteger("bbb", 222); | 306 sub->SetInteger("bbb", 222); |
189 list->Append(sub); | 307 list->Append(sub); |
190 sub = new base::DictionaryValue(); | 308 sub = new base::DictionaryValue(); |
191 sub->SetString("ccc", "333"); | 309 sub->SetString("ccc", "333"); |
192 sub->SetString("ddd", "444"); | 310 sub->SetString("ddd", "444"); |
193 list->Append(sub); | 311 list->Append(sub); |
194 dict->Set("sublist", list); | 312 dict->Set("sublist", list); |
195 expected_value.Set("dict", dict); | 313 expected_value.Set("dictionary", dict); |
196 | 314 |
197 CheckValue(test_policy_definitions::kKeyDictionary, | 315 CheckValue(test_policy_definitions::kKeyDictionary, |
198 expected_value, | 316 expected_value, |
199 base::Bind(&PolicyProviderTestHarness::InstallDictionaryPolicy, | 317 base::Bind(&PolicyProviderTestHarness::InstallDictionaryPolicy, |
200 base::Unretained(test_harness_.get()), | 318 base::Unretained(test_harness_.get()), |
201 test_policy_definitions::kKeyDictionary, | 319 test_policy_definitions::kKeyDictionary, |
202 &expected_value)); | 320 &expected_value)); |
203 } | 321 } |
204 | 322 |
205 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { | 323 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 Configuration3rdPartyPolicyProviderTest() {} | 394 Configuration3rdPartyPolicyProviderTest() {} |
277 | 395 |
278 Configuration3rdPartyPolicyProviderTest:: | 396 Configuration3rdPartyPolicyProviderTest:: |
279 ~Configuration3rdPartyPolicyProviderTest() {} | 397 ~Configuration3rdPartyPolicyProviderTest() {} |
280 | 398 |
281 TEST_P(Configuration3rdPartyPolicyProviderTest, Load3rdParty) { | 399 TEST_P(Configuration3rdPartyPolicyProviderTest, Load3rdParty) { |
282 base::DictionaryValue policy_dict; | 400 base::DictionaryValue policy_dict; |
283 policy_dict.SetBoolean("bool", true); | 401 policy_dict.SetBoolean("bool", true); |
284 policy_dict.SetDouble("double", 123.456); | 402 policy_dict.SetDouble("double", 123.456); |
285 policy_dict.SetInteger("int", 789); | 403 policy_dict.SetInteger("int", 789); |
286 policy_dict.SetString("str", "string value"); | 404 policy_dict.SetString("string", "string value"); |
287 | 405 |
288 base::ListValue* list = new base::ListValue(); | 406 base::ListValue* list = new base::ListValue(); |
289 for (int i = 0; i < 2; ++i) { | 407 for (int i = 0; i < 2; ++i) { |
290 base::DictionaryValue* dict = new base::DictionaryValue(); | 408 base::DictionaryValue* dict = new base::DictionaryValue(); |
291 dict->SetInteger("subdictindex", i); | 409 dict->SetInteger("subdictindex", i); |
292 dict->Set("subdict", policy_dict.DeepCopy()); | 410 dict->Set("subdict", policy_dict.DeepCopy()); |
293 list->Append(dict); | 411 list->Append(dict); |
294 } | 412 } |
295 policy_dict.Set("list", list); | 413 policy_dict.Set("list", list); |
296 policy_dict.Set("dict", policy_dict.DeepCopy()); | 414 policy_dict.Set("dict", policy_dict.DeepCopy()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 448 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
331 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) | 449 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) |
332 .CopyFrom(expected_policy); | 450 .CopyFrom(expected_policy); |
333 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 451 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
334 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) | 452 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) |
335 .CopyFrom(expected_policy); | 453 .CopyFrom(expected_policy); |
336 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); | 454 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); |
337 } | 455 } |
338 | 456 |
339 } // namespace policy | 457 } // namespace policy |
OLD | NEW |