| 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" |
| 11 #include "chrome/browser/password_manager/encryptor.h" | 11 #include "chrome/browser/password_manager/encryptor.h" |
| 12 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/net/gaia/gaia_auth_fetcher_unittest.h" | 14 #include "chrome/common/net/gaia/gaia_auth_fetcher_unittest.h" |
| 15 #include "chrome/common/net/gaia/gaia_constants.h" | 15 #include "chrome/common/net/gaia/gaia_constants.h" |
| 16 #include "content/common/test_url_fetcher_factory.h" | 16 #include "content/test/test_url_fetcher_factory.h" |
| 17 | 17 |
| 18 TokenAvailableTracker::TokenAvailableTracker() {} | 18 TokenAvailableTracker::TokenAvailableTracker() {} |
| 19 | 19 |
| 20 TokenAvailableTracker::~TokenAvailableTracker() {} | 20 TokenAvailableTracker::~TokenAvailableTracker() {} |
| 21 | 21 |
| 22 void TokenAvailableTracker::Observe(int type, | 22 void TokenAvailableTracker::Observe(int type, |
| 23 const NotificationSource& source, | 23 const NotificationSource& source, |
| 24 const NotificationDetails& details) { | 24 const NotificationDetails& details) { |
| 25 TestNotificationTracker::Observe(type, source, details); | 25 TestNotificationTracker::Observe(type, source, details); |
| 26 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { | 26 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 profile_.reset(new TestingProfile()); | 64 profile_.reset(new TestingProfile()); |
| 65 profile_->CreateWebDataService(false); | 65 profile_->CreateWebDataService(false); |
| 66 WaitForDBLoadCompletion(); | 66 WaitForDBLoadCompletion(); |
| 67 | 67 |
| 68 success_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_AVAILABLE, | 68 success_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 69 Source<TokenService>(&service_)); | 69 Source<TokenService>(&service_)); |
| 70 failure_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | 70 failure_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, |
| 71 Source<TokenService>(&service_)); | 71 Source<TokenService>(&service_)); |
| 72 | 72 |
| 73 service_.Initialize("test", profile_.get()); | 73 service_.Initialize("test", profile_.get()); |
| 74 | |
| 75 URLFetcher::set_factory(NULL); | |
| 76 } | 74 } |
| 77 | 75 |
| 78 void TokenServiceTestHarness::TearDown() { | 76 void TokenServiceTestHarness::TearDown() { |
| 79 // You have to destroy the profile before the db_thread_ stops. | 77 // You have to destroy the profile before the db_thread_ stops. |
| 80 if (profile_.get()) { | 78 if (profile_.get()) { |
| 81 profile_.reset(NULL); | 79 profile_.reset(NULL); |
| 82 } | 80 } |
| 83 | 81 |
| 84 db_thread_.Stop(); | 82 db_thread_.Stop(); |
| 85 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask); | 83 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 EXPECT_TRUE(service_.HasLsid()); | 182 EXPECT_TRUE(service_.HasLsid()); |
| 185 | 183 |
| 186 service_.ResetCredentialsInMemory(); | 184 service_.ResetCredentialsInMemory(); |
| 187 | 185 |
| 188 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 186 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 189 EXPECT_FALSE(service_.HasLsid()); | 187 EXPECT_FALSE(service_.HasLsid()); |
| 190 } | 188 } |
| 191 | 189 |
| 192 TEST_F(TokenServiceTest, ResetComplex) { | 190 TEST_F(TokenServiceTest, ResetComplex) { |
| 193 TestURLFetcherFactory factory; | 191 TestURLFetcherFactory factory; |
| 194 URLFetcher::set_factory(&factory); | |
| 195 service_.StartFetchingTokens(); | 192 service_.StartFetchingTokens(); |
| 196 // You have to call delegates by hand with the test fetcher, | 193 // You have to call delegates by hand with the test fetcher, |
| 197 // Let's pretend only one returned. | 194 // Let's pretend only one returned. |
| 198 | 195 |
| 199 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "eraseme"); | 196 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "eraseme"); |
| 200 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 197 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 201 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), | 198 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), |
| 202 "eraseme"); | 199 "eraseme"); |
| 203 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 200 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 204 | 201 |
| 205 service_.ResetCredentialsInMemory(); | 202 service_.ResetCredentialsInMemory(); |
| 206 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 203 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 207 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 204 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 208 EXPECT_FALSE(service_.HasLsid()); | 205 EXPECT_FALSE(service_.HasLsid()); |
| 209 | 206 |
| 210 // Now start using it again. | 207 // Now start using it again. |
| 211 service_.UpdateCredentials(credentials_); | 208 service_.UpdateCredentials(credentials_); |
| 212 EXPECT_TRUE(service_.HasLsid()); | 209 EXPECT_TRUE(service_.HasLsid()); |
| 213 service_.StartFetchingTokens(); | 210 service_.StartFetchingTokens(); |
| 214 | 211 |
| 215 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 212 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 216 service_.OnIssueAuthTokenSuccess(GaiaConstants::kTalkService, "token2"); | 213 service_.OnIssueAuthTokenSuccess(GaiaConstants::kTalkService, "token2"); |
| 217 | 214 |
| 218 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); | 215 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); |
| 219 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kTalkService), "token2"); | 216 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kTalkService), "token2"); |
| 220 } | 217 } |
| 221 | 218 |
| 222 TEST_F(TokenServiceTest, FullIntegration) { | 219 TEST_F(TokenServiceTest, FullIntegration) { |
| 223 MockFactory<MockFetcher> factory; | |
| 224 std::string result = "SID=sid\nLSID=lsid\nAuth=auth\n"; | 220 std::string result = "SID=sid\nLSID=lsid\nAuth=auth\n"; |
| 225 factory.set_results(result); | 221 |
| 226 URLFetcher::set_factory(&factory); | 222 { |
| 227 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 223 MockFactory<MockFetcher> factory; |
| 228 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 224 factory.set_results(result); |
| 229 service_.StartFetchingTokens(); | 225 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 230 URLFetcher::set_factory(NULL); | 226 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 227 service_.StartFetchingTokens(); |
| 228 } |
| 231 | 229 |
| 232 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 230 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 233 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kTalkService)); | 231 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kTalkService)); |
| 234 // Gaia returns the entire result as the token so while this is a shared | 232 // Gaia returns the entire result as the token so while this is a shared |
| 235 // result with ClientLogin, it doesn't matter, we should still get it back. | 233 // result with ClientLogin, it doesn't matter, we should still get it back. |
| 236 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), result); | 234 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), result); |
| 237 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kTalkService), result); | 235 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kTalkService), result); |
| 238 | 236 |
| 239 service_.ResetCredentialsInMemory(); | 237 service_.ResetCredentialsInMemory(); |
| 240 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 238 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 | 359 |
| 362 *CommandLine::ForCurrentProcess() = original_cl; | 360 *CommandLine::ForCurrentProcess() = original_cl; |
| 363 } | 361 } |
| 364 }; | 362 }; |
| 365 | 363 |
| 366 TEST_F(TokenServiceCommandLineTest, TestValueOverride) { | 364 TEST_F(TokenServiceCommandLineTest, TestValueOverride) { |
| 367 EXPECT_TRUE(service_.HasTokenForService("my_service")); | 365 EXPECT_TRUE(service_.HasTokenForService("my_service")); |
| 368 EXPECT_EQ("my_value", service_.GetTokenForService("my_service")); | 366 EXPECT_EQ("my_value", service_.GetTokenForService("my_service")); |
| 369 } | 367 } |
| 370 #endif // ifndef NDEBUG | 368 #endif // ifndef NDEBUG |
| OLD | NEW |