| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
| 14 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 17 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
| 18 #include "base/format_macros.h" | 18 #include "base/format_macros.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 21 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
| 22 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
| 24 #include "base/threading/platform_thread.h" | 24 #include "base/threading/platform_thread.h" |
| 25 #include "base/threading/thread_restrictions.h" |
| 25 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 26 #include "build/build_config.h" | 27 #include "build/build_config.h" |
| 27 #include "content/browser/byte_stream.h" | 28 #include "content/browser/byte_stream.h" |
| 28 #include "content/browser/download/download_file_factory.h" | 29 #include "content/browser/download/download_file_factory.h" |
| 29 #include "content/browser/download/download_file_impl.h" | 30 #include "content/browser/download/download_file_impl.h" |
| 30 #include "content/browser/download/download_item_impl.h" | 31 #include "content/browser/download/download_item_impl.h" |
| 31 #include "content/browser/download/download_manager_impl.h" | 32 #include "content/browser/download/download_manager_impl.h" |
| 32 #include "content/browser/download/download_resource_handler.h" | 33 #include "content/browser/download/download_resource_handler.h" |
| 33 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 34 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 34 #include "content/browser/web_contents/web_contents_impl.h" | 35 #include "content/browser/web_contents/web_contents_impl.h" |
| (...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(expected_terminal_state)); | 648 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(expected_terminal_state)); |
| 648 } | 649 } |
| 649 | 650 |
| 650 // Checks that |path| is has |file_size| bytes, and matches the |value| | 651 // Checks that |path| is has |file_size| bytes, and matches the |value| |
| 651 // string. | 652 // string. |
| 652 bool VerifyFile(const base::FilePath& path, | 653 bool VerifyFile(const base::FilePath& path, |
| 653 const std::string& value, | 654 const std::string& value, |
| 654 const int64_t file_size) { | 655 const int64_t file_size) { |
| 655 std::string file_contents; | 656 std::string file_contents; |
| 656 | 657 |
| 657 bool read = base::ReadFileToString(path, &file_contents); | 658 { |
| 658 EXPECT_TRUE(read) << "Failed reading file: " << path.value() << std::endl; | 659 base::ThreadRestrictions::ScopedAllowIO allow_io_during_test_verification; |
| 659 if (!read) | 660 bool read = base::ReadFileToString(path, &file_contents); |
| 660 return false; // Couldn't read the file. | 661 EXPECT_TRUE(read) << "Failed reading file: " << path.value() << std::endl; |
| 662 if (!read) |
| 663 return false; // Couldn't read the file. |
| 664 } |
| 661 | 665 |
| 662 // Note: we don't handle really large files (more than size_t can hold) | 666 // Note: we don't handle really large files (more than size_t can hold) |
| 663 // so we will fail in that case. | 667 // so we will fail in that case. |
| 664 size_t expected_size = static_cast<size_t>(file_size); | 668 size_t expected_size = static_cast<size_t>(file_size); |
| 665 | 669 |
| 666 // Check the size. | 670 // Check the size. |
| 667 EXPECT_EQ(expected_size, file_contents.size()); | 671 EXPECT_EQ(expected_size, file_contents.size()); |
| 668 if (expected_size != file_contents.size()) | 672 if (expected_size != file_contents.size()) |
| 669 return false; | 673 return false; |
| 670 | 674 |
| 671 // Check the contents. | 675 // Check the contents. |
| 672 EXPECT_EQ(value, file_contents); | 676 EXPECT_EQ(value, file_contents); |
| 673 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) | 677 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) |
| 674 return false; | 678 return false; |
| 675 | 679 |
| 676 return true; | 680 return true; |
| 677 } | 681 } |
| 678 | 682 |
| 679 // Start a download and return the item. | 683 // Start a download and return the item. |
| 680 DownloadItem* StartDownloadAndReturnItem(Shell* shell, GURL url) { | 684 DownloadItem* StartDownloadAndReturnItem(Shell* shell, GURL url) { |
| 681 std::unique_ptr<DownloadCreateObserver> observer( | 685 std::unique_ptr<DownloadCreateObserver> observer( |
| 682 new DownloadCreateObserver(DownloadManagerForShell(shell))); | 686 new DownloadCreateObserver(DownloadManagerForShell(shell))); |
| 683 shell->LoadURL(url); | 687 shell->LoadURL(url); |
| 684 return observer->WaitForFinished(); | 688 return observer->WaitForFinished(); |
| 685 } | 689 } |
| 686 | 690 |
| 691 static bool PathExists(const base::FilePath& path) { |
| 692 base::ThreadRestrictions::ScopedAllowIO allow_io_during_test_verification; |
| 693 return base::PathExists(path); |
| 694 } |
| 695 |
| 687 static void ReadAndVerifyFileContents(int seed, | 696 static void ReadAndVerifyFileContents(int seed, |
| 688 int64_t expected_size, | 697 int64_t expected_size, |
| 689 const base::FilePath& path) { | 698 const base::FilePath& path) { |
| 699 base::ThreadRestrictions::ScopedAllowIO allow_io_during_test_verification; |
| 690 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 700 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 691 ASSERT_TRUE(file.IsValid()); | 701 ASSERT_TRUE(file.IsValid()); |
| 692 int64_t file_length = file.GetLength(); | 702 int64_t file_length = file.GetLength(); |
| 693 ASSERT_EQ(expected_size, file_length); | 703 ASSERT_EQ(expected_size, file_length); |
| 694 | 704 |
| 695 const int64_t kBufferSize = 64 * 1024; | 705 const int64_t kBufferSize = 64 * 1024; |
| 696 std::vector<char> pattern; | 706 std::vector<char> pattern; |
| 697 std::vector<char> data; | 707 std::vector<char> data; |
| 698 pattern.resize(kBufferSize); | 708 pattern.resize(kBufferSize); |
| 699 data.resize(kBufferSize); | 709 data.resize(kBufferSize); |
| (...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1378 TestDownloadRequestHandler::Parameters parameters = | 1388 TestDownloadRequestHandler::Parameters parameters = |
| 1379 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); | 1389 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); |
| 1380 | 1390 |
| 1381 TestDownloadRequestHandler request_handler; | 1391 TestDownloadRequestHandler request_handler; |
| 1382 request_handler.StartServing(parameters); | 1392 request_handler.StartServing(parameters); |
| 1383 DownloadItem* download = | 1393 DownloadItem* download = |
| 1384 StartDownloadAndReturnItem(shell(), request_handler.url()); | 1394 StartDownloadAndReturnItem(shell(), request_handler.url()); |
| 1385 WaitForInterrupt(download); | 1395 WaitForInterrupt(download); |
| 1386 | 1396 |
| 1387 // Delete the intermediate file. | 1397 // Delete the intermediate file. |
| 1388 ASSERT_TRUE(base::PathExists(download->GetFullPath())); | 1398 { |
| 1389 ASSERT_TRUE(base::DeleteFile(download->GetFullPath(), false)); | 1399 base::ThreadRestrictions::ScopedAllowIO allow_io_for_testing; |
| 1400 ASSERT_TRUE(PathExists(download->GetFullPath())); |
| 1401 ASSERT_TRUE(base::DeleteFile(download->GetFullPath(), false)); |
| 1402 } |
| 1390 | 1403 |
| 1391 parameters.ClearInjectedErrors(); | 1404 parameters.ClearInjectedErrors(); |
| 1392 request_handler.StartServing(parameters); | 1405 request_handler.StartServing(parameters); |
| 1393 | 1406 |
| 1394 download->Resume(); | 1407 download->Resume(); |
| 1395 WaitForCompletion(download); | 1408 WaitForCompletion(download); |
| 1396 | 1409 |
| 1397 ASSERT_EQ(parameters.size, download->GetReceivedBytes()); | 1410 ASSERT_EQ(parameters.size, download->GetReceivedBytes()); |
| 1398 ASSERT_EQ(parameters.size, download->GetTotalBytes()); | 1411 ASSERT_EQ(parameters.size, download->GetTotalBytes()); |
| 1399 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents( | 1412 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents( |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1580 TestDownloadRequestHandler request_handler; | 1593 TestDownloadRequestHandler request_handler; |
| 1581 request_handler.StartServing( | 1594 request_handler.StartServing( |
| 1582 TestDownloadRequestHandler::Parameters::WithSingleInterruption()); | 1595 TestDownloadRequestHandler::Parameters::WithSingleInterruption()); |
| 1583 | 1596 |
| 1584 DownloadItem* download = | 1597 DownloadItem* download = |
| 1585 StartDownloadAndReturnItem(shell(), request_handler.url()); | 1598 StartDownloadAndReturnItem(shell(), request_handler.url()); |
| 1586 WaitForInterrupt(download); | 1599 WaitForInterrupt(download); |
| 1587 | 1600 |
| 1588 base::FilePath intermediate_path = download->GetFullPath(); | 1601 base::FilePath intermediate_path = download->GetFullPath(); |
| 1589 ASSERT_FALSE(intermediate_path.empty()); | 1602 ASSERT_FALSE(intermediate_path.empty()); |
| 1590 ASSERT_TRUE(base::PathExists(intermediate_path)); | 1603 ASSERT_TRUE(PathExists(intermediate_path)); |
| 1591 | 1604 |
| 1592 download->Cancel(true /* user_cancel */); | 1605 download->Cancel(true /* user_cancel */); |
| 1593 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1606 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 1594 RunAllPendingInMessageLoop(); | 1607 RunAllPendingInMessageLoop(); |
| 1595 | 1608 |
| 1596 // The intermediate file should now be gone. | 1609 // The intermediate file should now be gone. |
| 1597 EXPECT_FALSE(base::PathExists(intermediate_path)); | 1610 EXPECT_FALSE(PathExists(intermediate_path)); |
| 1598 EXPECT_TRUE(download->GetFullPath().empty()); | 1611 EXPECT_TRUE(download->GetFullPath().empty()); |
| 1599 } | 1612 } |
| 1600 | 1613 |
| 1601 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveInterruptedDownload) { | 1614 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveInterruptedDownload) { |
| 1602 TestDownloadRequestHandler request_handler; | 1615 TestDownloadRequestHandler request_handler; |
| 1603 request_handler.StartServing( | 1616 request_handler.StartServing( |
| 1604 TestDownloadRequestHandler::Parameters::WithSingleInterruption()); | 1617 TestDownloadRequestHandler::Parameters::WithSingleInterruption()); |
| 1605 | 1618 |
| 1606 DownloadItem* download = | 1619 DownloadItem* download = |
| 1607 StartDownloadAndReturnItem(shell(), request_handler.url()); | 1620 StartDownloadAndReturnItem(shell(), request_handler.url()); |
| 1608 WaitForInterrupt(download); | 1621 WaitForInterrupt(download); |
| 1609 | 1622 |
| 1610 base::FilePath intermediate_path = download->GetFullPath(); | 1623 base::FilePath intermediate_path = download->GetFullPath(); |
| 1611 ASSERT_FALSE(intermediate_path.empty()); | 1624 ASSERT_FALSE(intermediate_path.empty()); |
| 1612 ASSERT_TRUE(base::PathExists(intermediate_path)); | 1625 ASSERT_TRUE(PathExists(intermediate_path)); |
| 1613 | 1626 |
| 1614 download->Remove(); | 1627 download->Remove(); |
| 1615 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1628 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 1616 RunAllPendingInMessageLoop(); | 1629 RunAllPendingInMessageLoop(); |
| 1617 | 1630 |
| 1618 // The intermediate file should now be gone. | 1631 // The intermediate file should now be gone. |
| 1619 EXPECT_FALSE(base::PathExists(intermediate_path)); | 1632 EXPECT_FALSE(PathExists(intermediate_path)); |
| 1620 } | 1633 } |
| 1621 | 1634 |
| 1622 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveCompletedDownload) { | 1635 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveCompletedDownload) { |
| 1623 // A completed download shouldn't delete the downloaded file when it is | 1636 // A completed download shouldn't delete the downloaded file when it is |
| 1624 // removed. | 1637 // removed. |
| 1625 TestDownloadRequestHandler request_handler; | 1638 TestDownloadRequestHandler request_handler; |
| 1626 request_handler.StartServing(TestDownloadRequestHandler::Parameters()); | 1639 request_handler.StartServing(TestDownloadRequestHandler::Parameters()); |
| 1627 std::unique_ptr<DownloadTestObserver> completion_observer( | 1640 std::unique_ptr<DownloadTestObserver> completion_observer( |
| 1628 CreateWaiter(shell(), 1)); | 1641 CreateWaiter(shell(), 1)); |
| 1629 DownloadItem* download( | 1642 DownloadItem* download( |
| 1630 StartDownloadAndReturnItem(shell(), request_handler.url())); | 1643 StartDownloadAndReturnItem(shell(), request_handler.url())); |
| 1631 completion_observer->WaitForFinished(); | 1644 completion_observer->WaitForFinished(); |
| 1632 | 1645 |
| 1633 // The target path should exist. | 1646 // The target path should exist. |
| 1634 base::FilePath target_path(download->GetTargetFilePath()); | 1647 base::FilePath target_path(download->GetTargetFilePath()); |
| 1635 EXPECT_TRUE(base::PathExists(target_path)); | 1648 EXPECT_TRUE(PathExists(target_path)); |
| 1636 download->Remove(); | 1649 download->Remove(); |
| 1637 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1650 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 1638 RunAllPendingInMessageLoop(); | 1651 RunAllPendingInMessageLoop(); |
| 1639 | 1652 |
| 1640 // The file should still exist. | 1653 // The file should still exist. |
| 1641 EXPECT_TRUE(base::PathExists(target_path)); | 1654 EXPECT_TRUE(PathExists(target_path)); |
| 1642 } | 1655 } |
| 1643 | 1656 |
| 1644 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumingDownload) { | 1657 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumingDownload) { |
| 1645 TestDownloadRequestHandler::Parameters parameters = | 1658 TestDownloadRequestHandler::Parameters parameters = |
| 1646 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); | 1659 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); |
| 1647 TestDownloadRequestHandler request_handler; | 1660 TestDownloadRequestHandler request_handler; |
| 1648 request_handler.StartServing(parameters); | 1661 request_handler.StartServing(parameters); |
| 1649 | 1662 |
| 1650 DownloadItem* download = | 1663 DownloadItem* download = |
| 1651 StartDownloadAndReturnItem(shell(), request_handler.url()); | 1664 StartDownloadAndReturnItem(shell(), request_handler.url()); |
| 1652 WaitForInterrupt(download); | 1665 WaitForInterrupt(download); |
| 1653 | 1666 |
| 1654 base::FilePath intermediate_path(download->GetFullPath()); | 1667 base::FilePath intermediate_path(download->GetFullPath()); |
| 1655 ASSERT_FALSE(intermediate_path.empty()); | 1668 ASSERT_FALSE(intermediate_path.empty()); |
| 1656 EXPECT_TRUE(base::PathExists(intermediate_path)); | 1669 EXPECT_TRUE(PathExists(intermediate_path)); |
| 1657 | 1670 |
| 1658 // Resume and remove download. We expect only a single OnDownloadCreated() | 1671 // Resume and remove download. We expect only a single OnDownloadCreated() |
| 1659 // call, and that's for the second download created below. | 1672 // call, and that's for the second download created below. |
| 1660 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); | 1673 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); |
| 1661 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | 1674 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); |
| 1662 | 1675 |
| 1663 TestRequestStartHandler request_start_handler; | 1676 TestRequestStartHandler request_start_handler; |
| 1664 parameters.on_start_handler = request_start_handler.GetOnStartHandler(); | 1677 parameters.on_start_handler = request_start_handler.GetOnStartHandler(); |
| 1665 request_handler.StartServing(parameters); | 1678 request_handler.StartServing(parameters); |
| 1666 | 1679 |
| 1667 download->Resume(); | 1680 download->Resume(); |
| 1668 request_start_handler.WaitForCallback(); | 1681 request_start_handler.WaitForCallback(); |
| 1669 | 1682 |
| 1670 // At this point, the download resumption request has been sent out, but the | 1683 // At this point, the download resumption request has been sent out, but the |
| 1671 // reponse hasn't been received yet. | 1684 // reponse hasn't been received yet. |
| 1672 download->Remove(); | 1685 download->Remove(); |
| 1673 | 1686 |
| 1674 request_start_handler.RespondWith(std::string(), net::OK); | 1687 request_start_handler.RespondWith(std::string(), net::OK); |
| 1675 | 1688 |
| 1676 // The intermediate file should now be gone. | 1689 // The intermediate file should now be gone. |
| 1677 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1690 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 1678 RunAllPendingInMessageLoop(); | 1691 RunAllPendingInMessageLoop(); |
| 1679 EXPECT_FALSE(base::PathExists(intermediate_path)); | 1692 EXPECT_FALSE(PathExists(intermediate_path)); |
| 1680 | 1693 |
| 1681 parameters.ClearInjectedErrors(); | 1694 parameters.ClearInjectedErrors(); |
| 1682 parameters.on_start_handler.Reset(); | 1695 parameters.on_start_handler.Reset(); |
| 1683 request_handler.StartServing(parameters); | 1696 request_handler.StartServing(parameters); |
| 1684 | 1697 |
| 1685 // Start the second download and wait until it's done. This exercises the | 1698 // Start the second download and wait until it's done. This exercises the |
| 1686 // entire downloads stack and effectively flushes all of our worker threads. | 1699 // entire downloads stack and effectively flushes all of our worker threads. |
| 1687 // We are testing whether the URL request created in the previous | 1700 // We are testing whether the URL request created in the previous |
| 1688 // DownloadItem::Resume() call reulted in a new download or not. | 1701 // DownloadItem::Resume() call reulted in a new download or not. |
| 1689 NavigateToURLAndWaitForDownload(shell(), request_handler.url(), | 1702 NavigateToURLAndWaitForDownload(shell(), request_handler.url(), |
| 1690 DownloadItem::COMPLETE); | 1703 DownloadItem::COMPLETE); |
| 1691 EXPECT_TRUE(EnsureNoPendingDownloads()); | 1704 EXPECT_TRUE(EnsureNoPendingDownloads()); |
| 1692 } | 1705 } |
| 1693 | 1706 |
| 1694 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelResumingDownload) { | 1707 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelResumingDownload) { |
| 1695 TestDownloadRequestHandler::Parameters parameters = | 1708 TestDownloadRequestHandler::Parameters parameters = |
| 1696 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); | 1709 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); |
| 1697 TestDownloadRequestHandler request_handler; | 1710 TestDownloadRequestHandler request_handler; |
| 1698 request_handler.StartServing(parameters); | 1711 request_handler.StartServing(parameters); |
| 1699 | 1712 |
| 1700 DownloadItem* download = | 1713 DownloadItem* download = |
| 1701 StartDownloadAndReturnItem(shell(), request_handler.url()); | 1714 StartDownloadAndReturnItem(shell(), request_handler.url()); |
| 1702 WaitForInterrupt(download); | 1715 WaitForInterrupt(download); |
| 1703 | 1716 |
| 1704 base::FilePath intermediate_path(download->GetFullPath()); | 1717 base::FilePath intermediate_path(download->GetFullPath()); |
| 1705 ASSERT_FALSE(intermediate_path.empty()); | 1718 ASSERT_FALSE(intermediate_path.empty()); |
| 1706 EXPECT_TRUE(base::PathExists(intermediate_path)); | 1719 EXPECT_TRUE(PathExists(intermediate_path)); |
| 1707 | 1720 |
| 1708 // Resume and cancel download. We expect only a single OnDownloadCreated() | 1721 // Resume and cancel download. We expect only a single OnDownloadCreated() |
| 1709 // call, and that's for the second download created below. | 1722 // call, and that's for the second download created below. |
| 1710 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); | 1723 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); |
| 1711 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | 1724 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); |
| 1712 | 1725 |
| 1713 TestRequestStartHandler request_start_handler; | 1726 TestRequestStartHandler request_start_handler; |
| 1714 parameters.on_start_handler = request_start_handler.GetOnStartHandler(); | 1727 parameters.on_start_handler = request_start_handler.GetOnStartHandler(); |
| 1715 request_handler.StartServing(parameters); | 1728 request_handler.StartServing(parameters); |
| 1716 | 1729 |
| 1717 download->Resume(); | 1730 download->Resume(); |
| 1718 request_start_handler.WaitForCallback(); | 1731 request_start_handler.WaitForCallback(); |
| 1719 | 1732 |
| 1720 // At this point, the download item has initiated a network request for the | 1733 // At this point, the download item has initiated a network request for the |
| 1721 // resumption attempt, but hasn't received a response yet. | 1734 // resumption attempt, but hasn't received a response yet. |
| 1722 download->Cancel(true /* user_cancel */); | 1735 download->Cancel(true /* user_cancel */); |
| 1723 | 1736 |
| 1724 request_start_handler.RespondWith(std::string(), net::OK); | 1737 request_start_handler.RespondWith(std::string(), net::OK); |
| 1725 | 1738 |
| 1726 // The intermediate file should now be gone. | 1739 // The intermediate file should now be gone. |
| 1727 RunAllPendingInMessageLoop(BrowserThread::IO); | 1740 RunAllPendingInMessageLoop(BrowserThread::IO); |
| 1728 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1741 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 1729 RunAllPendingInMessageLoop(); | 1742 RunAllPendingInMessageLoop(); |
| 1730 EXPECT_FALSE(base::PathExists(intermediate_path)); | 1743 EXPECT_FALSE(PathExists(intermediate_path)); |
| 1731 | 1744 |
| 1732 parameters.ClearInjectedErrors(); | 1745 parameters.ClearInjectedErrors(); |
| 1733 parameters.on_start_handler.Reset(); | 1746 parameters.on_start_handler.Reset(); |
| 1734 request_handler.StartServing(parameters); | 1747 request_handler.StartServing(parameters); |
| 1735 | 1748 |
| 1736 // Start the second download and wait until it's done. This exercises the | 1749 // Start the second download and wait until it's done. This exercises the |
| 1737 // entire downloads stack and effectively flushes all of our worker threads. | 1750 // entire downloads stack and effectively flushes all of our worker threads. |
| 1738 // We are testing whether the URL request created in the previous | 1751 // We are testing whether the URL request created in the previous |
| 1739 // DownloadItem::Resume() call reulted in a new download or not. | 1752 // DownloadItem::Resume() call reulted in a new download or not. |
| 1740 NavigateToURLAndWaitForDownload(shell(), request_handler.url(), | 1753 NavigateToURLAndWaitForDownload(shell(), request_handler.url(), |
| 1741 DownloadItem::COMPLETE); | 1754 DownloadItem::COMPLETE); |
| 1742 EXPECT_TRUE(EnsureNoPendingDownloads()); | 1755 EXPECT_TRUE(EnsureNoPendingDownloads()); |
| 1743 } | 1756 } |
| 1744 | 1757 |
| 1745 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumedDownload) { | 1758 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumedDownload) { |
| 1746 TestDownloadRequestHandler::Parameters parameters = | 1759 TestDownloadRequestHandler::Parameters parameters = |
| 1747 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); | 1760 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); |
| 1748 TestDownloadRequestHandler request_handler; | 1761 TestDownloadRequestHandler request_handler; |
| 1749 request_handler.StartServing(parameters); | 1762 request_handler.StartServing(parameters); |
| 1750 | 1763 |
| 1751 DownloadItem* download = | 1764 DownloadItem* download = |
| 1752 StartDownloadAndReturnItem(shell(), request_handler.url()); | 1765 StartDownloadAndReturnItem(shell(), request_handler.url()); |
| 1753 WaitForInterrupt(download); | 1766 WaitForInterrupt(download); |
| 1754 | 1767 |
| 1755 base::FilePath intermediate_path(download->GetFullPath()); | 1768 base::FilePath intermediate_path(download->GetFullPath()); |
| 1756 base::FilePath target_path(download->GetTargetFilePath()); | 1769 base::FilePath target_path(download->GetTargetFilePath()); |
| 1757 ASSERT_FALSE(intermediate_path.empty()); | 1770 ASSERT_FALSE(intermediate_path.empty()); |
| 1758 EXPECT_TRUE(base::PathExists(intermediate_path)); | 1771 EXPECT_TRUE(PathExists(intermediate_path)); |
| 1759 EXPECT_FALSE(base::PathExists(target_path)); | 1772 EXPECT_FALSE(PathExists(target_path)); |
| 1760 | 1773 |
| 1761 // Resume and remove download. We don't expect OnDownloadCreated() calls. | 1774 // Resume and remove download. We don't expect OnDownloadCreated() calls. |
| 1762 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); | 1775 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); |
| 1763 EXPECT_CALL(dm_observer, OnDownloadCreated(_, _)).Times(0); | 1776 EXPECT_CALL(dm_observer, OnDownloadCreated(_, _)).Times(0); |
| 1764 | 1777 |
| 1765 download->Resume(); | 1778 download->Resume(); |
| 1766 WaitForInProgress(download); | 1779 WaitForInProgress(download); |
| 1767 | 1780 |
| 1768 download->Remove(); | 1781 download->Remove(); |
| 1769 | 1782 |
| 1770 // The intermediate file should now be gone. | 1783 // The intermediate file should now be gone. |
| 1771 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1784 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 1772 RunAllPendingInMessageLoop(); | 1785 RunAllPendingInMessageLoop(); |
| 1773 EXPECT_FALSE(base::PathExists(intermediate_path)); | 1786 EXPECT_FALSE(PathExists(intermediate_path)); |
| 1774 EXPECT_FALSE(base::PathExists(target_path)); | 1787 EXPECT_FALSE(PathExists(target_path)); |
| 1775 EXPECT_TRUE(EnsureNoPendingDownloads()); | 1788 EXPECT_TRUE(EnsureNoPendingDownloads()); |
| 1776 } | 1789 } |
| 1777 | 1790 |
| 1778 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelResumedDownload) { | 1791 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelResumedDownload) { |
| 1779 TestDownloadRequestHandler::Parameters parameters = | 1792 TestDownloadRequestHandler::Parameters parameters = |
| 1780 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); | 1793 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); |
| 1781 TestDownloadRequestHandler request_handler; | 1794 TestDownloadRequestHandler request_handler; |
| 1782 request_handler.StartServing(parameters); | 1795 request_handler.StartServing(parameters); |
| 1783 | 1796 |
| 1784 DownloadItem* download = | 1797 DownloadItem* download = |
| 1785 StartDownloadAndReturnItem(shell(), request_handler.url()); | 1798 StartDownloadAndReturnItem(shell(), request_handler.url()); |
| 1786 WaitForInterrupt(download); | 1799 WaitForInterrupt(download); |
| 1787 | 1800 |
| 1788 base::FilePath intermediate_path(download->GetFullPath()); | 1801 base::FilePath intermediate_path(download->GetFullPath()); |
| 1789 base::FilePath target_path(download->GetTargetFilePath()); | 1802 base::FilePath target_path(download->GetTargetFilePath()); |
| 1790 ASSERT_FALSE(intermediate_path.empty()); | 1803 ASSERT_FALSE(intermediate_path.empty()); |
| 1791 EXPECT_TRUE(base::PathExists(intermediate_path)); | 1804 EXPECT_TRUE(PathExists(intermediate_path)); |
| 1792 EXPECT_FALSE(base::PathExists(target_path)); | 1805 EXPECT_FALSE(PathExists(target_path)); |
| 1793 | 1806 |
| 1794 // Resume and remove download. We don't expect OnDownloadCreated() calls. | 1807 // Resume and remove download. We don't expect OnDownloadCreated() calls. |
| 1795 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); | 1808 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); |
| 1796 EXPECT_CALL(dm_observer, OnDownloadCreated(_, _)).Times(0); | 1809 EXPECT_CALL(dm_observer, OnDownloadCreated(_, _)).Times(0); |
| 1797 | 1810 |
| 1798 download->Resume(); | 1811 download->Resume(); |
| 1799 WaitForInProgress(download); | 1812 WaitForInProgress(download); |
| 1800 | 1813 |
| 1801 download->Cancel(true); | 1814 download->Cancel(true); |
| 1802 | 1815 |
| 1803 // The intermediate file should now be gone. | 1816 // The intermediate file should now be gone. |
| 1804 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1817 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 1805 RunAllPendingInMessageLoop(); | 1818 RunAllPendingInMessageLoop(); |
| 1806 EXPECT_FALSE(base::PathExists(intermediate_path)); | 1819 EXPECT_FALSE(PathExists(intermediate_path)); |
| 1807 EXPECT_FALSE(base::PathExists(target_path)); | 1820 EXPECT_FALSE(PathExists(target_path)); |
| 1808 EXPECT_TRUE(EnsureNoPendingDownloads()); | 1821 EXPECT_TRUE(EnsureNoPendingDownloads()); |
| 1809 } | 1822 } |
| 1810 | 1823 |
| 1811 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_NoFile) { | 1824 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_NoFile) { |
| 1812 TestDownloadRequestHandler request_handler; | 1825 TestDownloadRequestHandler request_handler; |
| 1813 TestDownloadRequestHandler::Parameters parameters; | 1826 TestDownloadRequestHandler::Parameters parameters; |
| 1814 request_handler.StartServing(parameters); | 1827 request_handler.StartServing(parameters); |
| 1815 | 1828 |
| 1816 base::FilePath intermediate_file_path = | 1829 base::FilePath intermediate_file_path = |
| 1817 GetDownloadDirectory().AppendASCII("intermediate"); | 1830 GetDownloadDirectory().AppendASCII("intermediate"); |
| 1818 std::vector<GURL> url_chain; | 1831 std::vector<GURL> url_chain; |
| 1819 | 1832 |
| 1820 const int kIntermediateSize = 1331; | 1833 const int kIntermediateSize = 1331; |
| 1821 url_chain.push_back(request_handler.url()); | 1834 url_chain.push_back(request_handler.url()); |
| 1822 | 1835 |
| 1823 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( | 1836 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( |
| 1824 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, | 1837 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, |
| 1825 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), | 1838 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), |
| 1826 "application/octet-stream", "application/octet-stream", base::Time::Now(), | 1839 "application/octet-stream", "application/octet-stream", base::Time::Now(), |
| 1827 base::Time(), parameters.etag, std::string(), kIntermediateSize, | 1840 base::Time(), parameters.etag, std::string(), kIntermediateSize, |
| 1828 parameters.size, std::string(), DownloadItem::INTERRUPTED, | 1841 parameters.size, std::string(), DownloadItem::INTERRUPTED, |
| 1829 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1842 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1830 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); | 1843 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); |
| 1831 | 1844 |
| 1832 download->Resume(); | 1845 download->Resume(); |
| 1833 WaitForCompletion(download); | 1846 WaitForCompletion(download); |
| 1834 | 1847 |
| 1835 EXPECT_FALSE(base::PathExists(intermediate_file_path)); | 1848 EXPECT_FALSE(PathExists(intermediate_file_path)); |
| 1836 ReadAndVerifyFileContents(parameters.pattern_generator_seed, | 1849 ReadAndVerifyFileContents(parameters.pattern_generator_seed, |
| 1837 parameters.size, | 1850 parameters.size, |
| 1838 download->GetTargetFilePath()); | 1851 download->GetTargetFilePath()); |
| 1839 | 1852 |
| 1840 TestDownloadRequestHandler::CompletedRequests completed_requests; | 1853 TestDownloadRequestHandler::CompletedRequests completed_requests; |
| 1841 request_handler.GetCompletedRequestInfo(&completed_requests); | 1854 request_handler.GetCompletedRequestInfo(&completed_requests); |
| 1842 | 1855 |
| 1843 // There will be two requests. The first one is issued optimistically assuming | 1856 // There will be two requests. The first one is issued optimistically assuming |
| 1844 // that the intermediate file exists and matches the size expectations set | 1857 // that the intermediate file exists and matches the size expectations set |
| 1845 // forth in the download metadata (i.e. assuming that a 1331 byte file exists | 1858 // forth in the download metadata (i.e. assuming that a 1331 byte file exists |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1867 request_handler.StartServing(parameters); | 1880 request_handler.StartServing(parameters); |
| 1868 | 1881 |
| 1869 base::FilePath intermediate_file_path = | 1882 base::FilePath intermediate_file_path = |
| 1870 GetDownloadDirectory().AppendASCII("intermediate"); | 1883 GetDownloadDirectory().AppendASCII("intermediate"); |
| 1871 std::vector<GURL> url_chain; | 1884 std::vector<GURL> url_chain; |
| 1872 | 1885 |
| 1873 const int kIntermediateSize = 1331; | 1886 const int kIntermediateSize = 1331; |
| 1874 std::vector<char> buffer(kIntermediateSize); | 1887 std::vector<char> buffer(kIntermediateSize); |
| 1875 request_handler.GetPatternBytes( | 1888 request_handler.GetPatternBytes( |
| 1876 parameters.pattern_generator_seed, 0, buffer.size(), buffer.data()); | 1889 parameters.pattern_generator_seed, 0, buffer.size(), buffer.data()); |
| 1877 ASSERT_EQ( | 1890 { |
| 1878 kIntermediateSize, | 1891 base::ThreadRestrictions::ScopedAllowIO allow_io_for_test_setup; |
| 1879 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size())); | 1892 ASSERT_EQ(kIntermediateSize, base::WriteFile(intermediate_file_path, |
| 1893 buffer.data(), buffer.size())); |
| 1894 } |
| 1880 | 1895 |
| 1881 url_chain.push_back(request_handler.url()); | 1896 url_chain.push_back(request_handler.url()); |
| 1882 | 1897 |
| 1883 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( | 1898 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( |
| 1884 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, | 1899 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, |
| 1885 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), | 1900 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), |
| 1886 "application/octet-stream", "application/octet-stream", base::Time::Now(), | 1901 "application/octet-stream", "application/octet-stream", base::Time::Now(), |
| 1887 base::Time(), parameters.etag, std::string(), kIntermediateSize, | 1902 base::Time(), parameters.etag, std::string(), kIntermediateSize, |
| 1888 parameters.size, std::string(), DownloadItem::INTERRUPTED, | 1903 parameters.size, std::string(), DownloadItem::INTERRUPTED, |
| 1889 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1904 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1890 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); | 1905 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); |
| 1891 | 1906 |
| 1892 download->Resume(); | 1907 download->Resume(); |
| 1893 WaitForCompletion(download); | 1908 WaitForCompletion(download); |
| 1894 | 1909 |
| 1895 EXPECT_FALSE(base::PathExists(intermediate_file_path)); | 1910 EXPECT_FALSE(PathExists(intermediate_file_path)); |
| 1896 ReadAndVerifyFileContents(parameters.pattern_generator_seed, | 1911 ReadAndVerifyFileContents(parameters.pattern_generator_seed, |
| 1897 parameters.size, | 1912 parameters.size, |
| 1898 download->GetTargetFilePath()); | 1913 download->GetTargetFilePath()); |
| 1899 | 1914 |
| 1900 TestDownloadRequestHandler::CompletedRequests completed_requests; | 1915 TestDownloadRequestHandler::CompletedRequests completed_requests; |
| 1901 request_handler.GetCompletedRequestInfo(&completed_requests); | 1916 request_handler.GetCompletedRequestInfo(&completed_requests); |
| 1902 | 1917 |
| 1903 // There's only one network request issued, and that is for the remainder of | 1918 // There's only one network request issued, and that is for the remainder of |
| 1904 // the file. | 1919 // the file. |
| 1905 ASSERT_EQ(1u, completed_requests.size()); | 1920 ASSERT_EQ(1u, completed_requests.size()); |
| 1906 EXPECT_EQ(parameters.size - kIntermediateSize, | 1921 EXPECT_EQ(parameters.size - kIntermediateSize, |
| 1907 completed_requests[0]->transferred_byte_count); | 1922 completed_requests[0]->transferred_byte_count); |
| 1908 } | 1923 } |
| 1909 | 1924 |
| 1910 IN_PROC_BROWSER_TEST_F(DownloadContentTest, | 1925 IN_PROC_BROWSER_TEST_F(DownloadContentTest, |
| 1911 ResumeRestoredDownload_EtagMismatch) { | 1926 ResumeRestoredDownload_EtagMismatch) { |
| 1912 TestDownloadRequestHandler request_handler; | 1927 TestDownloadRequestHandler request_handler; |
| 1913 TestDownloadRequestHandler::Parameters parameters; | 1928 TestDownloadRequestHandler::Parameters parameters; |
| 1914 request_handler.StartServing(parameters); | 1929 request_handler.StartServing(parameters); |
| 1915 | 1930 |
| 1916 base::FilePath intermediate_file_path = | 1931 base::FilePath intermediate_file_path = |
| 1917 GetDownloadDirectory().AppendASCII("intermediate"); | 1932 GetDownloadDirectory().AppendASCII("intermediate"); |
| 1918 std::vector<GURL> url_chain; | 1933 std::vector<GURL> url_chain; |
| 1919 | 1934 |
| 1920 const int kIntermediateSize = 1331; | 1935 const int kIntermediateSize = 1331; |
| 1921 std::vector<char> buffer(kIntermediateSize); | 1936 std::vector<char> buffer(kIntermediateSize); |
| 1922 request_handler.GetPatternBytes( | 1937 request_handler.GetPatternBytes( |
| 1923 parameters.pattern_generator_seed + 1, 0, buffer.size(), buffer.data()); | 1938 parameters.pattern_generator_seed + 1, 0, buffer.size(), buffer.data()); |
| 1924 ASSERT_EQ( | 1939 { |
| 1925 kIntermediateSize, | 1940 base::ThreadRestrictions::ScopedAllowIO allow_io_for_test_setup; |
| 1926 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size())); | 1941 ASSERT_EQ(kIntermediateSize, base::WriteFile(intermediate_file_path, |
| 1942 buffer.data(), buffer.size())); |
| 1943 } |
| 1927 | 1944 |
| 1928 url_chain.push_back(request_handler.url()); | 1945 url_chain.push_back(request_handler.url()); |
| 1929 | 1946 |
| 1930 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( | 1947 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( |
| 1931 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, | 1948 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, |
| 1932 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), | 1949 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), |
| 1933 "application/octet-stream", "application/octet-stream", base::Time::Now(), | 1950 "application/octet-stream", "application/octet-stream", base::Time::Now(), |
| 1934 base::Time(), "fake-etag", std::string(), kIntermediateSize, | 1951 base::Time(), "fake-etag", std::string(), kIntermediateSize, |
| 1935 parameters.size, std::string(), DownloadItem::INTERRUPTED, | 1952 parameters.size, std::string(), DownloadItem::INTERRUPTED, |
| 1936 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1953 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1937 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); | 1954 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); |
| 1938 | 1955 |
| 1939 download->Resume(); | 1956 download->Resume(); |
| 1940 WaitForCompletion(download); | 1957 WaitForCompletion(download); |
| 1941 | 1958 |
| 1942 EXPECT_FALSE(base::PathExists(intermediate_file_path)); | 1959 EXPECT_FALSE(PathExists(intermediate_file_path)); |
| 1943 ReadAndVerifyFileContents(parameters.pattern_generator_seed, | 1960 ReadAndVerifyFileContents(parameters.pattern_generator_seed, |
| 1944 parameters.size, | 1961 parameters.size, |
| 1945 download->GetTargetFilePath()); | 1962 download->GetTargetFilePath()); |
| 1946 | 1963 |
| 1947 TestDownloadRequestHandler::CompletedRequests completed_requests; | 1964 TestDownloadRequestHandler::CompletedRequests completed_requests; |
| 1948 request_handler.GetCompletedRequestInfo(&completed_requests); | 1965 request_handler.GetCompletedRequestInfo(&completed_requests); |
| 1949 | 1966 |
| 1950 // There's only one network request issued. The If-Range header allows the | 1967 // There's only one network request issued. The If-Range header allows the |
| 1951 // server to respond with the entire entity in one go. The existing contents | 1968 // server to respond with the entire entity in one go. The existing contents |
| 1952 // of the file should be discarded, and overwritten by the new contents. | 1969 // of the file should be discarded, and overwritten by the new contents. |
| 1953 ASSERT_EQ(1u, completed_requests.size()); | 1970 ASSERT_EQ(1u, completed_requests.size()); |
| 1954 EXPECT_EQ(parameters.size, completed_requests[0]->transferred_byte_count); | 1971 EXPECT_EQ(parameters.size, completed_requests[0]->transferred_byte_count); |
| 1955 } | 1972 } |
| 1956 | 1973 |
| 1957 IN_PROC_BROWSER_TEST_F(DownloadContentTest, | 1974 IN_PROC_BROWSER_TEST_F(DownloadContentTest, |
| 1958 ResumeRestoredDownload_CorrectHash) { | 1975 ResumeRestoredDownload_CorrectHash) { |
| 1959 TestDownloadRequestHandler request_handler; | 1976 TestDownloadRequestHandler request_handler; |
| 1960 TestDownloadRequestHandler::Parameters parameters; | 1977 TestDownloadRequestHandler::Parameters parameters; |
| 1961 request_handler.StartServing(parameters); | 1978 request_handler.StartServing(parameters); |
| 1962 | 1979 |
| 1963 base::FilePath intermediate_file_path = | 1980 base::FilePath intermediate_file_path = |
| 1964 GetDownloadDirectory().AppendASCII("intermediate"); | 1981 GetDownloadDirectory().AppendASCII("intermediate"); |
| 1965 std::vector<GURL> url_chain; | 1982 std::vector<GURL> url_chain; |
| 1966 | 1983 |
| 1967 const int kIntermediateSize = 1331; | 1984 const int kIntermediateSize = 1331; |
| 1968 std::vector<char> buffer(kIntermediateSize); | 1985 std::vector<char> buffer(kIntermediateSize); |
| 1969 request_handler.GetPatternBytes( | 1986 request_handler.GetPatternBytes( |
| 1970 parameters.pattern_generator_seed, 0, buffer.size(), buffer.data()); | 1987 parameters.pattern_generator_seed, 0, buffer.size(), buffer.data()); |
| 1971 ASSERT_EQ( | 1988 { |
| 1972 kIntermediateSize, | 1989 base::ThreadRestrictions::ScopedAllowIO allow_io_for_test_setup; |
| 1973 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size())); | 1990 ASSERT_EQ(kIntermediateSize, base::WriteFile(intermediate_file_path, |
| 1991 buffer.data(), buffer.size())); |
| 1992 } |
| 1974 // SHA-256 hash of the pattern bytes in buffer. | 1993 // SHA-256 hash of the pattern bytes in buffer. |
| 1975 static const uint8_t kPartialHash[] = { | 1994 static const uint8_t kPartialHash[] = { |
| 1976 0x77, 0x14, 0xfd, 0x83, 0x06, 0x15, 0x10, 0x7a, 0x47, 0x15, 0xd3, | 1995 0x77, 0x14, 0xfd, 0x83, 0x06, 0x15, 0x10, 0x7a, 0x47, 0x15, 0xd3, |
| 1977 0xcf, 0xdd, 0x46, 0xa2, 0x61, 0x96, 0xff, 0xc3, 0xbb, 0x49, 0x30, | 1996 0xcf, 0xdd, 0x46, 0xa2, 0x61, 0x96, 0xff, 0xc3, 0xbb, 0x49, 0x30, |
| 1978 0xaf, 0x31, 0x3a, 0x64, 0x0b, 0xd5, 0xfa, 0xb1, 0xe3, 0x81}; | 1997 0xaf, 0x31, 0x3a, 0x64, 0x0b, 0xd5, 0xfa, 0xb1, 0xe3, 0x81}; |
| 1979 | 1998 |
| 1980 url_chain.push_back(request_handler.url()); | 1999 url_chain.push_back(request_handler.url()); |
| 1981 | 2000 |
| 1982 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( | 2001 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( |
| 1983 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, | 2002 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, |
| 1984 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), | 2003 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), |
| 1985 "application/octet-stream", "application/octet-stream", base::Time::Now(), | 2004 "application/octet-stream", "application/octet-stream", base::Time::Now(), |
| 1986 base::Time(), parameters.etag, std::string(), kIntermediateSize, | 2005 base::Time(), parameters.etag, std::string(), kIntermediateSize, |
| 1987 parameters.size, | 2006 parameters.size, |
| 1988 std::string(std::begin(kPartialHash), std::end(kPartialHash)), | 2007 std::string(std::begin(kPartialHash), std::end(kPartialHash)), |
| 1989 DownloadItem::INTERRUPTED, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 2008 DownloadItem::INTERRUPTED, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1990 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); | 2009 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); |
| 1991 | 2010 |
| 1992 download->Resume(); | 2011 download->Resume(); |
| 1993 WaitForCompletion(download); | 2012 WaitForCompletion(download); |
| 1994 | 2013 |
| 1995 EXPECT_FALSE(base::PathExists(intermediate_file_path)); | 2014 EXPECT_FALSE(PathExists(intermediate_file_path)); |
| 1996 ReadAndVerifyFileContents(parameters.pattern_generator_seed, | 2015 ReadAndVerifyFileContents(parameters.pattern_generator_seed, |
| 1997 parameters.size, | 2016 parameters.size, |
| 1998 download->GetTargetFilePath()); | 2017 download->GetTargetFilePath()); |
| 1999 | 2018 |
| 2000 TestDownloadRequestHandler::CompletedRequests completed_requests; | 2019 TestDownloadRequestHandler::CompletedRequests completed_requests; |
| 2001 request_handler.GetCompletedRequestInfo(&completed_requests); | 2020 request_handler.GetCompletedRequestInfo(&completed_requests); |
| 2002 | 2021 |
| 2003 // There's only one network request issued, and that is for the remainder of | 2022 // There's only one network request issued, and that is for the remainder of |
| 2004 // the file. | 2023 // the file. |
| 2005 ASSERT_EQ(1u, completed_requests.size()); | 2024 ASSERT_EQ(1u, completed_requests.size()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 2019 TestDownloadRequestHandler request_handler; | 2038 TestDownloadRequestHandler request_handler; |
| 2020 TestDownloadRequestHandler::Parameters parameters; | 2039 TestDownloadRequestHandler::Parameters parameters; |
| 2021 request_handler.StartServing(parameters); | 2040 request_handler.StartServing(parameters); |
| 2022 | 2041 |
| 2023 base::FilePath intermediate_file_path = | 2042 base::FilePath intermediate_file_path = |
| 2024 GetDownloadDirectory().AppendASCII("intermediate"); | 2043 GetDownloadDirectory().AppendASCII("intermediate"); |
| 2025 std::vector<GURL> url_chain; | 2044 std::vector<GURL> url_chain; |
| 2026 | 2045 |
| 2027 const int kIntermediateSize = 1331; | 2046 const int kIntermediateSize = 1331; |
| 2028 std::vector<char> buffer(kIntermediateSize); | 2047 std::vector<char> buffer(kIntermediateSize); |
| 2029 ASSERT_EQ( | 2048 { |
| 2030 kIntermediateSize, | 2049 base::ThreadRestrictions::ScopedAllowIO allow_io_for_test_setup; |
| 2031 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size())); | 2050 ASSERT_EQ(kIntermediateSize, base::WriteFile(intermediate_file_path, |
| 2051 buffer.data(), buffer.size())); |
| 2052 } |
| 2032 // SHA-256 hash of the expected pattern bytes in buffer. This doesn't match | 2053 // SHA-256 hash of the expected pattern bytes in buffer. This doesn't match |
| 2033 // the current contents of the intermediate file which should all be 0. | 2054 // the current contents of the intermediate file which should all be 0. |
| 2034 static const uint8_t kPartialHash[] = { | 2055 static const uint8_t kPartialHash[] = { |
| 2035 0x77, 0x14, 0xfd, 0x83, 0x06, 0x15, 0x10, 0x7a, 0x47, 0x15, 0xd3, | 2056 0x77, 0x14, 0xfd, 0x83, 0x06, 0x15, 0x10, 0x7a, 0x47, 0x15, 0xd3, |
| 2036 0xcf, 0xdd, 0x46, 0xa2, 0x61, 0x96, 0xff, 0xc3, 0xbb, 0x49, 0x30, | 2057 0xcf, 0xdd, 0x46, 0xa2, 0x61, 0x96, 0xff, 0xc3, 0xbb, 0x49, 0x30, |
| 2037 0xaf, 0x31, 0x3a, 0x64, 0x0b, 0xd5, 0xfa, 0xb1, 0xe3, 0x81}; | 2058 0xaf, 0x31, 0x3a, 0x64, 0x0b, 0xd5, 0xfa, 0xb1, 0xe3, 0x81}; |
| 2038 | 2059 |
| 2039 url_chain.push_back(request_handler.url()); | 2060 url_chain.push_back(request_handler.url()); |
| 2040 | 2061 |
| 2041 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( | 2062 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( |
| 2042 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, | 2063 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, |
| 2043 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), | 2064 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), |
| 2044 "application/octet-stream", "application/octet-stream", base::Time::Now(), | 2065 "application/octet-stream", "application/octet-stream", base::Time::Now(), |
| 2045 base::Time(), parameters.etag, std::string(), kIntermediateSize, | 2066 base::Time(), parameters.etag, std::string(), kIntermediateSize, |
| 2046 parameters.size, | 2067 parameters.size, |
| 2047 std::string(std::begin(kPartialHash), std::end(kPartialHash)), | 2068 std::string(std::begin(kPartialHash), std::end(kPartialHash)), |
| 2048 DownloadItem::INTERRUPTED, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 2069 DownloadItem::INTERRUPTED, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 2049 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); | 2070 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); |
| 2050 | 2071 |
| 2051 download->Resume(); | 2072 download->Resume(); |
| 2052 WaitForCompletion(download); | 2073 WaitForCompletion(download); |
| 2053 | 2074 |
| 2054 EXPECT_FALSE(base::PathExists(intermediate_file_path)); | 2075 EXPECT_FALSE(PathExists(intermediate_file_path)); |
| 2055 ReadAndVerifyFileContents(parameters.pattern_generator_seed, | 2076 ReadAndVerifyFileContents(parameters.pattern_generator_seed, |
| 2056 parameters.size, | 2077 parameters.size, |
| 2057 download->GetTargetFilePath()); | 2078 download->GetTargetFilePath()); |
| 2058 | 2079 |
| 2059 TestDownloadRequestHandler::CompletedRequests completed_requests; | 2080 TestDownloadRequestHandler::CompletedRequests completed_requests; |
| 2060 request_handler.GetCompletedRequestInfo(&completed_requests); | 2081 request_handler.GetCompletedRequestInfo(&completed_requests); |
| 2061 | 2082 |
| 2062 // There will be two requests. The first one is issued optimistically assuming | 2083 // There will be two requests. The first one is issued optimistically assuming |
| 2063 // that the intermediate file exists and matches the size expectations set | 2084 // that the intermediate file exists and matches the size expectations set |
| 2064 // forth in the download metadata (i.e. assuming that a 1331 byte file exists | 2085 // forth in the download metadata (i.e. assuming that a 1331 byte file exists |
| (...skipping 29 matching lines...) Expand all Loading... |
| 2094 | 2115 |
| 2095 base::FilePath intermediate_file_path = | 2116 base::FilePath intermediate_file_path = |
| 2096 GetDownloadDirectory().AppendASCII("intermediate"); | 2117 GetDownloadDirectory().AppendASCII("intermediate"); |
| 2097 std::vector<GURL> url_chain; | 2118 std::vector<GURL> url_chain; |
| 2098 | 2119 |
| 2099 const int kIntermediateSize = 1331; | 2120 const int kIntermediateSize = 1331; |
| 2100 // Size of file is slightly shorter than the size known to DownloadItem. | 2121 // Size of file is slightly shorter than the size known to DownloadItem. |
| 2101 std::vector<char> buffer(kIntermediateSize - 100); | 2122 std::vector<char> buffer(kIntermediateSize - 100); |
| 2102 request_handler.GetPatternBytes( | 2123 request_handler.GetPatternBytes( |
| 2103 parameters.pattern_generator_seed, 0, buffer.size(), buffer.data()); | 2124 parameters.pattern_generator_seed, 0, buffer.size(), buffer.data()); |
| 2104 ASSERT_EQ( | 2125 { |
| 2105 kIntermediateSize - 100, | 2126 base::ThreadRestrictions::ScopedAllowIO allow_io_for_test_setup; |
| 2106 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size())); | 2127 ASSERT_EQ( |
| 2128 kIntermediateSize - 100, |
| 2129 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size())); |
| 2130 } |
| 2107 url_chain.push_back(request_handler.url()); | 2131 url_chain.push_back(request_handler.url()); |
| 2108 | 2132 |
| 2109 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( | 2133 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( |
| 2110 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, | 2134 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, |
| 2111 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), | 2135 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), |
| 2112 "application/octet-stream", "application/octet-stream", base::Time::Now(), | 2136 "application/octet-stream", "application/octet-stream", base::Time::Now(), |
| 2113 base::Time(), parameters.etag, std::string(), kIntermediateSize, | 2137 base::Time(), parameters.etag, std::string(), kIntermediateSize, |
| 2114 parameters.size, std::string(), DownloadItem::INTERRUPTED, | 2138 parameters.size, std::string(), DownloadItem::INTERRUPTED, |
| 2115 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 2139 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 2116 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); | 2140 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); |
| 2117 | 2141 |
| 2118 download->Resume(); | 2142 download->Resume(); |
| 2119 WaitForCompletion(download); | 2143 WaitForCompletion(download); |
| 2120 | 2144 |
| 2121 EXPECT_FALSE(base::PathExists(intermediate_file_path)); | 2145 EXPECT_FALSE(PathExists(intermediate_file_path)); |
| 2122 ReadAndVerifyFileContents(parameters.pattern_generator_seed, | 2146 ReadAndVerifyFileContents(parameters.pattern_generator_seed, |
| 2123 parameters.size, | 2147 parameters.size, |
| 2124 download->GetTargetFilePath()); | 2148 download->GetTargetFilePath()); |
| 2125 | 2149 |
| 2126 TestDownloadRequestHandler::CompletedRequests completed_requests; | 2150 TestDownloadRequestHandler::CompletedRequests completed_requests; |
| 2127 request_handler.GetCompletedRequestInfo(&completed_requests); | 2151 request_handler.GetCompletedRequestInfo(&completed_requests); |
| 2128 | 2152 |
| 2129 // There will be two requests. The first one is issued optimistically assuming | 2153 // There will be two requests. The first one is issued optimistically assuming |
| 2130 // that the intermediate file exists and matches the size expectations set | 2154 // that the intermediate file exists and matches the size expectations set |
| 2131 // forth in the download metadata (i.e. assuming that a 1331 byte file exists | 2155 // forth in the download metadata (i.e. assuming that a 1331 byte file exists |
| (...skipping 27 matching lines...) Expand all Loading... |
| 2159 request_handler.StartServing(parameters); | 2183 request_handler.StartServing(parameters); |
| 2160 | 2184 |
| 2161 base::FilePath intermediate_file_path = | 2185 base::FilePath intermediate_file_path = |
| 2162 GetDownloadDirectory().AppendASCII("intermediate"); | 2186 GetDownloadDirectory().AppendASCII("intermediate"); |
| 2163 std::vector<GURL> url_chain; | 2187 std::vector<GURL> url_chain; |
| 2164 | 2188 |
| 2165 // Size of file is slightly longer than the size known to DownloadItem. | 2189 // Size of file is slightly longer than the size known to DownloadItem. |
| 2166 std::vector<char> buffer(kIntermediateSize + 100); | 2190 std::vector<char> buffer(kIntermediateSize + 100); |
| 2167 request_handler.GetPatternBytes( | 2191 request_handler.GetPatternBytes( |
| 2168 parameters.pattern_generator_seed, 0, buffer.size(), buffer.data()); | 2192 parameters.pattern_generator_seed, 0, buffer.size(), buffer.data()); |
| 2169 ASSERT_EQ( | 2193 { |
| 2170 kIntermediateSize + 100, | 2194 base::ThreadRestrictions::ScopedAllowIO allow_io_for_test_setup; |
| 2171 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size())); | 2195 ASSERT_EQ( |
| 2196 kIntermediateSize + 100, |
| 2197 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size())); |
| 2198 } |
| 2172 url_chain.push_back(request_handler.url()); | 2199 url_chain.push_back(request_handler.url()); |
| 2173 | 2200 |
| 2174 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( | 2201 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem( |
| 2175 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, | 2202 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583", 1, intermediate_file_path, |
| 2176 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), | 2203 base::FilePath(), url_chain, GURL(), GURL(), GURL(), GURL(), |
| 2177 "application/octet-stream", "application/octet-stream", base::Time::Now(), | 2204 "application/octet-stream", "application/octet-stream", base::Time::Now(), |
| 2178 base::Time(), parameters.etag, std::string(), kIntermediateSize, | 2205 base::Time(), parameters.etag, std::string(), kIntermediateSize, |
| 2179 parameters.size, std::string(), DownloadItem::INTERRUPTED, | 2206 parameters.size, std::string(), DownloadItem::INTERRUPTED, |
| 2180 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 2207 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 2181 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); | 2208 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false); |
| 2182 | 2209 |
| 2183 download->Resume(); | 2210 download->Resume(); |
| 2184 WaitForCompletion(download); | 2211 WaitForCompletion(download); |
| 2185 | 2212 |
| 2186 EXPECT_FALSE(base::PathExists(intermediate_file_path)); | 2213 EXPECT_FALSE(PathExists(intermediate_file_path)); |
| 2187 ReadAndVerifyFileContents(parameters.pattern_generator_seed, | 2214 ReadAndVerifyFileContents(parameters.pattern_generator_seed, |
| 2188 parameters.size, | 2215 parameters.size, |
| 2189 download->GetTargetFilePath()); | 2216 download->GetTargetFilePath()); |
| 2190 | 2217 |
| 2191 TestDownloadRequestHandler::CompletedRequests completed_requests; | 2218 TestDownloadRequestHandler::CompletedRequests completed_requests; |
| 2192 request_handler.GetCompletedRequestInfo(&completed_requests); | 2219 request_handler.GetCompletedRequestInfo(&completed_requests); |
| 2193 | 2220 |
| 2194 // There should be only one request. The intermediate file should be truncated | 2221 // There should be only one request. The intermediate file should be truncated |
| 2195 // to the expected size, and the request should be issued for the remainder. | 2222 // to the expected size, and the request should be issued for the remainder. |
| 2196 // | 2223 // |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2516 GURL document_url = | 2543 GURL document_url = |
| 2517 origin_two.GetURL("/iframe-host.html?target=" + frame_url.spec()); | 2544 origin_two.GetURL("/iframe-host.html?target=" + frame_url.spec()); |
| 2518 DownloadItem* download = StartDownloadAndReturnItem(shell(), document_url); | 2545 DownloadItem* download = StartDownloadAndReturnItem(shell(), document_url); |
| 2519 WaitForCompletion(download); | 2546 WaitForCompletion(download); |
| 2520 | 2547 |
| 2521 EXPECT_STREQ(FILE_PATH_LITERAL("download-test.lib"), | 2548 EXPECT_STREQ(FILE_PATH_LITERAL("download-test.lib"), |
| 2522 download->GetTargetFilePath().BaseName().value().c_str()); | 2549 download->GetTargetFilePath().BaseName().value().c_str()); |
| 2523 } | 2550 } |
| 2524 | 2551 |
| 2525 } // namespace content | 2552 } // namespace content |
| OLD | NEW |