| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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.h" | 7 #include "chrome/browser/net/gaia/token_service_unittest.h" |
| 8 #include "chrome/browser/password_manager/encryptor.h" | 8 |
| 9 #include "chrome/browser/webdata/web_data_service.h" | |
| 10 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | |
| 11 #include "chrome/common/net/gaia/gaia_authenticator2_unittest.h" | 9 #include "chrome/common/net/gaia/gaia_authenticator2_unittest.h" |
| 12 #include "chrome/common/net/gaia/gaia_constants.h" | 10 #include "chrome/common/net/gaia/gaia_constants.h" |
| 13 #include "chrome/common/net/test_url_fetcher_factory.h" | 11 #include "chrome/common/net/test_url_fetcher_factory.h" |
| 14 #include "chrome/test/signaling_task.h" | |
| 15 #include "chrome/test/test_notification_tracker.h" | |
| 16 #include "chrome/test/testing_profile.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | 12 |
| 19 // TestNotificationTracker doesn't do a deep copy on the notification details. | 13 class TokenServiceTest : public TokenServiceTestHarness { |
| 20 // We have to in order to read it out, or we have a bad ptr, since the details | |
| 21 // are a reference on the stack. | |
| 22 class TokenAvailableTracker : public TestNotificationTracker { | |
| 23 public: | 14 public: |
| 24 const TokenService::TokenAvailableDetails& get_last_token_details() { | 15 virtual void SetUp() { |
| 25 return details_; | 16 TokenServiceTestHarness::SetUp(); |
| 17 service_.UpdateCredentials(credentials_); |
| 26 } | 18 } |
| 27 | |
| 28 private: | |
| 29 virtual void Observe(NotificationType type, | |
| 30 const NotificationSource& source, | |
| 31 const NotificationDetails& details) { | |
| 32 TestNotificationTracker::Observe(type, source, details); | |
| 33 if (type == NotificationType::TOKEN_AVAILABLE) { | |
| 34 Details<const TokenService::TokenAvailableDetails> full = details; | |
| 35 details_ = *full.ptr(); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 TokenService::TokenAvailableDetails details_; | |
| 40 }; | |
| 41 | |
| 42 class TokenFailedTracker : public TestNotificationTracker { | |
| 43 public: | |
| 44 const TokenService::TokenRequestFailedDetails& get_last_token_details() { | |
| 45 return details_; | |
| 46 } | |
| 47 | |
| 48 private: | |
| 49 virtual void Observe(NotificationType type, | |
| 50 const NotificationSource& source, | |
| 51 const NotificationDetails& details) { | |
| 52 TestNotificationTracker::Observe(type, source, details); | |
| 53 if (type == NotificationType::TOKEN_REQUEST_FAILED) { | |
| 54 Details<const TokenService::TokenRequestFailedDetails> full = details; | |
| 55 details_ = *full.ptr(); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 TokenService::TokenRequestFailedDetails details_; | |
| 60 }; | |
| 61 | |
| 62 class TokenServiceTest : public testing::Test { | |
| 63 public: | |
| 64 TokenServiceTest() | |
| 65 : ui_thread_(ChromeThread::UI, &message_loop_), | |
| 66 db_thread_(ChromeThread::DB) { | |
| 67 } | |
| 68 | |
| 69 virtual void SetUp() { | |
| 70 #if defined(OS_MACOSX) | |
| 71 Encryptor::UseMockKeychain(true); | |
| 72 #endif | |
| 73 credentials_.sid = "sid"; | |
| 74 credentials_.lsid = "lsid"; | |
| 75 credentials_.token = "token"; | |
| 76 credentials_.data = "data"; | |
| 77 | |
| 78 ASSERT_TRUE(db_thread_.Start()); | |
| 79 | |
| 80 profile_.reset(new TestingProfile()); | |
| 81 profile_->CreateWebDataService(false); | |
| 82 WaitForDBLoadCompletion(); | |
| 83 | |
| 84 success_tracker_.ListenFor(NotificationType::TOKEN_AVAILABLE, | |
| 85 Source<TokenService>(&service_)); | |
| 86 failure_tracker_.ListenFor(NotificationType::TOKEN_REQUEST_FAILED, | |
| 87 Source<TokenService>(&service_)); | |
| 88 | |
| 89 service_.Initialize("test", profile_.get()); | |
| 90 service_.UpdateCredentials(credentials_); | |
| 91 | |
| 92 URLFetcher::set_factory(NULL); | |
| 93 } | |
| 94 | |
| 95 virtual void TearDown() { | |
| 96 // You have to destroy the profile before the db_thread_ stops. | |
| 97 if (profile_.get()) { | |
| 98 profile_.reset(NULL); | |
| 99 } | |
| 100 | |
| 101 db_thread_.Stop(); | |
| 102 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask); | |
| 103 MessageLoop::current()->Run(); | |
| 104 } | |
| 105 | |
| 106 void WaitForDBLoadCompletion() { | |
| 107 // The WebDB does all work on the DB thread. This will add an event | |
| 108 // to the end of the DB thread, so when we reach this task, all DB | |
| 109 // operations should be complete. | |
| 110 WaitableEvent done(false, false); | |
| 111 ChromeThread::PostTask( | |
| 112 ChromeThread::DB, FROM_HERE, new SignalingTask(&done)); | |
| 113 done.Wait(); | |
| 114 | |
| 115 // Notifications should be returned from the DB thread onto the UI thread. | |
| 116 message_loop_.RunAllPending(); | |
| 117 } | |
| 118 | |
| 119 MessageLoopForUI message_loop_; | |
| 120 ChromeThread ui_thread_; // Mostly so DCHECKS pass. | |
| 121 ChromeThread db_thread_; // WDS on here | |
| 122 | |
| 123 TokenService service_; | |
| 124 TokenAvailableTracker success_tracker_; | |
| 125 TokenFailedTracker failure_tracker_; | |
| 126 GaiaAuthConsumer::ClientLoginResult credentials_; | |
| 127 scoped_ptr<TestingProfile> profile_; | |
| 128 }; | 19 }; |
| 129 | 20 |
| 130 TEST_F(TokenServiceTest, SanityCheck) { | 21 TEST_F(TokenServiceTest, SanityCheck) { |
| 131 EXPECT_TRUE(service_.HasLsid()); | 22 EXPECT_TRUE(service_.HasLsid()); |
| 132 EXPECT_EQ(service_.GetLsid(), "lsid"); | 23 EXPECT_EQ(service_.GetLsid(), "lsid"); |
| 133 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); | 24 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); |
| 134 } | 25 } |
| 135 | 26 |
| 136 TEST_F(TokenServiceTest, NoToken) { | 27 TEST_F(TokenServiceTest, NoToken) { |
| 137 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); | 28 EXPECT_FALSE(service_.HasTokenForService("nonexistent service")); |
| 138 EXPECT_EQ(service_.GetTokenForService("nonexistent service"), std::string()); | 29 EXPECT_EQ(service_.GetTokenForService("nonexistent service"), std::string()); |
| 139 } | 30 } |
| 140 | 31 |
| 141 TEST_F(TokenServiceTest, NotificationSuccess) { | 32 TEST_F(TokenServiceTest, NotificationSuccess) { |
| 142 EXPECT_EQ(0U, success_tracker_.size()); | 33 EXPECT_EQ(0U, success_tracker_.size()); |
| 143 EXPECT_EQ(0U, failure_tracker_.size()); | 34 EXPECT_EQ(0U, failure_tracker_.size()); |
| 144 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 35 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 145 EXPECT_EQ(1U, success_tracker_.size()); | 36 EXPECT_EQ(1U, success_tracker_.size()); |
| 146 EXPECT_EQ(0U, failure_tracker_.size()); | 37 EXPECT_EQ(0U, failure_tracker_.size()); |
| 147 | 38 |
| 148 TokenService::TokenAvailableDetails details = | 39 TokenService::TokenAvailableDetails details = success_tracker_.details(); |
| 149 success_tracker_.get_last_token_details(); | |
| 150 // MSVC doesn't like this comparison as EQ. | 40 // MSVC doesn't like this comparison as EQ. |
| 151 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); | 41 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); |
| 152 EXPECT_EQ(details.token(), "token"); | 42 EXPECT_EQ(details.token(), "token"); |
| 153 } | 43 } |
| 154 | 44 |
| 155 TEST_F(TokenServiceTest, NotificationFailed) { | 45 TEST_F(TokenServiceTest, NotificationFailed) { |
| 156 EXPECT_EQ(0U, success_tracker_.size()); | 46 EXPECT_EQ(0U, success_tracker_.size()); |
| 157 EXPECT_EQ(0U, failure_tracker_.size()); | 47 EXPECT_EQ(0U, failure_tracker_.size()); |
| 158 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 48 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 159 service_.OnIssueAuthTokenFailure(GaiaConstants::kSyncService, error); | 49 service_.OnIssueAuthTokenFailure(GaiaConstants::kSyncService, error); |
| 160 EXPECT_EQ(0U, success_tracker_.size()); | 50 EXPECT_EQ(0U, success_tracker_.size()); |
| 161 EXPECT_EQ(1U, failure_tracker_.size()); | 51 EXPECT_EQ(1U, failure_tracker_.size()); |
| 162 | 52 |
| 163 TokenService::TokenRequestFailedDetails details = | 53 TokenService::TokenRequestFailedDetails details = failure_tracker_.details(); |
| 164 failure_tracker_.get_last_token_details(); | |
| 165 | 54 |
| 166 // MSVC doesn't like this comparison as EQ. | 55 // MSVC doesn't like this comparison as EQ. |
| 167 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); | 56 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); |
| 168 EXPECT_TRUE(details.error() == error); // Struct has no print function. | 57 EXPECT_TRUE(details.error() == error); // Struct has no print function. |
| 169 } | 58 } |
| 170 | 59 |
| 171 TEST_F(TokenServiceTest, OnTokenSuccessUpdate) { | 60 TEST_F(TokenServiceTest, OnTokenSuccessUpdate) { |
| 172 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 61 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
| 173 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 62 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 174 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); | 63 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token"); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 | 157 |
| 269 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); | 158 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); |
| 270 EXPECT_TRUE(db_tokens.empty()); | 159 EXPECT_TRUE(db_tokens.empty()); |
| 271 EXPECT_TRUE(memory_tokens.empty()); | 160 EXPECT_TRUE(memory_tokens.empty()); |
| 272 EXPECT_EQ(0U, success_tracker_.size()); | 161 EXPECT_EQ(0U, success_tracker_.size()); |
| 273 | 162 |
| 274 db_tokens[GaiaConstants::kSyncService] = "token"; | 163 db_tokens[GaiaConstants::kSyncService] = "token"; |
| 275 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); | 164 service_.LoadTokensIntoMemory(db_tokens, &memory_tokens); |
| 276 EXPECT_EQ(1U, success_tracker_.size()); | 165 EXPECT_EQ(1U, success_tracker_.size()); |
| 277 | 166 |
| 278 TokenService::TokenAvailableDetails details = | 167 TokenService::TokenAvailableDetails details = success_tracker_.details(); |
| 279 success_tracker_.get_last_token_details(); | |
| 280 // MSVC doesn't like this comparison as EQ. | 168 // MSVC doesn't like this comparison as EQ. |
| 281 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); | 169 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); |
| 282 EXPECT_EQ(details.token(), "token"); | 170 EXPECT_EQ(details.token(), "token"); |
| 283 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncService)); | 171 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncService)); |
| 284 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncService], "token"); | 172 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncService], "token"); |
| 285 } | 173 } |
| 286 | 174 |
| 287 TEST_F(TokenServiceTest, LoadTokensIntoMemoryAdvanced) { | 175 TEST_F(TokenServiceTest, LoadTokensIntoMemoryAdvanced) { |
| 288 // LoadTokensIntoMemory should avoid setting tokens already in the | 176 // LoadTokensIntoMemory should avoid setting tokens already in the |
| 289 // token map. | 177 // token map. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 // Reset it one more time so there's no surprises. | 249 // Reset it one more time so there's no surprises. |
| 362 service_.ResetCredentialsInMemory(); | 250 service_.ResetCredentialsInMemory(); |
| 363 success_tracker_.Reset(); | 251 success_tracker_.Reset(); |
| 364 | 252 |
| 365 service_.LoadTokensFromDB(); | 253 service_.LoadTokensFromDB(); |
| 366 WaitForDBLoadCompletion(); | 254 WaitForDBLoadCompletion(); |
| 367 | 255 |
| 368 EXPECT_EQ(1U, success_tracker_.size()); | 256 EXPECT_EQ(1U, success_tracker_.size()); |
| 369 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); | 257 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService)); |
| 370 } | 258 } |
| OLD | NEW |