| 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/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/browser/prefs/browser_prefs.h" | 16 #include "chrome/browser/prefs/browser_prefs.h" |
| 17 #include "chrome/browser/signin/chrome_signin_manager_delegate.h" | 17 #include "chrome/browser/signin/chrome_signin_manager_delegate.h" |
| 18 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| 19 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 18 #include "chrome/browser/signin/signin_manager_factory.h" | 21 #include "chrome/browser/signin/signin_manager_factory.h" |
| 19 #include "chrome/browser/signin/token_service.h" | |
| 20 #include "chrome/browser/signin/token_service_unittest.h" | |
| 21 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 22 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 23 #include "chrome/test/base/testing_browser_process.h" | 24 #include "chrome/test/base/testing_browser_process.h" |
| 24 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 25 #include "components/webdata/encryptor/encryptor.h" | 26 #include "components/webdata/encryptor/encryptor.h" |
| 26 #include "content/public/browser/child_process_security_policy.h" | 27 #include "content/public/browser/child_process_security_policy.h" |
| 28 #include "content/public/browser/notification_source.h" |
| 27 #include "content/public/test/test_browser_thread_bundle.h" | 29 #include "content/public/test/test_browser_thread_bundle.h" |
| 30 #include "content/public/test/test_notification_tracker.h" |
| 28 #include "google_apis/gaia/gaia_constants.h" | 31 #include "google_apis/gaia/gaia_constants.h" |
| 29 #include "google_apis/gaia/gaia_urls.h" | 32 #include "google_apis/gaia/gaia_urls.h" |
| 30 #include "net/cookies/cookie_monster.h" | 33 #include "net/cookies/cookie_monster.h" |
| 31 #include "net/url_request/test_url_fetcher_factory.h" | 34 #include "net/url_request/test_url_fetcher_factory.h" |
| 32 #include "net/url_request/url_request.h" | 35 #include "net/url_request/url_request.h" |
| 33 #include "net/url_request/url_request_context_getter.h" | 36 #include "net/url_request/url_request_context_getter.h" |
| 34 #include "net/url_request/url_request_status.h" | 37 #include "net/url_request/url_request_status.h" |
| 35 | 38 |
| 36 #include "testing/gmock/include/gmock/gmock.h" | 39 #include "testing/gmock/include/gmock/gmock.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 40 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 55 service = new SigninManager( | 58 service = new SigninManager( |
| 56 scoped_ptr<SigninManagerDelegate>( | 59 scoped_ptr<SigninManagerDelegate>( |
| 57 new ChromeSigninManagerDelegate(profile))); | 60 new ChromeSigninManagerDelegate(profile))); |
| 58 service->Initialize(profile, NULL); | 61 service->Initialize(profile, NULL); |
| 59 return service; | 62 return service; |
| 60 } | 63 } |
| 61 | 64 |
| 62 } // namespace | 65 } // namespace |
| 63 | 66 |
| 64 | 67 |
| 65 class SigninManagerTest : public TokenServiceTestHarness { | 68 class SigninManagerTest : public testing::Test { |
| 66 public: | 69 public: |
| 67 SigninManagerTest() : manager_(NULL) {} | 70 SigninManagerTest() : manager_(NULL) {} |
| 68 virtual ~SigninManagerTest() {} | 71 virtual ~SigninManagerTest() {} |
| 69 | 72 |
| 70 virtual void SetUp() OVERRIDE { | 73 virtual void SetUp() OVERRIDE { |
| 71 manager_ = NULL; | 74 manager_ = NULL; |
| 72 prefs_.reset(new TestingPrefServiceSimple); | 75 prefs_.reset(new TestingPrefServiceSimple); |
| 73 chrome::RegisterLocalState(prefs_->registry()); | 76 chrome::RegisterLocalState(prefs_->registry()); |
| 74 TestingBrowserProcess::GetGlobal()->SetLocalState( | 77 TestingBrowserProcess::GetGlobal()->SetLocalState( |
| 75 prefs_.get()); | 78 prefs_.get()); |
| 76 TokenServiceTestHarness::SetUp(); | 79 TestingProfile::Builder builder; |
| 80 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 81 FakeProfileOAuth2TokenService::Build); |
| 82 profile_ = builder.Build(); |
| 77 google_login_success_.ListenFor( | 83 google_login_success_.ListenFor( |
| 78 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 84 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 79 content::Source<Profile>(profile())); | 85 content::Source<Profile>(profile())); |
| 80 google_login_failure_.ListenFor(chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, | 86 google_login_failure_.ListenFor(chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
| 81 content::Source<Profile>(profile())); | 87 content::Source<Profile>(profile())); |
| 82 } | 88 } |
| 83 | 89 |
| 84 virtual void TearDown() OVERRIDE { | 90 virtual void TearDown() OVERRIDE { |
| 85 // Destroy the SigninManager here, because it relies on profile() which is | 91 // Destroy the SigninManager here, because it relies on profile() which is |
| 86 // freed in the base class. | 92 // freed in the base class. |
| 87 if (naked_manager_) { | 93 if (naked_manager_) { |
| 88 naked_manager_->Shutdown(); | 94 naked_manager_->Shutdown(); |
| 89 naked_manager_.reset(NULL); | 95 naked_manager_.reset(NULL); |
| 90 } | 96 } |
| 91 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL); | 97 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL); |
| 92 prefs_.reset(NULL); | 98 |
| 93 TokenServiceTestHarness::TearDown(); | 99 // Manually destroy PrefService and Profile so that they are shutdown |
| 100 // in the correct order. Both need to be destroyed before the |
| 101 // |thread_bundle_| member. |
| 102 prefs_.reset(); |
| 103 profile_.reset(); |
| 94 } | 104 } |
| 95 | 105 |
| 106 TestingProfile* profile() { return profile_.get(); } |
| 107 |
| 96 // Create a signin manager as a service if other code will try to get it as | 108 // Create a signin manager as a service if other code will try to get it as |
| 97 // a PKS. | 109 // a PKS. |
| 98 void CreateSigninManagerAsService() { | 110 void CreateSigninManagerAsService() { |
| 99 DCHECK(!manager_); | 111 DCHECK(!manager_); |
| 100 DCHECK(!naked_manager_); | 112 DCHECK(!naked_manager_); |
| 101 manager_ = static_cast<SigninManager*>( | 113 manager_ = static_cast<SigninManager*>( |
| 102 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 114 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 103 profile(), SigninManagerBuild)); | 115 profile(), SigninManagerBuild)); |
| 104 } | 116 } |
| 105 | 117 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 cookies, "<html></html>"); | 193 cookies, "<html></html>"); |
| 182 } | 194 } |
| 183 | 195 |
| 184 void ExpectSignInWithCredentialsSuccess() { | 196 void ExpectSignInWithCredentialsSuccess() { |
| 185 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 197 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| 186 | 198 |
| 187 SimulateValidResponseSignInWithCredentials(); | 199 SimulateValidResponseSignInWithCredentials(); |
| 188 | 200 |
| 189 EXPECT_FALSE(manager_->GetAuthenticatedUsername().empty()); | 201 EXPECT_FALSE(manager_->GetAuthenticatedUsername().empty()); |
| 190 | 202 |
| 191 // This is flow, the oauth2 credentials should already be available in | 203 ProfileOAuth2TokenService* token_service = |
| 192 // the token service. | 204 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()); |
| 193 EXPECT_TRUE(service()->HasOAuthLoginToken()); | 205 EXPECT_TRUE(token_service->RefreshTokenIsAvailable( |
| 206 manager_->GetAuthenticatedUsername())); |
| 194 | 207 |
| 195 // Should go into token service and stop. | 208 // Should go into token service and stop. |
| 196 EXPECT_EQ(1U, google_login_success_.size()); | 209 EXPECT_EQ(1U, google_login_success_.size()); |
| 197 EXPECT_EQ(0U, google_login_failure_.size()); | 210 EXPECT_EQ(0U, google_login_failure_.size()); |
| 198 } | 211 } |
| 199 | 212 |
| 200 // Helper method that wraps the logic when signin with credentials | 213 // Helper method that wraps the logic when signin with credentials |
| 201 // should fail. If |requestSent| is true, then simulate valid resopnse. | 214 // should fail. If |requestSent| is true, then simulate valid resopnse. |
| 202 // Otherwise the sign-in is aborted before any request is sent, thus no need | 215 // Otherwise the sign-in is aborted before any request is sent, thus no need |
| 203 // to simulatate response. | 216 // to simulatate response. |
| 204 void ExpectSignInWithCredentialsFail(bool requestSent) { | 217 void ExpectSignInWithCredentialsFail(bool requestSent) { |
| 205 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 218 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| 206 | 219 |
| 207 if (requestSent) | 220 if (requestSent) |
| 208 SimulateValidResponseSignInWithCredentials(); | 221 SimulateValidResponseSignInWithCredentials(); |
| 209 | 222 |
| 210 // The oauth2 credentials should not be available in the token service | 223 ProfileOAuth2TokenService* token_service = |
| 211 // because the email was incorrect. | 224 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()); |
| 212 EXPECT_FALSE(service()->HasOAuthLoginToken()); | 225 EXPECT_FALSE(token_service->RefreshTokenIsAvailable( |
| 226 manager_->GetAuthenticatedUsername())); |
| 213 | 227 |
| 214 // Should go into token service and stop. | 228 // Should go into token service and stop. |
| 215 EXPECT_EQ(0U, google_login_success_.size()); | 229 EXPECT_EQ(0U, google_login_success_.size()); |
| 216 EXPECT_EQ(1U, google_login_failure_.size()); | 230 EXPECT_EQ(1U, google_login_failure_.size()); |
| 217 } | 231 } |
| 218 | 232 |
| 219 void CompleteSigninCallback(const std::string& oauth_token) { | 233 void CompleteSigninCallback(const std::string& oauth_token) { |
| 220 oauth_tokens_fetched_.push_back(oauth_token); | 234 oauth_tokens_fetched_.push_back(oauth_token); |
| 221 manager_->CompletePendingSignin(); | 235 manager_->CompletePendingSignin(); |
| 222 } | 236 } |
| 223 | 237 |
| 224 void CancelSigninCallback(const std::string& oauth_token) { | 238 void CancelSigninCallback(const std::string& oauth_token) { |
| 225 oauth_tokens_fetched_.push_back(oauth_token); | 239 oauth_tokens_fetched_.push_back(oauth_token); |
| 226 manager_->SignOut(); | 240 manager_->SignOut(); |
| 227 } | 241 } |
| 228 | 242 |
| 243 content::TestBrowserThreadBundle thread_bundle_; |
| 229 net::TestURLFetcherFactory factory_; | 244 net::TestURLFetcherFactory factory_; |
| 230 scoped_ptr<SigninManager> naked_manager_; | 245 scoped_ptr<SigninManager> naked_manager_; |
| 231 SigninManager* manager_; | 246 SigninManager* manager_; |
| 247 scoped_ptr<TestingProfile> profile_; |
| 232 content::TestNotificationTracker google_login_success_; | 248 content::TestNotificationTracker google_login_success_; |
| 233 content::TestNotificationTracker google_login_failure_; | 249 content::TestNotificationTracker google_login_failure_; |
| 234 std::vector<std::string> oauth_tokens_fetched_; | 250 std::vector<std::string> oauth_tokens_fetched_; |
| 235 scoped_ptr<TestingPrefServiceSimple> prefs_; | 251 scoped_ptr<TestingPrefServiceSimple> prefs_; |
| 236 std::vector<std::string> cookies_; | 252 std::vector<std::string> cookies_; |
| 237 }; | 253 }; |
| 238 | 254 |
| 239 TEST_F(SigninManagerTest, SignInWithCredentials) { | 255 TEST_F(SigninManagerTest, SignInWithCredentials) { |
| 240 CreateSigninManagerAsService(); | 256 CreateSigninManagerAsService(); |
| 241 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 257 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 profile()->GetPrefs()->GetString(prefs::kGoogleServicesUsername)); | 537 profile()->GetPrefs()->GetString(prefs::kGoogleServicesUsername)); |
| 522 EXPECT_EQ("external@example.com", manager_->GetAuthenticatedUsername()); | 538 EXPECT_EQ("external@example.com", manager_->GetAuthenticatedUsername()); |
| 523 } | 539 } |
| 524 | 540 |
| 525 TEST_F(SigninManagerTest, SigninNotAllowed) { | 541 TEST_F(SigninManagerTest, SigninNotAllowed) { |
| 526 std::string user("user@google.com"); | 542 std::string user("user@google.com"); |
| 527 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername, user); | 543 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername, user); |
| 528 profile()->GetPrefs()->SetBoolean(prefs::kSigninAllowed, false); | 544 profile()->GetPrefs()->SetBoolean(prefs::kSigninAllowed, false); |
| 529 CreateSigninManagerAsService(); | 545 CreateSigninManagerAsService(); |
| 530 } | 546 } |
| OLD | NEW |