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/policy_types.h"
15 #include "components/policy/core/common/schema.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 void GetIntegerTypeMap(
23 ScopedVector
<StringMappingListPolicyHandler::MappingEntry
>* result
) {
24 result
->push_back(new StringMappingListPolicyHandler::MappingEntry(
25 "one", scoped_ptr
<base::Value
>(new base::FundamentalValue(1))));
26 result
->push_back(new StringMappingListPolicyHandler::MappingEntry(
27 "two", scoped_ptr
<base::Value
>(new base::FundamentalValue(2))));
30 const char kTestPolicy
[] = "unit_test.test_policy";
31 const char kTestPref
[] = "unit_test.test_pref";
33 class TestSchemaValidatingPolicyHandler
: public SchemaValidatingPolicyHandler
{
35 TestSchemaValidatingPolicyHandler(const Schema
& schema
,
36 SchemaOnErrorStrategy strategy
)
37 : SchemaValidatingPolicyHandler("PolicyForTesting", schema
, strategy
) {}
38 ~TestSchemaValidatingPolicyHandler() override
{}
40 void ApplyPolicySettings(const policy::PolicyMap
&, PrefValueMap
*) override
{}
42 bool CheckAndGetValueForTest(const PolicyMap
& policies
,
43 scoped_ptr
<base::Value
>* value
) {
44 return SchemaValidatingPolicyHandler::CheckAndGetValue(
45 policies
, NULL
, value
);
51 TEST(StringToIntEnumListPolicyHandlerTest
, CheckPolicySettings
) {
54 PolicyErrorMap errors
;
55 StringMappingListPolicyHandler
handler(
58 base::Bind(GetIntegerTypeMap
));
60 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
61 POLICY_SOURCE_CLOUD
, list
.DeepCopy(), nullptr);
63 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
64 EXPECT_TRUE(errors
.empty());
66 list
.AppendString("one");
67 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
68 POLICY_SOURCE_CLOUD
, list
.DeepCopy(), nullptr);
70 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
71 EXPECT_TRUE(errors
.empty());
73 list
.AppendString("invalid");
74 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
75 POLICY_SOURCE_CLOUD
, list
.DeepCopy(), nullptr);
77 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
78 EXPECT_FALSE(errors
.empty());
79 EXPECT_FALSE(errors
.GetErrors(kTestPolicy
).empty());
81 policy_map
.Set(kTestPolicy
,
82 POLICY_LEVEL_MANDATORY
,
85 new base::StringValue("no list"),
88 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
89 EXPECT_FALSE(errors
.empty());
90 EXPECT_FALSE(errors
.GetErrors(kTestPolicy
).empty());
93 TEST(StringMappingListPolicyHandlerTest
, ApplyPolicySettings
) {
95 base::ListValue expected
;
99 StringMappingListPolicyHandler
handler(
102 base::Bind(GetIntegerTypeMap
));
104 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
105 POLICY_SOURCE_CLOUD
, list
.DeepCopy(), nullptr);
106 handler
.ApplyPolicySettings(policy_map
, &prefs
);
107 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
108 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
110 list
.AppendString("two");
111 expected
.AppendInteger(2);
112 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
113 POLICY_SOURCE_CLOUD
, list
.DeepCopy(), nullptr);
114 handler
.ApplyPolicySettings(policy_map
, &prefs
);
115 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
116 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
118 list
.AppendString("invalid");
119 policy_map
.Set(kTestPolicy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
120 POLICY_SOURCE_CLOUD
, list
.DeepCopy(), nullptr);
121 handler
.ApplyPolicySettings(policy_map
, &prefs
);
122 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
123 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
126 TEST(IntRangePolicyHandler
, CheckPolicySettingsClamp
) {
127 PolicyMap policy_map
;
128 PolicyErrorMap errors
;
130 // This tests needs to modify an int policy. The exact policy used and its
131 // semantics outside the test are irrelevant.
132 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, true);
134 // Check that values lying in the accepted range are not rejected.
135 policy_map
.Set(kTestPolicy
,
136 POLICY_LEVEL_MANDATORY
,
139 new base::FundamentalValue(0),
142 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
143 EXPECT_TRUE(errors
.empty());
145 policy_map
.Set(kTestPolicy
,
146 POLICY_LEVEL_MANDATORY
,
149 new base::FundamentalValue(5),
152 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
153 EXPECT_TRUE(errors
.empty());
155 policy_map
.Set(kTestPolicy
,
156 POLICY_LEVEL_MANDATORY
,
159 new base::FundamentalValue(10),
162 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
163 EXPECT_TRUE(errors
.empty());
165 // Check that values lying outside the accepted range are not rejected
166 // (because clamping is enabled) but do yield a warning message.
167 policy_map
.Set(kTestPolicy
,
168 POLICY_LEVEL_MANDATORY
,
171 new base::FundamentalValue(-5),
174 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
175 EXPECT_FALSE(errors
.empty());
177 policy_map
.Set(kTestPolicy
,
178 POLICY_LEVEL_MANDATORY
,
181 new base::FundamentalValue(15),
184 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
185 EXPECT_FALSE(errors
.empty());
187 // Check that an entirely invalid value is rejected and yields an error
189 policy_map
.Set(kTestPolicy
,
190 POLICY_LEVEL_MANDATORY
,
193 new base::StringValue("invalid"),
196 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
197 EXPECT_FALSE(errors
.empty());
200 TEST(IntRangePolicyHandler
, CheckPolicySettingsDontClamp
) {
201 PolicyMap policy_map
;
202 PolicyErrorMap errors
;
204 // This tests needs to modify an int policy. The exact policy used and its
205 // semantics outside the test are irrelevant.
206 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, false);
208 // Check that values lying in the accepted range are not rejected.
209 policy_map
.Set(kTestPolicy
,
210 POLICY_LEVEL_MANDATORY
,
213 new base::FundamentalValue(0),
216 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
217 EXPECT_TRUE(errors
.empty());
219 policy_map
.Set(kTestPolicy
,
220 POLICY_LEVEL_MANDATORY
,
223 new base::FundamentalValue(5),
226 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
227 EXPECT_TRUE(errors
.empty());
229 policy_map
.Set(kTestPolicy
,
230 POLICY_LEVEL_MANDATORY
,
233 new base::FundamentalValue(10),
236 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
237 EXPECT_TRUE(errors
.empty());
239 // Check that values lying outside the accepted range are rejected and yield
241 policy_map
.Set(kTestPolicy
,
242 POLICY_LEVEL_MANDATORY
,
245 new base::FundamentalValue(-5),
248 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
249 EXPECT_FALSE(errors
.empty());
251 policy_map
.Set(kTestPolicy
,
252 POLICY_LEVEL_MANDATORY
,
255 new base::FundamentalValue(15),
258 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
259 EXPECT_FALSE(errors
.empty());
261 // Check that an entirely invalid value is rejected and yields an error
263 policy_map
.Set(kTestPolicy
,
264 POLICY_LEVEL_MANDATORY
,
267 new base::StringValue("invalid"),
270 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
271 EXPECT_FALSE(errors
.empty());
274 TEST(IntRangePolicyHandler
, ApplyPolicySettingsClamp
) {
275 PolicyMap policy_map
;
277 scoped_ptr
<base::Value
> expected
;
278 const base::Value
* value
;
280 // This tests needs to modify an int policy. The exact policy used and its
281 // semantics outside the test are irrelevant.
282 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, true);
284 // Check that values lying in the accepted range are written to the pref.
285 policy_map
.Set(kTestPolicy
,
286 POLICY_LEVEL_MANDATORY
,
289 new base::FundamentalValue(0),
292 handler
.ApplyPolicySettings(policy_map
, &prefs
);
293 expected
.reset(new base::FundamentalValue(0));
294 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
295 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
297 policy_map
.Set(kTestPolicy
,
298 POLICY_LEVEL_MANDATORY
,
301 new base::FundamentalValue(5),
304 handler
.ApplyPolicySettings(policy_map
, &prefs
);
305 expected
.reset(new base::FundamentalValue(5));
306 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
307 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
309 policy_map
.Set(kTestPolicy
,
310 POLICY_LEVEL_MANDATORY
,
313 new base::FundamentalValue(10),
316 handler
.ApplyPolicySettings(policy_map
, &prefs
);
317 expected
.reset(new base::FundamentalValue(10));
318 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
319 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
321 // Check that values lying outside the accepted range are clamped and written
323 policy_map
.Set(kTestPolicy
,
324 POLICY_LEVEL_MANDATORY
,
327 new base::FundamentalValue(-5),
330 handler
.ApplyPolicySettings(policy_map
, &prefs
);
331 expected
.reset(new base::FundamentalValue(0));
332 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
333 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
335 policy_map
.Set(kTestPolicy
,
336 POLICY_LEVEL_MANDATORY
,
339 new base::FundamentalValue(15),
342 handler
.ApplyPolicySettings(policy_map
, &prefs
);
343 expected
.reset(new base::FundamentalValue(10));
344 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
345 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
348 TEST(IntRangePolicyHandler
, ApplyPolicySettingsDontClamp
) {
349 PolicyMap policy_map
;
351 scoped_ptr
<base::Value
> expected
;
352 const base::Value
* value
;
354 // This tests needs to modify an int policy. The exact policy used and its
355 // semantics outside the test are irrelevant.
356 IntRangePolicyHandler
handler(kTestPolicy
, kTestPref
, 0, 10, true);
358 // Check that values lying in the accepted range are written to the pref.
359 policy_map
.Set(kTestPolicy
,
360 POLICY_LEVEL_MANDATORY
,
363 new base::FundamentalValue(0),
366 handler
.ApplyPolicySettings(policy_map
, &prefs
);
367 expected
.reset(new base::FundamentalValue(0));
368 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
369 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
371 policy_map
.Set(kTestPolicy
,
372 POLICY_LEVEL_MANDATORY
,
375 new base::FundamentalValue(5),
378 handler
.ApplyPolicySettings(policy_map
, &prefs
);
379 expected
.reset(new base::FundamentalValue(5));
380 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
381 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
383 policy_map
.Set(kTestPolicy
,
384 POLICY_LEVEL_MANDATORY
,
387 new base::FundamentalValue(10),
390 handler
.ApplyPolicySettings(policy_map
, &prefs
);
391 expected
.reset(new base::FundamentalValue(10));
392 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
393 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
396 TEST(IntPercentageToDoublePolicyHandler
, CheckPolicySettingsClamp
) {
397 PolicyMap policy_map
;
398 PolicyErrorMap errors
;
400 // This tests needs to modify an int policy. The exact policy used and its
401 // semantics outside the test are irrelevant.
402 IntPercentageToDoublePolicyHandler
handler(
403 kTestPolicy
, kTestPref
, 0, 10, true);
405 // Check that values lying in the accepted range are not rejected.
406 policy_map
.Set(kTestPolicy
,
407 POLICY_LEVEL_MANDATORY
,
410 new base::FundamentalValue(0),
413 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
414 EXPECT_TRUE(errors
.empty());
416 policy_map
.Set(kTestPolicy
,
417 POLICY_LEVEL_MANDATORY
,
420 new base::FundamentalValue(5),
423 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
424 EXPECT_TRUE(errors
.empty());
426 policy_map
.Set(kTestPolicy
,
427 POLICY_LEVEL_MANDATORY
,
430 new base::FundamentalValue(10),
433 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
434 EXPECT_TRUE(errors
.empty());
436 // Check that values lying outside the accepted range are not rejected
437 // (because clamping is enabled) but do yield a warning message.
438 policy_map
.Set(kTestPolicy
,
439 POLICY_LEVEL_MANDATORY
,
442 new base::FundamentalValue(-5),
445 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
446 EXPECT_FALSE(errors
.empty());
448 policy_map
.Set(kTestPolicy
,
449 POLICY_LEVEL_MANDATORY
,
452 new base::FundamentalValue(15),
455 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
456 EXPECT_FALSE(errors
.empty());
458 // Check that an entirely invalid value is rejected and yields an error
460 policy_map
.Set(kTestPolicy
,
461 POLICY_LEVEL_MANDATORY
,
464 new base::StringValue("invalid"),
467 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
468 EXPECT_FALSE(errors
.empty());
471 TEST(IntPercentageToDoublePolicyHandler
, CheckPolicySettingsDontClamp
) {
472 PolicyMap policy_map
;
473 PolicyErrorMap errors
;
475 // This tests needs to modify an int policy. The exact policy used and its
476 // semantics outside the test are irrelevant.
477 IntPercentageToDoublePolicyHandler
handler(
478 kTestPolicy
, kTestPref
, 0, 10, false);
480 // Check that values lying in the accepted range are not rejected.
481 policy_map
.Set(kTestPolicy
,
482 POLICY_LEVEL_MANDATORY
,
485 new base::FundamentalValue(0),
488 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
489 EXPECT_TRUE(errors
.empty());
491 policy_map
.Set(kTestPolicy
,
492 POLICY_LEVEL_MANDATORY
,
495 new base::FundamentalValue(5),
498 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
499 EXPECT_TRUE(errors
.empty());
501 policy_map
.Set(kTestPolicy
,
502 POLICY_LEVEL_MANDATORY
,
505 new base::FundamentalValue(10),
508 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
509 EXPECT_TRUE(errors
.empty());
511 // Check that values lying outside the accepted range are rejected and yield
513 policy_map
.Set(kTestPolicy
,
514 POLICY_LEVEL_MANDATORY
,
517 new base::FundamentalValue(-5),
520 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
521 EXPECT_FALSE(errors
.empty());
523 policy_map
.Set(kTestPolicy
,
524 POLICY_LEVEL_MANDATORY
,
527 new base::FundamentalValue(15),
530 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
531 EXPECT_FALSE(errors
.empty());
533 // Check that an entirely invalid value is rejected and yields an error
535 policy_map
.Set(kTestPolicy
,
536 POLICY_LEVEL_MANDATORY
,
539 new base::StringValue("invalid"),
542 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
543 EXPECT_FALSE(errors
.empty());
546 TEST(IntPercentageToDoublePolicyHandler
, ApplyPolicySettingsClamp
) {
547 PolicyMap policy_map
;
549 scoped_ptr
<base::Value
> expected
;
550 const base::Value
* value
;
552 // This tests needs to modify an int policy. The exact policy used and its
553 // semantics outside the test are irrelevant.
554 IntPercentageToDoublePolicyHandler
handler(
555 kTestPolicy
, kTestPref
, 0, 10, true);
557 // Check that values lying in the accepted range are written to the pref.
558 policy_map
.Set(kTestPolicy
,
559 POLICY_LEVEL_MANDATORY
,
562 new base::FundamentalValue(0),
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
,
574 new base::FundamentalValue(5),
577 handler
.ApplyPolicySettings(policy_map
, &prefs
);
578 expected
.reset(new base::FundamentalValue(0.05));
579 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
580 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
582 policy_map
.Set(kTestPolicy
,
583 POLICY_LEVEL_MANDATORY
,
586 new base::FundamentalValue(10),
589 handler
.ApplyPolicySettings(policy_map
, &prefs
);
590 expected
.reset(new base::FundamentalValue(0.1));
591 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
592 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
594 // Check that values lying outside the accepted range are clamped and written
596 policy_map
.Set(kTestPolicy
,
597 POLICY_LEVEL_MANDATORY
,
600 new base::FundamentalValue(-5),
603 handler
.ApplyPolicySettings(policy_map
, &prefs
);
604 expected
.reset(new base::FundamentalValue(0.0));
605 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
606 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
608 policy_map
.Set(kTestPolicy
,
609 POLICY_LEVEL_MANDATORY
,
612 new base::FundamentalValue(15),
615 handler
.ApplyPolicySettings(policy_map
, &prefs
);
616 expected
.reset(new base::FundamentalValue(0.1));
617 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
618 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
621 TEST(IntPercentageToDoublePolicyHandler
, ApplyPolicySettingsDontClamp
) {
622 PolicyMap policy_map
;
624 scoped_ptr
<base::Value
> expected
;
625 const base::Value
* value
;
627 // This tests needs to modify an int policy. The exact policy used and its
628 // semantics outside the test are irrelevant.
629 IntPercentageToDoublePolicyHandler
handler(
630 kTestPolicy
, kTestPref
, 0, 10, true);
632 // Check that values lying in the accepted range are written to the pref.
633 policy_map
.Set(kTestPolicy
,
634 POLICY_LEVEL_MANDATORY
,
637 new base::FundamentalValue(0),
640 handler
.ApplyPolicySettings(policy_map
, &prefs
);
641 expected
.reset(new base::FundamentalValue(0.0));
642 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
643 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
645 policy_map
.Set(kTestPolicy
,
646 POLICY_LEVEL_MANDATORY
,
649 new base::FundamentalValue(5),
652 handler
.ApplyPolicySettings(policy_map
, &prefs
);
653 expected
.reset(new base::FundamentalValue(0.05));
654 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
655 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
657 policy_map
.Set(kTestPolicy
,
658 POLICY_LEVEL_MANDATORY
,
661 new base::FundamentalValue(10),
664 handler
.ApplyPolicySettings(policy_map
, &prefs
);
665 expected
.reset(new base::FundamentalValue(0.1));
666 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
667 EXPECT_TRUE(base::Value::Equals(expected
.get(), value
));
670 TEST(SchemaValidatingPolicyHandlerTest
, CheckAndGetValue
) {
672 static const char kSchemaJson
[] =
674 " \"type\": \"object\","
677 " \"type\": \"integer\","
682 " \"type\": \"string\","
683 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
687 Schema schema
= Schema::Parse(kSchemaJson
, &error
);
688 ASSERT_TRUE(schema
.valid()) << error
;
690 static const char kPolicyMapJson
[] =
692 " \"PolicyForTesting\": {"
693 " \"OneToThree\": 2,"
694 " \"Colors\": \"White\""
697 scoped_ptr
<base::Value
> policy_map_value
=
698 base::JSONReader::ReadAndReturnError(kPolicyMapJson
, base::JSON_PARSE_RFC
,
700 ASSERT_TRUE(policy_map_value
) << error
;
702 const base::DictionaryValue
* policy_map_dict
= NULL
;
703 ASSERT_TRUE(policy_map_value
->GetAsDictionary(&policy_map_dict
));
705 PolicyMap policy_map
;
706 policy_map
.LoadFrom(policy_map_dict
, POLICY_LEVEL_RECOMMENDED
,
707 POLICY_SCOPE_USER
, POLICY_SOURCE_CLOUD
);
709 TestSchemaValidatingPolicyHandler
handler(schema
, SCHEMA_ALLOW_INVALID
);
710 scoped_ptr
<base::Value
> output_value
;
711 ASSERT_TRUE(handler
.CheckAndGetValueForTest(policy_map
, &output_value
));
712 ASSERT_TRUE(output_value
);
714 base::DictionaryValue
* dict
= NULL
;
715 ASSERT_TRUE(output_value
->GetAsDictionary(&dict
));
717 // Test that CheckAndGetValue() actually dropped invalid properties.
719 EXPECT_TRUE(dict
->GetInteger("OneToThree", &int_value
));
720 EXPECT_EQ(2, int_value
);
721 EXPECT_FALSE(dict
->HasKey("Colors"));
724 TEST(SimpleSchemaValidatingPolicyHandlerTest
, CheckAndGetValue
) {
725 const char policy_name
[] = "PolicyForTesting";
726 static const char kSchemaJson
[] =
728 " \"type\": \"object\","
730 " \"PolicyForTesting\": {"
731 " \"type\": \"object\","
734 " \"type\": \"integer\","
739 " \"type\": \"string\","
740 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
747 Schema schema
= Schema::Parse(kSchemaJson
, &error
);
748 ASSERT_TRUE(schema
.valid()) << error
;
750 static const char kPolicyMapJson
[] =
752 " \"PolicyForTesting\": {"
753 " \"OneToThree\": 2,"
754 " \"Colors\": \"Green\""
757 scoped_ptr
<base::Value
> policy_map_value
=
758 base::JSONReader::ReadAndReturnError(kPolicyMapJson
, base::JSON_PARSE_RFC
,
760 ASSERT_TRUE(policy_map_value
) << error
;
762 const base::DictionaryValue
* policy_map_dict
= NULL
;
763 ASSERT_TRUE(policy_map_value
->GetAsDictionary(&policy_map_dict
));
765 PolicyMap policy_map_recommended
;
766 policy_map_recommended
.LoadFrom(
767 policy_map_dict
, POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
,
768 POLICY_SOURCE_CLOUD
);
770 PolicyMap policy_map_mandatory
;
771 policy_map_mandatory
.LoadFrom(
772 policy_map_dict
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
773 POLICY_SOURCE_CLOUD
);
775 SimpleSchemaValidatingPolicyHandler
handler_all(
780 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED
,
781 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED
);
783 SimpleSchemaValidatingPolicyHandler
handler_recommended(
788 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED
,
789 SimpleSchemaValidatingPolicyHandler::MANDATORY_PROHIBITED
);
791 SimpleSchemaValidatingPolicyHandler
handler_mandatory(
796 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_PROHIBITED
,
797 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED
);
799 SimpleSchemaValidatingPolicyHandler
handler_none(
804 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_PROHIBITED
,
805 SimpleSchemaValidatingPolicyHandler::MANDATORY_PROHIBITED
);
807 const base::Value
* value_expected_in_pref
;
808 policy_map_dict
->Get(policy_name
, &value_expected_in_pref
);
810 PolicyErrorMap errors
;
812 base::Value
* value_set_in_pref
;
814 EXPECT_TRUE(handler_all
.CheckPolicySettings(policy_map_mandatory
, &errors
));
815 EXPECT_TRUE(errors
.empty());
817 handler_all
.ApplyPolicySettings(policy_map_mandatory
, &prefs
);
818 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value_set_in_pref
));
819 EXPECT_TRUE(value_expected_in_pref
->Equals(value_set_in_pref
));
822 handler_recommended
.CheckPolicySettings(policy_map_mandatory
, &errors
));
823 EXPECT_FALSE(errors
.empty());
827 handler_mandatory
.CheckPolicySettings(policy_map_mandatory
, &errors
));
828 EXPECT_TRUE(errors
.empty());
830 handler_mandatory
.ApplyPolicySettings(policy_map_mandatory
, &prefs
);
831 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value_set_in_pref
));
832 EXPECT_TRUE(value_expected_in_pref
->Equals(value_set_in_pref
));
834 EXPECT_FALSE(handler_none
.CheckPolicySettings(policy_map_mandatory
, &errors
));
835 EXPECT_FALSE(errors
.empty());
838 EXPECT_TRUE(handler_all
.CheckPolicySettings(policy_map_recommended
, &errors
));
839 EXPECT_TRUE(errors
.empty());
841 handler_all
.ApplyPolicySettings(policy_map_mandatory
, &prefs
);
842 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value_set_in_pref
));
843 EXPECT_TRUE(value_expected_in_pref
->Equals(value_set_in_pref
));
846 handler_mandatory
.CheckPolicySettings(policy_map_recommended
, &errors
));
847 EXPECT_FALSE(errors
.empty());
851 handler_recommended
.CheckPolicySettings(policy_map_recommended
, &errors
));
852 EXPECT_TRUE(errors
.empty());
854 handler_recommended
.ApplyPolicySettings(policy_map_mandatory
, &prefs
);
855 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value_set_in_pref
));
856 EXPECT_TRUE(value_expected_in_pref
->Equals(value_set_in_pref
));
859 handler_none
.CheckPolicySettings(policy_map_recommended
, &errors
));
860 EXPECT_FALSE(errors
.empty());
863 } // namespace policy