| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "chrome/browser/download/download_danger_prompt.h" | 7 #include "chrome/browser/download/download_danger_prompt.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/safe_browsing/database_manager.h" |
| 10 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 9 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/browser_commands.h" | 12 #include "chrome/browser/ui/browser_commands.h" |
| 11 #include "chrome/browser/ui/browser_tabstrip.h" | 13 #include "chrome/browser/ui/browser_tabstrip.h" |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/common/safe_browsing/csd.pb.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "content/public/test/mock_download_item.h" | 18 #include "content/public/test/mock_download_item.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 19 | 22 |
| 20 using ::testing::_; | 23 using ::testing::_; |
| 21 using ::testing::ByRef; | 24 using ::testing::ByRef; |
| 22 using ::testing::Eq; | 25 using ::testing::Eq; |
| 23 using ::testing::Return; | 26 using ::testing::Return; |
| 24 using ::testing::ReturnRef; | 27 using ::testing::ReturnRef; |
| 25 using ::testing::SaveArg; | 28 using ::testing::SaveArg; |
| 29 using safe_browsing::ClientSafeBrowsingReportRequest; |
| 30 using safe_browsing::SafeBrowsingService; |
| 31 |
| 32 const char kTestDownloadUrl[] = "http://evildownload.com"; |
| 33 |
| 34 class FakeSafeBrowsingService : public SafeBrowsingService { |
| 35 public: |
| 36 FakeSafeBrowsingService() {} |
| 37 |
| 38 void SendDownloadRecoveryReport(const std::string& report) override { |
| 39 report_ = report; |
| 40 } |
| 41 |
| 42 std::string GetDownloadRecoveryReport() const { return report_; } |
| 43 |
| 44 protected: |
| 45 ~FakeSafeBrowsingService() override {} |
| 46 |
| 47 private: |
| 48 std::string report_; |
| 49 }; |
| 50 |
| 51 // Factory that creates FakeSafeBrowsingService instances. |
| 52 class TestSafeBrowsingServiceFactory |
| 53 : public safe_browsing::SafeBrowsingServiceFactory { |
| 54 public: |
| 55 TestSafeBrowsingServiceFactory() : fake_safe_browsing_service_(nullptr) {} |
| 56 ~TestSafeBrowsingServiceFactory() override {} |
| 57 |
| 58 SafeBrowsingService* CreateSafeBrowsingService() override { |
| 59 if (!fake_safe_browsing_service_) { |
| 60 fake_safe_browsing_service_ = new FakeSafeBrowsingService(); |
| 61 } |
| 62 return fake_safe_browsing_service_.get(); |
| 63 } |
| 64 |
| 65 scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service() { |
| 66 return fake_safe_browsing_service_; |
| 67 } |
| 68 |
| 69 private: |
| 70 scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service_; |
| 71 }; |
| 26 | 72 |
| 27 class DownloadDangerPromptTest : public InProcessBrowserTest { | 73 class DownloadDangerPromptTest : public InProcessBrowserTest { |
| 28 public: | 74 public: |
| 29 DownloadDangerPromptTest() | 75 DownloadDangerPromptTest() |
| 30 : prompt_(NULL), | 76 : prompt_(nullptr), |
| 31 expected_action_(DownloadDangerPrompt::CANCEL), | 77 expected_action_(DownloadDangerPrompt::CANCEL), |
| 32 did_receive_callback_(false) { | 78 did_receive_callback_(false), |
| 79 test_safe_browsing_factory_(new TestSafeBrowsingServiceFactory()), |
| 80 report_sent_(false) {} |
| 81 |
| 82 ~DownloadDangerPromptTest() override {} |
| 83 |
| 84 void SetUp() override { |
| 85 SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get()); |
| 86 InProcessBrowserTest::SetUp(); |
| 33 } | 87 } |
| 34 | 88 |
| 35 ~DownloadDangerPromptTest() override {} | 89 void TearDown() override { |
| 90 SafeBrowsingService::RegisterFactory(nullptr); |
| 91 InProcessBrowserTest::TearDown(); |
| 92 } |
| 36 | 93 |
| 37 // Opens a new tab and waits for navigations to finish. If there are pending | 94 // Opens a new tab and waits for navigations to finish. If there are pending |
| 38 // navigations, the constrained prompt might be dismissed when the navigation | 95 // navigations, the constrained prompt might be dismissed when the navigation |
| 39 // completes. | 96 // completes. |
| 40 void OpenNewTab() { | 97 void OpenNewTab() { |
| 41 ui_test_utils::NavigateToURLWithDisposition( | 98 ui_test_utils::NavigateToURLWithDisposition( |
| 42 browser(), GURL("about:blank"), | 99 browser(), GURL("about:blank"), |
| 43 NEW_FOREGROUND_TAB, | 100 NEW_FOREGROUND_TAB, |
| 44 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | 101 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | |
| 45 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 102 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 46 } | 103 } |
| 47 | 104 |
| 48 void SetUpExpectations(DownloadDangerPrompt::Action expected_action) { | 105 void SetUpExpectations(DownloadDangerPrompt::Action expected_action) { |
| 49 did_receive_callback_ = false; | 106 did_receive_callback_ = false; |
| 50 expected_action_ = expected_action; | 107 expected_action_ = expected_action; |
| 51 SetUpDownloadItemExpectations(); | 108 SetUpDownloadItemExpectations(); |
| 109 SetUpSafeBrowsingReportExpectations(expected_action == |
| 110 DownloadDangerPrompt::ACCEPT); |
| 52 CreatePrompt(); | 111 CreatePrompt(); |
| 112 report_sent_ = false; |
| 53 } | 113 } |
| 54 | 114 |
| 55 void VerifyExpectations() { | 115 void VerifyExpectations() { |
| 56 content::RunAllPendingInMessageLoop(); | 116 content::RunAllPendingInMessageLoop(); |
| 57 // At the end of each test, we expect no more activity from the prompt. The | 117 // At the end of each test, we expect no more activity from the prompt. The |
| 58 // prompt shouldn't exist anymore either. | 118 // prompt shouldn't exist anymore either. |
| 59 EXPECT_TRUE(did_receive_callback_); | 119 EXPECT_TRUE(did_receive_callback_); |
| 60 EXPECT_FALSE(prompt_); | 120 EXPECT_FALSE(prompt_); |
| 61 testing::Mock::VerifyAndClearExpectations(&download_); | 121 testing::Mock::VerifyAndClearExpectations(&download_); |
| 122 if (report_sent_) { |
| 123 EXPECT_EQ(expected_serialized_report_, |
| 124 test_safe_browsing_factory_->fake_safe_browsing_service() |
| 125 ->GetDownloadRecoveryReport()); |
| 126 } |
| 62 } | 127 } |
| 63 | 128 |
| 64 void SimulatePromptAction(DownloadDangerPrompt::Action action) { | 129 void SimulatePromptAction(DownloadDangerPrompt::Action action) { |
| 65 prompt_->InvokeActionForTesting(action); | 130 prompt_->InvokeActionForTesting(action); |
| 131 report_sent_ = true; |
| 66 } | 132 } |
| 67 | 133 |
| 68 content::MockDownloadItem& download() { return download_; } | 134 content::MockDownloadItem& download() { return download_; } |
| 69 | 135 |
| 70 DownloadDangerPrompt* prompt() { return prompt_; } | 136 DownloadDangerPrompt* prompt() { return prompt_; } |
| 71 | 137 |
| 72 private: | 138 private: |
| 73 void SetUpDownloadItemExpectations() { | 139 void SetUpDownloadItemExpectations() { |
| 74 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( | 140 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( |
| 75 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); | 141 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); |
| 76 EXPECT_CALL(download_, GetDangerType()) | 142 EXPECT_CALL(download_, GetDangerType()) |
| 77 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL)); | 143 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL)); |
| 78 } | 144 } |
| 79 | 145 |
| 146 void SetUpSafeBrowsingReportExpectations(bool did_proceed) { |
| 147 ClientSafeBrowsingReportRequest expected_report; |
| 148 expected_report.set_url(GURL(kTestDownloadUrl).spec()); |
| 149 expected_report.set_type( |
| 150 ClientSafeBrowsingReportRequest::MALICIOUS_DOWNLOAD_RECOVERY); |
| 151 expected_report.set_did_proceed(did_proceed); |
| 152 expected_report.SerializeToString(&expected_serialized_report_); |
| 153 } |
| 154 |
| 80 void CreatePrompt() { | 155 void CreatePrompt() { |
| 81 prompt_ = DownloadDangerPrompt::Create( | 156 prompt_ = DownloadDangerPrompt::Create( |
| 82 &download_, | 157 &download_, |
| 83 browser()->tab_strip_model()->GetActiveWebContents(), | 158 browser()->tab_strip_model()->GetActiveWebContents(), |
| 84 false, | 159 false, |
| 85 base::Bind(&DownloadDangerPromptTest::PromptCallback, this)); | 160 base::Bind(&DownloadDangerPromptTest::PromptCallback, this)); |
| 86 content::RunAllPendingInMessageLoop(); | 161 content::RunAllPendingInMessageLoop(); |
| 87 } | 162 } |
| 88 | 163 |
| 89 void PromptCallback(DownloadDangerPrompt::Action action) { | 164 void PromptCallback(DownloadDangerPrompt::Action action) { |
| 90 EXPECT_FALSE(did_receive_callback_); | 165 EXPECT_FALSE(did_receive_callback_); |
| 91 EXPECT_EQ(expected_action_, action); | 166 EXPECT_EQ(expected_action_, action); |
| 92 did_receive_callback_ = true; | 167 did_receive_callback_ = true; |
| 93 prompt_ = NULL; | 168 prompt_ = nullptr; |
| 94 } | 169 } |
| 95 | 170 |
| 96 content::MockDownloadItem download_; | 171 content::MockDownloadItem download_; |
| 97 DownloadDangerPrompt* prompt_; | 172 DownloadDangerPrompt* prompt_; |
| 98 DownloadDangerPrompt::Action expected_action_; | 173 DownloadDangerPrompt::Action expected_action_; |
| 99 bool did_receive_callback_; | 174 bool did_receive_callback_; |
| 175 scoped_ptr<TestSafeBrowsingServiceFactory> test_safe_browsing_factory_; |
| 176 std::string expected_serialized_report_; |
| 177 bool report_sent_; |
| 100 | 178 |
| 101 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); | 179 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); |
| 102 }; | 180 }; |
| 103 | 181 |
| 104 // Disabled for flaky timeouts on Windows. crbug.com/446696 | 182 // Disabled for flaky timeouts on Windows. crbug.com/446696 |
| 105 #if defined(OS_WIN) | 183 #if defined(OS_WIN) |
| 106 #define MAYBE_TestAll DISABLED_TestAll | 184 #define MAYBE_TestAll DISABLED_TestAll |
| 107 #else | 185 #else |
| 108 #define MAYBE_TestAll TestAll | 186 #define MAYBE_TestAll TestAll |
| 109 #endif | 187 #endif |
| 110 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) { | 188 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) { |
| 111 // ExperienceSampling: Set default actions for DownloadItem methods we need. | 189 // ExperienceSampling: Set default actions for DownloadItem methods we need. |
| 112 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(GURL::EmptyGURL())); | 190 GURL download_url(kTestDownloadUrl); |
| 191 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(download_url)); |
| 113 ON_CALL(download(), GetReferrerUrl()) | 192 ON_CALL(download(), GetReferrerUrl()) |
| 114 .WillByDefault(ReturnRef(GURL::EmptyGURL())); | 193 .WillByDefault(ReturnRef(GURL::EmptyGURL())); |
| 115 ON_CALL(download(), GetBrowserContext()) | 194 ON_CALL(download(), GetBrowserContext()) |
| 116 .WillByDefault(Return(browser()->profile())); | 195 .WillByDefault(Return(browser()->profile())); |
| 117 | 196 |
| 118 OpenNewTab(); | 197 OpenNewTab(); |
| 119 | 198 |
| 120 // Clicking the Accept button should invoke the ACCEPT action. | 199 // Clicking the Accept button should invoke the ACCEPT action. |
| 121 SetUpExpectations(DownloadDangerPrompt::ACCEPT); | 200 SetUpExpectations(DownloadDangerPrompt::ACCEPT); |
| 122 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); | 201 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 150 download().NotifyObserversDownloadUpdated(); | 229 download().NotifyObserversDownloadUpdated(); |
| 151 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); | 230 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
| 152 VerifyExpectations(); | 231 VerifyExpectations(); |
| 153 | 232 |
| 154 // If the containing tab is closed, the dialog should DISMISS itself. | 233 // If the containing tab is closed, the dialog should DISMISS itself. |
| 155 OpenNewTab(); | 234 OpenNewTab(); |
| 156 SetUpExpectations(DownloadDangerPrompt::DISMISS); | 235 SetUpExpectations(DownloadDangerPrompt::DISMISS); |
| 157 chrome::CloseTab(browser()); | 236 chrome::CloseTab(browser()); |
| 158 VerifyExpectations(); | 237 VerifyExpectations(); |
| 159 } | 238 } |
| OLD | NEW |