Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: content/browser/download/download_item_impl_unittest.cc

Issue 2799333002: Clear the received slices in DownloadItemImpl when etag changed. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/browser/download/download_item_impl.h" 5 #include "content/browser/download/download_item_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <deque> 9 #include <deque>
10 #include <iterator> 10 #include <iterator>
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 DownloadTargetCallback* callback) { 294 DownloadTargetCallback* callback) {
295 MockDownloadFile* mock_download_file = nullptr; 295 MockDownloadFile* mock_download_file = nullptr;
296 std::unique_ptr<DownloadFile> download_file; 296 std::unique_ptr<DownloadFile> download_file;
297 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 297 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
298 .WillOnce(SaveArg<1>(callback)); 298 .WillOnce(SaveArg<1>(callback));
299 299
300 // Only create a DownloadFile if the request was successful. 300 // Only create a DownloadFile if the request was successful.
301 if (create_info_->result == DOWNLOAD_INTERRUPT_REASON_NONE) { 301 if (create_info_->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
302 mock_download_file = new StrictMock<MockDownloadFile>; 302 mock_download_file = new StrictMock<MockDownloadFile>;
303 download_file.reset(mock_download_file); 303 download_file.reset(mock_download_file);
304 EXPECT_CALL(*mock_download_file, Initialize(_)) 304 EXPECT_CALL(*mock_download_file, Initialize(_, _))
305 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE)); 305 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE));
306 EXPECT_CALL(*mock_download_file, FullPath()) 306 EXPECT_CALL(*mock_download_file, FullPath())
307 .WillRepeatedly(ReturnRefOfCopy(base::FilePath())); 307 .WillRepeatedly(ReturnRefOfCopy(base::FilePath()));
308 } 308 }
309 309
310 std::unique_ptr<MockRequestHandle> request_handle = 310 std::unique_ptr<MockRequestHandle> request_handle =
311 base::MakeUnique<NiceMock<MockRequestHandle>>(); 311 base::MakeUnique<NiceMock<MockRequestHandle>>();
312 item->Start(std::move(download_file), std::move(request_handle), 312 item->Start(std::move(download_file), std::move(request_handle),
313 *create_info_); 313 *create_info_);
314 RunAllPendingInMessageLoops(); 314 RunAllPendingInMessageLoops();
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 } 586 }
587 587
588 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { 588 TEST_F(DownloadItemTest, NotificationAfterTogglePause) {
589 DownloadItemImpl* item = CreateDownloadItem(); 589 DownloadItemImpl* item = CreateDownloadItem();
590 TestDownloadItemObserver observer(item); 590 TestDownloadItemObserver observer(item);
591 MockDownloadFile* mock_download_file(new MockDownloadFile); 591 MockDownloadFile* mock_download_file(new MockDownloadFile);
592 std::unique_ptr<DownloadFile> download_file(mock_download_file); 592 std::unique_ptr<DownloadFile> download_file(mock_download_file);
593 std::unique_ptr<DownloadRequestHandleInterface> request_handle( 593 std::unique_ptr<DownloadRequestHandleInterface> request_handle(
594 new NiceMock<MockRequestHandle>); 594 new NiceMock<MockRequestHandle>);
595 595
596 EXPECT_CALL(*mock_download_file, Initialize(_)); 596 EXPECT_CALL(*mock_download_file, Initialize(_, _));
597 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)); 597 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _));
598 item->Start(std::move(download_file), std::move(request_handle), 598 item->Start(std::move(download_file), std::move(request_handle),
599 *create_info()); 599 *create_info());
600 600
601 item->Pause(); 601 item->Pause();
602 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 602 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
603 603
604 ASSERT_TRUE(item->IsPaused()); 604 ASSERT_TRUE(item->IsPaused());
605 605
606 item->Resume(); 606 item->Resume();
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); 884 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime());
885 EXPECT_EQ(kFirstURL, item->GetURL().spec()); 885 EXPECT_EQ(kFirstURL, item->GetURL().spec());
886 EXPECT_EQ(kMimeType, item->GetMimeType()); 886 EXPECT_EQ(kMimeType, item->GetMimeType());
887 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 887 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
888 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); 888 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason());
889 EXPECT_EQ(kDummyIntermediatePath, item->GetFullPath().value()); 889 EXPECT_EQ(kDummyIntermediatePath, item->GetFullPath().value());
890 EXPECT_EQ(1, item->GetReceivedBytes()); 890 EXPECT_EQ(1, item->GetReceivedBytes());
891 } 891 }
892 892
893 // If the download resumption request succeeds, the origin state should be 893 // If the download resumption request succeeds, the origin state should be
894 // updated 894 // updated.
895 TEST_F(DownloadItemTest, SucceededResumptionUpdatesOriginState) { 895 TEST_F(DownloadItemTest, SucceededResumptionUpdatesOriginState) {
896 const char kContentDisposition[] = "attachment; filename=foo"; 896 const char kContentDisposition[] = "attachment; filename=foo";
897 const char kFirstETag[] = "ABC"; 897 const char kFirstETag[] = "ABC";
898 const char kFirstLastModified[] = "Yesterday"; 898 const char kFirstLastModified[] = "Yesterday";
899 const char kFirstURL[] = "http://www.example.com/download"; 899 const char kFirstURL[] = "http://www.example.com/download";
900 const char kMimeType[] = "text/css"; 900 const char kMimeType[] = "text/css";
901 create_info()->content_disposition = kContentDisposition; 901 create_info()->content_disposition = kContentDisposition;
902 create_info()->etag = kFirstETag; 902 create_info()->etag = kFirstETag;
903 create_info()->last_modified = kFirstLastModified; 903 create_info()->last_modified = kFirstLastModified;
904 create_info()->url_chain.push_back(GURL(kFirstURL)); 904 create_info()->url_chain.push_back(GURL(kFirstURL));
(...skipping 29 matching lines...) Expand all
934 934
935 EXPECT_EQ(kSecondContentDisposition, item->GetContentDisposition()); 935 EXPECT_EQ(kSecondContentDisposition, item->GetContentDisposition());
936 EXPECT_EQ(kSecondETag, item->GetETag()); 936 EXPECT_EQ(kSecondETag, item->GetETag());
937 EXPECT_EQ(kSecondLastModified, item->GetLastModifiedTime()); 937 EXPECT_EQ(kSecondLastModified, item->GetLastModifiedTime());
938 EXPECT_EQ(kSecondURL, item->GetURL().spec()); 938 EXPECT_EQ(kSecondURL, item->GetURL().spec());
939 EXPECT_EQ(kSecondMimeType, item->GetMimeType()); 939 EXPECT_EQ(kSecondMimeType, item->GetMimeType());
940 940
941 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 941 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
942 } 942 }
943 943
944 // Ensure when strong validators changed on resumption, the received
945 // slices should be cleared.
946 TEST_F(DownloadItemTest, ClearReceivedSliceIfEtagChanged) {
947 const char kFirstETag[] = "ABC";
948 const char kSecondETag[] = "123";
949 const DownloadItem::ReceivedSlices kReceivedSlice = {
950 DownloadItem::ReceivedSlice(0, 10)};
951 create_info()->etag = kFirstETag;
952
953 DownloadItemImpl* item = CreateDownloadItem();
954 MockDownloadFile* download_file =
955 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
956
957 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _));
958 EXPECT_CALL(*download_file, Detach());
959
960 item->DestinationObserverAsWeakPtr()->DestinationUpdate(10, 100,
961 kReceivedSlice);
962 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices());
963
964 item->DestinationObserverAsWeakPtr()->DestinationError(
965 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0,
966 std::unique_ptr<crypto::SecureHash>());
967 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices());
968
969 RunAllPendingInMessageLoops();
970
971 // Change the strong validator and resume the download, the received slices
972 // should be cleared.
973 create_info()->etag = kSecondETag;
974 DownloadTargetCallback target_callback;
975 download_file = CallDownloadItemStart(item, &target_callback);
976 EXPECT_TRUE(item->GetReceivedSlices().empty());
977
978 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
979 }
980
944 // Test that resumption uses the final URL in a URL chain when resuming. 981 // Test that resumption uses the final URL in a URL chain when resuming.
945 TEST_F(DownloadItemTest, ResumeUsesFinalURL) { 982 TEST_F(DownloadItemTest, ResumeUsesFinalURL) {
946 create_info()->save_info->prompt_for_save_location = false; 983 create_info()->save_info->prompt_for_save_location = false;
947 create_info()->url_chain.clear(); 984 create_info()->url_chain.clear();
948 create_info()->url_chain.push_back(GURL("http://example.com/a")); 985 create_info()->url_chain.push_back(GURL("http://example.com/a"));
949 create_info()->url_chain.push_back(GURL("http://example.com/b")); 986 create_info()->url_chain.push_back(GURL("http://example.com/b"));
950 create_info()->url_chain.push_back(GURL("http://example.com/c")); 987 create_info()->url_chain.push_back(GURL("http://example.com/c"));
951 988
952 DownloadItemImpl* item = CreateDownloadItem(); 989 DownloadItemImpl* item = CreateDownloadItem();
953 TestDownloadItemObserver observer(item); 990 TestDownloadItemObserver observer(item);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1000 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), 1037 EXPECT_EQ(FILE_PATH_LITERAL("new.name"),
1001 item->GetFileNameToReportUser().value()); 1038 item->GetFileNameToReportUser().value());
1002 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 1039 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
1003 } 1040 }
1004 1041
1005 // Test to make sure that Start method calls DF initialize properly. 1042 // Test to make sure that Start method calls DF initialize properly.
1006 TEST_F(DownloadItemTest, Start) { 1043 TEST_F(DownloadItemTest, Start) {
1007 MockDownloadFile* mock_download_file(new MockDownloadFile); 1044 MockDownloadFile* mock_download_file(new MockDownloadFile);
1008 std::unique_ptr<DownloadFile> download_file(mock_download_file); 1045 std::unique_ptr<DownloadFile> download_file(mock_download_file);
1009 DownloadItemImpl* item = CreateDownloadItem(); 1046 DownloadItemImpl* item = CreateDownloadItem();
1010 EXPECT_CALL(*mock_download_file, Initialize(_)); 1047 EXPECT_CALL(*mock_download_file, Initialize(_, _));
1011 std::unique_ptr<DownloadRequestHandleInterface> request_handle( 1048 std::unique_ptr<DownloadRequestHandleInterface> request_handle(
1012 new NiceMock<MockRequestHandle>); 1049 new NiceMock<MockRequestHandle>);
1013 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); 1050 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _));
1014 item->Start(std::move(download_file), std::move(request_handle), 1051 item->Start(std::move(download_file), std::move(request_handle),
1015 *create_info()); 1052 *create_info());
1016 RunAllPendingInMessageLoops(); 1053 RunAllPendingInMessageLoops();
1017 1054
1018 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); 1055 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS);
1019 } 1056 }
1020 1057
1021 // Download file and the request should be cancelled as a result of download 1058 // Download file and the request should be cancelled as a result of download
1022 // file initialization failing. 1059 // file initialization failing.
1023 TEST_F(DownloadItemTest, InitDownloadFileFails) { 1060 TEST_F(DownloadItemTest, InitDownloadFileFails) {
1024 DownloadItemImpl* item = CreateDownloadItem(); 1061 DownloadItemImpl* item = CreateDownloadItem();
1025 std::unique_ptr<MockDownloadFile> file = base::MakeUnique<MockDownloadFile>(); 1062 std::unique_ptr<MockDownloadFile> file = base::MakeUnique<MockDownloadFile>();
1026 std::unique_ptr<MockRequestHandle> request_handle = 1063 std::unique_ptr<MockRequestHandle> request_handle =
1027 base::MakeUnique<MockRequestHandle>(); 1064 base::MakeUnique<MockRequestHandle>();
1028 1065
1029 EXPECT_CALL(*file, Cancel()); 1066 EXPECT_CALL(*file, Cancel());
1030 EXPECT_CALL(*request_handle, CancelRequest()); 1067 EXPECT_CALL(*request_handle, CancelRequest());
1031 EXPECT_CALL(*file, Initialize(_)) 1068 EXPECT_CALL(*file, Initialize(_, _))
1032 .WillOnce(ScheduleCallbackWithParam( 1069 .WillOnce(ScheduleCallbackWithParam(
1033 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED)); 1070 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED));
1034 1071
1035 base::RunLoop start_download_loop; 1072 base::RunLoop start_download_loop;
1036 DownloadTargetCallback download_target_callback; 1073 DownloadTargetCallback download_target_callback;
1037 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 1074 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
1038 .WillOnce(DoAll(SaveArg<1>(&download_target_callback), 1075 .WillOnce(DoAll(SaveArg<1>(&download_target_callback),
1039 ScheduleClosure(start_download_loop.QuitClosure()))); 1076 ScheduleClosure(start_download_loop.QuitClosure())));
1040 1077
1041 item->Start(std::move(file), std::move(request_handle), *create_info()); 1078 item->Start(std::move(file), std::move(request_handle), *create_info());
(...skipping 1001 matching lines...) Expand 10 before | Expand all | Expand 10 after
2043 // Run through the DII workflow but the embedder cancels the download at target 2080 // Run through the DII workflow but the embedder cancels the download at target
2044 // determination. 2081 // determination.
2045 TEST_P(DownloadItemDestinationUpdateRaceTest, DownloadCancelledByUser) { 2082 TEST_P(DownloadItemDestinationUpdateRaceTest, DownloadCancelledByUser) {
2046 // Expect that the download file and the request will be cancelled as a 2083 // Expect that the download file and the request will be cancelled as a
2047 // result. 2084 // result.
2048 EXPECT_CALL(*file_, Cancel()); 2085 EXPECT_CALL(*file_, Cancel());
2049 EXPECT_CALL(*request_handle_, CancelRequest()); 2086 EXPECT_CALL(*request_handle_, CancelRequest());
2050 2087
2051 base::RunLoop download_start_loop; 2088 base::RunLoop download_start_loop;
2052 DownloadFile::InitializeCallback initialize_callback; 2089 DownloadFile::InitializeCallback initialize_callback;
2053 EXPECT_CALL(*file_, Initialize(_)) 2090 EXPECT_CALL(*file_, Initialize(_, _))
2054 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), 2091 .WillOnce(DoAll(SaveArg<0>(&initialize_callback),
2055 ScheduleClosure(download_start_loop.QuitClosure()))); 2092 ScheduleClosure(download_start_loop.QuitClosure())));
2056 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); 2093 item_->Start(std::move(file_), std::move(request_handle_), *create_info());
2057 download_start_loop.Run(); 2094 download_start_loop.Run();
2058 2095
2059 base::WeakPtr<DownloadDestinationObserver> destination_observer = 2096 base::WeakPtr<DownloadDestinationObserver> destination_observer =
2060 item_->DestinationObserverAsWeakPtr(); 2097 item_->DestinationObserverAsWeakPtr();
2061 2098
2062 ScheduleObservations(PreInitializeFileObservations(), destination_observer); 2099 ScheduleObservations(PreInitializeFileObservations(), destination_observer);
2063 RunAllPendingInMessageLoops(); 2100 RunAllPendingInMessageLoops();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2096 // DownloadFile expectations since we are about to transfer its ownership to 2133 // DownloadFile expectations since we are about to transfer its ownership to
2097 // the DownloadItem. 2134 // the DownloadItem.
2098 base::RunLoop intermediate_rename_loop; 2135 base::RunLoop intermediate_rename_loop;
2099 DownloadFile::RenameCompletionCallback intermediate_rename_callback; 2136 DownloadFile::RenameCompletionCallback intermediate_rename_callback;
2100 EXPECT_CALL(*file_, RenameAndUniquify(_, _)) 2137 EXPECT_CALL(*file_, RenameAndUniquify(_, _))
2101 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback), 2138 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback),
2102 ScheduleClosure(intermediate_rename_loop.QuitClosure()))); 2139 ScheduleClosure(intermediate_rename_loop.QuitClosure())));
2103 2140
2104 base::RunLoop download_start_loop; 2141 base::RunLoop download_start_loop;
2105 DownloadFile::InitializeCallback initialize_callback; 2142 DownloadFile::InitializeCallback initialize_callback;
2106 EXPECT_CALL(*file_, Initialize(_)) 2143 EXPECT_CALL(*file_, Initialize(_, _))
2107 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), 2144 .WillOnce(DoAll(SaveArg<0>(&initialize_callback),
2108 ScheduleClosure(download_start_loop.QuitClosure()))); 2145 ScheduleClosure(download_start_loop.QuitClosure())));
2109 2146
2110 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); 2147 item_->Start(std::move(file_), std::move(request_handle_), *create_info());
2111 download_start_loop.Run(); 2148 download_start_loop.Run();
2112 base::WeakPtr<DownloadDestinationObserver> destination_observer = 2149 base::WeakPtr<DownloadDestinationObserver> destination_observer =
2113 item_->DestinationObserverAsWeakPtr(); 2150 item_->DestinationObserverAsWeakPtr();
2114 2151
2115 ScheduleObservations(PreInitializeFileObservations(), destination_observer); 2152 ScheduleObservations(PreInitializeFileObservations(), destination_observer);
2116 RunAllPendingInMessageLoops(); 2153 RunAllPendingInMessageLoops();
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
2166 // DownloadFile expectations since we are about to transfer its ownership to 2203 // DownloadFile expectations since we are about to transfer its ownership to
2167 // the DownloadItem. 2204 // the DownloadItem.
2168 base::RunLoop intermediate_rename_loop; 2205 base::RunLoop intermediate_rename_loop;
2169 DownloadFile::RenameCompletionCallback intermediate_rename_callback; 2206 DownloadFile::RenameCompletionCallback intermediate_rename_callback;
2170 EXPECT_CALL(*file_, RenameAndUniquify(_, _)) 2207 EXPECT_CALL(*file_, RenameAndUniquify(_, _))
2171 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback), 2208 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback),
2172 ScheduleClosure(intermediate_rename_loop.QuitClosure()))); 2209 ScheduleClosure(intermediate_rename_loop.QuitClosure())));
2173 2210
2174 base::RunLoop download_start_loop; 2211 base::RunLoop download_start_loop;
2175 DownloadFile::InitializeCallback initialize_callback; 2212 DownloadFile::InitializeCallback initialize_callback;
2176 EXPECT_CALL(*file_, Initialize(_)) 2213 EXPECT_CALL(*file_, Initialize(_, _))
2177 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), 2214 .WillOnce(DoAll(SaveArg<0>(&initialize_callback),
2178 ScheduleClosure(download_start_loop.QuitClosure()))); 2215 ScheduleClosure(download_start_loop.QuitClosure())));
2179 2216
2180 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); 2217 item_->Start(std::move(file_), std::move(request_handle_), *create_info());
2181 download_start_loop.Run(); 2218 download_start_loop.Run();
2182 base::WeakPtr<DownloadDestinationObserver> destination_observer = 2219 base::WeakPtr<DownloadDestinationObserver> destination_observer =
2183 item_->DestinationObserverAsWeakPtr(); 2220 item_->DestinationObserverAsWeakPtr();
2184 2221
2185 ScheduleObservations(PreInitializeFileObservations(), destination_observer); 2222 ScheduleObservations(PreInitializeFileObservations(), destination_observer);
2186 RunAllPendingInMessageLoops(); 2223 RunAllPendingInMessageLoops();
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
2232 2269
2233 item_->Cancel(true); 2270 item_->Cancel(true);
2234 RunAllPendingInMessageLoops(); 2271 RunAllPendingInMessageLoops();
2235 } 2272 }
2236 2273
2237 TEST(MockDownloadItem, Compiles) { 2274 TEST(MockDownloadItem, Compiles) {
2238 MockDownloadItem mock_item; 2275 MockDownloadItem mock_item;
2239 } 2276 }
2240 2277
2241 } // namespace content 2278 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698