Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / policy / core / common / policy_map_unittest.cc
blob58dfcac054b4653347e778e2862de257e5dde029
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"
13 namespace policy {
15 namespace {
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,
30 value, nullptr);
33 void SetPolicy(PolicyMap* map,
34 const char* name,
35 ExternalDataFetcher* external_data_fetcher) {
36 map->Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
37 NULL, external_data_fetcher);
40 } // namespace
42 class PolicyMapTest : public testing::Test {
43 protected:
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) {
55 PolicyMap map;
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) {
84 PolicyMap a;
85 SetPolicy(&a, kTestPolicyName1, new base::StringValue("aaa"));
86 PolicyMap a2;
87 SetPolicy(&a2, kTestPolicyName1, new base::StringValue("aaa"));
88 PolicyMap b;
89 SetPolicy(&b, kTestPolicyName1, new base::StringValue("bbb"));
90 PolicyMap c;
91 SetPolicy(&c, kTestPolicyName1, new base::StringValue("aaa"));
92 SetPolicy(&c, kTestPolicyName2, new base::FundamentalValue(true));
93 PolicyMap d;
94 SetPolicy(&d, kTestPolicyName1,
95 CreateExternalDataFetcher("ddd").release());
96 PolicyMap d2;
97 SetPolicy(&d2, kTestPolicyName1,
98 CreateExternalDataFetcher("ddd").release());
99 PolicyMap e;
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));
126 PolicyMap empty1;
127 PolicyMap empty2;
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) {
135 PolicyMap a;
136 SetPolicy(&a, kTestPolicyName1, new base::StringValue("aaa"));
137 SetPolicy(&a, kTestPolicyName2,
138 CreateExternalDataFetcher("dummy").release());
139 PolicyMap b;
140 SetPolicy(&b, kTestPolicyName1, new base::StringValue("bbb"));
141 SetPolicy(&b, kTestPolicyName3, new base::FundamentalValue(true));
143 a.Swap(&b);
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);
155 ASSERT_TRUE(entry);
156 EXPECT_TRUE(ExternalDataFetcher::Equals(
157 CreateExternalDataFetcher("dummy").get(), entry->external_data_fetcher));
159 b.Clear();
160 a.Swap(&b);
161 PolicyMap empty;
162 EXPECT_TRUE(a.Equals(empty));
163 EXPECT_FALSE(b.Equals(empty));
166 TEST_F(PolicyMapTest, MergeFrom) {
167 PolicyMap a;
168 a.Set(kTestPolicyName1,
169 POLICY_LEVEL_MANDATORY,
170 POLICY_SCOPE_USER,
171 POLICY_SOURCE_CLOUD,
172 new base::StringValue("google.com"),
173 NULL);
174 a.Set(kTestPolicyName2,
175 POLICY_LEVEL_MANDATORY,
176 POLICY_SCOPE_MACHINE,
177 POLICY_SOURCE_CLOUD,
178 new base::FundamentalValue(true),
179 NULL);
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,
187 POLICY_SCOPE_USER,
188 POLICY_SOURCE_CLOUD,
189 new base::FundamentalValue(false),
190 NULL);
191 a.Set(kTestPolicyName5,
192 POLICY_LEVEL_RECOMMENDED,
193 POLICY_SCOPE_MACHINE,
194 POLICY_SOURCE_CLOUD,
195 new base::StringValue("google.com/q={x}"),
196 NULL);
198 PolicyMap b;
199 b.Set(kTestPolicyName1,
200 POLICY_LEVEL_MANDATORY,
201 POLICY_SCOPE_MACHINE,
202 POLICY_SOURCE_CLOUD,
203 new base::StringValue("chromium.org"),
204 NULL);
205 b.Set(kTestPolicyName2,
206 POLICY_LEVEL_MANDATORY,
207 POLICY_SCOPE_MACHINE,
208 POLICY_SOURCE_CLOUD,
209 new base::FundamentalValue(false),
210 NULL);
211 b.Set(kTestPolicyName3,
212 POLICY_LEVEL_MANDATORY,
213 POLICY_SCOPE_MACHINE,
214 POLICY_SOURCE_CLOUD,
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),
221 NULL);
222 b.Set(kTestPolicyName5,
223 POLICY_LEVEL_MANDATORY,
224 POLICY_SCOPE_MACHINE,
225 POLICY_SOURCE_PLATFORM,
226 new base::StringValue(std::string()),
227 NULL);
228 b.Set(kTestPolicyName6,
229 POLICY_LEVEL_RECOMMENDED,
230 POLICY_SCOPE_USER,
231 POLICY_SOURCE_CLOUD,
232 new base::FundamentalValue(true),
233 NULL);
235 a.MergeFrom(b);
237 PolicyMap c;
238 // POLICY_SCOPE_MACHINE over POLICY_SCOPE_USER.
239 c.Set(kTestPolicyName1,
240 POLICY_LEVEL_MANDATORY,
241 POLICY_SCOPE_MACHINE,
242 POLICY_SOURCE_CLOUD,
243 new base::StringValue("chromium.org"),
244 NULL);
245 // |a| has precedence over |b|.
246 c.Set(kTestPolicyName2,
247 POLICY_LEVEL_MANDATORY,
248 POLICY_SCOPE_MACHINE,
249 POLICY_SOURCE_CLOUD,
250 new base::FundamentalValue(true),
251 NULL);
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),
263 NULL);
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()),
270 NULL);
271 // Merge new ones.
272 c.Set(kTestPolicyName6,
273 POLICY_LEVEL_RECOMMENDED,
274 POLICY_SCOPE_USER,
275 POLICY_SOURCE_CLOUD,
276 new base::FundamentalValue(true),
277 NULL);
279 EXPECT_TRUE(a.Equals(c));
282 TEST_F(PolicyMapTest, GetDifferingKeys) {
283 PolicyMap a;
284 a.Set(kTestPolicyName1,
285 POLICY_LEVEL_MANDATORY,
286 POLICY_SCOPE_USER,
287 POLICY_SOURCE_CLOUD,
288 new base::StringValue("google.com"),
289 NULL);
290 a.Set(kTestPolicyName2,
291 POLICY_LEVEL_MANDATORY,
292 POLICY_SCOPE_MACHINE,
293 POLICY_SOURCE_CLOUD,
294 NULL, CreateExternalDataFetcher("dummy").release());
295 a.Set(kTestPolicyName3,
296 POLICY_LEVEL_MANDATORY,
297 POLICY_SCOPE_MACHINE,
298 POLICY_SOURCE_CLOUD,
299 new base::FundamentalValue(true),
300 NULL);
301 a.Set(kTestPolicyName4,
302 POLICY_LEVEL_MANDATORY,
303 POLICY_SCOPE_MACHINE,
304 POLICY_SOURCE_CLOUD,
305 NULL, CreateExternalDataFetcher("a").release());
306 a.Set(kTestPolicyName5,
307 POLICY_LEVEL_RECOMMENDED,
308 POLICY_SCOPE_USER,
309 POLICY_SOURCE_CLOUD,
310 new base::FundamentalValue(false),
311 NULL);
312 a.Set(kTestPolicyName6,
313 POLICY_LEVEL_RECOMMENDED,
314 POLICY_SCOPE_MACHINE,
315 POLICY_SOURCE_CLOUD,
316 new base::StringValue("google.com/q={x}"),
317 NULL);
318 a.Set(kTestPolicyName7,
319 POLICY_LEVEL_MANDATORY,
320 POLICY_SCOPE_USER,
321 POLICY_SOURCE_CLOUD,
322 new base::FundamentalValue(true),
323 NULL);
325 PolicyMap b;
326 b.Set(kTestPolicyName1,
327 POLICY_LEVEL_MANDATORY,
328 POLICY_SCOPE_USER,
329 POLICY_SOURCE_CLOUD,
330 new base::StringValue("google.com"),
331 NULL);
332 b.Set(kTestPolicyName2,
333 POLICY_LEVEL_MANDATORY,
334 POLICY_SCOPE_MACHINE,
335 POLICY_SOURCE_CLOUD,
336 NULL, CreateExternalDataFetcher("dummy").release());
337 b.Set(kTestPolicyName3,
338 POLICY_LEVEL_MANDATORY,
339 POLICY_SCOPE_MACHINE,
340 POLICY_SOURCE_CLOUD,
341 new base::FundamentalValue(false),
342 NULL);
343 b.Set(kTestPolicyName4,
344 POLICY_LEVEL_MANDATORY,
345 POLICY_SCOPE_MACHINE,
346 POLICY_SOURCE_CLOUD,
347 NULL, CreateExternalDataFetcher("b").release());
348 b.Set(kTestPolicyName5,
349 POLICY_LEVEL_MANDATORY,
350 POLICY_SCOPE_USER,
351 POLICY_SOURCE_CLOUD,
352 new base::FundamentalValue(false),
353 NULL);
354 b.Set(kTestPolicyName6,
355 POLICY_LEVEL_RECOMMENDED,
356 POLICY_SCOPE_USER,
357 POLICY_SOURCE_CLOUD,
358 new base::StringValue("google.com/q={x}"),
359 NULL);
360 b.Set(kTestPolicyName8,
361 POLICY_LEVEL_RECOMMENDED,
362 POLICY_SCOPE_USER,
363 POLICY_SOURCE_CLOUD,
364 new base::FundamentalValue(true),
365 NULL);
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);
373 // No change.
374 EXPECT_TRUE(diff.find(kTestPolicyName1) == diff.end());
375 EXPECT_TRUE(diff.find(kTestPolicyName2) == diff.end());
376 // Different values.
377 EXPECT_TRUE(diff.find(kTestPolicyName3) != diff.end());
378 // Different external data references.
379 EXPECT_TRUE(diff.find(kTestPolicyName4) != diff.end());
380 // Different levels.
381 EXPECT_TRUE(diff.find(kTestPolicyName5) != diff.end());
382 // Different scopes.
383 EXPECT_TRUE(diff.find(kTestPolicyName6) != diff.end());
384 // Not in |a|.
385 EXPECT_TRUE(diff.find(kTestPolicyName8) != diff.end());
386 // Not in |b|.
387 EXPECT_TRUE(diff.find(kTestPolicyName7) != diff.end());
388 // No surprises.
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);
398 PolicyMap loaded;
399 loaded.LoadFrom(&policies,
400 POLICY_LEVEL_MANDATORY,
401 POLICY_SCOPE_USER,
402 POLICY_SOURCE_PLATFORM);
404 PolicyMap expected;
405 expected.Set("TestPolicy1",
406 POLICY_LEVEL_MANDATORY,
407 POLICY_SCOPE_USER,
408 POLICY_SOURCE_PLATFORM,
409 new base::StringValue("google.com"),
410 nullptr);
411 expected.Set("TestPolicy2",
412 POLICY_LEVEL_MANDATORY,
413 POLICY_SCOPE_USER,
414 POLICY_SOURCE_PLATFORM,
415 new base::FundamentalValue(true),
416 nullptr);
417 expected.Set("TestPolicy3",
418 POLICY_LEVEL_MANDATORY,
419 POLICY_SCOPE_USER,
420 POLICY_SOURCE_PLATFORM,
421 new base::FundamentalValue(-12321),
422 nullptr);
423 EXPECT_TRUE(loaded.Equals(expected));
426 } // namespace policy