| 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/signin/signin_manager.h" | 5 #include "chrome/browser/signin/signin_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/prefs/testing_pref_service.h" | 11 #include "base/prefs/testing_pref_service.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/prefs/browser_prefs.h" | 14 #include "chrome/browser/prefs/browser_prefs.h" |
| 15 #include "chrome/browser/signin/chrome_signin_manager_delegate.h" |
| 15 #include "chrome/browser/signin/token_service.h" | 16 #include "chrome/browser/signin/token_service.h" |
| 16 #include "chrome/browser/signin/token_service_unittest.h" | 17 #include "chrome/browser/signin/token_service_unittest.h" |
| 17 #include "chrome/browser/webdata/web_data_service.h" | 18 #include "chrome/browser/webdata/web_data_service.h" |
| 18 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 21 #include "chrome/test/base/testing_browser_process.h" | 22 #include "chrome/test/base/testing_browser_process.h" |
| 22 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
| 23 #include "components/webdata/encryptor/encryptor.h" | 24 #include "components/webdata/encryptor/encryptor.h" |
| 24 #include "content/public/browser/child_process_security_policy.h" | 25 #include "content/public/browser/child_process_security_policy.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 profile_->GetRequestContext()->GetURLRequestContext(); | 65 profile_->GetRequestContext()->GetURLRequestContext(); |
| 65 io_setup_complete->Signal(); | 66 io_setup_complete->Signal(); |
| 66 } | 67 } |
| 67 | 68 |
| 68 virtual void SetUp() OVERRIDE { | 69 virtual void SetUp() OVERRIDE { |
| 69 prefs_.reset(new TestingPrefServiceSimple); | 70 prefs_.reset(new TestingPrefServiceSimple); |
| 70 chrome::RegisterLocalState(prefs_->registry()); | 71 chrome::RegisterLocalState(prefs_->registry()); |
| 71 TestingBrowserProcess::GetGlobal()->SetLocalState( | 72 TestingBrowserProcess::GetGlobal()->SetLocalState( |
| 72 prefs_.get()); | 73 prefs_.get()); |
| 73 TokenServiceTestHarness::SetUp(); | 74 TokenServiceTestHarness::SetUp(); |
| 74 manager_.reset(new SigninManager()); | 75 manager_.reset(new SigninManager( |
| 76 scoped_ptr<SigninManagerDelegate>( |
| 77 new ChromeSigninManagerDelegate(profile_.get())))); |
| 75 google_login_success_.ListenFor( | 78 google_login_success_.ListenFor( |
| 76 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 79 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 77 content::Source<Profile>(profile_.get())); | 80 content::Source<Profile>(profile_.get())); |
| 78 google_login_failure_.ListenFor(chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, | 81 google_login_failure_.ListenFor(chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
| 79 content::Source<Profile>(profile_.get())); | 82 content::Source<Profile>(profile_.get())); |
| 80 | 83 |
| 81 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); | 84 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); |
| 82 ASSERT_TRUE(io_thread_->Start()); | 85 ASSERT_TRUE(io_thread_->Start()); |
| 83 base::WaitableEvent io_setup_complete(true, false); | 86 base::WaitableEvent io_setup_complete(true, false); |
| 84 BrowserThread::PostTask( | 87 BrowserThread::PostTask( |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 // This is flow, the oauth2 credentials should already be available in | 201 // This is flow, the oauth2 credentials should already be available in |
| 199 // the token service. | 202 // the token service. |
| 200 EXPECT_TRUE(service_->HasOAuthLoginToken()); | 203 EXPECT_TRUE(service_->HasOAuthLoginToken()); |
| 201 | 204 |
| 202 // Should go into token service and stop. | 205 // Should go into token service and stop. |
| 203 EXPECT_EQ(1U, google_login_success_.size()); | 206 EXPECT_EQ(1U, google_login_success_.size()); |
| 204 EXPECT_EQ(0U, google_login_failure_.size()); | 207 EXPECT_EQ(0U, google_login_failure_.size()); |
| 205 | 208 |
| 206 // Should persist across resets. | 209 // Should persist across resets. |
| 207 manager_->Shutdown(); | 210 manager_->Shutdown(); |
| 208 manager_.reset(new SigninManager()); | 211 manager_.reset(new SigninManager( |
| 212 scoped_ptr<SigninManagerDelegate>( |
| 213 new ChromeSigninManagerDelegate(profile_.get())))); |
| 209 manager_->Initialize(profile_.get()); | 214 manager_->Initialize(profile_.get()); |
| 210 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername()); | 215 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername()); |
| 211 } | 216 } |
| 212 | 217 |
| 213 // Helper method that wraps the logic when signin with credentials | 218 // Helper method that wraps the logic when signin with credentials |
| 214 // should fail. If |requestSent| is true, then simulate valid resopnse. | 219 // should fail. If |requestSent| is true, then simulate valid resopnse. |
| 215 // Otherwise the sign-in is aborted before any request is sent, thus no need | 220 // Otherwise the sign-in is aborted before any request is sent, thus no need |
| 216 // to simulatate response. | 221 // to simulatate response. |
| 217 void ExpectSignInWithCredentialsFail(bool requestSent) { | 222 void ExpectSignInWithCredentialsFail(bool requestSent) { |
| 218 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 223 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 | 273 |
| 269 service_->OnIssueAuthTokenSuccess(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 274 service_->OnIssueAuthTokenSuccess(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 270 "oauth2Token"); | 275 "oauth2Token"); |
| 271 SimulateValidUberToken(); | 276 SimulateValidUberToken(); |
| 272 // Check that the login cookie has been sent. | 277 // Check that the login cookie has been sent. |
| 273 ASSERT_NE(std::find(cookies_.begin(), cookies_.end(), "checkCookie = true"), | 278 ASSERT_NE(std::find(cookies_.begin(), cookies_.end(), "checkCookie = true"), |
| 274 cookies_.end()); | 279 cookies_.end()); |
| 275 | 280 |
| 276 // Should persist across resets. | 281 // Should persist across resets. |
| 277 manager_->Shutdown(); | 282 manager_->Shutdown(); |
| 278 manager_.reset(new SigninManager()); | 283 manager_.reset(new SigninManager( |
| 284 scoped_ptr<SigninManagerDelegate>( |
| 285 new ChromeSigninManagerDelegate(profile_.get())))); |
| 279 manager_->Initialize(profile_.get()); | 286 manager_->Initialize(profile_.get()); |
| 280 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername()); | 287 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername()); |
| 281 } | 288 } |
| 282 | 289 |
| 283 TEST_F(SigninManagerTest, SignInWithCredentials) { | 290 TEST_F(SigninManagerTest, SignInWithCredentials) { |
| 284 manager_->Initialize(profile_.get()); | 291 manager_->Initialize(profile_.get()); |
| 285 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 292 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| 286 | 293 |
| 287 manager_->StartSignInWithCredentials( | 294 manager_->StartSignInWithCredentials( |
| 288 "0", | 295 "0", |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 manager_->ClearTransientSigninData(); | 470 manager_->ClearTransientSigninData(); |
| 464 EXPECT_TRUE(manager_->last_result_.data.empty()); | 471 EXPECT_TRUE(manager_->last_result_.data.empty()); |
| 465 EXPECT_FALSE(manager_->GetAuthenticatedUsername().empty()); | 472 EXPECT_FALSE(manager_->GetAuthenticatedUsername().empty()); |
| 466 | 473 |
| 467 // Ensure preferences are not modified. | 474 // Ensure preferences are not modified. |
| 468 EXPECT_FALSE( | 475 EXPECT_FALSE( |
| 469 profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername).empty()); | 476 profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername).empty()); |
| 470 | 477 |
| 471 // On reset it should be regenerated. | 478 // On reset it should be regenerated. |
| 472 manager_->Shutdown(); | 479 manager_->Shutdown(); |
| 473 manager_.reset(new SigninManager()); | 480 manager_.reset(new SigninManager( |
| 481 scoped_ptr<SigninManagerDelegate>( |
| 482 new ChromeSigninManagerDelegate(profile_.get())))); |
| 474 manager_->Initialize(profile_.get()); | 483 manager_->Initialize(profile_.get()); |
| 475 | 484 |
| 476 // Now make sure we have the right user name. | 485 // Now make sure we have the right user name. |
| 477 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername()); | 486 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername()); |
| 478 } | 487 } |
| 479 | 488 |
| 480 TEST_F(SigninManagerTest, SignOutClientLogin) { | 489 TEST_F(SigninManagerTest, SignOutClientLogin) { |
| 481 manager_->Initialize(profile_.get()); | 490 manager_->Initialize(profile_.get()); |
| 482 manager_->StartSignIn("username", "password", std::string(), std::string()); | 491 manager_->StartSignIn("username", "password", std::string(), std::string()); |
| 483 SimulateValidResponseClientLogin(false); | 492 SimulateValidResponseClientLogin(false); |
| 484 manager_->OnClientLoginSuccess(credentials_); | 493 manager_->OnClientLoginSuccess(credentials_); |
| 485 | 494 |
| 486 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername()); | 495 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername()); |
| 487 manager_->SignOut(); | 496 manager_->SignOut(); |
| 488 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 497 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| 489 // Should not be persisted anymore | 498 // Should not be persisted anymore |
| 490 manager_->Shutdown(); | 499 manager_->Shutdown(); |
| 491 manager_.reset(new SigninManager()); | 500 manager_.reset(new SigninManager( |
| 501 scoped_ptr<SigninManagerDelegate>( |
| 502 new ChromeSigninManagerDelegate(profile_.get())))); |
| 492 manager_->Initialize(profile_.get()); | 503 manager_->Initialize(profile_.get()); |
| 493 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 504 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| 494 } | 505 } |
| 495 | 506 |
| 496 TEST_F(SigninManagerTest, SignInFailureClientLogin) { | 507 TEST_F(SigninManagerTest, SignInFailureClientLogin) { |
| 497 manager_->Initialize(profile_.get()); | 508 manager_->Initialize(profile_.get()); |
| 498 manager_->StartSignIn("username", "password", std::string(), std::string()); | 509 manager_->StartSignIn("username", "password", std::string(), std::string()); |
| 499 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 510 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 500 manager_->OnClientLoginFailure(error); | 511 manager_->OnClientLoginFailure(error); |
| 501 | 512 |
| 502 EXPECT_EQ(0U, google_login_success_.size()); | 513 EXPECT_EQ(0U, google_login_success_.size()); |
| 503 EXPECT_EQ(1U, google_login_failure_.size()); | 514 EXPECT_EQ(1U, google_login_failure_.size()); |
| 504 | 515 |
| 505 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 516 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| 506 | 517 |
| 507 // Should not be persisted | 518 // Should not be persisted |
| 508 manager_->Shutdown(); | 519 manager_->Shutdown(); |
| 509 manager_.reset(new SigninManager()); | 520 manager_.reset(new SigninManager( |
| 521 scoped_ptr<SigninManagerDelegate>( |
| 522 new ChromeSigninManagerDelegate(profile_.get())))); |
| 510 manager_->Initialize(profile_.get()); | 523 manager_->Initialize(profile_.get()); |
| 511 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 524 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| 512 } | 525 } |
| 513 | 526 |
| 514 TEST_F(SigninManagerTest, ProvideSecondFactorSuccess) { | 527 TEST_F(SigninManagerTest, ProvideSecondFactorSuccess) { |
| 515 manager_->Initialize(profile_.get()); | 528 manager_->Initialize(profile_.get()); |
| 516 manager_->StartSignIn("username", "password", std::string(), std::string()); | 529 manager_->StartSignIn("username", "password", std::string(), std::string()); |
| 517 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); | 530 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); |
| 518 manager_->OnClientLoginFailure(error); | 531 manager_->OnClientLoginFailure(error); |
| 519 | 532 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 GURL("http://accounts.google.com/ServiceLogin?service=chromiumsync"))); | 607 GURL("http://accounts.google.com/ServiceLogin?service=chromiumsync"))); |
| 595 // http, not https, should not be treated as web based signin. | 608 // http, not https, should not be treated as web based signin. |
| 596 EXPECT_FALSE(SigninManager::IsWebBasedSigninFlowURL( | 609 EXPECT_FALSE(SigninManager::IsWebBasedSigninFlowURL( |
| 597 GURL("http://accounts.google.com/ServiceLogin?service=googlemail"))); | 610 GURL("http://accounts.google.com/ServiceLogin?service=googlemail"))); |
| 598 // chromiumsync is double-embedded in a continue query param. | 611 // chromiumsync is double-embedded in a continue query param. |
| 599 EXPECT_TRUE(SigninManager::IsWebBasedSigninFlowURL( | 612 EXPECT_TRUE(SigninManager::IsWebBasedSigninFlowURL( |
| 600 GURL("https://accounts.google.com/CheckCookie?" | 613 GURL("https://accounts.google.com/CheckCookie?" |
| 601 "continue=https%3A%2F%2Fwww.google.com%2Fintl%2Fen-US%2Fchrome" | 614 "continue=https%3A%2F%2Fwww.google.com%2Fintl%2Fen-US%2Fchrome" |
| 602 "%2Fblank.html%3Fsource%3D3%26nonadv%3D1&service=chromiumsync"))); | 615 "%2Fblank.html%3Fsource%3D3%26nonadv%3D1&service=chromiumsync"))); |
| 603 } | 616 } |
| OLD | NEW |