OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "chrome/browser/policy/policy_map.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/memory/weak_ptr.h" | |
9 #include "chrome/browser/policy/external_data_manager.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace policy { | |
13 | |
14 namespace { | |
15 | |
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"; | |
25 | |
26 // Utility functions for the tests. | |
27 void SetPolicy(PolicyMap* map, const char* name, Value* value) { | |
28 map->Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, value, NULL); | |
29 } | |
30 | |
31 void SetPolicy(PolicyMap* map, | |
32 const char* name, | |
33 ExternalDataFetcher* external_data_fetcher) { | |
34 map->Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
35 NULL, external_data_fetcher); | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 class PolicyMapTest : public testing::Test { | |
41 protected: | |
42 scoped_ptr<ExternalDataFetcher> CreateExternalDataFetcher( | |
43 const std::string& policy) const; | |
44 }; | |
45 | |
46 scoped_ptr<ExternalDataFetcher> PolicyMapTest::CreateExternalDataFetcher( | |
47 const std::string& policy) const { | |
48 return make_scoped_ptr( | |
49 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(), policy)); | |
50 } | |
51 | |
52 TEST_F(PolicyMapTest, SetAndGet) { | |
53 PolicyMap map; | |
54 SetPolicy(&map, kTestPolicyName1, Value::CreateStringValue("aaa")); | |
55 StringValue expected("aaa"); | |
56 EXPECT_TRUE(expected.Equals(map.GetValue(kTestPolicyName1))); | |
57 SetPolicy(&map, kTestPolicyName1, Value::CreateStringValue("bbb")); | |
58 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); | |
77 } | |
78 | |
79 TEST_F(PolicyMapTest, Equals) { | |
80 PolicyMap a; | |
81 SetPolicy(&a, kTestPolicyName1, Value::CreateStringValue("aaa")); | |
82 PolicyMap a2; | |
83 SetPolicy(&a2, kTestPolicyName1, Value::CreateStringValue("aaa")); | |
84 PolicyMap b; | |
85 SetPolicy(&b, kTestPolicyName1, Value::CreateStringValue("bbb")); | |
86 PolicyMap c; | |
87 SetPolicy(&c, kTestPolicyName1, Value::CreateStringValue("aaa")); | |
88 SetPolicy(&c, kTestPolicyName2, Value::CreateBooleanValue(true)); | |
89 PolicyMap d; | |
90 SetPolicy(&d, kTestPolicyName1, | |
91 CreateExternalDataFetcher("ddd").release()); | |
92 PolicyMap d2; | |
93 SetPolicy(&d2, kTestPolicyName1, | |
94 CreateExternalDataFetcher("ddd").release()); | |
95 PolicyMap e; | |
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)); | |
122 PolicyMap empty1; | |
123 PolicyMap empty2; | |
124 EXPECT_TRUE(empty1.Equals(empty2)); | |
125 EXPECT_TRUE(empty2.Equals(empty1)); | |
126 EXPECT_FALSE(empty1.Equals(a)); | |
127 EXPECT_FALSE(a.Equals(empty1)); | |
128 } | |
129 | |
130 TEST_F(PolicyMapTest, Swap) { | |
131 PolicyMap a; | |
132 SetPolicy(&a, kTestPolicyName1, Value::CreateStringValue("aaa")); | |
133 SetPolicy(&a, kTestPolicyName2, | |
134 CreateExternalDataFetcher("dummy").release()); | |
135 PolicyMap b; | |
136 SetPolicy(&b, kTestPolicyName1, Value::CreateStringValue("bbb")); | |
137 SetPolicy(&b, kTestPolicyName3, Value::CreateBooleanValue(true)); | |
138 | |
139 a.Swap(&b); | |
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 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); | |
151 ASSERT_TRUE(entry); | |
152 EXPECT_TRUE(ExternalDataFetcher::Equals( | |
153 CreateExternalDataFetcher("dummy").get(), entry->external_data_fetcher)); | |
154 | |
155 b.Clear(); | |
156 a.Swap(&b); | |
157 PolicyMap empty; | |
158 EXPECT_TRUE(a.Equals(empty)); | |
159 EXPECT_FALSE(b.Equals(empty)); | |
160 } | |
161 | |
162 TEST_F(PolicyMapTest, MergeFrom) { | |
163 PolicyMap a; | |
164 a.Set(kTestPolicyName1, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
165 Value::CreateStringValue("google.com"), NULL); | |
166 a.Set(kTestPolicyName2, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
167 Value::CreateBooleanValue(true), NULL); | |
168 a.Set(kTestPolicyName3, | |
169 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
170 NULL, CreateExternalDataFetcher("a").release()); | |
171 a.Set(kTestPolicyName4, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | |
172 Value::CreateBooleanValue(false), NULL); | |
173 a.Set(kTestPolicyName5, | |
174 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_MACHINE, | |
175 Value::CreateStringValue("google.com/q={x}"), NULL); | |
176 | |
177 PolicyMap b; | |
178 b.Set(kTestPolicyName1, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
179 Value::CreateStringValue("chromium.org"), NULL); | |
180 b.Set(kTestPolicyName2, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
181 Value::CreateBooleanValue(false), NULL); | |
182 b.Set(kTestPolicyName3, | |
183 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
184 NULL, CreateExternalDataFetcher("b").release()); | |
185 b.Set(kTestPolicyName4, POLICY_LEVEL_RECOMMENDED, | |
186 POLICY_SCOPE_MACHINE, Value::CreateBooleanValue(true), NULL); | |
187 b.Set(kTestPolicyName5, | |
188 POLICY_LEVEL_MANDATORY, | |
189 POLICY_SCOPE_MACHINE, | |
190 Value::CreateStringValue(std::string()), | |
191 NULL); | |
192 b.Set(kTestPolicyName6, | |
193 POLICY_LEVEL_RECOMMENDED, | |
194 POLICY_SCOPE_USER, | |
195 Value::CreateBooleanValue(true), | |
196 NULL); | |
197 | |
198 a.MergeFrom(b); | |
199 | |
200 PolicyMap c; | |
201 // POLICY_SCOPE_MACHINE over POLICY_SCOPE_USER. | |
202 c.Set(kTestPolicyName1, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
203 Value::CreateStringValue("chromium.org"), NULL); | |
204 // |a| has precedence over |b|. | |
205 c.Set(kTestPolicyName2, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
206 Value::CreateBooleanValue(true), NULL); | |
207 c.Set(kTestPolicyName3, | |
208 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
209 NULL, CreateExternalDataFetcher("a").release()); | |
210 // POLICY_SCOPE_MACHINE over POLICY_SCOPE_USER for POLICY_LEVEL_RECOMMENDED. | |
211 c.Set(kTestPolicyName4, POLICY_LEVEL_RECOMMENDED, | |
212 POLICY_SCOPE_MACHINE, Value::CreateBooleanValue(true), NULL); | |
213 // POLICY_LEVEL_MANDATORY over POLICY_LEVEL_RECOMMENDED. | |
214 c.Set(kTestPolicyName5, | |
215 POLICY_LEVEL_MANDATORY, | |
216 POLICY_SCOPE_MACHINE, | |
217 Value::CreateStringValue(std::string()), | |
218 NULL); | |
219 // Merge new ones. | |
220 c.Set(kTestPolicyName6, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | |
221 Value::CreateBooleanValue(true), NULL); | |
222 | |
223 EXPECT_TRUE(a.Equals(c)); | |
224 } | |
225 | |
226 TEST_F(PolicyMapTest, GetDifferingKeys) { | |
227 PolicyMap a; | |
228 a.Set(kTestPolicyName1, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
229 Value::CreateStringValue("google.com"), NULL); | |
230 a.Set(kTestPolicyName2, | |
231 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
232 NULL, CreateExternalDataFetcher("dummy").release()); | |
233 a.Set(kTestPolicyName3, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
234 Value::CreateBooleanValue(true), NULL); | |
235 a.Set(kTestPolicyName4, | |
236 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
237 NULL, CreateExternalDataFetcher("a").release()); | |
238 a.Set(kTestPolicyName5, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | |
239 Value::CreateBooleanValue(false), NULL); | |
240 a.Set(kTestPolicyName6, | |
241 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_MACHINE, | |
242 Value::CreateStringValue("google.com/q={x}"), NULL); | |
243 a.Set(kTestPolicyName7, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
244 Value::CreateBooleanValue(true), NULL); | |
245 | |
246 PolicyMap b; | |
247 b.Set(kTestPolicyName1, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
248 Value::CreateStringValue("google.com"), NULL); | |
249 b.Set(kTestPolicyName2, | |
250 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
251 NULL, CreateExternalDataFetcher("dummy").release()); | |
252 b.Set(kTestPolicyName3, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
253 Value::CreateBooleanValue(false), NULL); | |
254 b.Set(kTestPolicyName4, | |
255 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
256 NULL, CreateExternalDataFetcher("b").release()); | |
257 b.Set(kTestPolicyName5, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
258 Value::CreateBooleanValue(false), NULL); | |
259 b.Set(kTestPolicyName6, POLICY_LEVEL_RECOMMENDED, | |
260 POLICY_SCOPE_USER, Value::CreateStringValue("google.com/q={x}"), NULL); | |
261 b.Set(kTestPolicyName8, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | |
262 Value::CreateBooleanValue(true), NULL); | |
263 | |
264 std::set<std::string> diff; | |
265 std::set<std::string> diff2; | |
266 a.GetDifferingKeys(b, &diff); | |
267 b.GetDifferingKeys(a, &diff2); | |
268 // Order shouldn't matter. | |
269 EXPECT_EQ(diff, diff2); | |
270 // No change. | |
271 EXPECT_TRUE(diff.find(kTestPolicyName1) == diff.end()); | |
272 EXPECT_TRUE(diff.find(kTestPolicyName2) == diff.end()); | |
273 // Different values. | |
274 EXPECT_TRUE(diff.find(kTestPolicyName3) != diff.end()); | |
275 // Different external data references. | |
276 EXPECT_TRUE(diff.find(kTestPolicyName4) != diff.end()); | |
277 // Different levels. | |
278 EXPECT_TRUE(diff.find(kTestPolicyName5) != diff.end()); | |
279 // Different scopes. | |
280 EXPECT_TRUE(diff.find(kTestPolicyName6) != diff.end()); | |
281 // Not in |a|. | |
282 EXPECT_TRUE(diff.find(kTestPolicyName8) != diff.end()); | |
283 // Not in |b|. | |
284 EXPECT_TRUE(diff.find(kTestPolicyName7) != diff.end()); | |
285 // No surprises. | |
286 EXPECT_EQ(6u, diff.size()); | |
287 } | |
288 | |
289 } // namespace policy | |
OLD | NEW |