| OLD | NEW |
| 1 // Copyright (c) 2011 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 // This file defines a unit test for the profile's token service. | 5 // This file defines a unit test for the profile's token service. |
| 6 | 6 |
| 7 #include "chrome/browser/net/gaia/token_service_unittest.h" | 7 #include "chrome/browser/net/gaia/token_service_unittest.h" |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 message_loop_.RunAllPending(); | 107 message_loop_.RunAllPending(); |
| 108 } | 108 } |
| 109 | 109 |
| 110 class TokenServiceTest : public TokenServiceTestHarness { | 110 class TokenServiceTest : public TokenServiceTestHarness { |
| 111 public: | 111 public: |
| 112 virtual void SetUp() { | 112 virtual void SetUp() { |
| 113 TokenServiceTestHarness::SetUp(); | 113 TokenServiceTestHarness::SetUp(); |
| 114 service_.UpdateCredentials(credentials_); | 114 service_.UpdateCredentials(credentials_); |
| 115 service_.UpdateOAuthCredentials(oauth_token_, oauth_secret_); | 115 service_.UpdateOAuthCredentials(oauth_token_, oauth_secret_); |
| 116 } | 116 } |
| 117 protected: |
| 118 void TestLoadSingleToken( |
| 119 std::map<std::string, std::string>* db_tokens, |
| 120 std::map<std::string, std::string>* memory_tokens, |
| 121 const std::string& service) { |
| 122 std::string token = service + "_token"; |
| 123 (*db_tokens)[service] = token; |
| 124 size_t prev_success_size = success_tracker_.size(); |
| 125 service_.LoadTokensIntoMemory(*db_tokens, memory_tokens); |
| 126 |
| 127 // Check notification. |
| 128 EXPECT_EQ(prev_success_size + 1, success_tracker_.size()); |
| 129 TokenService::TokenAvailableDetails details = success_tracker_.details(); |
| 130 EXPECT_EQ(details.service(), service); |
| 131 EXPECT_EQ(details.token(), token); |
| 132 // Check memory tokens. |
| 133 EXPECT_EQ(1U, memory_tokens->count(service)); |
| 134 EXPECT_EQ((*memory_tokens)[service], token); |
| 135 } |
| 117 }; | 136 }; |
| 118 | 137 |
| 119 TEST_F(TokenServiceTest, SanityCheck) { | 138 TEST_F(TokenServiceTest, SanityCheck) { |
| 120 EXPECT_TRUE(service_.HasLsid()); | 139 EXPECT_TRUE(service_.HasLsid()); |
| 121 EXPECT_EQ(service_.GetLsid(), "lsid"); | 140 EXPECT_EQ(service_.GetLsid(), "lsid"); |
| 122 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); | 141 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); |
| 123 EXPECT_TRUE(service_.HasOAuthCredentials()); | 142 EXPECT_TRUE(service_.HasOAuthCredentials()); |
| 124 EXPECT_EQ(service_.GetOAuthToken(), "oauth"); | 143 EXPECT_EQ(service_.GetOAuthToken(), "oauth"); |
| 125 EXPECT_EQ(service_.GetOAuthSecret(), "secret"); | 144 EXPECT_EQ(service_.GetOAuthSecret(), "secret"); |
| 126 } | 145 } |
| 127 | 146 |
| 128 TEST_F(TokenServiceTest, NoToken) { | 147 TEST_F(TokenServiceTest, NoToken) { |
| 129 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); | 148 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); |
| 130 EXPECT_EQ(service_.GetTokenForService("nonexistent service"), std::string()); | 149 EXPECT_EQ(service_.GetTokenForService("nonexistent service"), std::string()); |
| 131 } | 150 } |
| 132 | 151 |
| 133 TEST_F(TokenServiceTest, NotificationSuccess) { | 152 TEST_F(TokenServiceTest, NotificationSuccess) { |
| 134 EXPECT_EQ(0U, success_tracker_.size()); | 153 EXPECT_EQ(0U, success_tracker_.size()); |
| 135 EXPECT_EQ(0U, failure_tracker_.size()); | 154 EXPECT_EQ(0U, failure_tracker_.size()); |
| 136 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 155 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 137 EXPECT_EQ(1U, success_tracker_.size()); | 156 EXPECT_EQ(1U, success_tracker_.size()); |
| 138 EXPECT_EQ(0U, failure_tracker_.size()); | 157 EXPECT_EQ(0U, failure_tracker_.size()); |
| 139 | 158 |
| 140 TokenService::TokenAvailableDetails details = success_tracker_.details(); | 159 TokenService::TokenAvailableDetails details = success_tracker_.details(); |
| 141 // MSVC doesn't like this comparison as EQ. | 160 // MSVC doesn't like this comparison as EQ. |
| 142 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); | 161 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); |
| 143 EXPECT_EQ(details.token(), "token"); | 162 EXPECT_EQ(details.token(), "token"); |
| 144 } | 163 } |
| 145 | 164 |
| 165 TEST_F(TokenServiceTest, NotificationOAuthLoginTokenSuccess) { |
| 166 EXPECT_EQ(0U, success_tracker_.size()); |
| 167 EXPECT_EQ(0U, failure_tracker_.size()); |
| 168 service_.OnOAuthLoginTokenSuccess("rt1", "at1", 3600); |
| 169 EXPECT_EQ(1U, success_tracker_.size()); |
| 170 EXPECT_EQ(0U, failure_tracker_.size()); |
| 171 |
| 172 TokenService::TokenAvailableDetails details = success_tracker_.details(); |
| 173 // MSVC doesn't like this comparison as EQ. |
| 174 EXPECT_TRUE(details.service() == |
| 175 GaiaConstants::kGaiaOAuth2LoginRefreshToken); |
| 176 EXPECT_EQ(details.token(), "rt1"); |
| 177 } |
| 178 |
| 146 TEST_F(TokenServiceTest, NotificationSuccessOAuth) { | 179 TEST_F(TokenServiceTest, NotificationSuccessOAuth) { |
| 147 EXPECT_EQ(0U, success_tracker_.size()); | 180 EXPECT_EQ(0U, success_tracker_.size()); |
| 148 EXPECT_EQ(0U, failure_tracker_.size()); | 181 EXPECT_EQ(0U, failure_tracker_.size()); |
| 149 service_.OnOAuthWrapBridgeSuccess( | 182 service_.OnOAuthWrapBridgeSuccess( |
| 150 GaiaConstants::kSyncServiceOAuth, "token", "3600"); | 183 GaiaConstants::kSyncServiceOAuth, "token", "3600"); |
| 151 EXPECT_EQ(1U, success_tracker_.size()); | 184 EXPECT_EQ(1U, success_tracker_.size()); |
| 152 EXPECT_EQ(0U, failure_tracker_.size()); | 185 EXPECT_EQ(0U, failure_tracker_.size()); |
| 153 | 186 |
| 154 TokenService::TokenAvailableDetails details = success_tracker_.details(); | 187 TokenService::TokenAvailableDetails details = success_tracker_.details(); |
| 155 // MSVC doesn't like this comparison as EQ. | 188 // MSVC doesn't like this comparison as EQ. |
| 156 EXPECT_TRUE(details.service() == GaiaConstants::kSyncServiceOAuth); | 189 EXPECT_TRUE(details.service() == GaiaConstants::kSyncServiceOAuth); |
| 157 EXPECT_EQ(details.token(), "token"); | 190 EXPECT_EQ(details.token(), "token"); |
| 158 } | 191 } |
| 159 | 192 |
| 160 TEST_F(TokenServiceTest, NotificationFailed) { | 193 TEST_F(TokenServiceTest, NotificationFailed) { |
| 161 EXPECT_EQ(0U, success_tracker_.size()); | 194 EXPECT_EQ(0U, success_tracker_.size()); |
| 162 EXPECT_EQ(0U, failure_tracker_.size()); | 195 EXPECT_EQ(0U, failure_tracker_.size()); |
| 163 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 196 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 164 service_.OnIssueAuthTokenFailure(GaiaConstants::kSyncService, error); | 197 service_.OnIssueAuthTokenFailure(GaiaConstants::kSyncService, error); |
| 165 EXPECT_EQ(0U, success_tracker_.size()); | 198 EXPECT_EQ(0U, success_tracker_.size()); |
| 166 EXPECT_EQ(1U, failure_tracker_.size()); | 199 EXPECT_EQ(1U, failure_tracker_.size()); |
| 167 | 200 |
| 168 TokenService::TokenRequestFailedDetails details = failure_tracker_.details(); | 201 TokenService::TokenRequestFailedDetails details = failure_tracker_.details(); |
| 169 | 202 |
| 170 // MSVC doesn't like this comparison as EQ. | 203 // MSVC doesn't like this comparison as EQ. |
| 171 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); | 204 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); |
| 172 EXPECT_TRUE(details.error() == error); // Struct has no print function. | 205 EXPECT_TRUE(details.error() == error); // Struct has no print function. |
| 173 } | 206 } |
| 174 | 207 |
| 208 TEST_F(TokenServiceTest, NotificationOAuthLoginTokenFailed) { |
| 209 EXPECT_EQ(0U, success_tracker_.size()); |
| 210 EXPECT_EQ(0U, failure_tracker_.size()); |
| 211 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 212 service_.OnOAuthLoginTokenFailure(error); |
| 213 EXPECT_EQ(0U, success_tracker_.size()); |
| 214 EXPECT_EQ(1U, failure_tracker_.size()); |
| 215 |
| 216 TokenService::TokenRequestFailedDetails details = failure_tracker_.details(); |
| 217 |
| 218 // MSVC doesn't like this comparison as EQ. |
| 219 EXPECT_TRUE(details.service() == |
| 220 GaiaConstants::kGaiaOAuth2LoginRefreshToken); |
| 221 EXPECT_TRUE(details.error() == error); // Struct has no print function. |
| 222 } |
| 223 |
| 175 TEST_F(TokenServiceTest, NotificationFailedOAuth) { | 224 TEST_F(TokenServiceTest, NotificationFailedOAuth) { |
| 176 EXPECT_EQ(0U, success_tracker_.size()); | 225 EXPECT_EQ(0U, success_tracker_.size()); |
| 177 EXPECT_EQ(0U, failure_tracker_.size()); | 226 EXPECT_EQ(0U, failure_tracker_.size()); |
| 178 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 227 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 179 service_.OnOAuthWrapBridgeFailure(GaiaConstants::kSyncServiceOAuth, error); | 228 service_.OnOAuthWrapBridgeFailure(GaiaConstants::kSyncServiceOAuth, error); |
| 180 EXPECT_EQ(0U, success_tracker_.size()); | 229 EXPECT_EQ(0U, success_tracker_.size()); |
| 181 EXPECT_EQ(1U, failure_tracker_.size()); | 230 EXPECT_EQ(1U, failure_tracker_.size()); |
| 182 | 231 |
| 183 TokenService::TokenRequestFailedDetails details = failure_tracker_.details(); | 232 TokenService::TokenRequestFailedDetails details = failure_tracker_.details(); |
| 184 | 233 |
| 185 // MSVC doesn't like this comparison as EQ. | 234 // MSVC doesn't like this comparison as EQ. |
| 186 EXPECT_TRUE(details.service() == GaiaConstants::kSyncServiceOAuth); | 235 EXPECT_TRUE(details.service() == GaiaConstants::kSyncServiceOAuth); |
| 187 EXPECT_TRUE(details.error() == error); // Struct has no print function. | 236 EXPECT_TRUE(details.error() == error); // Struct has no print function. |
| 188 } | 237 } |
| 189 | 238 |
| 190 TEST_F(TokenServiceTest, OnTokenSuccessUpdate) { | 239 TEST_F(TokenServiceTest, OnTokenSuccessUpdate) { |
| 191 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 240 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 192 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 241 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 193 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); | 242 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); |
| 194 | 243 |
| 195 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token2"); | 244 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token2"); |
| 196 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 245 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 197 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token2"); | 246 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token2"); |
| 198 | 247 |
| 199 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, ""); | 248 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, ""); |
| 200 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 249 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 201 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), ""); | 250 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), ""); |
| 202 } | 251 } |
| 203 | 252 |
| 253 TEST_F(TokenServiceTest, OnOAuth2LoginTokenSuccessUpdate) { |
| 254 std::string service = GaiaConstants::kGaiaOAuth2LoginRefreshToken; |
| 255 service_.OnOAuthLoginTokenSuccess("rt1", "at1", 3600); |
| 256 EXPECT_TRUE(service_.HasOAuthLoginToken()); |
| 257 EXPECT_EQ(service_.GetOAuth2LoginRefreshToken(), "rt1"); |
| 258 |
| 259 service_.OnOAuthLoginTokenSuccess("rt2", "at2", 3600); |
| 260 EXPECT_TRUE(service_.HasOAuthLoginToken()); |
| 261 EXPECT_EQ(service_.GetOAuth2LoginRefreshToken(), "rt2"); |
| 262 |
| 263 service_.OnOAuthLoginTokenSuccess("rt3", "at3", 3600); |
| 264 EXPECT_TRUE(service_.HasOAuthLoginToken()); |
| 265 EXPECT_EQ(service_.GetOAuth2LoginRefreshToken(), "rt3"); |
| 266 } |
| 267 |
| 204 TEST_F(TokenServiceTest, OnTokenSuccess) { | 268 TEST_F(TokenServiceTest, OnTokenSuccess) { |
| 205 // Don't "start fetching", just go ahead and issue the callback. | 269 // Don't "start fetching", just go ahead and issue the callback. |
| 206 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 270 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 207 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 271 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 208 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); | 272 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 209 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 273 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 210 // Gaia returns the entire result as the token so while this is a shared | 274 // Gaia returns the entire result as the token so while this is a shared |
| 211 // result with ClientLogin, it doesn't matter, we should still get it back. | 275 // result with ClientLogin, it doesn't matter, we should still get it back. |
| 212 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); | 276 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); |
| 213 | 277 |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 TEST_F(TokenServiceTest, LoadTokensIntoMemoryBasic) { | 434 TEST_F(TokenServiceTest, LoadTokensIntoMemoryBasic) { |
| 371 // Validate that the method sets proper data in notifications and map. | 435 // Validate that the method sets proper data in notifications and map. |
| 372 std::map<std::string, std::string> db_tokens; | 436 std::map<std::string, std::string> db_tokens; |
| 373 std::map<std::string, std::string> memory_tokens; | 437 std::map<std::string, std::string> memory_tokens; |
| 374 | 438 |
| 375 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); | 439 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); |
| 376 EXPECT_TRUE(db_tokens.empty()); | 440 EXPECT_TRUE(db_tokens.empty()); |
| 377 EXPECT_TRUE(memory_tokens.empty()); | 441 EXPECT_TRUE(memory_tokens.empty()); |
| 378 EXPECT_EQ(0U, success_tracker_.size()); | 442 EXPECT_EQ(0U, success_tracker_.size()); |
| 379 | 443 |
| 380 db_tokens[GaiaConstants::kSyncServiceOAuth] = "token"; | 444 std::string service; |
| 381 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); | 445 std::string token; |
| 382 EXPECT_EQ(1U, success_tracker_.size()); | 446 for (int i = 0; i < TokenService::kNumServices; ++i) { |
| 383 | 447 service = TokenService::kServices[i]; |
| 384 TokenService::TokenAvailableDetails details = success_tracker_.details(); | 448 TestLoadSingleToken(&db_tokens, &memory_tokens, service); |
| 385 // MSVC doesn't like this comparison as EQ. | 449 } |
| 386 EXPECT_TRUE(details.service() == GaiaConstants::kSyncServiceOAuth); | 450 for (int i = 0; i < TokenService::kNumOAuthServices; ++i) { |
| 387 EXPECT_EQ(details.token(), "token"); | 451 service = TokenService::kOAuthServices[i]; |
| 388 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncServiceOAuth)); | 452 TestLoadSingleToken(&db_tokens, &memory_tokens, service); |
| 389 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncServiceOAuth], "token"); | 453 } |
| 454 service = GaiaConstants::kGaiaOAuth2LoginRefreshToken; |
| 455 TestLoadSingleToken(&db_tokens, &memory_tokens, service); |
| 456 service = GaiaConstants::kGaiaOAuth2LoginAccessToken; |
| 457 TestLoadSingleToken(&db_tokens, &memory_tokens, service); |
| 390 } | 458 } |
| 391 | 459 |
| 392 TEST_F(TokenServiceTest, LoadTokensIntoMemoryAdvanced) { | 460 TEST_F(TokenServiceTest, LoadTokensIntoMemoryAdvanced) { |
| 393 // LoadTokensIntoMemory should avoid setting tokens already in the | 461 // LoadTokensIntoMemory should avoid setting tokens already in the |
| 394 // token map. | 462 // token map. |
| 395 std::map<std::string, std::string> db_tokens; | 463 std::map<std::string, std::string> db_tokens; |
| 396 std::map<std::string, std::string> memory_tokens; | 464 std::map<std::string, std::string> memory_tokens; |
| 397 | 465 |
| 398 db_tokens["ignore"] = "token"; | 466 db_tokens["ignore"] = "token"; |
| 399 | 467 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 | 577 |
| 510 *CommandLine::ForCurrentProcess() = original_cl; | 578 *CommandLine::ForCurrentProcess() = original_cl; |
| 511 } | 579 } |
| 512 }; | 580 }; |
| 513 | 581 |
| 514 TEST_F(TokenServiceCommandLineTest, TestValueOverride) { | 582 TEST_F(TokenServiceCommandLineTest, TestValueOverride) { |
| 515 EXPECT_TRUE(service_.HasTokenForService("my_service")); | 583 EXPECT_TRUE(service_.HasTokenForService("my_service")); |
| 516 EXPECT_EQ("my_value", service_.GetTokenForService("my_service")); | 584 EXPECT_EQ("my_value", service_.GetTokenForService("my_service")); |
| 517 } | 585 } |
| 518 #endif // ifndef NDEBUG | 586 #endif // ifndef NDEBUG |
| OLD | NEW |