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