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 "testing/gtest/include/gtest/gtest.h"
16 // Dummy policy names.
17 const char kTestPolicyName1
[] = "policy.test.1";
18 const char kTestPolicyName2
[] = "policy.test.2";
19 const char kTestPolicyName3
[] = "policy.test.3";
20 const char kTestPolicyName4
[] = "policy.test.4";
21 const char kTestPolicyName5
[] = "policy.test.5";
22 const char kTestPolicyName6
[] = "policy.test.6";
23 const char kTestPolicyName7
[] = "policy.test.7";
24 const char kTestPolicyName8
[] = "policy.test.8";
26 // Utility functions for the tests.
27 void SetPolicy(PolicyMap
* map
, const char* name
, base::Value
* value
) {
28 map
->Set(name
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
, value
, NULL
);
31 void SetPolicy(PolicyMap
* map
,
33 ExternalDataFetcher
* external_data_fetcher
) {
34 map
->Set(name
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
35 NULL
, external_data_fetcher
);
40 class PolicyMapTest
: public testing::Test
{
42 scoped_ptr
<ExternalDataFetcher
> CreateExternalDataFetcher(
43 const std::string
& policy
) const;
46 scoped_ptr
<ExternalDataFetcher
> PolicyMapTest::CreateExternalDataFetcher(
47 const std::string
& policy
) const {
48 return make_scoped_ptr(
49 new ExternalDataFetcher(base::WeakPtr
<ExternalDataManager
>(), policy
));
52 TEST_F(PolicyMapTest
, SetAndGet
) {
54 SetPolicy(&map
, kTestPolicyName1
, new base::StringValue("aaa"));
55 base::StringValue
expected("aaa");
56 EXPECT_TRUE(expected
.Equals(map
.GetValue(kTestPolicyName1
)));
57 SetPolicy(&map
, kTestPolicyName1
, new base::StringValue("bbb"));
58 base::StringValue
expected_b("bbb");
59 EXPECT_TRUE(expected_b
.Equals(map
.GetValue(kTestPolicyName1
)));
60 SetPolicy(&map
, kTestPolicyName1
,
61 CreateExternalDataFetcher("dummy").release());
62 EXPECT_FALSE(map
.GetValue(kTestPolicyName1
));
63 const PolicyMap::Entry
* entry
= map
.Get(kTestPolicyName1
);
64 ASSERT_TRUE(entry
!= NULL
);
65 EXPECT_EQ(POLICY_LEVEL_MANDATORY
, entry
->level
);
66 EXPECT_EQ(POLICY_SCOPE_USER
, entry
->scope
);
67 EXPECT_TRUE(ExternalDataFetcher::Equals(
68 entry
->external_data_fetcher
, CreateExternalDataFetcher("dummy").get()));
69 map
.Set(kTestPolicyName1
, POLICY_LEVEL_RECOMMENDED
,
70 POLICY_SCOPE_MACHINE
, NULL
, NULL
);
71 EXPECT_FALSE(map
.GetValue(kTestPolicyName1
));
72 entry
= map
.Get(kTestPolicyName1
);
73 ASSERT_TRUE(entry
!= NULL
);
74 EXPECT_EQ(POLICY_LEVEL_RECOMMENDED
, entry
->level
);
75 EXPECT_EQ(POLICY_SCOPE_MACHINE
, entry
->scope
);
76 EXPECT_FALSE(entry
->external_data_fetcher
);
79 TEST_F(PolicyMapTest
, Equals
) {
81 SetPolicy(&a
, kTestPolicyName1
, new base::StringValue("aaa"));
83 SetPolicy(&a2
, kTestPolicyName1
, new base::StringValue("aaa"));
85 SetPolicy(&b
, kTestPolicyName1
, new base::StringValue("bbb"));
87 SetPolicy(&c
, kTestPolicyName1
, new base::StringValue("aaa"));
88 SetPolicy(&c
, kTestPolicyName2
, new base::FundamentalValue(true));
90 SetPolicy(&d
, kTestPolicyName1
,
91 CreateExternalDataFetcher("ddd").release());
93 SetPolicy(&d2
, kTestPolicyName1
,
94 CreateExternalDataFetcher("ddd").release());
96 SetPolicy(&e
, kTestPolicyName1
,
97 CreateExternalDataFetcher("eee").release());
98 EXPECT_FALSE(a
.Equals(b
));
99 EXPECT_FALSE(a
.Equals(c
));
100 EXPECT_FALSE(a
.Equals(d
));
101 EXPECT_FALSE(a
.Equals(e
));
102 EXPECT_FALSE(b
.Equals(a
));
103 EXPECT_FALSE(b
.Equals(c
));
104 EXPECT_FALSE(b
.Equals(d
));
105 EXPECT_FALSE(b
.Equals(e
));
106 EXPECT_FALSE(c
.Equals(a
));
107 EXPECT_FALSE(c
.Equals(b
));
108 EXPECT_FALSE(c
.Equals(d
));
109 EXPECT_FALSE(c
.Equals(e
));
110 EXPECT_FALSE(d
.Equals(a
));
111 EXPECT_FALSE(d
.Equals(b
));
112 EXPECT_FALSE(d
.Equals(c
));
113 EXPECT_FALSE(d
.Equals(e
));
114 EXPECT_FALSE(e
.Equals(a
));
115 EXPECT_FALSE(e
.Equals(b
));
116 EXPECT_FALSE(e
.Equals(c
));
117 EXPECT_FALSE(e
.Equals(d
));
118 EXPECT_TRUE(a
.Equals(a2
));
119 EXPECT_TRUE(a2
.Equals(a
));
120 EXPECT_TRUE(d
.Equals(d2
));
121 EXPECT_TRUE(d2
.Equals(d
));
124 EXPECT_TRUE(empty1
.Equals(empty2
));
125 EXPECT_TRUE(empty2
.Equals(empty1
));
126 EXPECT_FALSE(empty1
.Equals(a
));
127 EXPECT_FALSE(a
.Equals(empty1
));
130 TEST_F(PolicyMapTest
, Swap
) {
132 SetPolicy(&a
, kTestPolicyName1
, new base::StringValue("aaa"));
133 SetPolicy(&a
, kTestPolicyName2
,
134 CreateExternalDataFetcher("dummy").release());
136 SetPolicy(&b
, kTestPolicyName1
, new base::StringValue("bbb"));
137 SetPolicy(&b
, kTestPolicyName3
, new base::FundamentalValue(true));
140 base::StringValue
expected("bbb");
141 EXPECT_TRUE(expected
.Equals(a
.GetValue(kTestPolicyName1
)));
142 base::FundamentalValue
expected_bool(true);
143 EXPECT_TRUE(expected_bool
.Equals(a
.GetValue(kTestPolicyName3
)));
144 EXPECT_FALSE(a
.GetValue(kTestPolicyName2
));
145 EXPECT_FALSE(a
.Get(kTestPolicyName2
));
146 base::StringValue
expected_a("aaa");
147 EXPECT_TRUE(expected_a
.Equals(b
.GetValue(kTestPolicyName1
)));
148 EXPECT_FALSE(b
.GetValue(kTestPolicyName3
));
149 EXPECT_FALSE(a
.GetValue(kTestPolicyName2
));
150 const PolicyMap::Entry
* entry
= b
.Get(kTestPolicyName2
);
152 EXPECT_TRUE(ExternalDataFetcher::Equals(
153 CreateExternalDataFetcher("dummy").get(), entry
->external_data_fetcher
));
158 EXPECT_TRUE(a
.Equals(empty
));
159 EXPECT_FALSE(b
.Equals(empty
));
162 TEST_F(PolicyMapTest
, MergeFrom
) {
164 a
.Set(kTestPolicyName1
,
165 POLICY_LEVEL_MANDATORY
,
167 new base::StringValue("google.com"),
169 a
.Set(kTestPolicyName2
,
170 POLICY_LEVEL_MANDATORY
,
171 POLICY_SCOPE_MACHINE
,
172 new base::FundamentalValue(true),
174 a
.Set(kTestPolicyName3
,
175 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_MACHINE
,
176 NULL
, CreateExternalDataFetcher("a").release());
177 a
.Set(kTestPolicyName4
,
178 POLICY_LEVEL_RECOMMENDED
,
180 new base::FundamentalValue(false),
182 a
.Set(kTestPolicyName5
,
183 POLICY_LEVEL_RECOMMENDED
,
184 POLICY_SCOPE_MACHINE
,
185 new base::StringValue("google.com/q={x}"),
189 b
.Set(kTestPolicyName1
,
190 POLICY_LEVEL_MANDATORY
,
191 POLICY_SCOPE_MACHINE
,
192 new base::StringValue("chromium.org"),
194 b
.Set(kTestPolicyName2
,
195 POLICY_LEVEL_MANDATORY
,
196 POLICY_SCOPE_MACHINE
,
197 new base::FundamentalValue(false),
199 b
.Set(kTestPolicyName3
,
200 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_MACHINE
,
201 NULL
, CreateExternalDataFetcher("b").release());
202 b
.Set(kTestPolicyName4
,
203 POLICY_LEVEL_RECOMMENDED
,
204 POLICY_SCOPE_MACHINE
,
205 new base::FundamentalValue(true),
207 b
.Set(kTestPolicyName5
,
208 POLICY_LEVEL_MANDATORY
,
209 POLICY_SCOPE_MACHINE
,
210 new base::StringValue(std::string()),
212 b
.Set(kTestPolicyName6
,
213 POLICY_LEVEL_RECOMMENDED
,
215 new base::FundamentalValue(true),
221 // POLICY_SCOPE_MACHINE over POLICY_SCOPE_USER.
222 c
.Set(kTestPolicyName1
,
223 POLICY_LEVEL_MANDATORY
,
224 POLICY_SCOPE_MACHINE
,
225 new base::StringValue("chromium.org"),
227 // |a| has precedence over |b|.
228 c
.Set(kTestPolicyName2
,
229 POLICY_LEVEL_MANDATORY
,
230 POLICY_SCOPE_MACHINE
,
231 new base::FundamentalValue(true),
233 c
.Set(kTestPolicyName3
,
234 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_MACHINE
,
235 NULL
, CreateExternalDataFetcher("a").release());
236 // POLICY_SCOPE_MACHINE over POLICY_SCOPE_USER for POLICY_LEVEL_RECOMMENDED.
237 c
.Set(kTestPolicyName4
,
238 POLICY_LEVEL_RECOMMENDED
,
239 POLICY_SCOPE_MACHINE
,
240 new base::FundamentalValue(true),
242 // POLICY_LEVEL_MANDATORY over POLICY_LEVEL_RECOMMENDED.
243 c
.Set(kTestPolicyName5
,
244 POLICY_LEVEL_MANDATORY
,
245 POLICY_SCOPE_MACHINE
,
246 new base::StringValue(std::string()),
249 c
.Set(kTestPolicyName6
,
250 POLICY_LEVEL_RECOMMENDED
,
252 new base::FundamentalValue(true),
255 EXPECT_TRUE(a
.Equals(c
));
258 TEST_F(PolicyMapTest
, GetDifferingKeys
) {
260 a
.Set(kTestPolicyName1
,
261 POLICY_LEVEL_MANDATORY
,
263 new base::StringValue("google.com"),
265 a
.Set(kTestPolicyName2
,
266 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_MACHINE
,
267 NULL
, CreateExternalDataFetcher("dummy").release());
268 a
.Set(kTestPolicyName3
,
269 POLICY_LEVEL_MANDATORY
,
270 POLICY_SCOPE_MACHINE
,
271 new base::FundamentalValue(true),
273 a
.Set(kTestPolicyName4
,
274 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_MACHINE
,
275 NULL
, CreateExternalDataFetcher("a").release());
276 a
.Set(kTestPolicyName5
,
277 POLICY_LEVEL_RECOMMENDED
,
279 new base::FundamentalValue(false),
281 a
.Set(kTestPolicyName6
,
282 POLICY_LEVEL_RECOMMENDED
,
283 POLICY_SCOPE_MACHINE
,
284 new base::StringValue("google.com/q={x}"),
286 a
.Set(kTestPolicyName7
,
287 POLICY_LEVEL_MANDATORY
,
289 new base::FundamentalValue(true),
293 b
.Set(kTestPolicyName1
,
294 POLICY_LEVEL_MANDATORY
,
296 new base::StringValue("google.com"),
298 b
.Set(kTestPolicyName2
,
299 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_MACHINE
,
300 NULL
, CreateExternalDataFetcher("dummy").release());
301 b
.Set(kTestPolicyName3
,
302 POLICY_LEVEL_MANDATORY
,
303 POLICY_SCOPE_MACHINE
,
304 new base::FundamentalValue(false),
306 b
.Set(kTestPolicyName4
,
307 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_MACHINE
,
308 NULL
, CreateExternalDataFetcher("b").release());
309 b
.Set(kTestPolicyName5
,
310 POLICY_LEVEL_MANDATORY
,
312 new base::FundamentalValue(false),
314 b
.Set(kTestPolicyName6
,
315 POLICY_LEVEL_RECOMMENDED
,
317 new base::StringValue("google.com/q={x}"),
319 b
.Set(kTestPolicyName8
,
320 POLICY_LEVEL_RECOMMENDED
,
322 new base::FundamentalValue(true),
325 std::set
<std::string
> diff
;
326 std::set
<std::string
> diff2
;
327 a
.GetDifferingKeys(b
, &diff
);
328 b
.GetDifferingKeys(a
, &diff2
);
329 // Order shouldn't matter.
330 EXPECT_EQ(diff
, diff2
);
332 EXPECT_TRUE(diff
.find(kTestPolicyName1
) == diff
.end());
333 EXPECT_TRUE(diff
.find(kTestPolicyName2
) == diff
.end());
335 EXPECT_TRUE(diff
.find(kTestPolicyName3
) != diff
.end());
336 // Different external data references.
337 EXPECT_TRUE(diff
.find(kTestPolicyName4
) != diff
.end());
339 EXPECT_TRUE(diff
.find(kTestPolicyName5
) != diff
.end());
341 EXPECT_TRUE(diff
.find(kTestPolicyName6
) != diff
.end());
343 EXPECT_TRUE(diff
.find(kTestPolicyName8
) != diff
.end());
345 EXPECT_TRUE(diff
.find(kTestPolicyName7
) != diff
.end());
347 EXPECT_EQ(6u, diff
.size());
350 } // namespace policy