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
3 // found in the LICENSE file.
5 #include "base/callback.h"
6 #include "base/json/json_reader.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_value_map.h"
9 #include "base/values.h"
10 #include "components/policy/core/browser/configuration_policy_handler.h"
11 #include "components/policy/core/browser/policy_error_map.h"
12 #include "components/policy/core/common/policy_map.h"
13 #include "components/policy/core/common/schema.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 StringToIntEnumListPolicyHandler::MappingEntry kTestTypeMap
[] = {
25 const char kTestPolicy
[] = "unit_test.test_policy";
26 const char kTestPref
[] = "unit_test.test_pref";
28 class SimpleSchemaValidatingPolicyHandler
29 : public SchemaValidatingPolicyHandler
{
31 SimpleSchemaValidatingPolicyHandler(const Schema
& schema
,
32 SchemaOnErrorStrategy strategy
)
33 : SchemaValidatingPolicyHandler("PolicyForTesting", schema
, strategy
) {}
34 virtual ~SimpleSchemaValidatingPolicyHandler() {}
36 virtual void ApplyPolicySettings(const policy::PolicyMap
&,
37 PrefValueMap
*) OVERRIDE
{
40 bool CheckAndGetValueForTest(const PolicyMap
& policies
,
41 scoped_ptr
<base::Value
>* value
) {
42 return SchemaValidatingPolicyHandler::CheckAndGetValue(
43 policies
, NULL
, value
);
49 TEST(StringToIntEnumListPolicyHandlerTest
, CheckPolicySettings
) {
52 PolicyErrorMap errors
;
53 StringToIntEnumListPolicyHandler
handler(
57 kTestTypeMap
+ arraysize(kTestTypeMap
));
59 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
60 POLICY_SCOPE_USER
, list
.DeepCopy(), NULL
);
62 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
63 EXPECT_TRUE(errors
.empty());
65 list
.AppendString("one");
66 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
67 POLICY_SCOPE_USER
, list
.DeepCopy(), NULL
);
69 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
70 EXPECT_TRUE(errors
.empty());
72 list
.AppendString("invalid");
73 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
74 POLICY_SCOPE_USER
, list
.DeepCopy(), NULL
);
76 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
77 EXPECT_FALSE(errors
.empty());
78 EXPECT_FALSE(errors
.GetErrors(kTestPolicy
).empty());
80 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
82 base::Value::CreateStringValue("no list"), NULL
);
84 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
85 EXPECT_FALSE(errors
.empty());
86 EXPECT_FALSE(errors
.GetErrors(kTestPolicy
).empty());
89 TEST(StringToIntEnumListPolicyHandlerTest
, ApplyPolicySettings
) {
91 base::ListValue expected
;
95 StringToIntEnumListPolicyHandler
handler(
99 kTestTypeMap
+ arraysize(kTestTypeMap
));
101 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
102 POLICY_SCOPE_USER
, list
.DeepCopy(), NULL
);
103 handler
.ApplyPolicySettings(policy_map
, &prefs
);
104 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
105 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
107 list
.AppendString("two");
108 expected
.AppendInteger(2);
109 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
110 POLICY_SCOPE_USER
, list
.DeepCopy(), NULL
);
111 handler
.ApplyPolicySettings(policy_map
, &prefs
);
112 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
113 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
115 list
.AppendString("invalid");
116 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
117 POLICY_SCOPE_USER
, list
.DeepCopy(), NULL
);
118 handler
.ApplyPolicySettings(policy_map
, &prefs
);
119 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
120 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
123 TEST(IntRangePolicyHandler
, CheckPolicySettingsClamp
) {
124 PolicyMap policy_map
;
125 PolicyErrorMap errors
;
127 // This tests needs to modify an int policy. The exact policy used and its
128 // semantics outside the test are irrelevant.
129 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, true);
131 // Check that values lying in the accepted range are not rejected.
132 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
133 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(0), NULL
);
135 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
136 EXPECT_TRUE(errors
.empty());
138 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
139 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(5), NULL
);
141 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
142 EXPECT_TRUE(errors
.empty());
144 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
145 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(10), NULL
);
147 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
148 EXPECT_TRUE(errors
.empty());
150 // Check that values lying outside the accepted range are not rejected
151 // (because clamping is enabled) but do yield a warning message.
152 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
153 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(-5), NULL
);
155 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
156 EXPECT_FALSE(errors
.empty());
158 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
159 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(15), NULL
);
161 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
162 EXPECT_FALSE(errors
.empty());
164 // Check that an entirely invalid value is rejected and yields an error
166 policy_map
.Set(kTestPolicy
,
167 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
168 base::Value::CreateStringValue("invalid"), NULL
);
170 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
171 EXPECT_FALSE(errors
.empty());
174 TEST(IntRangePolicyHandler
, CheckPolicySettingsDontClamp
) {
175 PolicyMap policy_map
;
176 PolicyErrorMap errors
;
178 // This tests needs to modify an int policy. The exact policy used and its
179 // semantics outside the test are irrelevant.
180 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, false);
182 // Check that values lying in the accepted range are not rejected.
183 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
184 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(0), NULL
);
186 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
187 EXPECT_TRUE(errors
.empty());
189 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
190 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(5), NULL
);
192 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
193 EXPECT_TRUE(errors
.empty());
195 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
196 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(10), NULL
);
198 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
199 EXPECT_TRUE(errors
.empty());
201 // Check that values lying outside the accepted range are rejected and yield
203 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
204 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(-5), NULL
);
206 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
207 EXPECT_FALSE(errors
.empty());
209 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
210 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(15), NULL
);
212 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
213 EXPECT_FALSE(errors
.empty());
215 // Check that an entirely invalid value is rejected and yields an error
217 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
218 base::Value::CreateStringValue("invalid"), NULL
);
220 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
221 EXPECT_FALSE(errors
.empty());
224 TEST(IntRangePolicyHandler
, ApplyPolicySettingsClamp
) {
225 PolicyMap policy_map
;
227 scoped_ptr
<base::Value
> expected
;
228 const base::Value
* value
;
230 // This tests needs to modify an int policy. The exact policy used and its
231 // semantics outside the test are irrelevant.
232 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, true);
234 // Check that values lying in the accepted range are written to the pref.
235 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
236 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(0), NULL
);
238 handler
.ApplyPolicySettings(policy_map
, &prefs
);
239 expected
.reset(base::Value::CreateIntegerValue(0));
240 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
241 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
243 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
244 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(5), NULL
);
246 handler
.ApplyPolicySettings(policy_map
, &prefs
);
247 expected
.reset(base::Value::CreateIntegerValue(5));
248 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
249 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
251 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
252 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(10), NULL
);
254 handler
.ApplyPolicySettings(policy_map
, &prefs
);
255 expected
.reset(base::Value::CreateIntegerValue(10));
256 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
257 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
259 // Check that values lying outside the accepted range are clamped and written
261 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
262 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(-5), NULL
);
264 handler
.ApplyPolicySettings(policy_map
, &prefs
);
265 expected
.reset(base::Value::CreateIntegerValue(0));
266 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
267 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
269 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
270 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(15), NULL
);
272 handler
.ApplyPolicySettings(policy_map
, &prefs
);
273 expected
.reset(base::Value::CreateIntegerValue(10));
274 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
275 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
278 TEST(IntRangePolicyHandler
, ApplyPolicySettingsDontClamp
) {
279 PolicyMap policy_map
;
281 scoped_ptr
<base::Value
> expected
;
282 const base::Value
* value
;
284 // This tests needs to modify an int policy. The exact policy used and its
285 // semantics outside the test are irrelevant.
286 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, true);
288 // Check that values lying in the accepted range are written to the pref.
289 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
290 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(0), NULL
);
292 handler
.ApplyPolicySettings(policy_map
, &prefs
);
293 expected
.reset(base::Value::CreateIntegerValue(0));
294 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
295 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
297 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
298 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(5), NULL
);
300 handler
.ApplyPolicySettings(policy_map
, &prefs
);
301 expected
.reset(base::Value::CreateIntegerValue(5));
302 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
303 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
305 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
306 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(10), NULL
);
308 handler
.ApplyPolicySettings(policy_map
, &prefs
);
309 expected
.reset(base::Value::CreateIntegerValue(10));
310 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
311 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
314 TEST(IntPercentageToDoublePolicyHandler
, CheckPolicySettingsClamp
) {
315 PolicyMap policy_map
;
316 PolicyErrorMap errors
;
318 // This tests needs to modify an int policy. The exact policy used and its
319 // semantics outside the test are irrelevant.
320 IntPercentageToDoublePolicyHandler
handler(
321 kTestPolicy
, kTestPref
, 0, 10, true);
323 // Check that values lying in the accepted range are not rejected.
324 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
325 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(0), NULL
);
327 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
328 EXPECT_TRUE(errors
.empty());
330 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
331 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(5), NULL
);
333 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
334 EXPECT_TRUE(errors
.empty());
336 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
337 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(10), NULL
);
339 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
340 EXPECT_TRUE(errors
.empty());
342 // Check that values lying outside the accepted range are not rejected
343 // (because clamping is enabled) but do yield a warning message.
344 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
345 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(-5), NULL
);
347 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
348 EXPECT_FALSE(errors
.empty());
350 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
351 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(15), NULL
);
353 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
354 EXPECT_FALSE(errors
.empty());
356 // Check that an entirely invalid value is rejected and yields an error
358 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
359 base::Value::CreateStringValue("invalid"), NULL
);
361 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
362 EXPECT_FALSE(errors
.empty());
365 TEST(IntPercentageToDoublePolicyHandler
, CheckPolicySettingsDontClamp
) {
366 PolicyMap policy_map
;
367 PolicyErrorMap errors
;
369 // This tests needs to modify an int policy. The exact policy used and its
370 // semantics outside the test are irrelevant.
371 IntPercentageToDoublePolicyHandler
handler(
372 kTestPolicy
, kTestPref
, 0, 10, false);
374 // Check that values lying in the accepted range are not rejected.
375 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
376 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(0), NULL
);
378 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
379 EXPECT_TRUE(errors
.empty());
381 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
382 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(5), NULL
);
384 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
385 EXPECT_TRUE(errors
.empty());
387 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
388 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(10), NULL
);
390 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
391 EXPECT_TRUE(errors
.empty());
393 // Check that values lying outside the accepted range are rejected and yield
395 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
396 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(-5), NULL
);
398 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
399 EXPECT_FALSE(errors
.empty());
401 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
402 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(15), NULL
);
404 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
405 EXPECT_FALSE(errors
.empty());
407 // Check that an entirely invalid value is rejected and yields an error
409 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
410 base::Value::CreateStringValue("invalid"), NULL
);
412 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
413 EXPECT_FALSE(errors
.empty());
416 TEST(IntPercentageToDoublePolicyHandler
, ApplyPolicySettingsClamp
) {
417 PolicyMap policy_map
;
419 scoped_ptr
<base::Value
> expected
;
420 const base::Value
* value
;
422 // This tests needs to modify an int policy. The exact policy used and its
423 // semantics outside the test are irrelevant.
424 IntPercentageToDoublePolicyHandler
handler(
425 kTestPolicy
, kTestPref
, 0, 10, true);
427 // Check that values lying in the accepted range are written to the pref.
428 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
429 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(0), NULL
);
431 handler
.ApplyPolicySettings(policy_map
, &prefs
);
432 expected
.reset(base::Value::CreateDoubleValue(0.0));
433 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
434 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
436 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
437 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(5), NULL
);
439 handler
.ApplyPolicySettings(policy_map
, &prefs
);
440 expected
.reset(base::Value::CreateDoubleValue(0.05));
441 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
442 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
444 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
445 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(10), NULL
);
447 handler
.ApplyPolicySettings(policy_map
, &prefs
);
448 expected
.reset(base::Value::CreateDoubleValue(0.1));
449 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
450 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
452 // Check that values lying outside the accepted range are clamped and written
454 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
455 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(-5), NULL
);
457 handler
.ApplyPolicySettings(policy_map
, &prefs
);
458 expected
.reset(base::Value::CreateDoubleValue(0.0));
459 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
460 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
462 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
463 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(15), NULL
);
465 handler
.ApplyPolicySettings(policy_map
, &prefs
);
466 expected
.reset(base::Value::CreateDoubleValue(0.1));
467 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
468 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
471 TEST(IntPercentageToDoublePolicyHandler
, ApplyPolicySettingsDontClamp
) {
472 PolicyMap policy_map
;
474 scoped_ptr
<base::Value
> expected
;
475 const base::Value
* value
;
477 // This tests needs to modify an int policy. The exact policy used and its
478 // semantics outside the test are irrelevant.
479 IntPercentageToDoublePolicyHandler
handler(
480 kTestPolicy
, kTestPref
, 0, 10, true);
482 // Check that values lying in the accepted range are written to the pref.
483 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
484 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(0), NULL
);
486 handler
.ApplyPolicySettings(policy_map
, &prefs
);
487 expected
.reset(base::Value::CreateDoubleValue(0.0));
488 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
489 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
491 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
492 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(5), NULL
);
494 handler
.ApplyPolicySettings(policy_map
, &prefs
);
495 expected
.reset(base::Value::CreateDoubleValue(0.05));
496 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
497 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
499 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
500 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(10), NULL
);
502 handler
.ApplyPolicySettings(policy_map
, &prefs
);
503 expected
.reset(base::Value::CreateDoubleValue(0.1));
504 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
505 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
508 TEST(SchemaValidatingPolicyHandlerTest
, CheckAndGetValue
) {
510 static const char kSchemaJson
[] =
512 " \"type\": \"object\","
515 " \"type\": \"integer\","
520 " \"type\": \"string\","
521 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
525 Schema schema
= Schema::Parse(kSchemaJson
, &error
);
526 ASSERT_TRUE(schema
.valid()) << error
;
528 static const char kPolicyMapJson
[] =
530 " \"PolicyForTesting\": {"
531 " \"OneToThree\": 2,"
532 " \"Colors\": \"White\""
535 scoped_ptr
<base::Value
> policy_map_value(base::JSONReader::ReadAndReturnError(
536 kPolicyMapJson
, base::JSON_PARSE_RFC
, NULL
, &error
));
537 ASSERT_TRUE(policy_map_value
) << error
;
539 const base::DictionaryValue
* policy_map_dict
= NULL
;
540 ASSERT_TRUE(policy_map_value
->GetAsDictionary(&policy_map_dict
));
542 PolicyMap policy_map
;
544 policy_map_dict
, POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
);
546 SimpleSchemaValidatingPolicyHandler
handler(schema
, SCHEMA_ALLOW_INVALID
);
547 scoped_ptr
<base::Value
> output_value
;
548 ASSERT_TRUE(handler
.CheckAndGetValueForTest(policy_map
, &output_value
));
549 ASSERT_TRUE(output_value
);
551 base::DictionaryValue
* dict
= NULL
;
552 ASSERT_TRUE(output_value
->GetAsDictionary(&dict
));
554 // Test that CheckAndGetValue() actually dropped invalid properties.
556 EXPECT_TRUE(dict
->GetInteger("OneToThree", &int_value
));
557 EXPECT_EQ(2, int_value
);
558 EXPECT_FALSE(dict
->HasKey("Colors"));
561 } // namespace policy