1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <string> 6 7 #include "base/message_loop/message_loop.h" 8 #include "base/run_loop.h" 9 #include "google_apis/gaia/gaia_constants.h" 10 #include "google_apis/gaia/google_service_auth_error.h" 11 #include "google_apis/gaia/oauth2_access_token_consumer.h" 12 #include "google_apis/gaia/oauth2_access_token_fetcher.h" 13 #include "google_apis/gaia/oauth2_token_service.h" 14 #include "google_apis/gaia/oauth2_token_service_test_util.h" 15 #include "net/http/http_status_code.h" 16 #include "net/url_request/test_url_fetcher_factory.h" 17 #include "net/url_request/url_fetcher_delegate.h" 18 #include "net/url_request/url_request_test_util.h" 19 #include "testing/gtest/include/gtest/gtest.h" 20 21 // A testing consumer that retries on error. 22 class RetryingTestingOAuth2TokenServiceConsumer 23 : public TestingOAuth2TokenServiceConsumer { 24 public: 25 RetryingTestingOAuth2TokenServiceConsumer( 26 OAuth2TokenService* oauth2_service, 27 const std::string& account_id) 28 : oauth2_service_(oauth2_service), 29 account_id_(account_id) {} 30 virtual ~RetryingTestingOAuth2TokenServiceConsumer() {} 31 32 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, 33 const GoogleServiceAuthError& error) OVERRIDE { 34 TestingOAuth2TokenServiceConsumer::OnGetTokenFailure(request, error); 35 request_.reset(oauth2_service_->StartRequest( 36 account_id_, OAuth2TokenService::ScopeSet(), this).release()); 37 } 38 39 OAuth2TokenService* oauth2_service_; 40 std::string account_id_; 41 scoped_ptr<OAuth2TokenService::Request> request_; 42 }; 43 44 class TestOAuth2TokenService : public OAuth2TokenService { 45 public: 46 explicit TestOAuth2TokenService(net::TestURLRequestContextGetter* getter) 47 : request_context_getter_(getter) { 48 } 49 50 void CancelAllRequestsForTest() { CancelAllRequests(); } 51 52 void CancelRequestsForAccountForTest(const std::string& account_id) { 53 CancelRequestsForAccount(account_id); 54 } 55 56 // For testing: set the refresh token to be used. 57 void set_refresh_token(const std::string& account_id, 58 const std::string& refresh_token) { 59 refresh_tokens_[account_id] = refresh_token; 60 } 61 62 protected: 63 virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE { 64 // account_id explicitly ignored. 65 return refresh_tokens_[account_id]; 66 } 67 68 private: 69 // OAuth2TokenService implementation. 70 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE { 71 return request_context_getter_.get(); 72 } 73 74 std::map<std::string, std::string> refresh_tokens_; 75 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; 76 }; 77 78 class OAuth2TokenServiceTest : public testing::Test { 79 public: 80 virtual void SetUp() OVERRIDE { 81 oauth2_service_.reset( 82 new TestOAuth2TokenService(new net::TestURLRequestContextGetter( 83 message_loop_.message_loop_proxy()))); 84 account_id_ = "test_user (at) gmail.com"; 85 } 86 87 virtual void TearDown() OVERRIDE { 88 // Makes sure that all the clean up tasks are run. 89 base::RunLoop().RunUntilIdle(); 90 } 91 92 protected: 93 base::MessageLoopForIO message_loop_; // net:: stuff needs IO message loop. 94 net::TestURLFetcherFactory factory_; 95 scoped_ptr<TestOAuth2TokenService> oauth2_service_; 96 std::string account_id_; 97 TestingOAuth2TokenServiceConsumer consumer_; 98 }; 99 100 TEST_F(OAuth2TokenServiceTest, NoOAuth2RefreshToken) { 101 scoped_ptr<OAuth2TokenService::Request> request( 102 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(), 103 &consumer_)); 104 base::RunLoop().RunUntilIdle(); 105 106 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 107 EXPECT_EQ(1, consumer_.number_of_errors_); 108 } 109 110 TEST_F(OAuth2TokenServiceTest, FailureShouldNotRetry) { 111 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 112 scoped_ptr<OAuth2TokenService::Request> request( 113 oauth2_service_->StartRequest(account_id_, 114 OAuth2TokenService::ScopeSet(), 115 &consumer_)); 116 base::RunLoop().RunUntilIdle(); 117 118 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 119 EXPECT_EQ(0, consumer_.number_of_errors_); 120 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 121 ASSERT_TRUE(fetcher); 122 fetcher->set_response_code(net::HTTP_UNAUTHORIZED); 123 fetcher->SetResponseString(std::string()); 124 fetcher->delegate()->OnURLFetchComplete(fetcher); 125 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 126 EXPECT_EQ(1, consumer_.number_of_errors_); 127 EXPECT_EQ(fetcher, factory_.GetFetcherByID(0)); 128 } 129 130 TEST_F(OAuth2TokenServiceTest, SuccessWithoutCaching) { 131 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 132 scoped_ptr<OAuth2TokenService::Request> request( 133 oauth2_service_->StartRequest(account_id_, 134 OAuth2TokenService::ScopeSet(), 135 &consumer_)); 136 base::RunLoop().RunUntilIdle(); 137 138 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 139 EXPECT_EQ(0, consumer_.number_of_errors_); 140 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 141 ASSERT_TRUE(fetcher); 142 fetcher->set_response_code(net::HTTP_OK); 143 fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); 144 fetcher->delegate()->OnURLFetchComplete(fetcher); 145 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 146 EXPECT_EQ(0, consumer_.number_of_errors_); 147 EXPECT_EQ("token", consumer_.last_token_); 148 } 149 150 TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) { 151 OAuth2TokenService::ScopeSet scopes1; 152 scopes1.insert("s1"); 153 scopes1.insert("s2"); 154 OAuth2TokenService::ScopeSet scopes1_same; 155 scopes1_same.insert("s2"); 156 scopes1_same.insert("s1"); 157 OAuth2TokenService::ScopeSet scopes2; 158 scopes2.insert("s3"); 159 160 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 161 162 // First request. 163 scoped_ptr<OAuth2TokenService::Request> request( 164 oauth2_service_->StartRequest(account_id_, scopes1, &consumer_)); 165 base::RunLoop().RunUntilIdle(); 166 167 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 168 EXPECT_EQ(0, consumer_.number_of_errors_); 169 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 170 ASSERT_TRUE(fetcher); 171 fetcher->set_response_code(net::HTTP_OK); 172 fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); 173 fetcher->delegate()->OnURLFetchComplete(fetcher); 174 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 175 EXPECT_EQ(0, consumer_.number_of_errors_); 176 EXPECT_EQ("token", consumer_.last_token_); 177 178 // Second request to the same set of scopes, should return the same token 179 // without needing a network request. 180 scoped_ptr<OAuth2TokenService::Request> request2( 181 oauth2_service_->StartRequest(account_id_, scopes1_same, &consumer_)); 182 base::RunLoop().RunUntilIdle(); 183 184 // No new network fetcher. 185 EXPECT_EQ(2, consumer_.number_of_successful_tokens_); 186 EXPECT_EQ(0, consumer_.number_of_errors_); 187 EXPECT_EQ("token", consumer_.last_token_); 188 189 // Third request to a new set of scopes, should return another token. 190 scoped_ptr<OAuth2TokenService::Request> request3( 191 oauth2_service_->StartRequest(account_id_, scopes2, &consumer_)); 192 base::RunLoop().RunUntilIdle(); 193 EXPECT_EQ(2, consumer_.number_of_successful_tokens_); 194 EXPECT_EQ(0, consumer_.number_of_errors_); 195 fetcher = factory_.GetFetcherByID(0); 196 ASSERT_TRUE(fetcher); 197 fetcher->set_response_code(net::HTTP_OK); 198 fetcher->SetResponseString(GetValidTokenResponse("token2", 3600)); 199 fetcher->delegate()->OnURLFetchComplete(fetcher); 200 EXPECT_EQ(3, consumer_.number_of_successful_tokens_); 201 EXPECT_EQ(0, consumer_.number_of_errors_); 202 EXPECT_EQ("token2", consumer_.last_token_); 203 } 204 205 TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndFailure) { 206 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 207 208 // First request. 209 scoped_ptr<OAuth2TokenService::Request> request( 210 oauth2_service_->StartRequest(account_id_, 211 OAuth2TokenService::ScopeSet(), 212 &consumer_)); 213 base::RunLoop().RunUntilIdle(); 214 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 215 EXPECT_EQ(0, consumer_.number_of_errors_); 216 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 217 ASSERT_TRUE(fetcher); 218 fetcher->set_response_code(net::HTTP_OK); 219 fetcher->SetResponseString(GetValidTokenResponse("token", 0)); 220 fetcher->delegate()->OnURLFetchComplete(fetcher); 221 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 222 EXPECT_EQ(0, consumer_.number_of_errors_); 223 EXPECT_EQ("token", consumer_.last_token_); 224 225 // Second request must try to access the network as the token has expired. 226 scoped_ptr<OAuth2TokenService::Request> request2( 227 oauth2_service_->StartRequest(account_id_, 228 OAuth2TokenService::ScopeSet(), 229 &consumer_)); 230 base::RunLoop().RunUntilIdle(); 231 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 232 EXPECT_EQ(0, consumer_.number_of_errors_); 233 234 // Network failure. 235 fetcher = factory_.GetFetcherByID(0); 236 ASSERT_TRUE(fetcher); 237 fetcher->set_response_code(net::HTTP_UNAUTHORIZED); 238 fetcher->SetResponseString(std::string()); 239 fetcher->delegate()->OnURLFetchComplete(fetcher); 240 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 241 EXPECT_EQ(1, consumer_.number_of_errors_); 242 } 243 244 TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndSuccess) { 245 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 246 247 // First request. 248 scoped_ptr<OAuth2TokenService::Request> request( 249 oauth2_service_->StartRequest(account_id_, 250 OAuth2TokenService::ScopeSet(), 251 &consumer_)); 252 base::RunLoop().RunUntilIdle(); 253 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 254 EXPECT_EQ(0, consumer_.number_of_errors_); 255 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 256 ASSERT_TRUE(fetcher); 257 fetcher->set_response_code(net::HTTP_OK); 258 fetcher->SetResponseString(GetValidTokenResponse("token", 0)); 259 fetcher->delegate()->OnURLFetchComplete(fetcher); 260 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 261 EXPECT_EQ(0, consumer_.number_of_errors_); 262 EXPECT_EQ("token", consumer_.last_token_); 263 264 // Second request must try to access the network as the token has expired. 265 scoped_ptr<OAuth2TokenService::Request> request2( 266 oauth2_service_->StartRequest(account_id_, 267 OAuth2TokenService::ScopeSet(), 268 &consumer_)); 269 base::RunLoop().RunUntilIdle(); 270 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 271 EXPECT_EQ(0, consumer_.number_of_errors_); 272 273 fetcher = factory_.GetFetcherByID(0); 274 ASSERT_TRUE(fetcher); 275 fetcher->set_response_code(net::HTTP_OK); 276 fetcher->SetResponseString(GetValidTokenResponse("another token", 0)); 277 fetcher->delegate()->OnURLFetchComplete(fetcher); 278 EXPECT_EQ(2, consumer_.number_of_successful_tokens_); 279 EXPECT_EQ(0, consumer_.number_of_errors_); 280 EXPECT_EQ("another token", consumer_.last_token_); 281 } 282 283 TEST_F(OAuth2TokenServiceTest, RequestDeletedBeforeCompletion) { 284 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 285 286 scoped_ptr<OAuth2TokenService::Request> request( 287 oauth2_service_->StartRequest(account_id_, 288 OAuth2TokenService::ScopeSet(), 289 &consumer_)); 290 base::RunLoop().RunUntilIdle(); 291 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 292 EXPECT_EQ(0, consumer_.number_of_errors_); 293 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 294 ASSERT_TRUE(fetcher); 295 296 request.reset(); 297 298 fetcher->set_response_code(net::HTTP_OK); 299 fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); 300 fetcher->delegate()->OnURLFetchComplete(fetcher); 301 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 302 EXPECT_EQ(0, consumer_.number_of_errors_); 303 } 304 305 TEST_F(OAuth2TokenServiceTest, RequestDeletedAfterCompletion) { 306 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 307 308 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest( 309 account_id_, OAuth2TokenService::ScopeSet(), &consumer_)); 310 base::RunLoop().RunUntilIdle(); 311 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 312 ASSERT_TRUE(fetcher); 313 fetcher->set_response_code(net::HTTP_OK); 314 fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); 315 fetcher->delegate()->OnURLFetchComplete(fetcher); 316 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 317 EXPECT_EQ(0, consumer_.number_of_errors_); 318 EXPECT_EQ("token", consumer_.last_token_); 319 320 request.reset(); 321 322 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 323 EXPECT_EQ(0, consumer_.number_of_errors_); 324 EXPECT_EQ("token", consumer_.last_token_); 325 } 326 327 TEST_F(OAuth2TokenServiceTest, MultipleRequestsForTheSameScopesWithOneDeleted) { 328 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 329 330 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest( 331 account_id_, OAuth2TokenService::ScopeSet(), &consumer_)); 332 base::RunLoop().RunUntilIdle(); 333 scoped_ptr<OAuth2TokenService::Request> request2( 334 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(), 335 &consumer_)); 336 base::RunLoop().RunUntilIdle(); 337 338 request.reset(); 339 340 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 341 ASSERT_TRUE(fetcher); 342 fetcher->set_response_code(net::HTTP_OK); 343 fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); 344 fetcher->delegate()->OnURLFetchComplete(fetcher); 345 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 346 EXPECT_EQ(0, consumer_.number_of_errors_); 347 } 348 349 TEST_F(OAuth2TokenServiceTest, ClearedRefreshTokenFailsSubsequentRequests) { 350 // We have a valid refresh token; the first request is successful. 351 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 352 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest( 353 account_id_, OAuth2TokenService::ScopeSet(), &consumer_)); 354 base::RunLoop().RunUntilIdle(); 355 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 356 ASSERT_TRUE(fetcher); 357 fetcher->set_response_code(net::HTTP_OK); 358 fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); 359 fetcher->delegate()->OnURLFetchComplete(fetcher); 360 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 361 EXPECT_EQ(0, consumer_.number_of_errors_); 362 EXPECT_EQ("token", consumer_.last_token_); 363 364 // The refresh token is no longer available; subsequent requests fail. 365 oauth2_service_->set_refresh_token(account_id_, ""); 366 request = oauth2_service_->StartRequest(account_id_, 367 OAuth2TokenService::ScopeSet(), &consumer_); 368 base::RunLoop().RunUntilIdle(); 369 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 370 EXPECT_EQ(1, consumer_.number_of_errors_); 371 } 372 373 TEST_F(OAuth2TokenServiceTest, 374 ChangedRefreshTokenDoesNotAffectInFlightRequests) { 375 oauth2_service_->set_refresh_token(account_id_, "first refreshToken"); 376 OAuth2TokenService::ScopeSet scopes; 377 scopes.insert("s1"); 378 scopes.insert("s2"); 379 OAuth2TokenService::ScopeSet scopes1; 380 scopes.insert("s3"); 381 scopes.insert("s4"); 382 383 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest( 384 account_id_, scopes, &consumer_)); 385 base::RunLoop().RunUntilIdle(); 386 net::TestURLFetcher* fetcher1 = factory_.GetFetcherByID(0); 387 ASSERT_TRUE(fetcher1); 388 389 // Note |request| is still pending when the refresh token changes. 390 oauth2_service_->set_refresh_token(account_id_, "second refreshToken"); 391 392 // A 2nd request (using the new refresh token) that occurs and completes 393 // while the 1st request is in flight is successful. 394 TestingOAuth2TokenServiceConsumer consumer2; 395 scoped_ptr<OAuth2TokenService::Request> request2( 396 oauth2_service_->StartRequest(account_id_, scopes1, &consumer2)); 397 base::RunLoop().RunUntilIdle(); 398 399 net::TestURLFetcher* fetcher2 = factory_.GetFetcherByID(0); 400 fetcher2->set_response_code(net::HTTP_OK); 401 fetcher2->SetResponseString(GetValidTokenResponse("second token", 3600)); 402 fetcher2->delegate()->OnURLFetchComplete(fetcher2); 403 EXPECT_EQ(1, consumer2.number_of_successful_tokens_); 404 EXPECT_EQ(0, consumer2.number_of_errors_); 405 EXPECT_EQ("second token", consumer2.last_token_); 406 407 fetcher1->set_response_code(net::HTTP_OK); 408 fetcher1->SetResponseString(GetValidTokenResponse("first token", 3600)); 409 fetcher1->delegate()->OnURLFetchComplete(fetcher1); 410 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 411 EXPECT_EQ(0, consumer_.number_of_errors_); 412 EXPECT_EQ("first token", consumer_.last_token_); 413 } 414 415 TEST_F(OAuth2TokenServiceTest, ServiceShutDownBeforeFetchComplete) { 416 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 417 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest( 418 account_id_, OAuth2TokenService::ScopeSet(), &consumer_)); 419 base::RunLoop().RunUntilIdle(); 420 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 421 EXPECT_EQ(0, consumer_.number_of_errors_); 422 423 // The destructor should cancel all in-flight fetchers. 424 oauth2_service_.reset(NULL); 425 426 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 427 EXPECT_EQ(1, consumer_.number_of_errors_); 428 } 429 430 TEST_F(OAuth2TokenServiceTest, RetryingConsumer) { 431 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 432 RetryingTestingOAuth2TokenServiceConsumer consumer(oauth2_service_.get(), 433 account_id_); 434 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest( 435 account_id_, OAuth2TokenService::ScopeSet(), &consumer)); 436 base::RunLoop().RunUntilIdle(); 437 EXPECT_EQ(0, consumer.number_of_successful_tokens_); 438 EXPECT_EQ(0, consumer.number_of_errors_); 439 440 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 441 ASSERT_TRUE(fetcher); 442 fetcher->set_response_code(net::HTTP_UNAUTHORIZED); 443 fetcher->SetResponseString(std::string()); 444 fetcher->delegate()->OnURLFetchComplete(fetcher); 445 EXPECT_EQ(0, consumer.number_of_successful_tokens_); 446 EXPECT_EQ(1, consumer.number_of_errors_); 447 448 fetcher = factory_.GetFetcherByID(0); 449 ASSERT_TRUE(fetcher); 450 fetcher->set_response_code(net::HTTP_UNAUTHORIZED); 451 fetcher->SetResponseString(std::string()); 452 fetcher->delegate()->OnURLFetchComplete(fetcher); 453 EXPECT_EQ(0, consumer.number_of_successful_tokens_); 454 EXPECT_EQ(2, consumer.number_of_errors_); 455 } 456 457 TEST_F(OAuth2TokenServiceTest, InvalidateToken) { 458 OAuth2TokenService::ScopeSet scopes; 459 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 460 461 // First request. 462 scoped_ptr<OAuth2TokenService::Request> request( 463 oauth2_service_->StartRequest(account_id_, scopes, &consumer_)); 464 base::RunLoop().RunUntilIdle(); 465 466 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 467 EXPECT_EQ(0, consumer_.number_of_errors_); 468 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 469 ASSERT_TRUE(fetcher); 470 fetcher->set_response_code(net::HTTP_OK); 471 fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); 472 fetcher->delegate()->OnURLFetchComplete(fetcher); 473 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 474 EXPECT_EQ(0, consumer_.number_of_errors_); 475 EXPECT_EQ("token", consumer_.last_token_); 476 477 // Second request, should return the same token without needing a network 478 // request. 479 scoped_ptr<OAuth2TokenService::Request> request2( 480 oauth2_service_->StartRequest(account_id_, scopes, &consumer_)); 481 base::RunLoop().RunUntilIdle(); 482 483 // No new network fetcher. 484 EXPECT_EQ(2, consumer_.number_of_successful_tokens_); 485 EXPECT_EQ(0, consumer_.number_of_errors_); 486 EXPECT_EQ("token", consumer_.last_token_); 487 488 // Invalidating the token should return a new token on the next request. 489 oauth2_service_->InvalidateToken(account_id_, scopes, consumer_.last_token_); 490 scoped_ptr<OAuth2TokenService::Request> request3( 491 oauth2_service_->StartRequest(account_id_, scopes, &consumer_)); 492 base::RunLoop().RunUntilIdle(); 493 EXPECT_EQ(2, consumer_.number_of_successful_tokens_); 494 EXPECT_EQ(0, consumer_.number_of_errors_); 495 fetcher = factory_.GetFetcherByID(0); 496 ASSERT_TRUE(fetcher); 497 fetcher->set_response_code(net::HTTP_OK); 498 fetcher->SetResponseString(GetValidTokenResponse("token2", 3600)); 499 fetcher->delegate()->OnURLFetchComplete(fetcher); 500 EXPECT_EQ(3, consumer_.number_of_successful_tokens_); 501 EXPECT_EQ(0, consumer_.number_of_errors_); 502 EXPECT_EQ("token2", consumer_.last_token_); 503 } 504 505 TEST_F(OAuth2TokenServiceTest, CancelAllRequests) { 506 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 507 scoped_ptr<OAuth2TokenService::Request> request( 508 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(), 509 &consumer_)); 510 511 oauth2_service_->set_refresh_token("account_id_2", "refreshToken2"); 512 scoped_ptr<OAuth2TokenService::Request> request2( 513 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(), 514 &consumer_)); 515 516 base::RunLoop().RunUntilIdle(); 517 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 518 EXPECT_EQ(0, consumer_.number_of_errors_); 519 520 oauth2_service_->CancelAllRequestsForTest(); 521 522 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 523 EXPECT_EQ(2, consumer_.number_of_errors_); 524 } 525 526 TEST_F(OAuth2TokenServiceTest, CancelRequestsForAccount) { 527 OAuth2TokenService::ScopeSet scope_set_1; 528 scope_set_1.insert("scope1"); 529 scope_set_1.insert("scope2"); 530 OAuth2TokenService::ScopeSet scope_set_2(scope_set_1.begin(), 531 scope_set_1.end()); 532 scope_set_2.insert("scope3"); 533 534 oauth2_service_->set_refresh_token(account_id_, "refreshToken"); 535 scoped_ptr<OAuth2TokenService::Request> request1( 536 oauth2_service_->StartRequest(account_id_, scope_set_1, &consumer_)); 537 scoped_ptr<OAuth2TokenService::Request> request2( 538 oauth2_service_->StartRequest(account_id_, scope_set_2, &consumer_)); 539 540 std::string account_id_2("account_id_2"); 541 oauth2_service_->set_refresh_token(account_id_2, "refreshToken2"); 542 scoped_ptr<OAuth2TokenService::Request> request3( 543 oauth2_service_->StartRequest(account_id_2, scope_set_1, &consumer_)); 544 545 base::RunLoop().RunUntilIdle(); 546 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 547 EXPECT_EQ(0, consumer_.number_of_errors_); 548 549 oauth2_service_->CancelRequestsForAccountForTest(account_id_); 550 551 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 552 EXPECT_EQ(2, consumer_.number_of_errors_); 553 554 oauth2_service_->CancelRequestsForAccountForTest(account_id_2); 555 556 EXPECT_EQ(0, consumer_.number_of_successful_tokens_); 557 EXPECT_EQ(3, consumer_.number_of_errors_); 558 } 559 560 TEST_F(OAuth2TokenServiceTest, SameScopesRequestedForDifferentClients) { 561 std::string client_id_1("client1"); 562 std::string client_secret_1("secret1"); 563 std::string client_id_2("client2"); 564 std::string client_secret_2("secret2"); 565 std::set<std::string> scope_set; 566 scope_set.insert("scope1"); 567 scope_set.insert("scope2"); 568 569 std::string refresh_token("refreshToken"); 570 oauth2_service_->set_refresh_token(account_id_, refresh_token); 571 572 scoped_ptr<OAuth2TokenService::Request> request1( 573 oauth2_service_->StartRequestForClient(account_id_, 574 client_id_1, 575 client_secret_1, 576 scope_set, 577 &consumer_)); 578 scoped_ptr<OAuth2TokenService::Request> request2( 579 oauth2_service_->StartRequestForClient(account_id_, 580 client_id_2, 581 client_secret_2, 582 scope_set, 583 &consumer_)); 584 // Start a request that should be duplicate of |request1|. 585 scoped_ptr<OAuth2TokenService::Request> request3( 586 oauth2_service_->StartRequestForClient(account_id_, 587 client_id_1, 588 client_secret_1, 589 scope_set, 590 &consumer_)); 591 base::RunLoop().RunUntilIdle(); 592 593 ASSERT_EQ(2U, 594 oauth2_service_->GetNumPendingRequestsForTesting( 595 client_id_1, 596 account_id_, 597 scope_set)); 598 ASSERT_EQ(1U, 599 oauth2_service_->GetNumPendingRequestsForTesting( 600 client_id_2, 601 account_id_, 602 scope_set)); 603 } 604 605 TEST_F(OAuth2TokenServiceTest, RequestParametersOrderTest) { 606 OAuth2TokenService::ScopeSet set_0; 607 OAuth2TokenService::ScopeSet set_1; 608 set_1.insert("1"); 609 610 OAuth2TokenService::RequestParameters params[] = { 611 OAuth2TokenService::RequestParameters("0", "0", set_0), 612 OAuth2TokenService::RequestParameters("0", "0", set_1), 613 OAuth2TokenService::RequestParameters("0", "1", set_0), 614 OAuth2TokenService::RequestParameters("0", "1", set_1), 615 OAuth2TokenService::RequestParameters("1", "0", set_0), 616 OAuth2TokenService::RequestParameters("1", "0", set_1), 617 OAuth2TokenService::RequestParameters("1", "1", set_0), 618 OAuth2TokenService::RequestParameters("1", "1", set_1), 619 }; 620 621 for (size_t i = 0; i < arraysize(params); i++) { 622 for (size_t j = 0; j < arraysize(params); j++) { 623 if (i == j) { 624 EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j; 625 EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j; 626 } else if (i < j) { 627 EXPECT_TRUE(params[i] < params[j]) << " i=" << i << ", j=" << j; 628 EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j; 629 } else { 630 EXPECT_TRUE(params[j] < params[i]) << " i=" << i << ", j=" << j; 631 EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j; 632 } 633 } 634 } 635 } 636