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
3 // found in the LICENSE file.
5 #include "components/policy/core/common/policy_map.h"
7 #include "base/callback.h"
8 #include "base/memory/weak_ptr.h"
9 #include "components/policy/core/common/external_data_manager.h"
10 #include "components/policy/core/common/policy_types.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 // Dummy policy names.
18 const char kTestPolicyName1
[] = "policy.test.1";
19 const char kTestPolicyName2
[] = "policy.test.2";
20 const char kTestPolicyName3
[] = "policy.test.3";
21 const char kTestPolicyName4
[] = "policy.test.4";
22 const char kTestPolicyName5
[] = "policy.test.5";
23 const char kTestPolicyName6
[] = "policy.test.6";
24 const char kTestPolicyName7
[] = "policy.test.7";
25 const char kTestPolicyName8
[] = "policy.test.8";
27 // Utility functions for the tests.
28 void SetPolicy(PolicyMap
* map
, const char* name
, base::Value
* value
) {
29 map
->Set(name
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
, POLICY_SOURCE_CLOUD
,
33 void SetPolicy(PolicyMap
* map
,
35 ExternalDataFetcher
* external_data_fetcher
) {
36 map
->Set(name
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
, POLICY_SOURCE_CLOUD
,
37 NULL
, external_data_fetcher
);
42 class PolicyMapTest
: public testing::Test
{
44 scoped_ptr
<ExternalDataFetcher
> CreateExternalDataFetcher(
45 const std::string
& policy
) const;
48 scoped_ptr
<ExternalDataFetcher
> PolicyMapTest::CreateExternalDataFetcher(
49 const std::string
& policy
) const {
50 return make_scoped_ptr(
51 new ExternalDataFetcher(base::WeakPtr
<ExternalDataManager
>(), policy
));
54 TEST_F(PolicyMapTest
, SetAndGet
) {
56 SetPolicy(&map
, kTestPolicyName1
, new base::StringValue("aaa"));
57 base::StringValue
expected("aaa");
58 EXPECT_TRUE(expected
.Equals(map
.GetValue(kTestPolicyName1
)));
59 SetPolicy(&map
, kTestPolicyName1
, new base::StringValue("bbb"));
60 base::StringValue
expected_b("bbb");
61 EXPECT_TRUE(expected_b
.Equals(map
.GetValue(kTestPolicyName1
)));
62 SetPolicy(&map
, kTestPolicyName1
,
63 CreateExternalDataFetcher("dummy").release());
64 EXPECT_FALSE(map
.GetValue(kTestPolicyName1
));
65 const PolicyMap::Entry
* entry
= map
.Get(kTestPolicyName1
);
66 ASSERT_TRUE(entry
!= NULL
);
67 EXPECT_EQ(POLICY_LEVEL_MANDATORY
, entry
->level
);
68 EXPECT_EQ(POLICY_SCOPE_USER
, entry
->scope
);
69 EXPECT_EQ(POLICY_SOURCE_CLOUD
, entry
->source
);
70 EXPECT_TRUE(ExternalDataFetcher::Equals(
71 entry
->external_data_fetcher
, CreateExternalDataFetcher("dummy").get()));
72 map
.Set(kTestPolicyName1
, POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_MACHINE
,
73 POLICY_SOURCE_ENTERPRISE_DEFAULT
, nullptr, nullptr);
74 EXPECT_FALSE(map
.GetValue(kTestPolicyName1
));
75 entry
= map
.Get(kTestPolicyName1
);
76 ASSERT_TRUE(entry
!= NULL
);
77 EXPECT_EQ(POLICY_LEVEL_RECOMMENDED
, entry
->level
);
78 EXPECT_EQ(POLICY_SCOPE_MACHINE
, entry
->scope
);
79 EXPECT_EQ(POLICY_SOURCE_ENTERPRISE_DEFAULT
, entry
->source
);
80 EXPECT_FALSE(entry
->external_data_fetcher
);
83 TEST_F(PolicyMapTest
, Equals
) {
85 SetPolicy(&a
, kTestPolicyName1
, new base::StringValue("aaa"));
87 SetPolicy(&a2
, kTestPolicyName1
, new base::StringValue("aaa"));
89 SetPolicy(&b
, kTestPolicyName1
, new base::StringValue("bbb"));
91 SetPolicy(&c
, kTestPolicyName1
, new base::StringValue("aaa"));
92 SetPolicy(&c
, kTestPolicyName2
, new base::FundamentalValue(true));
94 SetPolicy(&d
, kTestPolicyName1
,
95 CreateExternalDataFetcher("ddd").release());
97 SetPolicy(&d2
, kTestPolicyName1
,
98 CreateExternalDataFetcher("ddd").release());
100 SetPolicy(&e
, kTestPolicyName1
,
101 CreateExternalDataFetcher("eee").release());
102 EXPECT_FALSE(a
.Equals(b
));
103 EXPECT_FALSE(a
.Equals(c
));
104 EXPECT_FALSE(a
.Equals(d
));
105 EXPECT_FALSE(a
.Equals(e
));
106 EXPECT_FALSE(b
.Equals(a
));
107 EXPECT_FALSE(b
.Equals(c
));
108 EXPECT_FALSE(b
.Equals(d
));
109 EXPECT_FALSE(b
.Equals(e
));
110 EXPECT_FALSE(c
.Equals(a
));
111 EXPECT_FALSE(c
.Equals(b
));
112 EXPECT_FALSE(c
.Equals(d
));
113 EXPECT_FALSE(c
.Equals(e
));
114 EXPECT_FALSE(d
.Equals(a
));
115 EXPECT_FALSE(d
.Equals(b
));
116 EXPECT_FALSE(d
.Equals(c
));
117 EXPECT_FALSE(d
.Equals(e
));
118 EXPECT_FALSE(e
.Equals(a
));
119 EXPECT_FALSE(e
.Equals(b
));
120 EXPECT_FALSE(e
.Equals(c
));
121 EXPECT_FALSE(e
.Equals(d
));
122 EXPECT_TRUE(a
.Equals(a2
));
123 EXPECT_TRUE(a2
.Equals(a
));
124 EXPECT_TRUE(d
.Equals(d2
));
125 EXPECT_TRUE(d2
.Equals(d
));
128 EXPECT_TRUE(empty1
.Equals(empty2
));
129 EXPECT_TRUE(empty2
.Equals(empty1
));
130 EXPECT_FALSE(empty1
.Equals(a
));
131 EXPECT_FALSE(a
.Equals(empty1
));
134 TEST_F(PolicyMapTest
, Swap
) {
136 SetPolicy(&a
, kTestPolicyName1
, new base::StringValue("aaa"));
137 SetPolicy(&a
, kTestPolicyName2
,
138 CreateExternalDataFetcher("dummy").release());
140 SetPolicy(&b
, kTestPolicyName1
, new base::StringValue("bbb"));
141 SetPolicy(&b
, kTestPolicyName3
, new base::FundamentalValue(true));
144 base::StringValue
expected("bbb");
145 EXPECT_TRUE(expected
.Equals(a
.GetValue(kTestPolicyName1
)));
146 base::FundamentalValue
expected_bool(true);
147 EXPECT_TRUE(expected_bool
.Equals(a
.GetValue(kTestPolicyName3
)));
148 EXPECT_FALSE(a
.GetValue(kTestPolicyName2
));
149 EXPECT_FALSE(a
.Get(kTestPolicyName2
));
150 base::StringValue
expected_a("aaa");
151 EXPECT_TRUE(expected_a
.Equals(b
.GetValue(kTestPolicyName1
)));
152 EXPECT_FALSE(b
.GetValue(kTestPolicyName3
));
153 EXPECT_FALSE(a
.GetValue(kTestPolicyName2
));
154 const PolicyMap::Entry
* entry
= b
.Get(kTestPolicyName2
);
156 EXPECT_TRUE(ExternalDataFetcher::Equals(
157 CreateExternalDataFetcher("dummy").get(), entry
->external_data_fetcher
));
162 EXPECT_TRUE(a
.Equals(empty
));
163 EXPECT_FALSE(b
.Equals(empty
));
166 TEST_F(PolicyMapTest
, MergeFrom
) {
168 a
.Set(kTestPolicyName1
,
169 POLICY_LEVEL_MANDATORY
,
172 new base::StringValue("google.com"),
174 a
.Set(kTestPolicyName2
,
175 POLICY_LEVEL_MANDATORY
,
176 POLICY_SCOPE_MACHINE
,
178 new base::FundamentalValue(true),
180 a
.Set(kTestPolicyName3
,
181 POLICY_LEVEL_MANDATORY
,
182 POLICY_SCOPE_MACHINE
,
183 POLICY_SOURCE_ENTERPRISE_DEFAULT
,
184 NULL
, CreateExternalDataFetcher("a").release());
185 a
.Set(kTestPolicyName4
,
186 POLICY_LEVEL_RECOMMENDED
,
189 new base::FundamentalValue(false),
191 a
.Set(kTestPolicyName5
,
192 POLICY_LEVEL_RECOMMENDED
,
193 POLICY_SCOPE_MACHINE
,
195 new base::StringValue("google.com/q={x}"),
199 b
.Set(kTestPolicyName1
,
200 POLICY_LEVEL_MANDATORY
,
201 POLICY_SCOPE_MACHINE
,
203 new base::StringValue("chromium.org"),
205 b
.Set(kTestPolicyName2
,
206 POLICY_LEVEL_MANDATORY
,
207 POLICY_SCOPE_MACHINE
,
209 new base::FundamentalValue(false),
211 b
.Set(kTestPolicyName3
,
212 POLICY_LEVEL_MANDATORY
,
213 POLICY_SCOPE_MACHINE
,
215 NULL
, CreateExternalDataFetcher("b").release());
216 b
.Set(kTestPolicyName4
,
217 POLICY_LEVEL_RECOMMENDED
,
218 POLICY_SCOPE_MACHINE
,
219 POLICY_SOURCE_ENTERPRISE_OVERRIDE
,
220 new base::FundamentalValue(true),
222 b
.Set(kTestPolicyName5
,
223 POLICY_LEVEL_MANDATORY
,
224 POLICY_SCOPE_MACHINE
,
225 POLICY_SOURCE_PLATFORM
,
226 new base::StringValue(std::string()),
228 b
.Set(kTestPolicyName6
,
229 POLICY_LEVEL_RECOMMENDED
,
232 new base::FundamentalValue(true),
238 // POLICY_SCOPE_MACHINE over POLICY_SCOPE_USER.
239 c
.Set(kTestPolicyName1
,
240 POLICY_LEVEL_MANDATORY
,
241 POLICY_SCOPE_MACHINE
,
243 new base::StringValue("chromium.org"),
245 // |a| has precedence over |b|.
246 c
.Set(kTestPolicyName2
,
247 POLICY_LEVEL_MANDATORY
,
248 POLICY_SCOPE_MACHINE
,
250 new base::FundamentalValue(true),
252 c
.Set(kTestPolicyName3
,
253 POLICY_LEVEL_MANDATORY
,
254 POLICY_SCOPE_MACHINE
,
255 POLICY_SOURCE_ENTERPRISE_DEFAULT
,
256 NULL
, CreateExternalDataFetcher("a").release());
257 // POLICY_SCOPE_MACHINE over POLICY_SCOPE_USER for POLICY_LEVEL_RECOMMENDED.
258 c
.Set(kTestPolicyName4
,
259 POLICY_LEVEL_RECOMMENDED
,
260 POLICY_SCOPE_MACHINE
,
261 POLICY_SOURCE_ENTERPRISE_OVERRIDE
,
262 new base::FundamentalValue(true),
264 // POLICY_LEVEL_MANDATORY over POLICY_LEVEL_RECOMMENDED.
265 c
.Set(kTestPolicyName5
,
266 POLICY_LEVEL_MANDATORY
,
267 POLICY_SCOPE_MACHINE
,
268 POLICY_SOURCE_PLATFORM
,
269 new base::StringValue(std::string()),
272 c
.Set(kTestPolicyName6
,
273 POLICY_LEVEL_RECOMMENDED
,
276 new base::FundamentalValue(true),
279 EXPECT_TRUE(a
.Equals(c
));
282 TEST_F(PolicyMapTest
, GetDifferingKeys
) {
284 a
.Set(kTestPolicyName1
,
285 POLICY_LEVEL_MANDATORY
,
288 new base::StringValue("google.com"),
290 a
.Set(kTestPolicyName2
,
291 POLICY_LEVEL_MANDATORY
,
292 POLICY_SCOPE_MACHINE
,
294 NULL
, CreateExternalDataFetcher("dummy").release());
295 a
.Set(kTestPolicyName3
,
296 POLICY_LEVEL_MANDATORY
,
297 POLICY_SCOPE_MACHINE
,
299 new base::FundamentalValue(true),
301 a
.Set(kTestPolicyName4
,
302 POLICY_LEVEL_MANDATORY
,
303 POLICY_SCOPE_MACHINE
,
305 NULL
, CreateExternalDataFetcher("a").release());
306 a
.Set(kTestPolicyName5
,
307 POLICY_LEVEL_RECOMMENDED
,
310 new base::FundamentalValue(false),
312 a
.Set(kTestPolicyName6
,
313 POLICY_LEVEL_RECOMMENDED
,
314 POLICY_SCOPE_MACHINE
,
316 new base::StringValue("google.com/q={x}"),
318 a
.Set(kTestPolicyName7
,
319 POLICY_LEVEL_MANDATORY
,
322 new base::FundamentalValue(true),
326 b
.Set(kTestPolicyName1
,
327 POLICY_LEVEL_MANDATORY
,
330 new base::StringValue("google.com"),
332 b
.Set(kTestPolicyName2
,
333 POLICY_LEVEL_MANDATORY
,
334 POLICY_SCOPE_MACHINE
,
336 NULL
, CreateExternalDataFetcher("dummy").release());
337 b
.Set(kTestPolicyName3
,
338 POLICY_LEVEL_MANDATORY
,
339 POLICY_SCOPE_MACHINE
,
341 new base::FundamentalValue(false),
343 b
.Set(kTestPolicyName4
,
344 POLICY_LEVEL_MANDATORY
,
345 POLICY_SCOPE_MACHINE
,
347 NULL
, CreateExternalDataFetcher("b").release());
348 b
.Set(kTestPolicyName5
,
349 POLICY_LEVEL_MANDATORY
,
352 new base::FundamentalValue(false),
354 b
.Set(kTestPolicyName6
,
355 POLICY_LEVEL_RECOMMENDED
,
358 new base::StringValue("google.com/q={x}"),
360 b
.Set(kTestPolicyName8
,
361 POLICY_LEVEL_RECOMMENDED
,
364 new base::FundamentalValue(true),
367 std::set
<std::string
> diff
;
368 std::set
<std::string
> diff2
;
369 a
.GetDifferingKeys(b
, &diff
);
370 b
.GetDifferingKeys(a
, &diff2
);
371 // Order shouldn't matter.
372 EXPECT_EQ(diff
, diff2
);
374 EXPECT_TRUE(diff
.find(kTestPolicyName1
) == diff
.end());
375 EXPECT_TRUE(diff
.find(kTestPolicyName2
) == diff
.end());
377 EXPECT_TRUE(diff
.find(kTestPolicyName3
) != diff
.end());
378 // Different external data references.
379 EXPECT_TRUE(diff
.find(kTestPolicyName4
) != diff
.end());
381 EXPECT_TRUE(diff
.find(kTestPolicyName5
) != diff
.end());
383 EXPECT_TRUE(diff
.find(kTestPolicyName6
) != diff
.end());
385 EXPECT_TRUE(diff
.find(kTestPolicyName8
) != diff
.end());
387 EXPECT_TRUE(diff
.find(kTestPolicyName7
) != diff
.end());
389 EXPECT_EQ(6u, diff
.size());
392 TEST_F(PolicyMapTest
, LoadFromSetsLevelScopeAndSource
) {
393 base::DictionaryValue policies
;
394 policies
.SetString("TestPolicy1", "google.com");
395 policies
.SetBoolean("TestPolicy2", true);
396 policies
.SetInteger("TestPolicy3", -12321);
399 loaded
.LoadFrom(&policies
,
400 POLICY_LEVEL_MANDATORY
,
402 POLICY_SOURCE_PLATFORM
);
405 expected
.Set("TestPolicy1",
406 POLICY_LEVEL_MANDATORY
,
408 POLICY_SOURCE_PLATFORM
,
409 new base::StringValue("google.com"),
411 expected
.Set("TestPolicy2",
412 POLICY_LEVEL_MANDATORY
,
414 POLICY_SOURCE_PLATFORM
,
415 new base::FundamentalValue(true),
417 expected
.Set("TestPolicy3",
418 POLICY_LEVEL_MANDATORY
,
420 POLICY_SOURCE_PLATFORM
,
421 new base::FundamentalValue(-12321),
423 EXPECT_TRUE(loaded
.Equals(expected
));
426 } // namespace policy