Chromium Code Reviews| 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/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" | |
| 13 #include "chrome/browser/chromeos/cros/cros_library.h" | 14 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 14 #include "chrome/browser/chromeos/cros/mock_cryptohome_library.h" | 15 #include "chrome/browser/chromeos/cros/mock_cryptohome_library.h" |
| 15 #include "chrome/browser/chromeos/cros/mock_library_loader.h" | 16 #include "chrome/browser/chromeos/cros/mock_library_loader.h" |
| 16 #include "chrome/browser/chromeos/cryptohome/mock_async_method_caller.h" | 17 #include "chrome/browser/chromeos/cryptohome/mock_async_method_caller.h" |
| 17 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" | 18 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" |
| 18 #include "chrome/browser/chromeos/login/authenticator.h" | 19 #include "chrome/browser/chromeos/login/authenticator.h" |
| 19 #include "chrome/browser/chromeos/login/login_status_consumer.h" | 20 #include "chrome/browser/chromeos/login/login_status_consumer.h" |
| 20 #include "chrome/browser/chromeos/login/user_manager.h" | 21 #include "chrome/browser/chromeos/login/user_manager.h" |
| 21 #include "chrome/browser/io_thread.h" | 22 #include "chrome/browser/io_thread.h" |
| 22 #include "chrome/browser/net/predictor.h" | 23 #include "chrome/browser/net/predictor.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 const char kDMToken[] = "1234"; | 83 const char kDMToken[] = "1234"; |
| 83 | 84 |
| 84 ACTION_P(MockSessionManagerClientRetrievePolicyCallback, policy) { | 85 ACTION_P(MockSessionManagerClientRetrievePolicyCallback, policy) { |
| 85 arg0.Run(*policy); | 86 arg0.Run(*policy); |
| 86 } | 87 } |
| 87 | 88 |
| 88 ACTION_P(MockSessionManagerClientStorePolicyCallback, success) { | 89 ACTION_P(MockSessionManagerClientStorePolicyCallback, success) { |
| 89 arg1.Run(success); | 90 arg1.Run(success); |
| 90 } | 91 } |
| 91 | 92 |
| 92 template<typename TESTBASE> | 93 template<typename TESTBASE> |
|
Mattias Nissler (ping if slow)
2012/05/04 13:45:04
while you're at it, how about changing this to use
Joao da Silva
2012/05/04 14:23:10
Sure, done.
| |
| 93 class LoginUtilsTestBase : public TESTBASE, | 94 class LoginUtilsTestBase : public TESTBASE, |
| 94 public LoginUtils::Delegate, | 95 public LoginUtils::Delegate, |
| 95 public LoginStatusConsumer { | 96 public LoginStatusConsumer { |
| 96 public: | 97 public: |
| 97 // Initialization here is important. The UI thread gets the test's | 98 // Initialization here is important. The UI thread gets the test's |
| 98 // message loop, as does the file thread (which never actually gets | 99 // message loop, as does the file thread (which never actually gets |
| 99 // started - so this is a way to fake multiple threads on a single | 100 // started - so this is a way to fake multiple threads on a single |
| 100 // test thread). The IO thread does not get the message loop set, | 101 // test thread). The IO thread does not get the message loop set, |
| 101 // and is never started. This is necessary so that we skip various | 102 // and is never started. This is necessary so that we skip various |
| 102 // bits of initialization that get posted to the IO thread. We do | 103 // bits of initialization that get posted to the IO thread. We do |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 EXPECT_CALL(*cryptohome_, InstallAttributesGet(kAttrEnterpriseDeviceId, _)) | 205 EXPECT_CALL(*cryptohome_, InstallAttributesGet(kAttrEnterpriseDeviceId, _)) |
| 205 .WillRepeatedly(DoAll(SetArgPointee<1>(kDeviceId), | 206 .WillRepeatedly(DoAll(SetArgPointee<1>(kDeviceId), |
| 206 Return(true))); | 207 Return(true))); |
| 207 test_api->SetCryptohomeLibrary(cryptohome_, true); | 208 test_api->SetCryptohomeLibrary(cryptohome_, true); |
| 208 | 209 |
| 209 browser_process_->SetProfileManager( | 210 browser_process_->SetProfileManager( |
| 210 new ProfileManagerWithoutInit(scoped_temp_dir_.path())); | 211 new ProfileManagerWithoutInit(scoped_temp_dir_.path())); |
| 211 connector_ = browser_process_->browser_policy_connector(); | 212 connector_ = browser_process_->browser_policy_connector(); |
| 212 connector_->Init(); | 213 connector_->Init(); |
| 213 | 214 |
| 214 loop_.RunAllPending(); | 215 RunAllPending(); |
| 215 } | 216 } |
| 216 | 217 |
| 217 virtual void TearDown() OVERRIDE { | 218 virtual void TearDown() OVERRIDE { |
| 218 cryptohome::AsyncMethodCaller::Shutdown(); | 219 cryptohome::AsyncMethodCaller::Shutdown(); |
| 219 mock_async_method_caller_ = NULL; | 220 mock_async_method_caller_ = NULL; |
| 220 | 221 |
| 221 loop_.RunAllPending(); | 222 RunAllPending(); |
| 222 { | 223 { |
| 223 // chrome_browser_net::Predictor usually skips its shutdown routines on | 224 // chrome_browser_net::Predictor usually skips its shutdown routines on |
| 224 // unit_tests, but does the full thing when | 225 // unit_tests, but does the full thing when |
| 225 // g_browser_process->profile_manager() is valid during initialization. | 226 // g_browser_process->profile_manager() is valid during initialization. |
| 226 // Run a task on a temporary BrowserThread::IO that allows skipping | 227 // Run a task on a temporary BrowserThread::IO that allows skipping |
| 227 // these routines. | 228 // these routines. |
| 228 // | 229 // |
| 229 // It is important to not have a fake message loop on the IO | 230 // It is important to not have a fake message loop on the IO |
| 230 // thread for the whole test, see comment on LoginUtilsTestBase | 231 // thread for the whole test, see comment on LoginUtilsTestBase |
| 231 // constructor for details. | 232 // constructor for details. |
| 232 io_thread_.DeprecatedSetMessageLoop(&loop_); | 233 io_thread_.DeprecatedSetMessageLoop(&loop_); |
| 233 loop_.PostTask(FROM_HERE, | 234 loop_.PostTask(FROM_HERE, |
| 234 base::Bind(&LoginUtilsTestBase::TearDownOnIO, | 235 base::Bind(&LoginUtilsTestBase::TearDownOnIO, |
| 235 base::Unretained(this))); | 236 base::Unretained(this))); |
| 236 loop_.RunAllPending(); | 237 RunAllPending(); |
| 237 io_thread_.DeprecatedSetMessageLoop(NULL); | 238 io_thread_.DeprecatedSetMessageLoop(NULL); |
| 238 } | 239 } |
| 239 | 240 |
| 240 // These trigger some tasks that have to run while BrowserThread::UI | 241 // These trigger some tasks that have to run while BrowserThread::UI |
| 241 // exists. | 242 // exists. |
| 242 connector_ = NULL; | 243 connector_ = NULL; |
| 243 browser_process_->SetBrowserPolicyConnector(NULL); | 244 browser_process_->SetBrowserPolicyConnector(NULL); |
| 244 browser_process_->SetProfileManager(NULL); | 245 browser_process_->SetProfileManager(NULL); |
| 245 loop_.RunAllPending(); | 246 RunAllPending(); |
| 246 } | 247 } |
| 247 | 248 |
| 248 void TearDownOnIO() { | 249 void TearDownOnIO() { |
| 249 std::vector<Profile*> profiles = | 250 std::vector<Profile*> profiles = |
| 250 browser_process_->profile_manager()->GetLoadedProfiles(); | 251 browser_process_->profile_manager()->GetLoadedProfiles(); |
| 251 for (size_t i = 0; i < profiles.size(); ++i) { | 252 for (size_t i = 0; i < profiles.size(); ++i) { |
| 252 chrome_browser_net::Predictor* predictor = | 253 chrome_browser_net::Predictor* predictor = |
| 253 profiles[i]->GetNetworkPredictor(); | 254 profiles[i]->GetNetworkPredictor(); |
| 254 if (predictor) { | 255 if (predictor) { |
| 255 predictor->EnablePredictorOnIOThread(false); | 256 predictor->EnablePredictorOnIOThread(false); |
| 256 predictor->Shutdown(); | 257 predictor->Shutdown(); |
| 257 } | 258 } |
| 258 } | 259 } |
| 259 } | 260 } |
| 260 | 261 |
| 262 void RunAllPending() { | |
| 263 loop_.RunAllPending(); | |
| 264 BrowserThread::GetBlockingPool()->FlushForTesting(); | |
| 265 loop_.RunAllPending(); | |
| 266 } | |
| 267 | |
| 261 virtual void OnProfilePrepared(Profile* profile) OVERRIDE { | 268 virtual void OnProfilePrepared(Profile* profile) OVERRIDE { |
| 262 EXPECT_FALSE(prepared_profile_); | 269 EXPECT_FALSE(prepared_profile_); |
| 263 prepared_profile_ = profile; | 270 prepared_profile_ = profile; |
| 264 } | 271 } |
| 265 | 272 |
| 266 virtual void OnLoginFailure(const LoginFailure& error) OVERRIDE { | 273 virtual void OnLoginFailure(const LoginFailure& error) OVERRIDE { |
| 267 FAIL() << "OnLoginFailure not expected"; | 274 FAIL() << "OnLoginFailure not expected"; |
| 268 } | 275 } |
| 269 | 276 |
| 270 virtual void OnLoginSuccess(const std::string& username, | 277 virtual void OnLoginSuccess(const std::string& username, |
| 271 const std::string& password, | 278 const std::string& password, |
| 272 bool pending_requests, | 279 bool pending_requests, |
| 273 bool using_oauth) OVERRIDE { | 280 bool using_oauth) OVERRIDE { |
| 274 FAIL() << "OnLoginSuccess not expected"; | 281 FAIL() << "OnLoginSuccess not expected"; |
| 275 } | 282 } |
| 276 | 283 |
| 277 void LockDevice(const std::string& username) { | 284 void LockDevice(const std::string& username) { |
| 278 EXPECT_CALL(*cryptohome_, InstallAttributesIsFirstInstall()) | 285 EXPECT_CALL(*cryptohome_, InstallAttributesIsFirstInstall()) |
| 279 .WillOnce(Return(true)) | 286 .WillOnce(Return(true)) |
| 280 .WillRepeatedly(Return(false)); | 287 .WillRepeatedly(Return(false)); |
| 281 policy::CloudPolicyDataStore* device_data_store = | 288 policy::CloudPolicyDataStore* device_data_store = |
| 282 connector_->GetDeviceCloudPolicyDataStore(); | 289 connector_->GetDeviceCloudPolicyDataStore(); |
| 283 device_data_store->set_device_mode(policy::DEVICE_MODE_ENTERPRISE); | 290 device_data_store->set_device_mode(policy::DEVICE_MODE_ENTERPRISE); |
| 284 device_data_store->set_device_id(kDeviceId); | 291 device_data_store->set_device_id(kDeviceId); |
| 285 EXPECT_EQ(policy::EnterpriseInstallAttributes::LOCK_SUCCESS, | 292 EXPECT_EQ(policy::EnterpriseInstallAttributes::LOCK_SUCCESS, |
| 286 connector_->LockDevice(username)); | 293 connector_->LockDevice(username)); |
| 287 loop_.RunAllPending(); | 294 RunAllPending(); |
| 288 } | 295 } |
| 289 | 296 |
| 290 void PrepareProfile(const std::string& username) { | 297 void PrepareProfile(const std::string& username) { |
| 291 MockSessionManagerClient* session_managed_client = | 298 MockSessionManagerClient* session_managed_client = |
| 292 mock_dbus_thread_manager_.mock_session_manager_client(); | 299 mock_dbus_thread_manager_.mock_session_manager_client(); |
| 293 EXPECT_CALL(*session_managed_client, StartSession(_)); | 300 EXPECT_CALL(*session_managed_client, StartSession(_)); |
| 294 EXPECT_CALL(*cryptohome_, GetSystemSalt()) | 301 EXPECT_CALL(*cryptohome_, GetSystemSalt()) |
| 295 .WillRepeatedly(Return(std::string("stub_system_salt"))); | 302 .WillRepeatedly(Return(std::string("stub_system_salt"))); |
| 296 EXPECT_CALL(*mock_async_method_caller_, AsyncMount(_, _, _, _)) | 303 EXPECT_CALL(*mock_async_method_caller_, AsyncMount(_, _, _, _)) |
| 297 .WillRepeatedly(Return()); | 304 .WillRepeatedly(Return()); |
| 298 | 305 |
| 299 scoped_refptr<Authenticator> authenticator = | 306 scoped_refptr<Authenticator> authenticator = |
| 300 LoginUtils::Get()->CreateAuthenticator(this); | 307 LoginUtils::Get()->CreateAuthenticator(this); |
| 301 authenticator->CompleteLogin(ProfileManager::GetDefaultProfile(), | 308 authenticator->CompleteLogin(ProfileManager::GetDefaultProfile(), |
| 302 username, | 309 username, |
| 303 "password"); | 310 "password"); |
| 304 | 311 |
| 305 LoginUtils::Get()->PrepareProfile(username, std::string(), "password", | 312 LoginUtils::Get()->PrepareProfile(username, std::string(), "password", |
| 306 false, true, false, this); | 313 false, true, false, this); |
| 307 loop_.RunAllPending(); | 314 RunAllPending(); |
| 308 } | 315 } |
| 309 | 316 |
| 310 TestURLFetcher* PrepareOAuthFetcher(const std::string& expected_url) { | 317 TestURLFetcher* PrepareOAuthFetcher(const std::string& expected_url) { |
| 311 TestURLFetcher* fetcher = test_url_fetcher_factory_.GetFetcherByID(0); | 318 TestURLFetcher* fetcher = test_url_fetcher_factory_.GetFetcherByID(0); |
| 312 EXPECT_TRUE(fetcher); | 319 EXPECT_TRUE(fetcher); |
| 313 EXPECT_TRUE(fetcher->delegate()); | 320 EXPECT_TRUE(fetcher->delegate()); |
| 314 EXPECT_TRUE(StartsWithASCII(fetcher->GetOriginalURL().spec(), | 321 EXPECT_TRUE(StartsWithASCII(fetcher->GetOriginalURL().spec(), |
| 315 expected_url, | 322 expected_url, |
| 316 true)); | 323 true)); |
| 317 fetcher->set_url(fetcher->GetOriginalURL()); | 324 fetcher->set_url(fetcher->GetOriginalURL()); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 481 fetcher->delegate()->OnURLFetchComplete(fetcher); | 488 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 482 if (steps < 3) break; | 489 if (steps < 3) break; |
| 483 | 490 |
| 484 // Fake OAuth service token retrieval: | 491 // Fake OAuth service token retrieval: |
| 485 fetcher = PrepareOAuthFetcher(gaia_urls->oauth_wrap_bridge_url()); | 492 fetcher = PrepareOAuthFetcher(gaia_urls->oauth_wrap_bridge_url()); |
| 486 fetcher->SetResponseString(kOAuthServiceTokenData); | 493 fetcher->SetResponseString(kOAuthServiceTokenData); |
| 487 fetcher->delegate()->OnURLFetchComplete(fetcher); | 494 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 488 | 495 |
| 489 // The cloud policy subsystem is now ready to fetch the dmtoken and the user | 496 // The cloud policy subsystem is now ready to fetch the dmtoken and the user |
| 490 // policy. | 497 // policy. |
| 491 loop_.RunAllPending(); | 498 RunAllPending(); |
| 492 if (steps < 4) break; | 499 if (steps < 4) break; |
| 493 | 500 |
| 494 fetcher = PrepareDMRegisterFetcher(); | 501 fetcher = PrepareDMRegisterFetcher(); |
| 495 fetcher->delegate()->OnURLFetchComplete(fetcher); | 502 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 496 // The policy fetch job has now been scheduled, run it: | 503 // The policy fetch job has now been scheduled, run it: |
| 497 loop_.RunAllPending(); | 504 RunAllPending(); |
| 498 if (steps < 5) break; | 505 if (steps < 5) break; |
| 499 | 506 |
| 500 // Verify that there is no profile prepared just before the policy fetch. | 507 // Verify that there is no profile prepared just before the policy fetch. |
| 501 EXPECT_FALSE(prepared_profile_); | 508 EXPECT_FALSE(prepared_profile_); |
| 502 | 509 |
| 503 fetcher = PrepareDMPolicyFetcher(); | 510 fetcher = PrepareDMPolicyFetcher(); |
| 504 fetcher->delegate()->OnURLFetchComplete(fetcher); | 511 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 505 } while (0); | 512 } while (0); |
| 506 | 513 |
| 507 if (steps < 5) { | 514 if (steps < 5) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 522 } | 529 } |
| 523 | 530 |
| 524 INSTANTIATE_TEST_CASE_P( | 531 INSTANTIATE_TEST_CASE_P( |
| 525 LoginUtilsBlockingLoginTestInstance, | 532 LoginUtilsBlockingLoginTestInstance, |
| 526 LoginUtilsBlockingLoginTest, | 533 LoginUtilsBlockingLoginTest, |
| 527 testing::Values(0, 1, 2, 3, 4, 5)); | 534 testing::Values(0, 1, 2, 3, 4, 5)); |
| 528 | 535 |
| 529 } // namespace | 536 } // namespace |
| 530 | 537 |
| 531 } | 538 } |
| OLD | NEW |