diff options
Diffstat (limited to 'google_apis/gaia/oauth2_token_service_unittest.cc')
-rw-r--r-- | google_apis/gaia/oauth2_token_service_unittest.cc | 155 |
1 files changed, 135 insertions, 20 deletions
diff --git a/google_apis/gaia/oauth2_token_service_unittest.cc b/google_apis/gaia/oauth2_token_service_unittest.cc index 9937268..8f4e922 100644 --- a/google_apis/gaia/oauth2_token_service_unittest.cc +++ b/google_apis/gaia/oauth2_token_service_unittest.cc @@ -9,6 +9,7 @@ #include "google_apis/gaia/gaia_constants.h" #include "google_apis/gaia/google_service_auth_error.h" #include "google_apis/gaia/oauth2_access_token_consumer.h" +#include "google_apis/gaia/oauth2_access_token_fetcher.h" #include "google_apis/gaia/oauth2_token_service.h" #include "google_apis/gaia/oauth2_token_service_test_util.h" #include "net/http/http_status_code.h" @@ -78,6 +79,7 @@ class OAuth2TokenServiceTest : public testing::Test { virtual void TearDown() OVERRIDE { // Makes sure that all the clean up tasks are run. base::RunLoop().RunUntilIdle(); + OAuth2AccessTokenFetcher::ResetLastFetcherIdForTest(); } protected: @@ -105,7 +107,7 @@ TEST_F(OAuth2TokenServiceTest, FailureShouldNotRetry) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - EXPECT_TRUE(fetcher); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_UNAUTHORIZED); fetcher->SetResponseString(std::string()); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -123,7 +125,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithoutCaching) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - EXPECT_TRUE(fetcher); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -152,7 +154,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - EXPECT_TRUE(fetcher); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -167,7 +169,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) { base::RunLoop().RunUntilIdle(); // No new network fetcher. - EXPECT_EQ(fetcher, factory_.GetFetcherByID(0)); + EXPECT_EQ(1U, factory_.GetFetcherCount()); EXPECT_EQ(2, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); EXPECT_EQ("token", consumer_.last_token_); @@ -178,8 +180,9 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) { base::RunLoop().RunUntilIdle(); EXPECT_EQ(2, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); - fetcher = factory_.GetFetcherByID(0); - EXPECT_TRUE(fetcher); + ASSERT_EQ(2U, factory_.GetFetcherCount()); + fetcher = factory_.GetFetcherByID(1); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token2", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -198,7 +201,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndFailure) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - EXPECT_TRUE(fetcher); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 0)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -214,8 +217,9 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndFailure) { EXPECT_EQ(0, consumer_.number_of_errors_); // Network failure. - fetcher = factory_.GetFetcherByID(0); - EXPECT_TRUE(fetcher); + ASSERT_EQ(2U, factory_.GetFetcherCount()); + fetcher = factory_.GetFetcherByID(1); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_UNAUTHORIZED); fetcher->SetResponseString(std::string()); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -233,7 +237,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndSuccess) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - EXPECT_TRUE(fetcher); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 0)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -248,8 +252,9 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndSuccess) { EXPECT_EQ(1, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); - fetcher = factory_.GetFetcherByID(0); - EXPECT_TRUE(fetcher); + ASSERT_EQ(2U, factory_.GetFetcherCount()); + fetcher = factory_.GetFetcherByID(1); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("another token", 0)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -267,7 +272,7 @@ TEST_F(OAuth2TokenServiceTest, RequestDeletedBeforeCompletion) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - EXPECT_TRUE(fetcher); + ASSERT_TRUE(fetcher); request.reset(); @@ -285,6 +290,7 @@ TEST_F(OAuth2TokenServiceTest, RequestDeletedAfterCompletion) { std::set<std::string>(), &consumer_)); base::RunLoop().RunUntilIdle(); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -312,6 +318,7 @@ TEST_F(OAuth2TokenServiceTest, MultipleRequestsForTheSameScopesWithOneDeleted) { request.reset(); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -326,6 +333,7 @@ TEST_F(OAuth2TokenServiceTest, ClearedRefreshTokenFailsSubsequentRequests) { std::set<std::string>(), &consumer_)); base::RunLoop().RunUntilIdle(); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -337,7 +345,7 @@ TEST_F(OAuth2TokenServiceTest, ClearedRefreshTokenFailsSubsequentRequests) { oauth2_service_->set_refresh_token(""); request = oauth2_service_->StartRequest(std::set<std::string>(), &consumer_); base::RunLoop().RunUntilIdle(); - EXPECT_EQ(fetcher, factory_.GetFetcherByID(0)); + EXPECT_EQ(1U, factory_.GetFetcherCount()); EXPECT_EQ(1, consumer_.number_of_successful_tokens_); EXPECT_EQ(1, consumer_.number_of_errors_); } @@ -352,7 +360,9 @@ TEST_F(OAuth2TokenServiceTest, scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest( scopes, &consumer_)); base::RunLoop().RunUntilIdle(); + ASSERT_EQ(1U, factory_.GetFetcherCount()); net::TestURLFetcher* fetcher1 = factory_.GetFetcherByID(0); + ASSERT_TRUE(fetcher1); // Note |request| is still pending when the refresh token changes. oauth2_service_->set_refresh_token("second refreshToken"); @@ -364,7 +374,8 @@ TEST_F(OAuth2TokenServiceTest, oauth2_service_->StartRequest(scopes, &consumer2)); base::RunLoop().RunUntilIdle(); - net::TestURLFetcher* fetcher2 = factory_.GetFetcherByID(0); + ASSERT_EQ(2U, factory_.GetFetcherCount()); + net::TestURLFetcher* fetcher2 = factory_.GetFetcherByID(1); fetcher2->set_response_code(net::HTTP_OK); fetcher2->SetResponseString(GetValidTokenResponse("second token", 3600)); fetcher2->delegate()->OnURLFetchComplete(fetcher2); @@ -412,7 +423,8 @@ TEST_F(OAuth2TokenServiceTest, RetryingConsumer) { EXPECT_EQ(0, consumer.number_of_successful_tokens_); EXPECT_EQ(1, consumer.number_of_errors_); - fetcher = factory_.GetFetcherByID(0); + ASSERT_EQ(2U, factory_.GetFetcherCount()); + fetcher = factory_.GetFetcherByID(1); ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_UNAUTHORIZED); fetcher->SetResponseString(std::string()); @@ -433,7 +445,7 @@ TEST_F(OAuth2TokenServiceTest, InvalidateToken) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - EXPECT_TRUE(fetcher); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -448,7 +460,7 @@ TEST_F(OAuth2TokenServiceTest, InvalidateToken) { base::RunLoop().RunUntilIdle(); // No new network fetcher. - EXPECT_EQ(fetcher, factory_.GetFetcherByID(0)); + ASSERT_EQ(1U, factory_.GetFetcherCount()); EXPECT_EQ(2, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); EXPECT_EQ("token", consumer_.last_token_); @@ -460,8 +472,9 @@ TEST_F(OAuth2TokenServiceTest, InvalidateToken) { base::RunLoop().RunUntilIdle(); EXPECT_EQ(2, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); - fetcher = factory_.GetFetcherByID(0); - EXPECT_TRUE(fetcher); + ASSERT_EQ(2U, factory_.GetFetcherCount()); + fetcher = factory_.GetFetcherByID(1); + ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token2", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -520,3 +533,105 @@ TEST_F(OAuth2TokenServiceTest, CancelRequestsForToken) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(3, consumer_.number_of_errors_); } + +TEST_F(OAuth2TokenServiceTest, SameScopesRequestedForDifferentClients) { + std::string client_id_1("client1"); + std::string client_secret_1("secret1"); + std::string client_id_2("client2"); + std::string client_secret_2("secret2"); + std::set<std::string> scope_set; + scope_set.insert("scope1"); + scope_set.insert("scope2"); + + std::string refresh_token("refreshToken"); + oauth2_service_->set_refresh_token(refresh_token); + + scoped_ptr<OAuth2TokenService::Request> request1( + oauth2_service_->StartRequestForClient(client_id_1, + client_secret_1, + scope_set, + &consumer_)); + scoped_ptr<OAuth2TokenService::Request> request2( + oauth2_service_->StartRequestForClient(client_id_2, + client_secret_2, + scope_set, + &consumer_)); + // Start a request that should be duplicate of |request1|. + scoped_ptr<OAuth2TokenService::Request> request3( + oauth2_service_->StartRequestForClient(client_id_1, + client_secret_1, + scope_set, + &consumer_)); + base::RunLoop().RunUntilIdle(); + + ASSERT_EQ(2U, + oauth2_service_->GetNumPendingRequestsForTesting( + client_id_1, + refresh_token, + scope_set)); + ASSERT_EQ(1U, + oauth2_service_->GetNumPendingRequestsForTesting( + client_id_2, + refresh_token, + scope_set)); +} + +TEST_F(OAuth2TokenServiceTest, ClientScopeSetOrderTest) { + OAuth2TokenService::ScopeSet set_0; + OAuth2TokenService::ScopeSet set_1; + set_1.insert("1"); + + OAuth2TokenService::ClientScopeSet sets[] = { + OAuth2TokenService::ClientScopeSet("0", set_0), + OAuth2TokenService::ClientScopeSet("0", set_1), + OAuth2TokenService::ClientScopeSet("1", set_0), + OAuth2TokenService::ClientScopeSet("1", set_1), + }; + + for (size_t i = 0; i < arraysize(sets); i++) { + for (size_t j = 0; j < arraysize(sets); j++) { + if (i == j) { + EXPECT_FALSE(sets[i] < sets[j]) << " i=" << i << ", j=" << j; + EXPECT_FALSE(sets[j] < sets[i]) << " i=" << i << ", j=" << j; + } else if (i < j) { + EXPECT_TRUE(sets[i] < sets[j]) << " i=" << i << ", j=" << j; + EXPECT_FALSE(sets[j] < sets[i]) << " i=" << i << ", j=" << j; + } else { + EXPECT_TRUE(sets[j] < sets[i]) << " i=" << i << ", j=" << j; + EXPECT_FALSE(sets[i] < sets[j]) << " i=" << i << ", j=" << j; + } + } + } +} + +TEST_F(OAuth2TokenServiceTest, FetchParametersOrderTest) { + OAuth2TokenService::ScopeSet set_0; + OAuth2TokenService::ScopeSet set_1; + set_1.insert("1"); + + OAuth2TokenService::FetchParameters params[] = { + OAuth2TokenService::FetchParameters("0", "0", set_0), + OAuth2TokenService::FetchParameters("0", "0", set_1), + OAuth2TokenService::FetchParameters("0", "1", set_0), + OAuth2TokenService::FetchParameters("0", "1", set_1), + OAuth2TokenService::FetchParameters("1", "0", set_0), + OAuth2TokenService::FetchParameters("1", "0", set_1), + OAuth2TokenService::FetchParameters("1", "1", set_0), + OAuth2TokenService::FetchParameters("1", "1", set_1), + }; + + for (size_t i = 0; i < arraysize(params); i++) { + for (size_t j = 0; j < arraysize(params); j++) { + if (i == j) { + EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j; + EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j; + } else if (i < j) { + EXPECT_TRUE(params[i] < params[j]) << " i=" << i << ", j=" << j; + EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j; + } else { + EXPECT_TRUE(params[j] < params[i]) << " i=" << i << ", j=" << j; + EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j; + } + } + } +} |