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

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

Issue 108663008: Additional OAuth2 tests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years 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
« no previous file with comments | « no previous file | chrome/browser/chromeos/login/saml_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
7 #include "chrome/browser/browser_process.h"
6 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/chrome_notification_types.h"
7 #include "chrome/browser/chromeos/login/oauth2_login_manager.h" 9 #include "chrome/browser/chromeos/login/oauth2_login_manager.h"
8 #include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h" 10 #include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h"
9 #include "chrome/browser/chromeos/login/oobe_base_test.h" 11 #include "chrome/browser/chromeos/login/oobe_base_test.h"
12 #include "chrome/browser/chromeos/login/user_manager.h"
10 #include "chrome/browser/chromeos/login/wizard_controller.h" 13 #include "chrome/browser/chromeos/login/wizard_controller.h"
11 #include "chrome/browser/profiles/profile_manager.h" 14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service.h"
16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
12 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" 17 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
13 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/notification_service.h"
14 #include "google_apis/gaia/gaia_constants.h" 19 #include "google_apis/gaia/gaia_constants.h"
15 #include "google_apis/gaia/gaia_urls.h" 20 #include "google_apis/gaia/gaia_urls.h"
21 #include "net/cookies/canonical_cookie.h"
22 #include "net/cookies/cookie_monster.h"
23 #include "net/cookies/cookie_store.h"
24 #include "net/url_request/url_request_context.h"
25 #include "net/url_request/url_request_context_getter.h"
16 26
17 namespace chromeos { 27 namespace chromeos {
18 28
19 namespace { 29 namespace {
20 30
21 // Email of owner account for test. 31 // Email of owner account for test.
22 const char kTestAccountId[] = "username@gmail.com"; 32 const char kTestAccountId[] = "username@gmail.com";
23 33 const char kTestRawAccountId[] = "User.Name";
34 const char kTestAccountPassword[] = "fake-password";
24 const char kTestAuthCode[] = "fake-auth-code"; 35 const char kTestAuthCode[] = "fake-auth-code";
25 const char kTestGaiaUberToken[] = "fake-uber-token"; 36 const char kTestGaiaUberToken[] = "fake-uber-token";
26 const char kTestAuthLoginAccessToken[] = "fake-access-token"; 37 const char kTestAuthLoginAccessToken[] = "fake-access-token";
27 const char kTestRefreshToken[] = "fake-refresh-token"; 38 const char kTestRefreshToken[] = "fake-refresh-token";
39 const char kTestAuthSIDCookie[] = "fake-auth-SID-cookie";
40 const char kTestAuthLSIDCookie[] = "fake-auth-LSID-cookie";
28 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; 41 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie";
29 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; 42 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie";
43 const char kTestSession2SIDCookie[] = "fake-session2-SID-cookie";
44 const char kTestSession2LSIDCookie[] = "fake-session2-LSID-cookie";
30 const char kTestUserinfoToken[] = "fake-userinfo-token"; 45 const char kTestUserinfoToken[] = "fake-userinfo-token";
31 const char kTestLoginToken[] = "fake-login-token"; 46 const char kTestLoginToken[] = "fake-login-token";
32 const char kTestSyncToken[] = "fake-sync-token"; 47 const char kTestSyncToken[] = "fake-sync-token";
33 const char kTestAuthLoginToken[] = "fake-oauthlogin-token"; 48 const char kTestAuthLoginToken[] = "fake-oauthlogin-token";
34 49
35 } // namespace 50 } // namespace
36 51
37 class OAuth2Test : public OobeBaseTest { 52 class OAuth2Test : public OobeBaseTest {
38 protected: 53 protected:
39 OAuth2Test() {} 54 OAuth2Test() {}
40 55
41 virtual void SetUpOnMainThread() OVERRIDE { 56 virtual void SetUpOnMainThread() OVERRIDE {
42 OobeBaseTest::SetUpOnMainThread(); 57 OobeBaseTest::SetUpOnMainThread();
58 }
43 59
60 void SetupGaiaServerForNewAccount() {
61 FakeGaia::MergeSessionParams params;
62 params.auth_sid_cookie = kTestAuthSIDCookie;
63 params.auth_lsid_cookie = kTestAuthLSIDCookie;
64 params.auth_code = kTestAuthCode;
65 params.refresh_token = kTestRefreshToken;
66 params.access_token = kTestAuthLoginAccessToken;
67 params.gaia_uber_token = kTestGaiaUberToken;
68 params.session_sid_cookie = kTestSessionSIDCookie;
69 params.session_lsid_cookie = kTestSessionLSIDCookie;
70 fake_gaia_.SetMergeSessionParams(params);
71 SetupGaiaServerWithAccessTokens();
72 }
73
74 void SetupGaiaServerForExistingAccount() {
75 FakeGaia::MergeSessionParams params;
76 params.gaia_uber_token = kTestGaiaUberToken;
77 params.session_sid_cookie = kTestSession2SIDCookie;
78 params.session_lsid_cookie = kTestSession2LSIDCookie;
79 fake_gaia_.SetMergeSessionParams(params);
80 SetupGaiaServerWithAccessTokens();
81 }
82
83 bool TryToLogin(const std::string& username,
84 const std::string& password) {
85 if (!AddUserTosession(username, password))
86 return false;
87
88 if (const User* active_user = UserManager::Get()->GetActiveUser())
89 return active_user->email() == username;
90
91 return false;
92 }
93
94 User::OAuthTokenStatus GetOAuthStatusFromLocalState(
95 const std::string& user_id) const {
96 PrefService* local_state = g_browser_process->local_state();
97 const DictionaryValue* prefs_oauth_status =
98 local_state->GetDictionary("OAuthTokenStatus");
99 int oauth_token_status = User::OAUTH_TOKEN_STATUS_UNKNOWN;
100 if (prefs_oauth_status &&
101 prefs_oauth_status->GetIntegerWithoutPathExpansion(
102 user_id, &oauth_token_status)) {
103 User::OAuthTokenStatus result =
104 static_cast<User::OAuthTokenStatus>(oauth_token_status);
105 return result;
106 }
107 return User::OAUTH_TOKEN_STATUS_UNKNOWN;
108 }
109
110 private:
111 bool AddUserTosession(const std::string& username,
112 const std::string& password) {
113 ExistingUserController* controller =
114 ExistingUserController::current_controller();
115 if (!controller) {
116 ADD_FAILURE();
117 return false;
118 }
119
120 controller->Login(UserContext(username, password, std::string()));
121 content::WindowedNotificationObserver(
122 chrome::NOTIFICATION_SESSION_STARTED,
123 content::NotificationService::AllSources()).Wait();
124 const UserList& logged_users = UserManager::Get()->GetLoggedInUsers();
125 for (UserList::const_iterator it = logged_users.begin();
126 it != logged_users.end(); ++it) {
127 if ((*it)->email() == username)
128 return true;
129 }
130 return false;
131 }
132
133 void SetupGaiaServerWithAccessTokens() {
44 // Configure OAuth authentication. 134 // Configure OAuth authentication.
45 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); 135 GaiaUrls* gaia_urls = GaiaUrls::GetInstance();
46 136
47 fake_gaia_.SetAuthTokens(kTestAuthCode,
48 kTestRefreshToken,
49 kTestAuthLoginAccessToken,
50 kTestGaiaUberToken,
51 kTestSessionSIDCookie,
52 kTestSessionLSIDCookie);
53 // This token satisfies the userinfo.email request from 137 // This token satisfies the userinfo.email request from
54 // DeviceOAuth2TokenService used in token validation. 138 // DeviceOAuth2TokenService used in token validation.
55 FakeGaia::AccessTokenInfo userinfo_token_info; 139 FakeGaia::AccessTokenInfo userinfo_token_info;
56 userinfo_token_info.token = kTestUserinfoToken; 140 userinfo_token_info.token = kTestUserinfoToken;
57 userinfo_token_info.scopes.insert( 141 userinfo_token_info.scopes.insert(
58 "https://www.googleapis.com/auth/userinfo.email"); 142 "https://www.googleapis.com/auth/userinfo.email");
59 userinfo_token_info.audience = gaia_urls->oauth2_chrome_client_id(); 143 userinfo_token_info.audience = gaia_urls->oauth2_chrome_client_id();
60 userinfo_token_info.email = kTestAccountId; 144 userinfo_token_info.email = kTestAccountId;
61 fake_gaia_.IssueOAuthToken(kTestRefreshToken, userinfo_token_info); 145 fake_gaia_.IssueOAuthToken(kTestRefreshToken, userinfo_token_info);
62 146
(...skipping 19 matching lines...) Expand all
82 sync_token_info.audience = gaia_urls->oauth2_chrome_client_id(); 166 sync_token_info.audience = gaia_urls->oauth2_chrome_client_id();
83 fake_gaia_.IssueOAuthToken(kTestRefreshToken, sync_token_info); 167 fake_gaia_.IssueOAuthToken(kTestRefreshToken, sync_token_info);
84 168
85 FakeGaia::AccessTokenInfo auth_login_token_info; 169 FakeGaia::AccessTokenInfo auth_login_token_info;
86 auth_login_token_info.token = kTestAuthLoginToken; 170 auth_login_token_info.token = kTestAuthLoginToken;
87 auth_login_token_info.scopes.insert(gaia_urls->oauth1_login_scope()); 171 auth_login_token_info.scopes.insert(gaia_urls->oauth1_login_scope());
88 auth_login_token_info.audience = gaia_urls->oauth2_chrome_client_id(); 172 auth_login_token_info.audience = gaia_urls->oauth2_chrome_client_id();
89 fake_gaia_.IssueOAuthToken(kTestRefreshToken, auth_login_token_info); 173 fake_gaia_.IssueOAuthToken(kTestRefreshToken, auth_login_token_info);
90 } 174 }
91 175
176 DISALLOW_COPY_AND_ASSIGN(OAuth2Test);
177 };
178
179 class CookieReader : public base::RefCountedThreadSafe<CookieReader> {
180 public:
181 CookieReader() {
182 }
183
184 void ReadCookies(Profile* profile) {
185 context_ = profile->GetRequestContext();
186 content::BrowserThread::PostTask(
187 content::BrowserThread::IO, FROM_HERE,
188 base::Bind(&CookieReader::ReadCookiesOnIOThread,
189 this));
190 runner_ = new content::MessageLoopRunner;
191 runner_->Run();
192 }
193
194 std::string GetCookieValue(const std::string& name) {
195 for (std::vector<net::CanonicalCookie>::const_iterator iter =
196 cookie_list_.begin();
197 iter != cookie_list_.end();
198 ++iter) {
199 if (iter->Name() == name) {
200 return iter->Value();
201 }
202 }
203 return std::string();
204 }
205
92 private: 206 private:
93 DISALLOW_COPY_AND_ASSIGN(OAuth2Test); 207 friend class base::RefCountedThreadSafe<CookieReader>;
208
209 virtual ~CookieReader() {
210 }
211
212 void ReadCookiesOnIOThread() {
213 context_->GetURLRequestContext()->cookie_store()->GetCookieMonster()->
214 GetAllCookiesAsync(base::Bind(
215 &CookieReader::OnGetAllCookiesOnUIThread,
216 this));
217 }
218
219 void OnGetAllCookiesOnUIThread(const net::CookieList& cookies) {
220 cookie_list_ = cookies;
221 content::BrowserThread::PostTask(
222 content::BrowserThread::UI, FROM_HERE,
223 base::Bind(&CookieReader::OnCookiesReadyOnUIThread,
224 this));
225 }
226
227 void OnCookiesReadyOnUIThread() {
228 runner_->Quit();
229 }
230
231 scoped_refptr<net::URLRequestContextGetter> context_;
232 net::CookieList cookie_list_;
233 scoped_refptr<content::MessageLoopRunner> runner_;
234
235 DISALLOW_COPY_AND_ASSIGN(CookieReader);
94 }; 236 };
95 237
96 class OAuth2LoginManagerStateWaiter : public OAuth2LoginManager::Observer { 238 class OAuth2LoginManagerStateWaiter : public OAuth2LoginManager::Observer {
97 public: 239 public:
98 explicit OAuth2LoginManagerStateWaiter(Profile* profile) 240 explicit OAuth2LoginManagerStateWaiter(Profile* profile)
99 : profile_(profile), 241 : profile_(profile),
100 waiting_for_state_(false), 242 waiting_for_state_(false),
101 final_state_(OAuth2LoginManager::SESSION_RESTORE_NOT_STARTED) { 243 final_state_(OAuth2LoginManager::SESSION_RESTORE_NOT_STARTED) {
102 } 244 }
103 245
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 281
140 Profile* profile_; 282 Profile* profile_;
141 std::set<OAuth2LoginManager::SessionRestoreState> states_; 283 std::set<OAuth2LoginManager::SessionRestoreState> states_;
142 bool waiting_for_state_; 284 bool waiting_for_state_;
143 OAuth2LoginManager::SessionRestoreState final_state_; 285 OAuth2LoginManager::SessionRestoreState final_state_;
144 scoped_refptr<content::MessageLoopRunner> runner_; 286 scoped_refptr<content::MessageLoopRunner> runner_;
145 287
146 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManagerStateWaiter); 288 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManagerStateWaiter);
147 }; 289 };
148 290
149 IN_PROC_BROWSER_TEST_F(OAuth2Test, NewUser) { 291 // PRE_MergeSession is testing merge session for a new profile.
292 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_PRE_MergeSession) {
293 SetupGaiaServerForNewAccount();
150 SimulateNetworkOnline(); 294 SimulateNetworkOnline();
151 chromeos::WizardController::SkipPostLoginScreensForTesting(); 295 chromeos::WizardController::SkipPostLoginScreensForTesting();
152 chromeos::WizardController* wizard_controller = 296 chromeos::WizardController* wizard_controller =
153 chromeos::WizardController::default_controller(); 297 chromeos::WizardController::default_controller();
154 wizard_controller->SkipToLoginForTesting(LoginScreenContext()); 298 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
155 299
156 content::WindowedNotificationObserver( 300 content::WindowedNotificationObserver(
157 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, 301 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
158 content::NotificationService::AllSources()).Wait(); 302 content::NotificationService::AllSources()).Wait();
159 303
304 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
305 User::OAUTH_TOKEN_STATUS_UNKNOWN);
306
160 // Use capitalized and dotted user name on purpose to make sure 307 // Use capitalized and dotted user name on purpose to make sure
161 // our email normalization kicks in. 308 // our email normalization kicks in.
162 GetLoginDisplay()->ShowSigninScreenForCreds("User.Name", "password"); 309 GetLoginDisplay()->ShowSigninScreenForCreds(kTestRawAccountId,
310 kTestAccountPassword);
163 311
164 content::WindowedNotificationObserver( 312 content::WindowedNotificationObserver(
165 chrome::NOTIFICATION_SESSION_STARTED, 313 chrome::NOTIFICATION_SESSION_STARTED,
166 content::NotificationService::AllSources()).Wait(); 314 content::NotificationService::AllSources()).Wait();
315 Profile* profile = ProfileManager::GetPrimaryUserProfile();
167 316
317 // Wait for the session merge to finish.
168 std::set<OAuth2LoginManager::SessionRestoreState> states; 318 std::set<OAuth2LoginManager::SessionRestoreState> states;
169 states.insert(OAuth2LoginManager::SESSION_RESTORE_DONE); 319 states.insert(OAuth2LoginManager::SESSION_RESTORE_DONE);
170 states.insert(OAuth2LoginManager::SESSION_RESTORE_FAILED); 320 states.insert(OAuth2LoginManager::SESSION_RESTORE_FAILED);
171 states.insert(OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED); 321 states.insert(OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED);
172 OAuth2LoginManagerStateWaiter merge_session_waiter( 322 OAuth2LoginManagerStateWaiter merge_session_waiter(
173 ProfileManager::GetPrimaryUserProfile()); 323 ProfileManager::GetPrimaryUserProfile());
174 merge_session_waiter.WaitForStates(states); 324 merge_session_waiter.WaitForStates(states);
175 EXPECT_EQ(merge_session_waiter.final_state(), 325 EXPECT_EQ(merge_session_waiter.final_state(),
176 OAuth2LoginManager::SESSION_RESTORE_DONE); 326 OAuth2LoginManager::SESSION_RESTORE_DONE);
327
328 // Check for existance of refresh token.
329 ProfileOAuth2TokenService* token_service =
330 ProfileOAuth2TokenServiceFactory::GetForProfile(
331 profile);
332 EXPECT_TRUE(token_service->RefreshTokenIsAvailable(kTestAccountId));
333
334 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
335 User::OAUTH2_TOKEN_STATUS_VALID);
336
337 scoped_refptr<CookieReader> cookie_reader(new CookieReader());
338 cookie_reader->ReadCookies(profile);
339 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSessionSIDCookie);
340 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSessionLSIDCookie);
341 }
342
343 // MergeSession test is running merge session process for an existing profile
344 // that was generated in PRE_PRE_MergeSession test.
345 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_MergeSession) {
346 SetupGaiaServerForExistingAccount();
347 SimulateNetworkOnline();
348
349 content::WindowedNotificationObserver(
350 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
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
380 scoped_refptr<CookieReader> cookie_reader(new CookieReader());
381 cookie_reader->ReadCookies(profile);
382 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSession2SIDCookie);
383 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSession2LSIDCookie);
384 }
385
386 // MergeSession test is attempting to merge session for an existing profile
387 // that was generated in PRE_PRE_MergeSession test. This attempt should fail
388 // since FakeGaia instance isn't configured to return relevant tokens/cookies.
389 IN_PROC_BROWSER_TEST_F(OAuth2Test, MergeSession) {
390 SimulateNetworkOnline();
391
392 content::WindowedNotificationObserver(
393 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
394 content::NotificationService::AllSources()).Wait();
395
396 JsExpect("!!document.querySelector('#account-picker')");
397 JsExpect("!!document.querySelector('#pod-row')");
398
399 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
400 User::OAUTH2_TOKEN_STATUS_VALID);
401
402 EXPECT_TRUE(TryToLogin(kTestAccountId, kTestAccountPassword));
403
404 // Wait for the session merge to finish.
405 std::set<OAuth2LoginManager::SessionRestoreState> states;
406 states.insert(OAuth2LoginManager::SESSION_RESTORE_DONE);
407 states.insert(OAuth2LoginManager::SESSION_RESTORE_FAILED);
408 states.insert(OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED);
409 OAuth2LoginManagerStateWaiter merge_session_waiter(
410 ProfileManager::GetPrimaryUserProfile());
411 merge_session_waiter.WaitForStates(states);
412 EXPECT_EQ(merge_session_waiter.final_state(),
413 OAuth2LoginManager::SESSION_RESTORE_FAILED);
414
415 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId),
416 User::OAUTH2_TOKEN_STATUS_INVALID);
177 } 417 }
178 418
179 } // namespace chromeos 419 } // namespace chromeos
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/chromeos/login/saml_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698