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/stringprintf.h" | |
| 10 #include "chrome/browser/password_manager/encryptor.h" | 11 #include "chrome/browser/password_manager/encryptor.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/signin/token_service.h" | 13 #include "chrome/browser/signin/token_service.h" |
| 13 #include "chrome/browser/signin/token_service_unittest.h" | 14 #include "chrome/browser/signin/token_service_unittest.h" |
| 14 #include "chrome/browser/webdata/web_data_service.h" | 15 #include "chrome/browser/webdata/web_data_service.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/net/gaia/gaia_constants.h" | |
| 16 #include "chrome/common/net/gaia/gaia_urls.h" | 18 #include "chrome/common/net/gaia/gaia_urls.h" |
| 17 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 19 #include "content/test/test_url_fetcher_factory.h" | 21 #include "content/test/test_url_fetcher_factory.h" |
| 20 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 21 #include "net/url_request/url_request_status.h" | 23 #include "net/url_request/url_request_status.h" |
| 22 | 24 |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 27 |
| 28 namespace { | |
| 29 | |
| 30 const char kGetTokenPairValidResponse[] = | |
| 31 "{" | |
| 32 " \"refresh_token\": \"rt1\"," | |
| 33 " \"access_token\": \"at1\"," | |
| 34 " \"expires_in\": 3600," | |
| 35 " \"token_type\": \"Bearer\"" | |
| 36 "}"; | |
| 37 | |
| 38 const char kUberAuthTokenURLFormat[] = | |
| 39 "%s?source=%s&issueuberauth=1"; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 | |
| 26 class SigninManagerTest : public TokenServiceTestHarness { | 44 class SigninManagerTest : public TokenServiceTestHarness { |
| 27 public: | 45 public: |
| 28 virtual void SetUp() OVERRIDE { | 46 virtual void SetUp() OVERRIDE { |
| 29 TokenServiceTestHarness::SetUp(); | 47 TokenServiceTestHarness::SetUp(); |
| 30 manager_.reset(new SigninManager()); | 48 manager_.reset(new SigninManager()); |
| 31 google_login_success_.ListenFor( | 49 google_login_success_.ListenFor( |
| 32 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 50 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 33 content::Source<Profile>(profile_.get())); | 51 content::Source<Profile>(profile_.get())); |
| 34 google_login_failure_.ListenFor(chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, | 52 google_login_failure_.ListenFor(chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
| 35 content::Source<Profile>(profile_.get())); | 53 content::Source<Profile>(profile_.get())); |
| 36 } | 54 } |
| 37 | 55 |
| 56 void SimulateValidResponseSignInWithCredentials() { | |
| 57 // Simulate the correct StartOAuthLoginTokenFetch response. This involves | |
| 58 // two separate fetches. | |
| 59 TestURLFetcher* fetcher = factory_.GetFetcherByID(0); | |
| 60 DCHECK(fetcher); | |
| 61 DCHECK(fetcher->delegate()); | |
| 62 | |
| 63 fetcher->set_url( | |
| 64 GURL(GaiaUrls::GetInstance()->client_login_to_oauth2_url())); | |
| 65 fetcher->set_status(net::URLRequestStatus()); | |
| 66 fetcher->set_response_code(200); | |
| 67 fetcher->SetResponseString(kGetTokenPairValidResponse); | |
| 68 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 69 | |
| 70 fetcher = factory_.GetFetcherByID(0); | |
| 71 DCHECK(fetcher); | |
| 72 DCHECK(fetcher->delegate()); | |
| 73 | |
| 74 fetcher->set_url( | |
| 75 GURL(GaiaUrls::GetInstance()->oauth2_token_url())); | |
| 76 fetcher->set_status(net::URLRequestStatus()); | |
| 77 fetcher->set_response_code(200); | |
| 78 fetcher->SetResponseString(kGetTokenPairValidResponse); | |
| 79 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 80 | |
| 81 // Simulate the correct StartUberAuthTokenFetch response. | |
| 82 GURL url(base::StringPrintf( | |
| 83 kUberAuthTokenURLFormat, | |
| 84 GaiaUrls::GetInstance()->oauth1_login_url().c_str(), | |
| 85 GaiaConstants::kChromeSource)); | |
| 86 fetcher = factory_.GetFetcherByID(0); | |
| 87 DCHECK(fetcher); | |
| 88 DCHECK(fetcher->delegate()); | |
| 89 fetcher->set_url(url); | |
| 90 fetcher->set_status(net::URLRequestStatus()); | |
| 91 fetcher->set_response_code(200); | |
| 92 fetcher->SetResponseString("token"); | |
| 93 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 94 | |
| 95 // Simulate the correct StartTokenAuth response. | |
| 96 fetcher = factory_.GetFetcherByID(0); | |
| 97 DCHECK(fetcher); | |
| 98 DCHECK(fetcher->delegate()); | |
| 99 | |
| 100 fetcher->set_url( | |
| 101 GURL(GaiaUrls::GetInstance()->token_auth_url())); | |
| 102 fetcher->set_status(net::URLRequestStatus()); | |
| 103 fetcher->set_response_code(200); | |
| 104 net::ResponseCookies cookies; | |
| 105 cookies.push_back("SID=sid"); | |
| 106 cookies.push_back("LSID=lsid"); | |
| 107 fetcher->set_cookies(cookies); | |
| 108 fetcher->SetResponseString("data"); | |
| 109 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 110 | |
| 111 // Simulate the correct GetUserInfo response. | |
| 112 fetcher = factory_.GetFetcherByID(0); | |
| 113 DCHECK(fetcher); | |
| 114 DCHECK(fetcher->delegate()); | |
| 115 fetcher->set_url(GURL(GaiaUrls::GetInstance()->get_user_info_url())); | |
| 116 fetcher->set_status(net::URLRequestStatus()); | |
| 117 fetcher->set_response_code(200); | |
| 118 fetcher->SetResponseString("email=user@gmail.com\nallServices="); | |
| 119 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 120 } | |
| 121 | |
| 38 void SimulateValidResponseClientLogin(bool isGPlusUser) { | 122 void SimulateValidResponseClientLogin(bool isGPlusUser) { |
| 39 // Simulate the correct ClientLogin response. | 123 // Simulate the correct ClientLogin response. |
| 40 std::string respons_string = isGPlusUser ? | 124 std::string respons_string = isGPlusUser ? |
| 41 "email=user@gmail.com\nallServices=googleme" : | 125 "email=user@gmail.com\nallServices=googleme" : |
| 42 "email=user@gmail.com\nallServices="; | 126 "email=user@gmail.com\nallServices="; |
| 43 TestURLFetcher* fetcher = factory_.GetFetcherByID(0); | 127 TestURLFetcher* fetcher = factory_.GetFetcherByID(0); |
| 44 DCHECK(fetcher); | 128 DCHECK(fetcher); |
| 45 DCHECK(fetcher->delegate()); | 129 DCHECK(fetcher->delegate()); |
| 46 | 130 |
| 47 fetcher->set_url(GURL(GaiaUrls::GetInstance()->client_login_url())); | 131 fetcher->set_url(GURL(GaiaUrls::GetInstance()->client_login_url())); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 // Should go into token service and stop. | 168 // Should go into token service and stop. |
| 85 EXPECT_EQ(1U, google_login_success_.size()); | 169 EXPECT_EQ(1U, google_login_success_.size()); |
| 86 EXPECT_EQ(0U, google_login_failure_.size()); | 170 EXPECT_EQ(0U, google_login_failure_.size()); |
| 87 | 171 |
| 88 // Should persist across resets. | 172 // Should persist across resets. |
| 89 manager_.reset(new SigninManager()); | 173 manager_.reset(new SigninManager()); |
| 90 manager_->Initialize(profile_.get()); | 174 manager_->Initialize(profile_.get()); |
| 91 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername()); | 175 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername()); |
| 92 } | 176 } |
| 93 | 177 |
| 178 TEST_F(SigninManagerTest, SignInWithCredentials) { | |
| 179 manager_->Initialize(profile_.get()); | |
| 180 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | |
| 181 | |
| 182 manager_->StartSignInWithCredentials("username", "password"); | |
| 183 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | |
| 184 | |
| 185 SimulateValidResponseSignInWithCredentials(); | |
| 186 EXPECT_FALSE(manager_->GetAuthenticatedUsername().empty()); | |
| 187 | |
| 188 // Should go into token service and stop. | |
| 189 EXPECT_EQ(1U, google_login_success_.size()); | |
|
Andrew T Wilson (Slow)
2012/02/27 19:00:10
Is it difficult to validate that the correct passw
Roger Tawa OOO till Jul 10th
2012/02/29 21:02:54
The password is cleared, so not sure what you mean
Andrew T Wilson (Slow)
2012/03/02 19:22:33
I meant that all we're doing is detecting that LOG
Roger Tawa OOO till Jul 10th
2012/03/02 20:10:58
Its not easy, probably why it was not done for the
| |
| 190 EXPECT_EQ(0U, google_login_failure_.size()); | |
| 191 | |
| 192 // Should persist across resets. | |
| 193 manager_.reset(new SigninManager()); | |
| 194 manager_->Initialize(profile_.get()); | |
| 195 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername()); | |
|
Andrew T Wilson (Slow)
2012/02/27 19:00:10
Why does the authenticated username not match the
Roger Tawa OOO till Jul 10th
2012/02/29 21:02:54
The test passed because SimulateValidResponseSignI
| |
| 196 } | |
| 197 | |
| 94 TEST_F(SigninManagerTest, SignInClientLoginNoGPlus) { | 198 TEST_F(SigninManagerTest, SignInClientLoginNoGPlus) { |
| 95 manager_->Initialize(profile_.get()); | 199 manager_->Initialize(profile_.get()); |
| 96 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 200 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| 97 | 201 |
| 98 manager_->StartSignIn("username", "password", "", ""); | 202 manager_->StartSignIn("username", "password", "", ""); |
| 99 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 203 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| 100 | 204 |
| 101 SimulateValidResponseClientLogin(false); | 205 SimulateValidResponseClientLogin(false); |
| 102 EXPECT_FALSE(manager_->GetAuthenticatedUsername().empty()); | 206 EXPECT_FALSE(manager_->GetAuthenticatedUsername().empty()); |
| 103 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(prefs::kIsGooglePlusUser)); | 207 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(prefs::kIsGooglePlusUser)); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 | 323 |
| 220 TEST_F(SigninManagerTest, SignOutMidConnect) { | 324 TEST_F(SigninManagerTest, SignOutMidConnect) { |
| 221 manager_->Initialize(profile_.get()); | 325 manager_->Initialize(profile_.get()); |
| 222 manager_->StartSignIn("username", "password", "", ""); | 326 manager_->StartSignIn("username", "password", "", ""); |
| 223 manager_->SignOut(); | 327 manager_->SignOut(); |
| 224 EXPECT_EQ(0U, google_login_success_.size()); | 328 EXPECT_EQ(0U, google_login_success_.size()); |
| 225 EXPECT_EQ(0U, google_login_failure_.size()); | 329 EXPECT_EQ(0U, google_login_failure_.size()); |
| 226 | 330 |
| 227 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 331 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
| 228 } | 332 } |
| OLD | NEW |