| 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 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 // This is a test DownloadFileImpl that has no retry delay and, on Posix, | 117 // This is a test DownloadFileImpl that has no retry delay and, on Posix, |
| 118 // retries renames failed due to ACCESS_DENIED. | 118 // retries renames failed due to ACCESS_DENIED. |
| 119 class TestDownloadFileImpl : public DownloadFileImpl { | 119 class TestDownloadFileImpl : public DownloadFileImpl { |
| 120 public: | 120 public: |
| 121 TestDownloadFileImpl( | 121 TestDownloadFileImpl( |
| 122 std::unique_ptr<DownloadSaveInfo> save_info, | 122 std::unique_ptr<DownloadSaveInfo> save_info, |
| 123 const base::FilePath& default_downloads_directory, | 123 const base::FilePath& default_downloads_directory, |
| 124 std::unique_ptr<ByteStreamReader> stream, | 124 std::unique_ptr<ByteStreamReader> stream, |
| 125 const std::vector<DownloadItem::ReceivedSlice>& received_slices, | 125 const std::vector<DownloadItem::ReceivedSlice>& received_slices, |
| 126 const net::NetLogWithSource& net_log, | 126 const net::NetLogWithSource& net_log, |
| 127 bool is_sparse_file, | |
| 128 base::WeakPtr<DownloadDestinationObserver> observer) | 127 base::WeakPtr<DownloadDestinationObserver> observer) |
| 129 : DownloadFileImpl(std::move(save_info), | 128 : DownloadFileImpl(std::move(save_info), |
| 130 default_downloads_directory, | 129 default_downloads_directory, |
| 131 std::move(stream), | 130 std::move(stream), |
| 132 received_slices, | 131 received_slices, |
| 133 net_log, | 132 net_log, |
| 134 is_sparse_file, | |
| 135 observer) {} | 133 observer) {} |
| 136 | 134 |
| 137 protected: | 135 protected: |
| 138 base::TimeDelta GetRetryDelayForFailedRename(int attempt_count) override { | 136 base::TimeDelta GetRetryDelayForFailedRename(int attempt_count) override { |
| 139 return base::TimeDelta::FromMilliseconds(0); | 137 return base::TimeDelta::FromMilliseconds(0); |
| 140 } | 138 } |
| 141 | 139 |
| 142 #if !defined(OS_WIN) | 140 #if !defined(OS_WIN) |
| 143 // On Posix, we don't encounter transient errors during renames, except | 141 // On Posix, we don't encounter transient errors during renames, except |
| 144 // possibly EAGAIN, which is difficult to replicate reliably. So we resort to | 142 // possibly EAGAIN, which is difficult to replicate reliably. So we resort to |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 } | 199 } |
| 202 | 200 |
| 203 void SetInterruptReasonCallback(const base::Closure& closure, | 201 void SetInterruptReasonCallback(const base::Closure& closure, |
| 204 DownloadInterruptReason* reason_p, | 202 DownloadInterruptReason* reason_p, |
| 205 DownloadInterruptReason reason) { | 203 DownloadInterruptReason reason) { |
| 206 *reason_p = reason; | 204 *reason_p = reason; |
| 207 closure.Run(); | 205 closure.Run(); |
| 208 } | 206 } |
| 209 | 207 |
| 210 bool CreateDownloadFile(int offset, bool calculate_hash) { | 208 bool CreateDownloadFile(int offset, bool calculate_hash) { |
| 211 return CreateDownloadFile(offset, 0, calculate_hash, false, | 209 return CreateDownloadFile(offset, 0, calculate_hash, |
| 212 DownloadItem::ReceivedSlices()); | 210 DownloadItem::ReceivedSlices()); |
| 213 } | 211 } |
| 214 | 212 |
| 215 bool CreateDownloadFile(int offset, | 213 bool CreateDownloadFile(int offset, |
| 216 int length, | 214 int length, |
| 217 bool calculate_hash, | 215 bool calculate_hash, |
| 218 bool is_sparse_file, | |
| 219 const DownloadItem::ReceivedSlices& received_slices) { | 216 const DownloadItem::ReceivedSlices& received_slices) { |
| 220 // There can be only one. | 217 // There can be only one. |
| 221 DCHECK(!download_file_.get()); | 218 DCHECK(!download_file_.get()); |
| 222 | 219 |
| 223 input_stream_ = new StrictMock<MockByteStreamReader>(); | 220 input_stream_ = new StrictMock<MockByteStreamReader>(); |
| 224 | 221 |
| 225 // TODO: Need to actually create a function that'll set the variables | 222 // TODO: Need to actually create a function that'll set the variables |
| 226 // based on the inputs from the callback. | 223 // based on the inputs from the callback. |
| 227 EXPECT_CALL(*input_stream_, RegisterCallback(_)) | 224 EXPECT_CALL(*input_stream_, RegisterCallback(_)) |
| 228 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) | 225 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) |
| 229 .RetiresOnSaturation(); | 226 .RetiresOnSaturation(); |
| 230 | 227 |
| 231 std::unique_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo()); | 228 std::unique_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo()); |
| 232 save_info->offset = offset; | 229 save_info->offset = offset; |
| 233 save_info->length = length; | 230 save_info->length = length; |
| 234 | 231 |
| 235 download_file_.reset(new TestDownloadFileImpl( | 232 download_file_.reset(new TestDownloadFileImpl( |
| 236 std::move(save_info), base::FilePath(), | 233 std::move(save_info), base::FilePath(), |
| 237 std::unique_ptr<ByteStreamReader>(input_stream_), received_slices, | 234 std::unique_ptr<ByteStreamReader>(input_stream_), received_slices, |
| 238 net::NetLogWithSource(), is_sparse_file, | 235 net::NetLogWithSource(), observer_factory_.GetWeakPtr())); |
| 239 observer_factory_.GetWeakPtr())); | |
| 240 | 236 |
| 241 EXPECT_CALL(*input_stream_, Read(_, _)) | 237 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 242 .WillOnce(Return(ByteStreamReader::STREAM_EMPTY)) | 238 .WillOnce(Return(ByteStreamReader::STREAM_EMPTY)) |
| 243 .RetiresOnSaturation(); | 239 .RetiresOnSaturation(); |
| 244 | 240 |
| 245 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); | 241 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); |
| 246 DownloadInterruptReason result = DOWNLOAD_INTERRUPT_REASON_NONE; | 242 DownloadInterruptReason result = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 247 base::RunLoop loop_runner; | 243 base::RunLoop loop_runner; |
| 248 download_file_->Initialize(base::Bind( | 244 download_file_->Initialize(base::Bind( |
| 249 &DownloadFileTest::SetInterruptReasonCallback, | 245 &DownloadFileTest::SetInterruptReasonCallback, |
| (...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 DestroyDownloadFile(0); | 899 DestroyDownloadFile(0); |
| 904 } | 900 } |
| 905 | 901 |
| 906 // Tests for concurrent streams handling, used for parallel download. | 902 // Tests for concurrent streams handling, used for parallel download. |
| 907 // | 903 // |
| 908 // Activate both streams at the same time. | 904 // Activate both streams at the same time. |
| 909 TEST_F(DownloadFileTest, MutipleStreamsWrite) { | 905 TEST_F(DownloadFileTest, MutipleStreamsWrite) { |
| 910 int64_t stream_0_length = GetBuffersLength(kTestData6, 2); | 906 int64_t stream_0_length = GetBuffersLength(kTestData6, 2); |
| 911 int64_t stream_1_length = GetBuffersLength(kTestData7, 2); | 907 int64_t stream_1_length = GetBuffersLength(kTestData7, 2); |
| 912 | 908 |
| 913 ASSERT_TRUE(CreateDownloadFile(0, stream_0_length, true, true, | 909 ASSERT_TRUE(CreateDownloadFile(0, stream_0_length, true, |
| 914 DownloadItem::ReceivedSlices())); | 910 DownloadItem::ReceivedSlices())); |
| 915 | 911 |
| 916 PrepareStream(&input_stream_, 0, false, true, kTestData6, 2); | 912 PrepareStream(&input_stream_, 0, false, true, kTestData6, 2); |
| 917 PrepareStream(&additional_streams_[0], stream_0_length, true, true, | 913 PrepareStream(&additional_streams_[0], stream_0_length, true, true, |
| 918 kTestData7, 2); | 914 kTestData7, 2); |
| 919 | 915 |
| 920 EXPECT_CALL(*additional_streams_[0], RegisterCallback(_)) | 916 EXPECT_CALL(*additional_streams_[0], RegisterCallback(_)) |
| 921 .RetiresOnSaturation(); | 917 .RetiresOnSaturation(); |
| 922 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); | 918 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); |
| 923 | 919 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 942 int64_t stream_0_length = GetBuffersLength(kTestData6, 2); | 938 int64_t stream_0_length = GetBuffersLength(kTestData6, 2); |
| 943 | 939 |
| 944 // The second stream has a limited length and should be partially written | 940 // The second stream has a limited length and should be partially written |
| 945 // to disk. When we prepare the stream, we fill the stream with 2 full buffer. | 941 // to disk. When we prepare the stream, we fill the stream with 2 full buffer. |
| 946 int64_t stream_1_length = GetBuffersLength(kTestData7, 2) - 1; | 942 int64_t stream_1_length = GetBuffersLength(kTestData7, 2) - 1; |
| 947 | 943 |
| 948 // The last stream can't have length limit, it's a half open request, e.g | 944 // The last stream can't have length limit, it's a half open request, e.g |
| 949 // "Range:50-". | 945 // "Range:50-". |
| 950 int64_t stream_2_length = GetBuffersLength(kTestData6, 2); | 946 int64_t stream_2_length = GetBuffersLength(kTestData6, 2); |
| 951 | 947 |
| 952 ASSERT_TRUE(CreateDownloadFile(0, stream_0_length, true, true, | 948 ASSERT_TRUE(CreateDownloadFile(0, stream_0_length, true, |
| 953 DownloadItem::ReceivedSlices())); | 949 DownloadItem::ReceivedSlices())); |
| 954 | 950 |
| 955 PrepareStream(&input_stream_, 0, false, true, kTestData6, 2); | 951 PrepareStream(&input_stream_, 0, false, true, kTestData6, 2); |
| 956 PrepareStream(&additional_streams_[0], stream_0_length, true, false, | 952 PrepareStream(&additional_streams_[0], stream_0_length, true, false, |
| 957 kTestData7, 2); | 953 kTestData7, 2); |
| 958 PrepareStream(&additional_streams_[1], stream_0_length + stream_1_length, | 954 PrepareStream(&additional_streams_[1], stream_0_length + stream_1_length, |
| 959 true, true, kTestData6, 2); | 955 true, true, kTestData6, 2); |
| 960 | 956 |
| 961 EXPECT_CALL(*additional_streams_[0], RegisterCallback(_)) | 957 EXPECT_CALL(*additional_streams_[0], RegisterCallback(_)) |
| 962 .Times(2) | 958 .Times(2) |
| (...skipping 28 matching lines...) Expand all Loading... |
| 991 | 987 |
| 992 download_file_->Cancel(); | 988 download_file_->Cancel(); |
| 993 DestroyDownloadFile(0, false); | 989 DestroyDownloadFile(0, false); |
| 994 } | 990 } |
| 995 | 991 |
| 996 // Activate and deplete one stream, later add the second stream. | 992 // Activate and deplete one stream, later add the second stream. |
| 997 TEST_F(DownloadFileTest, MutipleStreamsFirstStreamWriteAllData) { | 993 TEST_F(DownloadFileTest, MutipleStreamsFirstStreamWriteAllData) { |
| 998 int64_t stream_0_length = GetBuffersLength(kTestData8, 4); | 994 int64_t stream_0_length = GetBuffersLength(kTestData8, 4); |
| 999 | 995 |
| 1000 ASSERT_TRUE(CreateDownloadFile(0, DownloadSaveInfo::kLengthFullContent, true, | 996 ASSERT_TRUE(CreateDownloadFile(0, DownloadSaveInfo::kLengthFullContent, true, |
| 1001 true, DownloadItem::ReceivedSlices())); | 997 DownloadItem::ReceivedSlices())); |
| 1002 | 998 |
| 1003 PrepareStream(&input_stream_, 0, false, true, kTestData8, 4); | 999 PrepareStream(&input_stream_, 0, false, true, kTestData8, 4); |
| 1004 | 1000 |
| 1005 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); | 1001 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); |
| 1006 | 1002 |
| 1007 sink_callback_.Run(); | 1003 sink_callback_.Run(); |
| 1008 base::RunLoop().RunUntilIdle(); | 1004 base::RunLoop().RunUntilIdle(); |
| 1009 | 1005 |
| 1010 // Add another stream, the file is already closed, so nothing should be | 1006 // Add another stream, the file is already closed, so nothing should be |
| 1011 // called. | 1007 // called. |
| 1012 EXPECT_FALSE(download_file_->InProgress()); | 1008 EXPECT_FALSE(download_file_->InProgress()); |
| 1013 | 1009 |
| 1014 additional_streams_[0] = new StrictMock<MockByteStreamReader>(); | 1010 additional_streams_[0] = new StrictMock<MockByteStreamReader>(); |
| 1015 download_file_->AddByteStream( | 1011 download_file_->AddByteStream( |
| 1016 std::unique_ptr<MockByteStreamReader>(additional_streams_[0]), | 1012 std::unique_ptr<MockByteStreamReader>(additional_streams_[0]), |
| 1017 stream_0_length - 1, DownloadSaveInfo::kLengthFullContent); | 1013 stream_0_length - 1, DownloadSaveInfo::kLengthFullContent); |
| 1018 base::RunLoop().RunUntilIdle(); | 1014 base::RunLoop().RunUntilIdle(); |
| 1019 | 1015 |
| 1020 SourceStreamTestData stream_data_0(0, stream_0_length, true); | 1016 SourceStreamTestData stream_data_0(0, stream_0_length, true); |
| 1021 SourceStreamTestData stream_data_1(stream_0_length - 1, 0, false); | 1017 SourceStreamTestData stream_data_1(stream_0_length - 1, 0, false); |
| 1022 VerifySourceStreamsStates(stream_data_0); | 1018 VerifySourceStreamsStates(stream_data_0); |
| 1023 VerifySourceStreamsStates(stream_data_1); | 1019 VerifySourceStreamsStates(stream_data_1); |
| 1024 EXPECT_EQ(stream_0_length, TotalBytesReceived()); | 1020 EXPECT_EQ(stream_0_length, TotalBytesReceived()); |
| 1025 | 1021 |
| 1026 DestroyDownloadFile(0); | 1022 DestroyDownloadFile(0); |
| 1027 } | 1023 } |
| 1028 | 1024 |
| 1029 } // namespace content | 1025 } // namespace content |
| OLD | NEW |