OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/policy/core/common/policy_bundle.h" | 5 #include "components/policy/core/common/policy_bundle.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "components/policy/core/common/external_data_fetcher.h" | 10 #include "components/policy/core/common/external_data_fetcher.h" |
11 #include "components/policy/core/common/policy_map.h" | 11 #include "components/policy/core/common/policy_map.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace policy { | 14 namespace policy { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const char kPolicyClashing0[] = "policy-clashing-0"; | 18 const char kPolicyClashing0[] = "policy-clashing-0"; |
19 const char kPolicyClashing1[] = "policy-clashing-1"; | 19 const char kPolicyClashing1[] = "policy-clashing-1"; |
20 const char kPolicy0[] = "policy-0"; | 20 const char kPolicy0[] = "policy-0"; |
21 const char kPolicy1[] = "policy-1"; | 21 const char kPolicy1[] = "policy-1"; |
22 const char kPolicy2[] = "policy-2"; | 22 const char kPolicy2[] = "policy-2"; |
23 const char kExtension0[] = "extension-0"; | 23 const char kExtension0[] = "extension-0"; |
24 const char kExtension1[] = "extension-1"; | 24 const char kExtension1[] = "extension-1"; |
25 const char kExtension2[] = "extension-2"; | 25 const char kExtension2[] = "extension-2"; |
26 const char kExtension3[] = "extension-3"; | 26 const char kExtension3[] = "extension-3"; |
27 | 27 |
28 // Adds test policies to |policy|. | 28 // Adds test policies to |policy|. |
29 void AddTestPolicies(PolicyMap* policy) { | 29 void AddTestPolicies(PolicyMap* policy) { |
30 policy->Set("mandatory-user", POLICY_LEVEL_MANDATORY, | 30 policy->Set("mandatory-user", |
31 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(123), NULL); | 31 POLICY_LEVEL_MANDATORY, |
| 32 POLICY_SCOPE_USER, |
| 33 new base::FundamentalValue(123), |
| 34 NULL); |
32 policy->Set("mandatory-machine", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | 35 policy->Set("mandatory-machine", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, |
33 base::Value::CreateStringValue("omg"), NULL); | 36 base::Value::CreateStringValue("omg"), NULL); |
34 policy->Set("recommended-user", POLICY_LEVEL_RECOMMENDED, | 37 policy->Set("recommended-user", POLICY_LEVEL_RECOMMENDED, |
35 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); | 38 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); |
36 base::DictionaryValue* dict = new base::DictionaryValue(); | 39 base::DictionaryValue* dict = new base::DictionaryValue(); |
37 dict->SetBoolean("false", false); | 40 dict->SetBoolean("false", false); |
38 dict->SetInteger("int", 456); | 41 dict->SetInteger("int", 456); |
39 dict->SetString("str", "bbq"); | 42 dict->SetString("str", "bbq"); |
40 policy->Set("recommended-machine", POLICY_LEVEL_RECOMMENDED, | 43 policy->Set("recommended-machine", POLICY_LEVEL_RECOMMENDED, |
41 POLICY_SCOPE_MACHINE, dict, NULL); | 44 POLICY_SCOPE_MACHINE, dict, NULL); |
42 } | 45 } |
43 | 46 |
44 // Adds test policies to |policy| based on the parameters: | 47 // Adds test policies to |policy| based on the parameters: |
45 // - kPolicyClashing0 mapped to |value|, user mandatory | 48 // - kPolicyClashing0 mapped to |value|, user mandatory |
46 // - kPolicyClashing1 mapped to |value|, with |level| and |scope| | 49 // - kPolicyClashing1 mapped to |value|, with |level| and |scope| |
47 // - |name| mapped to |value|, user mandatory | 50 // - |name| mapped to |value|, user mandatory |
48 void AddTestPoliciesWithParams(PolicyMap *policy, | 51 void AddTestPoliciesWithParams(PolicyMap *policy, |
49 const char* name, | 52 const char* name, |
50 int value, | 53 int value, |
51 PolicyLevel level, | 54 PolicyLevel level, |
52 PolicyScope scope) { | 55 PolicyScope scope) { |
53 policy->Set(kPolicyClashing0, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 56 policy->Set(kPolicyClashing0, |
54 base::Value::CreateIntegerValue(value), NULL); | 57 POLICY_LEVEL_MANDATORY, |
55 policy->Set(kPolicyClashing1, level, scope, | 58 POLICY_SCOPE_USER, |
56 base::Value::CreateIntegerValue(value), NULL); | 59 new base::FundamentalValue(value), |
57 policy->Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 60 NULL); |
58 base::Value::CreateIntegerValue(value), NULL); | 61 policy->Set( |
| 62 kPolicyClashing1, level, scope, new base::FundamentalValue(value), NULL); |
| 63 policy->Set(name, |
| 64 POLICY_LEVEL_MANDATORY, |
| 65 POLICY_SCOPE_USER, |
| 66 new base::FundamentalValue(value), |
| 67 NULL); |
59 } | 68 } |
60 | 69 |
61 // Returns true if |bundle| is empty. | 70 // Returns true if |bundle| is empty. |
62 bool IsEmpty(const PolicyBundle& bundle) { | 71 bool IsEmpty(const PolicyBundle& bundle) { |
63 return bundle.begin() == bundle.end(); | 72 return bundle.begin() == bundle.end(); |
64 } | 73 } |
65 | 74 |
66 } // namespace | 75 } // namespace |
67 | 76 |
68 TEST(PolicyBundleTest, Get) { | 77 TEST(PolicyBundleTest, Get) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 merged.MergeFrom(bundle2); | 184 merged.MergeFrom(bundle2); |
176 PolicyBundle empty_bundle; | 185 PolicyBundle empty_bundle; |
177 merged.MergeFrom(empty_bundle); | 186 merged.MergeFrom(empty_bundle); |
178 | 187 |
179 // chrome and kExtension3 policies are merged: | 188 // chrome and kExtension3 policies are merged: |
180 // - kPolicyClashing0 comes from bundle0, which has the highest priority; | 189 // - kPolicyClashing0 comes from bundle0, which has the highest priority; |
181 // - kPolicyClashing1 comes from bundle1, which has the highest level/scope | 190 // - kPolicyClashing1 comes from bundle1, which has the highest level/scope |
182 // combination; | 191 // combination; |
183 // - kPolicyN are merged from each bundle. | 192 // - kPolicyN are merged from each bundle. |
184 PolicyMap expected; | 193 PolicyMap expected; |
185 expected.Set(kPolicyClashing0, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 194 expected.Set(kPolicyClashing0, |
186 base::Value::CreateIntegerValue(0), NULL); | 195 POLICY_LEVEL_MANDATORY, |
187 expected.Set(kPolicyClashing1, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | 196 POLICY_SCOPE_USER, |
188 base::Value::CreateIntegerValue(1), NULL); | 197 new base::FundamentalValue(0), |
189 expected.Set(kPolicy0, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 198 NULL); |
190 base::Value::CreateIntegerValue(0), NULL); | 199 expected.Set(kPolicyClashing1, |
191 expected.Set(kPolicy1, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 200 POLICY_LEVEL_MANDATORY, |
192 base::Value::CreateIntegerValue(1), NULL); | 201 POLICY_SCOPE_MACHINE, |
193 expected.Set(kPolicy2, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 202 new base::FundamentalValue(1), |
194 base::Value::CreateIntegerValue(2), NULL); | 203 NULL); |
| 204 expected.Set(kPolicy0, |
| 205 POLICY_LEVEL_MANDATORY, |
| 206 POLICY_SCOPE_USER, |
| 207 new base::FundamentalValue(0), |
| 208 NULL); |
| 209 expected.Set(kPolicy1, |
| 210 POLICY_LEVEL_MANDATORY, |
| 211 POLICY_SCOPE_USER, |
| 212 new base::FundamentalValue(1), |
| 213 NULL); |
| 214 expected.Set(kPolicy2, |
| 215 POLICY_LEVEL_MANDATORY, |
| 216 POLICY_SCOPE_USER, |
| 217 new base::FundamentalValue(2), |
| 218 NULL); |
195 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, | 219 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, |
196 std::string())).Equals(expected)); | 220 std::string())).Equals(expected)); |
197 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 221 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
198 kExtension3)).Equals(expected)); | 222 kExtension3)).Equals(expected)); |
199 // extension0 comes only from bundle0. | 223 // extension0 comes only from bundle0. |
200 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 224 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
201 kExtension0)).Equals(policy0)); | 225 kExtension0)).Equals(policy0)); |
202 // extension1 comes only from bundle1. | 226 // extension1 comes only from bundle1. |
203 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 227 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
204 kExtension1)).Equals(policy1)); | 228 kExtension1)).Equals(policy1)); |
(...skipping 21 matching lines...) Expand all Loading... |
226 EXPECT_TRUE(bundle.Equals(other)); | 250 EXPECT_TRUE(bundle.Equals(other)); |
227 AddTestPolicies(&other.Get( | 251 AddTestPolicies(&other.Get( |
228 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension2))); | 252 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension2))); |
229 EXPECT_FALSE(bundle.Equals(other)); | 253 EXPECT_FALSE(bundle.Equals(other)); |
230 | 254 |
231 other.CopyFrom(bundle); | 255 other.CopyFrom(bundle); |
232 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 256 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
233 .Set(kPolicy0, | 257 .Set(kPolicy0, |
234 POLICY_LEVEL_MANDATORY, | 258 POLICY_LEVEL_MANDATORY, |
235 POLICY_SCOPE_USER, | 259 POLICY_SCOPE_USER, |
236 base::Value::CreateIntegerValue(123), | 260 new base::FundamentalValue(123), |
237 NULL); | 261 NULL); |
238 EXPECT_FALSE(bundle.Equals(other)); | 262 EXPECT_FALSE(bundle.Equals(other)); |
239 other.CopyFrom(bundle); | 263 other.CopyFrom(bundle); |
240 EXPECT_TRUE(bundle.Equals(other)); | 264 EXPECT_TRUE(bundle.Equals(other)); |
241 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 265 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
242 .Set(kPolicy0, | 266 .Set(kPolicy0, |
243 POLICY_LEVEL_MANDATORY, | 267 POLICY_LEVEL_MANDATORY, |
244 POLICY_SCOPE_MACHINE, | 268 POLICY_SCOPE_MACHINE, |
245 base::Value::CreateIntegerValue(123), | 269 new base::FundamentalValue(123), |
246 NULL); | 270 NULL); |
247 EXPECT_FALSE(bundle.Equals(other)); | 271 EXPECT_FALSE(bundle.Equals(other)); |
248 | 272 |
249 // Test non-const Get(). | 273 // Test non-const Get(). |
250 bundle.Clear(); | 274 bundle.Clear(); |
251 other.Clear(); | 275 other.Clear(); |
252 PolicyMap& policy_map = | 276 PolicyMap& policy_map = |
253 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); | 277 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); |
254 EXPECT_TRUE(bundle.Equals(other)); | 278 EXPECT_TRUE(bundle.Equals(other)); |
255 policy_map.Set(kPolicy0, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 279 policy_map.Set(kPolicy0, |
256 base::Value::CreateIntegerValue(123), NULL); | 280 POLICY_LEVEL_MANDATORY, |
| 281 POLICY_SCOPE_USER, |
| 282 new base::FundamentalValue(123), |
| 283 NULL); |
257 EXPECT_FALSE(bundle.Equals(other)); | 284 EXPECT_FALSE(bundle.Equals(other)); |
258 } | 285 } |
259 | 286 |
260 } // namespace policy | 287 } // namespace policy |
OLD | NEW |