OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // A complete set of unit tests for GaiaAuthFetcher. | 5 // A complete set of unit tests for GaiaAuthFetcher. |
6 // Originally ported from GoogleAuthenticator tests. | 6 // Originally ported from GoogleAuthenticator tests. |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 108 |
109 EXPECT_FALSE(expected_error == matching_error); | 109 EXPECT_FALSE(expected_error == matching_error); |
110 | 110 |
111 matching_error = GoogleServiceAuthError(GoogleServiceAuthError::NONE); | 111 matching_error = GoogleServiceAuthError(GoogleServiceAuthError::NONE); |
112 | 112 |
113 EXPECT_TRUE(expected_error == matching_error); | 113 EXPECT_TRUE(expected_error == matching_error); |
114 } | 114 } |
115 | 115 |
116 TEST_F(GaiaAuthFetcherTest, LoginNetFailure) { | 116 TEST_F(GaiaAuthFetcherTest, LoginNetFailure) { |
117 int error_no = net::ERR_CONNECTION_RESET; | 117 int error_no = net::ERR_CONNECTION_RESET; |
118 URLRequestStatus status(URLRequestStatus::FAILED, error_no); | 118 net::URLRequestStatus status(net::URLRequestStatus::FAILED, error_no); |
119 | 119 |
120 GoogleServiceAuthError expected_error = | 120 GoogleServiceAuthError expected_error = |
121 GoogleServiceAuthError::FromConnectionError(error_no); | 121 GoogleServiceAuthError::FromConnectionError(error_no); |
122 | 122 |
123 MockGaiaConsumer consumer; | 123 MockGaiaConsumer consumer; |
124 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error)) | 124 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error)) |
125 .Times(1); | 125 .Times(1); |
126 | 126 |
127 GaiaAuthFetcher auth(&consumer, std::string(), | 127 GaiaAuthFetcher auth(&consumer, std::string(), |
128 profile_.GetRequestContext()); | 128 profile_.GetRequestContext()); |
129 | 129 |
130 auth.OnURLFetchComplete(NULL, | 130 auth.OnURLFetchComplete(NULL, |
131 client_login_source_, | 131 client_login_source_, |
132 status, | 132 status, |
133 0, | 133 0, |
134 cookies_, | 134 cookies_, |
135 std::string()); | 135 std::string()); |
136 } | 136 } |
137 | 137 |
138 TEST_F(GaiaAuthFetcherTest, TokenNetFailure) { | 138 TEST_F(GaiaAuthFetcherTest, TokenNetFailure) { |
139 int error_no = net::ERR_CONNECTION_RESET; | 139 int error_no = net::ERR_CONNECTION_RESET; |
140 URLRequestStatus status(URLRequestStatus::FAILED, error_no); | 140 net::URLRequestStatus status(net::URLRequestStatus::FAILED, error_no); |
141 | 141 |
142 GoogleServiceAuthError expected_error = | 142 GoogleServiceAuthError expected_error = |
143 GoogleServiceAuthError::FromConnectionError(error_no); | 143 GoogleServiceAuthError::FromConnectionError(error_no); |
144 | 144 |
145 MockGaiaConsumer consumer; | 145 MockGaiaConsumer consumer; |
146 EXPECT_CALL(consumer, OnIssueAuthTokenFailure(_, expected_error)) | 146 EXPECT_CALL(consumer, OnIssueAuthTokenFailure(_, expected_error)) |
147 .Times(1); | 147 .Times(1); |
148 | 148 |
149 GaiaAuthFetcher auth(&consumer, std::string(), | 149 GaiaAuthFetcher auth(&consumer, std::string(), |
150 profile_.GetRequestContext()); | 150 profile_.GetRequestContext()); |
151 | 151 |
152 auth.OnURLFetchComplete(NULL, | 152 auth.OnURLFetchComplete(NULL, |
153 issue_auth_token_source_, | 153 issue_auth_token_source_, |
154 status, | 154 status, |
155 0, | 155 0, |
156 cookies_, | 156 cookies_, |
157 std::string()); | 157 std::string()); |
158 } | 158 } |
159 | 159 |
160 | 160 |
161 TEST_F(GaiaAuthFetcherTest, LoginDenied) { | 161 TEST_F(GaiaAuthFetcherTest, LoginDenied) { |
162 std::string data("Error=BadAuthentication"); | 162 std::string data("Error=BadAuthentication"); |
163 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 163 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
164 | 164 |
165 GoogleServiceAuthError expected_error( | 165 GoogleServiceAuthError expected_error( |
166 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 166 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
167 | 167 |
168 MockGaiaConsumer consumer; | 168 MockGaiaConsumer consumer; |
169 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error)) | 169 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error)) |
170 .Times(1); | 170 .Times(1); |
171 | 171 |
172 GaiaAuthFetcher auth(&consumer, std::string(), | 172 GaiaAuthFetcher auth(&consumer, std::string(), |
173 profile_.GetRequestContext()); | 173 profile_.GetRequestContext()); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 result.sid = "sid"; | 213 result.sid = "sid"; |
214 result.token = "auth"; | 214 result.token = "auth"; |
215 result.data = data; | 215 result.data = data; |
216 | 216 |
217 MockGaiaConsumer consumer; | 217 MockGaiaConsumer consumer; |
218 EXPECT_CALL(consumer, OnClientLoginSuccess(result)) | 218 EXPECT_CALL(consumer, OnClientLoginSuccess(result)) |
219 .Times(1); | 219 .Times(1); |
220 | 220 |
221 GaiaAuthFetcher auth(&consumer, std::string(), | 221 GaiaAuthFetcher auth(&consumer, std::string(), |
222 profile_.GetRequestContext()); | 222 profile_.GetRequestContext()); |
223 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 223 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
224 auth.OnURLFetchComplete(NULL, | 224 auth.OnURLFetchComplete(NULL, |
225 client_login_source_, | 225 client_login_source_, |
226 status, | 226 status, |
227 RC_REQUEST_OK, | 227 RC_REQUEST_OK, |
228 cookies_, | 228 cookies_, |
229 data); | 229 data); |
230 } | 230 } |
231 | 231 |
232 TEST_F(GaiaAuthFetcherTest, WorkingIssueAuthToken) { | 232 TEST_F(GaiaAuthFetcherTest, WorkingIssueAuthToken) { |
233 MockGaiaConsumer consumer; | 233 MockGaiaConsumer consumer; |
234 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess(_, "token")) | 234 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess(_, "token")) |
235 .Times(1); | 235 .Times(1); |
236 | 236 |
237 GaiaAuthFetcher auth(&consumer, std::string(), | 237 GaiaAuthFetcher auth(&consumer, std::string(), |
238 profile_.GetRequestContext()); | 238 profile_.GetRequestContext()); |
239 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 239 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
240 auth.OnURLFetchComplete(NULL, | 240 auth.OnURLFetchComplete(NULL, |
241 issue_auth_token_source_, | 241 issue_auth_token_source_, |
242 status, | 242 status, |
243 RC_REQUEST_OK, | 243 RC_REQUEST_OK, |
244 cookies_, | 244 cookies_, |
245 "token"); | 245 "token"); |
246 } | 246 } |
247 | 247 |
248 TEST_F(GaiaAuthFetcherTest, CheckTwoFactorResponse) { | 248 TEST_F(GaiaAuthFetcherTest, CheckTwoFactorResponse) { |
249 std::string response = | 249 std::string response = |
(...skipping 13 matching lines...) Expand all Loading... |
263 | 263 |
264 GoogleServiceAuthError error = | 264 GoogleServiceAuthError error = |
265 GoogleServiceAuthError(GoogleServiceAuthError::TWO_FACTOR); | 265 GoogleServiceAuthError(GoogleServiceAuthError::TWO_FACTOR); |
266 | 266 |
267 MockGaiaConsumer consumer; | 267 MockGaiaConsumer consumer; |
268 EXPECT_CALL(consumer, OnClientLoginFailure(error)) | 268 EXPECT_CALL(consumer, OnClientLoginFailure(error)) |
269 .Times(1); | 269 .Times(1); |
270 | 270 |
271 GaiaAuthFetcher auth(&consumer, std::string(), | 271 GaiaAuthFetcher auth(&consumer, std::string(), |
272 profile_.GetRequestContext()); | 272 profile_.GetRequestContext()); |
273 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 273 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
274 auth.OnURLFetchComplete(NULL, | 274 auth.OnURLFetchComplete(NULL, |
275 client_login_source_, | 275 client_login_source_, |
276 status, | 276 status, |
277 RC_FORBIDDEN, | 277 RC_FORBIDDEN, |
278 cookies_, | 278 cookies_, |
279 response); | 279 response); |
280 } | 280 } |
281 | 281 |
282 TEST_F(GaiaAuthFetcherTest, CaptchaParse) { | 282 TEST_F(GaiaAuthFetcherTest, CaptchaParse) { |
283 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 283 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
284 std::string data = "Url=http://www.google.com/login/captcha\n" | 284 std::string data = "Url=http://www.google.com/login/captcha\n" |
285 "Error=CaptchaRequired\n" | 285 "Error=CaptchaRequired\n" |
286 "CaptchaToken=CCTOKEN\n" | 286 "CaptchaToken=CCTOKEN\n" |
287 "CaptchaUrl=Captcha?ctoken=CCTOKEN\n"; | 287 "CaptchaUrl=Captcha?ctoken=CCTOKEN\n"; |
288 GoogleServiceAuthError error = | 288 GoogleServiceAuthError error = |
289 GaiaAuthFetcher::GenerateAuthError(data, status); | 289 GaiaAuthFetcher::GenerateAuthError(data, status); |
290 | 290 |
291 std::string token = "CCTOKEN"; | 291 std::string token = "CCTOKEN"; |
292 GURL image_url("http://www.google.com/accounts/Captcha?ctoken=CCTOKEN"); | 292 GURL image_url("http://www.google.com/accounts/Captcha?ctoken=CCTOKEN"); |
293 GURL unlock_url("http://www.google.com/login/captcha"); | 293 GURL unlock_url("http://www.google.com/login/captcha"); |
294 | 294 |
295 EXPECT_EQ(error.state(), GoogleServiceAuthError::CAPTCHA_REQUIRED); | 295 EXPECT_EQ(error.state(), GoogleServiceAuthError::CAPTCHA_REQUIRED); |
296 EXPECT_EQ(error.captcha().token, token); | 296 EXPECT_EQ(error.captcha().token, token); |
297 EXPECT_EQ(error.captcha().image_url, image_url); | 297 EXPECT_EQ(error.captcha().image_url, image_url); |
298 EXPECT_EQ(error.captcha().unlock_url, unlock_url); | 298 EXPECT_EQ(error.captcha().unlock_url, unlock_url); |
299 } | 299 } |
300 | 300 |
301 TEST_F(GaiaAuthFetcherTest, AccountDeletedError) { | 301 TEST_F(GaiaAuthFetcherTest, AccountDeletedError) { |
302 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 302 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
303 std::string data = "Error=AccountDeleted\n"; | 303 std::string data = "Error=AccountDeleted\n"; |
304 GoogleServiceAuthError error = | 304 GoogleServiceAuthError error = |
305 GaiaAuthFetcher::GenerateAuthError(data, status); | 305 GaiaAuthFetcher::GenerateAuthError(data, status); |
306 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DELETED); | 306 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DELETED); |
307 } | 307 } |
308 | 308 |
309 TEST_F(GaiaAuthFetcherTest, AccountDisabledError) { | 309 TEST_F(GaiaAuthFetcherTest, AccountDisabledError) { |
310 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 310 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
311 std::string data = "Error=AccountDisabled\n"; | 311 std::string data = "Error=AccountDisabled\n"; |
312 GoogleServiceAuthError error = | 312 GoogleServiceAuthError error = |
313 GaiaAuthFetcher::GenerateAuthError(data, status); | 313 GaiaAuthFetcher::GenerateAuthError(data, status); |
314 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DISABLED); | 314 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DISABLED); |
315 } | 315 } |
316 | 316 |
317 TEST_F(GaiaAuthFetcherTest,BadAuthenticationError) { | 317 TEST_F(GaiaAuthFetcherTest,BadAuthenticationError) { |
318 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 318 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
319 std::string data = "Error=BadAuthentication\n"; | 319 std::string data = "Error=BadAuthentication\n"; |
320 GoogleServiceAuthError error = | 320 GoogleServiceAuthError error = |
321 GaiaAuthFetcher::GenerateAuthError(data, status); | 321 GaiaAuthFetcher::GenerateAuthError(data, status); |
322 EXPECT_EQ(error.state(), GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 322 EXPECT_EQ(error.state(), GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
323 } | 323 } |
324 | 324 |
325 TEST_F(GaiaAuthFetcherTest,IncomprehensibleError) { | 325 TEST_F(GaiaAuthFetcherTest,IncomprehensibleError) { |
326 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 326 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
327 std::string data = "Error=Gobbledygook\n"; | 327 std::string data = "Error=Gobbledygook\n"; |
328 GoogleServiceAuthError error = | 328 GoogleServiceAuthError error = |
329 GaiaAuthFetcher::GenerateAuthError(data, status); | 329 GaiaAuthFetcher::GenerateAuthError(data, status); |
330 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE); | 330 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE); |
331 } | 331 } |
332 | 332 |
333 TEST_F(GaiaAuthFetcherTest,ServiceUnavailableError) { | 333 TEST_F(GaiaAuthFetcherTest,ServiceUnavailableError) { |
334 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 334 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
335 std::string data = "Error=ServiceUnavailable\n"; | 335 std::string data = "Error=ServiceUnavailable\n"; |
336 GoogleServiceAuthError error = | 336 GoogleServiceAuthError error = |
337 GaiaAuthFetcher::GenerateAuthError(data, status); | 337 GaiaAuthFetcher::GenerateAuthError(data, status); |
338 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE); | 338 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE); |
339 } | 339 } |
340 | 340 |
341 TEST_F(GaiaAuthFetcherTest, FullLogin) { | 341 TEST_F(GaiaAuthFetcherTest, FullLogin) { |
342 MockGaiaConsumer consumer; | 342 MockGaiaConsumer consumer; |
343 EXPECT_CALL(consumer, OnClientLoginSuccess(_)) | 343 EXPECT_CALL(consumer, OnClientLoginSuccess(_)) |
344 .Times(1); | 344 .Times(1); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 profile_.GetRequestContext()); | 396 profile_.GetRequestContext()); |
397 auth.StartClientLogin("username", | 397 auth.StartClientLogin("username", |
398 "password", | 398 "password", |
399 "service", | 399 "service", |
400 std::string(), | 400 std::string(), |
401 std::string(), | 401 std::string(), |
402 GaiaAuthFetcher::HostedAccountsAllowed); | 402 GaiaAuthFetcher::HostedAccountsAllowed); |
403 | 403 |
404 URLFetcher::set_factory(NULL); | 404 URLFetcher::set_factory(NULL); |
405 EXPECT_TRUE(auth.HasPendingFetch()); | 405 EXPECT_TRUE(auth.HasPendingFetch()); |
406 auth.OnURLFetchComplete(NULL, | 406 auth.OnURLFetchComplete( |
407 client_login_source_, | 407 NULL, |
408 URLRequestStatus(URLRequestStatus::SUCCESS, 0), | 408 client_login_source_, |
409 RC_REQUEST_OK, | 409 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
410 cookies_, | 410 RC_REQUEST_OK, |
411 "SID=sid\nLSID=lsid\nAuth=auth\n"); | 411 cookies_, |
| 412 "SID=sid\nLSID=lsid\nAuth=auth\n"); |
412 EXPECT_FALSE(auth.HasPendingFetch()); | 413 EXPECT_FALSE(auth.HasPendingFetch()); |
413 } | 414 } |
414 | 415 |
415 TEST_F(GaiaAuthFetcherTest, FullTokenSuccess) { | 416 TEST_F(GaiaAuthFetcherTest, FullTokenSuccess) { |
416 MockGaiaConsumer consumer; | 417 MockGaiaConsumer consumer; |
417 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess("service", "token")) | 418 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess("service", "token")) |
418 .Times(1); | 419 .Times(1); |
419 | 420 |
420 TestingProfile profile; | 421 TestingProfile profile; |
421 TestURLFetcherFactory factory; | 422 TestURLFetcherFactory factory; |
422 URLFetcher::set_factory(&factory); | 423 URLFetcher::set_factory(&factory); |
423 | 424 |
424 GaiaAuthFetcher auth(&consumer, std::string(), | 425 GaiaAuthFetcher auth(&consumer, std::string(), |
425 profile_.GetRequestContext()); | 426 profile_.GetRequestContext()); |
426 auth.StartIssueAuthToken("sid", "lsid", "service"); | 427 auth.StartIssueAuthToken("sid", "lsid", "service"); |
427 | 428 |
428 URLFetcher::set_factory(NULL); | 429 URLFetcher::set_factory(NULL); |
429 EXPECT_TRUE(auth.HasPendingFetch()); | 430 EXPECT_TRUE(auth.HasPendingFetch()); |
430 auth.OnURLFetchComplete(NULL, | 431 auth.OnURLFetchComplete( |
431 issue_auth_token_source_, | 432 NULL, |
432 URLRequestStatus(URLRequestStatus::SUCCESS, 0), | 433 issue_auth_token_source_, |
433 RC_REQUEST_OK, | 434 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
434 cookies_, | 435 RC_REQUEST_OK, |
435 "token"); | 436 cookies_, |
| 437 "token"); |
436 EXPECT_FALSE(auth.HasPendingFetch()); | 438 EXPECT_FALSE(auth.HasPendingFetch()); |
437 } | 439 } |
438 | 440 |
439 TEST_F(GaiaAuthFetcherTest, FullTokenFailure) { | 441 TEST_F(GaiaAuthFetcherTest, FullTokenFailure) { |
440 MockGaiaConsumer consumer; | 442 MockGaiaConsumer consumer; |
441 EXPECT_CALL(consumer, OnIssueAuthTokenFailure("service", _)) | 443 EXPECT_CALL(consumer, OnIssueAuthTokenFailure("service", _)) |
442 .Times(1); | 444 .Times(1); |
443 | 445 |
444 TestingProfile profile; | 446 TestingProfile profile; |
445 TestURLFetcherFactory factory; | 447 TestURLFetcherFactory factory; |
446 URLFetcher::set_factory(&factory); | 448 URLFetcher::set_factory(&factory); |
447 | 449 |
448 GaiaAuthFetcher auth(&consumer, std::string(), | 450 GaiaAuthFetcher auth(&consumer, std::string(), |
449 profile_.GetRequestContext()); | 451 profile_.GetRequestContext()); |
450 auth.StartIssueAuthToken("sid", "lsid", "service"); | 452 auth.StartIssueAuthToken("sid", "lsid", "service"); |
451 | 453 |
452 URLFetcher::set_factory(NULL); | 454 URLFetcher::set_factory(NULL); |
453 EXPECT_TRUE(auth.HasPendingFetch()); | 455 EXPECT_TRUE(auth.HasPendingFetch()); |
454 auth.OnURLFetchComplete(NULL, | 456 auth.OnURLFetchComplete( |
455 issue_auth_token_source_, | 457 NULL, |
456 URLRequestStatus(URLRequestStatus::SUCCESS, 0), | 458 issue_auth_token_source_, |
457 RC_FORBIDDEN, | 459 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
458 cookies_, | 460 RC_FORBIDDEN, |
459 ""); | 461 cookies_, |
| 462 ""); |
460 EXPECT_FALSE(auth.HasPendingFetch()); | 463 EXPECT_FALSE(auth.HasPendingFetch()); |
461 } | 464 } |
OLD | NEW |