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