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

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

Issue 1751603002: [Downloads] Rework how hashes are calculated for download files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address first round of comments Created 4 years, 9 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 // 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>
(...skipping 13 matching lines...) Expand all
24 #include "base/time/time.h" 24 #include "base/time/time.h"
25 #include "build/build_config.h" 25 #include "build/build_config.h"
26 #include "content/browser/byte_stream.h" 26 #include "content/browser/byte_stream.h"
27 #include "content/browser/download/download_file_factory.h" 27 #include "content/browser/download/download_file_factory.h"
28 #include "content/browser/download/download_file_impl.h" 28 #include "content/browser/download/download_file_impl.h"
29 #include "content/browser/download/download_item_impl.h" 29 #include "content/browser/download/download_item_impl.h"
30 #include "content/browser/download/download_manager_impl.h" 30 #include "content/browser/download/download_manager_impl.h"
31 #include "content/browser/download/download_resource_handler.h" 31 #include "content/browser/download/download_resource_handler.h"
32 #include "content/browser/loader/resource_dispatcher_host_impl.h" 32 #include "content/browser/loader/resource_dispatcher_host_impl.h"
33 #include "content/browser/web_contents/web_contents_impl.h" 33 #include "content/browser/web_contents/web_contents_impl.h"
34 #include "content/public/browser/download_danger_type.h"
34 #include "content/public/browser/power_save_blocker.h" 35 #include "content/public/browser/power_save_blocker.h"
35 #include "content/public/browser/resource_dispatcher_host_delegate.h" 36 #include "content/public/browser/resource_dispatcher_host_delegate.h"
36 #include "content/public/browser/resource_throttle.h" 37 #include "content/public/browser/resource_throttle.h"
37 #include "content/public/common/content_features.h" 38 #include "content/public/common/content_features.h"
38 #include "content/public/common/webplugininfo.h" 39 #include "content/public/common/webplugininfo.h"
39 #include "content/public/test/browser_test_utils.h" 40 #include "content/public/test/browser_test_utils.h"
40 #include "content/public/test/content_browser_test.h" 41 #include "content/public/test/content_browser_test.h"
41 #include "content/public/test/content_browser_test_utils.h" 42 #include "content/public/test/content_browser_test_utils.h"
42 #include "content/public/test/download_test_observer.h" 43 #include "content/public/test/download_test_observer.h"
43 #include "content/public/test/test_download_request_handler.h" 44 #include "content/public/test/test_download_request_handler.h"
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 static DownloadManagerImpl* DownloadManagerForShell(Shell* shell) { 115 static DownloadManagerImpl* DownloadManagerForShell(Shell* shell) {
115 // We're in a content_browsertest; we know that the DownloadManager 116 // We're in a content_browsertest; we know that the DownloadManager
116 // is a DownloadManagerImpl. 117 // is a DownloadManagerImpl.
117 return static_cast<DownloadManagerImpl*>( 118 return static_cast<DownloadManagerImpl*>(
118 BrowserContext::GetDownloadManager( 119 BrowserContext::GetDownloadManager(
119 shell->web_contents()->GetBrowserContext())); 120 shell->web_contents()->GetBrowserContext()));
120 } 121 }
121 122
122 class DownloadFileWithDelay : public DownloadFileImpl { 123 class DownloadFileWithDelay : public DownloadFileImpl {
123 public: 124 public:
124 DownloadFileWithDelay(const DownloadSaveInfo& save_info, 125 DownloadFileWithDelay(scoped_ptr<DownloadSaveInfo> save_info,
125 const base::FilePath& default_download_directory, 126 const base::FilePath& default_download_directory,
126 const GURL& url,
127 const GURL& referrer_url,
128 bool calculate_hash,
129 base::File file,
130 scoped_ptr<ByteStreamReader> stream, 127 scoped_ptr<ByteStreamReader> stream,
131 const net::BoundNetLog& bound_net_log, 128 const net::BoundNetLog& bound_net_log,
132 scoped_ptr<PowerSaveBlocker> power_save_blocker, 129 scoped_ptr<PowerSaveBlocker> power_save_blocker,
133 base::WeakPtr<DownloadDestinationObserver> observer, 130 base::WeakPtr<DownloadDestinationObserver> observer,
134 base::WeakPtr<DownloadFileWithDelayFactory> owner); 131 base::WeakPtr<DownloadFileWithDelayFactory> owner);
135 132
136 ~DownloadFileWithDelay() override; 133 ~DownloadFileWithDelay() override;
137 134
138 // Wraps DownloadFileImpl::Rename* and intercepts the return callback, 135 // Wraps DownloadFileImpl::Rename* and intercepts the return callback,
139 // storing it in the factory that produced this object for later 136 // storing it in the factory that produced this object for later
140 // retrieval. 137 // retrieval.
141 void RenameAndUniquify(const base::FilePath& full_path, 138 void RenameAndUniquify(const base::FilePath& full_path,
142 const RenameCompletionCallback& callback) override; 139 const RenameCompletionCallback& callback) override;
143 void RenameAndAnnotate(const base::FilePath& full_path, 140 void RenameAndAnnotate(const base::FilePath& full_path,
141 const std::string& client_guid,
142 const GURL& source_url,
143 const GURL& referrer_url,
144 const RenameCompletionCallback& callback) override; 144 const RenameCompletionCallback& callback) override;
145 145
146 private: 146 private:
147 static void RenameCallbackWrapper( 147 static void RenameCallbackWrapper(
148 const base::WeakPtr<DownloadFileWithDelayFactory>& factory, 148 const base::WeakPtr<DownloadFileWithDelayFactory>& factory,
149 const RenameCompletionCallback& original_callback, 149 const RenameCompletionCallback& original_callback,
150 DownloadInterruptReason reason, 150 DownloadInterruptReason reason,
151 const base::FilePath& path); 151 const base::FilePath& path);
152 152
153 // This variable may only be read on the FILE thread, and may only be 153 // This variable may only be read on the FILE thread, and may only be
154 // indirected through (e.g. methods on DownloadFileWithDelayFactory called) 154 // indirected through (e.g. methods on DownloadFileWithDelayFactory called)
155 // on the UI thread. This is because after construction, 155 // on the UI thread. This is because after construction,
156 // DownloadFileWithDelay lives on the file thread, but 156 // DownloadFileWithDelay lives on the file thread, but
157 // DownloadFileWithDelayFactory is purely a UI thread object. 157 // DownloadFileWithDelayFactory is purely a UI thread object.
158 base::WeakPtr<DownloadFileWithDelayFactory> owner_; 158 base::WeakPtr<DownloadFileWithDelayFactory> owner_;
159 159
160 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); 160 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay);
161 }; 161 };
162 162
163 // All routines on this class must be called on the UI thread. 163 // All routines on this class must be called on the UI thread.
164 class DownloadFileWithDelayFactory : public DownloadFileFactory { 164 class DownloadFileWithDelayFactory : public DownloadFileFactory {
165 public: 165 public:
166 DownloadFileWithDelayFactory(); 166 DownloadFileWithDelayFactory();
167 ~DownloadFileWithDelayFactory() override; 167 ~DownloadFileWithDelayFactory() override;
168 168
169 // DownloadFileFactory interface. 169 // DownloadFileFactory interface.
170 DownloadFile* CreateFile( 170 DownloadFile* CreateFile(
171 const DownloadSaveInfo& save_info, 171 scoped_ptr<DownloadSaveInfo> save_info,
172 const base::FilePath& default_download_directory, 172 const base::FilePath& default_download_directory,
173 const GURL& url,
174 const GURL& referrer_url,
175 bool calculate_hash,
176 base::File file,
177 scoped_ptr<ByteStreamReader> stream, 173 scoped_ptr<ByteStreamReader> stream,
178 const net::BoundNetLog& bound_net_log, 174 const net::BoundNetLog& bound_net_log,
179 base::WeakPtr<DownloadDestinationObserver> observer) override; 175 base::WeakPtr<DownloadDestinationObserver> observer) override;
180 176
181 void AddRenameCallback(base::Closure callback); 177 void AddRenameCallback(base::Closure callback);
182 void GetAllRenameCallbacks(std::vector<base::Closure>* results); 178 void GetAllRenameCallbacks(std::vector<base::Closure>* results);
183 179
184 // Do not return until GetAllRenameCallbacks() will return a non-empty list. 180 // Do not return until GetAllRenameCallbacks() will return a non-empty list.
185 void WaitForSomeCallback(); 181 void WaitForSomeCallback();
186 182
187 private: 183 private:
188 std::vector<base::Closure> rename_callbacks_; 184 std::vector<base::Closure> rename_callbacks_;
189 bool waiting_; 185 bool waiting_;
190 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_; 186 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_;
191 187
192 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); 188 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory);
193 }; 189 };
194 190
195 DownloadFileWithDelay::DownloadFileWithDelay( 191 DownloadFileWithDelay::DownloadFileWithDelay(
196 const DownloadSaveInfo& save_info, 192 scoped_ptr<DownloadSaveInfo> save_info,
197 const base::FilePath& default_download_directory, 193 const base::FilePath& default_download_directory,
198 const GURL& url,
199 const GURL& referrer_url,
200 bool calculate_hash,
201 base::File file,
202 scoped_ptr<ByteStreamReader> stream, 194 scoped_ptr<ByteStreamReader> stream,
203 const net::BoundNetLog& bound_net_log, 195 const net::BoundNetLog& bound_net_log,
204 scoped_ptr<PowerSaveBlocker> power_save_blocker, 196 scoped_ptr<PowerSaveBlocker> power_save_blocker,
205 base::WeakPtr<DownloadDestinationObserver> observer, 197 base::WeakPtr<DownloadDestinationObserver> observer,
206 base::WeakPtr<DownloadFileWithDelayFactory> owner) 198 base::WeakPtr<DownloadFileWithDelayFactory> owner)
207 : DownloadFileImpl(save_info, 199 : DownloadFileImpl(std::move(save_info),
208 default_download_directory, 200 default_download_directory,
209 url,
210 referrer_url,
211 calculate_hash,
212 std::move(file),
213 std::move(stream), 201 std::move(stream),
214 bound_net_log, 202 bound_net_log,
215 observer), 203 observer),
216 owner_(owner) {} 204 owner_(owner) {}
217 205
218 DownloadFileWithDelay::~DownloadFileWithDelay() {} 206 DownloadFileWithDelay::~DownloadFileWithDelay() {}
219 207
220 void DownloadFileWithDelay::RenameAndUniquify( 208 void DownloadFileWithDelay::RenameAndUniquify(
221 const base::FilePath& full_path, 209 const base::FilePath& full_path,
222 const RenameCompletionCallback& callback) { 210 const RenameCompletionCallback& callback) {
223 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 211 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
224 DownloadFileImpl::RenameAndUniquify( 212 DownloadFileImpl::RenameAndUniquify(
225 full_path, base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, 213 full_path, base::Bind(DownloadFileWithDelay::RenameCallbackWrapper,
226 owner_, callback)); 214 owner_, callback));
227 } 215 }
228 216
229 void DownloadFileWithDelay::RenameAndAnnotate( 217 void DownloadFileWithDelay::RenameAndAnnotate(
230 const base::FilePath& full_path, const RenameCompletionCallback& callback) { 218 const base::FilePath& full_path,
219 const std::string& client_guid,
220 const GURL& source_url,
221 const GURL& referrer_url,
222 const RenameCompletionCallback& callback) {
231 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 223 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
232 DownloadFileImpl::RenameAndAnnotate( 224 DownloadFileImpl::RenameAndAnnotate(
233 full_path, base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, 225 full_path, client_guid, source_url, referrer_url,
234 owner_, callback)); 226 base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, owner_,
227 callback));
235 } 228 }
236 229
237 // static 230 // static
238 void DownloadFileWithDelay::RenameCallbackWrapper( 231 void DownloadFileWithDelay::RenameCallbackWrapper(
239 const base::WeakPtr<DownloadFileWithDelayFactory>& factory, 232 const base::WeakPtr<DownloadFileWithDelayFactory>& factory,
240 const RenameCompletionCallback& original_callback, 233 const RenameCompletionCallback& original_callback,
241 DownloadInterruptReason reason, 234 DownloadInterruptReason reason,
242 const base::FilePath& path) { 235 const base::FilePath& path) {
243 DCHECK_CURRENTLY_ON(BrowserThread::UI); 236 DCHECK_CURRENTLY_ON(BrowserThread::UI);
244 if (!factory) 237 if (!factory)
245 return; 238 return;
246 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); 239 factory->AddRenameCallback(base::Bind(original_callback, reason, path));
247 } 240 }
248 241
249 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() 242 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory()
250 : waiting_(false), 243 : waiting_(false),
251 weak_ptr_factory_(this) {} 244 weak_ptr_factory_(this) {}
252 245
253 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} 246 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {}
254 247
255 DownloadFile* DownloadFileWithDelayFactory::CreateFile( 248 DownloadFile* DownloadFileWithDelayFactory::CreateFile(
256 const DownloadSaveInfo& save_info, 249 scoped_ptr<DownloadSaveInfo> save_info,
257 const base::FilePath& default_download_directory, 250 const base::FilePath& default_download_directory,
258 const GURL& url,
259 const GURL& referrer_url,
260 bool calculate_hash,
261 base::File file,
262 scoped_ptr<ByteStreamReader> stream, 251 scoped_ptr<ByteStreamReader> stream,
263 const net::BoundNetLog& bound_net_log, 252 const net::BoundNetLog& bound_net_log,
264 base::WeakPtr<DownloadDestinationObserver> observer) { 253 base::WeakPtr<DownloadDestinationObserver> observer) {
265 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create( 254 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create(
266 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 255 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
267 PowerSaveBlocker::kReasonOther, "Download in progress")); 256 PowerSaveBlocker::kReasonOther, "Download in progress"));
268 return new DownloadFileWithDelay( 257 return new DownloadFileWithDelay(
269 save_info, default_download_directory, url, referrer_url, calculate_hash, 258 std::move(save_info), default_download_directory, std::move(stream),
270 std::move(file), std::move(stream), bound_net_log, std::move(psb), 259 bound_net_log, std::move(psb), observer, weak_ptr_factory_.GetWeakPtr());
271 observer, weak_ptr_factory_.GetWeakPtr());
272 } 260 }
273 261
274 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { 262 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) {
275 DCHECK_CURRENTLY_ON(BrowserThread::UI); 263 DCHECK_CURRENTLY_ON(BrowserThread::UI);
276 rename_callbacks_.push_back(callback); 264 rename_callbacks_.push_back(callback);
277 if (waiting_) 265 if (waiting_)
278 base::MessageLoopForUI::current()->QuitWhenIdle(); 266 base::MessageLoopForUI::current()->QuitWhenIdle();
279 } 267 }
280 268
281 void DownloadFileWithDelayFactory::GetAllRenameCallbacks( 269 void DownloadFileWithDelayFactory::GetAllRenameCallbacks(
282 std::vector<base::Closure>* results) { 270 std::vector<base::Closure>* results) {
283 DCHECK_CURRENTLY_ON(BrowserThread::UI); 271 DCHECK_CURRENTLY_ON(BrowserThread::UI);
284 results->swap(rename_callbacks_); 272 results->swap(rename_callbacks_);
285 } 273 }
286 274
287 void DownloadFileWithDelayFactory::WaitForSomeCallback() { 275 void DownloadFileWithDelayFactory::WaitForSomeCallback() {
288 DCHECK_CURRENTLY_ON(BrowserThread::UI); 276 DCHECK_CURRENTLY_ON(BrowserThread::UI);
289 277
290 if (rename_callbacks_.empty()) { 278 if (rename_callbacks_.empty()) {
291 waiting_ = true; 279 waiting_ = true;
292 RunMessageLoop(); 280 RunMessageLoop();
293 waiting_ = false; 281 waiting_ = false;
294 } 282 }
295 } 283 }
296 284
297 class CountingDownloadFile : public DownloadFileImpl { 285 class CountingDownloadFile : public DownloadFileImpl {
298 public: 286 public:
299 CountingDownloadFile(const DownloadSaveInfo& save_info, 287 CountingDownloadFile(scoped_ptr<DownloadSaveInfo> save_info,
300 const base::FilePath& default_downloads_directory, 288 const base::FilePath& default_downloads_directory,
301 const GURL& url,
302 const GURL& referrer_url,
303 bool calculate_hash,
304 base::File file,
305 scoped_ptr<ByteStreamReader> stream, 289 scoped_ptr<ByteStreamReader> stream,
306 const net::BoundNetLog& bound_net_log, 290 const net::BoundNetLog& bound_net_log,
307 scoped_ptr<PowerSaveBlocker> power_save_blocker, 291 scoped_ptr<PowerSaveBlocker> power_save_blocker,
308 base::WeakPtr<DownloadDestinationObserver> observer) 292 base::WeakPtr<DownloadDestinationObserver> observer)
309 : DownloadFileImpl(save_info, 293 : DownloadFileImpl(std::move(save_info),
310 default_downloads_directory, 294 default_downloads_directory,
311 url,
312 referrer_url,
313 calculate_hash,
314 std::move(file),
315 std::move(stream), 295 std::move(stream),
316 bound_net_log, 296 bound_net_log,
317 observer) {} 297 observer) {}
318 298
319 ~CountingDownloadFile() override { 299 ~CountingDownloadFile() override {
320 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 300 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
321 active_files_--; 301 active_files_--;
322 } 302 }
323 303
324 void Initialize(const InitializeCallback& callback) override { 304 void Initialize(const InitializeCallback& callback) override {
(...skipping 26 matching lines...) Expand all
351 331
352 int CountingDownloadFile::active_files_ = 0; 332 int CountingDownloadFile::active_files_ = 0;
353 333
354 class CountingDownloadFileFactory : public DownloadFileFactory { 334 class CountingDownloadFileFactory : public DownloadFileFactory {
355 public: 335 public:
356 CountingDownloadFileFactory() {} 336 CountingDownloadFileFactory() {}
357 ~CountingDownloadFileFactory() override {} 337 ~CountingDownloadFileFactory() override {}
358 338
359 // DownloadFileFactory interface. 339 // DownloadFileFactory interface.
360 DownloadFile* CreateFile( 340 DownloadFile* CreateFile(
361 const DownloadSaveInfo& save_info, 341 scoped_ptr<DownloadSaveInfo> save_info,
362 const base::FilePath& default_downloads_directory, 342 const base::FilePath& default_downloads_directory,
363 const GURL& url,
364 const GURL& referrer_url,
365 bool calculate_hash,
366 base::File file,
367 scoped_ptr<ByteStreamReader> stream, 343 scoped_ptr<ByteStreamReader> stream,
368 const net::BoundNetLog& bound_net_log, 344 const net::BoundNetLog& bound_net_log,
369 base::WeakPtr<DownloadDestinationObserver> observer) override { 345 base::WeakPtr<DownloadDestinationObserver> observer) override {
370 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create( 346 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create(
371 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 347 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
372 PowerSaveBlocker::kReasonOther, "Download in progress")); 348 PowerSaveBlocker::kReasonOther, "Download in progress"));
373 return new CountingDownloadFile(save_info, default_downloads_directory, url, 349 return new CountingDownloadFile(
374 referrer_url, calculate_hash, 350 std::move(save_info), default_downloads_directory, std::move(stream),
375 std::move(file), std::move(stream), 351 bound_net_log, std::move(psb), observer);
376 bound_net_log, std::move(psb), observer);
377 } 352 }
378 }; 353 };
379 354
380 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate { 355 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate {
381 public: 356 public:
382 TestShellDownloadManagerDelegate() 357 TestShellDownloadManagerDelegate()
383 : delay_download_open_(false) {} 358 : delay_download_open_(false) {}
384 ~TestShellDownloadManagerDelegate() override {} 359 ~TestShellDownloadManagerDelegate() override {}
385 360
386 bool ShouldOpenDownload( 361 bool ShouldOpenDownload(
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 BrowserThread::IO, FROM_HERE, 560 BrowserThread::IO, FROM_HERE,
586 base::Bind( 561 base::Bind(
587 &net::URLRequestMockHTTPJob::AddUrlHandlers, mock_base, 562 &net::URLRequestMockHTTPJob::AddUrlHandlers, mock_base,
588 make_scoped_refptr(content::BrowserThread::GetBlockingPool()))); 563 make_scoped_refptr(content::BrowserThread::GetBlockingPool())));
589 } 564 }
590 565
591 TestShellDownloadManagerDelegate* GetDownloadManagerDelegate() { 566 TestShellDownloadManagerDelegate* GetDownloadManagerDelegate() {
592 return test_delegate_.get(); 567 return test_delegate_.get();
593 } 568 }
594 569
570 const base::FilePath& GetDownloadDirectory() const {
571 return downloads_directory_.path();
572 }
573
595 // Create a DownloadTestObserverTerminal that will wait for the 574 // Create a DownloadTestObserverTerminal that will wait for the
596 // specified number of downloads to finish. 575 // specified number of downloads to finish.
597 DownloadTestObserver* CreateWaiter( 576 DownloadTestObserver* CreateWaiter(
598 Shell* shell, int num_downloads) { 577 Shell* shell, int num_downloads) {
599 DownloadManager* download_manager = DownloadManagerForShell(shell); 578 DownloadManager* download_manager = DownloadManagerForShell(shell);
600 return new DownloadTestObserverTerminal(download_manager, num_downloads, 579 return new DownloadTestObserverTerminal(download_manager, num_downloads,
601 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); 580 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
602 } 581 }
603 582
604 void WaitForInterrupt(DownloadItem* download) { 583 void WaitForInterrupt(DownloadItem* download) {
(...skipping 1200 matching lines...) Expand 10 before | Expand all | Expand 10 after
1805 download->Cancel(true); 1784 download->Cancel(true);
1806 1785
1807 // The intermediate file should now be gone. 1786 // The intermediate file should now be gone.
1808 RunAllPendingInMessageLoop(BrowserThread::FILE); 1787 RunAllPendingInMessageLoop(BrowserThread::FILE);
1809 RunAllPendingInMessageLoop(); 1788 RunAllPendingInMessageLoop();
1810 EXPECT_FALSE(base::PathExists(intermediate_path)); 1789 EXPECT_FALSE(base::PathExists(intermediate_path));
1811 EXPECT_FALSE(base::PathExists(target_path)); 1790 EXPECT_FALSE(base::PathExists(target_path));
1812 EXPECT_TRUE(EnsureNoPendingDownloads()); 1791 EXPECT_TRUE(EnsureNoPendingDownloads());
1813 } 1792 }
1814 1793
1794 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_NoFile) {
1795 TestDownloadRequestHandler request_handler;
1796 TestDownloadRequestHandler::Parameters parameters;
1797 request_handler.StartServing(parameters);
1798
1799 base::FilePath intermediate_file_path =
1800 GetDownloadDirectory().AppendASCII("intermediate");
1801 std::vector<GURL> url_chain;
1802
1803 const int kIntermediateSize = 1331;
1804 url_chain.push_back(request_handler.url());
1805
1806 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
1807 1, intermediate_file_path, base::FilePath(), url_chain, GURL(),
1808 "application/octet-stream", "application/octet-stream", base::Time::Now(),
1809 base::Time(), parameters.etag, std::string(), kIntermediateSize,
1810 parameters.size, std::string(), DownloadItem::INTERRUPTED,
1811 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1812 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false);
1813
1814 download->Resume();
1815 WaitForCompletion(download);
1816
1817 EXPECT_FALSE(base::PathExists(intermediate_file_path));
1818 ReadAndVerifyFileContents(parameters.pattern_generator_seed, parameters.size,
1819 download->GetTargetFilePath());
1820
1821 TestDownloadRequestHandler::CompletedRequests completed_requests;
1822 request_handler.GetCompletedRequestInfo(&completed_requests);
1823
1824 // There will be two requests. The first one is issued optimistically assuming
1825 // that the intermediate file exists and matches the size expectations set
1826 // forth in the download metadata (i.e. assuming that a 1331 byte file exists
1827 // at |intermediate_file_path|.
1828 //
1829 // However, once the response is received, DownloadFile will report that the
1830 // intermediate file doesn't exist and hence the download is marked
1831 // interrupted again.
1832 //
1833 // The second request reads the entire entity.
1834 //
1835 // N.b. we can't make any assumptions about how many bytes are transferred by
1836 // the first request since response data will be bufferred until DownloadFile
1837 // is done initializing.
1838 //
1839 // TODO(asanka): Ideally we'll check that the intermediate file matches
1840 // expectations prior to issuing the first resumption request.
1841 ASSERT_EQ(2u, completed_requests.size());
1842 EXPECT_EQ(parameters.size, completed_requests[1].transferred_byte_count);
1843 }
1844
1845 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_NoHash) {
1846 TestDownloadRequestHandler request_handler;
1847 TestDownloadRequestHandler::Parameters parameters;
1848 request_handler.StartServing(parameters);
1849
1850 base::FilePath intermediate_file_path =
1851 GetDownloadDirectory().AppendASCII("intermediate");
1852 std::vector<GURL> url_chain;
1853
1854 const int kIntermediateSize = 1331;
1855 std::vector<char> buffer(kIntermediateSize);
1856 request_handler.GetPatternBytes(parameters.pattern_generator_seed, 0,
1857 buffer.size(), buffer.data());
1858 ASSERT_EQ(kIntermediateSize, base::WriteFile(intermediate_file_path,
1859 buffer.data(), buffer.size()));
1860
1861 url_chain.push_back(request_handler.url());
1862
1863 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
1864 1, intermediate_file_path, base::FilePath(), url_chain, GURL(),
1865 "application/octet-stream", "application/octet-stream", base::Time::Now(),
1866 base::Time(), parameters.etag, std::string(), kIntermediateSize,
1867 parameters.size, std::string(), DownloadItem::INTERRUPTED,
1868 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1869 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false);
1870
1871 download->Resume();
1872 WaitForCompletion(download);
1873
1874 EXPECT_FALSE(base::PathExists(intermediate_file_path));
1875 ReadAndVerifyFileContents(parameters.pattern_generator_seed, parameters.size,
1876 download->GetTargetFilePath());
1877
1878 TestDownloadRequestHandler::CompletedRequests completed_requests;
1879 request_handler.GetCompletedRequestInfo(&completed_requests);
1880
1881 // There's only one network request issued, and that is for the remainder of
1882 // the file.
1883 ASSERT_EQ(1u, completed_requests.size());
1884 EXPECT_EQ(parameters.size - kIntermediateSize,
1885 completed_requests[0].transferred_byte_count);
1886 }
1887
1888 IN_PROC_BROWSER_TEST_F(DownloadContentTest,
1889 ResumeRestoredDownload_EtagMismatch) {
1890 TestDownloadRequestHandler request_handler;
1891 TestDownloadRequestHandler::Parameters parameters;
1892 request_handler.StartServing(parameters);
1893
1894 base::FilePath intermediate_file_path =
1895 GetDownloadDirectory().AppendASCII("intermediate");
1896 std::vector<GURL> url_chain;
1897
1898 const int kIntermediateSize = 1331;
1899 std::vector<char> buffer(kIntermediateSize);
1900 request_handler.GetPatternBytes(parameters.pattern_generator_seed + 1, 0,
1901 buffer.size(), buffer.data());
1902 ASSERT_EQ(kIntermediateSize, base::WriteFile(intermediate_file_path,
1903 buffer.data(), buffer.size()));
1904
1905 url_chain.push_back(request_handler.url());
1906
1907 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
1908 1, intermediate_file_path, base::FilePath(), url_chain, GURL(),
1909 "application/octet-stream", "application/octet-stream", base::Time::Now(),
1910 base::Time(), "fake-etag", std::string(), kIntermediateSize,
1911 parameters.size, std::string(), DownloadItem::INTERRUPTED,
1912 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1913 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false);
1914
1915 download->Resume();
1916 WaitForCompletion(download);
1917
1918 EXPECT_FALSE(base::PathExists(intermediate_file_path));
1919 ReadAndVerifyFileContents(parameters.pattern_generator_seed, parameters.size,
1920 download->GetTargetFilePath());
1921
1922 TestDownloadRequestHandler::CompletedRequests completed_requests;
1923 request_handler.GetCompletedRequestInfo(&completed_requests);
1924
1925 // There's only one network request issued. The If-Range header allows the
1926 // server to respond with the entire entity in one go. The existing contents
1927 // of the file should be discarded, and overwritten by the new contents.
1928 ASSERT_EQ(1u, completed_requests.size());
1929 EXPECT_EQ(parameters.size, completed_requests[0].transferred_byte_count);
1930 }
1931
1932 IN_PROC_BROWSER_TEST_F(DownloadContentTest,
1933 ResumeRestoredDownload_CorrectHash) {
1934 TestDownloadRequestHandler request_handler;
1935 TestDownloadRequestHandler::Parameters parameters;
1936 request_handler.StartServing(parameters);
1937
1938 base::FilePath intermediate_file_path =
1939 GetDownloadDirectory().AppendASCII("intermediate");
1940 std::vector<GURL> url_chain;
1941
1942 const int kIntermediateSize = 1331;
1943 std::vector<char> buffer(kIntermediateSize);
1944 request_handler.GetPatternBytes(parameters.pattern_generator_seed, 0,
1945 buffer.size(), buffer.data());
1946 ASSERT_EQ(kIntermediateSize, base::WriteFile(intermediate_file_path,
1947 buffer.data(), buffer.size()));
1948 // SHA-256 hash of the pattern bytes in buffer.
1949 static const uint8_t kPartialHash[] = {
1950 0x77, 0x14, 0xfd, 0x83, 0x06, 0x15, 0x10, 0x7a, 0x47, 0x15, 0xd3,
1951 0xcf, 0xdd, 0x46, 0xa2, 0x61, 0x96, 0xff, 0xc3, 0xbb, 0x49, 0x30,
1952 0xaf, 0x31, 0x3a, 0x64, 0x0b, 0xd5, 0xfa, 0xb1, 0xe3, 0x81};
1953
1954 url_chain.push_back(request_handler.url());
1955
1956 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
1957 1, intermediate_file_path, base::FilePath(), url_chain, GURL(),
1958 "application/octet-stream", "application/octet-stream", base::Time::Now(),
1959 base::Time(), parameters.etag, std::string(), kIntermediateSize,
1960 parameters.size,
1961 std::string(std::begin(kPartialHash), std::end(kPartialHash)),
1962 DownloadItem::INTERRUPTED, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1963 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false);
1964
1965 download->Resume();
1966 WaitForCompletion(download);
1967
1968 EXPECT_FALSE(base::PathExists(intermediate_file_path));
1969 ReadAndVerifyFileContents(parameters.pattern_generator_seed, parameters.size,
1970 download->GetTargetFilePath());
1971
1972 TestDownloadRequestHandler::CompletedRequests completed_requests;
1973 request_handler.GetCompletedRequestInfo(&completed_requests);
1974
1975 // There's only one network request issued, and that is for the remainder of
1976 // the file.
1977 ASSERT_EQ(1u, completed_requests.size());
1978 EXPECT_EQ(parameters.size - kIntermediateSize,
1979 completed_requests[0].transferred_byte_count);
1980
1981 // SHA-256 hash of the entire 102400 bytes in the target file.
1982 static const uint8_t kFullHash[] = {
1983 0xa7, 0x44, 0x49, 0x86, 0x24, 0xc6, 0x84, 0x6c, 0x89, 0xdf, 0xd8,
1984 0xec, 0xa0, 0xe0, 0x61, 0x12, 0xdc, 0x80, 0x13, 0xf2, 0x83, 0x49,
1985 0xa9, 0x14, 0x52, 0x32, 0xf0, 0x95, 0x20, 0xca, 0x5b, 0x30};
1986 EXPECT_EQ(std::string(std::begin(kFullHash), std::end(kFullHash)),
1987 download->GetHash());
1988 }
1989
1990 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_WrongHash) {
1991 TestDownloadRequestHandler request_handler;
1992 TestDownloadRequestHandler::Parameters parameters;
1993 request_handler.StartServing(parameters);
1994
1995 base::FilePath intermediate_file_path =
1996 GetDownloadDirectory().AppendASCII("intermediate");
1997 std::vector<GURL> url_chain;
1998
1999 const int kIntermediateSize = 1331;
2000 std::vector<char> buffer(kIntermediateSize);
2001 ASSERT_EQ(kIntermediateSize, base::WriteFile(intermediate_file_path,
2002 buffer.data(), buffer.size()));
2003 // SHA-256 hash of the expected pattern bytes in buffer. This doesn't match
2004 // the current contents of the intermediate file which should all be 0.
2005 static const uint8_t kPartialHash[] = {
2006 0x77, 0x14, 0xfd, 0x83, 0x06, 0x15, 0x10, 0x7a, 0x47, 0x15, 0xd3,
2007 0xcf, 0xdd, 0x46, 0xa2, 0x61, 0x96, 0xff, 0xc3, 0xbb, 0x49, 0x30,
2008 0xaf, 0x31, 0x3a, 0x64, 0x0b, 0xd5, 0xfa, 0xb1, 0xe3, 0x81};
2009
2010 url_chain.push_back(request_handler.url());
2011
2012 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
2013 1, intermediate_file_path, base::FilePath(), url_chain, GURL(),
2014 "application/octet-stream", "application/octet-stream", base::Time::Now(),
2015 base::Time(), parameters.etag, std::string(), kIntermediateSize,
2016 parameters.size,
2017 std::string(std::begin(kPartialHash), std::end(kPartialHash)),
2018 DownloadItem::INTERRUPTED, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2019 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false);
2020
2021 download->Resume();
2022 WaitForCompletion(download);
2023
2024 EXPECT_FALSE(base::PathExists(intermediate_file_path));
2025 ReadAndVerifyFileContents(parameters.pattern_generator_seed, parameters.size,
2026 download->GetTargetFilePath());
2027
2028 TestDownloadRequestHandler::CompletedRequests completed_requests;
2029 request_handler.GetCompletedRequestInfo(&completed_requests);
2030
2031 // There will be two requests. The first one is issued optimistically assuming
2032 // that the intermediate file exists and matches the size expectations set
2033 // forth in the download metadata (i.e. assuming that a 1331 byte file exists
2034 // at |intermediate_file_path|.
2035 //
2036 // However, once the response is received, DownloadFile will report that the
2037 // intermediate file doesn't match the expected hash.
2038 //
2039 // The second request reads the entire entity.
2040 //
2041 // N.b. we can't make any assumptions about how many bytes are transferred by
2042 // the first request since response data will be bufferred until DownloadFile
2043 // is done initializing.
2044 //
2045 // TODO(asanka): Ideally we'll check that the intermediate file matches
2046 // expectations prior to issuing the first resumption request.
2047 ASSERT_EQ(2u, completed_requests.size());
2048 EXPECT_EQ(parameters.size, completed_requests[1].transferred_byte_count);
2049
2050 // SHA-256 hash of the entire 102400 bytes in the target file.
2051 static const uint8_t kFullHash[] = {
2052 0xa7, 0x44, 0x49, 0x86, 0x24, 0xc6, 0x84, 0x6c, 0x89, 0xdf, 0xd8,
2053 0xec, 0xa0, 0xe0, 0x61, 0x12, 0xdc, 0x80, 0x13, 0xf2, 0x83, 0x49,
2054 0xa9, 0x14, 0x52, 0x32, 0xf0, 0x95, 0x20, 0xca, 0x5b, 0x30};
2055 EXPECT_EQ(std::string(std::begin(kFullHash), std::end(kFullHash)),
2056 download->GetHash());
2057 }
2058
2059 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_ShortFile) {
2060 TestDownloadRequestHandler request_handler;
2061 TestDownloadRequestHandler::Parameters parameters;
2062 request_handler.StartServing(parameters);
2063
2064 base::FilePath intermediate_file_path =
2065 GetDownloadDirectory().AppendASCII("intermediate");
2066 std::vector<GURL> url_chain;
2067
2068 const int kIntermediateSize = 1331;
2069 // Size of file is slightly shorter than the size known to DownloadItem.
2070 std::vector<char> buffer(kIntermediateSize - 100);
2071 request_handler.GetPatternBytes(parameters.pattern_generator_seed, 0,
2072 buffer.size(), buffer.data());
2073 ASSERT_EQ(
2074 kIntermediateSize - 100,
2075 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size()));
2076 url_chain.push_back(request_handler.url());
2077
2078 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
2079 1, intermediate_file_path, base::FilePath(), url_chain, GURL(),
2080 "application/octet-stream", "application/octet-stream", base::Time::Now(),
2081 base::Time(), parameters.etag, std::string(), kIntermediateSize,
2082 parameters.size, std::string(), DownloadItem::INTERRUPTED,
2083 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2084 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, false);
2085
2086 download->Resume();
2087 WaitForCompletion(download);
2088
2089 EXPECT_FALSE(base::PathExists(intermediate_file_path));
2090 ReadAndVerifyFileContents(parameters.pattern_generator_seed, parameters.size,
2091 download->GetTargetFilePath());
2092
2093 TestDownloadRequestHandler::CompletedRequests completed_requests;
2094 request_handler.GetCompletedRequestInfo(&completed_requests);
2095
2096 // There will be two requests. The first one is issued optimistically assuming
2097 // that the intermediate file exists and matches the size expectations set
2098 // forth in the download metadata (i.e. assuming that a 1331 byte file exists
2099 // at |intermediate_file_path|.
2100 //
2101 // However, once the response is received, DownloadFile will report that the
2102 // intermediate file is too short and hence the download is marked interrupted
2103 // again.
2104 //
2105 // The second request reads the entire entity.
2106 //
2107 // N.b. we can't make any assumptions about how many bytes are transferred by
2108 // the first request since response data will be bufferred until DownloadFile
2109 // is done initializing.
2110 //
2111 // TODO(asanka): Ideally we'll check that the intermediate file matches
2112 // expectations prior to issuing the first resumption request.
2113 ASSERT_EQ(2u, completed_requests.size());
2114 EXPECT_EQ(parameters.size, completed_requests[1].transferred_byte_count);
2115 }
2116
1815 // Check that the cookie policy is correctly updated when downloading a file 2117 // Check that the cookie policy is correctly updated when downloading a file
1816 // that redirects cross origin. 2118 // that redirects cross origin.
1817 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CookiePolicy) { 2119 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CookiePolicy) {
1818 net::EmbeddedTestServer origin_one; 2120 net::EmbeddedTestServer origin_one;
1819 net::EmbeddedTestServer origin_two; 2121 net::EmbeddedTestServer origin_two;
1820 ASSERT_TRUE(origin_one.Start()); 2122 ASSERT_TRUE(origin_one.Start());
1821 ASSERT_TRUE(origin_two.Start()); 2123 ASSERT_TRUE(origin_two.Start());
1822 2124
1823 // Block third-party cookies. 2125 // Block third-party cookies.
1824 ShellNetworkDelegate::SetAcceptAllCookies(false); 2126 ShellNetworkDelegate::SetAcceptAllCookies(false);
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
2044 2346
2045 std::vector<DownloadItem*> downloads; 2347 std::vector<DownloadItem*> downloads;
2046 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); 2348 DownloadManagerForShell(shell())->GetAllDownloads(&downloads);
2047 ASSERT_EQ(1u, downloads.size()); 2349 ASSERT_EQ(1u, downloads.size());
2048 2350
2049 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"), 2351 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"),
2050 downloads[0]->GetTargetFilePath().BaseName().value()); 2352 downloads[0]->GetTargetFilePath().BaseName().value());
2051 } 2353 }
2052 2354
2053 } // namespace content 2355 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698