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