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, |
| 610 kTestPref, |
| 611 schema, |
| 612 SCHEMA_STRICT, |
| 613 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED, |
| 614 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED); |
| 615 |
| 616 SimpleSchemaValidatingPolicyHandler handler_recommended( |
| 617 policy_name, |
| 618 kTestPref, |
| 619 schema, |
| 620 SCHEMA_STRICT, |
| 621 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED, |
| 622 SimpleSchemaValidatingPolicyHandler::MANDATORY_PROHIBITED); |
| 623 |
| 624 SimpleSchemaValidatingPolicyHandler handler_mandatory( |
| 625 policy_name, |
| 626 kTestPref, |
| 627 schema, |
| 628 SCHEMA_STRICT, |
| 629 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_PROHIBITED, |
| 630 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED); |
| 631 |
| 632 SimpleSchemaValidatingPolicyHandler handler_none( |
| 633 policy_name, |
| 634 kTestPref, |
| 635 schema, |
| 636 SCHEMA_STRICT, |
| 637 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_PROHIBITED, |
| 638 SimpleSchemaValidatingPolicyHandler::MANDATORY_PROHIBITED); |
| 639 |
| 640 const base::Value* value_expected_in_pref; |
| 641 policy_map_dict->Get(policy_name, &value_expected_in_pref); |
| 642 |
| 643 PolicyErrorMap errors; |
| 644 PrefValueMap prefs; |
| 645 base::Value* value_set_in_pref; |
| 646 |
| 647 EXPECT_TRUE(handler_all.CheckPolicySettings(policy_map_mandatory, &errors)); |
| 648 EXPECT_TRUE(errors.empty()); |
| 649 prefs.Clear(); |
| 650 handler_all.ApplyPolicySettings(policy_map_mandatory, &prefs); |
| 651 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); |
| 652 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); |
| 653 |
| 654 EXPECT_FALSE( |
| 655 handler_recommended.CheckPolicySettings(policy_map_mandatory, &errors)); |
| 656 EXPECT_FALSE(errors.empty()); |
| 657 errors.Clear(); |
| 658 |
| 659 EXPECT_TRUE( |
| 660 handler_mandatory.CheckPolicySettings(policy_map_mandatory, &errors)); |
| 661 EXPECT_TRUE(errors.empty()); |
| 662 prefs.Clear(); |
| 663 handler_mandatory.ApplyPolicySettings(policy_map_mandatory, &prefs); |
| 664 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); |
| 665 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); |
| 666 |
| 667 EXPECT_FALSE(handler_none.CheckPolicySettings(policy_map_mandatory, &errors)); |
| 668 EXPECT_FALSE(errors.empty()); |
| 669 errors.Clear(); |
| 670 |
| 671 EXPECT_TRUE(handler_all.CheckPolicySettings(policy_map_recommended, &errors)); |
| 672 EXPECT_TRUE(errors.empty()); |
| 673 prefs.Clear(); |
| 674 handler_all.ApplyPolicySettings(policy_map_mandatory, &prefs); |
| 675 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); |
| 676 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); |
| 677 |
| 678 EXPECT_FALSE( |
| 679 handler_mandatory.CheckPolicySettings(policy_map_recommended, &errors)); |
| 680 EXPECT_FALSE(errors.empty()); |
| 681 errors.Clear(); |
| 682 |
| 683 EXPECT_TRUE( |
| 684 handler_recommended.CheckPolicySettings(policy_map_recommended, &errors)); |
| 685 EXPECT_TRUE(errors.empty()); |
| 686 prefs.Clear(); |
| 687 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs); |
| 688 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); |
| 689 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); |
| 690 |
| 691 EXPECT_FALSE( |
| 692 handler_none.CheckPolicySettings(policy_map_recommended, &errors)); |
| 693 EXPECT_FALSE(errors.empty()); |
| 694 } |
| 695 |
561 } // namespace policy | 696 } // namespace policy |
OLD | NEW |