| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 TokenServiceTestHarness::~TokenServiceTestHarness() {} | 51 TokenServiceTestHarness::~TokenServiceTestHarness() {} |
| 52 | 52 |
| 53 void TokenServiceTestHarness::SetUp() { | 53 void TokenServiceTestHarness::SetUp() { |
| 54 #if defined(OS_MACOSX) | 54 #if defined(OS_MACOSX) |
| 55 Encryptor::UseMockKeychain(true); | 55 Encryptor::UseMockKeychain(true); |
| 56 #endif | 56 #endif |
| 57 credentials_.sid = "sid"; | 57 credentials_.sid = "sid"; |
| 58 credentials_.lsid = "lsid"; | 58 credentials_.lsid = "lsid"; |
| 59 credentials_.token = "token"; | 59 credentials_.token = "token"; |
| 60 credentials_.data = "data"; | 60 credentials_.data = "data"; |
| 61 oauth_token_ = "oauth"; |
| 62 oauth_secret_ = "secret"; |
| 61 | 63 |
| 62 ASSERT_TRUE(db_thread_.Start()); | 64 ASSERT_TRUE(db_thread_.Start()); |
| 63 | 65 |
| 64 profile_.reset(new TestingProfile()); | 66 profile_.reset(new TestingProfile()); |
| 65 profile_->CreateWebDataService(false); | 67 profile_->CreateWebDataService(false); |
| 66 WaitForDBLoadCompletion(); | 68 WaitForDBLoadCompletion(); |
| 67 | 69 |
| 68 success_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_AVAILABLE, | 70 success_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 69 Source<TokenService>(&service_)); | 71 Source<TokenService>(&service_)); |
| 70 failure_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | 72 failure_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 95 | 97 |
| 96 // Notifications should be returned from the DB thread onto the UI thread. | 98 // Notifications should be returned from the DB thread onto the UI thread. |
| 97 message_loop_.RunAllPending(); | 99 message_loop_.RunAllPending(); |
| 98 } | 100 } |
| 99 | 101 |
| 100 class TokenServiceTest : public TokenServiceTestHarness { | 102 class TokenServiceTest : public TokenServiceTestHarness { |
| 101 public: | 103 public: |
| 102 virtual void SetUp() { | 104 virtual void SetUp() { |
| 103 TokenServiceTestHarness::SetUp(); | 105 TokenServiceTestHarness::SetUp(); |
| 104 service_.UpdateCredentials(credentials_); | 106 service_.UpdateCredentials(credentials_); |
| 107 service_.UpdateOAuthCredentials(oauth_token_, oauth_secret_); |
| 105 } | 108 } |
| 106 }; | 109 }; |
| 107 | 110 |
| 108 TEST_F(TokenServiceTest, SanityCheck) { | 111 TEST_F(TokenServiceTest, SanityCheck) { |
| 109 EXPECT_TRUE(service_.HasLsid()); | 112 EXPECT_TRUE(service_.HasLsid()); |
| 110 EXPECT_EQ(service_.GetLsid(), "lsid"); | 113 EXPECT_EQ(service_.GetLsid(), "lsid"); |
| 111 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); | 114 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); |
| 112 } | 115 } |
| 113 | 116 |
| 114 TEST_F(TokenServiceTest, NoToken) { | 117 TEST_F(TokenServiceTest, NoToken) { |
| 115 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); | 118 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); |
| 116 EXPECT_EQ(service_.GetTokenForService("nonexistent service"), std::string()); | 119 EXPECT_EQ(service_.GetTokenForService("nonexistent service"), std::string()); |
| 117 } | 120 } |
| 118 | 121 |
| 119 TEST_F(TokenServiceTest, NotificationSuccess) { | 122 TEST_F(TokenServiceTest, NotificationSuccess) { |
| 120 EXPECT_EQ(0U, success_tracker_.size()); | 123 EXPECT_EQ(0U, success_tracker_.size()); |
| 121 EXPECT_EQ(0U, failure_tracker_.size()); | 124 EXPECT_EQ(0U, failure_tracker_.size()); |
| 122 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 125 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 123 EXPECT_EQ(1U, success_tracker_.size()); | 126 EXPECT_EQ(1U, success_tracker_.size()); |
| 124 EXPECT_EQ(0U, failure_tracker_.size()); | 127 EXPECT_EQ(0U, failure_tracker_.size()); |
| 125 | 128 |
| 126 TokenService::TokenAvailableDetails details = success_tracker_.details(); | 129 TokenService::TokenAvailableDetails details = success_tracker_.details(); |
| 127 // MSVC doesn't like this comparison as EQ. | 130 // MSVC doesn't like this comparison as EQ. |
| 128 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); | 131 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); |
| 129 EXPECT_EQ(details.token(), "token"); | 132 EXPECT_EQ(details.token(), "token"); |
| 130 } | 133 } |
| 131 | 134 |
| 135 TEST_F(TokenServiceTest, NotificationSuccessOAuth) { |
| 136 EXPECT_EQ(0U, success_tracker_.size()); |
| 137 EXPECT_EQ(0U, failure_tracker_.size()); |
| 138 service_.OnOAuthWrapBridgeSuccess( |
| 139 GaiaConstants::kSyncServiceOAuth, "token", "3600"); |
| 140 EXPECT_EQ(1U, success_tracker_.size()); |
| 141 EXPECT_EQ(0U, failure_tracker_.size()); |
| 142 |
| 143 TokenService::TokenAvailableDetails details = success_tracker_.details(); |
| 144 // MSVC doesn't like this comparison as EQ. |
| 145 EXPECT_TRUE(details.service() == GaiaConstants::kSyncServiceOAuth); |
| 146 EXPECT_EQ(details.token(), "token"); |
| 147 } |
| 148 |
| 132 TEST_F(TokenServiceTest, NotificationFailed) { | 149 TEST_F(TokenServiceTest, NotificationFailed) { |
| 133 EXPECT_EQ(0U, success_tracker_.size()); | 150 EXPECT_EQ(0U, success_tracker_.size()); |
| 134 EXPECT_EQ(0U, failure_tracker_.size()); | 151 EXPECT_EQ(0U, failure_tracker_.size()); |
| 135 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 152 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 136 service_.OnIssueAuthTokenFailure(GaiaConstants::kSyncService, error); | 153 service_.OnIssueAuthTokenFailure(GaiaConstants::kSyncService, error); |
| 137 EXPECT_EQ(0U, success_tracker_.size()); | 154 EXPECT_EQ(0U, success_tracker_.size()); |
| 138 EXPECT_EQ(1U, failure_tracker_.size()); | 155 EXPECT_EQ(1U, failure_tracker_.size()); |
| 139 | 156 |
| 140 TokenService::TokenRequestFailedDetails details = failure_tracker_.details(); | 157 TokenService::TokenRequestFailedDetails details = failure_tracker_.details(); |
| 141 | 158 |
| 142 // MSVC doesn't like this comparison as EQ. | 159 // MSVC doesn't like this comparison as EQ. |
| 143 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); | 160 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); |
| 144 EXPECT_TRUE(details.error() == error); // Struct has no print function. | 161 EXPECT_TRUE(details.error() == error); // Struct has no print function. |
| 145 } | 162 } |
| 146 | 163 |
| 164 TEST_F(TokenServiceTest, NotificationFailedOAuth) { |
| 165 EXPECT_EQ(0U, success_tracker_.size()); |
| 166 EXPECT_EQ(0U, failure_tracker_.size()); |
| 167 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 168 service_.OnOAuthWrapBridgeFailure(GaiaConstants::kSyncServiceOAuth, error); |
| 169 EXPECT_EQ(0U, success_tracker_.size()); |
| 170 EXPECT_EQ(1U, failure_tracker_.size()); |
| 171 |
| 172 TokenService::TokenRequestFailedDetails details = failure_tracker_.details(); |
| 173 |
| 174 // MSVC doesn't like this comparison as EQ. |
| 175 EXPECT_TRUE(details.service() == GaiaConstants::kSyncServiceOAuth); |
| 176 EXPECT_TRUE(details.error() == error); // Struct has no print function. |
| 177 } |
| 178 |
| 147 TEST_F(TokenServiceTest, OnTokenSuccessUpdate) { | 179 TEST_F(TokenServiceTest, OnTokenSuccessUpdate) { |
| 148 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 180 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 149 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 181 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 150 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); | 182 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); |
| 151 | 183 |
| 152 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token2"); | 184 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token2"); |
| 153 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 185 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 154 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token2"); | 186 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token2"); |
| 155 | 187 |
| 156 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, ""); | 188 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, ""); |
| 157 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 189 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 158 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), ""); | 190 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), ""); |
| 159 } | 191 } |
| 160 | 192 |
| 161 TEST_F(TokenServiceTest, OnTokenSuccess) { | 193 TEST_F(TokenServiceTest, OnTokenSuccess) { |
| 162 // Don't "start fetching", just go ahead and issue the callback. | 194 // Don't "start fetching", just go ahead and issue the callback. |
| 163 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 195 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 164 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 196 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 197 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 165 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 198 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 166 // Gaia returns the entire result as the token so while this is a shared | 199 // Gaia returns the entire result as the token so while this is a shared |
| 167 // result with ClientLogin, it doesn't matter, we should still get it back. | 200 // result with ClientLogin, it doesn't matter, we should still get it back. |
| 168 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); | 201 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); |
| 169 | 202 |
| 170 // Check the second service. | 203 // Try the OAuth service. |
| 171 service_.OnIssueAuthTokenSuccess(GaiaConstants::kTalkService, "token2"); | 204 service_.OnOAuthWrapBridgeSuccess( |
| 172 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 205 GaiaConstants::kSyncServiceOAuth, "token2", "3600"); |
| 173 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kTalkService), "token2"); | 206 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 207 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncServiceOAuth), |
| 208 "token2"); |
| 174 | 209 |
| 175 // It didn't change. | 210 // First didn't change. |
| 176 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); | 211 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); |
| 177 } | 212 } |
| 178 | 213 |
| 179 TEST_F(TokenServiceTest, ResetSimple) { | 214 TEST_F(TokenServiceTest, ResetSimple) { |
| 180 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 215 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 181 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 216 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 182 EXPECT_TRUE(service_.HasLsid()); | 217 EXPECT_TRUE(service_.HasLsid()); |
| 183 | 218 |
| 184 service_.ResetCredentialsInMemory(); | 219 service_.ResetCredentialsInMemory(); |
| 185 | 220 |
| 186 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 221 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 187 EXPECT_FALSE(service_.HasLsid()); | 222 EXPECT_FALSE(service_.HasLsid()); |
| 188 } | 223 } |
| 189 | 224 |
| 190 TEST_F(TokenServiceTest, ResetComplex) { | 225 TEST_F(TokenServiceTest, ResetComplex) { |
| 191 TestURLFetcherFactory factory; | 226 TestURLFetcherFactory factory; |
| 192 service_.StartFetchingTokens(); | 227 service_.StartFetchingTokens(); |
| 193 // You have to call delegates by hand with the test fetcher, | 228 // You have to call delegates by hand with the test fetcher, |
| 194 // Let's pretend only one returned. | 229 // Let's pretend only one returned. |
| 195 | 230 |
| 196 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "eraseme"); | 231 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "eraseme"); |
| 197 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 232 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 198 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), | 233 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), |
| 199 "eraseme"); | 234 "eraseme"); |
| 235 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 200 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 236 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 201 | 237 |
| 202 service_.ResetCredentialsInMemory(); | 238 service_.ResetCredentialsInMemory(); |
| 203 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 239 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 240 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 204 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 241 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 205 EXPECT_FALSE(service_.HasLsid()); | 242 EXPECT_FALSE(service_.HasLsid()); |
| 206 | 243 |
| 207 // Now start using it again. | 244 // Now start using it again. |
| 208 service_.UpdateCredentials(credentials_); | 245 service_.UpdateCredentials(credentials_); |
| 209 EXPECT_TRUE(service_.HasLsid()); | 246 EXPECT_TRUE(service_.HasLsid()); |
| 210 service_.StartFetchingTokens(); | 247 service_.StartFetchingTokens(); |
| 248 service_.UpdateOAuthCredentials(oauth_token_, oauth_secret_); |
| 249 service_.StartFetchingOAuthTokens(); |
| 211 | 250 |
| 212 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 251 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 213 service_.OnIssueAuthTokenSuccess(GaiaConstants::kTalkService, "token2"); | 252 service_.OnOAuthWrapBridgeSuccess( |
| 253 GaiaConstants::kSyncServiceOAuth, "token2", "3600"); |
| 254 service_.OnIssueAuthTokenSuccess(GaiaConstants::kTalkService, "token3"); |
| 214 | 255 |
| 215 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); | 256 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); |
| 216 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kTalkService), "token2"); | 257 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncServiceOAuth), |
| 258 "token2"); |
| 259 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kTalkService), "token3"); |
| 217 } | 260 } |
| 218 | 261 |
| 219 TEST_F(TokenServiceTest, FullIntegration) { | 262 TEST_F(TokenServiceTest, FullIntegration) { |
| 220 std::string result = "SID=sid\nLSID=lsid\nAuth=auth\n"; | 263 std::string result = "SID=sid\nLSID=lsid\nAuth=auth\n"; |
| 221 | 264 |
| 222 { | 265 { |
| 223 MockFactory<MockFetcher> factory; | 266 MockFactory<MockFetcher> factory; |
| 224 factory.set_results(result); | 267 factory.set_results(result); |
| 225 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 268 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 269 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 226 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 270 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 227 service_.StartFetchingTokens(); | 271 service_.StartFetchingTokens(); |
| 228 } | 272 } |
| 229 | 273 |
| 230 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 274 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 231 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 275 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 232 // Gaia returns the entire result as the token so while this is a shared | 276 // Gaia returns the entire result as the token so while this is a shared |
| 233 // result with ClientLogin, it doesn't matter, we should still get it back. | 277 // result with ClientLogin, it doesn't matter, we should still get it back. |
| 234 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), result); | 278 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), result); |
| 235 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kTalkService), result); | 279 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kTalkService), result); |
| 236 | 280 |
| 237 service_.ResetCredentialsInMemory(); | 281 service_.ResetCredentialsInMemory(); |
| 238 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 282 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 239 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 283 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 240 } | 284 } |
| 241 | 285 |
| 242 TEST_F(TokenServiceTest, LoadTokensIntoMemoryBasic) { | 286 TEST_F(TokenServiceTest, LoadTokensIntoMemoryBasic) { |
| 243 // Validate that the method sets proper data in notifications and map. | 287 // Validate that the method sets proper data in notifications and map. |
| 244 std::map<std::string, std::string> db_tokens; | 288 std::map<std::string, std::string> db_tokens; |
| 245 std::map<std::string, std::string> memory_tokens; | 289 std::map<std::string, std::string> memory_tokens; |
| 246 | 290 |
| 247 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); | 291 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); |
| 248 EXPECT_TRUE(db_tokens.empty()); | 292 EXPECT_TRUE(db_tokens.empty()); |
| 249 EXPECT_TRUE(memory_tokens.empty()); | 293 EXPECT_TRUE(memory_tokens.empty()); |
| 250 EXPECT_EQ(0U, success_tracker_.size()); | 294 EXPECT_EQ(0U, success_tracker_.size()); |
| 251 | 295 |
| 252 db_tokens[GaiaConstants::kSyncService] = "token"; | 296 db_tokens[GaiaConstants::kSyncServiceOAuth] = "token"; |
| 253 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); | 297 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); |
| 254 EXPECT_EQ(1U, success_tracker_.size()); | 298 EXPECT_EQ(1U, success_tracker_.size()); |
| 255 | 299 |
| 256 TokenService::TokenAvailableDetails details = success_tracker_.details(); | 300 TokenService::TokenAvailableDetails details = success_tracker_.details(); |
| 257 // MSVC doesn't like this comparison as EQ. | 301 // MSVC doesn't like this comparison as EQ. |
| 258 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); | 302 EXPECT_TRUE(details.service() == GaiaConstants::kSyncServiceOAuth); |
| 259 EXPECT_EQ(details.token(), "token"); | 303 EXPECT_EQ(details.token(), "token"); |
| 260 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncService)); | 304 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncServiceOAuth)); |
| 261 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncService], "token"); | 305 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncServiceOAuth], "token"); |
| 262 } | 306 } |
| 263 | 307 |
| 264 TEST_F(TokenServiceTest, LoadTokensIntoMemoryAdvanced) { | 308 TEST_F(TokenServiceTest, LoadTokensIntoMemoryAdvanced) { |
| 265 // LoadTokensIntoMemory should avoid setting tokens already in the | 309 // LoadTokensIntoMemory should avoid setting tokens already in the |
| 266 // token map. | 310 // token map. |
| 267 std::map<std::string, std::string> db_tokens; | 311 std::map<std::string, std::string> db_tokens; |
| 268 std::map<std::string, std::string> memory_tokens; | 312 std::map<std::string, std::string> memory_tokens; |
| 269 | 313 |
| 270 db_tokens["ignore"] = "token"; | 314 db_tokens["ignore"] = "token"; |
| 271 | 315 |
| 272 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); | 316 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); |
| 273 EXPECT_TRUE(memory_tokens.empty()); | 317 EXPECT_TRUE(memory_tokens.empty()); |
| 274 db_tokens[GaiaConstants::kSyncService] = "pepper"; | 318 db_tokens[GaiaConstants::kSyncService] = "pepper"; |
| 275 | 319 |
| 276 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); | 320 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); |
| 277 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncService)); | 321 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncService)); |
| 278 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncService], "pepper"); | 322 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncService], "pepper"); |
| 279 EXPECT_EQ(1U, success_tracker_.size()); | 323 EXPECT_EQ(1U, success_tracker_.size()); |
| 280 success_tracker_.Reset(); | 324 success_tracker_.Reset(); |
| 281 | 325 |
| 282 // SyncService token is already in memory. Pretend we got it off | 326 // SyncService token is already in memory. Pretend we got it off |
| 283 // the disk as well, but an older token. | 327 // the disk as well, but an older token. |
| 284 db_tokens[GaiaConstants::kSyncService] = "ignoreme"; | 328 db_tokens[GaiaConstants::kSyncService] = "ignoreme"; |
| 285 db_tokens[GaiaConstants::kTalkService] = "tomato"; | 329 db_tokens[GaiaConstants::kSyncServiceOAuth] = "tomato"; |
| 286 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); | 330 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); |
| 287 | 331 |
| 288 EXPECT_EQ(2U, memory_tokens.size()); | 332 EXPECT_EQ(2U, memory_tokens.size()); |
| 289 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kTalkService)); | 333 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncServiceOAuth)); |
| 290 EXPECT_EQ(memory_tokens[GaiaConstants::kTalkService], "tomato"); | 334 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncServiceOAuth], "tomato"); |
| 291 EXPECT_EQ(1U, success_tracker_.size()); | 335 EXPECT_EQ(1U, success_tracker_.size()); |
| 292 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncService)); | 336 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncService)); |
| 293 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncService], "pepper"); | 337 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncService], "pepper"); |
| 294 } | 338 } |
| 295 | 339 |
| 296 TEST_F(TokenServiceTest, WebDBLoadIntegration) { | 340 TEST_F(TokenServiceTest, WebDBLoadIntegration) { |
| 297 service_.LoadTokensFromDB(); | 341 service_.LoadTokensFromDB(); |
| 298 WaitForDBLoadCompletion(); | 342 WaitForDBLoadCompletion(); |
| 299 EXPECT_EQ(0U, success_tracker_.size()); | 343 EXPECT_EQ(0U, success_tracker_.size()); |
| 300 | 344 |
| 301 // Should result in DB write. | 345 // Should result in DB write. |
| 302 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 346 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 303 EXPECT_EQ(1U, success_tracker_.size()); | 347 EXPECT_EQ(1U, success_tracker_.size()); |
| 348 service_.OnOAuthWrapBridgeSuccess( |
| 349 GaiaConstants::kSyncServiceOAuth, "token2", "3600"); |
| 350 EXPECT_EQ(2U, success_tracker_.size()); |
| 304 | 351 |
| 305 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 352 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 353 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 306 // Clean slate. | 354 // Clean slate. |
| 307 service_.ResetCredentialsInMemory(); | 355 service_.ResetCredentialsInMemory(); |
| 308 success_tracker_.Reset(); | 356 success_tracker_.Reset(); |
| 309 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 357 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 358 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 310 | 359 |
| 311 service_.LoadTokensFromDB(); | 360 service_.LoadTokensFromDB(); |
| 312 WaitForDBLoadCompletion(); | 361 WaitForDBLoadCompletion(); |
| 313 | 362 |
| 314 EXPECT_EQ(1U, success_tracker_.size()); | 363 EXPECT_EQ(2U, success_tracker_.size()); |
| 315 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 364 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 365 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 316 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 366 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 317 EXPECT_TRUE(service_.HasLsid()); | 367 EXPECT_TRUE(service_.HasLsid()); |
| 318 } | 368 } |
| 319 | 369 |
| 320 TEST_F(TokenServiceTest, MultipleLoadResetIntegration) { | 370 TEST_F(TokenServiceTest, MultipleLoadResetIntegration) { |
| 321 // Should result in DB write. | 371 // Should result in DB write. |
| 322 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 372 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 323 service_.ResetCredentialsInMemory(); | 373 service_.ResetCredentialsInMemory(); |
| 324 success_tracker_.Reset(); | 374 success_tracker_.Reset(); |
| 325 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 375 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 326 EXPECT_FALSE(service_.HasLsid()); | 376 EXPECT_FALSE(service_.HasLsid()); |
| 327 | 377 |
| 378 service_.OnOAuthWrapBridgeSuccess( |
| 379 GaiaConstants::kSyncServiceOAuth, "token2", "3600"); |
| 380 service_.ResetCredentialsInMemory(); |
| 381 success_tracker_.Reset(); |
| 382 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 383 EXPECT_FALSE(service_.AreOAuthCredentialsValid()); |
| 384 |
| 328 service_.LoadTokensFromDB(); | 385 service_.LoadTokensFromDB(); |
| 329 WaitForDBLoadCompletion(); | 386 WaitForDBLoadCompletion(); |
| 330 | 387 |
| 331 service_.LoadTokensFromDB(); // Should do nothing. | 388 service_.LoadTokensFromDB(); // Should do nothing. |
| 332 WaitForDBLoadCompletion(); | 389 WaitForDBLoadCompletion(); |
| 333 | 390 |
| 334 EXPECT_EQ(1U, success_tracker_.size()); | 391 EXPECT_EQ(2U, success_tracker_.size()); |
| 335 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 392 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 393 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 336 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 394 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 337 EXPECT_TRUE(service_.HasLsid()); | 395 EXPECT_TRUE(service_.HasLsid()); |
| 338 | 396 |
| 339 // Reset it one more time so there's no surprises. | 397 // Reset it one more time so there's no surprises. |
| 340 service_.ResetCredentialsInMemory(); | 398 service_.ResetCredentialsInMemory(); |
| 341 success_tracker_.Reset(); | 399 success_tracker_.Reset(); |
| 342 | 400 |
| 343 service_.LoadTokensFromDB(); | 401 service_.LoadTokensFromDB(); |
| 344 WaitForDBLoadCompletion(); | 402 WaitForDBLoadCompletion(); |
| 345 | 403 |
| 346 EXPECT_EQ(1U, success_tracker_.size()); | 404 EXPECT_EQ(2U, success_tracker_.size()); |
| 347 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 405 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 406 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncServiceOAuth)); |
| 348 } | 407 } |
| 349 | 408 |
| 350 #ifndef NDEBUG | 409 #ifndef NDEBUG |
| 351 class TokenServiceCommandLineTest : public TokenServiceTestHarness { | 410 class TokenServiceCommandLineTest : public TokenServiceTestHarness { |
| 352 public: | 411 public: |
| 353 virtual void SetUp() { | 412 virtual void SetUp() { |
| 354 CommandLine original_cl(*CommandLine::ForCurrentProcess()); | 413 CommandLine original_cl(*CommandLine::ForCurrentProcess()); |
| 355 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 414 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 356 switches::kSetToken, "my_service:my_value"); | 415 switches::kSetToken, "my_service:my_value"); |
| 357 TokenServiceTestHarness::SetUp(); | 416 TokenServiceTestHarness::SetUp(); |
| 358 service_.UpdateCredentials(credentials_); | 417 service_.UpdateCredentials(credentials_); |
| 418 service_.UpdateOAuthCredentials(oauth_token_, oauth_secret_); |
| 359 | 419 |
| 360 *CommandLine::ForCurrentProcess() = original_cl; | 420 *CommandLine::ForCurrentProcess() = original_cl; |
| 361 } | 421 } |
| 362 }; | 422 }; |
| 363 | 423 |
| 364 TEST_F(TokenServiceCommandLineTest, TestValueOverride) { | 424 TEST_F(TokenServiceCommandLineTest, TestValueOverride) { |
| 365 EXPECT_TRUE(service_.HasTokenForService("my_service")); | 425 EXPECT_TRUE(service_.HasTokenForService("my_service")); |
| 366 EXPECT_EQ("my_value", service_.GetTokenForService("my_service")); | 426 EXPECT_EQ("my_value", service_.GetTokenForService("my_service")); |
| 367 } | 427 } |
| 368 #endif // ifndef NDEBUG | 428 #endif // ifndef NDEBUG |
| OLD | NEW |