OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "sync/internal_api/public/attachments/attachment_downloader_impl.h" | 5 #include "sync/internal_api/public/attachments/attachment_downloader_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 protected: | 145 protected: |
146 typedef std::map<AttachmentId, AttachmentDownloader::DownloadResult> | 146 typedef std::map<AttachmentId, AttachmentDownloader::DownloadResult> |
147 ResultsMap; | 147 ResultsMap; |
148 | 148 |
149 enum HashHeaderType { | 149 enum HashHeaderType { |
150 HASH_HEADER_NONE, | 150 HASH_HEADER_NONE, |
151 HASH_HEADER_VALID, | 151 HASH_HEADER_VALID, |
152 HASH_HEADER_INVALID | 152 HASH_HEADER_INVALID |
153 }; | 153 }; |
154 | 154 |
155 AttachmentDownloaderImplTest() : num_completed_downloads_(0) {} | 155 AttachmentDownloaderImplTest() |
| 156 : num_completed_downloads_(0), |
| 157 attachment_id_( |
| 158 Attachment::Create(new base::RefCountedStaticMemory( |
| 159 kAttachmentContent, |
| 160 strlen(kAttachmentContent))).GetId()) {} |
156 | 161 |
157 void SetUp() override; | 162 void SetUp() override; |
158 void TearDown() override; | 163 void TearDown() override; |
159 | 164 |
160 AttachmentDownloader* downloader() { return attachment_downloader_.get(); } | 165 AttachmentDownloader* downloader() { return attachment_downloader_.get(); } |
161 | 166 |
162 MockOAuth2TokenService* token_service() { return token_service_.get(); } | 167 MockOAuth2TokenService* token_service() { return token_service_.get(); } |
163 | 168 |
164 int num_completed_downloads() { return num_completed_downloads_; } | 169 int num_completed_downloads() { return num_completed_downloads_; } |
165 | 170 |
| 171 const AttachmentId attachment_id() const { return attachment_id_; } |
| 172 |
166 AttachmentDownloader::DownloadCallback download_callback( | 173 AttachmentDownloader::DownloadCallback download_callback( |
167 const AttachmentId& id) { | 174 const AttachmentId& id) { |
168 return base::Bind(&AttachmentDownloaderImplTest::DownloadDone, | 175 return base::Bind(&AttachmentDownloaderImplTest::DownloadDone, |
169 base::Unretained(this), | 176 base::Unretained(this), |
170 id); | 177 id); |
171 } | 178 } |
172 | 179 |
173 // Respond with |response_code| and hash header of type |hash_header_type|. | 180 // Respond with |response_code| and hash header of type |hash_header_type|. |
174 void CompleteDownload(int response_code, HashHeaderType hash_header_type); | 181 void CompleteDownload(int response_code, HashHeaderType hash_header_type); |
175 | 182 |
(...skipping 10 matching lines...) Expand all Loading... |
186 static void AddHashHeader(HashHeaderType hash_header_type, | 193 static void AddHashHeader(HashHeaderType hash_header_type, |
187 net::TestURLFetcher* fetcher); | 194 net::TestURLFetcher* fetcher); |
188 | 195 |
189 base::MessageLoopForIO message_loop_; | 196 base::MessageLoopForIO message_loop_; |
190 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 197 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
191 net::TestURLFetcherFactory url_fetcher_factory_; | 198 net::TestURLFetcherFactory url_fetcher_factory_; |
192 scoped_ptr<MockOAuth2TokenService> token_service_; | 199 scoped_ptr<MockOAuth2TokenService> token_service_; |
193 scoped_ptr<AttachmentDownloader> attachment_downloader_; | 200 scoped_ptr<AttachmentDownloader> attachment_downloader_; |
194 ResultsMap download_results_; | 201 ResultsMap download_results_; |
195 int num_completed_downloads_; | 202 int num_completed_downloads_; |
| 203 const AttachmentId attachment_id_; |
196 }; | 204 }; |
197 | 205 |
198 void AttachmentDownloaderImplTest::SetUp() { | 206 void AttachmentDownloaderImplTest::SetUp() { |
199 url_request_context_getter_ = | 207 url_request_context_getter_ = |
200 new net::TestURLRequestContextGetter(message_loop_.message_loop_proxy()); | 208 new net::TestURLRequestContextGetter(message_loop_.message_loop_proxy()); |
201 url_fetcher_factory_.set_remove_fetcher_on_delete(true); | 209 url_fetcher_factory_.set_remove_fetcher_on_delete(true); |
202 token_service_.reset(new MockOAuth2TokenService()); | 210 token_service_.reset(new MockOAuth2TokenService()); |
203 token_service_->AddAccount(kAccountId); | 211 token_service_->AddAccount(kAccountId); |
204 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider> | 212 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider> |
205 token_service_provider(new TokenServiceProvider(token_service_.get())); | 213 token_service_provider(new TokenServiceProvider(token_service_.get())); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 break; | 295 break; |
288 case HASH_HEADER_INVALID: | 296 case HASH_HEADER_INVALID: |
289 header += "BOGUS1=="; | 297 header += "BOGUS1=="; |
290 headers->AddHeader(header); | 298 headers->AddHeader(header); |
291 break; | 299 break; |
292 } | 300 } |
293 fetcher->set_response_headers(headers); | 301 fetcher->set_response_headers(headers); |
294 } | 302 } |
295 | 303 |
296 TEST_F(AttachmentDownloaderImplTest, HappyCase) { | 304 TEST_F(AttachmentDownloaderImplTest, HappyCase) { |
297 AttachmentId id1 = AttachmentId::Create(); | 305 AttachmentId id1 = attachment_id(); |
298 // DownloadAttachment should trigger RequestAccessToken. | 306 // DownloadAttachment should trigger RequestAccessToken. |
299 downloader()->DownloadAttachment(id1, download_callback(id1)); | 307 downloader()->DownloadAttachment(id1, download_callback(id1)); |
300 RunMessageLoop(); | 308 RunMessageLoop(); |
301 // Return valid access token. | 309 // Return valid access token. |
302 token_service()->RespondToAccessTokenRequest( | 310 token_service()->RespondToAccessTokenRequest( |
303 GoogleServiceAuthError::AuthErrorNone()); | 311 GoogleServiceAuthError::AuthErrorNone()); |
304 RunMessageLoop(); | 312 RunMessageLoop(); |
305 // Catch histogram entries. | 313 // Catch histogram entries. |
306 base::HistogramTester histogram_tester; | 314 base::HistogramTester histogram_tester; |
307 // Check that there is outstanding URLFetcher request and complete it. | 315 // Check that there is outstanding URLFetcher request and complete it. |
308 CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID); | 316 CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID); |
309 // Verify that the response code was logged properly. | 317 // Verify that the response code was logged properly. |
310 histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode", | 318 histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode", |
311 net::HTTP_OK, 1); | 319 net::HTTP_OK, 1); |
312 // Verify that callback was called for the right id with the right result. | 320 // Verify that callback was called for the right id with the right result. |
313 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS); | 321 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS); |
314 } | 322 } |
315 | 323 |
316 TEST_F(AttachmentDownloaderImplTest, SameIdMultipleDownloads) { | 324 TEST_F(AttachmentDownloaderImplTest, SameIdMultipleDownloads) { |
317 AttachmentId id1 = AttachmentId::Create(); | 325 AttachmentId id1 = attachment_id(); |
318 base::HistogramTester histogram_tester; | 326 base::HistogramTester histogram_tester; |
319 // Call DownloadAttachment two times for the same id. | 327 // Call DownloadAttachment two times for the same id. |
320 downloader()->DownloadAttachment(id1, download_callback(id1)); | 328 downloader()->DownloadAttachment(id1, download_callback(id1)); |
321 downloader()->DownloadAttachment(id1, download_callback(id1)); | 329 downloader()->DownloadAttachment(id1, download_callback(id1)); |
322 RunMessageLoop(); | 330 RunMessageLoop(); |
323 // Return valid access token. | 331 // Return valid access token. |
324 token_service()->RespondToAccessTokenRequest( | 332 token_service()->RespondToAccessTokenRequest( |
325 GoogleServiceAuthError::AuthErrorNone()); | 333 GoogleServiceAuthError::AuthErrorNone()); |
326 RunMessageLoop(); | 334 RunMessageLoop(); |
327 // Start one more download after access token is received. | 335 // Start one more download after access token is received. |
(...skipping 16 matching lines...) Expand all Loading... |
344 // Complete URLFetcher request. | 352 // Complete URLFetcher request. |
345 CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID); | 353 CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID); |
346 // Verify that all download requests completed. | 354 // Verify that all download requests completed. |
347 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS); | 355 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS); |
348 EXPECT_EQ(4, num_completed_downloads()); | 356 EXPECT_EQ(4, num_completed_downloads()); |
349 histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode", | 357 histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode", |
350 net::HTTP_OK, 2); | 358 net::HTTP_OK, 2); |
351 } | 359 } |
352 | 360 |
353 TEST_F(AttachmentDownloaderImplTest, RequestAccessTokenFails) { | 361 TEST_F(AttachmentDownloaderImplTest, RequestAccessTokenFails) { |
354 AttachmentId id1 = AttachmentId::Create(); | 362 AttachmentId id1 = attachment_id(); |
355 AttachmentId id2 = AttachmentId::Create(); | 363 AttachmentId id2 = AttachmentId::Create(id1.GetSize(), id1.GetCrc32c()); |
356 // Trigger first RequestAccessToken. | 364 // Trigger first RequestAccessToken. |
357 downloader()->DownloadAttachment(id1, download_callback(id1)); | 365 downloader()->DownloadAttachment(id1, download_callback(id1)); |
358 RunMessageLoop(); | 366 RunMessageLoop(); |
359 // Return valid access token. | 367 // Return valid access token. |
360 token_service()->RespondToAccessTokenRequest( | 368 token_service()->RespondToAccessTokenRequest( |
361 GoogleServiceAuthError::AuthErrorNone()); | 369 GoogleServiceAuthError::AuthErrorNone()); |
362 RunMessageLoop(); | 370 RunMessageLoop(); |
363 // Trigger second RequestAccessToken. | 371 // Trigger second RequestAccessToken. |
364 downloader()->DownloadAttachment(id2, download_callback(id2)); | 372 downloader()->DownloadAttachment(id2, download_callback(id2)); |
365 RunMessageLoop(); | 373 RunMessageLoop(); |
366 // Fail RequestAccessToken. | 374 // Fail RequestAccessToken. |
367 token_service()->RespondToAccessTokenRequest( | 375 token_service()->RespondToAccessTokenRequest( |
368 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | 376 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
369 RunMessageLoop(); | 377 RunMessageLoop(); |
370 // Only id2 should fail. | 378 // Only id2 should fail. |
371 VerifyDownloadResult(id2, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR); | 379 VerifyDownloadResult(id2, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR); |
372 // Complete request for id1. | 380 // Complete request for id1. |
373 CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID); | 381 CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID); |
374 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS); | 382 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS); |
375 } | 383 } |
376 | 384 |
377 TEST_F(AttachmentDownloaderImplTest, URLFetcher_BadToken) { | 385 TEST_F(AttachmentDownloaderImplTest, URLFetcher_BadToken) { |
378 AttachmentId id1 = AttachmentId::Create(); | 386 AttachmentId id1 = attachment_id(); |
379 downloader()->DownloadAttachment(id1, download_callback(id1)); | 387 downloader()->DownloadAttachment(id1, download_callback(id1)); |
380 RunMessageLoop(); | 388 RunMessageLoop(); |
381 // Return valid access token. | 389 // Return valid access token. |
382 token_service()->RespondToAccessTokenRequest( | 390 token_service()->RespondToAccessTokenRequest( |
383 GoogleServiceAuthError::AuthErrorNone()); | 391 GoogleServiceAuthError::AuthErrorNone()); |
384 RunMessageLoop(); | 392 RunMessageLoop(); |
385 // Fail URLFetcher. This should trigger download failure and access token | 393 // Fail URLFetcher. This should trigger download failure and access token |
386 // invalidation. | 394 // invalidation. |
387 base::HistogramTester histogram_tester; | 395 base::HistogramTester histogram_tester; |
388 CompleteDownload(net::HTTP_UNAUTHORIZED, HASH_HEADER_VALID); | 396 CompleteDownload(net::HTTP_UNAUTHORIZED, HASH_HEADER_VALID); |
389 EXPECT_EQ(1, token_service()->num_invalidate_token()); | 397 EXPECT_EQ(1, token_service()->num_invalidate_token()); |
390 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR); | 398 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR); |
391 histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode", | 399 histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode", |
392 net::HTTP_UNAUTHORIZED, 1); | 400 net::HTTP_UNAUTHORIZED, 1); |
393 } | 401 } |
394 | 402 |
395 TEST_F(AttachmentDownloaderImplTest, URLFetcher_ServiceUnavailable) { | 403 TEST_F(AttachmentDownloaderImplTest, URLFetcher_ServiceUnavailable) { |
396 AttachmentId id1 = AttachmentId::Create(); | 404 AttachmentId id1 = attachment_id(); |
397 downloader()->DownloadAttachment(id1, download_callback(id1)); | 405 downloader()->DownloadAttachment(id1, download_callback(id1)); |
398 RunMessageLoop(); | 406 RunMessageLoop(); |
399 // Return valid access token. | 407 // Return valid access token. |
400 token_service()->RespondToAccessTokenRequest( | 408 token_service()->RespondToAccessTokenRequest( |
401 GoogleServiceAuthError::AuthErrorNone()); | 409 GoogleServiceAuthError::AuthErrorNone()); |
402 RunMessageLoop(); | 410 RunMessageLoop(); |
403 // Fail URLFetcher. This should trigger download failure. Access token | 411 // Fail URLFetcher. This should trigger download failure. Access token |
404 // shouldn't be invalidated. | 412 // shouldn't be invalidated. |
405 base::HistogramTester histogram_tester; | 413 base::HistogramTester histogram_tester; |
406 CompleteDownload(net::HTTP_SERVICE_UNAVAILABLE, HASH_HEADER_VALID); | 414 CompleteDownload(net::HTTP_SERVICE_UNAVAILABLE, HASH_HEADER_VALID); |
407 EXPECT_EQ(0, token_service()->num_invalidate_token()); | 415 EXPECT_EQ(0, token_service()->num_invalidate_token()); |
408 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR); | 416 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR); |
409 histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode", | 417 histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode", |
410 net::HTTP_SERVICE_UNAVAILABLE, 1); | 418 net::HTTP_SERVICE_UNAVAILABLE, 1); |
411 } | 419 } |
412 | 420 |
413 // Verify that if no hash is present on the response the downloader accepts the | 421 // Verify that if no hash is present on the response the downloader accepts the |
414 // received attachment. | 422 // received attachment. |
415 TEST_F(AttachmentDownloaderImplTest, NoHash) { | 423 TEST_F(AttachmentDownloaderImplTest, NoHash) { |
416 AttachmentId id1 = AttachmentId::Create(); | 424 AttachmentId id1 = attachment_id(); |
417 downloader()->DownloadAttachment(id1, download_callback(id1)); | 425 downloader()->DownloadAttachment(id1, download_callback(id1)); |
418 RunMessageLoop(); | 426 RunMessageLoop(); |
419 token_service()->RespondToAccessTokenRequest( | 427 token_service()->RespondToAccessTokenRequest( |
420 GoogleServiceAuthError::AuthErrorNone()); | 428 GoogleServiceAuthError::AuthErrorNone()); |
421 RunMessageLoop(); | 429 RunMessageLoop(); |
422 CompleteDownload(net::HTTP_OK, HASH_HEADER_NONE); | 430 CompleteDownload(net::HTTP_OK, HASH_HEADER_NONE); |
423 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS); | 431 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS); |
424 } | 432 } |
425 | 433 |
426 // Verify that if an invalid hash is present on the response the downloader | 434 // Verify that if an invalid hash is present on the response the downloader |
427 // treats it as a transient error. | 435 // treats it as a transient error. |
428 TEST_F(AttachmentDownloaderImplTest, InvalidHash) { | 436 TEST_F(AttachmentDownloaderImplTest, InvalidHash) { |
429 AttachmentId id1 = AttachmentId::Create(); | 437 AttachmentId id1 = attachment_id(); |
430 downloader()->DownloadAttachment(id1, download_callback(id1)); | 438 downloader()->DownloadAttachment(id1, download_callback(id1)); |
431 RunMessageLoop(); | 439 RunMessageLoop(); |
432 token_service()->RespondToAccessTokenRequest( | 440 token_service()->RespondToAccessTokenRequest( |
433 GoogleServiceAuthError::AuthErrorNone()); | 441 GoogleServiceAuthError::AuthErrorNone()); |
434 RunMessageLoop(); | 442 RunMessageLoop(); |
435 CompleteDownload(net::HTTP_OK, HASH_HEADER_INVALID); | 443 CompleteDownload(net::HTTP_OK, HASH_HEADER_INVALID); |
436 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR); | 444 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR); |
437 } | 445 } |
438 | 446 |
439 // Verify that extract fails when there is no headers object. | 447 // Verify that extract fails when there is no headers object. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 raw += "\n"; | 513 raw += "\n"; |
506 std::replace(raw.begin(), raw.end(), '\n', '\0'); | 514 std::replace(raw.begin(), raw.end(), '\n', '\0'); |
507 scoped_refptr<net::HttpResponseHeaders> headers( | 515 scoped_refptr<net::HttpResponseHeaders> headers( |
508 new net::HttpResponseHeaders(raw)); | 516 new net::HttpResponseHeaders(raw)); |
509 uint32_t extracted; | 517 uint32_t extracted; |
510 ASSERT_FALSE( | 518 ASSERT_FALSE( |
511 AttachmentDownloaderImpl::ExtractCrc32c(headers.get(), &extracted)); | 519 AttachmentDownloaderImpl::ExtractCrc32c(headers.get(), &extracted)); |
512 } | 520 } |
513 | 521 |
514 } // namespace syncer | 522 } // namespace syncer |
OLD | NEW |