OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/prefs/pref_value_map.h" | 9 #include "base/prefs/pref_value_map.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "components/policy/core/browser/configuration_policy_handler.h" | 11 #include "components/policy/core/browser/configuration_policy_handler.h" |
12 #include "components/policy/core/browser/policy_error_map.h" | 12 #include "components/policy/core/browser/policy_error_map.h" |
13 #include "components/policy/core/common/policy_map.h" | 13 #include "components/policy/core/common/policy_map.h" |
14 #include "components/policy/core/common/policy_types.h" | |
15 #include "components/policy/core/common/schema.h" | 14 #include "components/policy/core/common/schema.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 | 16 |
18 namespace policy { | 17 namespace policy { |
19 | 18 |
20 namespace { | 19 namespace { |
21 | 20 |
22 void GetIntegerTypeMap( | 21 void GetIntegerTypeMap( |
23 ScopedVector<StringMappingListPolicyHandler::MappingEntry>* result) { | 22 ScopedVector<StringMappingListPolicyHandler::MappingEntry>* result) { |
24 result->push_back(new StringMappingListPolicyHandler::MappingEntry( | 23 result->push_back(new StringMappingListPolicyHandler::MappingEntry( |
(...skipping 25 matching lines...) Expand all Loading... |
50 | 49 |
51 TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) { | 50 TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) { |
52 base::ListValue list; | 51 base::ListValue list; |
53 PolicyMap policy_map; | 52 PolicyMap policy_map; |
54 PolicyErrorMap errors; | 53 PolicyErrorMap errors; |
55 StringMappingListPolicyHandler handler( | 54 StringMappingListPolicyHandler handler( |
56 kTestPolicy, | 55 kTestPolicy, |
57 kTestPref, | 56 kTestPref, |
58 base::Bind(GetIntegerTypeMap)); | 57 base::Bind(GetIntegerTypeMap)); |
59 | 58 |
60 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 59 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, |
61 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr); | 60 POLICY_SCOPE_USER, list.DeepCopy(), NULL); |
62 errors.Clear(); | 61 errors.Clear(); |
63 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 62 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
64 EXPECT_TRUE(errors.empty()); | 63 EXPECT_TRUE(errors.empty()); |
65 | 64 |
66 list.AppendString("one"); | 65 list.AppendString("one"); |
67 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 66 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, |
68 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr); | 67 POLICY_SCOPE_USER, list.DeepCopy(), NULL); |
69 errors.Clear(); | 68 errors.Clear(); |
70 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 69 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
71 EXPECT_TRUE(errors.empty()); | 70 EXPECT_TRUE(errors.empty()); |
72 | 71 |
73 list.AppendString("invalid"); | 72 list.AppendString("invalid"); |
74 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 73 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, |
75 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr); | 74 POLICY_SCOPE_USER, list.DeepCopy(), NULL); |
76 errors.Clear(); | 75 errors.Clear(); |
77 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 76 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
78 EXPECT_FALSE(errors.empty()); | 77 EXPECT_FALSE(errors.empty()); |
79 EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty()); | 78 EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty()); |
80 | 79 |
81 policy_map.Set(kTestPolicy, | 80 policy_map.Set(kTestPolicy, |
82 POLICY_LEVEL_MANDATORY, | 81 POLICY_LEVEL_MANDATORY, |
83 POLICY_SCOPE_USER, | 82 POLICY_SCOPE_USER, |
84 POLICY_SOURCE_CLOUD, | |
85 new base::StringValue("no list"), | 83 new base::StringValue("no list"), |
86 NULL); | 84 NULL); |
87 errors.Clear(); | 85 errors.Clear(); |
88 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 86 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
89 EXPECT_FALSE(errors.empty()); | 87 EXPECT_FALSE(errors.empty()); |
90 EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty()); | 88 EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty()); |
91 } | 89 } |
92 | 90 |
93 TEST(StringMappingListPolicyHandlerTest, ApplyPolicySettings) { | 91 TEST(StringMappingListPolicyHandlerTest, ApplyPolicySettings) { |
94 base::ListValue list; | 92 base::ListValue list; |
95 base::ListValue expected; | 93 base::ListValue expected; |
96 PolicyMap policy_map; | 94 PolicyMap policy_map; |
97 PrefValueMap prefs; | 95 PrefValueMap prefs; |
98 base::Value* value; | 96 base::Value* value; |
99 StringMappingListPolicyHandler handler( | 97 StringMappingListPolicyHandler handler( |
100 kTestPolicy, | 98 kTestPolicy, |
101 kTestPref, | 99 kTestPref, |
102 base::Bind(GetIntegerTypeMap)); | 100 base::Bind(GetIntegerTypeMap)); |
103 | 101 |
104 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 102 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, |
105 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr); | 103 POLICY_SCOPE_USER, list.DeepCopy(), NULL); |
106 handler.ApplyPolicySettings(policy_map, &prefs); | 104 handler.ApplyPolicySettings(policy_map, &prefs); |
107 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 105 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
108 EXPECT_TRUE(base::Value::Equals(&expected, value)); | 106 EXPECT_TRUE(base::Value::Equals(&expected, value)); |
109 | 107 |
110 list.AppendString("two"); | 108 list.AppendString("two"); |
111 expected.AppendInteger(2); | 109 expected.AppendInteger(2); |
112 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 110 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, |
113 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr); | 111 POLICY_SCOPE_USER, list.DeepCopy(), NULL); |
114 handler.ApplyPolicySettings(policy_map, &prefs); | 112 handler.ApplyPolicySettings(policy_map, &prefs); |
115 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 113 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
116 EXPECT_TRUE(base::Value::Equals(&expected, value)); | 114 EXPECT_TRUE(base::Value::Equals(&expected, value)); |
117 | 115 |
118 list.AppendString("invalid"); | 116 list.AppendString("invalid"); |
119 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 117 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, |
120 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr); | 118 POLICY_SCOPE_USER, list.DeepCopy(), NULL); |
121 handler.ApplyPolicySettings(policy_map, &prefs); | 119 handler.ApplyPolicySettings(policy_map, &prefs); |
122 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 120 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
123 EXPECT_TRUE(base::Value::Equals(&expected, value)); | 121 EXPECT_TRUE(base::Value::Equals(&expected, value)); |
124 } | 122 } |
125 | 123 |
126 TEST(IntRangePolicyHandler, CheckPolicySettingsClamp) { | 124 TEST(IntRangePolicyHandler, CheckPolicySettingsClamp) { |
127 PolicyMap policy_map; | 125 PolicyMap policy_map; |
128 PolicyErrorMap errors; | 126 PolicyErrorMap errors; |
129 | 127 |
130 // This tests needs to modify an int policy. The exact policy used and its | 128 // This tests needs to modify an int policy. The exact policy used and its |
131 // semantics outside the test are irrelevant. | 129 // semantics outside the test are irrelevant. |
132 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true); | 130 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true); |
133 | 131 |
134 // Check that values lying in the accepted range are not rejected. | 132 // Check that values lying in the accepted range are not rejected. |
135 policy_map.Set(kTestPolicy, | 133 policy_map.Set(kTestPolicy, |
136 POLICY_LEVEL_MANDATORY, | 134 POLICY_LEVEL_MANDATORY, |
137 POLICY_SCOPE_USER, | 135 POLICY_SCOPE_USER, |
138 POLICY_SOURCE_CLOUD, | |
139 new base::FundamentalValue(0), | 136 new base::FundamentalValue(0), |
140 NULL); | 137 NULL); |
141 errors.Clear(); | 138 errors.Clear(); |
142 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 139 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
143 EXPECT_TRUE(errors.empty()); | 140 EXPECT_TRUE(errors.empty()); |
144 | 141 |
145 policy_map.Set(kTestPolicy, | 142 policy_map.Set(kTestPolicy, |
146 POLICY_LEVEL_MANDATORY, | 143 POLICY_LEVEL_MANDATORY, |
147 POLICY_SCOPE_USER, | 144 POLICY_SCOPE_USER, |
148 POLICY_SOURCE_CLOUD, | |
149 new base::FundamentalValue(5), | 145 new base::FundamentalValue(5), |
150 NULL); | 146 NULL); |
151 errors.Clear(); | 147 errors.Clear(); |
152 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 148 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
153 EXPECT_TRUE(errors.empty()); | 149 EXPECT_TRUE(errors.empty()); |
154 | 150 |
155 policy_map.Set(kTestPolicy, | 151 policy_map.Set(kTestPolicy, |
156 POLICY_LEVEL_MANDATORY, | 152 POLICY_LEVEL_MANDATORY, |
157 POLICY_SCOPE_USER, | 153 POLICY_SCOPE_USER, |
158 POLICY_SOURCE_CLOUD, | |
159 new base::FundamentalValue(10), | 154 new base::FundamentalValue(10), |
160 NULL); | 155 NULL); |
161 errors.Clear(); | 156 errors.Clear(); |
162 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 157 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
163 EXPECT_TRUE(errors.empty()); | 158 EXPECT_TRUE(errors.empty()); |
164 | 159 |
165 // Check that values lying outside the accepted range are not rejected | 160 // Check that values lying outside the accepted range are not rejected |
166 // (because clamping is enabled) but do yield a warning message. | 161 // (because clamping is enabled) but do yield a warning message. |
167 policy_map.Set(kTestPolicy, | 162 policy_map.Set(kTestPolicy, |
168 POLICY_LEVEL_MANDATORY, | 163 POLICY_LEVEL_MANDATORY, |
169 POLICY_SCOPE_USER, | 164 POLICY_SCOPE_USER, |
170 POLICY_SOURCE_CLOUD, | |
171 new base::FundamentalValue(-5), | 165 new base::FundamentalValue(-5), |
172 NULL); | 166 NULL); |
173 errors.Clear(); | 167 errors.Clear(); |
174 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 168 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
175 EXPECT_FALSE(errors.empty()); | 169 EXPECT_FALSE(errors.empty()); |
176 | 170 |
177 policy_map.Set(kTestPolicy, | 171 policy_map.Set(kTestPolicy, |
178 POLICY_LEVEL_MANDATORY, | 172 POLICY_LEVEL_MANDATORY, |
179 POLICY_SCOPE_USER, | 173 POLICY_SCOPE_USER, |
180 POLICY_SOURCE_CLOUD, | |
181 new base::FundamentalValue(15), | 174 new base::FundamentalValue(15), |
182 NULL); | 175 NULL); |
183 errors.Clear(); | 176 errors.Clear(); |
184 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 177 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
185 EXPECT_FALSE(errors.empty()); | 178 EXPECT_FALSE(errors.empty()); |
186 | 179 |
187 // Check that an entirely invalid value is rejected and yields an error | 180 // Check that an entirely invalid value is rejected and yields an error |
188 // message. | 181 // message. |
189 policy_map.Set(kTestPolicy, | 182 policy_map.Set(kTestPolicy, |
190 POLICY_LEVEL_MANDATORY, | 183 POLICY_LEVEL_MANDATORY, |
191 POLICY_SCOPE_USER, | 184 POLICY_SCOPE_USER, |
192 POLICY_SOURCE_CLOUD, | |
193 new base::StringValue("invalid"), | 185 new base::StringValue("invalid"), |
194 NULL); | 186 NULL); |
195 errors.Clear(); | 187 errors.Clear(); |
196 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 188 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
197 EXPECT_FALSE(errors.empty()); | 189 EXPECT_FALSE(errors.empty()); |
198 } | 190 } |
199 | 191 |
200 TEST(IntRangePolicyHandler, CheckPolicySettingsDontClamp) { | 192 TEST(IntRangePolicyHandler, CheckPolicySettingsDontClamp) { |
201 PolicyMap policy_map; | 193 PolicyMap policy_map; |
202 PolicyErrorMap errors; | 194 PolicyErrorMap errors; |
203 | 195 |
204 // This tests needs to modify an int policy. The exact policy used and its | 196 // This tests needs to modify an int policy. The exact policy used and its |
205 // semantics outside the test are irrelevant. | 197 // semantics outside the test are irrelevant. |
206 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, false); | 198 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, false); |
207 | 199 |
208 // Check that values lying in the accepted range are not rejected. | 200 // Check that values lying in the accepted range are not rejected. |
209 policy_map.Set(kTestPolicy, | 201 policy_map.Set(kTestPolicy, |
210 POLICY_LEVEL_MANDATORY, | 202 POLICY_LEVEL_MANDATORY, |
211 POLICY_SCOPE_USER, | 203 POLICY_SCOPE_USER, |
212 POLICY_SOURCE_CLOUD, | |
213 new base::FundamentalValue(0), | 204 new base::FundamentalValue(0), |
214 NULL); | 205 NULL); |
215 errors.Clear(); | 206 errors.Clear(); |
216 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 207 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
217 EXPECT_TRUE(errors.empty()); | 208 EXPECT_TRUE(errors.empty()); |
218 | 209 |
219 policy_map.Set(kTestPolicy, | 210 policy_map.Set(kTestPolicy, |
220 POLICY_LEVEL_MANDATORY, | 211 POLICY_LEVEL_MANDATORY, |
221 POLICY_SCOPE_USER, | 212 POLICY_SCOPE_USER, |
222 POLICY_SOURCE_CLOUD, | |
223 new base::FundamentalValue(5), | 213 new base::FundamentalValue(5), |
224 NULL); | 214 NULL); |
225 errors.Clear(); | 215 errors.Clear(); |
226 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 216 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
227 EXPECT_TRUE(errors.empty()); | 217 EXPECT_TRUE(errors.empty()); |
228 | 218 |
229 policy_map.Set(kTestPolicy, | 219 policy_map.Set(kTestPolicy, |
230 POLICY_LEVEL_MANDATORY, | 220 POLICY_LEVEL_MANDATORY, |
231 POLICY_SCOPE_USER, | 221 POLICY_SCOPE_USER, |
232 POLICY_SOURCE_CLOUD, | |
233 new base::FundamentalValue(10), | 222 new base::FundamentalValue(10), |
234 NULL); | 223 NULL); |
235 errors.Clear(); | 224 errors.Clear(); |
236 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 225 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
237 EXPECT_TRUE(errors.empty()); | 226 EXPECT_TRUE(errors.empty()); |
238 | 227 |
239 // Check that values lying outside the accepted range are rejected and yield | 228 // Check that values lying outside the accepted range are rejected and yield |
240 // an error message. | 229 // an error message. |
241 policy_map.Set(kTestPolicy, | 230 policy_map.Set(kTestPolicy, |
242 POLICY_LEVEL_MANDATORY, | 231 POLICY_LEVEL_MANDATORY, |
243 POLICY_SCOPE_USER, | 232 POLICY_SCOPE_USER, |
244 POLICY_SOURCE_CLOUD, | |
245 new base::FundamentalValue(-5), | 233 new base::FundamentalValue(-5), |
246 NULL); | 234 NULL); |
247 errors.Clear(); | 235 errors.Clear(); |
248 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 236 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
249 EXPECT_FALSE(errors.empty()); | 237 EXPECT_FALSE(errors.empty()); |
250 | 238 |
251 policy_map.Set(kTestPolicy, | 239 policy_map.Set(kTestPolicy, |
252 POLICY_LEVEL_MANDATORY, | 240 POLICY_LEVEL_MANDATORY, |
253 POLICY_SCOPE_USER, | 241 POLICY_SCOPE_USER, |
254 POLICY_SOURCE_CLOUD, | |
255 new base::FundamentalValue(15), | 242 new base::FundamentalValue(15), |
256 NULL); | 243 NULL); |
257 errors.Clear(); | 244 errors.Clear(); |
258 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 245 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
259 EXPECT_FALSE(errors.empty()); | 246 EXPECT_FALSE(errors.empty()); |
260 | 247 |
261 // Check that an entirely invalid value is rejected and yields an error | 248 // Check that an entirely invalid value is rejected and yields an error |
262 // message. | 249 // message. |
263 policy_map.Set(kTestPolicy, | 250 policy_map.Set(kTestPolicy, |
264 POLICY_LEVEL_MANDATORY, | 251 POLICY_LEVEL_MANDATORY, |
265 POLICY_SCOPE_USER, | 252 POLICY_SCOPE_USER, |
266 POLICY_SOURCE_CLOUD, | |
267 new base::StringValue("invalid"), | 253 new base::StringValue("invalid"), |
268 NULL); | 254 NULL); |
269 errors.Clear(); | 255 errors.Clear(); |
270 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 256 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
271 EXPECT_FALSE(errors.empty()); | 257 EXPECT_FALSE(errors.empty()); |
272 } | 258 } |
273 | 259 |
274 TEST(IntRangePolicyHandler, ApplyPolicySettingsClamp) { | 260 TEST(IntRangePolicyHandler, ApplyPolicySettingsClamp) { |
275 PolicyMap policy_map; | 261 PolicyMap policy_map; |
276 PrefValueMap prefs; | 262 PrefValueMap prefs; |
277 scoped_ptr<base::Value> expected; | 263 scoped_ptr<base::Value> expected; |
278 const base::Value* value; | 264 const base::Value* value; |
279 | 265 |
280 // This tests needs to modify an int policy. The exact policy used and its | 266 // This tests needs to modify an int policy. The exact policy used and its |
281 // semantics outside the test are irrelevant. | 267 // semantics outside the test are irrelevant. |
282 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true); | 268 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true); |
283 | 269 |
284 // Check that values lying in the accepted range are written to the pref. | 270 // Check that values lying in the accepted range are written to the pref. |
285 policy_map.Set(kTestPolicy, | 271 policy_map.Set(kTestPolicy, |
286 POLICY_LEVEL_MANDATORY, | 272 POLICY_LEVEL_MANDATORY, |
287 POLICY_SCOPE_USER, | 273 POLICY_SCOPE_USER, |
288 POLICY_SOURCE_CLOUD, | |
289 new base::FundamentalValue(0), | 274 new base::FundamentalValue(0), |
290 NULL); | 275 NULL); |
291 prefs.Clear(); | 276 prefs.Clear(); |
292 handler.ApplyPolicySettings(policy_map, &prefs); | 277 handler.ApplyPolicySettings(policy_map, &prefs); |
293 expected.reset(new base::FundamentalValue(0)); | 278 expected.reset(new base::FundamentalValue(0)); |
294 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 279 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
295 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 280 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
296 | 281 |
297 policy_map.Set(kTestPolicy, | 282 policy_map.Set(kTestPolicy, |
298 POLICY_LEVEL_MANDATORY, | 283 POLICY_LEVEL_MANDATORY, |
299 POLICY_SCOPE_USER, | 284 POLICY_SCOPE_USER, |
300 POLICY_SOURCE_CLOUD, | |
301 new base::FundamentalValue(5), | 285 new base::FundamentalValue(5), |
302 NULL); | 286 NULL); |
303 prefs.Clear(); | 287 prefs.Clear(); |
304 handler.ApplyPolicySettings(policy_map, &prefs); | 288 handler.ApplyPolicySettings(policy_map, &prefs); |
305 expected.reset(new base::FundamentalValue(5)); | 289 expected.reset(new base::FundamentalValue(5)); |
306 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 290 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
307 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 291 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
308 | 292 |
309 policy_map.Set(kTestPolicy, | 293 policy_map.Set(kTestPolicy, |
310 POLICY_LEVEL_MANDATORY, | 294 POLICY_LEVEL_MANDATORY, |
311 POLICY_SCOPE_USER, | 295 POLICY_SCOPE_USER, |
312 POLICY_SOURCE_CLOUD, | |
313 new base::FundamentalValue(10), | 296 new base::FundamentalValue(10), |
314 NULL); | 297 NULL); |
315 prefs.Clear(); | 298 prefs.Clear(); |
316 handler.ApplyPolicySettings(policy_map, &prefs); | 299 handler.ApplyPolicySettings(policy_map, &prefs); |
317 expected.reset(new base::FundamentalValue(10)); | 300 expected.reset(new base::FundamentalValue(10)); |
318 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 301 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
319 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 302 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
320 | 303 |
321 // Check that values lying outside the accepted range are clamped and written | 304 // Check that values lying outside the accepted range are clamped and written |
322 // to the pref. | 305 // to the pref. |
323 policy_map.Set(kTestPolicy, | 306 policy_map.Set(kTestPolicy, |
324 POLICY_LEVEL_MANDATORY, | 307 POLICY_LEVEL_MANDATORY, |
325 POLICY_SCOPE_USER, | 308 POLICY_SCOPE_USER, |
326 POLICY_SOURCE_CLOUD, | |
327 new base::FundamentalValue(-5), | 309 new base::FundamentalValue(-5), |
328 NULL); | 310 NULL); |
329 prefs.Clear(); | 311 prefs.Clear(); |
330 handler.ApplyPolicySettings(policy_map, &prefs); | 312 handler.ApplyPolicySettings(policy_map, &prefs); |
331 expected.reset(new base::FundamentalValue(0)); | 313 expected.reset(new base::FundamentalValue(0)); |
332 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 314 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
333 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 315 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
334 | 316 |
335 policy_map.Set(kTestPolicy, | 317 policy_map.Set(kTestPolicy, |
336 POLICY_LEVEL_MANDATORY, | 318 POLICY_LEVEL_MANDATORY, |
337 POLICY_SCOPE_USER, | 319 POLICY_SCOPE_USER, |
338 POLICY_SOURCE_CLOUD, | |
339 new base::FundamentalValue(15), | 320 new base::FundamentalValue(15), |
340 NULL); | 321 NULL); |
341 prefs.Clear(); | 322 prefs.Clear(); |
342 handler.ApplyPolicySettings(policy_map, &prefs); | 323 handler.ApplyPolicySettings(policy_map, &prefs); |
343 expected.reset(new base::FundamentalValue(10)); | 324 expected.reset(new base::FundamentalValue(10)); |
344 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 325 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
345 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 326 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
346 } | 327 } |
347 | 328 |
348 TEST(IntRangePolicyHandler, ApplyPolicySettingsDontClamp) { | 329 TEST(IntRangePolicyHandler, ApplyPolicySettingsDontClamp) { |
349 PolicyMap policy_map; | 330 PolicyMap policy_map; |
350 PrefValueMap prefs; | 331 PrefValueMap prefs; |
351 scoped_ptr<base::Value> expected; | 332 scoped_ptr<base::Value> expected; |
352 const base::Value* value; | 333 const base::Value* value; |
353 | 334 |
354 // This tests needs to modify an int policy. The exact policy used and its | 335 // This tests needs to modify an int policy. The exact policy used and its |
355 // semantics outside the test are irrelevant. | 336 // semantics outside the test are irrelevant. |
356 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true); | 337 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true); |
357 | 338 |
358 // Check that values lying in the accepted range are written to the pref. | 339 // Check that values lying in the accepted range are written to the pref. |
359 policy_map.Set(kTestPolicy, | 340 policy_map.Set(kTestPolicy, |
360 POLICY_LEVEL_MANDATORY, | 341 POLICY_LEVEL_MANDATORY, |
361 POLICY_SCOPE_USER, | 342 POLICY_SCOPE_USER, |
362 POLICY_SOURCE_CLOUD, | |
363 new base::FundamentalValue(0), | 343 new base::FundamentalValue(0), |
364 NULL); | 344 NULL); |
365 prefs.Clear(); | 345 prefs.Clear(); |
366 handler.ApplyPolicySettings(policy_map, &prefs); | 346 handler.ApplyPolicySettings(policy_map, &prefs); |
367 expected.reset(new base::FundamentalValue(0)); | 347 expected.reset(new base::FundamentalValue(0)); |
368 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 348 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
369 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 349 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
370 | 350 |
371 policy_map.Set(kTestPolicy, | 351 policy_map.Set(kTestPolicy, |
372 POLICY_LEVEL_MANDATORY, | 352 POLICY_LEVEL_MANDATORY, |
373 POLICY_SCOPE_USER, | 353 POLICY_SCOPE_USER, |
374 POLICY_SOURCE_CLOUD, | |
375 new base::FundamentalValue(5), | 354 new base::FundamentalValue(5), |
376 NULL); | 355 NULL); |
377 prefs.Clear(); | 356 prefs.Clear(); |
378 handler.ApplyPolicySettings(policy_map, &prefs); | 357 handler.ApplyPolicySettings(policy_map, &prefs); |
379 expected.reset(new base::FundamentalValue(5)); | 358 expected.reset(new base::FundamentalValue(5)); |
380 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 359 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
381 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 360 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
382 | 361 |
383 policy_map.Set(kTestPolicy, | 362 policy_map.Set(kTestPolicy, |
384 POLICY_LEVEL_MANDATORY, | 363 POLICY_LEVEL_MANDATORY, |
385 POLICY_SCOPE_USER, | 364 POLICY_SCOPE_USER, |
386 POLICY_SOURCE_CLOUD, | |
387 new base::FundamentalValue(10), | 365 new base::FundamentalValue(10), |
388 NULL); | 366 NULL); |
389 prefs.Clear(); | 367 prefs.Clear(); |
390 handler.ApplyPolicySettings(policy_map, &prefs); | 368 handler.ApplyPolicySettings(policy_map, &prefs); |
391 expected.reset(new base::FundamentalValue(10)); | 369 expected.reset(new base::FundamentalValue(10)); |
392 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 370 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
393 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 371 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
394 } | 372 } |
395 | 373 |
396 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsClamp) { | 374 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsClamp) { |
397 PolicyMap policy_map; | 375 PolicyMap policy_map; |
398 PolicyErrorMap errors; | 376 PolicyErrorMap errors; |
399 | 377 |
400 // This tests needs to modify an int policy. The exact policy used and its | 378 // This tests needs to modify an int policy. The exact policy used and its |
401 // semantics outside the test are irrelevant. | 379 // semantics outside the test are irrelevant. |
402 IntPercentageToDoublePolicyHandler handler( | 380 IntPercentageToDoublePolicyHandler handler( |
403 kTestPolicy, kTestPref, 0, 10, true); | 381 kTestPolicy, kTestPref, 0, 10, true); |
404 | 382 |
405 // Check that values lying in the accepted range are not rejected. | 383 // Check that values lying in the accepted range are not rejected. |
406 policy_map.Set(kTestPolicy, | 384 policy_map.Set(kTestPolicy, |
407 POLICY_LEVEL_MANDATORY, | 385 POLICY_LEVEL_MANDATORY, |
408 POLICY_SCOPE_USER, | 386 POLICY_SCOPE_USER, |
409 POLICY_SOURCE_CLOUD, | |
410 new base::FundamentalValue(0), | 387 new base::FundamentalValue(0), |
411 NULL); | 388 NULL); |
412 errors.Clear(); | 389 errors.Clear(); |
413 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 390 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
414 EXPECT_TRUE(errors.empty()); | 391 EXPECT_TRUE(errors.empty()); |
415 | 392 |
416 policy_map.Set(kTestPolicy, | 393 policy_map.Set(kTestPolicy, |
417 POLICY_LEVEL_MANDATORY, | 394 POLICY_LEVEL_MANDATORY, |
418 POLICY_SCOPE_USER, | 395 POLICY_SCOPE_USER, |
419 POLICY_SOURCE_CLOUD, | |
420 new base::FundamentalValue(5), | 396 new base::FundamentalValue(5), |
421 NULL); | 397 NULL); |
422 errors.Clear(); | 398 errors.Clear(); |
423 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 399 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
424 EXPECT_TRUE(errors.empty()); | 400 EXPECT_TRUE(errors.empty()); |
425 | 401 |
426 policy_map.Set(kTestPolicy, | 402 policy_map.Set(kTestPolicy, |
427 POLICY_LEVEL_MANDATORY, | 403 POLICY_LEVEL_MANDATORY, |
428 POLICY_SCOPE_USER, | 404 POLICY_SCOPE_USER, |
429 POLICY_SOURCE_CLOUD, | |
430 new base::FundamentalValue(10), | 405 new base::FundamentalValue(10), |
431 NULL); | 406 NULL); |
432 errors.Clear(); | 407 errors.Clear(); |
433 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 408 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
434 EXPECT_TRUE(errors.empty()); | 409 EXPECT_TRUE(errors.empty()); |
435 | 410 |
436 // Check that values lying outside the accepted range are not rejected | 411 // Check that values lying outside the accepted range are not rejected |
437 // (because clamping is enabled) but do yield a warning message. | 412 // (because clamping is enabled) but do yield a warning message. |
438 policy_map.Set(kTestPolicy, | 413 policy_map.Set(kTestPolicy, |
439 POLICY_LEVEL_MANDATORY, | 414 POLICY_LEVEL_MANDATORY, |
440 POLICY_SCOPE_USER, | 415 POLICY_SCOPE_USER, |
441 POLICY_SOURCE_CLOUD, | |
442 new base::FundamentalValue(-5), | 416 new base::FundamentalValue(-5), |
443 NULL); | 417 NULL); |
444 errors.Clear(); | 418 errors.Clear(); |
445 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 419 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
446 EXPECT_FALSE(errors.empty()); | 420 EXPECT_FALSE(errors.empty()); |
447 | 421 |
448 policy_map.Set(kTestPolicy, | 422 policy_map.Set(kTestPolicy, |
449 POLICY_LEVEL_MANDATORY, | 423 POLICY_LEVEL_MANDATORY, |
450 POLICY_SCOPE_USER, | 424 POLICY_SCOPE_USER, |
451 POLICY_SOURCE_CLOUD, | |
452 new base::FundamentalValue(15), | 425 new base::FundamentalValue(15), |
453 NULL); | 426 NULL); |
454 errors.Clear(); | 427 errors.Clear(); |
455 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 428 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
456 EXPECT_FALSE(errors.empty()); | 429 EXPECT_FALSE(errors.empty()); |
457 | 430 |
458 // Check that an entirely invalid value is rejected and yields an error | 431 // Check that an entirely invalid value is rejected and yields an error |
459 // message. | 432 // message. |
460 policy_map.Set(kTestPolicy, | 433 policy_map.Set(kTestPolicy, |
461 POLICY_LEVEL_MANDATORY, | 434 POLICY_LEVEL_MANDATORY, |
462 POLICY_SCOPE_USER, | 435 POLICY_SCOPE_USER, |
463 POLICY_SOURCE_CLOUD, | |
464 new base::StringValue("invalid"), | 436 new base::StringValue("invalid"), |
465 NULL); | 437 NULL); |
466 errors.Clear(); | 438 errors.Clear(); |
467 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 439 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
468 EXPECT_FALSE(errors.empty()); | 440 EXPECT_FALSE(errors.empty()); |
469 } | 441 } |
470 | 442 |
471 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsDontClamp) { | 443 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsDontClamp) { |
472 PolicyMap policy_map; | 444 PolicyMap policy_map; |
473 PolicyErrorMap errors; | 445 PolicyErrorMap errors; |
474 | 446 |
475 // This tests needs to modify an int policy. The exact policy used and its | 447 // This tests needs to modify an int policy. The exact policy used and its |
476 // semantics outside the test are irrelevant. | 448 // semantics outside the test are irrelevant. |
477 IntPercentageToDoublePolicyHandler handler( | 449 IntPercentageToDoublePolicyHandler handler( |
478 kTestPolicy, kTestPref, 0, 10, false); | 450 kTestPolicy, kTestPref, 0, 10, false); |
479 | 451 |
480 // Check that values lying in the accepted range are not rejected. | 452 // Check that values lying in the accepted range are not rejected. |
481 policy_map.Set(kTestPolicy, | 453 policy_map.Set(kTestPolicy, |
482 POLICY_LEVEL_MANDATORY, | 454 POLICY_LEVEL_MANDATORY, |
483 POLICY_SCOPE_USER, | 455 POLICY_SCOPE_USER, |
484 POLICY_SOURCE_CLOUD, | |
485 new base::FundamentalValue(0), | 456 new base::FundamentalValue(0), |
486 NULL); | 457 NULL); |
487 errors.Clear(); | 458 errors.Clear(); |
488 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 459 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
489 EXPECT_TRUE(errors.empty()); | 460 EXPECT_TRUE(errors.empty()); |
490 | 461 |
491 policy_map.Set(kTestPolicy, | 462 policy_map.Set(kTestPolicy, |
492 POLICY_LEVEL_MANDATORY, | 463 POLICY_LEVEL_MANDATORY, |
493 POLICY_SCOPE_USER, | 464 POLICY_SCOPE_USER, |
494 POLICY_SOURCE_CLOUD, | |
495 new base::FundamentalValue(5), | 465 new base::FundamentalValue(5), |
496 NULL); | 466 NULL); |
497 errors.Clear(); | 467 errors.Clear(); |
498 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 468 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
499 EXPECT_TRUE(errors.empty()); | 469 EXPECT_TRUE(errors.empty()); |
500 | 470 |
501 policy_map.Set(kTestPolicy, | 471 policy_map.Set(kTestPolicy, |
502 POLICY_LEVEL_MANDATORY, | 472 POLICY_LEVEL_MANDATORY, |
503 POLICY_SCOPE_USER, | 473 POLICY_SCOPE_USER, |
504 POLICY_SOURCE_CLOUD, | |
505 new base::FundamentalValue(10), | 474 new base::FundamentalValue(10), |
506 NULL); | 475 NULL); |
507 errors.Clear(); | 476 errors.Clear(); |
508 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 477 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
509 EXPECT_TRUE(errors.empty()); | 478 EXPECT_TRUE(errors.empty()); |
510 | 479 |
511 // Check that values lying outside the accepted range are rejected and yield | 480 // Check that values lying outside the accepted range are rejected and yield |
512 // an error message. | 481 // an error message. |
513 policy_map.Set(kTestPolicy, | 482 policy_map.Set(kTestPolicy, |
514 POLICY_LEVEL_MANDATORY, | 483 POLICY_LEVEL_MANDATORY, |
515 POLICY_SCOPE_USER, | 484 POLICY_SCOPE_USER, |
516 POLICY_SOURCE_CLOUD, | |
517 new base::FundamentalValue(-5), | 485 new base::FundamentalValue(-5), |
518 NULL); | 486 NULL); |
519 errors.Clear(); | 487 errors.Clear(); |
520 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 488 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
521 EXPECT_FALSE(errors.empty()); | 489 EXPECT_FALSE(errors.empty()); |
522 | 490 |
523 policy_map.Set(kTestPolicy, | 491 policy_map.Set(kTestPolicy, |
524 POLICY_LEVEL_MANDATORY, | 492 POLICY_LEVEL_MANDATORY, |
525 POLICY_SCOPE_USER, | 493 POLICY_SCOPE_USER, |
526 POLICY_SOURCE_CLOUD, | |
527 new base::FundamentalValue(15), | 494 new base::FundamentalValue(15), |
528 NULL); | 495 NULL); |
529 errors.Clear(); | 496 errors.Clear(); |
530 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 497 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
531 EXPECT_FALSE(errors.empty()); | 498 EXPECT_FALSE(errors.empty()); |
532 | 499 |
533 // Check that an entirely invalid value is rejected and yields an error | 500 // Check that an entirely invalid value is rejected and yields an error |
534 // message. | 501 // message. |
535 policy_map.Set(kTestPolicy, | 502 policy_map.Set(kTestPolicy, |
536 POLICY_LEVEL_MANDATORY, | 503 POLICY_LEVEL_MANDATORY, |
537 POLICY_SCOPE_USER, | 504 POLICY_SCOPE_USER, |
538 POLICY_SOURCE_CLOUD, | |
539 new base::StringValue("invalid"), | 505 new base::StringValue("invalid"), |
540 NULL); | 506 NULL); |
541 errors.Clear(); | 507 errors.Clear(); |
542 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 508 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
543 EXPECT_FALSE(errors.empty()); | 509 EXPECT_FALSE(errors.empty()); |
544 } | 510 } |
545 | 511 |
546 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsClamp) { | 512 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsClamp) { |
547 PolicyMap policy_map; | 513 PolicyMap policy_map; |
548 PrefValueMap prefs; | 514 PrefValueMap prefs; |
549 scoped_ptr<base::Value> expected; | 515 scoped_ptr<base::Value> expected; |
550 const base::Value* value; | 516 const base::Value* value; |
551 | 517 |
552 // This tests needs to modify an int policy. The exact policy used and its | 518 // This tests needs to modify an int policy. The exact policy used and its |
553 // semantics outside the test are irrelevant. | 519 // semantics outside the test are irrelevant. |
554 IntPercentageToDoublePolicyHandler handler( | 520 IntPercentageToDoublePolicyHandler handler( |
555 kTestPolicy, kTestPref, 0, 10, true); | 521 kTestPolicy, kTestPref, 0, 10, true); |
556 | 522 |
557 // Check that values lying in the accepted range are written to the pref. | 523 // Check that values lying in the accepted range are written to the pref. |
558 policy_map.Set(kTestPolicy, | 524 policy_map.Set(kTestPolicy, |
559 POLICY_LEVEL_MANDATORY, | 525 POLICY_LEVEL_MANDATORY, |
560 POLICY_SCOPE_USER, | 526 POLICY_SCOPE_USER, |
561 POLICY_SOURCE_CLOUD, | |
562 new base::FundamentalValue(0), | 527 new base::FundamentalValue(0), |
563 NULL); | 528 NULL); |
564 prefs.Clear(); | 529 prefs.Clear(); |
565 handler.ApplyPolicySettings(policy_map, &prefs); | 530 handler.ApplyPolicySettings(policy_map, &prefs); |
566 expected.reset(new base::FundamentalValue(0.0)); | 531 expected.reset(new base::FundamentalValue(0.0)); |
567 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 532 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
568 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 533 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
569 | 534 |
570 policy_map.Set(kTestPolicy, | 535 policy_map.Set(kTestPolicy, |
571 POLICY_LEVEL_MANDATORY, | 536 POLICY_LEVEL_MANDATORY, |
572 POLICY_SCOPE_USER, | 537 POLICY_SCOPE_USER, |
573 POLICY_SOURCE_CLOUD, | |
574 new base::FundamentalValue(5), | 538 new base::FundamentalValue(5), |
575 NULL); | 539 NULL); |
576 prefs.Clear(); | 540 prefs.Clear(); |
577 handler.ApplyPolicySettings(policy_map, &prefs); | 541 handler.ApplyPolicySettings(policy_map, &prefs); |
578 expected.reset(new base::FundamentalValue(0.05)); | 542 expected.reset(new base::FundamentalValue(0.05)); |
579 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 543 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
580 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 544 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
581 | 545 |
582 policy_map.Set(kTestPolicy, | 546 policy_map.Set(kTestPolicy, |
583 POLICY_LEVEL_MANDATORY, | 547 POLICY_LEVEL_MANDATORY, |
584 POLICY_SCOPE_USER, | 548 POLICY_SCOPE_USER, |
585 POLICY_SOURCE_CLOUD, | |
586 new base::FundamentalValue(10), | 549 new base::FundamentalValue(10), |
587 NULL); | 550 NULL); |
588 prefs.Clear(); | 551 prefs.Clear(); |
589 handler.ApplyPolicySettings(policy_map, &prefs); | 552 handler.ApplyPolicySettings(policy_map, &prefs); |
590 expected.reset(new base::FundamentalValue(0.1)); | 553 expected.reset(new base::FundamentalValue(0.1)); |
591 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 554 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
592 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 555 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
593 | 556 |
594 // Check that values lying outside the accepted range are clamped and written | 557 // Check that values lying outside the accepted range are clamped and written |
595 // to the pref. | 558 // to the pref. |
596 policy_map.Set(kTestPolicy, | 559 policy_map.Set(kTestPolicy, |
597 POLICY_LEVEL_MANDATORY, | 560 POLICY_LEVEL_MANDATORY, |
598 POLICY_SCOPE_USER, | 561 POLICY_SCOPE_USER, |
599 POLICY_SOURCE_CLOUD, | |
600 new base::FundamentalValue(-5), | 562 new base::FundamentalValue(-5), |
601 NULL); | 563 NULL); |
602 prefs.Clear(); | 564 prefs.Clear(); |
603 handler.ApplyPolicySettings(policy_map, &prefs); | 565 handler.ApplyPolicySettings(policy_map, &prefs); |
604 expected.reset(new base::FundamentalValue(0.0)); | 566 expected.reset(new base::FundamentalValue(0.0)); |
605 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 567 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
606 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 568 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
607 | 569 |
608 policy_map.Set(kTestPolicy, | 570 policy_map.Set(kTestPolicy, |
609 POLICY_LEVEL_MANDATORY, | 571 POLICY_LEVEL_MANDATORY, |
610 POLICY_SCOPE_USER, | 572 POLICY_SCOPE_USER, |
611 POLICY_SOURCE_CLOUD, | |
612 new base::FundamentalValue(15), | 573 new base::FundamentalValue(15), |
613 NULL); | 574 NULL); |
614 prefs.Clear(); | 575 prefs.Clear(); |
615 handler.ApplyPolicySettings(policy_map, &prefs); | 576 handler.ApplyPolicySettings(policy_map, &prefs); |
616 expected.reset(new base::FundamentalValue(0.1)); | 577 expected.reset(new base::FundamentalValue(0.1)); |
617 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 578 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
618 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 579 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
619 } | 580 } |
620 | 581 |
621 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsDontClamp) { | 582 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsDontClamp) { |
622 PolicyMap policy_map; | 583 PolicyMap policy_map; |
623 PrefValueMap prefs; | 584 PrefValueMap prefs; |
624 scoped_ptr<base::Value> expected; | 585 scoped_ptr<base::Value> expected; |
625 const base::Value* value; | 586 const base::Value* value; |
626 | 587 |
627 // This tests needs to modify an int policy. The exact policy used and its | 588 // This tests needs to modify an int policy. The exact policy used and its |
628 // semantics outside the test are irrelevant. | 589 // semantics outside the test are irrelevant. |
629 IntPercentageToDoublePolicyHandler handler( | 590 IntPercentageToDoublePolicyHandler handler( |
630 kTestPolicy, kTestPref, 0, 10, true); | 591 kTestPolicy, kTestPref, 0, 10, true); |
631 | 592 |
632 // Check that values lying in the accepted range are written to the pref. | 593 // Check that values lying in the accepted range are written to the pref. |
633 policy_map.Set(kTestPolicy, | 594 policy_map.Set(kTestPolicy, |
634 POLICY_LEVEL_MANDATORY, | 595 POLICY_LEVEL_MANDATORY, |
635 POLICY_SCOPE_USER, | 596 POLICY_SCOPE_USER, |
636 POLICY_SOURCE_CLOUD, | |
637 new base::FundamentalValue(0), | 597 new base::FundamentalValue(0), |
638 NULL); | 598 NULL); |
639 prefs.Clear(); | 599 prefs.Clear(); |
640 handler.ApplyPolicySettings(policy_map, &prefs); | 600 handler.ApplyPolicySettings(policy_map, &prefs); |
641 expected.reset(new base::FundamentalValue(0.0)); | 601 expected.reset(new base::FundamentalValue(0.0)); |
642 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 602 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
643 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 603 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
644 | 604 |
645 policy_map.Set(kTestPolicy, | 605 policy_map.Set(kTestPolicy, |
646 POLICY_LEVEL_MANDATORY, | 606 POLICY_LEVEL_MANDATORY, |
647 POLICY_SCOPE_USER, | 607 POLICY_SCOPE_USER, |
648 POLICY_SOURCE_CLOUD, | |
649 new base::FundamentalValue(5), | 608 new base::FundamentalValue(5), |
650 NULL); | 609 NULL); |
651 prefs.Clear(); | 610 prefs.Clear(); |
652 handler.ApplyPolicySettings(policy_map, &prefs); | 611 handler.ApplyPolicySettings(policy_map, &prefs); |
653 expected.reset(new base::FundamentalValue(0.05)); | 612 expected.reset(new base::FundamentalValue(0.05)); |
654 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 613 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
655 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 614 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
656 | 615 |
657 policy_map.Set(kTestPolicy, | 616 policy_map.Set(kTestPolicy, |
658 POLICY_LEVEL_MANDATORY, | 617 POLICY_LEVEL_MANDATORY, |
659 POLICY_SCOPE_USER, | 618 POLICY_SCOPE_USER, |
660 POLICY_SOURCE_CLOUD, | |
661 new base::FundamentalValue(10), | 619 new base::FundamentalValue(10), |
662 NULL); | 620 NULL); |
663 prefs.Clear(); | 621 prefs.Clear(); |
664 handler.ApplyPolicySettings(policy_map, &prefs); | 622 handler.ApplyPolicySettings(policy_map, &prefs); |
665 expected.reset(new base::FundamentalValue(0.1)); | 623 expected.reset(new base::FundamentalValue(0.1)); |
666 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 624 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
667 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 625 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
668 } | 626 } |
669 | 627 |
670 TEST(SchemaValidatingPolicyHandlerTest, CheckAndGetValue) { | 628 TEST(SchemaValidatingPolicyHandlerTest, CheckAndGetValue) { |
(...skipping 25 matching lines...) Expand all Loading... |
696 "}"; | 654 "}"; |
697 scoped_ptr<base::Value> policy_map_value = | 655 scoped_ptr<base::Value> policy_map_value = |
698 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC, | 656 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC, |
699 NULL, &error); | 657 NULL, &error); |
700 ASSERT_TRUE(policy_map_value) << error; | 658 ASSERT_TRUE(policy_map_value) << error; |
701 | 659 |
702 const base::DictionaryValue* policy_map_dict = NULL; | 660 const base::DictionaryValue* policy_map_dict = NULL; |
703 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict)); | 661 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict)); |
704 | 662 |
705 PolicyMap policy_map; | 663 PolicyMap policy_map; |
706 policy_map.LoadFrom(policy_map_dict, POLICY_LEVEL_RECOMMENDED, | 664 policy_map.LoadFrom( |
707 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD); | 665 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); |
708 | 666 |
709 TestSchemaValidatingPolicyHandler handler(schema, SCHEMA_ALLOW_INVALID); | 667 TestSchemaValidatingPolicyHandler handler(schema, SCHEMA_ALLOW_INVALID); |
710 scoped_ptr<base::Value> output_value; | 668 scoped_ptr<base::Value> output_value; |
711 ASSERT_TRUE(handler.CheckAndGetValueForTest(policy_map, &output_value)); | 669 ASSERT_TRUE(handler.CheckAndGetValueForTest(policy_map, &output_value)); |
712 ASSERT_TRUE(output_value); | 670 ASSERT_TRUE(output_value); |
713 | 671 |
714 base::DictionaryValue* dict = NULL; | 672 base::DictionaryValue* dict = NULL; |
715 ASSERT_TRUE(output_value->GetAsDictionary(&dict)); | 673 ASSERT_TRUE(output_value->GetAsDictionary(&dict)); |
716 | 674 |
717 // Test that CheckAndGetValue() actually dropped invalid properties. | 675 // Test that CheckAndGetValue() actually dropped invalid properties. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
757 scoped_ptr<base::Value> policy_map_value = | 715 scoped_ptr<base::Value> policy_map_value = |
758 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC, | 716 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC, |
759 NULL, &error); | 717 NULL, &error); |
760 ASSERT_TRUE(policy_map_value) << error; | 718 ASSERT_TRUE(policy_map_value) << error; |
761 | 719 |
762 const base::DictionaryValue* policy_map_dict = NULL; | 720 const base::DictionaryValue* policy_map_dict = NULL; |
763 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict)); | 721 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict)); |
764 | 722 |
765 PolicyMap policy_map_recommended; | 723 PolicyMap policy_map_recommended; |
766 policy_map_recommended.LoadFrom( | 724 policy_map_recommended.LoadFrom( |
767 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 725 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); |
768 POLICY_SOURCE_CLOUD); | |
769 | 726 |
770 PolicyMap policy_map_mandatory; | 727 PolicyMap policy_map_mandatory; |
771 policy_map_mandatory.LoadFrom( | 728 policy_map_mandatory.LoadFrom( |
772 policy_map_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 729 policy_map_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
773 POLICY_SOURCE_CLOUD); | |
774 | 730 |
775 SimpleSchemaValidatingPolicyHandler handler_all( | 731 SimpleSchemaValidatingPolicyHandler handler_all( |
776 policy_name, | 732 policy_name, |
777 kTestPref, | 733 kTestPref, |
778 schema, | 734 schema, |
779 SCHEMA_STRICT, | 735 SCHEMA_STRICT, |
780 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED, | 736 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED, |
781 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED); | 737 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED); |
782 | 738 |
783 SimpleSchemaValidatingPolicyHandler handler_recommended( | 739 SimpleSchemaValidatingPolicyHandler handler_recommended( |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs); | 810 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs); |
855 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); | 811 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); |
856 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); | 812 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); |
857 | 813 |
858 EXPECT_FALSE( | 814 EXPECT_FALSE( |
859 handler_none.CheckPolicySettings(policy_map_recommended, &errors)); | 815 handler_none.CheckPolicySettings(policy_map_recommended, &errors)); |
860 EXPECT_FALSE(errors.empty()); | 816 EXPECT_FALSE(errors.empty()); |
861 } | 817 } |
862 | 818 |
863 } // namespace policy | 819 } // namespace policy |
OLD | NEW |