| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/cloud_policy_provider.h" | 5 #include "chrome/browser/policy/cloud_policy_provider.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/policy/browser_policy_connector.h" |
| 9 #include "chrome/browser/policy/cloud_policy_cache_base.h" | 10 #include "chrome/browser/policy/cloud_policy_cache_base.h" |
| 10 #include "chrome/browser/policy/cloud_policy_provider_impl.h" | 11 #include "chrome/browser/policy/cloud_policy_provider_impl.h" |
| 11 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 12 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 13 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 14 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 12 #include "policy/policy_constants.h" | 15 #include "policy/policy_constants.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 14 | 17 |
| 15 using testing::AnyNumber; | 18 using testing::AnyNumber; |
| 19 using testing::Mock; |
| 16 using testing::_; | 20 using testing::_; |
| 17 | 21 |
| 18 namespace policy { | 22 namespace policy { |
| 19 | 23 |
| 20 class MockCloudPolicyCache : public CloudPolicyCacheBase { | 24 class MockCloudPolicyCache : public CloudPolicyCacheBase { |
| 21 public: | 25 public: |
| 22 MockCloudPolicyCache() {} | 26 MockCloudPolicyCache() {} |
| 23 virtual ~MockCloudPolicyCache() {} | 27 virtual ~MockCloudPolicyCache() {} |
| 24 | 28 |
| 25 // CloudPolicyCacheBase implementation. | 29 // CloudPolicyCacheBase implementation. |
| 26 void Load() {} | 30 void Load() OVERRIDE {} |
| 27 void SetPolicy(const em::PolicyFetchResponse& policy) {} | 31 void SetPolicy(const em::PolicyFetchResponse& policy) OVERRIDE {} |
| 28 bool DecodePolicyData(const em::PolicyData& policy_data, | 32 bool DecodePolicyData(const em::PolicyData& policy_data, |
| 29 PolicyMap* mandatory, | 33 PolicyMap* mandatory, |
| 30 PolicyMap* recommended) { | 34 PolicyMap* recommended) OVERRIDE { |
| 31 return true; | |
| 32 } | |
| 33 bool IsReady() { | |
| 34 return true; | 35 return true; |
| 35 } | 36 } |
| 36 | 37 |
| 38 void SetUnmanaged() OVERRIDE { |
| 39 is_unmanaged_ = true; |
| 40 } |
| 41 |
| 37 // Non-const accessors for underlying PolicyMaps. | 42 // Non-const accessors for underlying PolicyMaps. |
| 38 PolicyMap* raw_mandatory_policy() { | 43 PolicyMap* raw_mandatory_policy() { |
| 39 return &mandatory_policy_; | 44 return &mandatory_policy_; |
| 40 } | 45 } |
| 41 | 46 |
| 42 PolicyMap* raw_recommended_policy() { | 47 PolicyMap* raw_recommended_policy() { |
| 43 return &recommended_policy_; | 48 return &recommended_policy_; |
| 44 } | 49 } |
| 45 | 50 |
| 46 void SetUnmanaged() { | |
| 47 is_unmanaged_ = true; | |
| 48 } | |
| 49 | |
| 50 void SetFetchingDone() { | |
| 51 // Implement pure virtual method. | |
| 52 } | |
| 53 | |
| 54 void set_initialized(bool initialized) { | 51 void set_initialized(bool initialized) { |
| 55 initialization_complete_ = initialized; | 52 initialization_complete_ = initialized; |
| 56 } | 53 } |
| 57 | 54 |
| 58 private: | 55 private: |
| 59 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyCache); | 56 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyCache); |
| 60 }; | 57 }; |
| 61 | 58 |
| 62 class CloudPolicyProviderTest : public testing::Test { | 59 class CloudPolicyProviderTest : public testing::Test { |
| 63 protected: | 60 protected: |
| 64 void CreateCloudPolicyProvider(CloudPolicyCacheBase::PolicyLevel level) { | 61 void CreateCloudPolicyProvider(CloudPolicyCacheBase::PolicyLevel level) { |
| 65 cloud_policy_provider_.reset(new CloudPolicyProviderImpl( | 62 cloud_policy_provider_.reset( |
| 66 GetChromePolicyDefinitionList(), level)); | 63 new CloudPolicyProviderImpl( |
| 64 &browser_policy_connector_, |
| 65 GetChromePolicyDefinitionList(), |
| 66 level)); |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Appends the caches to a provider and then provides the policies to | 69 // Appends the caches to a provider and then provides the policies to |
| 70 // |policy_map_|. | 70 // |policy_map_|. |
| 71 void RunCachesThroughProvider(MockCloudPolicyCache caches[], int n, | 71 void RunCachesThroughProvider(MockCloudPolicyCache caches[], int n, |
| 72 CloudPolicyCacheBase::PolicyLevel level) { | 72 CloudPolicyCacheBase::PolicyLevel level) { |
| 73 CloudPolicyProviderImpl provider( | 73 CloudPolicyProviderImpl provider( |
| 74 &browser_policy_connector_, |
| 74 GetChromePolicyDefinitionList(), | 75 GetChromePolicyDefinitionList(), |
| 75 level); | 76 level); |
| 76 for (int i = 0; i < n; i++) { | 77 for (int i = 0; i < n; i++) { |
| 77 provider.AppendCache(&caches[i]); | 78 provider.AppendCache(&caches[i]); |
| 78 } | 79 } |
| 79 policy_map_.reset(new PolicyMap()); | 80 policy_map_.reset(new PolicyMap()); |
| 80 provider.Provide(policy_map_.get()); | 81 provider.Provide(policy_map_.get()); |
| 81 } | 82 } |
| 82 | 83 |
| 83 // Checks a string policy in |policy_map_|. | 84 // Checks a string policy in |policy_map_|. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 103 EXPECT_TRUE(NULL == policy_map_->Get(type)); | 104 EXPECT_TRUE(NULL == policy_map_->Get(type)); |
| 104 } | 105 } |
| 105 | 106 |
| 106 void CombineTwoPolicyMaps(const PolicyMap& base, | 107 void CombineTwoPolicyMaps(const PolicyMap& base, |
| 107 const PolicyMap& overlay, | 108 const PolicyMap& overlay, |
| 108 PolicyMap* out_map) { | 109 PolicyMap* out_map) { |
| 109 DCHECK(cloud_policy_provider_.get()); | 110 DCHECK(cloud_policy_provider_.get()); |
| 110 cloud_policy_provider_->CombineTwoPolicyMaps(base, overlay, out_map); | 111 cloud_policy_provider_->CombineTwoPolicyMaps(base, overlay, out_map); |
| 111 } | 112 } |
| 112 | 113 |
| 114 scoped_ptr<CloudPolicyProviderImpl> cloud_policy_provider_; |
| 115 |
| 113 private: | 116 private: |
| 114 // Some tests need a list of policies that doesn't contain any proxy | 117 BrowserPolicyConnector browser_policy_connector_; |
| 115 // policies. Note: these policies will be handled as if they had the | |
| 116 // type of Value::TYPE_INTEGER. | |
| 117 static const ConfigurationPolicyType simple_policies[]; | |
| 118 | |
| 119 scoped_ptr<CloudPolicyProviderImpl> cloud_policy_provider_; | |
| 120 scoped_ptr<PolicyMap> policy_map_; | 118 scoped_ptr<PolicyMap> policy_map_; |
| 121 }; | 119 }; |
| 122 | 120 |
| 123 // Proxy setting distributed over multiple caches. | 121 // Proxy setting distributed over multiple caches. |
| 124 TEST_F(CloudPolicyProviderTest, | 122 TEST_F(CloudPolicyProviderTest, |
| 125 ProxySettingDistributedOverMultipleCaches) { | 123 ProxySettingDistributedOverMultipleCaches) { |
| 126 // There are proxy_policy_count()+1 = 6 caches and they are mixed together by | 124 // There are proxy_policy_count()+1 = 6 caches and they are mixed together by |
| 127 // one instance of CloudPolicyProvider. The first cache has some policies but | 125 // one instance of CloudPolicyProvider. The first cache has some policies but |
| 128 // no proxy-related ones. The following caches have each one proxy-policy set. | 126 // no proxy-related ones. The following caches have each one proxy-policy set. |
| 129 const int n = 6; | 127 const int n = 6; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 B.Set(kPolicyProxyServer, Value::CreateIntegerValue(b_value)); | 231 B.Set(kPolicyProxyServer, Value::CreateIntegerValue(b_value)); |
| 234 B.Set(kPolicyProxyPacUrl, Value::CreateIntegerValue(b_value)); | 232 B.Set(kPolicyProxyPacUrl, Value::CreateIntegerValue(b_value)); |
| 235 B.Set(kPolicyProxyBypassList, Value::CreateIntegerValue(b_value)); | 233 B.Set(kPolicyProxyBypassList, Value::CreateIntegerValue(b_value)); |
| 236 | 234 |
| 237 CombineTwoPolicyMaps(A, B, &C); | 235 CombineTwoPolicyMaps(A, B, &C); |
| 238 | 236 |
| 239 EXPECT_TRUE(A.Equals(C)); | 237 EXPECT_TRUE(A.Equals(C)); |
| 240 EXPECT_FALSE(B.Equals(C)); | 238 EXPECT_FALSE(B.Equals(C)); |
| 241 } | 239 } |
| 242 | 240 |
| 241 TEST_F(CloudPolicyProviderTest, RefreshPolicies) { |
| 242 CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY); |
| 243 MockCloudPolicyCache cache0; |
| 244 MockCloudPolicyCache cache1; |
| 245 MockCloudPolicyCache cache2; |
| 246 MockConfigurationPolicyObserver observer; |
| 247 ConfigurationPolicyObserverRegistrar registrar; |
| 248 registrar.Init(cloud_policy_provider_.get(), &observer); |
| 249 |
| 250 // OnUpdatePolicy is called when the provider doesn't have any caches. |
| 251 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1); |
| 252 cloud_policy_provider_->RefreshPolicies(); |
| 253 Mock::VerifyAndClearExpectations(&observer); |
| 254 |
| 255 // OnUpdatePolicy is called when all the caches have updated. |
| 256 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(2); |
| 257 cloud_policy_provider_->AppendCache(&cache0); |
| 258 cloud_policy_provider_->AppendCache(&cache1); |
| 259 Mock::VerifyAndClearExpectations(&observer); |
| 260 |
| 261 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0); |
| 262 cloud_policy_provider_->RefreshPolicies(); |
| 263 Mock::VerifyAndClearExpectations(&observer); |
| 264 |
| 265 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0); |
| 266 // Updating just one of the caches is not enough. |
| 267 cloud_policy_provider_->OnCacheUpdate(&cache0); |
| 268 Mock::VerifyAndClearExpectations(&observer); |
| 269 |
| 270 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0); |
| 271 // This cache wasn't available when RefreshPolicies was called, so it isn't |
| 272 // required to fire the update. |
| 273 cloud_policy_provider_->AppendCache(&cache2); |
| 274 Mock::VerifyAndClearExpectations(&observer); |
| 275 |
| 276 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1); |
| 277 cloud_policy_provider_->OnCacheUpdate(&cache1); |
| 278 Mock::VerifyAndClearExpectations(&observer); |
| 279 |
| 280 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0); |
| 281 cloud_policy_provider_->RefreshPolicies(); |
| 282 Mock::VerifyAndClearExpectations(&observer); |
| 283 |
| 284 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0); |
| 285 cloud_policy_provider_->OnCacheUpdate(&cache0); |
| 286 cloud_policy_provider_->OnCacheUpdate(&cache1); |
| 287 Mock::VerifyAndClearExpectations(&observer); |
| 288 |
| 289 // If a cache refreshes more than once, the provider should still wait for |
| 290 // the others before firing the update. |
| 291 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0); |
| 292 cloud_policy_provider_->OnCacheUpdate(&cache0); |
| 293 Mock::VerifyAndClearExpectations(&observer); |
| 294 |
| 295 // Fire updates if one of the required caches goes away while waiting. |
| 296 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1); |
| 297 cloud_policy_provider_->OnCacheGoingAway(&cache2); |
| 298 Mock::VerifyAndClearExpectations(&observer); |
| 299 } |
| 300 |
| 243 } // namespace policy | 301 } // namespace policy |
| OLD | NEW |