| 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/chromeos/login/login_utils.h" | 5 #include "chrome/browser/chromeos/login/login_utils.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 test_api->SetCryptohomeLibrary(cryptohome_, true); | 213 test_api->SetCryptohomeLibrary(cryptohome_, true); |
| 214 | 214 |
| 215 EXPECT_CALL(*mock_dbus_thread_manager_.mock_cryptohome_client(), | 215 EXPECT_CALL(*mock_dbus_thread_manager_.mock_cryptohome_client(), |
| 216 IsMounted(_)); | 216 IsMounted(_)); |
| 217 | 217 |
| 218 browser_process_->SetProfileManager( | 218 browser_process_->SetProfileManager( |
| 219 new ProfileManagerWithoutInit(scoped_temp_dir_.path())); | 219 new ProfileManagerWithoutInit(scoped_temp_dir_.path())); |
| 220 connector_ = browser_process_->browser_policy_connector(); | 220 connector_ = browser_process_->browser_policy_connector(); |
| 221 connector_->Init(); | 221 connector_->Init(); |
| 222 | 222 |
| 223 RunAllPending(); | 223 RunUntilIdle(); |
| 224 } | 224 } |
| 225 | 225 |
| 226 virtual void TearDown() OVERRIDE { | 226 virtual void TearDown() OVERRIDE { |
| 227 cryptohome::AsyncMethodCaller::Shutdown(); | 227 cryptohome::AsyncMethodCaller::Shutdown(); |
| 228 mock_async_method_caller_ = NULL; | 228 mock_async_method_caller_ = NULL; |
| 229 | 229 |
| 230 RunAllPending(); | 230 RunUntilIdle(); |
| 231 { | 231 { |
| 232 // chrome_browser_net::Predictor usually skips its shutdown routines on | 232 // chrome_browser_net::Predictor usually skips its shutdown routines on |
| 233 // unit_tests, but does the full thing when | 233 // unit_tests, but does the full thing when |
| 234 // g_browser_process->profile_manager() is valid during initialization. | 234 // g_browser_process->profile_manager() is valid during initialization. |
| 235 // Run a task on a temporary BrowserThread::IO that allows skipping | 235 // Run a task on a temporary BrowserThread::IO that allows skipping |
| 236 // these routines. | 236 // these routines. |
| 237 // | 237 // |
| 238 // It is important to not have a fake message loop on the IO | 238 // It is important to not have a fake message loop on the IO |
| 239 // thread for the whole test, see comment on LoginUtilsTest | 239 // thread for the whole test, see comment on LoginUtilsTest |
| 240 // constructor for details. | 240 // constructor for details. |
| 241 io_thread_.DeprecatedSetMessageLoop(&loop_); | 241 io_thread_.DeprecatedSetMessageLoop(&loop_); |
| 242 loop_.PostTask(FROM_HERE, | 242 loop_.PostTask(FROM_HERE, |
| 243 base::Bind(&LoginUtilsTest::TearDownOnIO, | 243 base::Bind(&LoginUtilsTest::TearDownOnIO, |
| 244 base::Unretained(this))); | 244 base::Unretained(this))); |
| 245 RunAllPending(); | 245 RunUntilIdle(); |
| 246 io_thread_.DeprecatedSetMessageLoop(NULL); | 246 io_thread_.DeprecatedSetMessageLoop(NULL); |
| 247 } | 247 } |
| 248 | 248 |
| 249 // These trigger some tasks that have to run while BrowserThread::UI | 249 // These trigger some tasks that have to run while BrowserThread::UI |
| 250 // exists. Delete all the profiles before deleting the connector. | 250 // exists. Delete all the profiles before deleting the connector. |
| 251 browser_process_->SetProfileManager(NULL); | 251 browser_process_->SetProfileManager(NULL); |
| 252 connector_ = NULL; | 252 connector_ = NULL; |
| 253 browser_process_->SetBrowserPolicyConnector(NULL); | 253 browser_process_->SetBrowserPolicyConnector(NULL); |
| 254 RunAllPending(); | 254 RunUntilIdle(); |
| 255 } | 255 } |
| 256 | 256 |
| 257 void TearDownOnIO() { | 257 void TearDownOnIO() { |
| 258 std::vector<Profile*> profiles = | 258 std::vector<Profile*> profiles = |
| 259 browser_process_->profile_manager()->GetLoadedProfiles(); | 259 browser_process_->profile_manager()->GetLoadedProfiles(); |
| 260 for (size_t i = 0; i < profiles.size(); ++i) { | 260 for (size_t i = 0; i < profiles.size(); ++i) { |
| 261 chrome_browser_net::Predictor* predictor = | 261 chrome_browser_net::Predictor* predictor = |
| 262 profiles[i]->GetNetworkPredictor(); | 262 profiles[i]->GetNetworkPredictor(); |
| 263 if (predictor) { | 263 if (predictor) { |
| 264 predictor->EnablePredictorOnIOThread(false); | 264 predictor->EnablePredictorOnIOThread(false); |
| 265 predictor->Shutdown(); | 265 predictor->Shutdown(); |
| 266 } | 266 } |
| 267 } | 267 } |
| 268 } | 268 } |
| 269 | 269 |
| 270 void RunAllPending() { | 270 void RunUntilIdle() { |
| 271 loop_.RunAllPending(); | 271 loop_.RunUntilIdle(); |
| 272 BrowserThread::GetBlockingPool()->FlushForTesting(); | 272 BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 273 loop_.RunAllPending(); | 273 loop_.RunUntilIdle(); |
| 274 } | 274 } |
| 275 | 275 |
| 276 virtual void OnProfilePrepared(Profile* profile) OVERRIDE { | 276 virtual void OnProfilePrepared(Profile* profile) OVERRIDE { |
| 277 EXPECT_FALSE(prepared_profile_); | 277 EXPECT_FALSE(prepared_profile_); |
| 278 prepared_profile_ = profile; | 278 prepared_profile_ = profile; |
| 279 } | 279 } |
| 280 | 280 |
| 281 virtual void OnLoginFailure(const LoginFailure& error) OVERRIDE { | 281 virtual void OnLoginFailure(const LoginFailure& error) OVERRIDE { |
| 282 FAIL() << "OnLoginFailure not expected"; | 282 FAIL() << "OnLoginFailure not expected"; |
| 283 } | 283 } |
| 284 | 284 |
| 285 virtual void OnLoginSuccess(const std::string& username, | 285 virtual void OnLoginSuccess(const std::string& username, |
| 286 const std::string& password, | 286 const std::string& password, |
| 287 bool pending_requests, | 287 bool pending_requests, |
| 288 bool using_oauth) OVERRIDE { | 288 bool using_oauth) OVERRIDE { |
| 289 FAIL() << "OnLoginSuccess not expected"; | 289 FAIL() << "OnLoginSuccess not expected"; |
| 290 } | 290 } |
| 291 | 291 |
| 292 void LockDevice(const std::string& username) { | 292 void LockDevice(const std::string& username) { |
| 293 EXPECT_CALL(*cryptohome_, InstallAttributesIsFirstInstall()) | 293 EXPECT_CALL(*cryptohome_, InstallAttributesIsFirstInstall()) |
| 294 .WillOnce(Return(true)) | 294 .WillOnce(Return(true)) |
| 295 .WillRepeatedly(Return(false)); | 295 .WillRepeatedly(Return(false)); |
| 296 policy::CloudPolicyDataStore* device_data_store = | 296 policy::CloudPolicyDataStore* device_data_store = |
| 297 connector_->GetDeviceCloudPolicyDataStore(); | 297 connector_->GetDeviceCloudPolicyDataStore(); |
| 298 device_data_store->set_device_mode(policy::DEVICE_MODE_ENTERPRISE); | 298 device_data_store->set_device_mode(policy::DEVICE_MODE_ENTERPRISE); |
| 299 device_data_store->set_device_id(kDeviceId); | 299 device_data_store->set_device_id(kDeviceId); |
| 300 EXPECT_EQ(policy::EnterpriseInstallAttributes::LOCK_SUCCESS, | 300 EXPECT_EQ(policy::EnterpriseInstallAttributes::LOCK_SUCCESS, |
| 301 connector_->LockDevice(username)); | 301 connector_->LockDevice(username)); |
| 302 RunAllPending(); | 302 RunUntilIdle(); |
| 303 } | 303 } |
| 304 | 304 |
| 305 void PrepareProfile(const std::string& username) { | 305 void PrepareProfile(const std::string& username) { |
| 306 ScopedDeviceSettingsTestHelper device_settings_test_helper; | 306 ScopedDeviceSettingsTestHelper device_settings_test_helper; |
| 307 MockSessionManagerClient* session_manager_client = | 307 MockSessionManagerClient* session_manager_client = |
| 308 mock_dbus_thread_manager_.mock_session_manager_client(); | 308 mock_dbus_thread_manager_.mock_session_manager_client(); |
| 309 EXPECT_CALL(*session_manager_client, StartSession(_)); | 309 EXPECT_CALL(*session_manager_client, StartSession(_)); |
| 310 EXPECT_CALL(*cryptohome_, GetSystemSalt()) | 310 EXPECT_CALL(*cryptohome_, GetSystemSalt()) |
| 311 .WillRepeatedly(Return(std::string("stub_system_salt"))); | 311 .WillRepeatedly(Return(std::string("stub_system_salt"))); |
| 312 EXPECT_CALL(*mock_async_method_caller_, AsyncMount(_, _, _, _)) | 312 EXPECT_CALL(*mock_async_method_caller_, AsyncMount(_, _, _, _)) |
| 313 .WillRepeatedly(Return()); | 313 .WillRepeatedly(Return()); |
| 314 | 314 |
| 315 scoped_refptr<Authenticator> authenticator = | 315 scoped_refptr<Authenticator> authenticator = |
| 316 LoginUtils::Get()->CreateAuthenticator(this); | 316 LoginUtils::Get()->CreateAuthenticator(this); |
| 317 authenticator->CompleteLogin(ProfileManager::GetDefaultProfile(), | 317 authenticator->CompleteLogin(ProfileManager::GetDefaultProfile(), |
| 318 username, | 318 username, |
| 319 "password"); | 319 "password"); |
| 320 | 320 |
| 321 const bool kPendingRequests = false; | 321 const bool kPendingRequests = false; |
| 322 const bool kUsingOAuth = true; | 322 const bool kUsingOAuth = true; |
| 323 const bool kHasCookies = true; | 323 const bool kHasCookies = true; |
| 324 LoginUtils::Get()->PrepareProfile(username, std::string(), "password", | 324 LoginUtils::Get()->PrepareProfile(username, std::string(), "password", |
| 325 kPendingRequests, kUsingOAuth, | 325 kPendingRequests, kUsingOAuth, |
| 326 kHasCookies, this); | 326 kHasCookies, this); |
| 327 device_settings_test_helper.Flush(); | 327 device_settings_test_helper.Flush(); |
| 328 RunAllPending(); | 328 RunUntilIdle(); |
| 329 } | 329 } |
| 330 | 330 |
| 331 net::TestURLFetcher* PrepareOAuthFetcher(const std::string& expected_url) { | 331 net::TestURLFetcher* PrepareOAuthFetcher(const std::string& expected_url) { |
| 332 net::TestURLFetcher* fetcher = test_url_fetcher_factory_.GetFetcherByID(0); | 332 net::TestURLFetcher* fetcher = test_url_fetcher_factory_.GetFetcherByID(0); |
| 333 EXPECT_TRUE(fetcher); | 333 EXPECT_TRUE(fetcher); |
| 334 EXPECT_TRUE(fetcher->delegate()); | 334 EXPECT_TRUE(fetcher->delegate()); |
| 335 EXPECT_TRUE(StartsWithASCII(fetcher->GetOriginalURL().spec(), | 335 EXPECT_TRUE(StartsWithASCII(fetcher->GetOriginalURL().spec(), |
| 336 expected_url, | 336 expected_url, |
| 337 true)); | 337 true)); |
| 338 fetcher->set_url(fetcher->GetOriginalURL()); | 338 fetcher->set_url(fetcher->GetOriginalURL()); |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 | 470 |
| 471 // 2. Get the pending oauth1 access token fetcher. | 471 // 2. Get the pending oauth1 access token fetcher. |
| 472 net::TestURLFetcher* fetcher = | 472 net::TestURLFetcher* fetcher = |
| 473 PrepareOAuthFetcher(GaiaUrls::GetInstance()->get_oauth_token_url()); | 473 PrepareOAuthFetcher(GaiaUrls::GetInstance()->get_oauth_token_url()); |
| 474 ASSERT_TRUE(fetcher); | 474 ASSERT_TRUE(fetcher); |
| 475 | 475 |
| 476 // 3. Issuing a RefreshPolicies() now blocks waiting for the oauth token. | 476 // 3. Issuing a RefreshPolicies() now blocks waiting for the oauth token. |
| 477 bool refresh_policies_completed = false; | 477 bool refresh_policies_completed = false; |
| 478 browser_process_->policy_service()->RefreshPolicies( | 478 browser_process_->policy_service()->RefreshPolicies( |
| 479 base::Bind(SetFlag, &refresh_policies_completed)); | 479 base::Bind(SetFlag, &refresh_policies_completed)); |
| 480 RunAllPending(); | 480 RunUntilIdle(); |
| 481 ASSERT_FALSE(refresh_policies_completed); | 481 ASSERT_FALSE(refresh_policies_completed); |
| 482 | 482 |
| 483 // 4. Now make the fetcher fail. RefreshPolicies() should unblock. | 483 // 4. Now make the fetcher fail. RefreshPolicies() should unblock. |
| 484 // The OAuth1TokenFetcher retries up to 5 times with a 3 second delay; | 484 // The OAuth1TokenFetcher retries up to 5 times with a 3 second delay; |
| 485 // just invoke the callback directly to avoid waiting for that. | 485 // just invoke the callback directly to avoid waiting for that. |
| 486 // The |mock_fetcher| is passed instead of the original because the original | 486 // The |mock_fetcher| is passed instead of the original because the original |
| 487 // is deleted by the GaiaOAuthFetcher after the first callback. | 487 // is deleted by the GaiaOAuthFetcher after the first callback. |
| 488 net::URLFetcherDelegate* delegate = fetcher->delegate(); | 488 net::URLFetcherDelegate* delegate = fetcher->delegate(); |
| 489 ASSERT_TRUE(delegate); | 489 ASSERT_TRUE(delegate); |
| 490 net::TestURLFetcher mock_fetcher(fetcher->id(), | 490 net::TestURLFetcher mock_fetcher(fetcher->id(), |
| 491 fetcher->GetOriginalURL(), | 491 fetcher->GetOriginalURL(), |
| 492 delegate); | 492 delegate); |
| 493 mock_fetcher.set_status(net::URLRequestStatus()); | 493 mock_fetcher.set_status(net::URLRequestStatus()); |
| 494 mock_fetcher.set_response_code(404); | 494 mock_fetcher.set_response_code(404); |
| 495 for (int i = 0; i < 6; ++i) { | 495 for (int i = 0; i < 6; ++i) { |
| 496 ASSERT_FALSE(refresh_policies_completed); | 496 ASSERT_FALSE(refresh_policies_completed); |
| 497 delegate->OnURLFetchComplete(&mock_fetcher); | 497 delegate->OnURLFetchComplete(&mock_fetcher); |
| 498 RunAllPending(); | 498 RunUntilIdle(); |
| 499 } | 499 } |
| 500 EXPECT_TRUE(refresh_policies_completed); | 500 EXPECT_TRUE(refresh_policies_completed); |
| 501 } | 501 } |
| 502 | 502 |
| 503 TEST_P(LoginUtilsBlockingLoginTest, EnterpriseLoginBlocksForEnterpriseUser) { | 503 TEST_P(LoginUtilsBlockingLoginTest, EnterpriseLoginBlocksForEnterpriseUser) { |
| 504 UserManager* user_manager = UserManager::Get(); | 504 UserManager* user_manager = UserManager::Get(); |
| 505 ASSERT_TRUE(!user_manager->IsUserLoggedIn()); | 505 ASSERT_TRUE(!user_manager->IsUserLoggedIn()); |
| 506 EXPECT_FALSE(connector_->IsEnterpriseManaged()); | 506 EXPECT_FALSE(connector_->IsEnterpriseManaged()); |
| 507 EXPECT_FALSE(prepared_profile_); | 507 EXPECT_FALSE(prepared_profile_); |
| 508 | 508 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 if (steps < 3) break; | 548 if (steps < 3) break; |
| 549 | 549 |
| 550 // Fake OAuth service token retrieval: | 550 // Fake OAuth service token retrieval: |
| 551 fetcher = PrepareOAuthFetcher(gaia_urls->oauth_wrap_bridge_url()); | 551 fetcher = PrepareOAuthFetcher(gaia_urls->oauth_wrap_bridge_url()); |
| 552 ASSERT_TRUE(fetcher); | 552 ASSERT_TRUE(fetcher); |
| 553 fetcher->SetResponseString(kOAuthServiceTokenData); | 553 fetcher->SetResponseString(kOAuthServiceTokenData); |
| 554 fetcher->delegate()->OnURLFetchComplete(fetcher); | 554 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 555 | 555 |
| 556 // The cloud policy subsystem is now ready to fetch the dmtoken and the user | 556 // The cloud policy subsystem is now ready to fetch the dmtoken and the user |
| 557 // policy. | 557 // policy. |
| 558 RunAllPending(); | 558 RunUntilIdle(); |
| 559 if (steps < 4) break; | 559 if (steps < 4) break; |
| 560 | 560 |
| 561 fetcher = PrepareDMRegisterFetcher(); | 561 fetcher = PrepareDMRegisterFetcher(); |
| 562 ASSERT_TRUE(fetcher); | 562 ASSERT_TRUE(fetcher); |
| 563 fetcher->delegate()->OnURLFetchComplete(fetcher); | 563 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 564 // The policy fetch job has now been scheduled, run it: | 564 // The policy fetch job has now been scheduled, run it: |
| 565 RunAllPending(); | 565 RunUntilIdle(); |
| 566 if (steps < 5) break; | 566 if (steps < 5) break; |
| 567 | 567 |
| 568 // Verify that there is no profile prepared just before the policy fetch. | 568 // Verify that there is no profile prepared just before the policy fetch. |
| 569 EXPECT_FALSE(prepared_profile_); | 569 EXPECT_FALSE(prepared_profile_); |
| 570 | 570 |
| 571 fetcher = PrepareDMPolicyFetcher(); | 571 fetcher = PrepareDMPolicyFetcher(); |
| 572 ASSERT_TRUE(fetcher); | 572 ASSERT_TRUE(fetcher); |
| 573 fetcher->delegate()->OnURLFetchComplete(fetcher); | 573 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 574 } while (0); | 574 } while (0); |
| 575 | 575 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 591 } | 591 } |
| 592 | 592 |
| 593 INSTANTIATE_TEST_CASE_P( | 593 INSTANTIATE_TEST_CASE_P( |
| 594 LoginUtilsBlockingLoginTestInstance, | 594 LoginUtilsBlockingLoginTestInstance, |
| 595 LoginUtilsBlockingLoginTest, | 595 LoginUtilsBlockingLoginTest, |
| 596 testing::Values(0, 1, 2, 3, 4, 5)); | 596 testing::Values(0, 1, 2, 3, 4, 5)); |
| 597 | 597 |
| 598 } // namespace | 598 } // namespace |
| 599 | 599 |
| 600 } | 600 } |
| OLD | NEW |