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

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: Rebase on top of https://codereview.chromium.org/1781983002 since that's going in first. 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,
234 owner_, callback)); 226 client_guid,
227 source_url,
228 referrer_url,
229 base::Bind(
230 DownloadFileWithDelay::RenameCallbackWrapper, owner_, callback));
235 } 231 }
236 232
237 // static 233 // static
238 void DownloadFileWithDelay::RenameCallbackWrapper( 234 void DownloadFileWithDelay::RenameCallbackWrapper(
239 const base::WeakPtr<DownloadFileWithDelayFactory>& factory, 235 const base::WeakPtr<DownloadFileWithDelayFactory>& factory,
240 const RenameCompletionCallback& original_callback, 236 const RenameCompletionCallback& original_callback,
241 DownloadInterruptReason reason, 237 DownloadInterruptReason reason,
242 const base::FilePath& path) { 238 const base::FilePath& path) {
243 DCHECK_CURRENTLY_ON(BrowserThread::UI); 239 DCHECK_CURRENTLY_ON(BrowserThread::UI);
244 if (!factory) 240 if (!factory)
245 return; 241 return;
246 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); 242 factory->AddRenameCallback(base::Bind(original_callback, reason, path));
247 } 243 }
248 244
249 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() 245 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory()
250 : waiting_(false), 246 : waiting_(false),
251 weak_ptr_factory_(this) {} 247 weak_ptr_factory_(this) {}
252 248
253 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} 249 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {}
254 250
255 DownloadFile* DownloadFileWithDelayFactory::CreateFile( 251 DownloadFile* DownloadFileWithDelayFactory::CreateFile(
256 const DownloadSaveInfo& save_info, 252 scoped_ptr<DownloadSaveInfo> save_info,
257 const base::FilePath& default_download_directory, 253 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, 254 scoped_ptr<ByteStreamReader> stream,
263 const net::BoundNetLog& bound_net_log, 255 const net::BoundNetLog& bound_net_log,
264 base::WeakPtr<DownloadDestinationObserver> observer) { 256 base::WeakPtr<DownloadDestinationObserver> observer) {
265 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create( 257 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create(
266 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 258 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
267 PowerSaveBlocker::kReasonOther, "Download in progress")); 259 PowerSaveBlocker::kReasonOther, "Download in progress"));
268 return new DownloadFileWithDelay( 260 return new DownloadFileWithDelay(std::move(save_info),
269 save_info, default_download_directory, url, referrer_url, calculate_hash, 261 default_download_directory,
270 std::move(file), std::move(stream), bound_net_log, std::move(psb), 262 std::move(stream),
271 observer, weak_ptr_factory_.GetWeakPtr()); 263 bound_net_log,
264 std::move(psb),
265 observer,
266 weak_ptr_factory_.GetWeakPtr());
272 } 267 }
273 268
274 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { 269 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) {
275 DCHECK_CURRENTLY_ON(BrowserThread::UI); 270 DCHECK_CURRENTLY_ON(BrowserThread::UI);
276 rename_callbacks_.push_back(callback); 271 rename_callbacks_.push_back(callback);
277 if (waiting_) 272 if (waiting_)
278 base::MessageLoopForUI::current()->QuitWhenIdle(); 273 base::MessageLoopForUI::current()->QuitWhenIdle();
279 } 274 }
280 275
281 void DownloadFileWithDelayFactory::GetAllRenameCallbacks( 276 void DownloadFileWithDelayFactory::GetAllRenameCallbacks(
282 std::vector<base::Closure>* results) { 277 std::vector<base::Closure>* results) {
283 DCHECK_CURRENTLY_ON(BrowserThread::UI); 278 DCHECK_CURRENTLY_ON(BrowserThread::UI);
284 results->swap(rename_callbacks_); 279 results->swap(rename_callbacks_);
285 } 280 }
286 281
287 void DownloadFileWithDelayFactory::WaitForSomeCallback() { 282 void DownloadFileWithDelayFactory::WaitForSomeCallback() {
288 DCHECK_CURRENTLY_ON(BrowserThread::UI); 283 DCHECK_CURRENTLY_ON(BrowserThread::UI);
289 284
290 if (rename_callbacks_.empty()) { 285 if (rename_callbacks_.empty()) {
291 waiting_ = true; 286 waiting_ = true;
292 RunMessageLoop(); 287 RunMessageLoop();
293 waiting_ = false; 288 waiting_ = false;
294 } 289 }
295 } 290 }
296 291
297 class CountingDownloadFile : public DownloadFileImpl { 292 class CountingDownloadFile : public DownloadFileImpl {
298 public: 293 public:
299 CountingDownloadFile(const DownloadSaveInfo& save_info, 294 CountingDownloadFile(scoped_ptr<DownloadSaveInfo> save_info,
300 const base::FilePath& default_downloads_directory, 295 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, 296 scoped_ptr<ByteStreamReader> stream,
306 const net::BoundNetLog& bound_net_log, 297 const net::BoundNetLog& bound_net_log,
307 scoped_ptr<PowerSaveBlocker> power_save_blocker, 298 scoped_ptr<PowerSaveBlocker> power_save_blocker,
308 base::WeakPtr<DownloadDestinationObserver> observer) 299 base::WeakPtr<DownloadDestinationObserver> observer)
309 : DownloadFileImpl(save_info, 300 : DownloadFileImpl(std::move(save_info),
310 default_downloads_directory, 301 default_downloads_directory,
311 url,
312 referrer_url,
313 calculate_hash,
314 std::move(file),
315 std::move(stream), 302 std::move(stream),
316 bound_net_log, 303 bound_net_log,
317 observer) {} 304 observer) {}
318 305
319 ~CountingDownloadFile() override { 306 ~CountingDownloadFile() override {
320 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 307 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
321 active_files_--; 308 active_files_--;
322 } 309 }
323 310
324 void Initialize(const InitializeCallback& callback) override { 311 void Initialize(const InitializeCallback& callback) override {
(...skipping 26 matching lines...) Expand all
351 338
352 int CountingDownloadFile::active_files_ = 0; 339 int CountingDownloadFile::active_files_ = 0;
353 340
354 class CountingDownloadFileFactory : public DownloadFileFactory { 341 class CountingDownloadFileFactory : public DownloadFileFactory {
355 public: 342 public:
356 CountingDownloadFileFactory() {} 343 CountingDownloadFileFactory() {}
357 ~CountingDownloadFileFactory() override {} 344 ~CountingDownloadFileFactory() override {}
358 345
359 // DownloadFileFactory interface. 346 // DownloadFileFactory interface.
360 DownloadFile* CreateFile( 347 DownloadFile* CreateFile(
361 const DownloadSaveInfo& save_info, 348 scoped_ptr<DownloadSaveInfo> save_info,
362 const base::FilePath& default_downloads_directory, 349 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, 350 scoped_ptr<ByteStreamReader> stream,
368 const net::BoundNetLog& bound_net_log, 351 const net::BoundNetLog& bound_net_log,
369 base::WeakPtr<DownloadDestinationObserver> observer) override { 352 base::WeakPtr<DownloadDestinationObserver> observer) override {
370 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create( 353 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create(
371 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 354 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
372 PowerSaveBlocker::kReasonOther, "Download in progress")); 355 PowerSaveBlocker::kReasonOther, "Download in progress"));
373 return new CountingDownloadFile(save_info, default_downloads_directory, url, 356 return new CountingDownloadFile(std::move(save_info),
374 referrer_url, calculate_hash, 357 default_downloads_directory,
375 std::move(file), std::move(stream), 358 std::move(stream),
376 bound_net_log, std::move(psb), observer); 359 bound_net_log,
360 std::move(psb),
361 observer);
377 } 362 }
378 }; 363 };
379 364
380 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate { 365 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate {
381 public: 366 public:
382 TestShellDownloadManagerDelegate() 367 TestShellDownloadManagerDelegate()
383 : delay_download_open_(false) {} 368 : delay_download_open_(false) {}
384 ~TestShellDownloadManagerDelegate() override {} 369 ~TestShellDownloadManagerDelegate() override {}
385 370
386 bool ShouldOpenDownload( 371 bool ShouldOpenDownload(
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 BrowserThread::IO, FROM_HERE, 570 BrowserThread::IO, FROM_HERE,
586 base::Bind( 571 base::Bind(
587 &net::URLRequestMockHTTPJob::AddUrlHandlers, mock_base, 572 &net::URLRequestMockHTTPJob::AddUrlHandlers, mock_base,
588 make_scoped_refptr(content::BrowserThread::GetBlockingPool()))); 573 make_scoped_refptr(content::BrowserThread::GetBlockingPool())));
589 } 574 }
590 575
591 TestShellDownloadManagerDelegate* GetDownloadManagerDelegate() { 576 TestShellDownloadManagerDelegate* GetDownloadManagerDelegate() {
592 return test_delegate_.get(); 577 return test_delegate_.get();
593 } 578 }
594 579
580 const base::FilePath& GetDownloadDirectory() const {
581 return downloads_directory_.path();
582 }
583
595 // Create a DownloadTestObserverTerminal that will wait for the 584 // Create a DownloadTestObserverTerminal that will wait for the
596 // specified number of downloads to finish. 585 // specified number of downloads to finish.
597 DownloadTestObserver* CreateWaiter( 586 DownloadTestObserver* CreateWaiter(
598 Shell* shell, int num_downloads) { 587 Shell* shell, int num_downloads) {
599 DownloadManager* download_manager = DownloadManagerForShell(shell); 588 DownloadManager* download_manager = DownloadManagerForShell(shell);
600 return new DownloadTestObserverTerminal(download_manager, num_downloads, 589 return new DownloadTestObserverTerminal(download_manager, num_downloads,
601 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); 590 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
602 } 591 }
603 592
604 void WaitForInterrupt(DownloadItem* download) { 593 void WaitForInterrupt(DownloadItem* download) {
(...skipping 1200 matching lines...) Expand 10 before | Expand all | Expand 10 after
1805 download->Cancel(true); 1794 download->Cancel(true);
1806 1795
1807 // The intermediate file should now be gone. 1796 // The intermediate file should now be gone.
1808 RunAllPendingInMessageLoop(BrowserThread::FILE); 1797 RunAllPendingInMessageLoop(BrowserThread::FILE);
1809 RunAllPendingInMessageLoop(); 1798 RunAllPendingInMessageLoop();
1810 EXPECT_FALSE(base::PathExists(intermediate_path)); 1799 EXPECT_FALSE(base::PathExists(intermediate_path));
1811 EXPECT_FALSE(base::PathExists(target_path)); 1800 EXPECT_FALSE(base::PathExists(target_path));
1812 EXPECT_TRUE(EnsureNoPendingDownloads()); 1801 EXPECT_TRUE(EnsureNoPendingDownloads());
1813 } 1802 }
1814 1803
1804 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_NoFile) {
1805 TestDownloadRequestHandler request_handler;
1806 TestDownloadRequestHandler::Parameters parameters;
1807 request_handler.StartServing(parameters);
1808
1809 base::FilePath intermediate_file_path =
1810 GetDownloadDirectory().AppendASCII("intermediate");
1811 std::vector<GURL> url_chain;
1812
1813 const int kIntermediateSize = 1331;
1814 url_chain.push_back(request_handler.url());
1815
1816 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
1817 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583",
1818 1,
1819 intermediate_file_path,
1820 base::FilePath(),
1821 url_chain,
1822 GURL(),
1823 "application/octet-stream",
1824 "application/octet-stream",
1825 base::Time::Now(),
1826 base::Time(),
1827 parameters.etag,
1828 std::string(),
1829 kIntermediateSize,
1830 parameters.size,
1831 std::string(),
1832 DownloadItem::INTERRUPTED,
1833 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1834 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED,
1835 false);
1836
1837 download->Resume();
1838 WaitForCompletion(download);
1839
1840 EXPECT_FALSE(base::PathExists(intermediate_file_path));
1841 ReadAndVerifyFileContents(parameters.pattern_generator_seed,
1842 parameters.size,
1843 download->GetTargetFilePath());
1844
1845 TestDownloadRequestHandler::CompletedRequests completed_requests;
1846 request_handler.GetCompletedRequestInfo(&completed_requests);
1847
1848 // There will be two requests. The first one is issued optimistically assuming
1849 // that the intermediate file exists and matches the size expectations set
1850 // forth in the download metadata (i.e. assuming that a 1331 byte file exists
1851 // at |intermediate_file_path|.
1852 //
1853 // However, once the response is received, DownloadFile will report that the
1854 // intermediate file doesn't exist and hence the download is marked
1855 // interrupted again.
1856 //
1857 // The second request reads the entire entity.
1858 //
1859 // N.b. we can't make any assumptions about how many bytes are transferred by
1860 // the first request since response data will be bufferred until DownloadFile
1861 // is done initializing.
1862 //
1863 // TODO(asanka): Ideally we'll check that the intermediate file matches
1864 // expectations prior to issuing the first resumption request.
1865 ASSERT_EQ(2u, completed_requests.size());
1866 EXPECT_EQ(parameters.size, completed_requests[1].transferred_byte_count);
1867 }
1868
1869 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_NoHash) {
1870 TestDownloadRequestHandler request_handler;
1871 TestDownloadRequestHandler::Parameters parameters;
1872 request_handler.StartServing(parameters);
1873
1874 base::FilePath intermediate_file_path =
1875 GetDownloadDirectory().AppendASCII("intermediate");
1876 std::vector<GURL> url_chain;
1877
1878 const int kIntermediateSize = 1331;
1879 std::vector<char> buffer(kIntermediateSize);
1880 request_handler.GetPatternBytes(
1881 parameters.pattern_generator_seed, 0, buffer.size(), buffer.data());
1882 ASSERT_EQ(
1883 kIntermediateSize,
1884 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size()));
1885
1886 url_chain.push_back(request_handler.url());
1887
1888 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
1889 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583",
1890 1,
1891 intermediate_file_path,
1892 base::FilePath(),
1893 url_chain,
1894 GURL(),
1895 "application/octet-stream",
1896 "application/octet-stream",
1897 base::Time::Now(),
1898 base::Time(),
1899 parameters.etag,
1900 std::string(),
1901 kIntermediateSize,
1902 parameters.size,
1903 std::string(),
1904 DownloadItem::INTERRUPTED,
1905 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1906 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED,
1907 false);
1908
1909 download->Resume();
1910 WaitForCompletion(download);
1911
1912 EXPECT_FALSE(base::PathExists(intermediate_file_path));
1913 ReadAndVerifyFileContents(parameters.pattern_generator_seed,
1914 parameters.size,
1915 download->GetTargetFilePath());
1916
1917 TestDownloadRequestHandler::CompletedRequests completed_requests;
1918 request_handler.GetCompletedRequestInfo(&completed_requests);
1919
1920 // There's only one network request issued, and that is for the remainder of
1921 // the file.
1922 ASSERT_EQ(1u, completed_requests.size());
1923 EXPECT_EQ(parameters.size - kIntermediateSize,
1924 completed_requests[0].transferred_byte_count);
1925 }
1926
1927 IN_PROC_BROWSER_TEST_F(DownloadContentTest,
1928 ResumeRestoredDownload_EtagMismatch) {
1929 TestDownloadRequestHandler request_handler;
1930 TestDownloadRequestHandler::Parameters parameters;
1931 request_handler.StartServing(parameters);
1932
1933 base::FilePath intermediate_file_path =
1934 GetDownloadDirectory().AppendASCII("intermediate");
1935 std::vector<GURL> url_chain;
1936
1937 const int kIntermediateSize = 1331;
1938 std::vector<char> buffer(kIntermediateSize);
1939 request_handler.GetPatternBytes(
1940 parameters.pattern_generator_seed + 1, 0, buffer.size(), buffer.data());
1941 ASSERT_EQ(
1942 kIntermediateSize,
1943 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size()));
1944
1945 url_chain.push_back(request_handler.url());
1946
1947 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
1948 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583",
1949 1,
1950 intermediate_file_path,
1951 base::FilePath(),
1952 url_chain,
1953 GURL(),
1954 "application/octet-stream",
1955 "application/octet-stream",
1956 base::Time::Now(),
1957 base::Time(),
1958 "fake-etag",
1959 std::string(),
1960 kIntermediateSize,
1961 parameters.size,
1962 std::string(),
1963 DownloadItem::INTERRUPTED,
1964 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1965 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED,
1966 false);
1967
1968 download->Resume();
1969 WaitForCompletion(download);
1970
1971 EXPECT_FALSE(base::PathExists(intermediate_file_path));
1972 ReadAndVerifyFileContents(parameters.pattern_generator_seed,
1973 parameters.size,
1974 download->GetTargetFilePath());
1975
1976 TestDownloadRequestHandler::CompletedRequests completed_requests;
1977 request_handler.GetCompletedRequestInfo(&completed_requests);
1978
1979 // There's only one network request issued. The If-Range header allows the
1980 // server to respond with the entire entity in one go. The existing contents
1981 // of the file should be discarded, and overwritten by the new contents.
1982 ASSERT_EQ(1u, completed_requests.size());
1983 EXPECT_EQ(parameters.size, completed_requests[0].transferred_byte_count);
1984 }
1985
1986 IN_PROC_BROWSER_TEST_F(DownloadContentTest,
1987 ResumeRestoredDownload_CorrectHash) {
1988 TestDownloadRequestHandler request_handler;
1989 TestDownloadRequestHandler::Parameters parameters;
1990 request_handler.StartServing(parameters);
1991
1992 base::FilePath intermediate_file_path =
1993 GetDownloadDirectory().AppendASCII("intermediate");
1994 std::vector<GURL> url_chain;
1995
1996 const int kIntermediateSize = 1331;
1997 std::vector<char> buffer(kIntermediateSize);
1998 request_handler.GetPatternBytes(
1999 parameters.pattern_generator_seed, 0, buffer.size(), buffer.data());
2000 ASSERT_EQ(
2001 kIntermediateSize,
2002 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size()));
2003 // SHA-256 hash of the pattern bytes in buffer.
2004 static const uint8_t kPartialHash[] = {
2005 0x77, 0x14, 0xfd, 0x83, 0x06, 0x15, 0x10, 0x7a, 0x47, 0x15, 0xd3,
2006 0xcf, 0xdd, 0x46, 0xa2, 0x61, 0x96, 0xff, 0xc3, 0xbb, 0x49, 0x30,
2007 0xaf, 0x31, 0x3a, 0x64, 0x0b, 0xd5, 0xfa, 0xb1, 0xe3, 0x81};
2008
2009 url_chain.push_back(request_handler.url());
2010
2011 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
2012 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583",
2013 1,
2014 intermediate_file_path,
2015 base::FilePath(),
2016 url_chain,
2017 GURL(),
2018 "application/octet-stream",
2019 "application/octet-stream",
2020 base::Time::Now(),
2021 base::Time(),
2022 parameters.etag,
2023 std::string(),
2024 kIntermediateSize,
2025 parameters.size,
2026 std::string(std::begin(kPartialHash), std::end(kPartialHash)),
2027 DownloadItem::INTERRUPTED,
2028 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2029 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED,
2030 false);
2031
2032 download->Resume();
2033 WaitForCompletion(download);
2034
2035 EXPECT_FALSE(base::PathExists(intermediate_file_path));
2036 ReadAndVerifyFileContents(parameters.pattern_generator_seed,
2037 parameters.size,
2038 download->GetTargetFilePath());
2039
2040 TestDownloadRequestHandler::CompletedRequests completed_requests;
2041 request_handler.GetCompletedRequestInfo(&completed_requests);
2042
2043 // There's only one network request issued, and that is for the remainder of
2044 // the file.
2045 ASSERT_EQ(1u, completed_requests.size());
2046 EXPECT_EQ(parameters.size - kIntermediateSize,
2047 completed_requests[0].transferred_byte_count);
2048
2049 // SHA-256 hash of the entire 102400 bytes in the target file.
2050 static const uint8_t kFullHash[] = {
2051 0xa7, 0x44, 0x49, 0x86, 0x24, 0xc6, 0x84, 0x6c, 0x89, 0xdf, 0xd8,
2052 0xec, 0xa0, 0xe0, 0x61, 0x12, 0xdc, 0x80, 0x13, 0xf2, 0x83, 0x49,
2053 0xa9, 0x14, 0x52, 0x32, 0xf0, 0x95, 0x20, 0xca, 0x5b, 0x30};
2054 EXPECT_EQ(std::string(std::begin(kFullHash), std::end(kFullHash)),
2055 download->GetHash());
2056 }
2057
2058 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_WrongHash) {
2059 TestDownloadRequestHandler request_handler;
2060 TestDownloadRequestHandler::Parameters parameters;
2061 request_handler.StartServing(parameters);
2062
2063 base::FilePath intermediate_file_path =
2064 GetDownloadDirectory().AppendASCII("intermediate");
2065 std::vector<GURL> url_chain;
2066
2067 const int kIntermediateSize = 1331;
2068 std::vector<char> buffer(kIntermediateSize);
2069 ASSERT_EQ(
2070 kIntermediateSize,
2071 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size()));
2072 // SHA-256 hash of the expected pattern bytes in buffer. This doesn't match
2073 // the current contents of the intermediate file which should all be 0.
2074 static const uint8_t kPartialHash[] = {
2075 0x77, 0x14, 0xfd, 0x83, 0x06, 0x15, 0x10, 0x7a, 0x47, 0x15, 0xd3,
2076 0xcf, 0xdd, 0x46, 0xa2, 0x61, 0x96, 0xff, 0xc3, 0xbb, 0x49, 0x30,
2077 0xaf, 0x31, 0x3a, 0x64, 0x0b, 0xd5, 0xfa, 0xb1, 0xe3, 0x81};
2078
2079 url_chain.push_back(request_handler.url());
2080
2081 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
2082 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583",
2083 1,
2084 intermediate_file_path,
2085 base::FilePath(),
2086 url_chain,
2087 GURL(),
2088 "application/octet-stream",
2089 "application/octet-stream",
2090 base::Time::Now(),
2091 base::Time(),
2092 parameters.etag,
2093 std::string(),
2094 kIntermediateSize,
2095 parameters.size,
2096 std::string(std::begin(kPartialHash), std::end(kPartialHash)),
2097 DownloadItem::INTERRUPTED,
2098 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2099 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED,
2100 false);
2101
2102 download->Resume();
2103 WaitForCompletion(download);
2104
2105 EXPECT_FALSE(base::PathExists(intermediate_file_path));
2106 ReadAndVerifyFileContents(parameters.pattern_generator_seed,
2107 parameters.size,
2108 download->GetTargetFilePath());
2109
2110 TestDownloadRequestHandler::CompletedRequests completed_requests;
2111 request_handler.GetCompletedRequestInfo(&completed_requests);
2112
2113 // There will be two requests. The first one is issued optimistically assuming
2114 // that the intermediate file exists and matches the size expectations set
2115 // forth in the download metadata (i.e. assuming that a 1331 byte file exists
2116 // at |intermediate_file_path|.
2117 //
2118 // However, once the response is received, DownloadFile will report that the
2119 // intermediate file doesn't match the expected hash.
2120 //
2121 // The second request reads the entire entity.
2122 //
2123 // N.b. we can't make any assumptions about how many bytes are transferred by
2124 // the first request since response data will be bufferred until DownloadFile
2125 // is done initializing.
2126 //
2127 // TODO(asanka): Ideally we'll check that the intermediate file matches
2128 // expectations prior to issuing the first resumption request.
2129 ASSERT_EQ(2u, completed_requests.size());
2130 EXPECT_EQ(parameters.size, completed_requests[1].transferred_byte_count);
2131
2132 // SHA-256 hash of the entire 102400 bytes in the target file.
2133 static const uint8_t kFullHash[] = {
2134 0xa7, 0x44, 0x49, 0x86, 0x24, 0xc6, 0x84, 0x6c, 0x89, 0xdf, 0xd8,
2135 0xec, 0xa0, 0xe0, 0x61, 0x12, 0xdc, 0x80, 0x13, 0xf2, 0x83, 0x49,
2136 0xa9, 0x14, 0x52, 0x32, 0xf0, 0x95, 0x20, 0xca, 0x5b, 0x30};
2137 EXPECT_EQ(std::string(std::begin(kFullHash), std::end(kFullHash)),
2138 download->GetHash());
2139 }
2140
2141 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_ShortFile) {
2142 TestDownloadRequestHandler request_handler;
2143 TestDownloadRequestHandler::Parameters parameters;
2144 request_handler.StartServing(parameters);
2145
2146 base::FilePath intermediate_file_path =
2147 GetDownloadDirectory().AppendASCII("intermediate");
2148 std::vector<GURL> url_chain;
2149
2150 const int kIntermediateSize = 1331;
2151 // Size of file is slightly shorter than the size known to DownloadItem.
2152 std::vector<char> buffer(kIntermediateSize - 100);
2153 request_handler.GetPatternBytes(
2154 parameters.pattern_generator_seed, 0, buffer.size(), buffer.data());
2155 ASSERT_EQ(
2156 kIntermediateSize - 100,
2157 base::WriteFile(intermediate_file_path, buffer.data(), buffer.size()));
2158 url_chain.push_back(request_handler.url());
2159
2160 DownloadItem* download = DownloadManagerForShell(shell())->CreateDownloadItem(
2161 "F7FB1F59-7DE1-4845-AFDB-8A688F70F583",
2162 1,
2163 intermediate_file_path,
2164 base::FilePath(),
2165 url_chain,
2166 GURL(),
2167 "application/octet-stream",
2168 "application/octet-stream",
2169 base::Time::Now(),
2170 base::Time(),
2171 parameters.etag,
2172 std::string(),
2173 kIntermediateSize,
2174 parameters.size,
2175 std::string(),
2176 DownloadItem::INTERRUPTED,
2177 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2178 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED,
2179 false);
2180
2181 download->Resume();
2182 WaitForCompletion(download);
2183
2184 EXPECT_FALSE(base::PathExists(intermediate_file_path));
2185 ReadAndVerifyFileContents(parameters.pattern_generator_seed,
2186 parameters.size,
2187 download->GetTargetFilePath());
2188
2189 TestDownloadRequestHandler::CompletedRequests completed_requests;
2190 request_handler.GetCompletedRequestInfo(&completed_requests);
2191
2192 // There will be two requests. The first one is issued optimistically assuming
2193 // that the intermediate file exists and matches the size expectations set
2194 // forth in the download metadata (i.e. assuming that a 1331 byte file exists
2195 // at |intermediate_file_path|.
2196 //
2197 // However, once the response is received, DownloadFile will report that the
2198 // intermediate file is too short and hence the download is marked interrupted
2199 // again.
2200 //
2201 // The second request reads the entire entity.
2202 //
2203 // N.b. we can't make any assumptions about how many bytes are transferred by
2204 // the first request since response data will be bufferred until DownloadFile
2205 // is done initializing.
2206 //
2207 // TODO(asanka): Ideally we'll check that the intermediate file matches
2208 // expectations prior to issuing the first resumption request.
2209 ASSERT_EQ(2u, completed_requests.size());
2210 EXPECT_EQ(parameters.size, completed_requests[1].transferred_byte_count);
2211 }
2212
1815 // Check that the cookie policy is correctly updated when downloading a file 2213 // Check that the cookie policy is correctly updated when downloading a file
1816 // that redirects cross origin. 2214 // that redirects cross origin.
1817 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CookiePolicy) { 2215 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CookiePolicy) {
1818 net::EmbeddedTestServer origin_one; 2216 net::EmbeddedTestServer origin_one;
1819 net::EmbeddedTestServer origin_two; 2217 net::EmbeddedTestServer origin_two;
1820 ASSERT_TRUE(origin_one.Start()); 2218 ASSERT_TRUE(origin_one.Start());
1821 ASSERT_TRUE(origin_two.Start()); 2219 ASSERT_TRUE(origin_two.Start());
1822 2220
1823 // Block third-party cookies. 2221 // Block third-party cookies.
1824 ShellNetworkDelegate::SetAcceptAllCookies(false); 2222 ShellNetworkDelegate::SetAcceptAllCookies(false);
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
2044 2442
2045 std::vector<DownloadItem*> downloads; 2443 std::vector<DownloadItem*> downloads;
2046 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); 2444 DownloadManagerForShell(shell())->GetAllDownloads(&downloads);
2047 ASSERT_EQ(1u, downloads.size()); 2445 ASSERT_EQ(1u, downloads.size());
2048 2446
2049 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"), 2447 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"),
2050 downloads[0]->GetTargetFilePath().BaseName().value()); 2448 downloads[0]->GetTargetFilePath().BaseName().value());
2051 } 2449 }
2052 2450
2053 } // namespace content 2451 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/base_file_win.cc ('k') | content/browser/download/download_destination_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698