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 contains download browser tests that are known to be runnable | 5 // This file contains download browser tests that are known to be runnable |
6 // in a pure content context. Over time tests should be migrated here. | 6 // in a pure content context. Over time tests should be migrated here. |
7 | 7 |
8 #include <vector> | |
9 | |
10 #include "base/callback_helpers.h" | |
8 #include "base/command_line.h" | 11 #include "base/command_line.h" |
9 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
11 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
15 #include "base/format_macros.h" | |
12 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
13 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
15 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
16 #include "base/time/time.h" | 20 #include "base/time/time.h" |
17 #include "content/browser/byte_stream.h" | 21 #include "content/browser/byte_stream.h" |
18 #include "content/browser/download/download_file_factory.h" | 22 #include "content/browser/download/download_file_factory.h" |
19 #include "content/browser/download/download_file_impl.h" | 23 #include "content/browser/download/download_file_impl.h" |
20 #include "content/browser/download/download_item_impl.h" | 24 #include "content/browser/download/download_item_impl.h" |
21 #include "content/browser/download/download_manager_impl.h" | 25 #include "content/browser/download/download_manager_impl.h" |
22 #include "content/browser/download/download_resource_handler.h" | 26 #include "content/browser/download/download_resource_handler.h" |
23 #include "content/browser/web_contents/web_contents_impl.h" | 27 #include "content/browser/web_contents/web_contents_impl.h" |
24 #include "content/public/browser/power_save_blocker.h" | 28 #include "content/public/browser/power_save_blocker.h" |
25 #include "content/public/common/content_switches.h" | 29 #include "content/public/common/content_switches.h" |
26 #include "content/public/common/webplugininfo.h" | 30 #include "content/public/common/webplugininfo.h" |
27 #include "content/public/test/browser_test_utils.h" | 31 #include "content/public/test/browser_test_utils.h" |
28 #include "content/public/test/content_browser_test.h" | 32 #include "content/public/test/content_browser_test.h" |
29 #include "content/public/test/content_browser_test_utils.h" | 33 #include "content/public/test/content_browser_test_utils.h" |
30 #include "content/public/test/download_test_observer.h" | 34 #include "content/public/test/download_test_observer.h" |
35 #include "content/public/test/test_download_request_handler.h" | |
31 #include "content/public/test/test_file_error_injector.h" | 36 #include "content/public/test/test_file_error_injector.h" |
32 #include "content/public/test/test_utils.h" | 37 #include "content/public/test/test_utils.h" |
33 #include "content/shell/browser/shell.h" | 38 #include "content/shell/browser/shell.h" |
34 #include "content/shell/browser/shell_browser_context.h" | 39 #include "content/shell/browser/shell_browser_context.h" |
35 #include "content/shell/browser/shell_download_manager_delegate.h" | 40 #include "content/shell/browser/shell_download_manager_delegate.h" |
36 #include "content/shell/browser/shell_network_delegate.h" | 41 #include "content/shell/browser/shell_network_delegate.h" |
37 #include "net/test/embedded_test_server/embedded_test_server.h" | 42 #include "net/test/embedded_test_server/embedded_test_server.h" |
38 #include "net/test/embedded_test_server/http_request.h" | 43 #include "net/test/embedded_test_server/http_request.h" |
39 #include "net/test/embedded_test_server/http_response.h" | 44 #include "net/test/embedded_test_server/http_response.h" |
40 #include "net/test/spawned_test_server/spawned_test_server.h" | |
41 #include "net/test/url_request/url_request_mock_http_job.h" | 45 #include "net/test/url_request/url_request_mock_http_job.h" |
42 #include "net/test/url_request/url_request_slow_download_job.h" | 46 #include "net/test/url_request/url_request_slow_download_job.h" |
43 #include "testing/gmock/include/gmock/gmock.h" | 47 #include "testing/gmock/include/gmock/gmock.h" |
44 #include "testing/gtest/include/gtest/gtest.h" | 48 #include "testing/gtest/include/gtest/gtest.h" |
45 #include "url/gurl.h" | 49 #include "url/gurl.h" |
46 | 50 |
47 #if defined(ENABLE_PLUGINS) | 51 #if defined(ENABLE_PLUGINS) |
48 #include "content/browser/plugin_service_impl.h" | 52 #include "content/browser/plugin_service_impl.h" |
49 #endif | 53 #endif |
50 | 54 |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 | 375 |
372 void GetDelayedCallbacks( | 376 void GetDelayedCallbacks( |
373 std::vector<DownloadOpenDelayedCallback>* callbacks) { | 377 std::vector<DownloadOpenDelayedCallback>* callbacks) { |
374 callbacks->swap(delayed_callbacks_); | 378 callbacks->swap(delayed_callbacks_); |
375 } | 379 } |
376 private: | 380 private: |
377 bool delay_download_open_; | 381 bool delay_download_open_; |
378 std::vector<DownloadOpenDelayedCallback> delayed_callbacks_; | 382 std::vector<DownloadOpenDelayedCallback> delayed_callbacks_; |
379 }; | 383 }; |
380 | 384 |
381 // Record all state transitions and byte counts on the observed download. | |
382 class RecordingDownloadObserver : DownloadItem::Observer { | |
383 public: | |
384 struct RecordStruct { | |
385 DownloadItem::DownloadState state; | |
386 int bytes_received; | |
387 }; | |
388 | |
389 typedef std::vector<RecordStruct> RecordVector; | |
390 | |
391 RecordingDownloadObserver(DownloadItem* download) | |
392 : download_(download) { | |
393 last_state_.state = download->GetState(); | |
394 last_state_.bytes_received = download->GetReceivedBytes(); | |
395 download_->AddObserver(this); | |
396 } | |
397 | |
398 ~RecordingDownloadObserver() override { RemoveObserver(); } | |
399 | |
400 void CompareToExpectedRecord(const RecordStruct expected[], size_t size) { | |
401 EXPECT_EQ(size, record_.size()); | |
402 int min = size > record_.size() ? record_.size() : size; | |
403 for (int i = 0; i < min; ++i) { | |
404 EXPECT_EQ(expected[i].state, record_[i].state) << "Iteration " << i; | |
405 EXPECT_EQ(expected[i].bytes_received, record_[i].bytes_received) | |
406 << "Iteration " << i; | |
407 } | |
408 } | |
409 | |
410 private: | |
411 void OnDownloadUpdated(DownloadItem* download) override { | |
412 DCHECK_EQ(download_, download); | |
413 DownloadItem::DownloadState state = download->GetState(); | |
414 int bytes = download->GetReceivedBytes(); | |
415 if (last_state_.state != state || last_state_.bytes_received > bytes) { | |
416 last_state_.state = state; | |
417 last_state_.bytes_received = bytes; | |
418 record_.push_back(last_state_); | |
419 } | |
420 } | |
421 | |
422 void OnDownloadDestroyed(DownloadItem* download) override { | |
423 DCHECK_EQ(download_, download); | |
424 RemoveObserver(); | |
425 } | |
426 | |
427 void RemoveObserver() { | |
428 if (download_) { | |
429 download_->RemoveObserver(this); | |
430 download_ = NULL; | |
431 } | |
432 } | |
433 | |
434 DownloadItem* download_; | |
435 RecordStruct last_state_; | |
436 RecordVector record_; | |
437 }; | |
438 | |
439 // Get the next created download. | 385 // Get the next created download. |
440 class DownloadCreateObserver : DownloadManager::Observer { | 386 class DownloadCreateObserver : DownloadManager::Observer { |
441 public: | 387 public: |
442 DownloadCreateObserver(DownloadManager* manager) | 388 DownloadCreateObserver(DownloadManager* manager) |
443 : manager_(manager), | 389 : manager_(manager), item_(NULL) { |
444 item_(NULL), | |
445 waiting_(false) { | |
446 manager_->AddObserver(this); | 390 manager_->AddObserver(this); |
447 } | 391 } |
448 | 392 |
449 ~DownloadCreateObserver() override { | 393 ~DownloadCreateObserver() override { |
450 if (manager_) | 394 if (manager_) |
451 manager_->RemoveObserver(this); | 395 manager_->RemoveObserver(this); |
452 manager_ = NULL; | 396 manager_ = NULL; |
453 } | 397 } |
454 | 398 |
455 void ManagerGoingDown(DownloadManager* manager) override { | 399 void ManagerGoingDown(DownloadManager* manager) override { |
456 DCHECK_EQ(manager_, manager); | 400 DCHECK_EQ(manager_, manager); |
457 manager_->RemoveObserver(this); | 401 manager_->RemoveObserver(this); |
458 manager_ = NULL; | 402 manager_ = NULL; |
459 } | 403 } |
460 | 404 |
461 void OnDownloadCreated(DownloadManager* manager, | 405 void OnDownloadCreated(DownloadManager* manager, |
462 DownloadItem* download) override { | 406 DownloadItem* download) override { |
463 if (!item_) | 407 if (!item_) |
464 item_ = download; | 408 item_ = download; |
465 | 409 |
466 if (waiting_) | 410 if (!completion_closure_.is_null()) |
467 base::MessageLoopForUI::current()->QuitWhenIdle(); | 411 base::ResetAndReturn(&completion_closure_).Run(); |
468 } | 412 } |
469 | 413 |
470 DownloadItem* WaitForFinished() { | 414 DownloadItem* WaitForFinished() { |
471 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 415 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
472 if (!item_) { | 416 if (!item_) { |
473 waiting_ = true; | 417 base::RunLoop run_loop; |
474 RunMessageLoop(); | 418 completion_closure_ = run_loop.QuitClosure(); |
475 waiting_ = false; | 419 run_loop.Run(); |
476 } | 420 } |
477 return item_; | 421 return item_; |
478 } | 422 } |
479 | 423 |
480 private: | 424 private: |
481 DownloadManager* manager_; | 425 DownloadManager* manager_; |
482 DownloadItem* item_; | 426 DownloadItem* item_; |
483 bool waiting_; | 427 base::Closure completion_closure_; |
484 }; | 428 }; |
485 | 429 |
486 | |
487 // Filter for waiting for a certain number of bytes. | |
488 bool DataReceivedFilter(int number_of_bytes, DownloadItem* download) { | |
489 return download->GetReceivedBytes() >= number_of_bytes; | |
490 } | |
491 | |
492 // Filter for download completion. | 430 // Filter for download completion. |
493 bool DownloadCompleteFilter(DownloadItem* download) { | 431 bool DownloadCompleteFilter(DownloadItem* download) { |
494 return download->GetState() == DownloadItem::COMPLETE; | 432 return download->GetState() == DownloadItem::COMPLETE; |
495 } | 433 } |
496 | 434 |
497 // Filter for saving the size of the download when the first IN_PROGRESS | 435 bool DownloadInterruptFilter(DownloadItem* download) { |
498 // is hit. | 436 return download->GetState() == DownloadItem::INTERRUPTED; |
499 bool InitialSizeFilter(int* download_size, DownloadItem* download) { | 437 } |
500 if (download->GetState() != DownloadItem::IN_PROGRESS) | |
501 return false; | |
502 | 438 |
503 *download_size = download->GetReceivedBytes(); | 439 bool DownloadInProgressFilter(DownloadItem* download) { |
504 return true; | 440 return download->GetState() == DownloadItem::IN_PROGRESS; |
505 } | 441 } |
506 | 442 |
507 // Request handler to be used with CreateRedirectHandler(). | 443 // Request handler to be used with CreateRedirectHandler(). |
508 scoped_ptr<net::test_server::HttpResponse> HandleRequestAndSendRedirectResponse( | 444 scoped_ptr<net::test_server::HttpResponse> HandleRequestAndSendRedirectResponse( |
509 const std::string& relative_url, | 445 const std::string& relative_url, |
510 const GURL& target_url, | 446 const GURL& target_url, |
511 const net::test_server::HttpRequest& request) { | 447 const net::test_server::HttpRequest& request) { |
512 scoped_ptr<net::test_server::BasicHttpResponse> response; | 448 scoped_ptr<net::test_server::BasicHttpResponse> response; |
513 if (request.relative_url == relative_url) { | 449 if (request.relative_url == relative_url) { |
514 response.reset(new net::test_server::BasicHttpResponse); | 450 response.reset(new net::test_server::BasicHttpResponse); |
515 response->set_code(net::HTTP_FOUND); | 451 response->set_code(net::HTTP_FOUND); |
516 response->AddCustomHeader("Location", target_url.spec()); | 452 response->AddCustomHeader("Location", target_url.spec()); |
517 } | 453 } |
518 return response.Pass(); | 454 return response.Pass(); |
519 } | 455 } |
520 | 456 |
521 // Creates a request handler for EmbeddedTestServer that responds with a HTTP | 457 // Creates a request handler for EmbeddedTestServer that responds with a HTTP |
522 // 302 redirect if the request URL matches |relative_url|. | 458 // 302 redirect if the request URL matches |relative_url|. |
523 net::EmbeddedTestServer::HandleRequestCallback CreateRedirectHandler( | 459 net::EmbeddedTestServer::HandleRequestCallback CreateRedirectHandler( |
524 const std::string& relative_url, | 460 const std::string& relative_url, |
525 const GURL& target_url) { | 461 const GURL& target_url) { |
526 return base::Bind( | 462 return base::Bind( |
527 &HandleRequestAndSendRedirectResponse, relative_url, target_url); | 463 &HandleRequestAndSendRedirectResponse, relative_url, target_url); |
528 } | 464 } |
529 | 465 |
530 // Request handler to be used with CreateBasicResponseHandler(). | 466 // Request handler to be used with CreateBasicResponseHandler(). |
531 scoped_ptr<net::test_server::HttpResponse> HandleRequestAndSendBasicResponse( | 467 scoped_ptr<net::test_server::HttpResponse> HandleRequestAndSendBasicResponse( |
532 const std::string& relative_url, | 468 const std::string& relative_url, |
469 const base::StringPairs& headers, | |
533 const std::string& content_type, | 470 const std::string& content_type, |
534 const std::string& body, | 471 const std::string& body, |
535 const net::test_server::HttpRequest& request) { | 472 const net::test_server::HttpRequest& request) { |
536 scoped_ptr<net::test_server::BasicHttpResponse> response; | 473 scoped_ptr<net::test_server::BasicHttpResponse> response; |
537 if (request.relative_url == relative_url) { | 474 if (request.relative_url == relative_url) { |
538 response.reset(new net::test_server::BasicHttpResponse); | 475 response.reset(new net::test_server::BasicHttpResponse); |
476 for (const auto& pair : headers) | |
477 response->AddCustomHeader(pair.first, pair.second); | |
539 response->set_content_type(content_type); | 478 response->set_content_type(content_type); |
540 response->set_content(body); | 479 response->set_content(body); |
541 } | 480 } |
542 return response.Pass(); | 481 return response.Pass(); |
543 } | 482 } |
544 | 483 |
545 // Creates a request handler for an EmbeddedTestServer that response with an | 484 // Creates a request handler for an EmbeddedTestServer that response with an |
546 // HTTP 200 status code, a Content-Type header and a body. | 485 // HTTP 200 status code, a Content-Type header and a body. |
547 net::EmbeddedTestServer::HandleRequestCallback CreateBasicResponseHandler( | 486 net::EmbeddedTestServer::HandleRequestCallback CreateBasicResponseHandler( |
548 const std::string& relative_url, | 487 const std::string& relative_url, |
488 const base::StringPairs& headers, | |
549 const std::string& content_type, | 489 const std::string& content_type, |
550 const std::string& body) { | 490 const std::string& body) { |
551 return base::Bind( | 491 return base::Bind(&HandleRequestAndSendBasicResponse, relative_url, headers, |
552 &HandleRequestAndSendBasicResponse, relative_url, content_type, body); | 492 content_type, body); |
553 } | 493 } |
554 | 494 |
555 } // namespace | |
556 | |
557 class DownloadContentTest : public ContentBrowserTest { | 495 class DownloadContentTest : public ContentBrowserTest { |
558 protected: | 496 protected: |
559 // An initial send from a website of at least this size will not be | |
560 // help up by buffering in the underlying downloads ByteStream data | |
561 // transfer. This is important because on resumption tests we wait | |
562 // until we've gotten the data we expect before allowing the test server | |
563 // to send its reset, to get around hard close semantics on the Windows | |
564 // socket layer implementation. | |
565 int GetSafeBufferChunk() const { | |
566 return (DownloadResourceHandler::kDownloadByteStreamSize / | |
567 ByteStreamWriter::kFractionBufferBeforeSending) + 1; | |
568 } | |
569 | |
570 void SetUpOnMainThread() override { | 497 void SetUpOnMainThread() override { |
571 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); | 498 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); |
572 | 499 |
573 test_delegate_.reset(new TestShellDownloadManagerDelegate()); | 500 test_delegate_.reset(new TestShellDownloadManagerDelegate()); |
574 test_delegate_->SetDownloadBehaviorForTesting(downloads_directory_.path()); | 501 test_delegate_->SetDownloadBehaviorForTesting(downloads_directory_.path()); |
575 DownloadManager* manager = DownloadManagerForShell(shell()); | 502 DownloadManager* manager = DownloadManagerForShell(shell()); |
576 manager->GetDelegate()->Shutdown(); | 503 manager->GetDelegate()->Shutdown(); |
577 manager->SetDelegate(test_delegate_.get()); | 504 manager->SetDelegate(test_delegate_.get()); |
578 test_delegate_->SetDownloadManager(manager); | 505 test_delegate_->SetDownloadManager(manager); |
579 | 506 |
(...skipping 14 matching lines...) Expand all Loading... | |
594 | 521 |
595 // Create a DownloadTestObserverTerminal that will wait for the | 522 // Create a DownloadTestObserverTerminal that will wait for the |
596 // specified number of downloads to finish. | 523 // specified number of downloads to finish. |
597 DownloadTestObserver* CreateWaiter( | 524 DownloadTestObserver* CreateWaiter( |
598 Shell* shell, int num_downloads) { | 525 Shell* shell, int num_downloads) { |
599 DownloadManager* download_manager = DownloadManagerForShell(shell); | 526 DownloadManager* download_manager = DownloadManagerForShell(shell); |
600 return new DownloadTestObserverTerminal(download_manager, num_downloads, | 527 return new DownloadTestObserverTerminal(download_manager, num_downloads, |
601 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); | 528 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); |
602 } | 529 } |
603 | 530 |
604 // Create a DownloadTestObserverInProgress that will wait for the | 531 void WaitForInterrupt(DownloadItem* download) { |
605 // specified number of downloads to start. | 532 DownloadUpdatedObserver(download, base::Bind(&DownloadInterruptFilter)) |
606 DownloadCreateObserver* CreateInProgressWaiter( | 533 .WaitForEvent(); |
607 Shell* shell, int num_downloads) { | |
608 DownloadManager* download_manager = DownloadManagerForShell(shell); | |
609 return new DownloadCreateObserver(download_manager); | |
610 } | 534 } |
611 | 535 |
612 DownloadTestObserver* CreateInterruptedWaiter( | 536 void WaitForInProgress(DownloadItem* download) { |
613 Shell* shell, int num_downloads) { | 537 DownloadUpdatedObserver(download, base::Bind(&DownloadInProgressFilter)) |
614 DownloadManager* download_manager = DownloadManagerForShell(shell); | 538 .WaitForEvent(); |
615 return new DownloadTestObserverInterrupted(download_manager, num_downloads, | 539 } |
616 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); | 540 |
541 void WaitForCompletion(DownloadItem* download) { | |
542 DownloadUpdatedObserver(download, base::Bind(&DownloadCompleteFilter)) | |
543 .WaitForEvent(); | |
617 } | 544 } |
618 | 545 |
619 // Note: Cannot be used with other alternative DownloadFileFactorys | 546 // Note: Cannot be used with other alternative DownloadFileFactorys |
620 void SetupEnsureNoPendingDownloads() { | 547 void SetupEnsureNoPendingDownloads() { |
621 DownloadManagerForShell(shell())->SetDownloadFileFactoryForTesting( | 548 DownloadManagerForShell(shell())->SetDownloadFileFactoryForTesting( |
622 scoped_ptr<DownloadFileFactory>( | 549 scoped_ptr<DownloadFileFactory>( |
623 new CountingDownloadFileFactory()).Pass()); | 550 new CountingDownloadFileFactory()).Pass()); |
624 } | 551 } |
625 | 552 |
626 bool EnsureNoPendingDownloads() { | 553 bool EnsureNoPendingDownloads() { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
668 EXPECT_EQ(value, file_contents); | 595 EXPECT_EQ(value, file_contents); |
669 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) | 596 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) |
670 return false; | 597 return false; |
671 | 598 |
672 return true; | 599 return true; |
673 } | 600 } |
674 | 601 |
675 // Start a download and return the item. | 602 // Start a download and return the item. |
676 DownloadItem* StartDownloadAndReturnItem(GURL url) { | 603 DownloadItem* StartDownloadAndReturnItem(GURL url) { |
677 scoped_ptr<DownloadCreateObserver> observer( | 604 scoped_ptr<DownloadCreateObserver> observer( |
678 CreateInProgressWaiter(shell(), 1)); | 605 new DownloadCreateObserver(DownloadManagerForShell(shell()))); |
679 NavigateToURL(shell(), url); | 606 NavigateToURL(shell(), url); |
680 observer->WaitForFinished(); | 607 return observer->WaitForFinished(); |
681 std::vector<DownloadItem*> downloads; | |
682 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | |
683 EXPECT_EQ(1u, downloads.size()); | |
684 if (1u != downloads.size()) | |
685 return NULL; | |
686 return downloads[0]; | |
687 } | 608 } |
688 | 609 |
689 // Wait for data | 610 static void ReadAndVerifyFileContents(int seed, |
690 void WaitForData(DownloadItem* download, int size) { | 611 int64_t expected_size, |
691 DownloadUpdatedObserver data_observer( | 612 const base::FilePath& path) { |
692 download, base::Bind(&DataReceivedFilter, size)); | 613 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
693 data_observer.WaitForEvent(); | 614 ASSERT_TRUE(file.IsValid()); |
694 ASSERT_EQ(size, download->GetReceivedBytes()); | 615 int64_t file_length = file.GetLength(); |
695 ASSERT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 616 ASSERT_EQ(expected_size, file_length); |
696 } | |
697 | 617 |
698 // Tell the test server to release a pending RST and confirm | 618 const int64_t kBufferSize = 64 * 1024; |
699 // that the interrupt is received properly (for download resumption | 619 std::vector<char> pattern; |
700 // testing). | 620 std::vector<char> data; |
701 void ReleaseRSTAndConfirmInterruptForResume(DownloadItem* download) { | 621 pattern.resize(kBufferSize); |
702 scoped_ptr<DownloadTestObserver> rst_observer( | 622 data.resize(kBufferSize); |
703 CreateInterruptedWaiter(shell(), 1)); | 623 for (int64_t offset = 0; offset < file_length;) { |
704 NavigateToURL(shell(), spawned_test_server()->GetURL("download-finish")); | 624 int bytes_read = file.Read(offset, &data.front(), kBufferSize); |
705 rst_observer->WaitForFinished(); | 625 ASSERT_LT(0, bytes_read); |
706 EXPECT_EQ(DownloadItem::INTERRUPTED, download->GetState()); | 626 ASSERT_GE(kBufferSize, bytes_read); |
707 } | |
708 | 627 |
709 // Confirm file status expected for the given location in a stream | 628 TestDownloadRequestHandler::GetPatternBytes(seed, offset, bytes_read, |
710 // provided by the resume test server. | 629 &pattern.front()); |
711 void ConfirmFileStatusForResume( | 630 ASSERT_EQ(0, memcmp(&pattern.front(), &data.front(), bytes_read)) |
712 DownloadItem* download, bool file_exists, | 631 << "Comparing block at offset " << offset << " and length " |
713 int received_bytes, int total_bytes, | 632 << bytes_read; |
714 const base::FilePath& expected_filename) { | 633 offset += bytes_read; |
715 // expected_filename is only known if the file exists. | |
716 ASSERT_EQ(file_exists, !expected_filename.empty()); | |
717 EXPECT_EQ(received_bytes, download->GetReceivedBytes()); | |
718 EXPECT_EQ(total_bytes, download->GetTotalBytes()); | |
719 EXPECT_EQ(expected_filename.value(), | |
720 download->GetFullPath().BaseName().value()); | |
721 EXPECT_EQ(file_exists, | |
722 (!download->GetFullPath().empty() && | |
723 base::PathExists(download->GetFullPath()))); | |
724 | |
725 if (file_exists) { | |
726 std::string file_contents; | |
727 EXPECT_TRUE(base::ReadFileToString( | |
728 download->GetFullPath(), &file_contents)); | |
729 | |
730 ASSERT_EQ(static_cast<size_t>(received_bytes), file_contents.size()); | |
731 for (int i = 0; i < received_bytes; ++i) { | |
732 EXPECT_EQ(static_cast<char>((i * 2 + 15) % 256), file_contents[i]) | |
733 << "File contents diverged at position " << i | |
734 << " for " << expected_filename.value(); | |
735 | |
736 if (static_cast<char>((i * 2 + 15) % 256) != file_contents[i]) | |
737 return; | |
738 } | |
739 } | 634 } |
740 } | 635 } |
741 | 636 |
742 private: | 637 private: |
743 static void EnsureNoPendingDownloadJobsOnIO(bool* result) { | 638 static void EnsureNoPendingDownloadJobsOnIO(bool* result) { |
744 if (net::URLRequestSlowDownloadJob::NumberOutstandingRequests()) | 639 if (net::URLRequestSlowDownloadJob::NumberOutstandingRequests()) |
745 *result = false; | 640 *result = false; |
746 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 641 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
747 base::MessageLoop::QuitWhenIdleClosure()); | 642 base::MessageLoop::QuitWhenIdleClosure()); |
748 } | 643 } |
749 | 644 |
750 // Location of the downloads directory for these tests | 645 // Location of the downloads directory for these tests |
751 base::ScopedTempDir downloads_directory_; | 646 base::ScopedTempDir downloads_directory_; |
752 scoped_ptr<TestShellDownloadManagerDelegate> test_delegate_; | 647 scoped_ptr<TestShellDownloadManagerDelegate> test_delegate_; |
753 }; | 648 }; |
754 | 649 |
650 } // namespace | |
651 | |
755 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadCancelled) { | 652 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadCancelled) { |
756 SetupEnsureNoPendingDownloads(); | 653 SetupEnsureNoPendingDownloads(); |
757 | 654 |
758 // Create a download, wait until it's started, and confirm | 655 // Create a download, wait until it's started, and confirm |
759 // we're in the expected state. | 656 // we're in the expected state. |
760 scoped_ptr<DownloadCreateObserver> observer( | 657 DownloadItem* download = StartDownloadAndReturnItem( |
761 CreateInProgressWaiter(shell(), 1)); | 658 GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
762 NavigateToURL(shell(), GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl)); | 659 ASSERT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
763 observer->WaitForFinished(); | |
764 | |
765 std::vector<DownloadItem*> downloads; | |
766 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | |
767 ASSERT_EQ(1u, downloads.size()); | |
768 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->GetState()); | |
769 | 660 |
770 // Cancel the download and wait for download system quiesce. | 661 // Cancel the download and wait for download system quiesce. |
771 downloads[0]->Cancel(true); | 662 download->Cancel(true); |
772 scoped_refptr<DownloadTestFlushObserver> flush_observer( | 663 scoped_refptr<DownloadTestFlushObserver> flush_observer( |
773 new DownloadTestFlushObserver(DownloadManagerForShell(shell()))); | 664 new DownloadTestFlushObserver(DownloadManagerForShell(shell()))); |
774 flush_observer->WaitForFlush(); | 665 flush_observer->WaitForFlush(); |
775 | 666 |
776 // Get the important info from other threads and check it. | 667 // Get the important info from other threads and check it. |
777 EXPECT_TRUE(EnsureNoPendingDownloads()); | 668 EXPECT_TRUE(EnsureNoPendingDownloads()); |
778 } | 669 } |
779 | 670 |
780 // Check that downloading multiple (in this case, 2) files does not result in | 671 // Check that downloading multiple (in this case, 2) files does not result in |
781 // corrupted files. | 672 // corrupted files. |
782 IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) { | 673 IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) { |
783 SetupEnsureNoPendingDownloads(); | 674 SetupEnsureNoPendingDownloads(); |
784 | 675 |
785 // Create a download, wait until it's started, and confirm | 676 // Create a download, wait until it's started, and confirm |
786 // we're in the expected state. | 677 // we're in the expected state. |
787 scoped_ptr<DownloadCreateObserver> observer1( | 678 DownloadItem* download1 = StartDownloadAndReturnItem( |
788 CreateInProgressWaiter(shell(), 1)); | 679 GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
789 NavigateToURL(shell(), GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl)); | 680 ASSERT_EQ(DownloadItem::IN_PROGRESS, download1->GetState()); |
790 observer1->WaitForFinished(); | |
791 | |
792 std::vector<DownloadItem*> downloads; | |
793 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | |
794 ASSERT_EQ(1u, downloads.size()); | |
795 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->GetState()); | |
796 DownloadItem* download1 = downloads[0]; // The only download. | |
797 | 681 |
798 // Start the second download and wait until it's done. | 682 // Start the second download and wait until it's done. |
799 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("download-test.lib")); | 683 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("download-test.lib")); |
800 // Download the file and wait. | 684 DownloadItem* download2 = StartDownloadAndReturnItem(url); |
801 NavigateToURLAndWaitForDownload(shell(), url, DownloadItem::COMPLETE); | 685 WaitForCompletion(download2); |
802 | |
803 // Should now have 2 items on the manager. | |
804 downloads.clear(); | |
805 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | |
806 ASSERT_EQ(2u, downloads.size()); | |
807 // We don't know the order of the downloads. | |
808 DownloadItem* download2 = downloads[(download1 == downloads[0]) ? 1 : 0]; | |
809 | 686 |
810 ASSERT_EQ(DownloadItem::IN_PROGRESS, download1->GetState()); | 687 ASSERT_EQ(DownloadItem::IN_PROGRESS, download1->GetState()); |
811 ASSERT_EQ(DownloadItem::COMPLETE, download2->GetState()); | 688 ASSERT_EQ(DownloadItem::COMPLETE, download2->GetState()); |
812 | 689 |
813 // Allow the first request to finish. | 690 // Allow the first request to finish. |
814 scoped_ptr<DownloadTestObserver> observer2(CreateWaiter(shell(), 1)); | 691 scoped_ptr<DownloadTestObserver> observer2(CreateWaiter(shell(), 1)); |
815 NavigateToURL(shell(), | 692 NavigateToURL(shell(), |
816 GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl)); | 693 GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl)); |
817 observer2->WaitForFinished(); // Wait for the third request. | 694 observer2->WaitForFinished(); // Wait for the third request. |
818 EXPECT_EQ(1u, observer2->NumDownloadsSeenInState(DownloadItem::COMPLETE)); | 695 EXPECT_EQ(1u, observer2->NumDownloadsSeenInState(DownloadItem::COMPLETE)); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
955 delayed_callbacks[0].Run(true); | 832 delayed_callbacks[0].Run(true); |
956 | 833 |
957 // *Now* the download should be complete. | 834 // *Now* the download should be complete. |
958 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); | 835 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); |
959 } | 836 } |
960 | 837 |
961 // Try to shutdown with a download in progress to make sure shutdown path | 838 // Try to shutdown with a download in progress to make sure shutdown path |
962 // works properly. | 839 // works properly. |
963 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) { | 840 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) { |
964 // Create a download that won't complete. | 841 // Create a download that won't complete. |
965 scoped_ptr<DownloadCreateObserver> observer( | 842 DownloadItem* download = StartDownloadAndReturnItem( |
966 CreateInProgressWaiter(shell(), 1)); | 843 GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
967 NavigateToURL(shell(), GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl)); | |
968 observer->WaitForFinished(); | |
969 | 844 |
970 // Get the item. | 845 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
971 std::vector<DownloadItem*> items; | |
972 DownloadManagerForShell(shell())->GetAllDownloads(&items); | |
973 ASSERT_EQ(1u, items.size()); | |
974 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | |
975 | 846 |
976 // Shutdown the download manager and make sure we get the right | 847 // Shutdown the download manager and make sure we get the right |
977 // notifications in the right order. | 848 // notifications in the right order. |
978 StrictMock<MockDownloadItemObserver> item_observer; | 849 StrictMock<MockDownloadItemObserver> item_observer; |
979 items[0]->AddObserver(&item_observer); | 850 download->AddObserver(&item_observer); |
980 MockDownloadManagerObserver manager_observer( | 851 MockDownloadManagerObserver manager_observer( |
981 DownloadManagerForShell(shell())); | 852 DownloadManagerForShell(shell())); |
982 // Don't care about ModelChanged() events. | 853 // Don't care about ModelChanged() events. |
983 EXPECT_CALL(manager_observer, ModelChanged(_)) | 854 EXPECT_CALL(manager_observer, ModelChanged(_)) |
984 .WillRepeatedly(Return()); | 855 .WillRepeatedly(Return()); |
985 { | 856 { |
986 InSequence notifications; | 857 InSequence notifications; |
987 | 858 |
988 EXPECT_CALL(manager_observer, MockManagerGoingDown( | 859 EXPECT_CALL(manager_observer, MockManagerGoingDown( |
989 DownloadManagerForShell(shell()))) | 860 DownloadManagerForShell(shell()))) |
990 .WillOnce(Return()); | 861 .WillOnce(Return()); |
991 EXPECT_CALL(item_observer, OnDownloadUpdated( | 862 EXPECT_CALL( |
992 AllOf(items[0], | 863 item_observer, |
993 Property(&DownloadItem::GetState, DownloadItem::CANCELLED)))) | 864 OnDownloadUpdated(AllOf(download, Property(&DownloadItem::GetState, |
865 DownloadItem::CANCELLED)))) | |
994 .WillOnce(Return()); | 866 .WillOnce(Return()); |
995 EXPECT_CALL(item_observer, OnDownloadDestroyed(items[0])) | 867 EXPECT_CALL(item_observer, OnDownloadDestroyed(download)) |
996 .WillOnce(Return()); | 868 .WillOnce(Return()); |
997 } | 869 } |
998 | 870 |
999 // See http://crbug.com/324525. If we have a refcount release/post task | 871 // See http://crbug.com/324525. If we have a refcount release/post task |
1000 // race, the second post will stall the IO thread long enough so that we'll | 872 // race, the second post will stall the IO thread long enough so that we'll |
1001 // lose the race and crash. The first stall is just to give the UI thread | 873 // lose the race and crash. The first stall is just to give the UI thread |
1002 // a chance to get the second stall onto the IO thread queue after the cancel | 874 // a chance to get the second stall onto the IO thread queue after the cancel |
1003 // message created by Shutdown and before the notification callback | 875 // message created by Shutdown and before the notification callback |
1004 // created by the IO thread in canceling the request. | 876 // created by the IO thread in canceling the request. |
1005 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 877 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
1006 base::Bind(&base::PlatformThread::Sleep, | 878 base::Bind(&base::PlatformThread::Sleep, |
1007 base::TimeDelta::FromMilliseconds(25))); | 879 base::TimeDelta::FromMilliseconds(25))); |
1008 DownloadManagerForShell(shell())->Shutdown(); | 880 DownloadManagerForShell(shell())->Shutdown(); |
1009 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 881 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
1010 base::Bind(&base::PlatformThread::Sleep, | 882 base::Bind(&base::PlatformThread::Sleep, |
1011 base::TimeDelta::FromMilliseconds(25))); | 883 base::TimeDelta::FromMilliseconds(25))); |
1012 items.clear(); | |
1013 } | 884 } |
1014 | 885 |
1015 // Try to shutdown just after we release the download file, by delaying | 886 // Try to shutdown just after we release the download file, by delaying |
1016 // release. | 887 // release. |
1017 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) { | 888 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) { |
1018 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell())); | 889 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell())); |
1019 | 890 |
1020 // Mark delegate for delayed open. | 891 // Mark delegate for delayed open. |
1021 GetDownloadManagerDelegate()->SetDelayedOpen(true); | 892 GetDownloadManagerDelegate()->SetDelayedOpen(true); |
1022 | 893 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1061 | 932 |
1062 MockDownloadItemObserver observer; | 933 MockDownloadItemObserver observer; |
1063 items[0]->AddObserver(&observer); | 934 items[0]->AddObserver(&observer); |
1064 EXPECT_CALL(observer, OnDownloadDestroyed(items[0])); | 935 EXPECT_CALL(observer, OnDownloadDestroyed(items[0])); |
1065 | 936 |
1066 // Shutdown the download manager. Mostly this is confirming a lack of | 937 // Shutdown the download manager. Mostly this is confirming a lack of |
1067 // crashes. | 938 // crashes. |
1068 DownloadManagerForShell(shell())->Shutdown(); | 939 DownloadManagerForShell(shell())->Shutdown(); |
1069 } | 940 } |
1070 | 941 |
1071 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeInterruptedDownload) { | 942 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Resume_BasicWithStrongValidators) { |
1072 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 943 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
1073 switches::kEnableDownloadResumption); | 944 switches::kEnableDownloadResumption); |
1074 ASSERT_TRUE(spawned_test_server()->Start()); | 945 |
1075 | 946 TestDownloadRequestHandler request_handler; |
1076 GURL url = spawned_test_server()->GetURL( | 947 auto parameters = |
1077 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", | 948 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); |
1078 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | 949 auto interruption = parameters.injected_errors.front(); |
1079 | 950 request_handler.StartServing(parameters); |
1080 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); | 951 |
1081 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | 952 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); |
1082 | 953 WaitForInterrupt(download); |
1083 DownloadItem* download(StartDownloadAndReturnItem(url)); | 954 |
1084 WaitForData(download, GetSafeBufferChunk()); | 955 ASSERT_EQ(interruption.offset, download->GetReceivedBytes()); |
1085 ::testing::Mock::VerifyAndClearExpectations(&dm_observer); | 956 ASSERT_EQ(parameters.size, download->GetTotalBytes()); |
1086 | 957 |
1087 // Confirm resumption while in progress doesn't do anything. | 958 download->Resume(); |
1088 download->Resume(); | 959 WaitForCompletion(download); |
1089 ASSERT_EQ(GetSafeBufferChunk(), download->GetReceivedBytes()); | 960 |
1090 ASSERT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 961 ASSERT_EQ(parameters.size, download->GetReceivedBytes()); |
1091 | 962 ASSERT_EQ(parameters.size, download->GetTotalBytes()); |
1092 // Tell the server to send the RST and confirm the interrupt happens. | 963 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents( |
1093 ReleaseRSTAndConfirmInterruptForResume(download); | 964 parameters.pattern_generator_seed, parameters.size, |
1094 ConfirmFileStatusForResume( | 965 download->GetTargetFilePath())); |
1095 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | 966 |
1096 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | 967 // Characterization risk: The next portion of the test examines the requests |
1097 | 968 // that were sent out while downloading our resource. These requests |
1098 // Resume, confirming received bytes on resumption is correct. | 969 // correspond to the requests that were generated by the browser and the |
1099 // Make sure no creation calls are included. | 970 // downloads system and may change as implementation details change. |
1100 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(0); | 971 TestDownloadRequestHandler::CompletedRequests requests; |
1101 int initial_size = 0; | 972 request_handler.GetCompletedRequestInfo(&requests); |
1102 DownloadUpdatedObserver initial_size_observer( | 973 |
1103 download, base::Bind(&InitialSizeFilter, &initial_size)); | 974 ASSERT_EQ(2u, requests.size()); |
1104 download->Resume(); | 975 |
1105 initial_size_observer.WaitForEvent(); | 976 // The first request only transferrs bytes up until the interruption point. |
1106 EXPECT_EQ(GetSafeBufferChunk(), initial_size); | 977 EXPECT_EQ(interruption.offset, requests[0].transferred_content_length); |
1107 ::testing::Mock::VerifyAndClearExpectations(&dm_observer); | 978 |
1108 | 979 // The next request should only have transferred the remainder of the |
1109 // and wait for expected data. | 980 // resource. |
1110 WaitForData(download, GetSafeBufferChunk() * 2); | 981 EXPECT_EQ(parameters.size - interruption.offset, |
1111 | 982 requests[1].transferred_content_length); |
1112 // Tell the server to send the RST and confirm the interrupt happens. | 983 |
1113 ReleaseRSTAndConfirmInterruptForResume(download); | 984 std::string value; |
1114 ConfirmFileStatusForResume( | 985 ASSERT_TRUE(requests[1].request_headers.GetHeader( |
1115 download, true, GetSafeBufferChunk() * 2, GetSafeBufferChunk() * 3, | 986 net::HttpRequestHeaders::kIfRange, &value)); |
1116 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | 987 EXPECT_EQ(parameters.etag, value); |
1117 | 988 |
1118 // Resume and wait for completion. | 989 ASSERT_TRUE(requests[1].request_headers.GetHeader( |
1119 DownloadUpdatedObserver completion_observer( | 990 net::HttpRequestHeaders::kRange, &value)); |
1120 download, base::Bind(DownloadCompleteFilter)); | 991 EXPECT_EQ(base::StringPrintf("bytes=%" PRId64 "-", interruption.offset), |
1121 download->Resume(); | 992 value); |
1122 completion_observer.WaitForEvent(); | 993 } |
1123 | 994 |
1124 ConfirmFileStatusForResume( | 995 // A partial resumption results in an HTTP 200 response. I.e. ther server |
1125 download, true, GetSafeBufferChunk() * 3, GetSafeBufferChunk() * 3, | 996 // ignored the range request and sent the entire resource instead. For If-Range |
1126 base::FilePath(FILE_PATH_LITERAL("rangereset"))); | 997 // requests (as opposed to If-Match), the behavior for a precondition failure is |
1127 | 998 // also to respond with a 200. So this test case covers both validation failure |
1128 // Confirm resumption while complete doesn't do anything. | 999 // and ignoring the range request. |
1129 download->Resume(); | |
1130 ASSERT_EQ(GetSafeBufferChunk() * 3, download->GetReceivedBytes()); | |
1131 ASSERT_EQ(DownloadItem::COMPLETE, download->GetState()); | |
1132 RunAllPendingInMessageLoop(); | |
1133 ASSERT_EQ(GetSafeBufferChunk() * 3, download->GetReceivedBytes()); | |
1134 ASSERT_EQ(DownloadItem::COMPLETE, download->GetState()); | |
1135 } | |
1136 | |
1137 // Confirm restart fallback happens if a range request is bounced. | |
1138 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeInterruptedDownloadNoRange) { | |
1139 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
1140 switches::kEnableDownloadResumption); | |
1141 ASSERT_TRUE(spawned_test_server()->Start()); | |
1142 | |
1143 // Auto-restart if server doesn't handle ranges. | |
1144 GURL url = spawned_test_server()->GetURL(base::StringPrintf( | |
1145 // First download hits an RST, rest don't, no ranges. | |
1146 "rangereset?size=%d&rst_boundary=%d&" | |
1147 "token=NoRange&rst_limit=1&bounce_range", | |
1148 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | |
1149 | |
1150 // Start the download and wait for first data chunk. | |
1151 DownloadItem* download(StartDownloadAndReturnItem(url)); | |
1152 WaitForData(download, GetSafeBufferChunk()); | |
1153 | |
1154 RecordingDownloadObserver recorder(download); | |
1155 | |
1156 ReleaseRSTAndConfirmInterruptForResume(download); | |
1157 ConfirmFileStatusForResume( | |
1158 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | |
1159 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | |
1160 | |
1161 DownloadUpdatedObserver completion_observer( | |
1162 download, base::Bind(DownloadCompleteFilter)); | |
1163 download->Resume(); | |
1164 completion_observer.WaitForEvent(); | |
1165 | |
1166 ConfirmFileStatusForResume( | |
1167 download, true, GetSafeBufferChunk() * 3, GetSafeBufferChunk() * 3, | |
1168 base::FilePath(FILE_PATH_LITERAL("rangereset"))); | |
1169 | |
1170 static const RecordingDownloadObserver::RecordStruct expected_record[] = { | |
1171 // Result of RST | |
1172 {DownloadItem::INTERRUPTED, GetSafeBufferChunk()}, | |
1173 // Starting continuation | |
1174 {DownloadItem::IN_PROGRESS, GetSafeBufferChunk()}, | |
1175 // Notification of receiving whole file. | |
1176 {DownloadItem::IN_PROGRESS, 0}, | |
1177 // Completion. | |
1178 {DownloadItem::COMPLETE, GetSafeBufferChunk() * 3}, | |
1179 }; | |
1180 | |
1181 recorder.CompareToExpectedRecord(expected_record, arraysize(expected_record)); | |
1182 } | |
1183 | |
1184 // Confirm restart fallback happens if a precondition is failed. | |
1185 IN_PROC_BROWSER_TEST_F(DownloadContentTest, | 1000 IN_PROC_BROWSER_TEST_F(DownloadContentTest, |
1186 ResumeInterruptedDownloadBadPrecondition) { | 1001 Resume_RestartIfNotPartialResponse) { |
1187 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 1002 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
1188 switches::kEnableDownloadResumption); | 1003 switches::kEnableDownloadResumption); |
1189 ASSERT_TRUE(spawned_test_server()->Start()); | 1004 const int kOriginalPatternGeneratorSeed = 1; |
1190 | 1005 const int kNewPatternGeneratorSeed = 2; |
1191 GURL url = spawned_test_server()->GetURL(base::StringPrintf( | 1006 |
1192 // First download hits an RST, rest don't, precondition fail. | 1007 TestDownloadRequestHandler::Parameters parameters = |
1193 "rangereset?size=%d&rst_boundary=%d&" | 1008 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); |
1194 "token=BadPrecondition&rst_limit=1&fail_precondition=2", | 1009 parameters.pattern_generator_seed = kOriginalPatternGeneratorSeed; |
1195 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | 1010 const TestDownloadRequestHandler::InjectedError interruption = |
1196 | 1011 parameters.injected_errors.front(); |
1197 // Start the download and wait for first data chunk. | 1012 |
1198 DownloadItem* download(StartDownloadAndReturnItem(url)); | 1013 TestDownloadRequestHandler request_handler; |
1199 WaitForData(download, GetSafeBufferChunk()); | 1014 request_handler.StartServing(parameters); |
1200 | 1015 |
1201 RecordingDownloadObserver recorder(download); | 1016 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); |
1202 | 1017 WaitForInterrupt(download); |
1203 ReleaseRSTAndConfirmInterruptForResume(download); | 1018 |
1204 ConfirmFileStatusForResume( | 1019 ASSERT_EQ(interruption.offset, download->GetReceivedBytes()); |
1205 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | 1020 ASSERT_EQ(parameters.size, download->GetTotalBytes()); |
1206 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | 1021 |
1207 EXPECT_EQ("BadPrecondition2", download->GetETag()); | 1022 parameters = TestDownloadRequestHandler::Parameters(); |
1208 | 1023 parameters.support_byte_ranges = false; |
1209 DownloadUpdatedObserver completion_observer( | 1024 parameters.pattern_generator_seed = kNewPatternGeneratorSeed; |
1210 download, base::Bind(DownloadCompleteFilter)); | 1025 request_handler.StartServing(parameters); |
1211 download->Resume(); | 1026 |
1212 completion_observer.WaitForEvent(); | 1027 download->Resume(); |
1213 | 1028 WaitForCompletion(download); |
1214 ConfirmFileStatusForResume( | 1029 |
1215 download, true, GetSafeBufferChunk() * 3, GetSafeBufferChunk() * 3, | 1030 ASSERT_EQ(parameters.size, download->GetReceivedBytes()); |
1216 base::FilePath(FILE_PATH_LITERAL("rangereset"))); | 1031 ASSERT_EQ(parameters.size, download->GetTotalBytes()); |
1217 EXPECT_EQ("BadPrecondition0", download->GetETag()); | 1032 ASSERT_NO_FATAL_FAILURE( |
1218 | 1033 ReadAndVerifyFileContents(kNewPatternGeneratorSeed, parameters.size, |
1219 static const RecordingDownloadObserver::RecordStruct expected_record[] = { | 1034 download->GetTargetFilePath())); |
1220 // Result of RST | 1035 |
1221 {DownloadItem::INTERRUPTED, GetSafeBufferChunk()}, | 1036 // Characterization risk: The next portion of the test examines the requests |
1222 // Starting continuation | 1037 // that were sent out while downloading our resource. These requests |
1223 {DownloadItem::IN_PROGRESS, GetSafeBufferChunk()}, | 1038 // correspond to the requests that were generated by the browser and the |
1224 // Server precondition fail. | 1039 // downloads system and may change as implementation details change. |
1225 {DownloadItem::INTERRUPTED, 0}, | 1040 TestDownloadRequestHandler::CompletedRequests requests; |
1226 // Notification of successful restart. | 1041 request_handler.GetCompletedRequestInfo(&requests); |
1227 {DownloadItem::IN_PROGRESS, 0}, | 1042 |
1228 // Completion. | 1043 ASSERT_EQ(2u, requests.size()); |
1229 {DownloadItem::COMPLETE, GetSafeBufferChunk() * 3}, | 1044 |
1230 }; | 1045 // The first request only sends data up to the interruption point. |
1231 | 1046 EXPECT_EQ(interruption.offset, requests[0].transferred_content_length); |
1232 recorder.CompareToExpectedRecord(expected_record, arraysize(expected_record)); | 1047 |
1048 // The second request sends the entire response. | |
1049 EXPECT_EQ(parameters.size, requests[1].transferred_content_length); | |
1050 | |
1051 std::string value; | |
1052 ASSERT_TRUE(requests[1].request_headers.GetHeader( | |
1053 net::HttpRequestHeaders::kIfRange, &value)); | |
1054 EXPECT_EQ(parameters.etag, value); | |
1055 | |
1056 ASSERT_TRUE(requests[1].request_headers.GetHeader( | |
1057 net::HttpRequestHeaders::kRange, &value)); | |
1058 EXPECT_EQ(base::StringPrintf("bytes=%" PRId64 "-", interruption.offset), | |
1059 value); | |
1233 } | 1060 } |
1234 | 1061 |
1235 // Confirm we don't try to resume if we don't have a verifier. | 1062 // Confirm we don't try to resume if we don't have a verifier. |
1236 IN_PROC_BROWSER_TEST_F(DownloadContentTest, | 1063 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Resume_RestartIfNoETag) { |
1237 ResumeInterruptedDownloadNoVerifiers) { | 1064 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
1238 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 1065 switches::kEnableDownloadResumption); |
1239 switches::kEnableDownloadResumption); | 1066 const int kOriginalPatternGeneratorSeed = 1; |
1240 ASSERT_TRUE(spawned_test_server()->Start()); | 1067 const int kNewPatternGeneratorSeed = 2; |
1241 | 1068 |
1242 GURL url = spawned_test_server()->GetURL(base::StringPrintf( | 1069 TestDownloadRequestHandler::Parameters parameters = |
1243 // First download hits an RST, rest don't, no verifiers. | 1070 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); |
1244 "rangereset?size=%d&rst_boundary=%d&" | 1071 ASSERT_EQ(1u, parameters.injected_errors.size()); |
1245 "token=NoRange&rst_limit=1&no_verifiers", | 1072 parameters.etag.clear(); |
1246 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | 1073 parameters.pattern_generator_seed = kOriginalPatternGeneratorSeed; |
1247 | 1074 |
1248 // Start the download and wait for first data chunk. | 1075 TestDownloadRequestHandler request_handler; |
1249 DownloadItem* download(StartDownloadAndReturnItem(url)); | 1076 request_handler.StartServing(parameters); |
1250 WaitForData(download, GetSafeBufferChunk()); | 1077 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); |
1251 | 1078 WaitForInterrupt(download); |
1252 RecordingDownloadObserver recorder(download); | 1079 |
1253 | 1080 parameters.pattern_generator_seed = kNewPatternGeneratorSeed; |
1254 ReleaseRSTAndConfirmInterruptForResume(download); | 1081 parameters.ClearInjectedErrors(); |
1255 ConfirmFileStatusForResume( | 1082 request_handler.StartServing(parameters); |
1256 download, false, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | 1083 |
1257 base::FilePath()); | 1084 download->Resume(); |
1258 | 1085 WaitForCompletion(download); |
1259 DownloadUpdatedObserver completion_observer( | 1086 |
1260 download, base::Bind(DownloadCompleteFilter)); | 1087 ASSERT_EQ(parameters.size, download->GetReceivedBytes()); |
1261 download->Resume(); | 1088 ASSERT_EQ(parameters.size, download->GetTotalBytes()); |
1262 completion_observer.WaitForEvent(); | 1089 ASSERT_NO_FATAL_FAILURE( |
1263 | 1090 ReadAndVerifyFileContents(kNewPatternGeneratorSeed, parameters.size, |
1264 ConfirmFileStatusForResume( | 1091 download->GetTargetFilePath())); |
1265 download, true, GetSafeBufferChunk() * 3, GetSafeBufferChunk() * 3, | 1092 |
1266 base::FilePath(FILE_PATH_LITERAL("rangereset"))); | 1093 TestDownloadRequestHandler::CompletedRequests requests; |
1267 | 1094 request_handler.GetCompletedRequestInfo(&requests); |
1268 static const RecordingDownloadObserver::RecordStruct expected_record[] = { | 1095 |
1269 // Result of RST | 1096 // Neither If-Range nor Range headers should be present in the second request. |
1270 {DownloadItem::INTERRUPTED, GetSafeBufferChunk()}, | 1097 ASSERT_EQ(2u, requests.size()); |
1271 // Restart for lack of verifiers | 1098 std::string value; |
1272 {DownloadItem::IN_PROGRESS, 0}, | 1099 EXPECT_FALSE(requests[1].request_headers.GetHeader( |
1273 // Completion. | 1100 net::HttpRequestHeaders::kIfRange, &value)); |
1274 {DownloadItem::COMPLETE, GetSafeBufferChunk() * 3}, | 1101 EXPECT_FALSE(requests[1].request_headers.GetHeader( |
1275 }; | 1102 net::HttpRequestHeaders::kRange, &value)); |
1276 | 1103 } |
1277 recorder.CompareToExpectedRecord(expected_record, arraysize(expected_record)); | 1104 |
1278 } | 1105 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Resume_RestartIfNoPartialFile) { |
1279 | 1106 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
1280 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeWithDeletedFile) { | 1107 switches::kEnableDownloadResumption); |
1281 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 1108 TestDownloadRequestHandler::Parameters parameters = |
1282 switches::kEnableDownloadResumption); | 1109 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); |
1283 ASSERT_TRUE(spawned_test_server()->Start()); | 1110 |
1284 | 1111 TestDownloadRequestHandler request_handler; |
1285 GURL url = spawned_test_server()->GetURL(base::StringPrintf( | 1112 request_handler.StartServing(parameters); |
1286 // First download hits an RST, rest don't | 1113 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); |
1287 "rangereset?size=%d&rst_boundary=%d&" | 1114 WaitForInterrupt(download); |
1288 "token=NoRange&rst_limit=1", | |
1289 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | |
1290 | |
1291 // Start the download and wait for first data chunk. | |
1292 DownloadItem* download(StartDownloadAndReturnItem(url)); | |
1293 WaitForData(download, GetSafeBufferChunk()); | |
1294 | |
1295 RecordingDownloadObserver recorder(download); | |
1296 | |
1297 ReleaseRSTAndConfirmInterruptForResume(download); | |
1298 ConfirmFileStatusForResume( | |
1299 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | |
1300 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | |
1301 | 1115 |
1302 // Delete the intermediate file. | 1116 // Delete the intermediate file. |
1303 base::DeleteFile(download->GetFullPath(), false); | 1117 ASSERT_TRUE(base::PathExists(download->GetFullPath())); |
1304 | 1118 ASSERT_TRUE(base::DeleteFile(download->GetFullPath(), false)); |
1305 DownloadUpdatedObserver completion_observer( | 1119 |
1306 download, base::Bind(DownloadCompleteFilter)); | 1120 parameters.ClearInjectedErrors(); |
1307 download->Resume(); | 1121 request_handler.StartServing(parameters); |
1308 completion_observer.WaitForEvent(); | 1122 |
1309 | 1123 download->Resume(); |
1310 ConfirmFileStatusForResume( | 1124 WaitForCompletion(download); |
1311 download, true, GetSafeBufferChunk() * 3, GetSafeBufferChunk() * 3, | 1125 |
1312 base::FilePath(FILE_PATH_LITERAL("rangereset"))); | 1126 ASSERT_EQ(parameters.size, download->GetReceivedBytes()); |
1313 | 1127 ASSERT_EQ(parameters.size, download->GetTotalBytes()); |
1314 static const RecordingDownloadObserver::RecordStruct expected_record[] = { | 1128 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents( |
1315 // Result of RST | 1129 parameters.pattern_generator_seed, parameters.size, |
1316 {DownloadItem::INTERRUPTED, GetSafeBufferChunk()}, | 1130 download->GetTargetFilePath())); |
1317 // Starting continuation | 1131 |
1318 {DownloadItem::IN_PROGRESS, GetSafeBufferChunk()}, | 1132 // The following verification is definitely a characterization test of |
1319 // Error because file isn't there. | 1133 // implementation details. We are going to see three requests being made. The |
1320 {DownloadItem::INTERRUPTED, 0}, | 1134 // one in the middle is a partial request that is abandoned because the |
1321 // Restart. | 1135 // intermediate file is missing. |
1322 {DownloadItem::IN_PROGRESS, 0}, | 1136 TestDownloadRequestHandler::CompletedRequests requests; |
1323 // Completion. | 1137 request_handler.GetCompletedRequestInfo(&requests); |
1324 {DownloadItem::COMPLETE, GetSafeBufferChunk() * 3}, | 1138 ASSERT_EQ(3u, requests.size()); |
1325 }; | 1139 EXPECT_EQ(parameters.size, requests[2].transferred_content_length); |
1326 | 1140 std::string dummy_value; |
1327 recorder.CompareToExpectedRecord(expected_record, arraysize(expected_record)); | 1141 EXPECT_FALSE(requests[2].request_headers.GetHeader( |
1328 } | 1142 net::HttpRequestHeaders::kRange, &dummy_value)); |
1329 | 1143 EXPECT_FALSE(requests[2].request_headers.GetHeader( |
1330 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeWithFileInitError) { | 1144 net::HttpRequestHeaders::kIfRange, &dummy_value)); |
1331 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 1145 } |
1332 switches::kEnableDownloadResumption); | 1146 |
1333 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("download-test.lib")); | 1147 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Resume_RecoverFromInitFileError) { |
1148 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
1149 switches::kEnableDownloadResumption); | |
1150 TestDownloadRequestHandler request_handler; | |
1151 request_handler.StartServing(TestDownloadRequestHandler::Parameters()); | |
1334 | 1152 |
1335 // Setup the error injector. | 1153 // Setup the error injector. |
1336 scoped_refptr<TestFileErrorInjector> injector( | 1154 scoped_refptr<TestFileErrorInjector> injector( |
1337 TestFileErrorInjector::Create(DownloadManagerForShell(shell()))); | 1155 TestFileErrorInjector::Create(DownloadManagerForShell(shell()))); |
1338 | 1156 |
1339 TestFileErrorInjector::FileErrorInfo err = { | 1157 TestFileErrorInjector::FileErrorInfo err = { |
1340 url.spec(), | 1158 request_handler.url().spec(), |
1341 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, | 1159 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, 0, |
1342 0, | 1160 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE}; |
1343 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE | |
1344 }; | |
1345 injector->AddError(err); | 1161 injector->AddError(err); |
1346 injector->InjectErrors(); | 1162 injector->InjectErrors(); |
1347 | 1163 |
1348 // Start and watch for interrupt. | 1164 // Start and watch for interrupt. |
1349 scoped_ptr<DownloadTestObserver> int_observer( | 1165 DownloadItem* download(StartDownloadAndReturnItem(request_handler.url())); |
1350 CreateInterruptedWaiter(shell(), 1)); | 1166 WaitForInterrupt(download); |
1351 DownloadItem* download(StartDownloadAndReturnItem(url)); | |
1352 int_observer->WaitForFinished(); | |
1353 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); | 1167 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); |
1354 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, | 1168 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, |
1355 download->GetLastReason()); | 1169 download->GetLastReason()); |
1356 EXPECT_EQ(0, download->GetReceivedBytes()); | 1170 EXPECT_EQ(0, download->GetReceivedBytes()); |
1357 EXPECT_TRUE(download->GetFullPath().empty()); | 1171 EXPECT_TRUE(download->GetFullPath().empty()); |
1358 EXPECT_TRUE(download->GetTargetFilePath().empty()); | 1172 EXPECT_TRUE(download->GetTargetFilePath().empty()); |
1359 | 1173 |
1360 // We need to make sure that any cross-thread downloads communication has | 1174 // We need to make sure that any cross-thread downloads communication has |
1361 // quiesced before clearing and injecting the new errors, as the | 1175 // quiesced before clearing and injecting the new errors, as the |
1362 // InjectErrors() routine alters the currently in use download file | 1176 // InjectErrors() routine alters the currently in use download file |
1363 // factory, which is a file thread object. | 1177 // factory, which is a file thread object. |
1364 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1178 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1365 RunAllPendingInMessageLoop(); | 1179 RunAllPendingInMessageLoop(); |
1366 | 1180 |
1367 // Clear the old errors list. | 1181 // Clear the old errors list. |
1368 injector->ClearErrors(); | 1182 injector->ClearErrors(); |
1369 injector->InjectErrors(); | 1183 injector->InjectErrors(); |
1370 | 1184 |
1371 // Resume and watch completion. | 1185 // Resume and watch completion. |
1372 DownloadUpdatedObserver completion_observer( | 1186 DownloadUpdatedObserver completion_observer( |
1373 download, base::Bind(DownloadCompleteFilter)); | 1187 download, base::Bind(DownloadCompleteFilter)); |
1374 download->Resume(); | 1188 download->Resume(); |
1375 completion_observer.WaitForEvent(); | 1189 completion_observer.WaitForEvent(); |
1376 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE); | 1190 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE); |
1377 } | 1191 } |
1378 | 1192 |
1379 IN_PROC_BROWSER_TEST_F(DownloadContentTest, | 1193 IN_PROC_BROWSER_TEST_F(DownloadContentTest, |
1380 ResumeWithFileIntermediateRenameError) { | 1194 Resume_RecoverFromIntermediateFileRenameError) { |
1381 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 1195 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
1382 switches::kEnableDownloadResumption); | 1196 switches::kEnableDownloadResumption); |
1383 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("download-test.lib")); | 1197 TestDownloadRequestHandler request_handler; |
1198 request_handler.StartServing(TestDownloadRequestHandler::Parameters()); | |
1384 | 1199 |
1385 // Setup the error injector. | 1200 // Setup the error injector. |
1386 scoped_refptr<TestFileErrorInjector> injector( | 1201 scoped_refptr<TestFileErrorInjector> injector( |
1387 TestFileErrorInjector::Create(DownloadManagerForShell(shell()))); | 1202 TestFileErrorInjector::Create(DownloadManagerForShell(shell()))); |
1388 | 1203 |
1389 TestFileErrorInjector::FileErrorInfo err = { | 1204 TestFileErrorInjector::FileErrorInfo err = { |
1390 url.spec(), | 1205 request_handler.url().spec(), |
1391 TestFileErrorInjector::FILE_OPERATION_RENAME_UNIQUIFY, | 1206 TestFileErrorInjector::FILE_OPERATION_RENAME_UNIQUIFY, 0, |
1392 0, | 1207 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE}; |
1393 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE | |
1394 }; | |
1395 injector->AddError(err); | 1208 injector->AddError(err); |
1396 injector->InjectErrors(); | 1209 injector->InjectErrors(); |
1397 | 1210 |
1398 // Start and watch for interrupt. | 1211 // Start and watch for interrupt. |
1399 scoped_ptr<DownloadTestObserver> int_observer( | 1212 DownloadItem* download(StartDownloadAndReturnItem(request_handler.url())); |
1400 CreateInterruptedWaiter(shell(), 1)); | 1213 WaitForInterrupt(download); |
1401 DownloadItem* download(StartDownloadAndReturnItem(url)); | |
1402 int_observer->WaitForFinished(); | |
1403 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); | 1214 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); |
1404 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, | 1215 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, |
1405 download->GetLastReason()); | 1216 download->GetLastReason()); |
1406 EXPECT_TRUE(download->GetFullPath().empty()); | 1217 EXPECT_TRUE(download->GetFullPath().empty()); |
1407 // Target path will have been set after file name determination. GetFullPath() | 1218 // Target path will have been set after file name determination. GetFullPath() |
1408 // being empty is sufficient to signal that filename determination needs to be | 1219 // being empty is sufficient to signal that filename determination needs to be |
1409 // redone. | 1220 // redone. |
1410 EXPECT_FALSE(download->GetTargetFilePath().empty()); | 1221 EXPECT_FALSE(download->GetTargetFilePath().empty()); |
1411 | 1222 |
1412 // We need to make sure that any cross-thread downloads communication has | 1223 // We need to make sure that any cross-thread downloads communication has |
1413 // quiesced before clearing and injecting the new errors, as the | 1224 // quiesced before clearing and injecting the new errors, as the |
1414 // InjectErrors() routine alters the currently in use download file | 1225 // InjectErrors() routine alters the currently in use download file |
1415 // factory, which is a file thread object. | 1226 // factory, which is a file thread object. |
1416 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1227 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1417 RunAllPendingInMessageLoop(); | 1228 RunAllPendingInMessageLoop(); |
1418 | 1229 |
1419 // Clear the old errors list. | 1230 // Clear the old errors list. |
1420 injector->ClearErrors(); | 1231 injector->ClearErrors(); |
1421 injector->InjectErrors(); | 1232 injector->InjectErrors(); |
1422 | 1233 |
1423 // Resume and watch completion. | 1234 // Resume and watch completion. |
1424 DownloadUpdatedObserver completion_observer( | 1235 DownloadUpdatedObserver completion_observer( |
1425 download, base::Bind(DownloadCompleteFilter)); | 1236 download, base::Bind(DownloadCompleteFilter)); |
1426 download->Resume(); | 1237 download->Resume(); |
1427 completion_observer.WaitForEvent(); | 1238 completion_observer.WaitForEvent(); |
1428 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE); | 1239 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE); |
1429 } | 1240 } |
1430 | 1241 |
1431 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeWithFileFinalRenameError) { | 1242 IN_PROC_BROWSER_TEST_F(DownloadContentTest, |
1243 Resume_RecoverFromFinalRenameError) { | |
1432 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 1244 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
1433 switches::kEnableDownloadResumption); | 1245 switches::kEnableDownloadResumption); |
1434 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("download-test.lib")); | 1246 TestDownloadRequestHandler request_handler; |
1247 request_handler.StartServing(TestDownloadRequestHandler::Parameters()); | |
1435 | 1248 |
1436 // Setup the error injector. | 1249 // Setup the error injector. |
1437 scoped_refptr<TestFileErrorInjector> injector( | 1250 scoped_refptr<TestFileErrorInjector> injector( |
1438 TestFileErrorInjector::Create(DownloadManagerForShell(shell()))); | 1251 TestFileErrorInjector::Create(DownloadManagerForShell(shell()))); |
1439 | 1252 |
1440 DownloadManagerForShell(shell())->RemoveAllDownloads(); | 1253 DownloadManagerForShell(shell())->RemoveAllDownloads(); |
1441 TestFileErrorInjector::FileErrorInfo err = { | 1254 TestFileErrorInjector::FileErrorInfo err = { |
1442 url.spec(), | 1255 request_handler.url().spec(), |
1443 TestFileErrorInjector::FILE_OPERATION_RENAME_ANNOTATE, | 1256 TestFileErrorInjector::FILE_OPERATION_RENAME_ANNOTATE, 0, |
1444 0, | 1257 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE}; |
1445 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE | |
1446 }; | |
1447 injector->AddError(err); | 1258 injector->AddError(err); |
1448 injector->InjectErrors(); | 1259 injector->InjectErrors(); |
1449 | 1260 |
1450 // Start and watch for interrupt. | 1261 // Start and watch for interrupt. |
1451 scoped_ptr<DownloadTestObserver> int_observer( | 1262 DownloadItem* download(StartDownloadAndReturnItem(request_handler.url())); |
1452 CreateInterruptedWaiter(shell(), 1)); | 1263 WaitForInterrupt(download); |
1453 DownloadItem* download(StartDownloadAndReturnItem(url)); | |
1454 int_observer->WaitForFinished(); | |
1455 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); | 1264 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); |
1456 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, | 1265 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, |
1457 download->GetLastReason()); | 1266 download->GetLastReason()); |
1458 EXPECT_TRUE(download->GetFullPath().empty()); | 1267 EXPECT_TRUE(download->GetFullPath().empty()); |
1459 // Target path should still be intact. | 1268 // Target path should still be intact. |
1460 EXPECT_FALSE(download->GetTargetFilePath().empty()); | 1269 EXPECT_FALSE(download->GetTargetFilePath().empty()); |
1461 | 1270 |
1462 // We need to make sure that any cross-thread downloads communication has | 1271 // We need to make sure that any cross-thread downloads communication has |
1463 // quiesced before clearing and injecting the new errors, as the | 1272 // quiesced before clearing and injecting the new errors, as the |
1464 // InjectErrors() routine alters the currently in use download file | 1273 // InjectErrors() routine alters the currently in use download file |
(...skipping 11 matching lines...) Expand all Loading... | |
1476 download->Resume(); | 1285 download->Resume(); |
1477 completion_observer.WaitForEvent(); | 1286 completion_observer.WaitForEvent(); |
1478 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE); | 1287 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE); |
1479 } | 1288 } |
1480 | 1289 |
1481 // An interrupted download should remove the intermediate file when it is | 1290 // An interrupted download should remove the intermediate file when it is |
1482 // cancelled. | 1291 // cancelled. |
1483 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelInterruptedDownload) { | 1292 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelInterruptedDownload) { |
1484 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 1293 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
1485 switches::kEnableDownloadResumption); | 1294 switches::kEnableDownloadResumption); |
1486 ASSERT_TRUE(spawned_test_server()->Start()); | 1295 TestDownloadRequestHandler::Parameters parameters = |
1296 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); | |
1297 TestDownloadRequestHandler request_handler; | |
1298 request_handler.StartServing(parameters); | |
1487 | 1299 |
1488 GURL url1 = spawned_test_server()->GetURL( | 1300 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); |
1489 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", | 1301 WaitForInterrupt(download); |
1490 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | |
1491 | 1302 |
1492 DownloadItem* download(StartDownloadAndReturnItem(url1)); | 1303 base::FilePath intermediate_path = download->GetFullPath(); |
1493 WaitForData(download, GetSafeBufferChunk()); | |
1494 | |
1495 ReleaseRSTAndConfirmInterruptForResume(download); | |
1496 ConfirmFileStatusForResume( | |
1497 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | |
1498 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | |
1499 | |
1500 base::FilePath intermediate_path(download->GetFullPath()); | |
1501 ASSERT_FALSE(intermediate_path.empty()); | 1304 ASSERT_FALSE(intermediate_path.empty()); |
1502 EXPECT_TRUE(base::PathExists(intermediate_path)); | 1305 ASSERT_TRUE(base::PathExists(intermediate_path)); |
1503 | 1306 |
1504 download->Cancel(true /* user_cancel */); | 1307 download->Cancel(true /* user_cancel */); |
1505 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1308 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1506 RunAllPendingInMessageLoop(); | 1309 RunAllPendingInMessageLoop(); |
1507 | 1310 |
1508 // The intermediate file should now be gone. | 1311 // The intermediate file should now be gone. |
1509 EXPECT_FALSE(base::PathExists(intermediate_path)); | 1312 EXPECT_FALSE(base::PathExists(intermediate_path)); |
1510 EXPECT_TRUE(download->GetFullPath().empty()); | 1313 EXPECT_TRUE(download->GetFullPath().empty()); |
1511 } | 1314 } |
1512 | 1315 |
1513 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveDownload) { | 1316 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveInterruptedDownload) { |
1514 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 1317 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
1515 switches::kEnableDownloadResumption); | 1318 switches::kEnableDownloadResumption); |
1516 ASSERT_TRUE(spawned_test_server()->Start()); | 1319 TestDownloadRequestHandler::Parameters parameters = |
1320 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); | |
1321 TestDownloadRequestHandler request_handler; | |
1322 request_handler.StartServing(parameters); | |
1517 | 1323 |
1518 // An interrupted download should remove the intermediate file when it is | 1324 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); |
1519 // removed. | 1325 WaitForInterrupt(download); |
1520 { | |
1521 GURL url1 = spawned_test_server()->GetURL( | |
1522 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", | |
1523 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | |
1524 | 1326 |
1525 DownloadItem* download(StartDownloadAndReturnItem(url1)); | 1327 base::FilePath intermediate_path = download->GetFullPath(); |
1526 WaitForData(download, GetSafeBufferChunk()); | 1328 ASSERT_FALSE(intermediate_path.empty()); |
1527 ReleaseRSTAndConfirmInterruptForResume(download); | 1329 ASSERT_TRUE(base::PathExists(intermediate_path)); |
1528 ConfirmFileStatusForResume( | |
1529 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | |
1530 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | |
1531 | 1330 |
1532 base::FilePath intermediate_path(download->GetFullPath()); | 1331 download->Remove(); |
1533 ASSERT_FALSE(intermediate_path.empty()); | 1332 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1534 EXPECT_TRUE(base::PathExists(intermediate_path)); | 1333 RunAllPendingInMessageLoop(); |
1535 | 1334 |
1536 download->Remove(); | 1335 // The intermediate file should now be gone. |
1537 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1336 EXPECT_FALSE(base::PathExists(intermediate_path)); |
1538 RunAllPendingInMessageLoop(); | 1337 } |
1539 | 1338 |
1540 // The intermediate file should now be gone. | 1339 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveCompletedDownload) { |
1541 EXPECT_FALSE(base::PathExists(intermediate_path)); | |
1542 } | |
1543 | |
1544 // A completed download shouldn't delete the downloaded file when it is | 1340 // A completed download shouldn't delete the downloaded file when it is |
1545 // removed. | 1341 // removed. |
1546 { | 1342 TestDownloadRequestHandler request_handler; |
1547 // Start the second download and wait until it's done. | 1343 request_handler.StartServing(TestDownloadRequestHandler::Parameters()); |
1548 GURL url2(net::URLRequestMockHTTPJob::GetMockUrl("download-test.lib")); | 1344 scoped_ptr<DownloadTestObserver> completion_observer( |
1549 scoped_ptr<DownloadTestObserver> completion_observer( | 1345 CreateWaiter(shell(), 1)); |
1550 CreateWaiter(shell(), 1)); | 1346 DownloadItem* download(StartDownloadAndReturnItem(request_handler.url())); |
1551 DownloadItem* download(StartDownloadAndReturnItem(url2)); | 1347 completion_observer->WaitForFinished(); |
1552 completion_observer->WaitForFinished(); | |
1553 | 1348 |
1554 // The target path should exist. | 1349 // The target path should exist. |
1555 base::FilePath target_path(download->GetTargetFilePath()); | 1350 base::FilePath target_path(download->GetTargetFilePath()); |
1556 EXPECT_TRUE(base::PathExists(target_path)); | 1351 EXPECT_TRUE(base::PathExists(target_path)); |
1557 download->Remove(); | 1352 download->Remove(); |
1558 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1353 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1559 RunAllPendingInMessageLoop(); | 1354 RunAllPendingInMessageLoop(); |
1560 | 1355 |
1561 // The file should still exist. | 1356 // The file should still exist. |
1562 EXPECT_TRUE(base::PathExists(target_path)); | 1357 EXPECT_TRUE(base::PathExists(target_path)); |
1563 } | |
1564 } | 1358 } |
1565 | 1359 |
1566 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumingDownload) { | 1360 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumingDownload) { |
1567 SetupEnsureNoPendingDownloads(); | |
1568 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 1361 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
1569 switches::kEnableDownloadResumption); | 1362 switches::kEnableDownloadResumption); |
1570 ASSERT_TRUE(spawned_test_server()->Start()); | 1363 TestDownloadRequestHandler::Parameters parameters = |
1364 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); | |
1365 TestDownloadRequestHandler request_handler; | |
1366 request_handler.StartServing(parameters); | |
1571 | 1367 |
1572 GURL url = spawned_test_server()->GetURL( | 1368 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); |
1573 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", | 1369 WaitForInterrupt(download); |
1574 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | |
1575 | |
1576 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); | |
1577 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | |
1578 | |
1579 DownloadItem* download(StartDownloadAndReturnItem(url)); | |
1580 WaitForData(download, GetSafeBufferChunk()); | |
1581 ::testing::Mock::VerifyAndClearExpectations(&dm_observer); | |
1582 | |
1583 // Tell the server to send the RST and confirm the interrupt happens. | |
1584 ReleaseRSTAndConfirmInterruptForResume(download); | |
1585 ConfirmFileStatusForResume( | |
1586 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | |
1587 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | |
1588 | 1370 |
1589 base::FilePath intermediate_path(download->GetFullPath()); | 1371 base::FilePath intermediate_path(download->GetFullPath()); |
1590 ASSERT_FALSE(intermediate_path.empty()); | 1372 ASSERT_FALSE(intermediate_path.empty()); |
1591 EXPECT_TRUE(base::PathExists(intermediate_path)); | 1373 EXPECT_TRUE(base::PathExists(intermediate_path)); |
1592 | 1374 |
1593 // Resume and remove download. We expect only a single OnDownloadCreated() | 1375 // Resume and remove download. We expect only a single OnDownloadCreated() |
1594 // call, and that's for the second download created below. | 1376 // call, and that's for the second download created below. |
1377 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); | |
1595 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | 1378 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); |
1596 download->Resume(); | 1379 download->Resume(); |
1380 EXPECT_EQ(DownloadItem::INTERRUPTED, download->GetState()); | |
1597 download->Remove(); | 1381 download->Remove(); |
1598 | 1382 |
1599 // The intermediate file should now be gone. | 1383 // The intermediate file should now be gone. |
1600 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1384 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1601 RunAllPendingInMessageLoop(); | 1385 RunAllPendingInMessageLoop(); |
1602 EXPECT_FALSE(base::PathExists(intermediate_path)); | 1386 EXPECT_FALSE(base::PathExists(intermediate_path)); |
1603 | 1387 |
1604 // Start the second download and wait until it's done. The test server is | 1388 parameters.ClearInjectedErrors(); |
1605 // single threaded. The response to this download request should follow the | 1389 request_handler.StartServing(parameters); |
1606 // response to the previous resumption request. | |
1607 GURL url2( | |
1608 spawned_test_server()->GetURL("rangereset?size=100&rst_limit=0&token=x")); | |
1609 NavigateToURLAndWaitForDownload(shell(), url2, DownloadItem::COMPLETE); | |
1610 | 1390 |
1391 // Start the second download and wait until it's done. This exercises the | |
1392 // entire downloads stack and effectively flushes all of our worker threads. | |
1393 // We are testing whether the URL request created in the previous | |
1394 // DownloadItem::Resume() call reulted in a new download or not. | |
1395 NavigateToURLAndWaitForDownload(shell(), request_handler.url(), | |
1396 DownloadItem::COMPLETE); | |
1611 EXPECT_TRUE(EnsureNoPendingDownloads()); | 1397 EXPECT_TRUE(EnsureNoPendingDownloads()); |
1612 } | 1398 } |
1613 | 1399 |
1614 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelResumingDownload) { | 1400 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelResumingDownload) { |
1615 SetupEnsureNoPendingDownloads(); | |
1616 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 1401 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
1617 switches::kEnableDownloadResumption); | 1402 switches::kEnableDownloadResumption); |
1618 ASSERT_TRUE(spawned_test_server()->Start()); | 1403 TestDownloadRequestHandler::Parameters parameters = |
1404 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); | |
1405 TestDownloadRequestHandler request_handler; | |
1406 request_handler.StartServing(parameters); | |
1619 | 1407 |
1620 GURL url = spawned_test_server()->GetURL( | 1408 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); |
1621 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", | 1409 WaitForInterrupt(download); |
1622 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | |
1623 | |
1624 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); | |
1625 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | |
1626 | |
1627 DownloadItem* download(StartDownloadAndReturnItem(url)); | |
1628 WaitForData(download, GetSafeBufferChunk()); | |
1629 ::testing::Mock::VerifyAndClearExpectations(&dm_observer); | |
1630 | |
1631 // Tell the server to send the RST and confirm the interrupt happens. | |
1632 ReleaseRSTAndConfirmInterruptForResume(download); | |
1633 ConfirmFileStatusForResume( | |
1634 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | |
1635 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | |
1636 | 1410 |
1637 base::FilePath intermediate_path(download->GetFullPath()); | 1411 base::FilePath intermediate_path(download->GetFullPath()); |
1638 ASSERT_FALSE(intermediate_path.empty()); | 1412 ASSERT_FALSE(intermediate_path.empty()); |
1639 EXPECT_TRUE(base::PathExists(intermediate_path)); | 1413 EXPECT_TRUE(base::PathExists(intermediate_path)); |
1640 | 1414 |
1641 // Resume and cancel download. We expect only a single OnDownloadCreated() | 1415 // Resume and remove download. We expect only a single OnDownloadCreated() |
1642 // call, and that's for the second download created below. | 1416 // call, and that's for the second download created below. |
1417 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); | |
1643 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | 1418 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); |
1644 download->Resume(); | 1419 download->Resume(); |
1645 download->Cancel(true); | 1420 EXPECT_EQ(DownloadItem::INTERRUPTED, download->GetState()); |
1421 download->Cancel(true /* user_cancel */); | |
1646 | 1422 |
1647 // The intermediate file should now be gone. | 1423 // The intermediate file should now be gone. |
1648 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1424 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1649 RunAllPendingInMessageLoop(); | 1425 RunAllPendingInMessageLoop(); |
1650 EXPECT_FALSE(base::PathExists(intermediate_path)); | 1426 EXPECT_FALSE(base::PathExists(intermediate_path)); |
1651 EXPECT_TRUE(download->GetFullPath().empty()); | |
1652 | 1427 |
1653 // Start the second download and wait until it's done. The test server is | 1428 parameters.ClearInjectedErrors(); |
1654 // single threaded. The response to this download request should follow the | 1429 request_handler.StartServing(parameters); |
1655 // response to the previous resumption request. | |
1656 GURL url2( | |
1657 spawned_test_server()->GetURL("rangereset?size=100&rst_limit=0&token=x")); | |
1658 NavigateToURLAndWaitForDownload(shell(), url2, DownloadItem::COMPLETE); | |
1659 | 1430 |
1431 // Start the second download and wait until it's done. This exercises the | |
1432 // entire downloads stack and effectively flushes all of our worker threads. | |
1433 // We are testing whether the URL request created in the previous | |
1434 // DownloadItem::Resume() call reulted in a new download or not. | |
1435 NavigateToURLAndWaitForDownload(shell(), request_handler.url(), | |
1436 DownloadItem::COMPLETE); | |
1660 EXPECT_TRUE(EnsureNoPendingDownloads()); | 1437 EXPECT_TRUE(EnsureNoPendingDownloads()); |
1661 } | 1438 } |
1662 | 1439 |
1663 // Check that the cookie policy is correctly updated when downloading a file | 1440 // Check that the cookie policy is correctly updated when downloading a file |
1664 // that redirects cross origin. | 1441 // that redirects cross origin. |
1665 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CookiePolicy) { | 1442 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CookiePolicy) { |
1666 ASSERT_TRUE(spawned_test_server()->Start()); | 1443 net::EmbeddedTestServer origin_one; |
1667 net::HostPortPair host_port = spawned_test_server()->host_port_pair(); | 1444 net::EmbeddedTestServer origin_two; |
1668 DCHECK_EQ(host_port.host(), std::string("127.0.0.1")); | 1445 ASSERT_TRUE(origin_one.InitializeAndWaitUntilReady()); |
svaldez
2015/11/11 20:42:41
Use .Start()
asanka
2015/11/11 20:57:25
Done.
| |
1446 ASSERT_TRUE(origin_two.InitializeAndWaitUntilReady()); | |
1669 | 1447 |
1670 // Block third-party cookies. | 1448 // Block third-party cookies. |
1671 ShellNetworkDelegate::SetAcceptAllCookies(false); | 1449 ShellNetworkDelegate::SetAcceptAllCookies(false); |
1672 | 1450 |
1673 // |url| redirects to a different origin |download| which tries to set a | 1451 // |url| redirects to a different origin |download| which tries to set a |
1674 // cookie. | 1452 // cookie. |
1675 std::string download(base::StringPrintf( | 1453 base::StringPairs cookie_header; |
1676 "http://localhost:%d/set-cookie?A=B", host_port.port())); | 1454 cookie_header.push_back( |
1677 GURL url(spawned_test_server()->GetURL("server-redirect?" + download)); | 1455 std::make_pair(std::string("Set-Cookie"), std::string("A=B"))); |
1456 origin_one.RegisterRequestHandler(CreateBasicResponseHandler( | |
1457 "/foo", cookie_header, "application/octet-stream", "abcd")); | |
1458 origin_two.RegisterRequestHandler( | |
1459 CreateRedirectHandler("/bar", origin_one.GetURL("/foo"))); | |
1678 | 1460 |
1679 // Download the file. | 1461 // Download the file. |
1680 SetupEnsureNoPendingDownloads(); | 1462 SetupEnsureNoPendingDownloads(); |
1681 scoped_ptr<DownloadUrlParameters> dl_params( | 1463 scoped_ptr<DownloadUrlParameters> download_parameters( |
1682 DownloadUrlParameters::FromWebContents(shell()->web_contents(), url)); | 1464 DownloadUrlParameters::FromWebContents(shell()->web_contents(), |
1465 origin_two.GetURL("/bar"))); | |
1683 scoped_ptr<DownloadTestObserver> observer(CreateWaiter(shell(), 1)); | 1466 scoped_ptr<DownloadTestObserver> observer(CreateWaiter(shell(), 1)); |
1684 DownloadManagerForShell(shell())->DownloadUrl(dl_params.Pass()); | 1467 DownloadManagerForShell(shell())->DownloadUrl(download_parameters.Pass()); |
1685 observer->WaitForFinished(); | 1468 observer->WaitForFinished(); |
1686 | 1469 |
1687 // Get the important info from other threads and check it. | 1470 // Get the important info from other threads and check it. |
1688 EXPECT_TRUE(EnsureNoPendingDownloads()); | 1471 EXPECT_TRUE(EnsureNoPendingDownloads()); |
1689 | 1472 |
1690 std::vector<DownloadItem*> downloads; | 1473 std::vector<DownloadItem*> downloads; |
1691 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | 1474 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); |
1692 ASSERT_EQ(1u, downloads.size()); | 1475 ASSERT_EQ(1u, downloads.size()); |
1693 ASSERT_EQ(DownloadItem::COMPLETE, downloads[0]->GetState()); | 1476 ASSERT_EQ(DownloadItem::COMPLETE, downloads[0]->GetState()); |
1694 | 1477 |
1695 // Check that the cookies were correctly set. | 1478 // Check that the cookies were correctly set. |
1696 EXPECT_EQ("A=B", | 1479 EXPECT_EQ("A=B", |
1697 content::GetCookies(shell()->web_contents()->GetBrowserContext(), | 1480 content::GetCookies(shell()->web_contents()->GetBrowserContext(), |
1698 GURL(download))); | 1481 origin_one.GetURL("/"))); |
1699 } | 1482 } |
1700 | 1483 |
1701 // A filename suggestion specified via a @download attribute should not be | 1484 // A filename suggestion specified via a @download attribute should not be |
1702 // effective if the final download URL is in another origin from the original | 1485 // effective if the final download URL is in another origin from the original |
1703 // download URL. | 1486 // download URL. |
1704 IN_PROC_BROWSER_TEST_F(DownloadContentTest, | 1487 IN_PROC_BROWSER_TEST_F(DownloadContentTest, |
1705 DownloadAttributeCrossOriginRedirect) { | 1488 DownloadAttributeCrossOriginRedirect) { |
1706 net::EmbeddedTestServer origin_one; | 1489 net::EmbeddedTestServer origin_one; |
1707 net::EmbeddedTestServer origin_two; | 1490 net::EmbeddedTestServer origin_two; |
1708 ASSERT_TRUE(origin_one.Start()); | 1491 ASSERT_TRUE(origin_one.Start()); |
(...skipping 11 matching lines...) Expand all Loading... | |
1720 GURL download_url = origin_one.GetURL("/ping"); | 1503 GURL download_url = origin_one.GetURL("/ping"); |
1721 GURL referrer_url = origin_one.GetURL( | 1504 GURL referrer_url = origin_one.GetURL( |
1722 std::string("/download-attribute.html?target=") + download_url.spec()); | 1505 std::string("/download-attribute.html?target=") + download_url.spec()); |
1723 | 1506 |
1724 // <origin_one>/download-attribute.html initiates a download of | 1507 // <origin_one>/download-attribute.html initiates a download of |
1725 // <origin_one>/ping, which redirects to <origin_two>/download. | 1508 // <origin_one>/ping, which redirects to <origin_two>/download. |
1726 origin_one.ServeFilesFromDirectory(GetTestFilePath("download", "")); | 1509 origin_one.ServeFilesFromDirectory(GetTestFilePath("download", "")); |
1727 origin_one.RegisterRequestHandler( | 1510 origin_one.RegisterRequestHandler( |
1728 CreateRedirectHandler("/ping", origin_two.GetURL("/download"))); | 1511 CreateRedirectHandler("/ping", origin_two.GetURL("/download"))); |
1729 origin_two.RegisterRequestHandler(CreateBasicResponseHandler( | 1512 origin_two.RegisterRequestHandler(CreateBasicResponseHandler( |
1730 "/download", "application/octet-stream", "Hello")); | 1513 "/download", base::StringPairs(), "application/octet-stream", "Hello")); |
1731 | 1514 |
1732 NavigateToURLAndWaitForDownload( | 1515 NavigateToURLAndWaitForDownload( |
1733 shell(), referrer_url, DownloadItem::COMPLETE); | 1516 shell(), referrer_url, DownloadItem::COMPLETE); |
1734 | 1517 |
1735 std::vector<DownloadItem*> downloads; | 1518 std::vector<DownloadItem*> downloads; |
1736 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | 1519 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); |
1737 ASSERT_EQ(1u, downloads.size()); | 1520 ASSERT_EQ(1u, downloads.size()); |
1738 | 1521 |
1739 EXPECT_EQ(FILE_PATH_LITERAL("download"), | 1522 EXPECT_EQ(FILE_PATH_LITERAL("download"), |
1740 downloads[0]->GetTargetFilePath().BaseName().value()); | 1523 downloads[0]->GetTargetFilePath().BaseName().value()); |
(...skipping 27 matching lines...) Expand all Loading... | |
1768 origin_one.ServeFilesFromDirectory(GetTestFilePath("download", "")); | 1551 origin_one.ServeFilesFromDirectory(GetTestFilePath("download", "")); |
1769 | 1552 |
1770 // <origin_one>/download-attribute.html initiates a download of | 1553 // <origin_one>/download-attribute.html initiates a download of |
1771 // <origin_one>/ping, which redirects to <origin_two>/pong, and then finally | 1554 // <origin_one>/ping, which redirects to <origin_two>/pong, and then finally |
1772 // to <origin_one>/download. | 1555 // to <origin_one>/download. |
1773 origin_one.RegisterRequestHandler( | 1556 origin_one.RegisterRequestHandler( |
1774 CreateRedirectHandler("/ping", origin_two.GetURL("/pong"))); | 1557 CreateRedirectHandler("/ping", origin_two.GetURL("/pong"))); |
1775 origin_two.RegisterRequestHandler( | 1558 origin_two.RegisterRequestHandler( |
1776 CreateRedirectHandler("/pong", origin_one.GetURL("/download"))); | 1559 CreateRedirectHandler("/pong", origin_one.GetURL("/download"))); |
1777 origin_one.RegisterRequestHandler(CreateBasicResponseHandler( | 1560 origin_one.RegisterRequestHandler(CreateBasicResponseHandler( |
1778 "/download", "application/octet-stream", "Hello")); | 1561 "/download", base::StringPairs(), "application/octet-stream", "Hello")); |
1779 | 1562 |
1780 NavigateToURLAndWaitForDownload( | 1563 NavigateToURLAndWaitForDownload( |
1781 shell(), referrer_url, DownloadItem::COMPLETE); | 1564 shell(), referrer_url, DownloadItem::COMPLETE); |
1782 | 1565 |
1783 std::vector<DownloadItem*> downloads; | 1566 std::vector<DownloadItem*> downloads; |
1784 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | 1567 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); |
1785 ASSERT_EQ(1u, downloads.size()); | 1568 ASSERT_EQ(1u, downloads.size()); |
1786 | 1569 |
1787 EXPECT_EQ(FILE_PATH_LITERAL("suggested-filename"), | 1570 EXPECT_EQ(FILE_PATH_LITERAL("suggested-filename"), |
1788 downloads[0]->GetTargetFilePath().BaseName().value()); | 1571 downloads[0]->GetTargetFilePath().BaseName().value()); |
1789 ASSERT_TRUE(origin_one.ShutdownAndWaitUntilComplete()); | 1572 ASSERT_TRUE(origin_one.ShutdownAndWaitUntilComplete()); |
1790 ASSERT_TRUE(origin_two.ShutdownAndWaitUntilComplete()); | 1573 ASSERT_TRUE(origin_two.ShutdownAndWaitUntilComplete()); |
1791 } | 1574 } |
1792 | 1575 |
1793 // The file empty.bin is served with a MIME type of application/octet-stream. | 1576 // The file empty.bin is served with a MIME type of application/octet-stream. |
1794 // The content body is empty. Make sure this case is handled properly and we | 1577 // The content body is empty. Make sure this case is handled properly and we |
1795 // don't regress on http://crbug.com/320394. | 1578 // don't regress on http://crbug.com/320394. |
1796 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadGZipWithNoContent) { | 1579 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadGZipWithNoContent) { |
1797 net::EmbeddedTestServer test_server; | 1580 GURL url = net::URLRequestMockHTTPJob::GetMockUrl("empty.bin"); |
1798 ASSERT_TRUE(test_server.Start()); | |
1799 | |
1800 GURL url = test_server.GetURL("/empty.bin"); | |
1801 test_server.ServeFilesFromDirectory(GetTestFilePath("download", "")); | |
1802 | |
1803 NavigateToURLAndWaitForDownload(shell(), url, DownloadItem::COMPLETE); | 1581 NavigateToURLAndWaitForDownload(shell(), url, DownloadItem::COMPLETE); |
1804 // That's it. This should work without crashing. | 1582 // That's it. This should work without crashing. |
1805 } | 1583 } |
1806 | 1584 |
1807 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Spam) { | 1585 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Spam) { |
1808 ASSERT_TRUE(embedded_test_server()->Start()); | 1586 ASSERT_TRUE(embedded_test_server()->Start()); |
1809 | 1587 |
1810 NavigateToURLAndWaitForDownload( | 1588 NavigateToURLAndWaitForDownload( |
1811 shell(), | 1589 shell(), |
1812 embedded_test_server()->GetURL( | 1590 embedded_test_server()->GetURL( |
1813 "/download/double-content-disposition.txt"), | 1591 "/download/double-content-disposition.txt"), |
1814 DownloadItem::COMPLETE); | 1592 DownloadItem::COMPLETE); |
1815 | 1593 |
1816 std::vector<DownloadItem*> downloads; | 1594 std::vector<DownloadItem*> downloads; |
1817 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | 1595 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); |
1818 ASSERT_EQ(1u, downloads.size()); | 1596 ASSERT_EQ(1u, downloads.size()); |
1819 | 1597 |
1820 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"), | 1598 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"), |
1821 downloads[0]->GetTargetFilePath().BaseName().value()); | 1599 downloads[0]->GetTargetFilePath().BaseName().value()); |
1822 } | 1600 } |
1823 | 1601 |
1824 } // namespace content | 1602 } // namespace content |
OLD | NEW |