Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "chrome/browser/download/download_browsertest.h" | 5 #include "chrome/browser/download/download_browsertest.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
| 26 #include "base/strings/stringprintf.h" | 26 #include "base/strings/stringprintf.h" |
| 27 #include "base/strings/utf_string_conversions.h" | 27 #include "base/strings/utf_string_conversions.h" |
| 28 #include "base/sys_info.h" | 28 #include "base/sys_info.h" |
| 29 #include "base/test/test_file_util.h" | 29 #include "base/test/test_file_util.h" |
| 30 #include "build/build_config.h" | 30 #include "build/build_config.h" |
| 31 #include "chrome/app/chrome_command_ids.h" | 31 #include "chrome/app/chrome_command_ids.h" |
| 32 #include "chrome/browser/browser_process.h" | 32 #include "chrome/browser/browser_process.h" |
| 33 #include "chrome/browser/chrome_notification_types.h" | 33 #include "chrome/browser/chrome_notification_types.h" |
| 34 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 34 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 35 #include "chrome/browser/download/download_commands.h" | |
| 35 #include "chrome/browser/download/download_crx_util.h" | 36 #include "chrome/browser/download/download_crx_util.h" |
| 36 #include "chrome/browser/download/download_history.h" | 37 #include "chrome/browser/download/download_history.h" |
| 37 #include "chrome/browser/download/download_item_model.h" | 38 #include "chrome/browser/download/download_item_model.h" |
| 38 #include "chrome/browser/download/download_prefs.h" | 39 #include "chrome/browser/download/download_prefs.h" |
| 39 #include "chrome/browser/download/download_request_limiter.h" | 40 #include "chrome/browser/download/download_request_limiter.h" |
| 40 #include "chrome/browser/download/download_service.h" | 41 #include "chrome/browser/download/download_service.h" |
| 41 #include "chrome/browser/download/download_service_factory.h" | 42 #include "chrome/browser/download/download_service_factory.h" |
| 42 #include "chrome/browser/download/download_shelf.h" | 43 #include "chrome/browser/download/download_shelf.h" |
| 43 #include "chrome/browser/download/download_target_determiner.h" | 44 #include "chrome/browser/download/download_target_determiner.h" |
| 44 #include "chrome/browser/download/download_test_file_activity_observer.h" | 45 #include "chrome/browser/download/download_test_file_activity_observer.h" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 68 #include "chrome/test/base/in_process_browser_test.h" | 69 #include "chrome/test/base/in_process_browser_test.h" |
| 69 #include "chrome/test/base/test_switches.h" | 70 #include "chrome/test/base/test_switches.h" |
| 70 #include "chrome/test/base/ui_test_utils.h" | 71 #include "chrome/test/base/ui_test_utils.h" |
| 71 #include "components/history/content/browser/download_constants_utils.h" | 72 #include "components/history/content/browser/download_constants_utils.h" |
| 72 #include "components/history/core/browser/download_constants.h" | 73 #include "components/history/core/browser/download_constants.h" |
| 73 #include "components/history/core/browser/download_row.h" | 74 #include "components/history/core/browser/download_row.h" |
| 74 #include "components/history/core/browser/history_service.h" | 75 #include "components/history/core/browser/history_service.h" |
| 75 #include "components/infobars/core/confirm_infobar_delegate.h" | 76 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 76 #include "components/infobars/core/infobar.h" | 77 #include "components/infobars/core/infobar.h" |
| 77 #include "components/prefs/pref_service.h" | 78 #include "components/prefs/pref_service.h" |
| 79 #include "content/public/browser/download_danger_type.h" | |
| 78 #include "content/public/browser/download_interrupt_reasons.h" | 80 #include "content/public/browser/download_interrupt_reasons.h" |
| 79 #include "content/public/browser/download_item.h" | 81 #include "content/public/browser/download_item.h" |
| 80 #include "content/public/browser/download_manager.h" | 82 #include "content/public/browser/download_manager.h" |
| 81 #include "content/public/browser/download_save_info.h" | 83 #include "content/public/browser/download_save_info.h" |
| 82 #include "content/public/browser/download_url_parameters.h" | 84 #include "content/public/browser/download_url_parameters.h" |
| 83 #include "content/public/browser/notification_source.h" | 85 #include "content/public/browser/notification_source.h" |
| 84 #include "content/public/browser/render_frame_host.h" | 86 #include "content/public/browser/render_frame_host.h" |
| 85 #include "content/public/browser/render_view_host.h" | 87 #include "content/public/browser/render_view_host.h" |
| 86 #include "content/public/browser/render_widget_host.h" | 88 #include "content/public/browser/render_widget_host.h" |
| 87 #include "content/public/browser/resource_context.h" | 89 #include "content/public/browser/resource_context.h" |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 381 | 383 |
| 382 private: | 384 private: |
| 383 Profile* profile_; | 385 Profile* profile_; |
| 384 bool waiting_; | 386 bool waiting_; |
| 385 bool seen_stored_; | 387 bool seen_stored_; |
| 386 FilterCallback callback_; | 388 FilterCallback callback_; |
| 387 | 389 |
| 388 DISALLOW_COPY_AND_ASSIGN(HistoryObserver); | 390 DISALLOW_COPY_AND_ASSIGN(HistoryObserver); |
| 389 }; | 391 }; |
| 390 | 392 |
| 393 class FakeSafeBrowsingService : public safe_browsing::SafeBrowsingService { | |
|
asanka
2016/03/14 18:13:46
This the 7th FakeSafeBrowsingService :-(. Not nece
Jialiu Lin
2016/03/14 23:54:04
Totally agree. Bug created at https://crbug.com/59
| |
| 394 public: | |
| 395 FakeSafeBrowsingService() {} | |
| 396 | |
| 397 std::string GetDownloadReport() const { return report_; } | |
| 398 | |
| 399 protected: | |
| 400 ~FakeSafeBrowsingService() override {} | |
| 401 | |
| 402 void SendSerializedDownloadReport(const std::string& report) override { | |
| 403 // Disable download protection to prevent flackiness. | |
|
asanka
2016/03/14 18:13:46
*flakiness.
Also, you'll need to explain how disa
Jialiu Lin
2016/03/14 23:54:04
No need of this line since FakeDownloadProtectionS
| |
| 404 download_protection_service()->SetEnabled(false); | |
| 405 report_ = report; | |
| 406 } | |
| 407 | |
| 408 std::string report_; | |
| 409 }; | |
| 410 | |
| 411 // Factory that creates FakeSafeBrowsingService instances. | |
| 412 class TestSafeBrowsingServiceFactory | |
| 413 : public safe_browsing::SafeBrowsingServiceFactory { | |
| 414 public: | |
| 415 TestSafeBrowsingServiceFactory() : fake_safe_browsing_service_(nullptr) {} | |
| 416 ~TestSafeBrowsingServiceFactory() override {} | |
| 417 | |
| 418 safe_browsing::SafeBrowsingService* CreateSafeBrowsingService() override { | |
| 419 if (!fake_safe_browsing_service_) { | |
|
asanka
2016/03/14 18:13:46
This function should be getting called at most onc
Jialiu Lin
2016/03/14 23:54:04
Done.
| |
| 420 fake_safe_browsing_service_ = new FakeSafeBrowsingService(); | |
| 421 } | |
| 422 return fake_safe_browsing_service_.get(); | |
| 423 } | |
| 424 | |
| 425 scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service() { | |
| 426 return fake_safe_browsing_service_; | |
| 427 } | |
| 428 | |
| 429 private: | |
| 430 scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service_; | |
| 431 }; | |
| 432 | |
| 391 class DownloadTest : public InProcessBrowserTest { | 433 class DownloadTest : public InProcessBrowserTest { |
| 392 public: | 434 public: |
| 393 // Choice of navigation or direct fetch. Used by |DownloadFileCheckErrors()|. | 435 // Choice of navigation or direct fetch. Used by |DownloadFileCheckErrors()|. |
| 394 enum DownloadMethod { | 436 enum DownloadMethod { |
| 395 DOWNLOAD_NAVIGATE, | 437 DOWNLOAD_NAVIGATE, |
| 396 DOWNLOAD_DIRECT | 438 DOWNLOAD_DIRECT |
| 397 }; | 439 }; |
| 398 | 440 |
| 399 // Information passed in to |DownloadFileCheckErrors()|. | 441 // Information passed in to |DownloadFileCheckErrors()|. |
| 400 struct DownloadInfo { | 442 struct DownloadInfo { |
| 401 const char* starting_url; // URL for initiating the download. | 443 const char* starting_url; // URL for initiating the download. |
| 402 const char* expected_download_url; // Expected value of DI::GetURL(). Can | 444 const char* expected_download_url; // Expected value of DI::GetURL(). Can |
| 403 // be different if |starting_url| | 445 // be different if |starting_url| |
| 404 // initiates a download from another | 446 // initiates a download from another |
| 405 // URL. | 447 // URL. |
| 406 DownloadMethod download_method; // Navigation or Direct. | 448 DownloadMethod download_method; // Navigation or Direct. |
| 407 // Download interrupt reason (NONE is OK). | 449 // Download interrupt reason (NONE is OK). |
| 408 content::DownloadInterruptReason reason; | 450 content::DownloadInterruptReason reason; |
| 409 bool show_download_item; // True if the download item appears on the shelf. | 451 bool show_download_item; // True if the download item appears on the shelf. |
| 410 bool should_redirect_to_documents; // True if we save it in "My Documents". | 452 bool should_redirect_to_documents; // True if we save it in "My Documents". |
| 411 }; | 453 }; |
| 412 | 454 |
| 413 struct FileErrorInjectInfo { | 455 struct FileErrorInjectInfo { |
| 414 DownloadInfo download_info; | 456 DownloadInfo download_info; |
| 415 content::TestFileErrorInjector::FileErrorInfo error_info; | 457 content::TestFileErrorInjector::FileErrorInfo error_info; |
| 416 }; | 458 }; |
| 417 | 459 |
| 418 DownloadTest() {} | 460 DownloadTest() |
| 461 : test_safe_browsing_factory_(new TestSafeBrowsingServiceFactory()) {} | |
| 462 | |
| 463 void SetUp() override { | |
| 464 safe_browsing::SafeBrowsingService::RegisterFactory( | |
| 465 test_safe_browsing_factory_.get()); | |
| 466 InProcessBrowserTest::SetUp(); | |
| 467 } | |
| 468 | |
| 469 void TearDown() override { | |
| 470 safe_browsing::SafeBrowsingService::RegisterFactory(nullptr); | |
| 471 InProcessBrowserTest::TearDown(); | |
| 472 } | |
| 419 | 473 |
| 420 void SetUpOnMainThread() override { | 474 void SetUpOnMainThread() override { |
| 421 base::FeatureList::ClearInstanceForTesting(); | 475 base::FeatureList::ClearInstanceForTesting(); |
| 422 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList); | 476 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 423 feature_list->InitializeFromCommandLine( | 477 feature_list->InitializeFromCommandLine( |
| 424 features::kDownloadResumption.name, std::string()); | 478 features::kDownloadResumption.name, std::string()); |
| 425 base::FeatureList::SetInstance(std::move(feature_list)); | 479 base::FeatureList::SetInstance(std::move(feature_list)); |
| 426 BrowserThread::PostTask( | 480 BrowserThread::PostTask( |
| 427 BrowserThread::IO, FROM_HERE, | 481 BrowserThread::IO, FROM_HERE, |
| 428 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 482 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| (...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1052 if (downloads.size() != 1) | 1106 if (downloads.size() != 1) |
| 1053 return NULL; | 1107 return NULL; |
| 1054 | 1108 |
| 1055 error_injector->ClearError(); | 1109 error_injector->ClearError(); |
| 1056 DownloadItem* download = downloads[0]; | 1110 DownloadItem* download = downloads[0]; |
| 1057 EXPECT_EQ(DownloadItem::INTERRUPTED, download->GetState()); | 1111 EXPECT_EQ(DownloadItem::INTERRUPTED, download->GetState()); |
| 1058 EXPECT_EQ(error, download->GetLastReason()); | 1112 EXPECT_EQ(error, download->GetLastReason()); |
| 1059 return download; | 1113 return download; |
| 1060 } | 1114 } |
| 1061 | 1115 |
| 1116 protected: | |
| 1117 scoped_ptr<TestSafeBrowsingServiceFactory> test_safe_browsing_factory_; | |
| 1118 | |
| 1062 private: | 1119 private: |
| 1063 static void EnsureNoPendingDownloadJobsOnIO(bool* result) { | 1120 static void EnsureNoPendingDownloadJobsOnIO(bool* result) { |
| 1064 if (net::URLRequestSlowDownloadJob::NumberOutstandingRequests()) | 1121 if (net::URLRequestSlowDownloadJob::NumberOutstandingRequests()) |
| 1065 *result = false; | 1122 *result = false; |
| 1066 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 1123 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 1067 base::MessageLoop::QuitWhenIdleClosure()); | 1124 base::MessageLoop::QuitWhenIdleClosure()); |
| 1068 } | 1125 } |
| 1069 | 1126 |
| 1070 // Location of the test data. | 1127 // Location of the test data. |
| 1071 base::FilePath test_dir_; | 1128 base::FilePath test_dir_; |
| (...skipping 2191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3263 ASSERT_TRUE(safe_browsing::DownloadFeedbackService::IsEnabledForDownload( | 3320 ASSERT_TRUE(safe_browsing::DownloadFeedbackService::IsEnabledForDownload( |
| 3264 *(downloads[0]))); | 3321 *(downloads[0]))); |
| 3265 | 3322 |
| 3266 // Begin feedback and check that the file is "stolen". | 3323 // Begin feedback and check that the file is "stolen". |
| 3267 download_protection_service->feedback_service()->BeginFeedbackForDownload( | 3324 download_protection_service->feedback_service()->BeginFeedbackForDownload( |
| 3268 downloads[0]); | 3325 downloads[0]); |
| 3269 std::vector<DownloadItem*> updated_downloads; | 3326 std::vector<DownloadItem*> updated_downloads; |
| 3270 GetDownloads(browser(), &updated_downloads); | 3327 GetDownloads(browser(), &updated_downloads); |
| 3271 ASSERT_TRUE(updated_downloads.empty()); | 3328 ASSERT_TRUE(updated_downloads.empty()); |
| 3272 } | 3329 } |
| 3330 | |
| 3331 IN_PROC_BROWSER_TEST_F(DownloadTest, SendUncommonDownloadAcceptReport) { | |
| 3332 browser()->profile()->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled, | |
| 3333 true); | |
| 3334 // Make a dangerous file. | |
| 3335 GURL download_url( | |
| 3336 net::URLRequestMockHTTPJob::GetMockUrl(kDangerousMockFilePath)); | |
| 3337 scoped_ptr<content::DownloadTestObserver> dangerous_observer( | |
| 3338 DangerousDownloadWaiter( | |
| 3339 browser(), 1, | |
| 3340 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT)); | |
| 3341 scoped_ptr<content::DownloadTestObserver> in_progress_observer( | |
| 3342 new content::DownloadTestObserverInProgress( | |
| 3343 DownloadManagerForBrowser(browser()), 1)); | |
| 3344 | |
| 3345 ui_test_utils::NavigateToURL(browser(), download_url); | |
| 3346 in_progress_observer->WaitForFinished(); | |
| 3347 | |
| 3348 std::vector<DownloadItem*> downloads; | |
| 3349 DownloadManagerForBrowser(browser())->GetAllDownloads(&downloads); | |
| 3350 ASSERT_EQ(1u, downloads.size()); | |
| 3351 DownloadItem* download = downloads[0]; | |
| 3352 // Simulates an uncommon download. | |
| 3353 download->OnContentCheckCompleted( | |
|
asanka
2016/03/14 18:13:46
OnContentCheckCompleted cannot be issued until all
Jialiu Lin
2016/03/14 23:54:04
implemented based on option 1 here. Now , no need
| |
| 3354 content::DownloadDangerType::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT); | |
| 3355 ASSERT_EQ(download->GetDangerType(), | |
| 3356 content::DownloadDangerType::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT); | |
| 3357 DownloadCommands(download).ExecuteCommand(DownloadCommands::KEEP); | |
| 3358 dangerous_observer->WaitForFinished(); | |
| 3359 | |
| 3360 safe_browsing::ClientSafeBrowsingReportRequest actual_report; | |
| 3361 actual_report.ParseFromString( | |
| 3362 test_safe_browsing_factory_->fake_safe_browsing_service() | |
| 3363 ->GetDownloadReport()); | |
| 3364 EXPECT_EQ(safe_browsing::ClientSafeBrowsingReportRequest:: | |
| 3365 DANGEROUS_DOWNLOAD_WARNING, | |
| 3366 actual_report.type()); | |
| 3367 EXPECT_EQ(safe_browsing::ClientDownloadResponse::UNCOMMON, | |
| 3368 actual_report.download_verdict()); | |
| 3369 EXPECT_EQ(download->GetURL().spec(), actual_report.url()); | |
| 3370 EXPECT_TRUE(actual_report.did_proceed()); | |
| 3371 | |
| 3372 download->Cancel(true); | |
| 3373 } | |
| 3273 #endif // FULL_SAFE_BROWSING | 3374 #endif // FULL_SAFE_BROWSING |
| 3274 | 3375 |
| 3275 class DownloadTestWithShelf : public DownloadTest { | 3376 class DownloadTestWithShelf : public DownloadTest { |
| 3276 void SetUpCommandLine(base::CommandLine* command_line) override { | 3377 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 3277 #if defined(OS_CHROMEOS) | 3378 #if defined(OS_CHROMEOS) |
| 3278 command_line->AppendSwitch(switches::kDisableDownloadNotification); | 3379 command_line->AppendSwitch(switches::kDisableDownloadNotification); |
| 3279 #endif | 3380 #endif |
| 3280 DownloadTest::SetUpCommandLine(command_line); | 3381 DownloadTest::SetUpCommandLine(command_line); |
| 3281 } | 3382 } |
| 3282 }; | 3383 }; |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3525 | 3626 |
| 3526 scoped_ptr<content::DownloadTestObserver> observer(DangerousDownloadWaiter( | 3627 scoped_ptr<content::DownloadTestObserver> observer(DangerousDownloadWaiter( |
| 3527 browser(), 1, content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_DENY)); | 3628 browser(), 1, content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_DENY)); |
| 3528 ui_test_utils::NavigateToURL(browser(), extension_url); | 3629 ui_test_utils::NavigateToURL(browser(), extension_url); |
| 3529 | 3630 |
| 3530 observer->WaitForFinished(); | 3631 observer->WaitForFinished(); |
| 3531 | 3632 |
| 3532 // Download shelf should close. | 3633 // Download shelf should close. |
| 3533 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); | 3634 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); |
| 3534 } | 3635 } |
| OLD | NEW |