| 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/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/browser_policy_connector.h" |
| 10 #include "chrome/browser/policy/cloud_policy_cache_base.h" | 10 #include "chrome/browser/policy/cloud_policy_cache_base.h" |
| 11 #include "chrome/browser/policy/cloud_policy_provider.h" | 11 #include "chrome/browser/policy/cloud_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_bundle.h" |
| 14 #include "chrome/browser/policy/policy_map.h" |
| 13 #include "content/test/test_browser_thread.h" | 15 #include "content/test/test_browser_thread.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 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 19 |
| 18 using testing::Mock; | 20 using testing::Mock; |
| 19 | 21 |
| 20 namespace em = enterprise_management; | 22 namespace em = enterprise_management; |
| 21 | 23 |
| 22 namespace policy { | 24 namespace policy { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 MockCloudPolicyCache device_policy_cache_; | 103 MockCloudPolicyCache device_policy_cache_; |
| 102 #endif | 104 #endif |
| 103 | 105 |
| 104 CloudPolicyProvider cloud_policy_provider_; | 106 CloudPolicyProvider cloud_policy_provider_; |
| 105 MockConfigurationPolicyObserver observer_; | 107 MockConfigurationPolicyObserver observer_; |
| 106 ConfigurationPolicyObserverRegistrar registrar_; | 108 ConfigurationPolicyObserverRegistrar registrar_; |
| 107 }; | 109 }; |
| 108 | 110 |
| 109 TEST_F(CloudPolicyProviderTest, Initialization) { | 111 TEST_F(CloudPolicyProviderTest, Initialization) { |
| 110 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); | 112 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); |
| 111 | |
| 112 // The provider only becomes initialized when it has all caches, and the | 113 // The provider only becomes initialized when it has all caches, and the |
| 113 // caches are ready too. | 114 // caches are ready too. |
| 114 AddDeviceCache(); | 115 AddDeviceCache(); |
| 115 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); | 116 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); |
| 116 SetDeviceCacheReady(); | 117 SetDeviceCacheReady(); |
| 117 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); | 118 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); |
| 118 AddUserCache(); | 119 AddUserCache(); |
| 119 EXPECT_FALSE(user_policy_cache_.IsReady()); | 120 EXPECT_FALSE(user_policy_cache_.IsReady()); |
| 120 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); | 121 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); |
| 121 | 122 PolicyBundle expected_bundle; |
| 122 PolicyMap policy; | 123 EXPECT_TRUE(cloud_policy_provider_.policies().Equals(expected_bundle)); |
| 123 EXPECT_TRUE(cloud_policy_provider_.Provide(&policy)); | |
| 124 EXPECT_TRUE(policy.empty()); | |
| 125 | |
| 126 SetUserCacheReady(); | 124 SetUserCacheReady(); |
| 127 EXPECT_TRUE(cloud_policy_provider_.IsInitializationComplete()); | 125 EXPECT_TRUE(cloud_policy_provider_.IsInitializationComplete()); |
| 128 EXPECT_TRUE(cloud_policy_provider_.Provide(&policy)); | 126 EXPECT_TRUE(cloud_policy_provider_.policies().Equals(expected_bundle)); |
| 129 EXPECT_TRUE(policy.empty()); | |
| 130 | 127 |
| 131 const std::string kUrl("http://chromium.org"); | 128 const std::string kUrl("http://chromium.org"); |
| 132 user_policy_cache_.mutable_policy()->Set(key::kHomepageLocation, | 129 user_policy_cache_.mutable_policy()->Set(key::kHomepageLocation, |
| 133 POLICY_LEVEL_MANDATORY, | 130 POLICY_LEVEL_MANDATORY, |
| 134 POLICY_SCOPE_USER, | 131 POLICY_SCOPE_USER, |
| 135 Value::CreateStringValue(kUrl)); | 132 Value::CreateStringValue(kUrl)); |
| 136 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); | 133 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); |
| 137 user_policy_cache_.NotifyObservers(); | 134 user_policy_cache_.NotifyObservers(); |
| 138 Mock::VerifyAndClearExpectations(&observer_); | 135 Mock::VerifyAndClearExpectations(&observer_); |
| 139 EXPECT_TRUE(cloud_policy_provider_.Provide(&policy)); | 136 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") |
| 140 | 137 .Set(key::kHomepageLocation, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 141 PolicyMap expected; | 138 base::Value::CreateStringValue(kUrl)); |
| 142 expected.Set(key::kHomepageLocation, | 139 EXPECT_TRUE(cloud_policy_provider_.policies().Equals(expected_bundle)); |
| 143 POLICY_LEVEL_MANDATORY, | |
| 144 POLICY_SCOPE_USER, | |
| 145 Value::CreateStringValue(kUrl)); | |
| 146 EXPECT_TRUE(policy.Equals(expected)); | |
| 147 } | 140 } |
| 148 | 141 |
| 149 TEST_F(CloudPolicyProviderTest, RefreshPolicies) { | 142 TEST_F(CloudPolicyProviderTest, RefreshPolicies) { |
| 150 // OnUpdatePolicy is called when the provider doesn't have any caches. | 143 // OnUpdatePolicy is called when the provider doesn't have any caches. |
| 151 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)).Times(1); | 144 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)).Times(1); |
| 152 cloud_policy_provider_.RefreshPolicies(); | 145 cloud_policy_provider_.RefreshPolicies(); |
| 153 Mock::VerifyAndClearExpectations(&observer_); | 146 Mock::VerifyAndClearExpectations(&observer_); |
| 154 | 147 |
| 155 // OnUpdatePolicy is called when all the caches have updated. | 148 // OnUpdatePolicy is called when all the caches have updated. |
| 156 AddUserCache(); | 149 AddUserCache(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 187 #endif | 180 #endif |
| 188 } | 181 } |
| 189 | 182 |
| 190 #if defined(OS_CHROMEOS) | 183 #if defined(OS_CHROMEOS) |
| 191 TEST_F(CloudPolicyProviderTest, MergeProxyPolicies) { | 184 TEST_F(CloudPolicyProviderTest, MergeProxyPolicies) { |
| 192 AddDeviceCache(); | 185 AddDeviceCache(); |
| 193 AddUserCache(); | 186 AddUserCache(); |
| 194 SetDeviceCacheReady(); | 187 SetDeviceCacheReady(); |
| 195 SetUserCacheReady(); | 188 SetUserCacheReady(); |
| 196 EXPECT_TRUE(cloud_policy_provider_.IsInitializationComplete()); | 189 EXPECT_TRUE(cloud_policy_provider_.IsInitializationComplete()); |
| 197 PolicyMap policy; | 190 PolicyBundle expected_bundle; |
| 198 EXPECT_TRUE(cloud_policy_provider_.Provide(&policy)); | 191 EXPECT_TRUE(cloud_policy_provider_.policies().Equals(expected_bundle)); |
| 199 EXPECT_TRUE(policy.empty()); | |
| 200 | 192 |
| 201 // User policy takes precedence over device policy. | 193 // User policy takes precedence over device policy. |
| 202 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); | 194 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); |
| 203 device_policy_cache_.mutable_policy()->Set( | 195 device_policy_cache_.mutable_policy()->Set( |
| 204 key::kProxyMode, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_MACHINE, | 196 key::kProxyMode, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_MACHINE, |
| 205 Value::CreateStringValue("device mode")); | 197 Value::CreateStringValue("device mode")); |
| 206 device_policy_cache_.NotifyObservers(); | 198 device_policy_cache_.NotifyObservers(); |
| 207 Mock::VerifyAndClearExpectations(&observer_); | 199 Mock::VerifyAndClearExpectations(&observer_); |
| 208 | 200 |
| 209 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); | 201 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); |
| 210 user_policy_cache_.mutable_policy()->Set( | 202 user_policy_cache_.mutable_policy()->Set( |
| 211 key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 203 key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 212 Value::CreateStringValue("user mode")); | 204 Value::CreateStringValue("user mode")); |
| 213 user_policy_cache_.NotifyObservers(); | 205 user_policy_cache_.NotifyObservers(); |
| 214 Mock::VerifyAndClearExpectations(&observer_); | 206 Mock::VerifyAndClearExpectations(&observer_); |
| 215 | 207 |
| 216 EXPECT_TRUE(cloud_policy_provider_.Provide(&policy)); | |
| 217 | |
| 218 // The deprecated proxy policies are converted to the new ProxySettings. | 208 // The deprecated proxy policies are converted to the new ProxySettings. |
| 219 EXPECT_TRUE(policy.Get(key::kProxyMode) == NULL); | 209 base::DictionaryValue* proxy_settings = new base::DictionaryValue(); |
| 220 EXPECT_TRUE(policy.Get(key::kProxyServerMode) == NULL); | 210 proxy_settings->SetString(key::kProxyMode, "user mode"); |
| 221 EXPECT_TRUE(policy.Get(key::kProxyServer) == NULL); | 211 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") |
| 222 EXPECT_TRUE(policy.Get(key::kProxyPacUrl) == NULL); | 212 .Set(key::kProxySettings, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 223 | 213 proxy_settings); |
| 224 EXPECT_EQ(1u, policy.size()); | 214 EXPECT_TRUE(cloud_policy_provider_.policies().Equals(expected_bundle)); |
| 225 const PolicyMap::Entry* entry = policy.Get(key::kProxySettings); | |
| 226 ASSERT_TRUE(entry != NULL); | |
| 227 ASSERT_TRUE(entry->value != NULL); | |
| 228 EXPECT_EQ(POLICY_LEVEL_MANDATORY, entry->level); | |
| 229 EXPECT_EQ(POLICY_SCOPE_USER, entry->scope); | |
| 230 const DictionaryValue* settings; | |
| 231 ASSERT_TRUE(entry->value->GetAsDictionary(&settings)); | |
| 232 std::string mode; | |
| 233 EXPECT_TRUE(settings->GetString(key::kProxyMode, &mode)); | |
| 234 EXPECT_EQ("user mode", mode); | |
| 235 } | 215 } |
| 236 #endif | 216 #endif |
| 237 | 217 |
| 238 } // namespace policy | 218 } // namespace policy |
| OLD | NEW |