Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: chrome/browser/chromeos/login/oauth2_browsertest.cc

Issue 148463004: Perform /ListAccounts check before session merge to see if there is a need for session merge at all. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "base/message_loop/message_loop.h" 5 #include "base/message_loop/message_loop.h"
6 #include "base/prefs/pref_service.h" 6 #include "base/prefs/pref_service.h"
7 #include "chrome/browser/browser_process.h" 7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/chromeos/login/oauth2_login_manager.h" 9 #include "chrome/browser/chromeos/login/oauth2_login_manager.h"
10 #include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h" 10 #include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; 42 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie";
43 const char kTestSession2SIDCookie[] = "fake-session2-SID-cookie"; 43 const char kTestSession2SIDCookie[] = "fake-session2-SID-cookie";
44 const char kTestSession2LSIDCookie[] = "fake-session2-LSID-cookie"; 44 const char kTestSession2LSIDCookie[] = "fake-session2-LSID-cookie";
45 const char kTestUserinfoToken[] = "fake-userinfo-token"; 45 const char kTestUserinfoToken[] = "fake-userinfo-token";
46 const char kTestLoginToken[] = "fake-login-token"; 46 const char kTestLoginToken[] = "fake-login-token";
47 const char kTestSyncToken[] = "fake-sync-token"; 47 const char kTestSyncToken[] = "fake-sync-token";
48 const char kTestAuthLoginToken[] = "fake-oauthlogin-token"; 48 const char kTestAuthLoginToken[] = "fake-oauthlogin-token";
49 49
50 } // namespace 50 } // namespace
51 51
52 class OAuth2LoginManagerStateWaiter : public OAuth2LoginManager::Observer {
53 public:
54 explicit OAuth2LoginManagerStateWaiter(Profile* profile)
55 : profile_(profile),
56 waiting_for_state_(false),
57 final_state_(OAuth2LoginManager::SESSION_RESTORE_NOT_STARTED) {
58 }
59
60 void WaitForStates(
61 const std::set<OAuth2LoginManager::SessionRestoreState>& states) {
62 DCHECK(!waiting_for_state_);
63 OAuth2LoginManager* login_manager =
64 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile_);
65 states_ = states;
66 if (states_.find(login_manager->state()) != states_.end()) {
67 final_state_ = login_manager->state();
68 return;
69 }
70
71 waiting_for_state_ = true;
72 login_manager->AddObserver(this);
73 runner_ = new content::MessageLoopRunner;
74 runner_->Run();
75 login_manager->RemoveObserver(this);
76 }
77
78 OAuth2LoginManager::SessionRestoreState final_state() { return final_state_; }
79
80 private:
81 // OAuth2LoginManager::Observer overrides.
82 virtual void OnSessionRestoreStateChanged(
83 Profile* user_profile,
84 OAuth2LoginManager::SessionRestoreState state) OVERRIDE {
85 if (!waiting_for_state_)
86 return;
87
88 if (states_.find(state) == states_.end())
89 return;
90
91 final_state_ = state;
92 waiting_for_state_ = false;
93 runner_->Quit();
94 }
95
96 Profile* profile_;
97 std::set<OAuth2LoginManager::SessionRestoreState> states_;
98 bool waiting_for_state_;
99 OAuth2LoginManager::SessionRestoreState final_state_;
100 scoped_refptr<content::MessageLoopRunner> runner_;
101
102 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManagerStateWaiter);
103 };
104
52 class OAuth2Test : public OobeBaseTest { 105 class OAuth2Test : public OobeBaseTest {
53 protected: 106 protected:
54 OAuth2Test() {} 107 OAuth2Test() {}
55 108
56 virtual void SetUpOnMainThread() OVERRIDE { 109 virtual void SetUpOnMainThread() OVERRIDE {
57 OobeBaseTest::SetUpOnMainThread(); 110 OobeBaseTest::SetUpOnMainThread();
58 } 111 }
59 112
60 void SetupGaiaServerForNewAccount() { 113 void SetupGaiaServerForNewAccount() {
61 FakeGaia::MergeSessionParams params; 114 FakeGaia::MergeSessionParams params;
62 params.auth_sid_cookie = kTestAuthSIDCookie; 115 params.auth_sid_cookie = kTestAuthSIDCookie;
63 params.auth_lsid_cookie = kTestAuthLSIDCookie; 116 params.auth_lsid_cookie = kTestAuthLSIDCookie;
64 params.auth_code = kTestAuthCode; 117 params.auth_code = kTestAuthCode;
65 params.refresh_token = kTestRefreshToken; 118 params.refresh_token = kTestRefreshToken;
66 params.access_token = kTestAuthLoginAccessToken; 119 params.access_token = kTestAuthLoginAccessToken;
67 params.gaia_uber_token = kTestGaiaUberToken; 120 params.gaia_uber_token = kTestGaiaUberToken;
68 params.session_sid_cookie = kTestSessionSIDCookie; 121 params.session_sid_cookie = kTestSessionSIDCookie;
69 params.session_lsid_cookie = kTestSessionLSIDCookie; 122 params.session_lsid_cookie = kTestSessionLSIDCookie;
70 fake_gaia_.SetMergeSessionParams(params); 123 fake_gaia_.SetMergeSessionParams(params);
71 SetupGaiaServerWithAccessTokens(); 124 SetupGaiaServerWithAccessTokens();
72 } 125 }
73 126
74 void SetupGaiaServerForExistingAccount() { 127 void SetupGaiaServerForUnexpiredAccount() {
128 FakeGaia::MergeSessionParams params;
129 params.email = kTestAccountId;
130 fake_gaia_.SetMergeSessionParams(params);
131 SetupGaiaServerWithAccessTokens();
132 }
133
134 void SetupGaiaServerForExpiredAccount() {
75 FakeGaia::MergeSessionParams params; 135 FakeGaia::MergeSessionParams params;
76 params.gaia_uber_token = kTestGaiaUberToken; 136 params.gaia_uber_token = kTestGaiaUberToken;
77 params.session_sid_cookie = kTestSession2SIDCookie; 137 params.session_sid_cookie = kTestSession2SIDCookie;
78 params.session_lsid_cookie = kTestSession2LSIDCookie; 138 params.session_lsid_cookie = kTestSession2LSIDCookie;
79 fake_gaia_.SetMergeSessionParams(params); 139 fake_gaia_.SetMergeSessionParams(params);
80 SetupGaiaServerWithAccessTokens(); 140 SetupGaiaServerWithAccessTokens();
81 } 141 }
82 142
143 void LoginAsExistingUser(Profile** user_profile) {
144 content::WindowedNotificationObserver(
145 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
146 content::NotificationService::AllSources()).Wait();
147
148 JsExpect("!!document.querySelector('#account-picker')");
149 JsExpect("!!document.querySelector('#pod-row')");
150
151 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
152 User::OAUTH2_TOKEN_STATUS_VALID);
153
154 EXPECT_TRUE(TryToLogin(kTestAccountId, kTestAccountPassword));
155 Profile* profile = ProfileManager::GetPrimaryUserProfile();
156
157 // Wait for the session merge to finish.
158 std::set<OAuth2LoginManager::SessionRestoreState> states;
159 states.insert(OAuth2LoginManager::SESSION_RESTORE_DONE);
160 states.insert(OAuth2LoginManager::SESSION_RESTORE_FAILED);
161 states.insert(OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED);
162 OAuth2LoginManagerStateWaiter merge_session_waiter(profile);
163 merge_session_waiter.WaitForStates(states);
164 EXPECT_EQ(merge_session_waiter.final_state(),
165 OAuth2LoginManager::SESSION_RESTORE_DONE);
166
167 // Check for existance of refresh token.
168 ProfileOAuth2TokenService* token_service =
169 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
170 EXPECT_TRUE(token_service->RefreshTokenIsAvailable(kTestAccountId));
171
172 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
173 User::OAUTH2_TOKEN_STATUS_VALID);
174
175 *user_profile = profile;
176 }
177
83 bool TryToLogin(const std::string& username, 178 bool TryToLogin(const std::string& username,
84 const std::string& password) { 179 const std::string& password) {
85 if (!AddUserTosession(username, password)) 180 if (!AddUserToSession(username, password))
86 return false; 181 return false;
87 182
88 if (const User* active_user = UserManager::Get()->GetActiveUser()) 183 if (const User* active_user = UserManager::Get()->GetActiveUser())
89 return active_user->email() == username; 184 return active_user->email() == username;
90 185
91 return false; 186 return false;
92 } 187 }
93 188
94 User::OAuthTokenStatus GetOAuthStatusFromLocalState( 189 User::OAuthTokenStatus GetOAuthStatusFromLocalState(
95 const std::string& user_id) const { 190 const std::string& user_id) const {
96 PrefService* local_state = g_browser_process->local_state(); 191 PrefService* local_state = g_browser_process->local_state();
97 const base::DictionaryValue* prefs_oauth_status = 192 const base::DictionaryValue* prefs_oauth_status =
98 local_state->GetDictionary("OAuthTokenStatus"); 193 local_state->GetDictionary("OAuthTokenStatus");
99 int oauth_token_status = User::OAUTH_TOKEN_STATUS_UNKNOWN; 194 int oauth_token_status = User::OAUTH_TOKEN_STATUS_UNKNOWN;
100 if (prefs_oauth_status && 195 if (prefs_oauth_status &&
101 prefs_oauth_status->GetIntegerWithoutPathExpansion( 196 prefs_oauth_status->GetIntegerWithoutPathExpansion(
102 user_id, &oauth_token_status)) { 197 user_id, &oauth_token_status)) {
103 User::OAuthTokenStatus result = 198 User::OAuthTokenStatus result =
104 static_cast<User::OAuthTokenStatus>(oauth_token_status); 199 static_cast<User::OAuthTokenStatus>(oauth_token_status);
105 return result; 200 return result;
106 } 201 }
107 return User::OAUTH_TOKEN_STATUS_UNKNOWN; 202 return User::OAUTH_TOKEN_STATUS_UNKNOWN;
108 } 203 }
109 204
110 private: 205 private:
111 bool AddUserTosession(const std::string& username, 206 bool AddUserToSession(const std::string& username,
112 const std::string& password) { 207 const std::string& password) {
113 ExistingUserController* controller = 208 ExistingUserController* controller =
114 ExistingUserController::current_controller(); 209 ExistingUserController::current_controller();
115 if (!controller) { 210 if (!controller) {
116 ADD_FAILURE(); 211 ADD_FAILURE();
117 return false; 212 return false;
118 } 213 }
119 214
120 controller->Login(UserContext(username, password, std::string())); 215 controller->Login(UserContext(username, password, std::string()));
121 content::WindowedNotificationObserver( 216 content::WindowedNotificationObserver(
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 runner_->Quit(); 323 runner_->Quit();
229 } 324 }
230 325
231 scoped_refptr<net::URLRequestContextGetter> context_; 326 scoped_refptr<net::URLRequestContextGetter> context_;
232 net::CookieList cookie_list_; 327 net::CookieList cookie_list_;
233 scoped_refptr<content::MessageLoopRunner> runner_; 328 scoped_refptr<content::MessageLoopRunner> runner_;
234 329
235 DISALLOW_COPY_AND_ASSIGN(CookieReader); 330 DISALLOW_COPY_AND_ASSIGN(CookieReader);
236 }; 331 };
237 332
238 class OAuth2LoginManagerStateWaiter : public OAuth2LoginManager::Observer {
239 public:
240 explicit OAuth2LoginManagerStateWaiter(Profile* profile)
241 : profile_(profile),
242 waiting_for_state_(false),
243 final_state_(OAuth2LoginManager::SESSION_RESTORE_NOT_STARTED) {
244 }
245
246 void WaitForStates(
247 const std::set<OAuth2LoginManager::SessionRestoreState>& states) {
248 DCHECK(!waiting_for_state_);
249 OAuth2LoginManager* login_manager =
250 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile_);
251 states_ = states;
252 if (states_.find(login_manager->state()) != states_.end()) {
253 final_state_ = login_manager->state();
254 return;
255 }
256
257 waiting_for_state_ = true;
258 login_manager->AddObserver(this);
259 runner_ = new content::MessageLoopRunner;
260 runner_->Run();
261 login_manager->RemoveObserver(this);
262 }
263
264 OAuth2LoginManager::SessionRestoreState final_state() { return final_state_; }
265
266 private:
267 // OAuth2LoginManager::Observer overrides.
268 virtual void OnSessionRestoreStateChanged(
269 Profile* user_profile,
270 OAuth2LoginManager::SessionRestoreState state) OVERRIDE {
271 if (!waiting_for_state_)
272 return;
273
274 if (states_.find(state) == states_.end())
275 return;
276
277 final_state_ = state;
278 waiting_for_state_ = false;
279 runner_->Quit();
280 }
281
282 Profile* profile_;
283 std::set<OAuth2LoginManager::SessionRestoreState> states_;
284 bool waiting_for_state_;
285 OAuth2LoginManager::SessionRestoreState final_state_;
286 scoped_refptr<content::MessageLoopRunner> runner_;
287
288 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManagerStateWaiter);
289 };
290
291 // PRE_MergeSession is testing merge session for a new profile. 333 // PRE_MergeSession is testing merge session for a new profile.
292 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_PRE_MergeSession) { 334 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_PRE_PRE_MergeSession) {
293 SetupGaiaServerForNewAccount(); 335 SetupGaiaServerForNewAccount();
294 SimulateNetworkOnline(); 336 SimulateNetworkOnline();
295 chromeos::WizardController::SkipPostLoginScreensForTesting(); 337 chromeos::WizardController::SkipPostLoginScreensForTesting();
296 chromeos::WizardController* wizard_controller = 338 chromeos::WizardController* wizard_controller =
297 chromeos::WizardController::default_controller(); 339 chromeos::WizardController::default_controller();
298 wizard_controller->SkipToLoginForTesting(LoginScreenContext()); 340 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
299 341
300 content::WindowedNotificationObserver( 342 content::WindowedNotificationObserver(
301 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, 343 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
302 content::NotificationService::AllSources()).Wait(); 344 content::NotificationService::AllSources()).Wait();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId), 376 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
335 User::OAUTH2_TOKEN_STATUS_VALID); 377 User::OAUTH2_TOKEN_STATUS_VALID);
336 378
337 scoped_refptr<CookieReader> cookie_reader(new CookieReader()); 379 scoped_refptr<CookieReader> cookie_reader(new CookieReader());
338 cookie_reader->ReadCookies(profile); 380 cookie_reader->ReadCookies(profile);
339 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSessionSIDCookie); 381 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSessionSIDCookie);
340 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSessionLSIDCookie); 382 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSessionLSIDCookie);
341 } 383 }
342 384
343 // MergeSession test is running merge session process for an existing profile 385 // MergeSession test is running merge session process for an existing profile
386 // that was generated in PRE_PRE_PRE_MergeSession test. In this test, we
387 // are not running /MergeSession process since the /ListAccounts call confirms
388 // that the session is not stale.
389 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_PRE_MergeSession) {
390 SetupGaiaServerForUnexpiredAccount();
391 SimulateNetworkOnline();
392
393 Profile* profile = NULL;
394 LoginAsExistingUser(&profile);
395
396 scoped_refptr<CookieReader> cookie_reader(new CookieReader());
397 cookie_reader->ReadCookies(profile);
398 // These are still cookie values form the initial session since
399 // /ListAccounts
400 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSessionSIDCookie);
401 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSessionLSIDCookie);
402 }
403
404 // MergeSession test is running merge session process for an existing profile
344 // that was generated in PRE_PRE_MergeSession test. 405 // that was generated in PRE_PRE_MergeSession test.
345 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_MergeSession) { 406 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_MergeSession) {
346 SetupGaiaServerForExistingAccount(); 407 SetupGaiaServerForExpiredAccount();
347 SimulateNetworkOnline(); 408 SimulateNetworkOnline();
348 409
349 content::WindowedNotificationObserver( 410 Profile* profile = NULL;
350 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, 411 LoginAsExistingUser(&profile);
351 content::NotificationService::AllSources()).Wait();
352
353 JsExpect("!!document.querySelector('#account-picker')");
354 JsExpect("!!document.querySelector('#pod-row')");
355
356 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
357 User::OAUTH2_TOKEN_STATUS_VALID);
358
359 EXPECT_TRUE(TryToLogin(kTestAccountId, kTestAccountPassword));
360 Profile* profile = ProfileManager::GetPrimaryUserProfile();
361
362 // Wait for the session merge to finish.
363 std::set<OAuth2LoginManager::SessionRestoreState> states;
364 states.insert(OAuth2LoginManager::SESSION_RESTORE_DONE);
365 states.insert(OAuth2LoginManager::SESSION_RESTORE_FAILED);
366 states.insert(OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED);
367 OAuth2LoginManagerStateWaiter merge_session_waiter(profile);
368 merge_session_waiter.WaitForStates(states);
369 EXPECT_EQ(merge_session_waiter.final_state(),
370 OAuth2LoginManager::SESSION_RESTORE_DONE);
371
372 // Check for existance of refresh token.
373 ProfileOAuth2TokenService* token_service =
374 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
375 EXPECT_TRUE(token_service->RefreshTokenIsAvailable(kTestAccountId));
376
377 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
378 User::OAUTH2_TOKEN_STATUS_VALID);
379 412
380 scoped_refptr<CookieReader> cookie_reader(new CookieReader()); 413 scoped_refptr<CookieReader> cookie_reader(new CookieReader());
381 cookie_reader->ReadCookies(profile); 414 cookie_reader->ReadCookies(profile);
415 // These should be cookie values that we generated by calling /MergeSession,
416 // since /ListAccounts should have tell us that the initial session cookies
417 // are stale.
382 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSession2SIDCookie); 418 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSession2SIDCookie);
383 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSession2LSIDCookie); 419 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSession2LSIDCookie);
384 } 420 }
385 421
386 // MergeSession test is attempting to merge session for an existing profile 422 // MergeSession test is attempting to merge session for an existing profile
387 // that was generated in PRE_PRE_MergeSession test. This attempt should fail 423 // that was generated in PRE_PRE_MergeSession test. This attempt should fail
388 // since FakeGaia instance isn't configured to return relevant tokens/cookies. 424 // since FakeGaia instance isn't configured to return relevant tokens/cookies.
389 IN_PROC_BROWSER_TEST_F(OAuth2Test, MergeSession) { 425 IN_PROC_BROWSER_TEST_F(OAuth2Test, MergeSession) {
390 SimulateNetworkOnline(); 426 SimulateNetworkOnline();
391 427
(...skipping 18 matching lines...) Expand all
410 ProfileManager::GetPrimaryUserProfile()); 446 ProfileManager::GetPrimaryUserProfile());
411 merge_session_waiter.WaitForStates(states); 447 merge_session_waiter.WaitForStates(states);
412 EXPECT_EQ(merge_session_waiter.final_state(), 448 EXPECT_EQ(merge_session_waiter.final_state(),
413 OAuth2LoginManager::SESSION_RESTORE_FAILED); 449 OAuth2LoginManager::SESSION_RESTORE_FAILED);
414 450
415 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId), 451 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
416 User::OAUTH2_TOKEN_STATUS_INVALID); 452 User::OAUTH2_TOKEN_STATUS_INVALID);
417 } 453 }
418 454
419 } // namespace chromeos 455 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698