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

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

Issue 9762002: Disable downloads from "file:" or "data:" URLs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changed test to make sure to run message loop while pinging IO thread. Created 8 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/test/data/download-anchor-attrib.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <sstream> 5 #include <sstream>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "base/scoped_temp_dir.h" 13 #include "base/scoped_temp_dir.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/stringprintf.h" 15 #include "base/stringprintf.h"
16 #include "base/test/test_file_util.h" 16 #include "base/test/test_file_util.h"
17 #include "base/test/thread_test_helper.h"
17 #include "base/utf_string_conversions.h" 18 #include "base/utf_string_conversions.h"
18 #include "chrome/app/chrome_command_ids.h" 19 #include "chrome/app/chrome_command_ids.h"
19 #include "chrome/browser/browser_process.h" 20 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/cancelable_request.h" 21 #include "chrome/browser/cancelable_request.h"
21 #include "chrome/browser/download/chrome_download_manager_delegate.h" 22 #include "chrome/browser/download/chrome_download_manager_delegate.h"
22 #include "chrome/browser/download/download_crx_util.h" 23 #include "chrome/browser/download/download_crx_util.h"
23 #include "chrome/browser/download/download_history.h" 24 #include "chrome/browser/download/download_history.h"
24 #include "chrome/browser/download/download_prefs.h" 25 #include "chrome/browser/download/download_prefs.h"
25 #include "chrome/browser/download/download_request_limiter.h" 26 #include "chrome/browser/download/download_request_limiter.h"
26 #include "chrome/browser/download/download_service.h" 27 #include "chrome/browser/download/download_service.h"
(...skipping 11 matching lines...) Expand all
38 #include "chrome/browser/ui/browser.h" 39 #include "chrome/browser/ui/browser.h"
39 #include "chrome/browser/ui/browser_list.h" 40 #include "chrome/browser/ui/browser_list.h"
40 #include "chrome/browser/ui/browser_window.h" 41 #include "chrome/browser/ui/browser_window.h"
41 #include "chrome/browser/ui/webui/chromeos/active_downloads_ui.h" 42 #include "chrome/browser/ui/webui/chromeos/active_downloads_ui.h"
42 #include "chrome/common/chrome_notification_types.h" 43 #include "chrome/common/chrome_notification_types.h"
43 #include "chrome/common/chrome_paths.h" 44 #include "chrome/common/chrome_paths.h"
44 #include "chrome/common/pref_names.h" 45 #include "chrome/common/pref_names.h"
45 #include "chrome/common/url_constants.h" 46 #include "chrome/common/url_constants.h"
46 #include "chrome/test/base/in_process_browser_test.h" 47 #include "chrome/test/base/in_process_browser_test.h"
47 #include "chrome/test/base/ui_test_utils.h" 48 #include "chrome/test/base/ui_test_utils.h"
49 #include "content/public/browser/browser_thread.h"
48 #include "content/public/browser/download_item.h" 50 #include "content/public/browser/download_item.h"
49 #include "content/public/browser/download_manager.h" 51 #include "content/public/browser/download_manager.h"
50 #include "content/public/browser/download_persistent_store_info.h" 52 #include "content/public/browser/download_persistent_store_info.h"
51 #include "content/public/browser/download_save_info.h" 53 #include "content/public/browser/download_save_info.h"
52 #include "content/public/browser/notification_source.h" 54 #include "content/public/browser/notification_source.h"
53 #include "content/public/browser/render_view_host.h" 55 #include "content/public/browser/render_view_host.h"
54 #include "content/public/browser/resource_context.h" 56 #include "content/public/browser/resource_context.h"
55 #include "content/public/browser/web_contents.h" 57 #include "content/public/browser/web_contents.h"
56 #include "content/public/common/context_menu_params.h" 58 #include "content/public/common/context_menu_params.h"
57 #include "content/public/common/page_transition_types.h" 59 #include "content/public/common/page_transition_types.h"
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 enum SizeTestType { 309 enum SizeTestType {
308 SIZE_TEST_TYPE_KNOWN, 310 SIZE_TEST_TYPE_KNOWN,
309 SIZE_TEST_TYPE_UNKNOWN, 311 SIZE_TEST_TYPE_UNKNOWN,
310 }; 312 };
311 313
312 // Location of the file source (the place from which it is downloaded). 314 // Location of the file source (the place from which it is downloaded).
313 FilePath OriginFile(FilePath file) { 315 FilePath OriginFile(FilePath file) {
314 return test_dir_.Append(file); 316 return test_dir_.Append(file);
315 } 317 }
316 318
319 GURL OriginFileUrl(FilePath file) {
320 std::string file_str = test_dir_.Append(file).MaybeAsASCII();
321 DCHECK(!file_str.empty()); // We only expect ASCII paths in tests.
322 return GURL("file://" + file_str);
323 }
324
317 // Location of the file destination (place to which it is downloaded). 325 // Location of the file destination (place to which it is downloaded).
318 FilePath DestinationFile(Browser* browser, FilePath file) { 326 FilePath DestinationFile(Browser* browser, FilePath file) {
319 return GetDownloadDirectory(browser).Append(file); 327 return GetDownloadDirectory(browser).Append(file);
320 } 328 }
321 329
322 // Must be called after browser creation. Creates a temporary 330 // Must be called after browser creation. Creates a temporary
323 // directory for downloads that is auto-deleted on destruction. 331 // directory for downloads that is auto-deleted on destruction.
324 // Returning false indicates a failure of the function, and should be asserted 332 // Returning false indicates a failure of the function, and should be asserted
325 // in the caller. 333 // in the caller.
326 bool CreateAndSetDownloadsDirectory(Browser* browser) { 334 bool CreateAndSetDownloadsDirectory(Browser* browser) {
(...skipping 2124 matching lines...) Expand 10 before | Expand all | Expand 10 after
2451 GURL url(test_server()->GetURL(path)); 2459 GURL url(test_server()->GetURL(path));
2452 2460
2453 DownloadTestObserver* observer(DangerousDownloadWaiter( 2461 DownloadTestObserver* observer(DangerousDownloadWaiter(
2454 browser(), 1, DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_ACCEPT)); 2462 browser(), 1, DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_ACCEPT));
2455 ui_test_utils::NavigateToURL(browser(), url); 2463 ui_test_utils::NavigateToURL(browser(), url);
2456 observer->WaitForFinished(); 2464 observer->WaitForFinished();
2457 2465
2458 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); 2466 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE));
2459 EXPECT_EQ(1u, observer->NumDangerousDownloadsSeen()); 2467 EXPECT_EQ(1u, observer->NumDangerousDownloadsSeen());
2460 } 2468 }
2469
2470 IN_PROC_BROWSER_TEST_F(DownloadTest, TestFileDataBlocker) {
2471 ASSERT_TRUE(InitialSetup(false));
2472 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
2473 GURL urls[] = {
2474 // file: URL
2475 OriginFileUrl(file),
2476
2477 // data: URL
2478 GURL("data:application/octet-stream,abcdefghijklmnop%01%02%03l")
2479 };
2480
2481 for (size_t i = 0; i < arraysize(urls); i++) {
2482 // Navigate & block until navigation is done.
2483 ui_test_utils::NavigateToURLWithDisposition(
2484 browser(), urls[i], CURRENT_TAB,
2485 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
2486
2487 // Do a round trip to the IO thread to increase chances of any download
2488 // showing up on the UI thread.
2489 // Using DownloadTestFlushObserver is overkill, but it'll do the job.
2490 scoped_refptr<DownloadTestFlushObserver> flush_observer(
2491 new DownloadTestFlushObserver(
2492 DownloadManagerForBrowser(browser())));
2493 flush_observer->WaitForFlush();
2494
2495 // Confirm no downloads
2496 std::vector<DownloadItem*> downloads;
2497 GetDownloads(browser(), &downloads);
2498 EXPECT_EQ(0u, downloads.size());
2499
2500 DownloadManagerForBrowser(browser())->RemoveAllDownloads();
2501
2502 // Try the same thing with a direct download. Also check that the
2503 // callback gives the right error.
2504 WebContents* web_contents = browser()->GetSelectedWebContents();
2505 ASSERT_TRUE(web_contents);
2506 scoped_refptr<DownloadTestItemCreationObserver> creation_observer(
2507 new DownloadTestItemCreationObserver);
2508 // Only for cleanup if a download is actually created.
2509 DownloadTestObserverTerminal backup_observer(
2510 DownloadManagerForBrowser(browser()),
2511 1,
2512 false,
2513 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
2514
2515 DownloadManagerForBrowser(browser())->DownloadUrl(
2516 urls[i], GURL(), "", false, -1, content::DownloadSaveInfo(),
2517 web_contents, creation_observer->callback());
2518
2519 creation_observer->WaitForDownloadItemCreation();
2520
2521 EXPECT_FALSE(creation_observer->succeeded());
2522 EXPECT_EQ(net::ERR_DISALLOWED_URL_SCHEME, creation_observer->error());
2523 EXPECT_EQ(content::DownloadId::Invalid(), creation_observer->download_id());
2524 downloads.clear();
2525 GetDownloads(browser(), &downloads);
2526 EXPECT_EQ(0u, downloads.size());
2527
2528 if (creation_observer->succeeded()) {
2529 // Wait until the download is done. We don't care how it's finished.
2530 backup_observer.WaitForFinished();
2531 }
2532 DownloadManagerForBrowser(browser())->RemoveAllDownloads();
2533 }
2534 }
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/download-anchor-attrib.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698