| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/policy/configuration_policy_provider_test.h" | 5 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/policy/asynchronous_policy_loader.h" | 9 #include "chrome/browser/policy/asynchronous_policy_loader.h" |
| 10 #include "chrome/browser/policy/asynchronous_policy_provider.h" | 10 #include "chrome/browser/policy/asynchronous_policy_provider.h" |
| 11 #include "chrome/browser/policy/configuration_policy_provider.h" | 11 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 13 #include "chrome/browser/policy/policy_map.h" | 13 #include "chrome/browser/policy/policy_map.h" |
| 14 #include "policy/policy_constants.h" | 14 #include "policy/policy_constants.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 | 16 |
| 17 using ::testing::Mock; | 17 using ::testing::Mock; |
| 18 using ::testing::_; | 18 using ::testing::_; |
| 19 | 19 |
| 20 namespace policy { | 20 namespace policy { |
| 21 | 21 |
| 22 namespace test_policy_definitions { | 22 namespace test_policy_definitions { |
| 23 | 23 |
| 24 const char kKeyString[] = "StringPolicy"; | 24 const char kKeyString[] = "StringPolicy"; |
| 25 const char kKeyBoolean[] = "BooleanPolicy"; | 25 const char kKeyBoolean[] = "BooleanPolicy"; |
| 26 const char kKeyInteger[] = "IntegerPolicy"; | 26 const char kKeyInteger[] = "IntegerPolicy"; |
| 27 const char kKeyStringList[] = "StringListPolicy"; | 27 const char kKeyStringList[] = "StringListPolicy"; |
| 28 | 28 |
| 29 // In order to have correct enum values, we alias these to some actual policies. | |
| 30 const ConfigurationPolicyType kPolicyString = kPolicyHomepageLocation; | |
| 31 const ConfigurationPolicyType kPolicyBoolean = kPolicyHomepageIsNewTabPage; | |
| 32 const ConfigurationPolicyType kPolicyInteger = kPolicyRestoreOnStartup; | |
| 33 const ConfigurationPolicyType kPolicyStringList = kPolicyRestoreOnStartupURLs; | |
| 34 | |
| 35 static const PolicyDefinitionList::Entry kEntries[] = { | 29 static const PolicyDefinitionList::Entry kEntries[] = { |
| 36 { kPolicyString, base::Value::TYPE_STRING, kKeyString }, | 30 { kKeyString, base::Value::TYPE_STRING }, |
| 37 { kPolicyBoolean, base::Value::TYPE_BOOLEAN, kKeyBoolean }, | 31 { kKeyBoolean, base::Value::TYPE_BOOLEAN }, |
| 38 { kPolicyInteger, base::Value::TYPE_INTEGER, kKeyInteger }, | 32 { kKeyInteger, base::Value::TYPE_INTEGER }, |
| 39 { kPolicyStringList, base::Value::TYPE_LIST, kKeyStringList }, | 33 { kKeyStringList, base::Value::TYPE_LIST }, |
| 40 }; | 34 }; |
| 41 | 35 |
| 42 const PolicyDefinitionList kList = { | 36 const PolicyDefinitionList kList = { |
| 43 kEntries, kEntries + arraysize(kEntries) | 37 kEntries, kEntries + arraysize(kEntries) |
| 44 }; | 38 }; |
| 45 | 39 |
| 46 } // namespace test_policy_definitions | 40 } // namespace test_policy_definitions |
| 47 | 41 |
| 48 PolicyProviderTestHarness::PolicyProviderTestHarness() {} | 42 PolicyProviderTestHarness::PolicyProviderTestHarness() {} |
| 49 | 43 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 72 | 66 |
| 73 void ConfigurationPolicyProviderTest::TearDown() { | 67 void ConfigurationPolicyProviderTest::TearDown() { |
| 74 // Give providers the chance to clean up after themselves on the file thread. | 68 // Give providers the chance to clean up after themselves on the file thread. |
| 75 provider_.reset(); | 69 provider_.reset(); |
| 76 | 70 |
| 77 AsynchronousPolicyTestBase::TearDown(); | 71 AsynchronousPolicyTestBase::TearDown(); |
| 78 } | 72 } |
| 79 | 73 |
| 80 void ConfigurationPolicyProviderTest::CheckValue( | 74 void ConfigurationPolicyProviderTest::CheckValue( |
| 81 const char* policy_name, | 75 const char* policy_name, |
| 82 ConfigurationPolicyType policy_type, | |
| 83 const base::Value& expected_value, | 76 const base::Value& expected_value, |
| 84 base::Closure install_value) { | 77 base::Closure install_value) { |
| 85 // Install the value, reload policy and check the provider for the value. | 78 // Install the value, reload policy and check the provider for the value. |
| 86 install_value.Run(); | 79 install_value.Run(); |
| 87 provider_->RefreshPolicies(); | 80 provider_->RefreshPolicies(); |
| 88 loop_.RunAllPending(); | 81 loop_.RunAllPending(); |
| 89 PolicyMap policy_map; | 82 PolicyMap policy_map; |
| 90 EXPECT_TRUE(provider_->Provide(&policy_map)); | 83 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 91 EXPECT_EQ(1U, policy_map.size()); | 84 EXPECT_EQ(1U, policy_map.size()); |
| 92 EXPECT_TRUE(base::Value::Equals(&expected_value, | 85 EXPECT_TRUE(base::Value::Equals(&expected_value, |
| 93 policy_map.Get(policy_type))); | 86 policy_map.GetValue(policy_name))); |
| 94 } | 87 } |
| 95 | 88 |
| 96 TEST_P(ConfigurationPolicyProviderTest, Empty) { | 89 TEST_P(ConfigurationPolicyProviderTest, Empty) { |
| 97 provider_->RefreshPolicies(); | 90 provider_->RefreshPolicies(); |
| 98 loop_.RunAllPending(); | 91 loop_.RunAllPending(); |
| 99 PolicyMap policy_map; | 92 PolicyMap policy_map; |
| 100 EXPECT_TRUE(provider_->Provide(&policy_map)); | 93 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 101 EXPECT_TRUE(policy_map.empty()); | 94 EXPECT_TRUE(policy_map.empty()); |
| 102 } | 95 } |
| 103 | 96 |
| 104 TEST_P(ConfigurationPolicyProviderTest, StringValue) { | 97 TEST_P(ConfigurationPolicyProviderTest, StringValue) { |
| 105 const char kTestString[] = "string_value"; | 98 const char kTestString[] = "string_value"; |
| 106 StringValue expected_value(kTestString); | 99 StringValue expected_value(kTestString); |
| 107 CheckValue(test_policy_definitions::kKeyString, | 100 CheckValue(test_policy_definitions::kKeyString, |
| 108 test_policy_definitions::kPolicyString, | |
| 109 expected_value, | 101 expected_value, |
| 110 base::Bind(&PolicyProviderTestHarness::InstallStringPolicy, | 102 base::Bind(&PolicyProviderTestHarness::InstallStringPolicy, |
| 111 base::Unretained(test_harness_.get()), | 103 base::Unretained(test_harness_.get()), |
| 112 test_policy_definitions::kKeyString, | 104 test_policy_definitions::kKeyString, |
| 113 kTestString)); | 105 kTestString)); |
| 114 } | 106 } |
| 115 | 107 |
| 116 TEST_P(ConfigurationPolicyProviderTest, BooleanValue) { | 108 TEST_P(ConfigurationPolicyProviderTest, BooleanValue) { |
| 117 base::FundamentalValue expected_value(true); | 109 base::FundamentalValue expected_value(true); |
| 118 CheckValue(test_policy_definitions::kKeyBoolean, | 110 CheckValue(test_policy_definitions::kKeyBoolean, |
| 119 test_policy_definitions::kPolicyBoolean, | |
| 120 expected_value, | 111 expected_value, |
| 121 base::Bind(&PolicyProviderTestHarness::InstallBooleanPolicy, | 112 base::Bind(&PolicyProviderTestHarness::InstallBooleanPolicy, |
| 122 base::Unretained(test_harness_.get()), | 113 base::Unretained(test_harness_.get()), |
| 123 test_policy_definitions::kKeyBoolean, | 114 test_policy_definitions::kKeyBoolean, |
| 124 true)); | 115 true)); |
| 125 } | 116 } |
| 126 | 117 |
| 127 TEST_P(ConfigurationPolicyProviderTest, IntegerValue) { | 118 TEST_P(ConfigurationPolicyProviderTest, IntegerValue) { |
| 128 base::FundamentalValue expected_value(42); | 119 base::FundamentalValue expected_value(42); |
| 129 CheckValue(test_policy_definitions::kKeyInteger, | 120 CheckValue(test_policy_definitions::kKeyInteger, |
| 130 test_policy_definitions::kPolicyInteger, | |
| 131 expected_value, | 121 expected_value, |
| 132 base::Bind(&PolicyProviderTestHarness::InstallIntegerPolicy, | 122 base::Bind(&PolicyProviderTestHarness::InstallIntegerPolicy, |
| 133 base::Unretained(test_harness_.get()), | 123 base::Unretained(test_harness_.get()), |
| 134 test_policy_definitions::kKeyInteger, | 124 test_policy_definitions::kKeyInteger, |
| 135 42)); | 125 42)); |
| 136 } | 126 } |
| 137 | 127 |
| 138 TEST_P(ConfigurationPolicyProviderTest, StringListValue) { | 128 TEST_P(ConfigurationPolicyProviderTest, StringListValue) { |
| 139 base::ListValue expected_value; | 129 base::ListValue expected_value; |
| 140 expected_value.Set(0U, base::Value::CreateStringValue("first")); | 130 expected_value.Set(0U, base::Value::CreateStringValue("first")); |
| 141 expected_value.Set(1U, base::Value::CreateStringValue("second")); | 131 expected_value.Set(1U, base::Value::CreateStringValue("second")); |
| 142 CheckValue(test_policy_definitions::kKeyStringList, | 132 CheckValue(test_policy_definitions::kKeyStringList, |
| 143 test_policy_definitions::kPolicyStringList, | |
| 144 expected_value, | 133 expected_value, |
| 145 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, | 134 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, |
| 146 base::Unretained(test_harness_.get()), | 135 base::Unretained(test_harness_.get()), |
| 147 test_policy_definitions::kKeyStringList, | 136 test_policy_definitions::kKeyStringList, |
| 148 &expected_value)); | 137 &expected_value)); |
| 149 } | 138 } |
| 150 | 139 |
| 151 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { | 140 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { |
| 152 PolicyMap policy_map; | 141 PolicyMap policy_map; |
| 153 EXPECT_TRUE(provider_->Provide(&policy_map)); | 142 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 171 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 160 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
| 172 provider_->RefreshPolicies(); | 161 provider_->RefreshPolicies(); |
| 173 loop_.RunAllPending(); | 162 loop_.RunAllPending(); |
| 174 Mock::VerifyAndClearExpectations(&observer); | 163 Mock::VerifyAndClearExpectations(&observer); |
| 175 | 164 |
| 176 policy_map.Clear(); | 165 policy_map.Clear(); |
| 177 EXPECT_TRUE(provider_->Provide(&policy_map)); | 166 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 178 EXPECT_EQ(1U, policy_map.size()); | 167 EXPECT_EQ(1U, policy_map.size()); |
| 179 } | 168 } |
| 180 | 169 |
| 170 TEST(ConfigurationPolicyProviderTest, FixDeprecatedPolicies) { |
| 171 PolicyMap policy_map; |
| 172 policy_map.Set(key::kProxyServerMode, |
| 173 POLICY_LEVEL_MANDATORY, |
| 174 POLICY_SCOPE_USER, |
| 175 Value::CreateIntegerValue(3)); |
| 176 |
| 177 // Both these policies should be ignored, since there's a higher priority |
| 178 // policy available. |
| 179 policy_map.Set(key::kProxyMode, |
| 180 POLICY_LEVEL_RECOMMENDED, |
| 181 POLICY_SCOPE_USER, |
| 182 Value::CreateStringValue("pac_script")); |
| 183 policy_map.Set(key::kProxyPacUrl, |
| 184 POLICY_LEVEL_RECOMMENDED, |
| 185 POLICY_SCOPE_USER, |
| 186 Value::CreateStringValue("http://proxy.example.com/wpad.dat")); |
| 187 |
| 188 ConfigurationPolicyProvider::FixDeprecatedPolicies(&policy_map); |
| 189 DictionaryValue expected; |
| 190 expected.SetInteger(key::kProxyServerMode, 3); |
| 191 EXPECT_EQ(1U, policy_map.size()); |
| 192 EXPECT_TRUE(Value::Equals(&expected, |
| 193 policy_map.GetValue(key::kProxySettings))); |
| 194 } |
| 195 |
| 181 } // namespace policy | 196 } // namespace policy |
| OLD | NEW |