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::GetCookieStoreForScheme( |
| 208 content::BrowserContext::GetDefaultStoragePartition(profile()), |
| 209 origin.scheme())->GetCookieMonster(); |
| 210 } |
| 211 |
203 net::TestURLFetcherFactory factory_; | 212 net::TestURLFetcherFactory factory_; |
204 scoped_ptr<SigninManager> manager_; | 213 scoped_ptr<SigninManager> manager_; |
205 content::TestNotificationTracker google_login_success_; | 214 content::TestNotificationTracker google_login_success_; |
206 content::TestNotificationTracker google_login_failure_; | 215 content::TestNotificationTracker google_login_failure_; |
207 std::vector<std::string> oauth_tokens_fetched_; | 216 std::vector<std::string> oauth_tokens_fetched_; |
208 scoped_ptr<TestingPrefServiceSimple> prefs_; | 217 scoped_ptr<TestingPrefServiceSimple> prefs_; |
209 std::vector<std::string> cookies_; | 218 std::vector<std::string> cookies_; |
210 }; | 219 }; |
211 | 220 |
212 // NOTE: ClientLogin's "StartSignin" is called after collecting credentials | 221 // 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()); | 291 SigninManager::OAuthTokenFetchedCallback()); |
283 | 292 |
284 ExpectSignInWithCredentialsFail(true /* requestSent */); | 293 ExpectSignInWithCredentialsFail(true /* requestSent */); |
285 } | 294 } |
286 | 295 |
287 TEST_F(SigninManagerTest, SignInWithCredentialsEmptyPasswordValidCookie) { | 296 TEST_F(SigninManagerTest, SignInWithCredentialsEmptyPasswordValidCookie) { |
288 manager_->Initialize(profile(), NULL); | 297 manager_->Initialize(profile(), NULL); |
289 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 298 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
290 | 299 |
291 // Set a valid LSID cookie in the test cookie store. | 300 // Set a valid LSID cookie in the test cookie store. |
292 scoped_refptr<net::CookieMonster> cookie_monster = | 301 GURL origin("https://accounts.google.com"); |
293 profile()->GetCookieMonster(); | 302 scoped_refptr<net::CookieMonster> cookie_monster = GetCookieMonster(origin); |
294 net::CookieOptions options; | 303 net::CookieOptions options; |
295 options.set_include_httponly(); | 304 options.set_include_httponly(); |
296 cookie_monster->SetCookieWithOptionsAsync( | 305 cookie_monster->SetCookieWithOptionsAsync( |
297 GURL("https://accounts.google.com"), | 306 origin, |
298 "LSID=1234; secure; httponly", options, | 307 "LSID=1234; secure; httponly", options, |
299 net::CookieMonster::SetCookiesCallback()); | 308 net::CookieMonster::SetCookiesCallback()); |
300 | 309 |
301 // Since the password is empty, will verify the gaia cookies first. | 310 // Since the password is empty, will verify the gaia cookies first. |
302 manager_->StartSignInWithCredentials( | 311 manager_->StartSignInWithCredentials( |
303 "0", | 312 "0", |
304 "user@gmail.com", | 313 "user@gmail.com", |
305 std::string(), | 314 std::string(), |
306 SigninManager::OAuthTokenFetchedCallback()); | 315 SigninManager::OAuthTokenFetchedCallback()); |
307 | 316 |
308 base::RunLoop().RunUntilIdle(); | 317 base::RunLoop().RunUntilIdle(); |
309 | 318 |
(...skipping 17 matching lines...) Expand all Loading... |
327 // Since the test cookie store is empty, verification should fail and throws | 336 // Since the test cookie store is empty, verification should fail and throws |
328 // a login error. | 337 // a login error. |
329 ExpectSignInWithCredentialsFail(false /* requestSent */); | 338 ExpectSignInWithCredentialsFail(false /* requestSent */); |
330 } | 339 } |
331 | 340 |
332 TEST_F(SigninManagerTest, SignInWithCredentialsEmptyPasswordInValidCookie) { | 341 TEST_F(SigninManagerTest, SignInWithCredentialsEmptyPasswordInValidCookie) { |
333 manager_->Initialize(profile(), NULL); | 342 manager_->Initialize(profile(), NULL); |
334 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); | 343 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty()); |
335 | 344 |
336 // Set an invalid LSID cookie in the test cookie store. | 345 // Set an invalid LSID cookie in the test cookie store. |
337 scoped_refptr<net::CookieMonster> cookie_monster = | 346 GURL origin("https://accounts.google.com"); |
338 profile()->GetCookieMonster(); | 347 scoped_refptr<net::CookieMonster> cookie_monster = GetCookieMonster(origin); |
339 net::CookieOptions options; | 348 net::CookieOptions options; |
340 options.set_include_httponly(); | 349 options.set_include_httponly(); |
341 cookie_monster->SetCookieWithOptionsAsync( | 350 cookie_monster->SetCookieWithOptionsAsync( |
342 GURL("https://accounts.google.com"), | 351 origin, |
343 "LSID=1234; domain=google.com; secure; httponly", options, | 352 "LSID=1234; domain=google.com; secure; httponly", options, |
344 net::CookieMonster::SetCookiesCallback()); | 353 net::CookieMonster::SetCookiesCallback()); |
345 | 354 |
346 // Since the password is empty, must verify the gaia cookies first. | 355 // Since the password is empty, must verify the gaia cookies first. |
347 manager_->StartSignInWithCredentials( | 356 manager_->StartSignInWithCredentials( |
348 "0", | 357 "0", |
349 "user@gmail.com", | 358 "user@gmail.com", |
350 std::string(), | 359 std::string(), |
351 SigninManager::OAuthTokenFetchedCallback()); | 360 SigninManager::OAuthTokenFetchedCallback()); |
352 | 361 |
353 base::RunLoop().RunUntilIdle(); | 362 base::RunLoop().RunUntilIdle(); |
354 | 363 |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 EXPECT_EQ("", manager_->GetAuthenticatedUsername()); | 632 EXPECT_EQ("", manager_->GetAuthenticatedUsername()); |
624 EXPECT_EQ(0u, google_login_success_.size()); | 633 EXPECT_EQ(0u, google_login_success_.size()); |
625 | 634 |
626 manager_->OnExternalSigninCompleted("external@example.com"); | 635 manager_->OnExternalSigninCompleted("external@example.com"); |
627 EXPECT_EQ(1u, google_login_success_.size()); | 636 EXPECT_EQ(1u, google_login_success_.size()); |
628 EXPECT_EQ(0u, google_login_failure_.size()); | 637 EXPECT_EQ(0u, google_login_failure_.size()); |
629 EXPECT_EQ("external@example.com", | 638 EXPECT_EQ("external@example.com", |
630 profile()->GetPrefs()->GetString(prefs::kGoogleServicesUsername)); | 639 profile()->GetPrefs()->GetString(prefs::kGoogleServicesUsername)); |
631 EXPECT_EQ("external@example.com", manager_->GetAuthenticatedUsername()); | 640 EXPECT_EQ("external@example.com", manager_->GetAuthenticatedUsername()); |
632 } | 641 } |
OLD | NEW |