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/token_service.h" | 18 #include "chrome/browser/signin/token_service.h" |
19 #include "chrome/browser/signin/token_service_unittest.h" | 19 #include "chrome/browser/signin/token_service_unittest.h" |
20 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
21 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
22 #include "chrome/test/base/testing_browser_process.h" | 22 #include "chrome/test/base/testing_browser_process.h" |
23 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
24 #include "components/webdata/encryptor/encryptor.h" | 24 #include "components/webdata/encryptor/encryptor.h" |
25 #include "content/public/browser/child_process_security_policy.h" | 25 #include "content/public/browser/child_process_security_policy.h" |
| 26 #include "content/public/browser/storage_partition.h" |
26 #include "content/public/test/test_browser_thread_bundle.h" | 27 #include "content/public/test/test_browser_thread_bundle.h" |
| 28 #include "content/public/test/test_utils.h" |
27 #include "google_apis/gaia/gaia_constants.h" | 29 #include "google_apis/gaia/gaia_constants.h" |
28 #include "google_apis/gaia/gaia_urls.h" | 30 #include "google_apis/gaia/gaia_urls.h" |
29 #include "net/cookies/cookie_monster.h" | 31 #include "net/cookies/cookie_monster.h" |
30 #include "net/url_request/test_url_fetcher_factory.h" | 32 #include "net/url_request/test_url_fetcher_factory.h" |
31 #include "net/url_request/url_request.h" | 33 #include "net/url_request/url_request.h" |
32 #include "net/url_request/url_request_context_getter.h" | 34 #include "net/url_request/url_request_context_getter.h" |
33 #include "net/url_request/url_request_status.h" | 35 #include "net/url_request/url_request_status.h" |
34 | 36 |
35 #include "testing/gmock/include/gmock/gmock.h" | 37 #include "testing/gmock/include/gmock/gmock.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 void CompleteSigninCallback(const std::string& oauth_token) { | 195 void CompleteSigninCallback(const std::string& oauth_token) { |
194 oauth_tokens_fetched_.push_back(oauth_token); | 196 oauth_tokens_fetched_.push_back(oauth_token); |
195 manager_->CompletePendingSignin(); | 197 manager_->CompletePendingSignin(); |
196 } | 198 } |
197 | 199 |
198 void CancelSigninCallback(const std::string& oauth_token) { | 200 void CancelSigninCallback(const std::string& oauth_token) { |
199 oauth_tokens_fetched_.push_back(oauth_token); | 201 oauth_tokens_fetched_.push_back(oauth_token); |
200 manager_->SignOut(); | 202 manager_->SignOut(); |
201 } | 203 } |
202 | 204 |
| 205 net::CookieMonster* GetCookieMonster(const GURL& origin) { |
| 206 // Since it's a unittest, assume default StoragePartition. |
| 207 return content::BrowserContext::GetDefaultStoragePartition(profile())-> |
| 208 GetCookieStoreForScheme(origin.scheme())->GetCookieMonster(); |
| 209 } |
| 210 |
203 net::TestURLFetcherFactory factory_; | 211 net::TestURLFetcherFactory factory_; |
204 scoped_ptr<SigninManager> manager_; | 212 scoped_ptr<SigninManager> manager_; |
205 content::TestNotificationTracker google_login_success_; | 213 content::TestNotificationTracker google_login_success_; |
206 content::TestNotificationTracker google_login_failure_; | 214 content::TestNotificationTracker google_login_failure_; |
207 std::vector<std::string> oauth_tokens_fetched_; | 215 std::vector<std::string> oauth_tokens_fetched_; |
208 scoped_ptr<TestingPrefServiceSimple> prefs_; | 216 scoped_ptr<TestingPrefServiceSimple> prefs_; |
209 std::vector<std::string> cookies_; | 217 std::vector<std::string> cookies_; |
210 }; | 218 }; |
211 | 219 |
212 // NOTE: ClientLogin's "StartSignin" is called after collecting credentials | 220 // NOTE: ClientLogin's "StartSignin" is called after collecting credentials |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 SigninManager::OAuthTokenFetchedCallback()); | 290 SigninManager::OAuthTokenFetchedCallback()); |
283 | 291 |
284 ExpectSignInWithCredentialsFail(true /* requestSent */); | 292 ExpectSignInWithCredentialsFail(true /* requestSent */); |
285 } | 293 } |
286 | 294 |
287 TEST_F(SigninManagerTest, SignInWithCredentialsEmptyPasswordValidCookie) { | 295 TEST_F(SigninManagerTest, SignInWithCredentialsEmptyPasswordValidCookie) { |
288 manager_->Initialize(profile(), NULL); | 296 manager_->Initialize(profile(), NULL); |
289 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 297 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
290 | 298 |
291 // Set a valid LSID cookie in the test cookie store. | 299 // Set a valid LSID cookie in the test cookie store. |
292 scoped_refptr<net::CookieMonster> cookie_monster = | 300 GURL origin("https://accounts.google.com"); |
293 profile()->GetCookieMonster(); | 301 scoped_refptr<net::CookieMonster> cookie_monster = GetCookieMonster(origin); |
294 net::CookieOptions options; | 302 net::CookieOptions options; |
295 options.set_include_httponly(); | 303 options.set_include_httponly(); |
296 cookie_monster->SetCookieWithOptionsAsync( | 304 cookie_monster->SetCookieWithOptionsAsync( |
297 GURL("https://accounts.google.com"), | 305 origin, |
298 "LSID=1234; secure; httponly", options, | 306 "LSID=1234; secure; httponly", options, |
299 net::CookieMonster::SetCookiesCallback()); | 307 net::CookieMonster::SetCookiesCallback()); |
300 | 308 |
301 // Since the password is empty, will verify the gaia cookies first. | 309 // Since the password is empty, will verify the gaia cookies first. |
302 manager_->StartSignInWithCredentials( | 310 manager_->StartSignInWithCredentials( |
303 "0", | 311 "0", |
304 "user@gmail.com", | 312 "user@gmail.com", |
305 std::string(), | 313 std::string(), |
306 SigninManager::OAuthTokenFetchedCallback()); | 314 SigninManager::OAuthTokenFetchedCallback()); |
307 | 315 |
308 base::RunLoop().RunUntilIdle(); | 316 base::RunLoop().RunUntilIdle(); |
309 | 317 |
(...skipping 17 matching lines...) Expand all Loading... |
327 // Since the test cookie store is empty, verification should fail and throws | 335 // Since the test cookie store is empty, verification should fail and throws |
328 // a login error. | 336 // a login error. |
329 ExpectSignInWithCredentialsFail(false /* requestSent */); | 337 ExpectSignInWithCredentialsFail(false /* requestSent */); |
330 } | 338 } |
331 | 339 |
332 TEST_F(SigninManagerTest, SignInWithCredentialsEmptyPasswordInValidCookie) { | 340 TEST_F(SigninManagerTest, SignInWithCredentialsEmptyPasswordInValidCookie) { |
333 manager_->Initialize(profile(), NULL); | 341 manager_->Initialize(profile(), NULL); |
334 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 342 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
335 | 343 |
336 // Set an invalid LSID cookie in the test cookie store. | 344 // Set an invalid LSID cookie in the test cookie store. |
337 scoped_refptr<net::CookieMonster> cookie_monster = | 345 GURL origin("https://accounts.google.com"); |
338 profile()->GetCookieMonster(); | 346 scoped_refptr<net::CookieMonster> cookie_monster = GetCookieMonster(origin); |
339 net::CookieOptions options; | 347 net::CookieOptions options; |
340 options.set_include_httponly(); | 348 options.set_include_httponly(); |
341 cookie_monster->SetCookieWithOptionsAsync( | 349 cookie_monster->SetCookieWithOptionsAsync( |
342 GURL("https://accounts.google.com"), | 350 origin, |
343 "LSID=1234; domain=google.com; secure; httponly", options, | 351 "LSID=1234; domain=google.com; secure; httponly", options, |
344 net::CookieMonster::SetCookiesCallback()); | 352 net::CookieMonster::SetCookiesCallback()); |
345 | 353 |
346 // Since the password is empty, must verify the gaia cookies first. | 354 // Since the password is empty, must verify the gaia cookies first. |
347 manager_->StartSignInWithCredentials( | 355 manager_->StartSignInWithCredentials( |
348 "0", | 356 "0", |
349 "user@gmail.com", | 357 "user@gmail.com", |
350 std::string(), | 358 std::string(), |
351 SigninManager::OAuthTokenFetchedCallback()); | 359 SigninManager::OAuthTokenFetchedCallback()); |
352 | 360 |
353 base::RunLoop().RunUntilIdle(); | 361 base::RunLoop().RunUntilIdle(); |
354 | 362 |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 EXPECT_EQ("", manager_->GetAuthenticatedUsername()); | 631 EXPECT_EQ("", manager_->GetAuthenticatedUsername()); |
624 EXPECT_EQ(0u, google_login_success_.size()); | 632 EXPECT_EQ(0u, google_login_success_.size()); |
625 | 633 |
626 manager_->OnExternalSigninCompleted("external@example.com"); | 634 manager_->OnExternalSigninCompleted("external@example.com"); |
627 EXPECT_EQ(1u, google_login_success_.size()); | 635 EXPECT_EQ(1u, google_login_success_.size()); |
628 EXPECT_EQ(0u, google_login_failure_.size()); | 636 EXPECT_EQ(0u, google_login_failure_.size()); |
629 EXPECT_EQ("external@example.com", | 637 EXPECT_EQ("external@example.com", |
630 profile()->GetPrefs()->GetString(prefs::kGoogleServicesUsername)); | 638 profile()->GetPrefs()->GetString(prefs::kGoogleServicesUsername)); |
631 EXPECT_EQ("external@example.com", manager_->GetAuthenticatedUsername()); | 639 EXPECT_EQ("external@example.com", manager_->GetAuthenticatedUsername()); |
632 } | 640 } |
OLD | NEW |