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

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

Issue 565053002: [Downloads] Gracefully handle SafeBrowsing check failures. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <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/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file.h" 10 #include "base/files/file.h"
(...skipping 29 matching lines...) Expand all
40 #include "chrome/browser/extensions/extension_install_prompt.h" 40 #include "chrome/browser/extensions/extension_install_prompt.h"
41 #include "chrome/browser/extensions/extension_service.h" 41 #include "chrome/browser/extensions/extension_service.h"
42 #include "chrome/browser/history/download_row.h" 42 #include "chrome/browser/history/download_row.h"
43 #include "chrome/browser/history/history_service.h" 43 #include "chrome/browser/history/history_service.h"
44 #include "chrome/browser/history/history_service_factory.h" 44 #include "chrome/browser/history/history_service_factory.h"
45 #include "chrome/browser/infobars/infobar_service.h" 45 #include "chrome/browser/infobars/infobar_service.h"
46 #include "chrome/browser/net/url_request_mock_util.h" 46 #include "chrome/browser/net/url_request_mock_util.h"
47 #include "chrome/browser/profiles/profile.h" 47 #include "chrome/browser/profiles/profile.h"
48 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsert est_util.h" 48 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsert est_util.h"
49 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti l.h" 49 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti l.h"
50 #include "chrome/browser/safe_browsing/download_feedback_service.h"
51 #include "chrome/browser/safe_browsing/download_protection_service.h"
52 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
53 #include "chrome/browser/ui/browser.h" 50 #include "chrome/browser/ui/browser.h"
54 #include "chrome/browser/ui/browser_commands.h" 51 #include "chrome/browser/ui/browser_commands.h"
55 #include "chrome/browser/ui/browser_finder.h" 52 #include "chrome/browser/ui/browser_finder.h"
56 #include "chrome/browser/ui/browser_list.h" 53 #include "chrome/browser/ui/browser_list.h"
57 #include "chrome/browser/ui/browser_tabstrip.h" 54 #include "chrome/browser/ui/browser_tabstrip.h"
58 #include "chrome/browser/ui/browser_window.h" 55 #include "chrome/browser/ui/browser_window.h"
59 #include "chrome/browser/ui/chrome_pages.h" 56 #include "chrome/browser/ui/chrome_pages.h"
60 #include "chrome/browser/ui/host_desktop.h" 57 #include "chrome/browser/ui/host_desktop.h"
61 #include "chrome/browser/ui/tabs/tab_strip_model.h" 58 #include "chrome/browser/ui/tabs/tab_strip_model.h"
62 #include "chrome/common/chrome_paths.h" 59 #include "chrome/common/chrome_paths.h"
(...skipping 25 matching lines...) Expand all
88 #include "content/public/test/test_navigation_observer.h" 85 #include "content/public/test/test_navigation_observer.h"
89 #include "content/test/net/url_request_slow_download_job.h" 86 #include "content/test/net/url_request_slow_download_job.h"
90 #include "extensions/browser/extension_system.h" 87 #include "extensions/browser/extension_system.h"
91 #include "extensions/common/feature_switch.h" 88 #include "extensions/common/feature_switch.h"
92 #include "net/base/filename_util.h" 89 #include "net/base/filename_util.h"
93 #include "net/test/spawned_test_server/spawned_test_server.h" 90 #include "net/test/spawned_test_server/spawned_test_server.h"
94 #include "net/test/url_request/url_request_mock_http_job.h" 91 #include "net/test/url_request/url_request_mock_http_job.h"
95 #include "testing/gtest/include/gtest/gtest.h" 92 #include "testing/gtest/include/gtest/gtest.h"
96 #include "ui/base/l10n/l10n_util.h" 93 #include "ui/base/l10n/l10n_util.h"
97 94
95 #if defined(FULL_SAFE_BROWSING)
96 #include "chrome/browser/safe_browsing/download_feedback_service.h"
97 #include "chrome/browser/safe_browsing/download_protection_service.h"
98 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
99 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
100 #endif
101
98 using content::BrowserContext; 102 using content::BrowserContext;
99 using content::BrowserThread; 103 using content::BrowserThread;
100 using content::DownloadItem; 104 using content::DownloadItem;
101 using content::DownloadManager; 105 using content::DownloadManager;
102 using content::DownloadUrlParameters; 106 using content::DownloadUrlParameters;
103 using content::URLRequestSlowDownloadJob; 107 using content::URLRequestSlowDownloadJob;
104 using content::WebContents; 108 using content::WebContents;
105 using extensions::Extension; 109 using extensions::Extension;
106 using extensions::FeatureSwitch; 110 using extensions::FeatureSwitch;
107 using net::URLRequestMockHTTPJob; 111 using net::URLRequestMockHTTPJob;
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 download_manager, num_downloads); 569 download_manager, num_downloads);
566 } 570 }
567 571
568 // Create a DownloadTestObserverTerminal that will wait for the 572 // Create a DownloadTestObserverTerminal that will wait for the
569 // specified number of downloads to finish, or for 573 // specified number of downloads to finish, or for
570 // a dangerous download warning to be shown. 574 // a dangerous download warning to be shown.
571 content::DownloadTestObserver* DangerousDownloadWaiter( 575 content::DownloadTestObserver* DangerousDownloadWaiter(
572 Browser* browser, 576 Browser* browser,
573 int num_downloads, 577 int num_downloads,
574 content::DownloadTestObserver::DangerousDownloadAction 578 content::DownloadTestObserver::DangerousDownloadAction
575 dangerous_download_action) { 579 dangerous_download_action) {
576 DownloadManager* download_manager = DownloadManagerForBrowser(browser); 580 DownloadManager* download_manager = DownloadManagerForBrowser(browser);
577 return new content::DownloadTestObserverTerminal( 581 return new content::DownloadTestObserverTerminal(
578 download_manager, num_downloads, 582 download_manager, num_downloads, dangerous_download_action);
579 dangerous_download_action);
580 } 583 }
581 584
582 void CheckDownloadStatesForBrowser(Browser* browser, 585 void CheckDownloadStatesForBrowser(Browser* browser,
583 size_t num, 586 size_t num,
584 DownloadItem::DownloadState state) { 587 DownloadItem::DownloadState state) {
585 std::vector<DownloadItem*> download_items; 588 std::vector<DownloadItem*> download_items;
586 GetDownloads(browser, &download_items); 589 GetDownloads(browser, &download_items);
587 590
588 EXPECT_EQ(num, download_items.size()); 591 EXPECT_EQ(num, download_items.size());
589 592
(...skipping 1317 matching lines...) Expand 10 before | Expand all | Expand 10 after
1907 DownloadAndWait(browser(), download_url); 1910 DownloadAndWait(browser(), download_url);
1908 observer.WaitForStored(); 1911 observer.WaitForStored();
1909 HistoryServiceFactory::GetForProfile( 1912 HistoryServiceFactory::GetForProfile(
1910 browser()->profile(), Profile::IMPLICIT_ACCESS)->FlushForTest( 1913 browser()->profile(), Profile::IMPLICIT_ACCESS)->FlushForTest(
1911 base::Bind(&base::MessageLoop::Quit, 1914 base::Bind(&base::MessageLoop::Quit,
1912 base::Unretained(base::MessageLoop::current()->current()))); 1915 base::Unretained(base::MessageLoop::current()->current())));
1913 content::RunMessageLoop(); 1916 content::RunMessageLoop();
1914 } 1917 }
1915 1918
1916 #if defined(OS_CHROMEOS) 1919 #if defined(OS_CHROMEOS)
1920 // Times out on ChromeOS: http://crbug.com/217810
1917 #define MAYBE_DownloadTest_History DISABLED_DownloadTest_History 1921 #define MAYBE_DownloadTest_History DISABLED_DownloadTest_History
1918 #else 1922 #else
1919 #define MAYBE_DownloadTest_History DownloadTest_History 1923 #define MAYBE_DownloadTest_History DownloadTest_History
1920 #endif 1924 #endif
1921 IN_PROC_BROWSER_TEST_F(DownloadTest, MAYBE_DownloadTest_History) { 1925 IN_PROC_BROWSER_TEST_F(DownloadTest, MAYBE_DownloadTest_History) {
1922 // This starts up right after PRE_DownloadTest_History and shares the same 1926 // This starts up right after PRE_DownloadTest_History and shares the same
1923 // profile directory. 1927 // profile directory.
1924 base::FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1928 base::FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1925 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file)); 1929 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file));
1926 std::vector<DownloadItem*> downloads; 1930 std::vector<DownloadItem*> downloads;
(...skipping 1409 matching lines...) Expand 10 before | Expand all | Expand 10 after
3336 GURL url(test_server()->GetURL("files/downloads/empty.bin")); 3340 GURL url(test_server()->GetURL("files/downloads/empty.bin"));
3337 // Downloading the same URL twice causes the second request to be served from 3341 // Downloading the same URL twice causes the second request to be served from
3338 // cached (with a high probability). This test verifies that that doesn't 3342 // cached (with a high probability). This test verifies that that doesn't
3339 // happen regardless of whether the request is served via the cache or from 3343 // happen regardless of whether the request is served via the cache or from
3340 // the network. 3344 // the network.
3341 DownloadAndWait(browser(), url); 3345 DownloadAndWait(browser(), url);
3342 DownloadAndWait(browser(), url); 3346 DownloadAndWait(browser(), url);
3343 } 3347 }
3344 3348
3345 #if defined(FULL_SAFE_BROWSING) 3349 #if defined(FULL_SAFE_BROWSING)
3350
3351 // The following two tests are only meaningful on OS_WIN since that's the only
3352 // platform where client download checks are currently performed.
3353 // TODO(asanka): Relax this restriction as other platforms are added.
3354 #if defined(OS_WIN)
3355 namespace {
3356
3357 // This is a custom DownloadTestObserver for
3358 // DangerousFileWithSBDisabledBeforeCompletion test that disables the
3359 // SafeBrowsing service when a single download is IN_PROGRESS and has a target
3360 // path assigned. DownloadItemImpl is expected to call MaybeCompleteDownload
3361 // soon afterwards and we want to disable the service before then.
3362 class DisableSafeBrowsingOnInProgressDownload
3363 : public content::DownloadTestObserver {
3364 public:
3365 explicit DisableSafeBrowsingOnInProgressDownload(Browser* browser)
3366 : DownloadTestObserver(DownloadManagerForBrowser(browser),
3367 1,
3368 ON_DANGEROUS_DOWNLOAD_QUIT),
3369 browser_(browser),
3370 final_state_seen_(false) {
3371 Init();
3372 }
3373 virtual ~DisableSafeBrowsingOnInProgressDownload() {}
3374
3375 virtual bool IsDownloadInFinalState(DownloadItem* download) OVERRIDE {
3376 if (download->GetState() != DownloadItem::IN_PROGRESS ||
3377 download->GetTargetFilePath().empty())
3378 return false;
3379
3380 if (final_state_seen_)
3381 return true;
3382
3383 final_state_seen_ = true;
3384 browser_->profile()->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled,
3385 false);
3386 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
3387 download->GetDangerType());
3388 EXPECT_FALSE(download->IsDangerous());
3389 EXPECT_TRUE(DownloadItemModel(download).IsDangerousFileBasedOnType());
3390 return true;
3391 }
3392
3393 private:
3394 Browser* browser_;
3395 bool final_state_seen_;
3396 };
3397
3398 } // namespace
3399
3400 IN_PROC_BROWSER_TEST_F(DownloadTest,
3401 DangerousFileWithSBDisabledBeforeCompletion) {
3402 browser()->profile()->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled,
3403 true);
3404 ASSERT_TRUE(test_server()->Start());
3405 GURL download_url(
3406 test_server()->GetURL("files/downloads/dangerous/dangerous.exe"));
3407 scoped_ptr<content::DownloadTestObserver> dangerous_observer(
3408 DangerousDownloadWaiter(
3409 browser(),
3410 1,
3411 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT));
3412 scoped_ptr<content::DownloadTestObserver> in_progress_observer(
3413 new DisableSafeBrowsingOnInProgressDownload(browser()));
3414 ui_test_utils::NavigateToURLWithDisposition(browser(),
3415 download_url,
3416 NEW_BACKGROUND_TAB,
3417 ui_test_utils::BROWSER_TEST_NONE);
3418 in_progress_observer->WaitForFinished();
3419
3420 // SafeBrowsing should have been disabled by our observer.
3421 ASSERT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
3422 prefs::kSafeBrowsingEnabled));
3423
3424 std::vector<DownloadItem*> downloads;
3425 DownloadManagerForBrowser(browser())->GetAllDownloads(&downloads);
3426 ASSERT_EQ(1u, downloads.size());
3427 DownloadItem* download = downloads[0];
3428
3429 dangerous_observer->WaitForFinished();
3430
3431 EXPECT_TRUE(download->IsDangerous());
3432 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
3433 download->GetDangerType());
3434 download->Cancel(true);
3435 }
3436
3437 IN_PROC_BROWSER_TEST_F(DownloadTest, DangerousFileWithSBDisabledBeforeStart) {
3438 // Disable SafeBrowsing
3439 browser()->profile()->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled,
3440 false);
3441
3442 ASSERT_TRUE(test_server()->Start());
3443 GURL download_url(
3444 test_server()->GetURL("files/downloads/dangerous/dangerous.exe"));
3445 scoped_ptr<content::DownloadTestObserver> dangerous_observer(
3446 DangerousDownloadWaiter(
3447 browser(),
3448 1,
3449 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT));
3450 ui_test_utils::NavigateToURLWithDisposition(browser(),
3451 download_url,
3452 NEW_BACKGROUND_TAB,
3453 ui_test_utils::BROWSER_TEST_NONE);
3454 dangerous_observer->WaitForFinished();
3455
3456 std::vector<DownloadItem*> downloads;
3457 DownloadManagerForBrowser(browser())->GetAllDownloads(&downloads);
3458 ASSERT_EQ(1u, downloads.size());
3459
3460 DownloadItem* download = downloads[0];
3461 EXPECT_TRUE(download->IsDangerous());
3462 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
3463 download->GetDangerType());
3464
3465 download->Cancel(true);
3466 }
3467
3468 IN_PROC_BROWSER_TEST_F(DownloadTest, SafeSupportedFile) {
3469 ASSERT_TRUE(test_server()->Start());
3470 GURL download_url(test_server()->GetURL("files/downloads/a_zip_file.zip"));
3471 DownloadAndWait(browser(), download_url);
3472
3473 std::vector<DownloadItem*> downloads;
3474 DownloadManagerForBrowser(browser())->GetAllDownloads(&downloads);
3475 ASSERT_EQ(1u, downloads.size());
3476
3477 DownloadItem* download = downloads[0];
3478 EXPECT_FALSE(download->IsDangerous());
3479 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
3480 download->GetDangerType());
3481
3482 download->Cancel(true);
3483 }
3484
3485 #endif // OS_WIN
3486
3346 IN_PROC_BROWSER_TEST_F(DownloadTest, FeedbackService) { 3487 IN_PROC_BROWSER_TEST_F(DownloadTest, FeedbackService) {
3347 // Make a dangerous file. 3488 // Make a dangerous file.
3348 base::FilePath file(FILE_PATH_LITERAL("downloads/dangerous/dangerous.swf")); 3489 base::FilePath file(FILE_PATH_LITERAL("downloads/dangerous/dangerous.swf"));
3349 GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl(file)); 3490 GURL download_url(net::URLRequestMockHTTPJob::GetMockUrl(file));
3350 scoped_ptr<content::DownloadTestObserverInterrupted> observer( 3491 scoped_ptr<content::DownloadTestObserverInterrupted> observer(
3351 new content::DownloadTestObserverInterrupted( 3492 new content::DownloadTestObserverInterrupted(
3352 DownloadManagerForBrowser(browser()), 1, 3493 DownloadManagerForBrowser(browser()), 1,
3353 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT)); 3494 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT));
3354 ui_test_utils::NavigateToURLWithDisposition( 3495 ui_test_utils::NavigateToURLWithDisposition(
3355 browser(), 3496 browser(),
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
3387 *(downloads[0]))); 3528 *(downloads[0])));
3388 3529
3389 // Begin feedback and check that the file is "stolen". 3530 // Begin feedback and check that the file is "stolen".
3390 download_protection_service->feedback_service()->BeginFeedbackForDownload( 3531 download_protection_service->feedback_service()->BeginFeedbackForDownload(
3391 downloads[0]); 3532 downloads[0]);
3392 std::vector<DownloadItem*> updated_downloads; 3533 std::vector<DownloadItem*> updated_downloads;
3393 GetDownloads(browser(), &updated_downloads); 3534 GetDownloads(browser(), &updated_downloads);
3394 ASSERT_TRUE(updated_downloads.empty()); 3535 ASSERT_TRUE(updated_downloads.empty());
3395 } 3536 }
3396 #endif 3537 #endif
OLDNEW
« no previous file with comments | « chrome/browser/download/chrome_download_manager_delegate.cc ('k') | chrome/browser/download/download_item_model.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698