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