| OLD | NEW |
| 1 // Copyright (c) 2012 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/memory/scoped_ptr.h" |
| 8 #include "base/values.h" | 9 #include "base/values.h" |
| 9 #include "chrome/browser/policy/asynchronous_policy_loader.h" | 10 #include "chrome/browser/policy/asynchronous_policy_loader.h" |
| 10 #include "chrome/browser/policy/asynchronous_policy_provider.h" | 11 #include "chrome/browser/policy/asynchronous_policy_provider.h" |
| 11 #include "chrome/browser/policy/configuration_policy_provider.h" | 12 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 13 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 14 #include "chrome/browser/policy/policy_bundle.h" |
| 13 #include "chrome/browser/policy/policy_map.h" | 15 #include "chrome/browser/policy/policy_map.h" |
| 14 #include "policy/policy_constants.h" | 16 #include "policy/policy_constants.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 16 | 18 |
| 17 using ::testing::Mock; | 19 using ::testing::Mock; |
| 18 using ::testing::_; | 20 using ::testing::_; |
| 19 | 21 |
| 20 namespace policy { | 22 namespace policy { |
| 21 | 23 |
| 22 namespace test_policy_definitions { | 24 namespace test_policy_definitions { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 | 56 |
| 55 test_harness_.reset((*GetParam())()); | 57 test_harness_.reset((*GetParam())()); |
| 56 test_harness_->SetUp(); | 58 test_harness_->SetUp(); |
| 57 | 59 |
| 58 provider_.reset( | 60 provider_.reset( |
| 59 test_harness_->CreateProvider(&test_policy_definitions::kList)); | 61 test_harness_->CreateProvider(&test_policy_definitions::kList)); |
| 60 // Some providers do a reload on init. Make sure any notifications generated | 62 // Some providers do a reload on init. Make sure any notifications generated |
| 61 // are fired now. | 63 // are fired now. |
| 62 loop_.RunAllPending(); | 64 loop_.RunAllPending(); |
| 63 | 65 |
| 64 PolicyMap policy_map; | 66 const PolicyBundle kEmptyBundle; |
| 65 EXPECT_TRUE(provider_->Provide(&policy_map)); | 67 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); |
| 66 EXPECT_TRUE(policy_map.empty()); | |
| 67 } | 68 } |
| 68 | 69 |
| 69 void ConfigurationPolicyProviderTest::TearDown() { | 70 void ConfigurationPolicyProviderTest::TearDown() { |
| 70 // Give providers the chance to clean up after themselves on the file thread. | 71 // Give providers the chance to clean up after themselves on the file thread. |
| 71 provider_.reset(); | 72 provider_.reset(); |
| 72 | 73 |
| 73 AsynchronousPolicyTestBase::TearDown(); | 74 AsynchronousPolicyTestBase::TearDown(); |
| 74 } | 75 } |
| 75 | 76 |
| 76 void ConfigurationPolicyProviderTest::CheckValue( | 77 void ConfigurationPolicyProviderTest::CheckValue( |
| 77 const char* policy_name, | 78 const char* policy_name, |
| 78 const base::Value& expected_value, | 79 const base::Value& expected_value, |
| 79 base::Closure install_value) { | 80 base::Closure install_value) { |
| 80 // Install the value, reload policy and check the provider for the value. | 81 // Install the value, reload policy and check the provider for the value. |
| 81 install_value.Run(); | 82 install_value.Run(); |
| 82 provider_->RefreshPolicies(); | 83 provider_->RefreshPolicies(); |
| 83 loop_.RunAllPending(); | 84 loop_.RunAllPending(); |
| 84 PolicyMap policy_map; | 85 PolicyBundle expected_bundle; |
| 85 EXPECT_TRUE(provider_->Provide(&policy_map)); | 86 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") |
| 86 EXPECT_EQ(1U, policy_map.size()); | 87 .Set(policy_name, |
| 87 EXPECT_TRUE(base::Value::Equals(&expected_value, | 88 test_harness_->GetPolicyLevel(), |
| 88 policy_map.GetValue(policy_name))); | 89 test_harness_->GetPolicyScope(), |
| 90 expected_value.DeepCopy()); |
| 91 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); |
| 92 // TODO(joaodasilva): set the policy in the POLICY_DOMAIN_EXTENSIONS too, |
| 93 // and extend the |expected_bundle|, once all providers are ready. |
| 89 } | 94 } |
| 90 | 95 |
| 91 TEST_P(ConfigurationPolicyProviderTest, Empty) { | 96 TEST_P(ConfigurationPolicyProviderTest, Empty) { |
| 92 provider_->RefreshPolicies(); | 97 provider_->RefreshPolicies(); |
| 93 loop_.RunAllPending(); | 98 loop_.RunAllPending(); |
| 94 PolicyMap policy_map; | 99 const PolicyBundle kEmptyBundle; |
| 95 EXPECT_TRUE(provider_->Provide(&policy_map)); | 100 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); |
| 96 EXPECT_TRUE(policy_map.empty()); | |
| 97 } | 101 } |
| 98 | 102 |
| 99 TEST_P(ConfigurationPolicyProviderTest, StringValue) { | 103 TEST_P(ConfigurationPolicyProviderTest, StringValue) { |
| 100 const char kTestString[] = "string_value"; | 104 const char kTestString[] = "string_value"; |
| 101 base::StringValue expected_value(kTestString); | 105 base::StringValue expected_value(kTestString); |
| 102 CheckValue(test_policy_definitions::kKeyString, | 106 CheckValue(test_policy_definitions::kKeyString, |
| 103 expected_value, | 107 expected_value, |
| 104 base::Bind(&PolicyProviderTestHarness::InstallStringPolicy, | 108 base::Bind(&PolicyProviderTestHarness::InstallStringPolicy, |
| 105 base::Unretained(test_harness_.get()), | 109 base::Unretained(test_harness_.get()), |
| 106 test_policy_definitions::kKeyString, | 110 test_policy_definitions::kKeyString, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 170 |
| 167 CheckValue(test_policy_definitions::kKeyDictionary, | 171 CheckValue(test_policy_definitions::kKeyDictionary, |
| 168 expected_value, | 172 expected_value, |
| 169 base::Bind(&PolicyProviderTestHarness::InstallDictionaryPolicy, | 173 base::Bind(&PolicyProviderTestHarness::InstallDictionaryPolicy, |
| 170 base::Unretained(test_harness_.get()), | 174 base::Unretained(test_harness_.get()), |
| 171 test_policy_definitions::kKeyDictionary, | 175 test_policy_definitions::kKeyDictionary, |
| 172 &expected_value)); | 176 &expected_value)); |
| 173 } | 177 } |
| 174 | 178 |
| 175 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { | 179 TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) { |
| 176 PolicyMap policy_map; | 180 PolicyBundle bundle; |
| 177 EXPECT_TRUE(provider_->Provide(&policy_map)); | 181 EXPECT_TRUE(provider_->policies().Equals(bundle)); |
| 178 EXPECT_EQ(0U, policy_map.size()); | |
| 179 | 182 |
| 180 // OnUpdatePolicy is called even when there are no changes. | 183 // OnUpdatePolicy is called even when there are no changes. |
| 181 MockConfigurationPolicyObserver observer; | 184 MockConfigurationPolicyObserver observer; |
| 182 ConfigurationPolicyObserverRegistrar registrar; | 185 ConfigurationPolicyObserverRegistrar registrar; |
| 183 registrar.Init(provider_.get(), &observer); | 186 registrar.Init(provider_.get(), &observer); |
| 184 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 187 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
| 185 provider_->RefreshPolicies(); | 188 provider_->RefreshPolicies(); |
| 186 loop_.RunAllPending(); | 189 loop_.RunAllPending(); |
| 187 Mock::VerifyAndClearExpectations(&observer); | 190 Mock::VerifyAndClearExpectations(&observer); |
| 188 | 191 |
| 189 EXPECT_TRUE(provider_->Provide(&policy_map)); | 192 EXPECT_TRUE(provider_->policies().Equals(bundle)); |
| 190 EXPECT_EQ(0U, policy_map.size()); | |
| 191 | 193 |
| 192 // OnUpdatePolicy is called when there are changes. | 194 // OnUpdatePolicy is called when there are changes. |
| 193 test_harness_->InstallStringPolicy(test_policy_definitions::kKeyString, | 195 test_harness_->InstallStringPolicy(test_policy_definitions::kKeyString, |
| 194 "value"); | 196 "value"); |
| 195 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 197 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
| 196 provider_->RefreshPolicies(); | 198 provider_->RefreshPolicies(); |
| 197 loop_.RunAllPending(); | 199 loop_.RunAllPending(); |
| 198 Mock::VerifyAndClearExpectations(&observer); | 200 Mock::VerifyAndClearExpectations(&observer); |
| 199 | 201 |
| 200 policy_map.Clear(); | 202 bundle.Get(POLICY_DOMAIN_CHROME, "") |
| 201 EXPECT_TRUE(provider_->Provide(&policy_map)); | 203 .Set(test_policy_definitions::kKeyString, |
| 202 EXPECT_EQ(1U, policy_map.size()); | 204 test_harness_->GetPolicyLevel(), |
| 205 test_harness_->GetPolicyScope(), |
| 206 base::Value::CreateStringValue("value")); |
| 207 EXPECT_TRUE(provider_->policies().Equals(bundle)); |
| 203 } | 208 } |
| 204 | 209 |
| 205 TEST(ConfigurationPolicyProviderTest, FixDeprecatedPolicies) { | 210 TEST(ConfigurationPolicyProviderTest, FixDeprecatedPolicies) { |
| 206 PolicyMap policy_map; | 211 PolicyMap policy_map; |
| 207 policy_map.Set(key::kProxyServerMode, | 212 policy_map.Set(key::kProxyServerMode, |
| 208 POLICY_LEVEL_MANDATORY, | 213 POLICY_LEVEL_MANDATORY, |
| 209 POLICY_SCOPE_USER, | 214 POLICY_SCOPE_USER, |
| 210 base::Value::CreateIntegerValue(3)); | 215 base::Value::CreateIntegerValue(3)); |
| 211 | 216 |
| 212 // Both these policies should be ignored, since there's a higher priority | 217 // Both these policies should be ignored, since there's a higher priority |
| 213 // policy available. | 218 // policy available. |
| 214 policy_map.Set(key::kProxyMode, | 219 policy_map.Set(key::kProxyMode, |
| 215 POLICY_LEVEL_RECOMMENDED, | 220 POLICY_LEVEL_RECOMMENDED, |
| 216 POLICY_SCOPE_USER, | 221 POLICY_SCOPE_USER, |
| 217 base::Value::CreateStringValue("pac_script")); | 222 base::Value::CreateStringValue("pac_script")); |
| 218 policy_map.Set(key::kProxyPacUrl, | 223 policy_map.Set(key::kProxyPacUrl, |
| 219 POLICY_LEVEL_RECOMMENDED, | 224 POLICY_LEVEL_RECOMMENDED, |
| 220 POLICY_SCOPE_USER, | 225 POLICY_SCOPE_USER, |
| 221 base::Value::CreateStringValue("http://example.com/wpad.dat")); | 226 base::Value::CreateStringValue("http://example.com/wpad.dat")); |
| 222 | 227 |
| 223 ConfigurationPolicyProvider::FixDeprecatedPolicies(&policy_map); | 228 MockConfigurationPolicyProvider provider; |
| 224 base::DictionaryValue expected; | 229 provider.UpdateChromePolicy(policy_map); |
| 225 expected.SetInteger(key::kProxyServerMode, 3); | 230 |
| 226 EXPECT_EQ(1U, policy_map.size()); | 231 PolicyBundle expected_bundle; |
| 227 EXPECT_TRUE(base::Value::Equals(&expected, | 232 base::DictionaryValue* expected_value = new base::DictionaryValue(); |
| 228 policy_map.GetValue(key::kProxySettings))); | 233 expected_value->SetInteger(key::kProxyServerMode, 3); |
| 234 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") |
| 235 .Set(key::kProxySettings, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 236 expected_value); |
| 237 EXPECT_TRUE(provider.policies().Equals(expected_bundle)); |
| 229 } | 238 } |
| 230 | 239 |
| 231 } // namespace policy | 240 } // namespace policy |
| OLD | NEW |