| 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_controller.h" | 5 #include "chrome/browser/policy/cloud_policy_controller.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "chrome/browser/policy/cloud_policy_data_store.h" | 10 #include "chrome/browser/policy/cloud_policy_data_store.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 public: | 44 public: |
| 45 CloudPolicyControllerTest() | 45 CloudPolicyControllerTest() |
| 46 : ui_thread_(BrowserThread::UI, &loop_), | 46 : ui_thread_(BrowserThread::UI, &loop_), |
| 47 file_thread_(BrowserThread::FILE, &loop_) {} | 47 file_thread_(BrowserThread::FILE, &loop_) {} |
| 48 | 48 |
| 49 virtual ~CloudPolicyControllerTest() {} | 49 virtual ~CloudPolicyControllerTest() {} |
| 50 | 50 |
| 51 virtual void SetUp() { | 51 virtual void SetUp() { |
| 52 ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); | 52 ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); |
| 53 cache_.reset(new UserPolicyCache( | 53 cache_.reset(new UserPolicyCache( |
| 54 temp_user_data_dir_.path().AppendASCII("CloudPolicyControllerTest"))); | 54 temp_user_data_dir_.path().AppendASCII("CloudPolicyControllerTest"), |
| 55 false /* wait_for_policy_fetch */)); |
| 55 token_fetcher_.reset(new MockDeviceTokenFetcher(cache_.get())); | 56 token_fetcher_.reset(new MockDeviceTokenFetcher(cache_.get())); |
| 56 EXPECT_CALL(service_, CreateBackend()) | 57 EXPECT_CALL(service_, CreateBackend()) |
| 57 .Times(AnyNumber()) | 58 .Times(AnyNumber()) |
| 58 .WillRepeatedly(MockDeviceManagementServiceProxyBackend(&backend_)); | 59 .WillRepeatedly(MockDeviceManagementServiceProxyBackend(&backend_)); |
| 59 data_store_.reset(CloudPolicyDataStore::CreateForUserPolicies()); | 60 data_store_.reset(CloudPolicyDataStore::CreateForUserPolicies()); |
| 60 } | 61 } |
| 61 | 62 |
| 62 virtual void TearDown() { | 63 virtual void TearDown() { |
| 63 controller_.reset(); // Unregisters observers. | 64 controller_.reset(); // Unregisters observers. |
| 64 data_store_.reset(); | 65 data_store_.reset(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 void CreateNewController() { | 68 void CreateNewController() { |
| 68 controller_.reset(new CloudPolicyController( | 69 controller_.reset(new CloudPolicyController( |
| 69 &service_, cache_.get(), token_fetcher_.get(), data_store_.get(), | 70 &service_, cache_.get(), token_fetcher_.get(), data_store_.get(), |
| 70 ¬ifier_, new DummyWorkScheduler)); | 71 ¬ifier_, new DummyWorkScheduler)); |
| 71 } | 72 } |
| 72 | 73 |
| 74 void CreateNewWaitingCache() { |
| 75 cache_.reset(new UserPolicyCache( |
| 76 temp_user_data_dir_.path().AppendASCII("CloudPolicyControllerTest"), |
| 77 true /* wait_for_policy_fetch */)); |
| 78 // Make this cache's disk cache ready, but have it still waiting for a |
| 79 // policy fetch. |
| 80 cache_->Load(); |
| 81 loop_.RunAllPending(); |
| 82 ASSERT_TRUE(cache_->last_policy_refresh_time().is_null()); |
| 83 ASSERT_FALSE(cache_->IsReady()); |
| 84 } |
| 85 |
| 73 void ExpectHasSpdyPolicy() { | 86 void ExpectHasSpdyPolicy() { |
| 74 base::FundamentalValue expected(true); | 87 base::FundamentalValue expected(true); |
| 75 const PolicyMap* policy_map = cache_->policy( | 88 const PolicyMap* policy_map = cache_->policy( |
| 76 CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY); | 89 CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY); |
| 77 ASSERT_TRUE(Value::Equals(&expected, policy_map->Get(kPolicyDisableSpdy))); | 90 ASSERT_TRUE(Value::Equals(&expected, policy_map->Get(kPolicyDisableSpdy))); |
| 78 } | 91 } |
| 79 | 92 |
| 80 void StopMessageLoop() { | 93 void StopMessageLoop() { |
| 81 loop_.QuitNow(); | 94 loop_.QuitNow(); |
| 82 } | 95 } |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 data_store_->SetupForTesting("device_token", "device_id", | 245 data_store_->SetupForTesting("device_token", "device_id", |
| 233 "who@what.com", "auth", true); | 246 "who@what.com", "auth", true); |
| 234 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 247 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( |
| 235 MockDeviceManagementBackendFailPolicy( | 248 MockDeviceManagementBackendFailPolicy( |
| 236 DeviceManagementBackend::kErrorServiceInvalidSerialNumber)); | 249 DeviceManagementBackend::kErrorServiceInvalidSerialNumber)); |
| 237 EXPECT_CALL(*token_fetcher_.get(), SetSerialNumberInvalidState()).Times(1); | 250 EXPECT_CALL(*token_fetcher_.get(), SetSerialNumberInvalidState()).Times(1); |
| 238 CreateNewController(); | 251 CreateNewController(); |
| 239 loop_.RunAllPending(); | 252 loop_.RunAllPending(); |
| 240 } | 253 } |
| 241 | 254 |
| 255 TEST_F(CloudPolicyControllerTest, DontSetFetchingDone0) { |
| 256 CreateNewWaitingCache(); |
| 257 CreateNewController(); |
| 258 // Initialized without an oauth token, goes into TOKEN_UNAVAILABLE state. |
| 259 // This means the controller is still waiting for an oauth token fetch. |
| 260 EXPECT_FALSE(cache_->IsReady()); |
| 261 } |
| 262 |
| 263 TEST_F(CloudPolicyControllerTest, DontSetFetchingDone1) { |
| 264 CreateNewWaitingCache(); |
| 265 data_store_->SetupForTesting("device_token", "device_id", |
| 266 "who@what.com", "auth", true); |
| 267 CreateNewController(); |
| 268 // Initialized with an oauth token, goes into TOKEN_VALID state. |
| 269 // This means the controller has an oauth token and should fetch the next |
| 270 // token, which is the dm server register token. |
| 271 EXPECT_FALSE(cache_->IsReady()); |
| 272 } |
| 273 |
| 274 TEST_F(CloudPolicyControllerTest, SetFetchingDone0) { |
| 275 CreateNewWaitingCache(); |
| 276 data_store_->SetupForTesting("", "device_id", |
| 277 "user@gmail.com", "auth", true); |
| 278 CreateNewController(); |
| 279 loop_.RunAllPending(); |
| 280 // User is in an unmanaged domain. |
| 281 EXPECT_TRUE(cache_->IsReady()); |
| 282 EXPECT_TRUE(cache_->last_policy_refresh_time().is_null()); |
| 283 } |
| 284 |
| 285 TEST_F(CloudPolicyControllerTest, SetFetchingDone1) { |
| 286 CreateNewWaitingCache(); |
| 287 data_store_->SetupForTesting("device_token", "device_id", |
| 288 "user@enterprise.com", "auth", true); |
| 289 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce(DoAll( |
| 290 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), |
| 291 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); |
| 292 CreateNewController(); |
| 293 loop_.RunAllPending(); |
| 294 EXPECT_TRUE(cache_->IsReady()); |
| 295 EXPECT_FALSE(cache_->last_policy_refresh_time().is_null()); |
| 296 } |
| 297 |
| 298 TEST_F(CloudPolicyControllerTest, SetFetchingDone2) { |
| 299 CreateNewWaitingCache(); |
| 300 data_store_->SetupForTesting("device_token", "device_id", |
| 301 "user@enterprise.com", "auth", true); |
| 302 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce(DoAll( |
| 303 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), |
| 304 MockDeviceManagementBackendFailPolicy( |
| 305 DeviceManagementBackend::kErrorRequestFailed))); |
| 306 CreateNewController(); |
| 307 loop_.RunAllPending(); |
| 308 EXPECT_TRUE(cache_->IsReady()); |
| 309 EXPECT_TRUE(cache_->last_policy_refresh_time().is_null()); |
| 310 } |
| 311 |
| 242 } // namespace policy | 312 } // namespace policy |
| OLD | NEW |