OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/signin/token_service_unittest.h" | 7 #include "chrome/browser/signin/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" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/synchronization/waitable_event.h" | |
14 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/signin/token_service_factory.h" | 14 #include "chrome/browser/signin/token_service_factory.h" |
16 #include "chrome/browser/webdata/token_web_data.h" | 15 #include "chrome/browser/webdata/token_web_data.h" |
17 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
18 #include "components/webdata/encryptor/encryptor.h" | 17 #include "components/webdata/encryptor/encryptor.h" |
19 #include "google_apis/gaia/gaia_constants.h" | 18 #include "google_apis/gaia/gaia_constants.h" |
20 #include "google_apis/gaia/mock_url_fetcher_factory.h" | 19 #include "google_apis/gaia/mock_url_fetcher_factory.h" |
21 #include "net/url_request/test_url_fetcher_factory.h" | 20 #include "net/url_request/test_url_fetcher_factory.h" |
22 | 21 |
23 using content::BrowserThread; | |
24 | |
25 TokenAvailableTracker::TokenAvailableTracker() {} | 22 TokenAvailableTracker::TokenAvailableTracker() {} |
26 | 23 |
27 TokenAvailableTracker::~TokenAvailableTracker() {} | 24 TokenAvailableTracker::~TokenAvailableTracker() {} |
28 | 25 |
29 void TokenAvailableTracker::Observe( | 26 void TokenAvailableTracker::Observe( |
30 int type, | 27 int type, |
31 const content::NotificationSource& source, | 28 const content::NotificationSource& source, |
32 const content::NotificationDetails& details) { | 29 const content::NotificationDetails& details) { |
33 TestNotificationTracker::Observe(type, source, details); | 30 TestNotificationTracker::Observe(type, source, details); |
34 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { | 31 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { |
(...skipping 10 matching lines...) Expand all Loading... |
45 const content::NotificationSource& source, | 42 const content::NotificationSource& source, |
46 const content::NotificationDetails& details) { | 43 const content::NotificationDetails& details) { |
47 TestNotificationTracker::Observe(type, source, details); | 44 TestNotificationTracker::Observe(type, source, details); |
48 if (type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED) { | 45 if (type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED) { |
49 content::Details<const TokenService::TokenRequestFailedDetails> full = | 46 content::Details<const TokenService::TokenRequestFailedDetails> full = |
50 details; | 47 details; |
51 details_ = *full.ptr(); | 48 details_ = *full.ptr(); |
52 } | 49 } |
53 } | 50 } |
54 | 51 |
55 TokenServiceTestHarness::TokenServiceTestHarness() | 52 TokenServiceTestHarness::TokenServiceTestHarness() {} |
56 : ui_thread_(BrowserThread::UI, &message_loop_), | |
57 db_thread_(BrowserThread::DB) { | |
58 } | |
59 | 53 |
60 TokenServiceTestHarness::~TokenServiceTestHarness() {} | 54 TokenServiceTestHarness::~TokenServiceTestHarness() {} |
61 | 55 |
62 void TokenServiceTestHarness::SetUp() { | 56 void TokenServiceTestHarness::SetUp() { |
63 #if defined(OS_MACOSX) | 57 #if defined(OS_MACOSX) |
64 Encryptor::UseMockKeychain(true); | 58 Encryptor::UseMockKeychain(true); |
65 #endif | 59 #endif |
66 credentials_.sid = "sid"; | 60 credentials_.sid = "sid"; |
67 credentials_.lsid = "lsid"; | 61 credentials_.lsid = "lsid"; |
68 credentials_.token = "token"; | 62 credentials_.token = "token"; |
69 credentials_.data = "data"; | 63 credentials_.data = "data"; |
70 oauth_token_ = "oauth"; | 64 oauth_token_ = "oauth"; |
71 oauth_secret_ = "secret"; | 65 oauth_secret_ = "secret"; |
72 | 66 |
73 ASSERT_TRUE(db_thread_.Start()); | |
74 | |
75 profile_.reset(new TestingProfile()); | 67 profile_.reset(new TestingProfile()); |
76 profile_->CreateWebDataService(); | 68 profile_->CreateWebDataService(); |
77 | 69 |
78 WaitForDBLoadCompletion(); | 70 // Force the loading of the WebDataService. |
| 71 TokenWebData::FromBrowserContext(profile_.get()); |
| 72 base::RunLoop().RunUntilIdle(); |
| 73 |
79 service_ = TokenServiceFactory::GetForProfile(profile_.get()); | 74 service_ = TokenServiceFactory::GetForProfile(profile_.get()); |
80 | 75 |
81 success_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_AVAILABLE, | 76 success_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_AVAILABLE, |
82 content::Source<TokenService>(service_)); | 77 content::Source<TokenService>(service_)); |
83 failure_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | 78 failure_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, |
84 content::Source<TokenService>(service_)); | 79 content::Source<TokenService>(service_)); |
85 | 80 |
86 service_->Initialize("test", profile_.get()); | 81 service()->Initialize("test", profile_.get()); |
87 } | 82 } |
88 | 83 |
89 void TokenServiceTestHarness::TearDown() { | 84 void TokenServiceTestHarness::TearDown() { |
90 // You have to destroy the profile before the db_thread_ stops. | 85 // You have to destroy the profile before the threads are shut down. |
91 if (profile_.get()) { | 86 profile_.reset(); |
92 profile_.reset(NULL); | |
93 } | |
94 // Schedule another task on the DB thread to notify us that it's safe to | |
95 // carry on with the test. | |
96 base::WaitableEvent done(false, false); | |
97 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | |
98 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | |
99 done.Wait(); | |
100 base::MessageLoop::current()->PostTask(FROM_HERE, | |
101 base::MessageLoop::QuitClosure()); | |
102 base::MessageLoop::current()->Run(); | |
103 db_thread_.Stop(); | |
104 } | 87 } |
105 | 88 |
106 void TokenServiceTestHarness::WaitForDBLoadCompletion() { | 89 void TokenServiceTestHarness::UpdateCredentialsOnService() { |
107 // Force the loading of the WebDataService. | 90 service()->UpdateCredentials(credentials_); |
108 TokenWebData::FromBrowserContext(profile_.get()); | |
109 | |
110 // The WebDB does all work on the DB thread. This will add an event | |
111 // to the end of the DB thread, so when we reach this task, all DB | |
112 // operations should be complete. | |
113 base::WaitableEvent done(false, false); | |
114 BrowserThread::PostTask( | |
115 BrowserThread::DB, | |
116 FROM_HERE, | |
117 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | |
118 done.Wait(); | |
119 | |
120 // Notifications should be returned from the DB thread onto the UI thread. | |
121 message_loop_.RunUntilIdle(); | |
122 } | 91 } |
123 | 92 |
124 class TokenServiceTest : public TokenServiceTestHarness { | 93 class TokenServiceTest : public TokenServiceTestHarness { |
125 public: | 94 public: |
126 virtual void SetUp() { | 95 virtual void SetUp() { |
127 TokenServiceTestHarness::SetUp(); | 96 TokenServiceTestHarness::SetUp(); |
128 service_->UpdateCredentials(credentials_); | 97 UpdateCredentialsOnService(); |
129 } | 98 } |
130 protected: | 99 protected: |
131 void TestLoadSingleToken( | 100 void TestLoadSingleToken( |
132 std::map<std::string, std::string>* db_tokens, | 101 std::map<std::string, std::string>* db_tokens, |
133 std::map<std::string, std::string>* memory_tokens, | 102 std::map<std::string, std::string>* memory_tokens, |
134 const std::string& service) { | 103 const std::string& service_name) { |
135 std::string token = service + "_token"; | 104 std::string token = service_name + "_token"; |
136 (*db_tokens)[service] = token; | 105 (*db_tokens)[service_name] = token; |
137 size_t prev_success_size = success_tracker_.size(); | 106 size_t prev_success_size = success_tracker()->size(); |
138 service_->LoadTokensIntoMemory(*db_tokens, memory_tokens); | 107 service()->LoadTokensIntoMemory(*db_tokens, memory_tokens); |
139 | 108 |
140 // Check notification. | 109 // Check notification. |
141 EXPECT_EQ(prev_success_size + 1, success_tracker_.size()); | 110 EXPECT_EQ(prev_success_size + 1, success_tracker()->size()); |
142 TokenService::TokenAvailableDetails details = success_tracker_.details(); | 111 TokenService::TokenAvailableDetails details = success_tracker()->details(); |
143 EXPECT_EQ(details.service(), service); | 112 EXPECT_EQ(details.service(), service_name); |
144 EXPECT_EQ(details.token(), token); | 113 EXPECT_EQ(details.token(), token); |
145 // Check memory tokens. | 114 // Check memory tokens. |
146 EXPECT_EQ(1U, memory_tokens->count(service)); | 115 EXPECT_EQ(1U, memory_tokens->count(service_name)); |
147 EXPECT_EQ((*memory_tokens)[service], token); | 116 EXPECT_EQ((*memory_tokens)[service_name], token); |
148 } | 117 } |
149 }; | 118 }; |
150 | 119 |
151 TEST_F(TokenServiceTest, SanityCheck) { | 120 TEST_F(TokenServiceTest, SanityCheck) { |
152 EXPECT_FALSE(service_->HasTokenForService("nonexistent service")); | 121 EXPECT_FALSE(service()->HasTokenForService("nonexistent service")); |
153 EXPECT_FALSE(service_->TokensLoadedFromDB()); | 122 EXPECT_FALSE(service()->TokensLoadedFromDB()); |
154 } | 123 } |
155 | 124 |
156 TEST_F(TokenServiceTest, NoToken) { | 125 TEST_F(TokenServiceTest, NoToken) { |
157 EXPECT_FALSE(service_->HasTokenForService("nonexistent service")); | 126 EXPECT_FALSE(service()->HasTokenForService("nonexistent service")); |
158 EXPECT_EQ(service_->GetTokenForService("nonexistent service"), std::string()); | 127 EXPECT_EQ(service()->GetTokenForService("nonexistent service"), |
| 128 std::string()); |
159 } | 129 } |
160 | 130 |
161 TEST_F(TokenServiceTest, NotificationSuccess) { | 131 TEST_F(TokenServiceTest, NotificationSuccess) { |
162 EXPECT_EQ(0U, success_tracker_.size()); | 132 EXPECT_EQ(0U, success_tracker()->size()); |
163 EXPECT_EQ(0U, failure_tracker_.size()); | 133 EXPECT_EQ(0U, failure_tracker()->size()); |
164 service_->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 134 service()->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
165 EXPECT_EQ(1U, success_tracker_.size()); | 135 EXPECT_EQ(1U, success_tracker()->size()); |
166 EXPECT_EQ(0U, failure_tracker_.size()); | 136 EXPECT_EQ(0U, failure_tracker()->size()); |
167 | 137 |
168 TokenService::TokenAvailableDetails details = success_tracker_.details(); | 138 TokenService::TokenAvailableDetails details = success_tracker()->details(); |
169 // MSVC doesn't like this comparison as EQ. | 139 // MSVC doesn't like this comparison as EQ. |
170 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); | 140 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); |
171 EXPECT_EQ(details.token(), "token"); | 141 EXPECT_EQ(details.token(), "token"); |
172 } | 142 } |
173 | 143 |
174 TEST_F(TokenServiceTest, NotificationOAuthLoginTokenSuccess) { | 144 TEST_F(TokenServiceTest, NotificationOAuthLoginTokenSuccess) { |
175 EXPECT_EQ(0U, success_tracker_.size()); | 145 EXPECT_EQ(0U, success_tracker()->size()); |
176 EXPECT_EQ(0U, failure_tracker_.size()); | 146 EXPECT_EQ(0U, failure_tracker()->size()); |
177 service_->OnClientOAuthSuccess( | 147 service()->OnClientOAuthSuccess( |
178 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600)); | 148 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600)); |
179 EXPECT_EQ(1U, success_tracker_.size()); | 149 EXPECT_EQ(1U, success_tracker()->size()); |
180 EXPECT_EQ(0U, failure_tracker_.size()); | 150 EXPECT_EQ(0U, failure_tracker()->size()); |
181 | 151 |
182 TokenService::TokenAvailableDetails details = success_tracker_.details(); | 152 TokenService::TokenAvailableDetails details = success_tracker()->details(); |
183 // MSVC doesn't like this comparison as EQ. | 153 // MSVC doesn't like this comparison as EQ. |
184 EXPECT_TRUE(details.service() == | 154 EXPECT_TRUE(details.service() == |
185 GaiaConstants::kGaiaOAuth2LoginRefreshToken); | 155 GaiaConstants::kGaiaOAuth2LoginRefreshToken); |
186 EXPECT_EQ(details.token(), "rt1"); | 156 EXPECT_EQ(details.token(), "rt1"); |
187 } | 157 } |
188 | 158 |
189 TEST_F(TokenServiceTest, NotificationFailed) { | 159 TEST_F(TokenServiceTest, NotificationFailed) { |
190 EXPECT_EQ(0U, success_tracker_.size()); | 160 EXPECT_EQ(0U, success_tracker()->size()); |
191 EXPECT_EQ(0U, failure_tracker_.size()); | 161 EXPECT_EQ(0U, failure_tracker()->size()); |
192 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 162 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
193 service_->OnIssueAuthTokenFailure(GaiaConstants::kSyncService, error); | 163 service()->OnIssueAuthTokenFailure(GaiaConstants::kSyncService, error); |
194 EXPECT_EQ(0U, success_tracker_.size()); | 164 EXPECT_EQ(0U, success_tracker()->size()); |
195 EXPECT_EQ(1U, failure_tracker_.size()); | 165 EXPECT_EQ(1U, failure_tracker()->size()); |
196 | 166 |
197 TokenService::TokenRequestFailedDetails details = failure_tracker_.details(); | 167 TokenService::TokenRequestFailedDetails details = |
| 168 failure_tracker()->details(); |
198 // MSVC doesn't like this comparison as EQ. | 169 // MSVC doesn't like this comparison as EQ. |
199 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); | 170 EXPECT_TRUE(details.service() == GaiaConstants::kSyncService); |
200 EXPECT_TRUE(details.error() == error); // Struct has no print function. | 171 EXPECT_TRUE(details.error() == error); // Struct has no print function. |
201 } | 172 } |
202 | 173 |
203 TEST_F(TokenServiceTest, NotificationOAuthLoginTokenFailed) { | 174 TEST_F(TokenServiceTest, NotificationOAuthLoginTokenFailed) { |
204 EXPECT_EQ(0U, success_tracker_.size()); | 175 EXPECT_EQ(0U, success_tracker()->size()); |
205 EXPECT_EQ(0U, failure_tracker_.size()); | 176 EXPECT_EQ(0U, failure_tracker()->size()); |
206 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 177 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
207 service_->OnClientOAuthFailure(error); | 178 service()->OnClientOAuthFailure(error); |
208 EXPECT_EQ(0U, success_tracker_.size()); | 179 EXPECT_EQ(0U, success_tracker()->size()); |
209 EXPECT_EQ(1U, failure_tracker_.size()); | 180 EXPECT_EQ(1U, failure_tracker()->size()); |
210 | 181 |
211 TokenService::TokenRequestFailedDetails details = failure_tracker_.details(); | 182 TokenService::TokenRequestFailedDetails details = |
| 183 failure_tracker()->details(); |
212 | 184 |
213 // MSVC doesn't like this comparison as EQ. | 185 // MSVC doesn't like this comparison as EQ. |
214 EXPECT_TRUE(details.service() == | 186 EXPECT_TRUE(details.service() == |
215 GaiaConstants::kGaiaOAuth2LoginRefreshToken); | 187 GaiaConstants::kGaiaOAuth2LoginRefreshToken); |
216 EXPECT_TRUE(details.error() == error); // Struct has no print function. | 188 EXPECT_TRUE(details.error() == error); // Struct has no print function. |
217 } | 189 } |
218 | 190 |
219 TEST_F(TokenServiceTest, OnTokenSuccessUpdate) { | 191 TEST_F(TokenServiceTest, OnTokenSuccessUpdate) { |
220 service_->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 192 service()->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
221 EXPECT_TRUE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 193 EXPECT_TRUE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
222 EXPECT_EQ(service_->GetTokenForService(GaiaConstants::kSyncService), "token"); | 194 EXPECT_EQ(service()->GetTokenForService(GaiaConstants::kSyncService), |
| 195 "token"); |
223 | 196 |
224 service_->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token2"); | 197 service()->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token2"); |
225 EXPECT_TRUE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 198 EXPECT_TRUE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
226 EXPECT_EQ(service_->GetTokenForService(GaiaConstants::kSyncService), | 199 EXPECT_EQ(service()->GetTokenForService(GaiaConstants::kSyncService), |
227 "token2"); | 200 "token2"); |
228 | 201 |
229 service_->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, std::string()); | 202 service()->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, |
230 EXPECT_TRUE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 203 std::string()); |
231 EXPECT_EQ(service_->GetTokenForService(GaiaConstants::kSyncService), ""); | 204 EXPECT_TRUE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
| 205 EXPECT_EQ(service()->GetTokenForService(GaiaConstants::kSyncService), ""); |
232 } | 206 } |
233 | 207 |
234 TEST_F(TokenServiceTest, OnOAuth2LoginTokenSuccessUpdate) { | 208 TEST_F(TokenServiceTest, OnOAuth2LoginTokenSuccessUpdate) { |
235 EXPECT_FALSE(service_->HasOAuthLoginToken()); | 209 EXPECT_FALSE(service()->HasOAuthLoginToken()); |
236 | 210 |
237 service_->OnClientOAuthSuccess( | 211 service()->OnClientOAuthSuccess( |
238 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600)); | 212 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600)); |
239 EXPECT_TRUE(service_->HasOAuthLoginToken()); | 213 EXPECT_TRUE(service()->HasOAuthLoginToken()); |
240 EXPECT_EQ(service_->GetOAuth2LoginRefreshToken(), "rt1"); | 214 EXPECT_EQ(service()->GetOAuth2LoginRefreshToken(), "rt1"); |
241 | 215 |
242 service_->OnClientOAuthSuccess( | 216 service()->OnClientOAuthSuccess( |
243 GaiaAuthConsumer::ClientOAuthResult("rt2", "at2", 3600)); | 217 GaiaAuthConsumer::ClientOAuthResult("rt2", "at2", 3600)); |
244 EXPECT_TRUE(service_->HasOAuthLoginToken()); | 218 EXPECT_TRUE(service()->HasOAuthLoginToken()); |
245 EXPECT_EQ(service_->GetOAuth2LoginRefreshToken(), "rt2"); | 219 EXPECT_EQ(service()->GetOAuth2LoginRefreshToken(), "rt2"); |
246 | 220 |
247 service_->OnClientOAuthSuccess( | 221 service()->OnClientOAuthSuccess( |
248 GaiaAuthConsumer::ClientOAuthResult("rt3", "at3", 3600)); | 222 GaiaAuthConsumer::ClientOAuthResult("rt3", "at3", 3600)); |
249 EXPECT_TRUE(service_->HasOAuthLoginToken()); | 223 EXPECT_TRUE(service()->HasOAuthLoginToken()); |
250 EXPECT_EQ(service_->GetOAuth2LoginRefreshToken(), "rt3"); | 224 EXPECT_EQ(service()->GetOAuth2LoginRefreshToken(), "rt3"); |
251 } | 225 } |
252 | 226 |
253 TEST_F(TokenServiceTest, OnTokenSuccess) { | 227 TEST_F(TokenServiceTest, OnTokenSuccess) { |
254 // Don't "start fetching", just go ahead and issue the callback. | 228 // Don't "start fetching", just go ahead and issue the callback. |
255 service_->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 229 service()->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
256 EXPECT_TRUE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 230 EXPECT_TRUE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
257 // Gaia returns the entire result as the token so while this is a shared | 231 // Gaia returns the entire result as the token so while this is a shared |
258 // result with ClientLogin, it doesn't matter, we should still get it back. | 232 // result with ClientLogin, it doesn't matter, we should still get it back. |
259 EXPECT_EQ(service_->GetTokenForService(GaiaConstants::kSyncService), "token"); | 233 EXPECT_EQ(service()->GetTokenForService(GaiaConstants::kSyncService), |
| 234 "token"); |
260 } | 235 } |
261 | 236 |
262 TEST_F(TokenServiceTest, Reset) { | 237 TEST_F(TokenServiceTest, Reset) { |
263 net::TestURLFetcherFactory factory; | 238 net::TestURLFetcherFactory factory; |
264 service_->StartFetchingTokens(); | 239 service()->StartFetchingTokens(); |
265 // You have to call delegates by hand with the test fetcher, | 240 // You have to call delegates by hand with the test fetcher, |
266 // Let's pretend only one returned. | 241 // Let's pretend only one returned. |
267 | 242 |
268 service_->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "eraseme"); | 243 service()->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "eraseme"); |
269 EXPECT_TRUE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 244 EXPECT_TRUE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
270 EXPECT_EQ(service_->GetTokenForService(GaiaConstants::kSyncService), | 245 EXPECT_EQ(service()->GetTokenForService(GaiaConstants::kSyncService), |
271 "eraseme"); | 246 "eraseme"); |
272 | 247 |
273 service_->ResetCredentialsInMemory(); | 248 service()->ResetCredentialsInMemory(); |
274 EXPECT_FALSE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 249 EXPECT_FALSE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
275 | 250 |
276 // Now start using it again. | 251 // Now start using it again. |
277 service_->UpdateCredentials(credentials_); | 252 UpdateCredentialsOnService(); |
278 service_->StartFetchingTokens(); | 253 service()->StartFetchingTokens(); |
279 | 254 |
280 service_->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 255 service()->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
281 | 256 |
282 EXPECT_EQ(service_->GetTokenForService(GaiaConstants::kSyncService), "token"); | 257 EXPECT_EQ(service()->GetTokenForService(GaiaConstants::kSyncService), |
| 258 "token"); |
283 } | 259 } |
284 | 260 |
285 TEST_F(TokenServiceTest, FullIntegration) { | 261 TEST_F(TokenServiceTest, FullIntegration) { |
286 std::string result = "SID=sid\nLSID=lsid\nAuth=auth\n"; | 262 std::string result = "SID=sid\nLSID=lsid\nAuth=auth\n"; |
287 | 263 |
288 { | 264 { |
289 MockURLFetcherFactory<MockFetcher> factory; | 265 MockURLFetcherFactory<MockFetcher> factory; |
290 factory.set_results(result); | 266 factory.set_results(result); |
291 EXPECT_FALSE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 267 EXPECT_FALSE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
292 service_->StartFetchingTokens(); | 268 service()->StartFetchingTokens(); |
293 } | 269 } |
294 | 270 |
295 EXPECT_TRUE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 271 EXPECT_TRUE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
296 // Gaia returns the entire result as the token so while this is a shared | 272 // Gaia returns the entire result as the token so while this is a shared |
297 // result with ClientLogin, it doesn't matter, we should still get it back. | 273 // result with ClientLogin, it doesn't matter, we should still get it back. |
298 EXPECT_EQ(service_->GetTokenForService(GaiaConstants::kSyncService), result); | 274 EXPECT_EQ(service()->GetTokenForService(GaiaConstants::kSyncService), result); |
299 | 275 |
300 service_->ResetCredentialsInMemory(); | 276 service()->ResetCredentialsInMemory(); |
301 EXPECT_FALSE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 277 EXPECT_FALSE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
302 } | 278 } |
303 | 279 |
304 TEST_F(TokenServiceTest, LoadTokensIntoMemoryBasic) { | 280 TEST_F(TokenServiceTest, LoadTokensIntoMemoryBasic) { |
305 // Validate that the method sets proper data in notifications and map. | 281 // Validate that the method sets proper data in notifications and map. |
306 std::map<std::string, std::string> db_tokens; | 282 std::map<std::string, std::string> db_tokens; |
307 std::map<std::string, std::string> memory_tokens; | 283 std::map<std::string, std::string> memory_tokens; |
308 | 284 |
309 EXPECT_FALSE(service_->TokensLoadedFromDB()); | 285 EXPECT_FALSE(service()->TokensLoadedFromDB()); |
310 service_->LoadTokensIntoMemory(db_tokens, &memory_tokens); | 286 service()->LoadTokensIntoMemory(db_tokens, &memory_tokens); |
311 EXPECT_TRUE(db_tokens.empty()); | 287 EXPECT_TRUE(db_tokens.empty()); |
312 EXPECT_TRUE(memory_tokens.empty()); | 288 EXPECT_TRUE(memory_tokens.empty()); |
313 EXPECT_EQ(0U, success_tracker_.size()); | 289 EXPECT_EQ(0U, success_tracker()->size()); |
314 | 290 |
315 std::vector<std::string> services; | 291 std::vector<std::string> services; |
316 TokenService::GetServiceNames(&services); | 292 TokenService::GetServiceNames(&services); |
317 for (std::vector<std::string>::const_iterator i = services.begin(); | 293 for (std::vector<std::string>::const_iterator i = services.begin(); |
318 i != services.end(); ++i) { | 294 i != services.end(); ++i) { |
319 const std::string& service = *i; | 295 const std::string& service = *i; |
320 TestLoadSingleToken(&db_tokens, &memory_tokens, service); | 296 TestLoadSingleToken(&db_tokens, &memory_tokens, service); |
321 } | 297 } |
322 std::string service = GaiaConstants::kGaiaOAuth2LoginRefreshToken; | 298 std::string service = GaiaConstants::kGaiaOAuth2LoginRefreshToken; |
323 TestLoadSingleToken(&db_tokens, &memory_tokens, service); | 299 TestLoadSingleToken(&db_tokens, &memory_tokens, service); |
324 } | 300 } |
325 | 301 |
326 TEST_F(TokenServiceTest, LoadTokensIntoMemoryAdvanced) { | 302 TEST_F(TokenServiceTest, LoadTokensIntoMemoryAdvanced) { |
327 // LoadTokensIntoMemory should avoid setting tokens already in the | 303 // LoadTokensIntoMemory should avoid setting tokens already in the |
328 // token map. | 304 // token map. |
329 std::map<std::string, std::string> db_tokens; | 305 std::map<std::string, std::string> db_tokens; |
330 std::map<std::string, std::string> memory_tokens; | 306 std::map<std::string, std::string> memory_tokens; |
331 | 307 |
332 db_tokens["ignore"] = "token"; | 308 db_tokens["ignore"] = "token"; |
333 | 309 |
334 service_->LoadTokensIntoMemory(db_tokens, &memory_tokens); | 310 service()->LoadTokensIntoMemory(db_tokens, &memory_tokens); |
335 EXPECT_TRUE(memory_tokens.empty()); | 311 EXPECT_TRUE(memory_tokens.empty()); |
336 db_tokens[GaiaConstants::kSyncService] = "pepper"; | 312 db_tokens[GaiaConstants::kSyncService] = "pepper"; |
337 | 313 |
338 service_->LoadTokensIntoMemory(db_tokens, &memory_tokens); | 314 service()->LoadTokensIntoMemory(db_tokens, &memory_tokens); |
339 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncService)); | 315 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncService)); |
340 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncService], "pepper"); | 316 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncService], "pepper"); |
341 EXPECT_EQ(1U, success_tracker_.size()); | 317 EXPECT_EQ(1U, success_tracker()->size()); |
342 success_tracker_.Reset(); | 318 success_tracker()->Reset(); |
343 | 319 |
344 // SyncService token is already in memory. Pretend we got it off | 320 // SyncService token is already in memory. Pretend we got it off |
345 // the disk as well, but an older token. | 321 // the disk as well, but an older token. |
346 db_tokens[GaiaConstants::kSyncService] = "ignoreme"; | 322 db_tokens[GaiaConstants::kSyncService] = "ignoreme"; |
347 service_->LoadTokensIntoMemory(db_tokens, &memory_tokens); | 323 service()->LoadTokensIntoMemory(db_tokens, &memory_tokens); |
348 | 324 |
349 EXPECT_EQ(1U, memory_tokens.size()); | 325 EXPECT_EQ(1U, memory_tokens.size()); |
350 EXPECT_EQ(0U, success_tracker_.size()); | 326 EXPECT_EQ(0U, success_tracker()->size()); |
351 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncService)); | 327 EXPECT_EQ(1U, memory_tokens.count(GaiaConstants::kSyncService)); |
352 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncService], "pepper"); | 328 EXPECT_EQ(memory_tokens[GaiaConstants::kSyncService], "pepper"); |
353 } | 329 } |
354 | 330 |
355 TEST_F(TokenServiceTest, WebDBLoadIntegration) { | 331 TEST_F(TokenServiceTest, WebDBLoadIntegration) { |
356 service_->LoadTokensFromDB(); | 332 service()->LoadTokensFromDB(); |
357 WaitForDBLoadCompletion(); | 333 base::RunLoop().RunUntilIdle(); |
358 EXPECT_TRUE(service_->TokensLoadedFromDB()); | 334 EXPECT_TRUE(service()->TokensLoadedFromDB()); |
359 EXPECT_EQ(0U, success_tracker_.size()); | 335 EXPECT_EQ(0U, success_tracker()->size()); |
360 | 336 |
361 // Should result in DB write. | 337 // Should result in DB write. |
362 service_->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 338 service()->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
363 EXPECT_EQ(1U, success_tracker_.size()); | 339 EXPECT_EQ(1U, success_tracker()->size()); |
364 | 340 |
365 EXPECT_TRUE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 341 EXPECT_TRUE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
366 // Clean slate. | 342 // Clean slate. |
367 service_->ResetCredentialsInMemory(); | 343 service()->ResetCredentialsInMemory(); |
368 success_tracker_.Reset(); | 344 success_tracker()->Reset(); |
369 EXPECT_FALSE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 345 EXPECT_FALSE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
370 | 346 |
371 service_->LoadTokensFromDB(); | 347 service()->LoadTokensFromDB(); |
372 WaitForDBLoadCompletion(); | 348 base::RunLoop().RunUntilIdle(); |
373 | 349 |
374 EXPECT_EQ(1U, success_tracker_.size()); | 350 EXPECT_EQ(1U, success_tracker()->size()); |
375 EXPECT_TRUE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 351 EXPECT_TRUE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
376 } | 352 } |
377 | 353 |
378 TEST_F(TokenServiceTest, MultipleLoadResetIntegration) { | 354 TEST_F(TokenServiceTest, MultipleLoadResetIntegration) { |
379 // Should result in DB write. | 355 // Should result in DB write. |
380 service_->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); | 356 service()->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token"); |
381 service_->ResetCredentialsInMemory(); | 357 service()->ResetCredentialsInMemory(); |
382 success_tracker_.Reset(); | 358 success_tracker()->Reset(); |
383 EXPECT_FALSE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 359 EXPECT_FALSE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
384 | 360 |
385 EXPECT_FALSE(service_->TokensLoadedFromDB()); | 361 EXPECT_FALSE(service()->TokensLoadedFromDB()); |
386 service_->LoadTokensFromDB(); | 362 service()->LoadTokensFromDB(); |
387 WaitForDBLoadCompletion(); | 363 base::RunLoop().RunUntilIdle(); |
388 EXPECT_TRUE(service_->TokensLoadedFromDB()); | 364 EXPECT_TRUE(service()->TokensLoadedFromDB()); |
389 | 365 |
390 service_->LoadTokensFromDB(); // Should do nothing. | 366 service()->LoadTokensFromDB(); // Should do nothing. |
391 WaitForDBLoadCompletion(); | 367 base::RunLoop().RunUntilIdle(); |
392 EXPECT_TRUE(service_->TokensLoadedFromDB()); | 368 EXPECT_TRUE(service()->TokensLoadedFromDB()); |
393 | 369 |
394 EXPECT_EQ(1U, success_tracker_.size()); | 370 EXPECT_EQ(1U, success_tracker()->size()); |
395 EXPECT_TRUE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 371 EXPECT_TRUE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
396 | 372 |
397 // Reset it one more time so there's no surprises. | 373 // Reset it one more time so there's no surprises. |
398 service_->ResetCredentialsInMemory(); | 374 service()->ResetCredentialsInMemory(); |
399 EXPECT_FALSE(service_->TokensLoadedFromDB()); | 375 EXPECT_FALSE(service()->TokensLoadedFromDB()); |
400 success_tracker_.Reset(); | 376 success_tracker()->Reset(); |
401 | 377 |
402 service_->LoadTokensFromDB(); | 378 service()->LoadTokensFromDB(); |
403 WaitForDBLoadCompletion(); | 379 base::RunLoop().RunUntilIdle(); |
404 EXPECT_TRUE(service_->TokensLoadedFromDB()); | 380 EXPECT_TRUE(service()->TokensLoadedFromDB()); |
405 | 381 |
406 EXPECT_EQ(1U, success_tracker_.size()); | 382 EXPECT_EQ(1U, success_tracker()->size()); |
407 EXPECT_TRUE(service_->HasTokenForService(GaiaConstants::kSyncService)); | 383 EXPECT_TRUE(service()->HasTokenForService(GaiaConstants::kSyncService)); |
408 } | 384 } |
409 | 385 |
410 #ifndef NDEBUG | 386 #ifndef NDEBUG |
411 class TokenServiceCommandLineTest : public TokenServiceTestHarness { | 387 class TokenServiceCommandLineTest : public TokenServiceTestHarness { |
412 public: | 388 public: |
413 virtual void SetUp() { | 389 virtual void SetUp() { |
414 CommandLine original_cl(*CommandLine::ForCurrentProcess()); | 390 CommandLine original_cl(*CommandLine::ForCurrentProcess()); |
415 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 391 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
416 switches::kSetToken, "my_service:my_value"); | 392 switches::kSetToken, "my_service:my_value"); |
417 TokenServiceTestHarness::SetUp(); | 393 TokenServiceTestHarness::SetUp(); |
418 service_->UpdateCredentials(credentials_); | 394 UpdateCredentialsOnService(); |
419 | 395 |
420 *CommandLine::ForCurrentProcess() = original_cl; | 396 *CommandLine::ForCurrentProcess() = original_cl; |
421 } | 397 } |
422 }; | 398 }; |
423 | 399 |
424 TEST_F(TokenServiceCommandLineTest, TestValueOverride) { | 400 TEST_F(TokenServiceCommandLineTest, TestValueOverride) { |
425 EXPECT_TRUE(service_->HasTokenForService("my_service")); | 401 EXPECT_TRUE(service()->HasTokenForService("my_service")); |
426 EXPECT_EQ("my_value", service_->GetTokenForService("my_service")); | 402 EXPECT_EQ("my_value", service()->GetTokenForService("my_service")); |
427 } | 403 } |
428 #endif // ifndef NDEBUG | 404 #endif // ifndef NDEBUG |
OLD | NEW |