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/callback.h" | 5 #include "base/callback.h" |
6 #include "base/json/json_reader.h" | 6 #include "base/json/json_reader.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/prefs/pref_value_map.h" | 8 #include "base/prefs/pref_value_map.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "components/policy/core/browser/configuration_policy_handler.h" | 10 #include "components/policy/core/browser/configuration_policy_handler.h" |
11 #include "components/policy/core/browser/policy_error_map.h" | 11 #include "components/policy/core/browser/policy_error_map.h" |
12 #include "components/policy/core/common/policy_map.h" | 12 #include "components/policy/core/common/policy_map.h" |
13 #include "components/policy/core/common/schema.h" | 13 #include "components/policy/core/common/schema.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace policy { | 16 namespace policy { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 StringToIntEnumListPolicyHandler::MappingEntry kTestTypeMap[] = { | 20 StringToIntEnumListPolicyHandler::MappingEntry kTestTypeMap[] = { |
21 { "one", 1 }, | 21 { "one", 1 }, |
22 { "two", 2 }, | 22 { "two", 2 }, |
23 }; | 23 }; |
24 | 24 |
25 const char kTestPolicy[] = "unit_test.test_policy"; | 25 const char kTestPolicy[] = "unit_test.test_policy"; |
26 const char kTestPref[] = "unit_test.test_pref"; | 26 const char kTestPref[] = "unit_test.test_pref"; |
27 | 27 |
28 class SimpleSchemaValidatingPolicyHandler | 28 class TestSchemaValidatingPolicyHandler : public SchemaValidatingPolicyHandler { |
29 : public SchemaValidatingPolicyHandler { | |
30 public: | 29 public: |
31 SimpleSchemaValidatingPolicyHandler(const Schema& schema, | 30 TestSchemaValidatingPolicyHandler(const Schema& schema, |
32 SchemaOnErrorStrategy strategy) | 31 SchemaOnErrorStrategy strategy) |
33 : SchemaValidatingPolicyHandler("PolicyForTesting", schema, strategy) {} | 32 : SchemaValidatingPolicyHandler("PolicyForTesting", schema, strategy) {} |
34 virtual ~SimpleSchemaValidatingPolicyHandler() {} | 33 virtual ~TestSchemaValidatingPolicyHandler() {} |
35 | 34 |
36 virtual void ApplyPolicySettings(const policy::PolicyMap&, | 35 virtual void ApplyPolicySettings(const policy::PolicyMap&, |
37 PrefValueMap*) OVERRIDE { | 36 PrefValueMap*) OVERRIDE { |
38 } | 37 } |
39 | 38 |
40 bool CheckAndGetValueForTest(const PolicyMap& policies, | 39 bool CheckAndGetValueForTest(const PolicyMap& policies, |
41 scoped_ptr<base::Value>* value) { | 40 scoped_ptr<base::Value>* value) { |
42 return SchemaValidatingPolicyHandler::CheckAndGetValue( | 41 return SchemaValidatingPolicyHandler::CheckAndGetValue( |
43 policies, NULL, value); | 42 policies, NULL, value); |
44 } | 43 } |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
536 kPolicyMapJson, base::JSON_PARSE_RFC, NULL, &error)); | 535 kPolicyMapJson, base::JSON_PARSE_RFC, NULL, &error)); |
537 ASSERT_TRUE(policy_map_value) << error; | 536 ASSERT_TRUE(policy_map_value) << error; |
538 | 537 |
539 const base::DictionaryValue* policy_map_dict = NULL; | 538 const base::DictionaryValue* policy_map_dict = NULL; |
540 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict)); | 539 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict)); |
541 | 540 |
542 PolicyMap policy_map; | 541 PolicyMap policy_map; |
543 policy_map.LoadFrom( | 542 policy_map.LoadFrom( |
544 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); | 543 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); |
545 | 544 |
546 SimpleSchemaValidatingPolicyHandler handler(schema, SCHEMA_ALLOW_INVALID); | 545 TestSchemaValidatingPolicyHandler handler(schema, SCHEMA_ALLOW_INVALID); |
547 scoped_ptr<base::Value> output_value; | 546 scoped_ptr<base::Value> output_value; |
548 ASSERT_TRUE(handler.CheckAndGetValueForTest(policy_map, &output_value)); | 547 ASSERT_TRUE(handler.CheckAndGetValueForTest(policy_map, &output_value)); |
549 ASSERT_TRUE(output_value); | 548 ASSERT_TRUE(output_value); |
550 | 549 |
551 base::DictionaryValue* dict = NULL; | 550 base::DictionaryValue* dict = NULL; |
552 ASSERT_TRUE(output_value->GetAsDictionary(&dict)); | 551 ASSERT_TRUE(output_value->GetAsDictionary(&dict)); |
553 | 552 |
554 // Test that CheckAndGetValue() actually dropped invalid properties. | 553 // Test that CheckAndGetValue() actually dropped invalid properties. |
555 int int_value = -1; | 554 int int_value = -1; |
556 EXPECT_TRUE(dict->GetInteger("OneToThree", &int_value)); | 555 EXPECT_TRUE(dict->GetInteger("OneToThree", &int_value)); |
557 EXPECT_EQ(2, int_value); | 556 EXPECT_EQ(2, int_value); |
558 EXPECT_FALSE(dict->HasKey("Colors")); | 557 EXPECT_FALSE(dict->HasKey("Colors")); |
559 } | 558 } |
560 | 559 |
560 TEST(SimpleSchemaValidatingPolicyHandlerTest, CheckAndGetValue) { | |
561 const char policy_name[] = "PolicyForTesting"; | |
562 static const char kSchemaJson[] = | |
563 "{" | |
564 " \"type\": \"object\"," | |
565 " \"properties\": {" | |
566 " \"PolicyForTesting\": {" | |
567 " \"type\": \"object\"," | |
568 " \"properties\": {" | |
569 " \"OneToThree\": {" | |
570 " \"type\": \"integer\"," | |
571 " \"minimum\": 1," | |
572 " \"maximum\": 3" | |
573 " }," | |
574 " \"Colors\": {" | |
575 " \"type\": \"string\"," | |
576 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]" | |
577 " }" | |
578 " }" | |
579 " }" | |
580 " }" | |
581 "}"; | |
582 std::string error; | |
583 Schema schema = Schema::Parse(kSchemaJson, &error); | |
584 ASSERT_TRUE(schema.valid()) << error; | |
585 | |
586 static const char kPolicyMapJson[] = | |
587 "{" | |
588 " \"PolicyForTesting\": {" | |
589 " \"OneToThree\": 2," | |
590 " \"Colors\": \"Green\"" | |
591 " }" | |
592 "}"; | |
593 scoped_ptr<base::Value> policy_map_value(base::JSONReader::ReadAndReturnError( | |
594 kPolicyMapJson, base::JSON_PARSE_RFC, NULL, &error)); | |
595 ASSERT_TRUE(policy_map_value) << error; | |
596 | |
597 const base::DictionaryValue* policy_map_dict = NULL; | |
598 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict)); | |
599 | |
600 PolicyMap policy_map_recommended; | |
601 policy_map_recommended.LoadFrom( | |
602 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); | |
603 | |
604 PolicyMap policy_map_mandatory; | |
605 policy_map_mandatory.LoadFrom( | |
606 policy_map_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | |
607 | |
608 SimpleSchemaValidatingPolicyHandler handler_all( | |
609 policy_name, kTestPref, schema, SCHEMA_STRICT, true, true); | |
610 | |
611 SimpleSchemaValidatingPolicyHandler handler_recommended( | |
612 policy_name, kTestPref, schema, SCHEMA_STRICT, true, false); | |
613 | |
614 SimpleSchemaValidatingPolicyHandler handler_mandatory( | |
615 policy_name, kTestPref, schema, SCHEMA_STRICT, false, true); | |
616 | |
617 SimpleSchemaValidatingPolicyHandler handler_none( | |
618 policy_name, kTestPref, schema, SCHEMA_STRICT, false, false); | |
619 | |
620 const base::Value* value_expected_in_pref; | |
621 policy_map_dict->Get(policy_name, &value_expected_in_pref); | |
622 | |
623 PolicyErrorMap errors; | |
624 PrefValueMap prefs; | |
625 base::Value* value_set_in_pref; | |
626 | |
627 EXPECT_TRUE(handler_all.CheckPolicySettings(policy_map_mandatory, &errors)); | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that |errors| is empty.
kaliamoorthi
2014/06/13 13:22:27
Done.
| |
628 prefs.Clear(); | |
629 handler_all.ApplyPolicySettings(policy_map_mandatory, &prefs); | |
630 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that the pref value was set as mandato
kaliamoorthi
2014/06/13 13:22:28
We discussed this.
| |
631 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); | |
632 | |
633 EXPECT_FALSE( | |
634 handler_recommended.CheckPolicySettings(policy_map_mandatory, &errors)); | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that |errors| is not empty.
kaliamoorthi
2014/06/13 13:22:28
Done.
| |
635 | |
636 EXPECT_TRUE( | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that |errors| is empty.
kaliamoorthi
2014/06/13 13:22:27
Done.
| |
637 handler_mandatory.CheckPolicySettings(policy_map_mandatory, &errors)); | |
638 prefs.Clear(); | |
639 handler_mandatory.ApplyPolicySettings(policy_map_mandatory, &prefs); | |
640 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that the pref value was set as mandato
kaliamoorthi
2014/06/13 13:22:28
We discussed this
| |
641 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); | |
642 | |
643 EXPECT_FALSE(handler_none.CheckPolicySettings(policy_map_mandatory, &errors)); | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that |errors| is not empty.
kaliamoorthi
2014/06/13 13:22:28
Done.
| |
644 | |
645 EXPECT_TRUE(handler_all.CheckPolicySettings(policy_map_recommended, &errors)); | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that |errors| is empty.
kaliamoorthi
2014/06/13 13:22:27
Done.
| |
646 prefs.Clear(); | |
647 handler_all.ApplyPolicySettings(policy_map_mandatory, &prefs); | |
648 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that the pref value was set as recomme
kaliamoorthi
2014/06/13 13:22:27
We discussed this
| |
649 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); | |
650 | |
651 EXPECT_FALSE( | |
652 handler_mandatory.CheckPolicySettings(policy_map_recommended, &errors)); | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that |errors| is not empty.
kaliamoorthi
2014/06/13 13:22:27
Done.
| |
653 | |
654 EXPECT_TRUE( | |
655 handler_recommended.CheckPolicySettings(policy_map_recommended, &errors)); | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that |errors| is empty.
kaliamoorthi
2014/06/13 13:22:28
Done.
| |
656 prefs.Clear(); | |
657 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs); | |
658 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that the pref value was set as recomme
kaliamoorthi
2014/06/13 13:22:28
We discussed this
| |
659 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); | |
660 | |
661 EXPECT_FALSE( | |
662 handler_none.CheckPolicySettings(policy_map_recommended, &errors)); | |
bartfab (slow)
2014/06/11 10:08:44
Nit: Verify that |errors| is not empty.
kaliamoorthi
2014/06/13 13:22:28
Done.
| |
663 } | |
664 | |
561 } // namespace policy | 665 } // namespace policy |
OLD | NEW |