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.
6 #include "base/callback.h"
7 #include "base/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/pref_value_map.h"
10 #include "base/values.h"
11 #include "components/policy/core/browser/configuration_policy_handler.h"
12 #include "components/policy/core/browser/policy_error_map.h"
13 #include "components/policy/core/common/policy_map.h"
14 #include "components/policy/core/common/schema.h"
15 #include "testing/gtest/include/gtest/gtest.h"
21 void GetIntegerTypeMap(
22 ScopedVector
<StringMappingListPolicyHandler::MappingEntry
>* result
) {
23 result
->push_back(new StringMappingListPolicyHandler::MappingEntry(
24 "one", scoped_ptr
<base::Value
>(new base::FundamentalValue(1))));
25 result
->push_back(new StringMappingListPolicyHandler::MappingEntry(
26 "two", scoped_ptr
<base::Value
>(new base::FundamentalValue(2))));
29 const char kTestPolicy
[] = "unit_test.test_policy";
30 const char kTestPref
[] = "unit_test.test_pref";
32 class TestSchemaValidatingPolicyHandler
: public SchemaValidatingPolicyHandler
{
34 TestSchemaValidatingPolicyHandler(const Schema
& schema
,
35 SchemaOnErrorStrategy strategy
)
36 : SchemaValidatingPolicyHandler("PolicyForTesting", schema
, strategy
) {}
37 ~TestSchemaValidatingPolicyHandler() override
{}
39 void ApplyPolicySettings(const policy::PolicyMap
&, PrefValueMap
*) override
{}
41 bool CheckAndGetValueForTest(const PolicyMap
& policies
,
42 scoped_ptr
<base::Value
>* value
) {
43 return SchemaValidatingPolicyHandler::CheckAndGetValue(
44 policies
, NULL
, value
);
50 TEST(StringToIntEnumListPolicyHandlerTest
, CheckPolicySettings
) {
53 PolicyErrorMap errors
;
54 StringMappingListPolicyHandler
handler(
57 base::Bind(GetIntegerTypeMap
));
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
,
81 POLICY_LEVEL_MANDATORY
,
83 new base::StringValue("no list"),
86 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
87 EXPECT_FALSE(errors
.empty());
88 EXPECT_FALSE(errors
.GetErrors(kTestPolicy
).empty());
91 TEST(StringMappingListPolicyHandlerTest
, ApplyPolicySettings
) {
93 base::ListValue expected
;
97 StringMappingListPolicyHandler
handler(
100 base::Bind(GetIntegerTypeMap
));
102 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
103 POLICY_SCOPE_USER
, list
.DeepCopy(), NULL
);
104 handler
.ApplyPolicySettings(policy_map
, &prefs
);
105 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
106 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
108 list
.AppendString("two");
109 expected
.AppendInteger(2);
110 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
111 POLICY_SCOPE_USER
, list
.DeepCopy(), NULL
);
112 handler
.ApplyPolicySettings(policy_map
, &prefs
);
113 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
114 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
116 list
.AppendString("invalid");
117 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
,
118 POLICY_SCOPE_USER
, list
.DeepCopy(), NULL
);
119 handler
.ApplyPolicySettings(policy_map
, &prefs
);
120 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
121 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
124 TEST(IntRangePolicyHandler
, CheckPolicySettingsClamp
) {
125 PolicyMap policy_map
;
126 PolicyErrorMap errors
;
128 // This tests needs to modify an int policy. The exact policy used and its
129 // semantics outside the test are irrelevant.
130 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, true);
132 // Check that values lying in the accepted range are not rejected.
133 policy_map
.Set(kTestPolicy
,
134 POLICY_LEVEL_MANDATORY
,
136 new base::FundamentalValue(0),
139 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
140 EXPECT_TRUE(errors
.empty());
142 policy_map
.Set(kTestPolicy
,
143 POLICY_LEVEL_MANDATORY
,
145 new base::FundamentalValue(5),
148 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
149 EXPECT_TRUE(errors
.empty());
151 policy_map
.Set(kTestPolicy
,
152 POLICY_LEVEL_MANDATORY
,
154 new base::FundamentalValue(10),
157 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
158 EXPECT_TRUE(errors
.empty());
160 // Check that values lying outside the accepted range are not rejected
161 // (because clamping is enabled) but do yield a warning message.
162 policy_map
.Set(kTestPolicy
,
163 POLICY_LEVEL_MANDATORY
,
165 new base::FundamentalValue(-5),
168 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
169 EXPECT_FALSE(errors
.empty());
171 policy_map
.Set(kTestPolicy
,
172 POLICY_LEVEL_MANDATORY
,
174 new base::FundamentalValue(15),
177 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
178 EXPECT_FALSE(errors
.empty());
180 // Check that an entirely invalid value is rejected and yields an error
182 policy_map
.Set(kTestPolicy
,
183 POLICY_LEVEL_MANDATORY
,
185 new base::StringValue("invalid"),
188 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
189 EXPECT_FALSE(errors
.empty());
192 TEST(IntRangePolicyHandler
, CheckPolicySettingsDontClamp
) {
193 PolicyMap policy_map
;
194 PolicyErrorMap errors
;
196 // This tests needs to modify an int policy. The exact policy used and its
197 // semantics outside the test are irrelevant.
198 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, false);
200 // Check that values lying in the accepted range are not rejected.
201 policy_map
.Set(kTestPolicy
,
202 POLICY_LEVEL_MANDATORY
,
204 new base::FundamentalValue(0),
207 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
208 EXPECT_TRUE(errors
.empty());
210 policy_map
.Set(kTestPolicy
,
211 POLICY_LEVEL_MANDATORY
,
213 new base::FundamentalValue(5),
216 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
217 EXPECT_TRUE(errors
.empty());
219 policy_map
.Set(kTestPolicy
,
220 POLICY_LEVEL_MANDATORY
,
222 new base::FundamentalValue(10),
225 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
226 EXPECT_TRUE(errors
.empty());
228 // Check that values lying outside the accepted range are rejected and yield
230 policy_map
.Set(kTestPolicy
,
231 POLICY_LEVEL_MANDATORY
,
233 new base::FundamentalValue(-5),
236 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
237 EXPECT_FALSE(errors
.empty());
239 policy_map
.Set(kTestPolicy
,
240 POLICY_LEVEL_MANDATORY
,
242 new base::FundamentalValue(15),
245 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
246 EXPECT_FALSE(errors
.empty());
248 // Check that an entirely invalid value is rejected and yields an error
250 policy_map
.Set(kTestPolicy
,
251 POLICY_LEVEL_MANDATORY
,
253 new base::StringValue("invalid"),
256 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
257 EXPECT_FALSE(errors
.empty());
260 TEST(IntRangePolicyHandler
, ApplyPolicySettingsClamp
) {
261 PolicyMap policy_map
;
263 scoped_ptr
<base::Value
> expected
;
264 const base::Value
* value
;
266 // This tests needs to modify an int policy. The exact policy used and its
267 // semantics outside the test are irrelevant.
268 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, true);
270 // Check that values lying in the accepted range are written to the pref.
271 policy_map
.Set(kTestPolicy
,
272 POLICY_LEVEL_MANDATORY
,
274 new base::FundamentalValue(0),
277 handler
.ApplyPolicySettings(policy_map
, &prefs
);
278 expected
.reset(new base::FundamentalValue(0));
279 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
280 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
282 policy_map
.Set(kTestPolicy
,
283 POLICY_LEVEL_MANDATORY
,
285 new base::FundamentalValue(5),
288 handler
.ApplyPolicySettings(policy_map
, &prefs
);
289 expected
.reset(new base::FundamentalValue(5));
290 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
291 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
293 policy_map
.Set(kTestPolicy
,
294 POLICY_LEVEL_MANDATORY
,
296 new base::FundamentalValue(10),
299 handler
.ApplyPolicySettings(policy_map
, &prefs
);
300 expected
.reset(new base::FundamentalValue(10));
301 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
302 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
304 // Check that values lying outside the accepted range are clamped and written
306 policy_map
.Set(kTestPolicy
,
307 POLICY_LEVEL_MANDATORY
,
309 new base::FundamentalValue(-5),
312 handler
.ApplyPolicySettings(policy_map
, &prefs
);
313 expected
.reset(new base::FundamentalValue(0));
314 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
315 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
317 policy_map
.Set(kTestPolicy
,
318 POLICY_LEVEL_MANDATORY
,
320 new base::FundamentalValue(15),
323 handler
.ApplyPolicySettings(policy_map
, &prefs
);
324 expected
.reset(new base::FundamentalValue(10));
325 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
326 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
329 TEST(IntRangePolicyHandler
, ApplyPolicySettingsDontClamp
) {
330 PolicyMap policy_map
;
332 scoped_ptr
<base::Value
> expected
;
333 const base::Value
* value
;
335 // This tests needs to modify an int policy. The exact policy used and its
336 // semantics outside the test are irrelevant.
337 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, true);
339 // Check that values lying in the accepted range are written to the pref.
340 policy_map
.Set(kTestPolicy
,
341 POLICY_LEVEL_MANDATORY
,
343 new base::FundamentalValue(0),
346 handler
.ApplyPolicySettings(policy_map
, &prefs
);
347 expected
.reset(new base::FundamentalValue(0));
348 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
349 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
351 policy_map
.Set(kTestPolicy
,
352 POLICY_LEVEL_MANDATORY
,
354 new base::FundamentalValue(5),
357 handler
.ApplyPolicySettings(policy_map
, &prefs
);
358 expected
.reset(new base::FundamentalValue(5));
359 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
360 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
362 policy_map
.Set(kTestPolicy
,
363 POLICY_LEVEL_MANDATORY
,
365 new base::FundamentalValue(10),
368 handler
.ApplyPolicySettings(policy_map
, &prefs
);
369 expected
.reset(new base::FundamentalValue(10));
370 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
371 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
374 TEST(IntPercentageToDoublePolicyHandler
, CheckPolicySettingsClamp
) {
375 PolicyMap policy_map
;
376 PolicyErrorMap errors
;
378 // This tests needs to modify an int policy. The exact policy used and its
379 // semantics outside the test are irrelevant.
380 IntPercentageToDoublePolicyHandler
handler(
381 kTestPolicy
, kTestPref
, 0, 10, true);
383 // Check that values lying in the accepted range are not rejected.
384 policy_map
.Set(kTestPolicy
,
385 POLICY_LEVEL_MANDATORY
,
387 new base::FundamentalValue(0),
390 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
391 EXPECT_TRUE(errors
.empty());
393 policy_map
.Set(kTestPolicy
,
394 POLICY_LEVEL_MANDATORY
,
396 new base::FundamentalValue(5),
399 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
400 EXPECT_TRUE(errors
.empty());
402 policy_map
.Set(kTestPolicy
,
403 POLICY_LEVEL_MANDATORY
,
405 new base::FundamentalValue(10),
408 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
409 EXPECT_TRUE(errors
.empty());
411 // Check that values lying outside the accepted range are not rejected
412 // (because clamping is enabled) but do yield a warning message.
413 policy_map
.Set(kTestPolicy
,
414 POLICY_LEVEL_MANDATORY
,
416 new base::FundamentalValue(-5),
419 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
420 EXPECT_FALSE(errors
.empty());
422 policy_map
.Set(kTestPolicy
,
423 POLICY_LEVEL_MANDATORY
,
425 new base::FundamentalValue(15),
428 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
429 EXPECT_FALSE(errors
.empty());
431 // Check that an entirely invalid value is rejected and yields an error
433 policy_map
.Set(kTestPolicy
,
434 POLICY_LEVEL_MANDATORY
,
436 new base::StringValue("invalid"),
439 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
440 EXPECT_FALSE(errors
.empty());
443 TEST(IntPercentageToDoublePolicyHandler
, CheckPolicySettingsDontClamp
) {
444 PolicyMap policy_map
;
445 PolicyErrorMap errors
;
447 // This tests needs to modify an int policy. The exact policy used and its
448 // semantics outside the test are irrelevant.
449 IntPercentageToDoublePolicyHandler
handler(
450 kTestPolicy
, kTestPref
, 0, 10, false);
452 // Check that values lying in the accepted range are not rejected.
453 policy_map
.Set(kTestPolicy
,
454 POLICY_LEVEL_MANDATORY
,
456 new base::FundamentalValue(0),
459 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
460 EXPECT_TRUE(errors
.empty());
462 policy_map
.Set(kTestPolicy
,
463 POLICY_LEVEL_MANDATORY
,
465 new base::FundamentalValue(5),
468 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
469 EXPECT_TRUE(errors
.empty());
471 policy_map
.Set(kTestPolicy
,
472 POLICY_LEVEL_MANDATORY
,
474 new base::FundamentalValue(10),
477 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
478 EXPECT_TRUE(errors
.empty());
480 // Check that values lying outside the accepted range are rejected and yield
482 policy_map
.Set(kTestPolicy
,
483 POLICY_LEVEL_MANDATORY
,
485 new base::FundamentalValue(-5),
488 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
489 EXPECT_FALSE(errors
.empty());
491 policy_map
.Set(kTestPolicy
,
492 POLICY_LEVEL_MANDATORY
,
494 new base::FundamentalValue(15),
497 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
498 EXPECT_FALSE(errors
.empty());
500 // Check that an entirely invalid value is rejected and yields an error
502 policy_map
.Set(kTestPolicy
,
503 POLICY_LEVEL_MANDATORY
,
505 new base::StringValue("invalid"),
508 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
509 EXPECT_FALSE(errors
.empty());
512 TEST(IntPercentageToDoublePolicyHandler
, ApplyPolicySettingsClamp
) {
513 PolicyMap policy_map
;
515 scoped_ptr
<base::Value
> expected
;
516 const base::Value
* value
;
518 // This tests needs to modify an int policy. The exact policy used and its
519 // semantics outside the test are irrelevant.
520 IntPercentageToDoublePolicyHandler
handler(
521 kTestPolicy
, kTestPref
, 0, 10, true);
523 // Check that values lying in the accepted range are written to the pref.
524 policy_map
.Set(kTestPolicy
,
525 POLICY_LEVEL_MANDATORY
,
527 new base::FundamentalValue(0),
530 handler
.ApplyPolicySettings(policy_map
, &prefs
);
531 expected
.reset(new base::FundamentalValue(0.0));
532 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
533 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
535 policy_map
.Set(kTestPolicy
,
536 POLICY_LEVEL_MANDATORY
,
538 new base::FundamentalValue(5),
541 handler
.ApplyPolicySettings(policy_map
, &prefs
);
542 expected
.reset(new base::FundamentalValue(0.05));
543 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
544 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
546 policy_map
.Set(kTestPolicy
,
547 POLICY_LEVEL_MANDATORY
,
549 new base::FundamentalValue(10),
552 handler
.ApplyPolicySettings(policy_map
, &prefs
);
553 expected
.reset(new base::FundamentalValue(0.1));
554 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
555 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
557 // Check that values lying outside the accepted range are clamped and written
559 policy_map
.Set(kTestPolicy
,
560 POLICY_LEVEL_MANDATORY
,
562 new base::FundamentalValue(-5),
565 handler
.ApplyPolicySettings(policy_map
, &prefs
);
566 expected
.reset(new base::FundamentalValue(0.0));
567 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
568 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
570 policy_map
.Set(kTestPolicy
,
571 POLICY_LEVEL_MANDATORY
,
573 new base::FundamentalValue(15),
576 handler
.ApplyPolicySettings(policy_map
, &prefs
);
577 expected
.reset(new base::FundamentalValue(0.1));
578 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
579 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
582 TEST(IntPercentageToDoublePolicyHandler
, ApplyPolicySettingsDontClamp
) {
583 PolicyMap policy_map
;
585 scoped_ptr
<base::Value
> expected
;
586 const base::Value
* value
;
588 // This tests needs to modify an int policy. The exact policy used and its
589 // semantics outside the test are irrelevant.
590 IntPercentageToDoublePolicyHandler
handler(
591 kTestPolicy
, kTestPref
, 0, 10, true);
593 // Check that values lying in the accepted range are written to the pref.
594 policy_map
.Set(kTestPolicy
,
595 POLICY_LEVEL_MANDATORY
,
597 new base::FundamentalValue(0),
600 handler
.ApplyPolicySettings(policy_map
, &prefs
);
601 expected
.reset(new base::FundamentalValue(0.0));
602 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
603 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
605 policy_map
.Set(kTestPolicy
,
606 POLICY_LEVEL_MANDATORY
,
608 new base::FundamentalValue(5),
611 handler
.ApplyPolicySettings(policy_map
, &prefs
);
612 expected
.reset(new base::FundamentalValue(0.05));
613 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
614 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
616 policy_map
.Set(kTestPolicy
,
617 POLICY_LEVEL_MANDATORY
,
619 new base::FundamentalValue(10),
622 handler
.ApplyPolicySettings(policy_map
, &prefs
);
623 expected
.reset(new base::FundamentalValue(0.1));
624 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
625 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
628 TEST(SchemaValidatingPolicyHandlerTest
, CheckAndGetValue
) {
630 static const char kSchemaJson
[] =
632 " \"type\": \"object\","
635 " \"type\": \"integer\","
640 " \"type\": \"string\","
641 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
645 Schema schema
= Schema::Parse(kSchemaJson
, &error
);
646 ASSERT_TRUE(schema
.valid()) << error
;
648 static const char kPolicyMapJson
[] =
650 " \"PolicyForTesting\": {"
651 " \"OneToThree\": 2,"
652 " \"Colors\": \"White\""
655 scoped_ptr
<base::Value
> policy_map_value
=
656 base::JSONReader::ReadAndReturnError(kPolicyMapJson
, base::JSON_PARSE_RFC
,
658 ASSERT_TRUE(policy_map_value
) << error
;
660 const base::DictionaryValue
* policy_map_dict
= NULL
;
661 ASSERT_TRUE(policy_map_value
->GetAsDictionary(&policy_map_dict
));
663 PolicyMap policy_map
;
665 policy_map_dict
, POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
);
667 TestSchemaValidatingPolicyHandler
handler(schema
, SCHEMA_ALLOW_INVALID
);
668 scoped_ptr
<base::Value
> output_value
;
669 ASSERT_TRUE(handler
.CheckAndGetValueForTest(policy_map
, &output_value
));
670 ASSERT_TRUE(output_value
);
672 base::DictionaryValue
* dict
= NULL
;
673 ASSERT_TRUE(output_value
->GetAsDictionary(&dict
));
675 // Test that CheckAndGetValue() actually dropped invalid properties.
677 EXPECT_TRUE(dict
->GetInteger("OneToThree", &int_value
));
678 EXPECT_EQ(2, int_value
);
679 EXPECT_FALSE(dict
->HasKey("Colors"));
682 TEST(SimpleSchemaValidatingPolicyHandlerTest
, CheckAndGetValue
) {
683 const char policy_name
[] = "PolicyForTesting";
684 static const char kSchemaJson
[] =
686 " \"type\": \"object\","
688 " \"PolicyForTesting\": {"
689 " \"type\": \"object\","
692 " \"type\": \"integer\","
697 " \"type\": \"string\","
698 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
705 Schema schema
= Schema::Parse(kSchemaJson
, &error
);
706 ASSERT_TRUE(schema
.valid()) << error
;
708 static const char kPolicyMapJson
[] =
710 " \"PolicyForTesting\": {"
711 " \"OneToThree\": 2,"
712 " \"Colors\": \"Green\""
715 scoped_ptr
<base::Value
> policy_map_value
=
716 base::JSONReader::ReadAndReturnError(kPolicyMapJson
, base::JSON_PARSE_RFC
,
718 ASSERT_TRUE(policy_map_value
) << error
;
720 const base::DictionaryValue
* policy_map_dict
= NULL
;
721 ASSERT_TRUE(policy_map_value
->GetAsDictionary(&policy_map_dict
));
723 PolicyMap policy_map_recommended
;
724 policy_map_recommended
.LoadFrom(
725 policy_map_dict
, POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
);
727 PolicyMap policy_map_mandatory
;
728 policy_map_mandatory
.LoadFrom(
729 policy_map_dict
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
);
731 SimpleSchemaValidatingPolicyHandler
handler_all(
736 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED
,
737 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED
);
739 SimpleSchemaValidatingPolicyHandler
handler_recommended(
744 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED
,
745 SimpleSchemaValidatingPolicyHandler::MANDATORY_PROHIBITED
);
747 SimpleSchemaValidatingPolicyHandler
handler_mandatory(
752 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_PROHIBITED
,
753 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED
);
755 SimpleSchemaValidatingPolicyHandler
handler_none(
760 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_PROHIBITED
,
761 SimpleSchemaValidatingPolicyHandler::MANDATORY_PROHIBITED
);
763 const base::Value
* value_expected_in_pref
;
764 policy_map_dict
->Get(policy_name
, &value_expected_in_pref
);
766 PolicyErrorMap errors
;
768 base::Value
* value_set_in_pref
;
770 EXPECT_TRUE(handler_all
.CheckPolicySettings(policy_map_mandatory
, &errors
));
771 EXPECT_TRUE(errors
.empty());
773 handler_all
.ApplyPolicySettings(policy_map_mandatory
, &prefs
);
774 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value_set_in_pref
));
775 EXPECT_TRUE(value_expected_in_pref
->Equals(value_set_in_pref
));
778 handler_recommended
.CheckPolicySettings(policy_map_mandatory
, &errors
));
779 EXPECT_FALSE(errors
.empty());
783 handler_mandatory
.CheckPolicySettings(policy_map_mandatory
, &errors
));
784 EXPECT_TRUE(errors
.empty());
786 handler_mandatory
.ApplyPolicySettings(policy_map_mandatory
, &prefs
);
787 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value_set_in_pref
));
788 EXPECT_TRUE(value_expected_in_pref
->Equals(value_set_in_pref
));
790 EXPECT_FALSE(handler_none
.CheckPolicySettings(policy_map_mandatory
, &errors
));
791 EXPECT_FALSE(errors
.empty());
794 EXPECT_TRUE(handler_all
.CheckPolicySettings(policy_map_recommended
, &errors
));
795 EXPECT_TRUE(errors
.empty());
797 handler_all
.ApplyPolicySettings(policy_map_mandatory
, &prefs
);
798 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value_set_in_pref
));
799 EXPECT_TRUE(value_expected_in_pref
->Equals(value_set_in_pref
));
802 handler_mandatory
.CheckPolicySettings(policy_map_recommended
, &errors
));
803 EXPECT_FALSE(errors
.empty());
807 handler_recommended
.CheckPolicySettings(policy_map_recommended
, &errors
));
808 EXPECT_TRUE(errors
.empty());
810 handler_recommended
.ApplyPolicySettings(policy_map_mandatory
, &prefs
);
811 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value_set_in_pref
));
812 EXPECT_TRUE(value_expected_in_pref
->Equals(value_set_in_pref
));
815 handler_none
.CheckPolicySettings(policy_map_recommended
, &errors
));
816 EXPECT_FALSE(errors
.empty());
819 } // namespace policy