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 "components/policy/core/common/policy_types.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
14 | 13 |
15 namespace policy { | 14 namespace policy { |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 const char kPolicyClashing0[] = "policy-clashing-0"; | 18 const char kPolicyClashing0[] = "policy-clashing-0"; |
20 const char kPolicyClashing1[] = "policy-clashing-1"; | 19 const char kPolicyClashing1[] = "policy-clashing-1"; |
21 const char kPolicy0[] = "policy-0"; | 20 const char kPolicy0[] = "policy-0"; |
22 const char kPolicy1[] = "policy-1"; | 21 const char kPolicy1[] = "policy-1"; |
23 const char kPolicy2[] = "policy-2"; | 22 const char kPolicy2[] = "policy-2"; |
24 const char kExtension0[] = "extension-0"; | 23 const char kExtension0[] = "extension-0"; |
25 const char kExtension1[] = "extension-1"; | 24 const char kExtension1[] = "extension-1"; |
26 const char kExtension2[] = "extension-2"; | 25 const char kExtension2[] = "extension-2"; |
27 const char kExtension3[] = "extension-3"; | 26 const char kExtension3[] = "extension-3"; |
28 | 27 |
29 // Adds test policies to |policy|. | 28 // Adds test policies to |policy|. |
30 void AddTestPolicies(PolicyMap* policy) { | 29 void AddTestPolicies(PolicyMap* policy) { |
31 policy->Set("mandatory-user", | 30 policy->Set("mandatory-user", |
32 POLICY_LEVEL_MANDATORY, | 31 POLICY_LEVEL_MANDATORY, |
33 POLICY_SCOPE_USER, | 32 POLICY_SCOPE_USER, |
34 POLICY_SOURCE_CLOUD, | |
35 new base::FundamentalValue(123), | 33 new base::FundamentalValue(123), |
36 NULL); | 34 NULL); |
37 policy->Set("mandatory-machine", | 35 policy->Set("mandatory-machine", |
38 POLICY_LEVEL_MANDATORY, | 36 POLICY_LEVEL_MANDATORY, |
39 POLICY_SCOPE_MACHINE, | 37 POLICY_SCOPE_MACHINE, |
40 POLICY_SOURCE_CLOUD, | |
41 new base::StringValue("omg"), | 38 new base::StringValue("omg"), |
42 NULL); | 39 NULL); |
43 policy->Set("recommended-user", | 40 policy->Set("recommended-user", |
44 POLICY_LEVEL_RECOMMENDED, | 41 POLICY_LEVEL_RECOMMENDED, |
45 POLICY_SCOPE_USER, | 42 POLICY_SCOPE_USER, |
46 POLICY_SOURCE_CLOUD, | |
47 new base::FundamentalValue(true), | 43 new base::FundamentalValue(true), |
48 NULL); | 44 NULL); |
49 base::DictionaryValue* dict = new base::DictionaryValue(); | 45 base::DictionaryValue* dict = new base::DictionaryValue(); |
50 dict->SetBoolean("false", false); | 46 dict->SetBoolean("false", false); |
51 dict->SetInteger("int", 456); | 47 dict->SetInteger("int", 456); |
52 dict->SetString("str", "bbq"); | 48 dict->SetString("str", "bbq"); |
53 policy->Set("recommended-machine", POLICY_LEVEL_RECOMMENDED, | 49 policy->Set("recommended-machine", POLICY_LEVEL_RECOMMENDED, |
54 POLICY_SCOPE_MACHINE, POLICY_SOURCE_CLOUD, dict, nullptr); | 50 POLICY_SCOPE_MACHINE, dict, NULL); |
55 } | 51 } |
56 | 52 |
57 // Adds test policies to |policy| based on the parameters: | 53 // Adds test policies to |policy| based on the parameters: |
58 // - kPolicyClashing0 mapped to |value|, user mandatory | 54 // - kPolicyClashing0 mapped to |value|, user mandatory |
59 // - kPolicyClashing1 mapped to |value|, with |level| and |scope| | 55 // - kPolicyClashing1 mapped to |value|, with |level| and |scope| |
60 // - |name| mapped to |value|, user mandatory | 56 // - |name| mapped to |value|, user mandatory |
61 void AddTestPoliciesWithParams(PolicyMap *policy, | 57 void AddTestPoliciesWithParams(PolicyMap *policy, |
62 const char* name, | 58 const char* name, |
63 int value, | 59 int value, |
64 PolicyLevel level, | 60 PolicyLevel level, |
65 PolicyScope scope) { | 61 PolicyScope scope) { |
66 policy->Set(kPolicyClashing0, | 62 policy->Set(kPolicyClashing0, |
67 POLICY_LEVEL_MANDATORY, | 63 POLICY_LEVEL_MANDATORY, |
68 POLICY_SCOPE_USER, | 64 POLICY_SCOPE_USER, |
69 POLICY_SOURCE_CLOUD, | |
70 new base::FundamentalValue(value), | 65 new base::FundamentalValue(value), |
71 NULL); | 66 NULL); |
72 policy->Set(kPolicyClashing1, level, scope, POLICY_SOURCE_CLOUD, | 67 policy->Set( |
73 new base::FundamentalValue(value), nullptr); | 68 kPolicyClashing1, level, scope, new base::FundamentalValue(value), NULL); |
74 policy->Set(name, | 69 policy->Set(name, |
75 POLICY_LEVEL_MANDATORY, | 70 POLICY_LEVEL_MANDATORY, |
76 POLICY_SCOPE_USER, | 71 POLICY_SCOPE_USER, |
77 POLICY_SOURCE_CLOUD, | |
78 new base::FundamentalValue(value), | 72 new base::FundamentalValue(value), |
79 NULL); | 73 NULL); |
80 } | 74 } |
81 | 75 |
82 // Returns true if |bundle| is empty. | 76 // Returns true if |bundle| is empty. |
83 bool IsEmpty(const PolicyBundle& bundle) { | 77 bool IsEmpty(const PolicyBundle& bundle) { |
84 return bundle.begin() == bundle.end(); | 78 return bundle.begin() == bundle.end(); |
85 } | 79 } |
86 | 80 |
87 } // namespace | 81 } // namespace |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 193 |
200 // chrome and kExtension3 policies are merged: | 194 // chrome and kExtension3 policies are merged: |
201 // - kPolicyClashing0 comes from bundle0, which has the highest priority; | 195 // - kPolicyClashing0 comes from bundle0, which has the highest priority; |
202 // - kPolicyClashing1 comes from bundle1, which has the highest level/scope | 196 // - kPolicyClashing1 comes from bundle1, which has the highest level/scope |
203 // combination; | 197 // combination; |
204 // - kPolicyN are merged from each bundle. | 198 // - kPolicyN are merged from each bundle. |
205 PolicyMap expected; | 199 PolicyMap expected; |
206 expected.Set(kPolicyClashing0, | 200 expected.Set(kPolicyClashing0, |
207 POLICY_LEVEL_MANDATORY, | 201 POLICY_LEVEL_MANDATORY, |
208 POLICY_SCOPE_USER, | 202 POLICY_SCOPE_USER, |
209 POLICY_SOURCE_CLOUD, | |
210 new base::FundamentalValue(0), | 203 new base::FundamentalValue(0), |
211 NULL); | 204 NULL); |
212 expected.Set(kPolicyClashing1, | 205 expected.Set(kPolicyClashing1, |
213 POLICY_LEVEL_MANDATORY, | 206 POLICY_LEVEL_MANDATORY, |
214 POLICY_SCOPE_MACHINE, | 207 POLICY_SCOPE_MACHINE, |
215 POLICY_SOURCE_CLOUD, | |
216 new base::FundamentalValue(1), | 208 new base::FundamentalValue(1), |
217 NULL); | 209 NULL); |
218 expected.Set(kPolicy0, | 210 expected.Set(kPolicy0, |
219 POLICY_LEVEL_MANDATORY, | 211 POLICY_LEVEL_MANDATORY, |
220 POLICY_SCOPE_USER, | 212 POLICY_SCOPE_USER, |
221 POLICY_SOURCE_CLOUD, | |
222 new base::FundamentalValue(0), | 213 new base::FundamentalValue(0), |
223 NULL); | 214 NULL); |
224 expected.Set(kPolicy1, | 215 expected.Set(kPolicy1, |
225 POLICY_LEVEL_MANDATORY, | 216 POLICY_LEVEL_MANDATORY, |
226 POLICY_SCOPE_USER, | 217 POLICY_SCOPE_USER, |
227 POLICY_SOURCE_CLOUD, | |
228 new base::FundamentalValue(1), | 218 new base::FundamentalValue(1), |
229 NULL); | 219 NULL); |
230 expected.Set(kPolicy2, | 220 expected.Set(kPolicy2, |
231 POLICY_LEVEL_MANDATORY, | 221 POLICY_LEVEL_MANDATORY, |
232 POLICY_SCOPE_USER, | 222 POLICY_SCOPE_USER, |
233 POLICY_SOURCE_CLOUD, | |
234 new base::FundamentalValue(2), | 223 new base::FundamentalValue(2), |
235 NULL); | 224 NULL); |
236 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, | 225 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, |
237 std::string())).Equals(expected)); | 226 std::string())).Equals(expected)); |
238 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 227 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
239 kExtension3)).Equals(expected)); | 228 kExtension3)).Equals(expected)); |
240 // extension0 comes only from bundle0. | 229 // extension0 comes only from bundle0. |
241 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 230 EXPECT_TRUE(merged.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
242 kExtension0)).Equals(policy0)); | 231 kExtension0)).Equals(policy0)); |
243 // extension1 comes only from bundle1. | 232 // extension1 comes only from bundle1. |
(...skipping 23 matching lines...) Expand all Loading... |
267 EXPECT_TRUE(bundle.Equals(other)); | 256 EXPECT_TRUE(bundle.Equals(other)); |
268 AddTestPolicies(&other.Get( | 257 AddTestPolicies(&other.Get( |
269 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension2))); | 258 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension2))); |
270 EXPECT_FALSE(bundle.Equals(other)); | 259 EXPECT_FALSE(bundle.Equals(other)); |
271 | 260 |
272 other.CopyFrom(bundle); | 261 other.CopyFrom(bundle); |
273 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 262 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
274 .Set(kPolicy0, | 263 .Set(kPolicy0, |
275 POLICY_LEVEL_MANDATORY, | 264 POLICY_LEVEL_MANDATORY, |
276 POLICY_SCOPE_USER, | 265 POLICY_SCOPE_USER, |
277 POLICY_SOURCE_CLOUD, | |
278 new base::FundamentalValue(123), | 266 new base::FundamentalValue(123), |
279 NULL); | 267 NULL); |
280 EXPECT_FALSE(bundle.Equals(other)); | 268 EXPECT_FALSE(bundle.Equals(other)); |
281 other.CopyFrom(bundle); | 269 other.CopyFrom(bundle); |
282 EXPECT_TRUE(bundle.Equals(other)); | 270 EXPECT_TRUE(bundle.Equals(other)); |
283 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 271 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
284 .Set(kPolicy0, | 272 .Set(kPolicy0, |
285 POLICY_LEVEL_MANDATORY, | 273 POLICY_LEVEL_MANDATORY, |
286 POLICY_SCOPE_MACHINE, | 274 POLICY_SCOPE_MACHINE, |
287 POLICY_SOURCE_CLOUD, | |
288 new base::FundamentalValue(123), | 275 new base::FundamentalValue(123), |
289 NULL); | 276 NULL); |
290 EXPECT_FALSE(bundle.Equals(other)); | 277 EXPECT_FALSE(bundle.Equals(other)); |
291 | 278 |
292 // Test non-const Get(). | 279 // Test non-const Get(). |
293 bundle.Clear(); | 280 bundle.Clear(); |
294 other.Clear(); | 281 other.Clear(); |
295 PolicyMap& policy_map = | 282 PolicyMap& policy_map = |
296 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); | 283 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); |
297 EXPECT_TRUE(bundle.Equals(other)); | 284 EXPECT_TRUE(bundle.Equals(other)); |
298 policy_map.Set(kPolicy0, | 285 policy_map.Set(kPolicy0, |
299 POLICY_LEVEL_MANDATORY, | 286 POLICY_LEVEL_MANDATORY, |
300 POLICY_SCOPE_USER, | 287 POLICY_SCOPE_USER, |
301 POLICY_SOURCE_CLOUD, | |
302 new base::FundamentalValue(123), | 288 new base::FundamentalValue(123), |
303 NULL); | 289 NULL); |
304 EXPECT_FALSE(bundle.Equals(other)); | 290 EXPECT_FALSE(bundle.Equals(other)); |
305 } | 291 } |
306 | 292 |
307 } // namespace policy | 293 } // namespace policy |
OLD | NEW |