| 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "base/scoped_temp_dir.h" | 7 #include "base/scoped_temp_dir.h" |
| 8 #include "chrome/browser/browser_thread.h" | 8 #include "chrome/browser/browser_thread.h" |
| 9 #include "chrome/browser/net/gaia/token_service.h" | 9 #include "chrome/browser/net/gaia/token_service.h" |
| 10 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 10 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | 58 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), |
| 59 backend_, | 59 backend_, |
| 60 profile_.get())); | 60 profile_.get())); |
| 61 provider_->SetDeviceTokenFetcher( | 61 provider_->SetDeviceTokenFetcher( |
| 62 new TestingDeviceTokenFetcher(backend_, | 62 new TestingDeviceTokenFetcher(backend_, |
| 63 profile_.get(), | 63 profile_.get(), |
| 64 provider_->GetTokenPath())); | 64 provider_->GetTokenPath())); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void CreateNewProvider(int64 policy_refresh_rate_ms, | 67 void CreateNewProvider(int64 policy_refresh_rate_ms, |
| 68 int64 policy_refresh_max_earlier_ms, | 68 int policy_refresh_fuzz_factor_percent, |
| 69 int64 policy_refresh_fuzz_max, |
| 69 int64 policy_refresh_error_delay_ms, | 70 int64 policy_refresh_error_delay_ms, |
| 70 int64 token_fetch_error_delay_ms, | 71 int64 token_fetch_error_delay_ms, |
| 71 int64 unmanaged_device_refresh_rate_ms) { | 72 int64 unmanaged_device_refresh_rate_ms) { |
| 72 backend_ = new MockDeviceManagementBackend; | 73 backend_ = new MockDeviceManagementBackend; |
| 73 provider_.reset(new DeviceManagementPolicyProvider( | 74 provider_.reset(new DeviceManagementPolicyProvider( |
| 74 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | 75 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), |
| 75 backend_, | 76 backend_, |
| 76 profile_.get(), | 77 profile_.get(), |
| 77 policy_refresh_rate_ms, | 78 policy_refresh_rate_ms, |
| 78 policy_refresh_max_earlier_ms, | 79 policy_refresh_fuzz_factor_percent, |
| 80 policy_refresh_fuzz_max, |
| 79 policy_refresh_error_delay_ms, | 81 policy_refresh_error_delay_ms, |
| 80 token_fetch_error_delay_ms, | 82 token_fetch_error_delay_ms, |
| 81 unmanaged_device_refresh_rate_ms)); | 83 unmanaged_device_refresh_rate_ms)); |
| 82 provider_->SetDeviceTokenFetcher( | 84 provider_->SetDeviceTokenFetcher( |
| 83 new TestingDeviceTokenFetcher(backend_, | 85 new TestingDeviceTokenFetcher(backend_, |
| 84 profile_.get(), | 86 profile_.get(), |
| 85 provider_->GetTokenPath())); | 87 provider_->GetTokenPath())); |
| 86 } | 88 } |
| 87 | 89 |
| 88 FilePath GetTokenPath() const { | 90 FilePath GetTokenPath() const { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 110 SimulateSuccessfulLoginAndRunPending(); | 112 SimulateSuccessfulLoginAndRunPending(); |
| 111 EXPECT_FALSE(waiting_for_initial_policies()); | 113 EXPECT_FALSE(waiting_for_initial_policies()); |
| 112 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1); | 114 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1); |
| 113 provider_->Provide(&store); | 115 provider_->Provide(&store); |
| 114 ASSERT_EQ(1U, store.policy_map().size()); | 116 ASSERT_EQ(1U, store.policy_map().size()); |
| 115 Mock::VerifyAndClearExpectations(backend_); | 117 Mock::VerifyAndClearExpectations(backend_); |
| 116 Mock::VerifyAndClearExpectations(&store); | 118 Mock::VerifyAndClearExpectations(&store); |
| 117 } | 119 } |
| 118 | 120 |
| 119 virtual void TearDown() { | 121 virtual void TearDown() { |
| 122 provider_.reset(); |
| 120 loop_.RunAllPending(); | 123 loop_.RunAllPending(); |
| 121 } | 124 } |
| 122 | 125 |
| 123 bool waiting_for_initial_policies() const { | 126 bool waiting_for_initial_policies() const { |
| 124 return provider_->waiting_for_initial_policies_; | 127 return !provider_->IsInitializationComplete(); |
| 125 } | 128 } |
| 126 | 129 |
| 127 MockDeviceManagementBackend* backend_; // weak | 130 MockDeviceManagementBackend* backend_; // weak |
| 128 scoped_ptr<DeviceManagementPolicyProvider> provider_; | 131 scoped_ptr<DeviceManagementPolicyProvider> provider_; |
| 129 | 132 |
| 130 protected: | 133 protected: |
| 131 DeviceManagementPolicyCache* cache(DeviceManagementPolicyProvider* provider) { | 134 DeviceManagementPolicyCache* cache(DeviceManagementPolicyProvider* provider) { |
| 132 return provider->cache_.get(); | 135 return provider->cache_.get(); |
| 133 } | 136 } |
| 134 | 137 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 TEST_F(DeviceManagementPolicyProviderTest, FetchTriggersRefresh) { | 211 TEST_F(DeviceManagementPolicyProviderTest, FetchTriggersRefresh) { |
| 209 MockConfigurationPolicyObserver observer; | 212 MockConfigurationPolicyObserver observer; |
| 210 ConfigurationPolicyObserverRegistrar registrar; | 213 ConfigurationPolicyObserverRegistrar registrar; |
| 211 registrar.Init(provider_.get(), &observer); | 214 registrar.Init(provider_.get(), &observer); |
| 212 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); | 215 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); |
| 213 SimulateSuccessfulInitialPolicyFetch(); | 216 SimulateSuccessfulInitialPolicyFetch(); |
| 214 } | 217 } |
| 215 | 218 |
| 216 TEST_F(DeviceManagementPolicyProviderTest, ErrorCausesNewRequest) { | 219 TEST_F(DeviceManagementPolicyProviderTest, ErrorCausesNewRequest) { |
| 217 InSequence s; | 220 InSequence s; |
| 218 CreateNewProvider(1000 * 1000, 0, 0, 0, 0); | 221 CreateNewProvider(1000 * 1000, 0, 0, 0, 0, 0); |
| 219 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 222 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 220 MockDeviceManagementBackendFailRegister( | 223 MockDeviceManagementBackendFailRegister( |
| 221 DeviceManagementBackend::kErrorRequestFailed)); | 224 DeviceManagementBackend::kErrorRequestFailed)); |
| 222 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 225 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 223 MockDeviceManagementBackendSucceedRegister()); | 226 MockDeviceManagementBackendSucceedRegister()); |
| 224 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 227 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 225 MockDeviceManagementBackendFailPolicy( | 228 MockDeviceManagementBackendFailPolicy( |
| 226 DeviceManagementBackend::kErrorRequestFailed)); | 229 DeviceManagementBackend::kErrorRequestFailed)); |
| 227 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 230 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 228 MockDeviceManagementBackendFailPolicy( | 231 MockDeviceManagementBackendFailPolicy( |
| 229 DeviceManagementBackend::kErrorRequestFailed)); | 232 DeviceManagementBackend::kErrorRequestFailed)); |
| 230 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 233 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 231 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); | 234 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
| 232 SimulateSuccessfulLoginAndRunPending(); | 235 SimulateSuccessfulLoginAndRunPending(); |
| 233 } | 236 } |
| 234 | 237 |
| 235 TEST_F(DeviceManagementPolicyProviderTest, RefreshPolicies) { | 238 TEST_F(DeviceManagementPolicyProviderTest, RefreshPolicies) { |
| 236 InSequence s; | 239 InSequence s; |
| 237 CreateNewProvider(0, 0, 1000 * 1000, 1000, 0); | 240 CreateNewProvider(0, 0, 0, 1000 * 1000, 1000, 0); |
| 238 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 241 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 239 MockDeviceManagementBackendSucceedRegister()); | 242 MockDeviceManagementBackendSucceedRegister()); |
| 240 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 243 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 241 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); | 244 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
| 242 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 245 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 243 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); | 246 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
| 244 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 247 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 245 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); | 248 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
| 246 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 249 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 247 MockDeviceManagementBackendFailPolicy( | 250 MockDeviceManagementBackendFailPolicy( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 278 MockDeviceManagementBackendSucceedRegister()); | 281 MockDeviceManagementBackendSucceedRegister()); |
| 279 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 282 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 280 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); | 283 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
| 281 SimulateSuccessfulLoginAndRunPending(); | 284 SimulateSuccessfulLoginAndRunPending(); |
| 282 } | 285 } |
| 283 | 286 |
| 284 // If the client is successfully managed, but the admin stops managing the | 287 // If the client is successfully managed, but the admin stops managing the |
| 285 // device, the client should notice and throw away the device token and id. | 288 // device, the client should notice and throw away the device token and id. |
| 286 TEST_F(DeviceManagementPolicyProviderTest, DeviceNoLongerManaged) { | 289 TEST_F(DeviceManagementPolicyProviderTest, DeviceNoLongerManaged) { |
| 287 InSequence s; | 290 InSequence s; |
| 288 CreateNewProvider(0, 0, 0, 0, 1000 * 1000); | 291 CreateNewProvider(0, 0, 0, 0, 0, 1000 * 1000); |
| 289 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 292 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 290 MockDeviceManagementBackendSucceedRegister()); | 293 MockDeviceManagementBackendSucceedRegister()); |
| 291 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 294 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 292 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); | 295 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
| 293 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 296 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 294 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); | 297 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
| 295 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 298 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 296 MockDeviceManagementBackendFailPolicy( | 299 MockDeviceManagementBackendFailPolicy( |
| 297 DeviceManagementBackend::kErrorServiceManagementNotSupported)); | 300 DeviceManagementBackend::kErrorServiceManagementNotSupported)); |
| 298 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 301 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 299 MockDeviceManagementBackendFailRegister( | 302 MockDeviceManagementBackendFailRegister( |
| 300 DeviceManagementBackend::kErrorServiceManagementNotSupported)); | 303 DeviceManagementBackend::kErrorServiceManagementNotSupported)); |
| 301 SimulateSuccessfulLoginAndRunPending(); | 304 SimulateSuccessfulLoginAndRunPending(); |
| 302 FilePath token_path(GetTokenPath()); | 305 FilePath token_path(GetTokenPath()); |
| 303 EXPECT_FALSE(file_util::PathExists(token_path)); | 306 EXPECT_FALSE(file_util::PathExists(token_path)); |
| 304 } | 307 } |
| 305 | 308 |
| 306 // This test tests three things (see numbered comments below): | 309 // This test tests three things (see numbered comments below): |
| 307 TEST_F(DeviceManagementPolicyProviderTest, UnmanagedDevice) { | 310 TEST_F(DeviceManagementPolicyProviderTest, UnmanagedDevice) { |
| 308 InSequence s; | 311 InSequence s; |
| 309 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 312 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 310 MockDeviceManagementBackendFailRegister( | 313 MockDeviceManagementBackendFailRegister( |
| 311 DeviceManagementBackend::kErrorServiceManagementNotSupported)); | 314 DeviceManagementBackend::kErrorServiceManagementNotSupported)); |
| 312 SimulateSuccessfulLoginAndRunPending(); | 315 SimulateSuccessfulLoginAndRunPending(); |
| 313 // (1) The provider's DMPolicyCache should know that the device is not | 316 // (1) The provider's DMPolicyCache should know that the device is not |
| 314 // managed. | 317 // managed. |
| 315 EXPECT_TRUE(cache(provider_.get())->is_device_unmanaged()); | 318 EXPECT_TRUE(cache(provider_.get())->is_device_unmanaged()); |
| 316 // (2) On restart, the provider should detect that this is not the first | 319 // (2) On restart, the provider should detect that this is not the first |
| 317 // login. | 320 // login. |
| 318 CreateNewProvider(1000*1000, 0, 0, 0, 0); | 321 CreateNewProvider(1000*1000, 0, 0, 0, 0, 0); |
| 319 EXPECT_FALSE(waiting_for_initial_policies()); | 322 EXPECT_FALSE(waiting_for_initial_policies()); |
| 320 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 323 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 321 MockDeviceManagementBackendSucceedRegister()); | 324 MockDeviceManagementBackendSucceedRegister()); |
| 322 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | 325 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 323 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); | 326 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
| 324 SimulateSuccessfulLoginAndRunPending(); | 327 SimulateSuccessfulLoginAndRunPending(); |
| 325 // (3) Since the backend call this time returned a device id, the "unmanaged" | 328 // (3) Since the backend call this time returned a device id, the "unmanaged" |
| 326 // marker should have been deleted. | 329 // marker should have been deleted. |
| 327 EXPECT_FALSE(cache(provider_.get())->is_device_unmanaged()); | 330 EXPECT_FALSE(cache(provider_.get())->is_device_unmanaged()); |
| 328 } | 331 } |
| 329 | 332 |
| 330 } // namespace policy | 333 } // namespace policy |
| OLD | NEW |