| 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 "components/policy/core/browser/configuration_policy_handler.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 5 #include "base/bind.h" | 9 #include "base/bind.h" |
| 6 #include "base/callback.h" | 10 #include "base/callback.h" |
| 7 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/values.h" | 12 #include "base/values.h" |
| 10 #include "components/policy/core/browser/configuration_policy_handler.h" | |
| 11 #include "components/policy/core/browser/policy_error_map.h" | 13 #include "components/policy/core/browser/policy_error_map.h" |
| 12 #include "components/policy/core/common/policy_map.h" | 14 #include "components/policy/core/common/policy_map.h" |
| 13 #include "components/policy/core/common/policy_types.h" | 15 #include "components/policy/core/common/policy_types.h" |
| 14 #include "components/policy/core/common/schema.h" | 16 #include "components/policy/core/common/schema.h" |
| 15 #include "components/prefs/pref_value_map.h" | 17 #include "components/prefs/pref_value_map.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 19 |
| 18 namespace policy { | 20 namespace policy { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 void GetIntegerTypeMap( | 24 void GetIntegerTypeMap( |
| 23 ScopedVector<StringMappingListPolicyHandler::MappingEntry>* result) { | 25 ScopedVector<StringMappingListPolicyHandler::MappingEntry>* result) { |
| 24 result->push_back(new StringMappingListPolicyHandler::MappingEntry( | 26 result->push_back(new StringMappingListPolicyHandler::MappingEntry( |
| 25 "one", scoped_ptr<base::Value>(new base::FundamentalValue(1)))); | 27 "one", std::unique_ptr<base::Value>(new base::FundamentalValue(1)))); |
| 26 result->push_back(new StringMappingListPolicyHandler::MappingEntry( | 28 result->push_back(new StringMappingListPolicyHandler::MappingEntry( |
| 27 "two", scoped_ptr<base::Value>(new base::FundamentalValue(2)))); | 29 "two", std::unique_ptr<base::Value>(new base::FundamentalValue(2)))); |
| 28 } | 30 } |
| 29 | 31 |
| 30 const char kTestPolicy[] = "unit_test.test_policy"; | 32 const char kTestPolicy[] = "unit_test.test_policy"; |
| 31 const char kTestPref[] = "unit_test.test_pref"; | 33 const char kTestPref[] = "unit_test.test_pref"; |
| 32 | 34 |
| 33 class TestSchemaValidatingPolicyHandler : public SchemaValidatingPolicyHandler { | 35 class TestSchemaValidatingPolicyHandler : public SchemaValidatingPolicyHandler { |
| 34 public: | 36 public: |
| 35 TestSchemaValidatingPolicyHandler(const Schema& schema, | 37 TestSchemaValidatingPolicyHandler(const Schema& schema, |
| 36 SchemaOnErrorStrategy strategy) | 38 SchemaOnErrorStrategy strategy) |
| 37 : SchemaValidatingPolicyHandler("PolicyForTesting", schema, strategy) {} | 39 : SchemaValidatingPolicyHandler("PolicyForTesting", schema, strategy) {} |
| 38 ~TestSchemaValidatingPolicyHandler() override {} | 40 ~TestSchemaValidatingPolicyHandler() override {} |
| 39 | 41 |
| 40 void ApplyPolicySettings(const policy::PolicyMap&, PrefValueMap*) override {} | 42 void ApplyPolicySettings(const policy::PolicyMap&, PrefValueMap*) override {} |
| 41 | 43 |
| 42 bool CheckAndGetValueForTest(const PolicyMap& policies, | 44 bool CheckAndGetValueForTest(const PolicyMap& policies, |
| 43 scoped_ptr<base::Value>* value) { | 45 std::unique_ptr<base::Value>* value) { |
| 44 return SchemaValidatingPolicyHandler::CheckAndGetValue( | 46 return SchemaValidatingPolicyHandler::CheckAndGetValue( |
| 45 policies, NULL, value); | 47 policies, NULL, value); |
| 46 } | 48 } |
| 47 }; | 49 }; |
| 48 | 50 |
| 49 } // namespace | 51 } // namespace |
| 50 | 52 |
| 51 TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) { | 53 TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) { |
| 52 base::ListValue list; | 54 base::ListValue list; |
| 53 PolicyMap policy_map; | 55 PolicyMap policy_map; |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 new base::StringValue("invalid"), | 269 new base::StringValue("invalid"), |
| 268 NULL); | 270 NULL); |
| 269 errors.Clear(); | 271 errors.Clear(); |
| 270 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 272 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
| 271 EXPECT_FALSE(errors.empty()); | 273 EXPECT_FALSE(errors.empty()); |
| 272 } | 274 } |
| 273 | 275 |
| 274 TEST(IntRangePolicyHandler, ApplyPolicySettingsClamp) { | 276 TEST(IntRangePolicyHandler, ApplyPolicySettingsClamp) { |
| 275 PolicyMap policy_map; | 277 PolicyMap policy_map; |
| 276 PrefValueMap prefs; | 278 PrefValueMap prefs; |
| 277 scoped_ptr<base::Value> expected; | 279 std::unique_ptr<base::Value> expected; |
| 278 const base::Value* value; | 280 const base::Value* value; |
| 279 | 281 |
| 280 // This tests needs to modify an int policy. The exact policy used and its | 282 // This tests needs to modify an int policy. The exact policy used and its |
| 281 // semantics outside the test are irrelevant. | 283 // semantics outside the test are irrelevant. |
| 282 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true); | 284 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true); |
| 283 | 285 |
| 284 // Check that values lying in the accepted range are written to the pref. | 286 // Check that values lying in the accepted range are written to the pref. |
| 285 policy_map.Set(kTestPolicy, | 287 policy_map.Set(kTestPolicy, |
| 286 POLICY_LEVEL_MANDATORY, | 288 POLICY_LEVEL_MANDATORY, |
| 287 POLICY_SCOPE_USER, | 289 POLICY_SCOPE_USER, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 prefs.Clear(); | 343 prefs.Clear(); |
| 342 handler.ApplyPolicySettings(policy_map, &prefs); | 344 handler.ApplyPolicySettings(policy_map, &prefs); |
| 343 expected.reset(new base::FundamentalValue(10)); | 345 expected.reset(new base::FundamentalValue(10)); |
| 344 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 346 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
| 345 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 347 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
| 346 } | 348 } |
| 347 | 349 |
| 348 TEST(IntRangePolicyHandler, ApplyPolicySettingsDontClamp) { | 350 TEST(IntRangePolicyHandler, ApplyPolicySettingsDontClamp) { |
| 349 PolicyMap policy_map; | 351 PolicyMap policy_map; |
| 350 PrefValueMap prefs; | 352 PrefValueMap prefs; |
| 351 scoped_ptr<base::Value> expected; | 353 std::unique_ptr<base::Value> expected; |
| 352 const base::Value* value; | 354 const base::Value* value; |
| 353 | 355 |
| 354 // This tests needs to modify an int policy. The exact policy used and its | 356 // This tests needs to modify an int policy. The exact policy used and its |
| 355 // semantics outside the test are irrelevant. | 357 // semantics outside the test are irrelevant. |
| 356 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true); | 358 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true); |
| 357 | 359 |
| 358 // Check that values lying in the accepted range are written to the pref. | 360 // Check that values lying in the accepted range are written to the pref. |
| 359 policy_map.Set(kTestPolicy, | 361 policy_map.Set(kTestPolicy, |
| 360 POLICY_LEVEL_MANDATORY, | 362 POLICY_LEVEL_MANDATORY, |
| 361 POLICY_SCOPE_USER, | 363 POLICY_SCOPE_USER, |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 new base::StringValue("invalid"), | 541 new base::StringValue("invalid"), |
| 540 NULL); | 542 NULL); |
| 541 errors.Clear(); | 543 errors.Clear(); |
| 542 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 544 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
| 543 EXPECT_FALSE(errors.empty()); | 545 EXPECT_FALSE(errors.empty()); |
| 544 } | 546 } |
| 545 | 547 |
| 546 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsClamp) { | 548 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsClamp) { |
| 547 PolicyMap policy_map; | 549 PolicyMap policy_map; |
| 548 PrefValueMap prefs; | 550 PrefValueMap prefs; |
| 549 scoped_ptr<base::Value> expected; | 551 std::unique_ptr<base::Value> expected; |
| 550 const base::Value* value; | 552 const base::Value* value; |
| 551 | 553 |
| 552 // This tests needs to modify an int policy. The exact policy used and its | 554 // This tests needs to modify an int policy. The exact policy used and its |
| 553 // semantics outside the test are irrelevant. | 555 // semantics outside the test are irrelevant. |
| 554 IntPercentageToDoublePolicyHandler handler( | 556 IntPercentageToDoublePolicyHandler handler( |
| 555 kTestPolicy, kTestPref, 0, 10, true); | 557 kTestPolicy, kTestPref, 0, 10, true); |
| 556 | 558 |
| 557 // Check that values lying in the accepted range are written to the pref. | 559 // Check that values lying in the accepted range are written to the pref. |
| 558 policy_map.Set(kTestPolicy, | 560 policy_map.Set(kTestPolicy, |
| 559 POLICY_LEVEL_MANDATORY, | 561 POLICY_LEVEL_MANDATORY, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 prefs.Clear(); | 616 prefs.Clear(); |
| 615 handler.ApplyPolicySettings(policy_map, &prefs); | 617 handler.ApplyPolicySettings(policy_map, &prefs); |
| 616 expected.reset(new base::FundamentalValue(0.1)); | 618 expected.reset(new base::FundamentalValue(0.1)); |
| 617 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 619 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
| 618 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 620 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
| 619 } | 621 } |
| 620 | 622 |
| 621 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsDontClamp) { | 623 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsDontClamp) { |
| 622 PolicyMap policy_map; | 624 PolicyMap policy_map; |
| 623 PrefValueMap prefs; | 625 PrefValueMap prefs; |
| 624 scoped_ptr<base::Value> expected; | 626 std::unique_ptr<base::Value> expected; |
| 625 const base::Value* value; | 627 const base::Value* value; |
| 626 | 628 |
| 627 // This tests needs to modify an int policy. The exact policy used and its | 629 // This tests needs to modify an int policy. The exact policy used and its |
| 628 // semantics outside the test are irrelevant. | 630 // semantics outside the test are irrelevant. |
| 629 IntPercentageToDoublePolicyHandler handler( | 631 IntPercentageToDoublePolicyHandler handler( |
| 630 kTestPolicy, kTestPref, 0, 10, true); | 632 kTestPolicy, kTestPref, 0, 10, true); |
| 631 | 633 |
| 632 // Check that values lying in the accepted range are written to the pref. | 634 // Check that values lying in the accepted range are written to the pref. |
| 633 policy_map.Set(kTestPolicy, | 635 policy_map.Set(kTestPolicy, |
| 634 POLICY_LEVEL_MANDATORY, | 636 POLICY_LEVEL_MANDATORY, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 687 Schema schema = Schema::Parse(kSchemaJson, &error); | 689 Schema schema = Schema::Parse(kSchemaJson, &error); |
| 688 ASSERT_TRUE(schema.valid()) << error; | 690 ASSERT_TRUE(schema.valid()) << error; |
| 689 | 691 |
| 690 static const char kPolicyMapJson[] = | 692 static const char kPolicyMapJson[] = |
| 691 "{" | 693 "{" |
| 692 " \"PolicyForTesting\": {" | 694 " \"PolicyForTesting\": {" |
| 693 " \"OneToThree\": 2," | 695 " \"OneToThree\": 2," |
| 694 " \"Colors\": \"White\"" | 696 " \"Colors\": \"White\"" |
| 695 " }" | 697 " }" |
| 696 "}"; | 698 "}"; |
| 697 scoped_ptr<base::Value> policy_map_value = | 699 std::unique_ptr<base::Value> policy_map_value = |
| 698 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC, | 700 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC, |
| 699 NULL, &error); | 701 NULL, &error); |
| 700 ASSERT_TRUE(policy_map_value) << error; | 702 ASSERT_TRUE(policy_map_value) << error; |
| 701 | 703 |
| 702 const base::DictionaryValue* policy_map_dict = NULL; | 704 const base::DictionaryValue* policy_map_dict = NULL; |
| 703 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict)); | 705 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict)); |
| 704 | 706 |
| 705 PolicyMap policy_map; | 707 PolicyMap policy_map; |
| 706 policy_map.LoadFrom(policy_map_dict, POLICY_LEVEL_RECOMMENDED, | 708 policy_map.LoadFrom(policy_map_dict, POLICY_LEVEL_RECOMMENDED, |
| 707 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD); | 709 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD); |
| 708 | 710 |
| 709 TestSchemaValidatingPolicyHandler handler(schema, SCHEMA_ALLOW_INVALID); | 711 TestSchemaValidatingPolicyHandler handler(schema, SCHEMA_ALLOW_INVALID); |
| 710 scoped_ptr<base::Value> output_value; | 712 std::unique_ptr<base::Value> output_value; |
| 711 ASSERT_TRUE(handler.CheckAndGetValueForTest(policy_map, &output_value)); | 713 ASSERT_TRUE(handler.CheckAndGetValueForTest(policy_map, &output_value)); |
| 712 ASSERT_TRUE(output_value); | 714 ASSERT_TRUE(output_value); |
| 713 | 715 |
| 714 base::DictionaryValue* dict = NULL; | 716 base::DictionaryValue* dict = NULL; |
| 715 ASSERT_TRUE(output_value->GetAsDictionary(&dict)); | 717 ASSERT_TRUE(output_value->GetAsDictionary(&dict)); |
| 716 | 718 |
| 717 // Test that CheckAndGetValue() actually dropped invalid properties. | 719 // Test that CheckAndGetValue() actually dropped invalid properties. |
| 718 int int_value = -1; | 720 int int_value = -1; |
| 719 EXPECT_TRUE(dict->GetInteger("OneToThree", &int_value)); | 721 EXPECT_TRUE(dict->GetInteger("OneToThree", &int_value)); |
| 720 EXPECT_EQ(2, int_value); | 722 EXPECT_EQ(2, int_value); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 747 Schema schema = Schema::Parse(kSchemaJson, &error); | 749 Schema schema = Schema::Parse(kSchemaJson, &error); |
| 748 ASSERT_TRUE(schema.valid()) << error; | 750 ASSERT_TRUE(schema.valid()) << error; |
| 749 | 751 |
| 750 static const char kPolicyMapJson[] = | 752 static const char kPolicyMapJson[] = |
| 751 "{" | 753 "{" |
| 752 " \"PolicyForTesting\": {" | 754 " \"PolicyForTesting\": {" |
| 753 " \"OneToThree\": 2," | 755 " \"OneToThree\": 2," |
| 754 " \"Colors\": \"Green\"" | 756 " \"Colors\": \"Green\"" |
| 755 " }" | 757 " }" |
| 756 "}"; | 758 "}"; |
| 757 scoped_ptr<base::Value> policy_map_value = | 759 std::unique_ptr<base::Value> policy_map_value = |
| 758 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC, | 760 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC, |
| 759 NULL, &error); | 761 NULL, &error); |
| 760 ASSERT_TRUE(policy_map_value) << error; | 762 ASSERT_TRUE(policy_map_value) << error; |
| 761 | 763 |
| 762 const base::DictionaryValue* policy_map_dict = NULL; | 764 const base::DictionaryValue* policy_map_dict = NULL; |
| 763 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict)); | 765 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict)); |
| 764 | 766 |
| 765 PolicyMap policy_map_recommended; | 767 PolicyMap policy_map_recommended; |
| 766 policy_map_recommended.LoadFrom( | 768 policy_map_recommended.LoadFrom( |
| 767 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 769 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs); | 856 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs); |
| 855 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); | 857 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); |
| 856 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); | 858 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); |
| 857 | 859 |
| 858 EXPECT_FALSE( | 860 EXPECT_FALSE( |
| 859 handler_none.CheckPolicySettings(policy_map_recommended, &errors)); | 861 handler_none.CheckPolicySettings(policy_map_recommended, &errors)); |
| 860 EXPECT_FALSE(errors.empty()); | 862 EXPECT_FALSE(errors.empty()); |
| 861 } | 863 } |
| 862 | 864 |
| 863 } // namespace policy | 865 } // namespace policy |
| OLD | NEW |