| 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 "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "chrome/browser/download/download_danger_prompt.h" | 9 #include "chrome/browser/download/download_danger_prompt.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 using ::testing::ByRef; | 26 using ::testing::ByRef; |
| 27 using ::testing::Eq; | 27 using ::testing::Eq; |
| 28 using ::testing::Return; | 28 using ::testing::Return; |
| 29 using ::testing::ReturnRef; | 29 using ::testing::ReturnRef; |
| 30 using ::testing::SaveArg; | 30 using ::testing::SaveArg; |
| 31 using safe_browsing::ClientDownloadResponse; | 31 using safe_browsing::ClientDownloadResponse; |
| 32 using safe_browsing::ClientSafeBrowsingReportRequest; | 32 using safe_browsing::ClientSafeBrowsingReportRequest; |
| 33 using safe_browsing::SafeBrowsingService; | 33 using safe_browsing::SafeBrowsingService; |
| 34 | 34 |
| 35 const char kTestDownloadUrl[] = "http://evildownload.com"; | 35 const char kTestDownloadUrl[] = "http://evildownload.com"; |
| 36 const char kDownloadResponseToken[] = "default_token"; |
| 36 | 37 |
| 37 class DownloadDangerPromptTest : public InProcessBrowserTest { | 38 class DownloadDangerPromptTest : public InProcessBrowserTest { |
| 38 public: | 39 public: |
| 39 DownloadDangerPromptTest() | 40 DownloadDangerPromptTest() |
| 40 : prompt_(nullptr), | 41 : prompt_(nullptr), |
| 41 expected_action_(DownloadDangerPrompt::CANCEL), | 42 expected_action_(DownloadDangerPrompt::CANCEL), |
| 42 did_receive_callback_(false), | 43 did_receive_callback_(false), |
| 43 test_safe_browsing_factory_( | 44 test_safe_browsing_factory_( |
| 44 new safe_browsing::TestSafeBrowsingServiceFactory()) {} | 45 new safe_browsing::TestSafeBrowsingServiceFactory()) {} |
| 45 | 46 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 62 ui_test_utils::NavigateToURLWithDisposition( | 63 ui_test_utils::NavigateToURLWithDisposition( |
| 63 browser(), GURL("about:blank"), | 64 browser(), GURL("about:blank"), |
| 64 NEW_FOREGROUND_TAB, | 65 NEW_FOREGROUND_TAB, |
| 65 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | 66 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | |
| 66 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 67 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 67 } | 68 } |
| 68 | 69 |
| 69 void SetUpExpectations( | 70 void SetUpExpectations( |
| 70 const DownloadDangerPrompt::Action& expected_action, | 71 const DownloadDangerPrompt::Action& expected_action, |
| 71 const content::DownloadDangerType& danger_type, | 72 const content::DownloadDangerType& danger_type, |
| 72 const ClientDownloadResponse::Verdict& download_verdict) { | 73 const ClientDownloadResponse::Verdict& download_verdict, |
| 74 const std::string& token, |
| 75 bool from_download_api) { |
| 73 did_receive_callback_ = false; | 76 did_receive_callback_ = false; |
| 74 expected_action_ = expected_action; | 77 expected_action_ = expected_action; |
| 75 SetUpDownloadItemExpectations(danger_type); | 78 SetUpDownloadItemExpectations(danger_type, token); |
| 76 SetUpSafeBrowsingReportExpectations( | 79 SetUpSafeBrowsingReportExpectations( |
| 77 expected_action == DownloadDangerPrompt::ACCEPT, download_verdict); | 80 expected_action == DownloadDangerPrompt::ACCEPT, |
| 78 CreatePrompt(); | 81 download_verdict, |
| 82 token, |
| 83 from_download_api); |
| 84 CreatePrompt(from_download_api); |
| 79 } | 85 } |
| 80 | 86 |
| 81 void VerifyExpectations(bool should_send_report) { | 87 void VerifyExpectations(bool should_send_report) { |
| 82 content::RunAllPendingInMessageLoop(); | 88 content::RunAllPendingInMessageLoop(); |
| 83 // At the end of each test, we expect no more activity from the prompt. The | 89 // At the end of each test, we expect no more activity from the prompt. The |
| 84 // prompt shouldn't exist anymore either. | 90 // prompt shouldn't exist anymore either. |
| 85 EXPECT_TRUE(did_receive_callback_); | 91 EXPECT_TRUE(did_receive_callback_); |
| 86 EXPECT_FALSE(prompt_); | 92 EXPECT_FALSE(prompt_); |
| 87 | 93 |
| 88 if (should_send_report) { | 94 if (should_send_report) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 102 void SimulatePromptAction(DownloadDangerPrompt::Action action) { | 108 void SimulatePromptAction(DownloadDangerPrompt::Action action) { |
| 103 prompt_->InvokeActionForTesting(action); | 109 prompt_->InvokeActionForTesting(action); |
| 104 } | 110 } |
| 105 | 111 |
| 106 content::MockDownloadItem& download() { return download_; } | 112 content::MockDownloadItem& download() { return download_; } |
| 107 | 113 |
| 108 DownloadDangerPrompt* prompt() { return prompt_; } | 114 DownloadDangerPrompt* prompt() { return prompt_; } |
| 109 | 115 |
| 110 private: | 116 private: |
| 111 void SetUpDownloadItemExpectations( | 117 void SetUpDownloadItemExpectations( |
| 112 const content::DownloadDangerType& danger_type) { | 118 const content::DownloadDangerType& danger_type, |
| 119 const std::string& token) { |
| 113 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( | 120 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( |
| 114 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); | 121 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); |
| 115 EXPECT_CALL(download_, GetDangerType()).WillRepeatedly(Return(danger_type)); | 122 EXPECT_CALL(download_, GetDangerType()).WillRepeatedly(Return(danger_type)); |
| 123 EXPECT_CALL(download_, GetDownloadPingToken()) |
| 124 .WillRepeatedly(Return(token)); |
| 116 } | 125 } |
| 117 | 126 |
| 118 void SetUpSafeBrowsingReportExpectations( | 127 void SetUpSafeBrowsingReportExpectations( |
| 119 bool did_proceed, | 128 bool did_proceed, |
| 120 const ClientDownloadResponse::Verdict& download_verdict) { | 129 const ClientDownloadResponse::Verdict& download_verdict, |
| 130 const std::string& token, |
| 131 bool from_download_api) { |
| 121 ClientSafeBrowsingReportRequest expected_report; | 132 ClientSafeBrowsingReportRequest expected_report; |
| 122 expected_report.set_url(GURL(kTestDownloadUrl).spec()); | 133 expected_report.set_url(GURL(kTestDownloadUrl).spec()); |
| 123 expected_report.set_type( | 134 if (from_download_api) |
| 124 ClientSafeBrowsingReportRequest::DANGEROUS_DOWNLOAD_RECOVERY); | 135 expected_report.set_type( |
| 136 ClientSafeBrowsingReportRequest::DANGEROUS_DOWNLOAD_BY_API); |
| 137 else |
| 138 expected_report.set_type( |
| 139 ClientSafeBrowsingReportRequest::DANGEROUS_DOWNLOAD_RECOVERY); |
| 125 expected_report.set_download_verdict(download_verdict); | 140 expected_report.set_download_verdict(download_verdict); |
| 126 expected_report.set_did_proceed(did_proceed); | 141 expected_report.set_did_proceed(did_proceed); |
| 142 if (!token.empty()) |
| 143 expected_report.set_token(token); |
| 127 expected_report.SerializeToString(&expected_serialized_report_); | 144 expected_report.SerializeToString(&expected_serialized_report_); |
| 128 } | 145 } |
| 129 | 146 |
| 130 void CreatePrompt() { | 147 void CreatePrompt(bool from_download_api) { |
| 131 prompt_ = DownloadDangerPrompt::Create( | 148 prompt_ = DownloadDangerPrompt::Create( |
| 132 &download_, | 149 &download_, |
| 133 browser()->tab_strip_model()->GetActiveWebContents(), | 150 browser()->tab_strip_model()->GetActiveWebContents(), |
| 134 false, | 151 from_download_api, |
| 135 base::Bind(&DownloadDangerPromptTest::PromptCallback, this)); | 152 base::Bind(&DownloadDangerPromptTest::PromptCallback, this)); |
| 136 content::RunAllPendingInMessageLoop(); | 153 content::RunAllPendingInMessageLoop(); |
| 137 } | 154 } |
| 138 | 155 |
| 139 void PromptCallback(DownloadDangerPrompt::Action action) { | 156 void PromptCallback(DownloadDangerPrompt::Action action) { |
| 140 EXPECT_FALSE(did_receive_callback_); | 157 EXPECT_FALSE(did_receive_callback_); |
| 141 EXPECT_EQ(expected_action_, action); | 158 EXPECT_EQ(expected_action_, action); |
| 142 did_receive_callback_ = true; | 159 did_receive_callback_ = true; |
| 143 prompt_ = nullptr; | 160 prompt_ = nullptr; |
| 144 } | 161 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 170 .WillByDefault(Return(browser()->profile())); | 187 .WillByDefault(Return(browser()->profile())); |
| 171 base::FilePath empty_file_path; | 188 base::FilePath empty_file_path; |
| 172 ON_CALL(download(), GetTargetFilePath()) | 189 ON_CALL(download(), GetTargetFilePath()) |
| 173 .WillByDefault(ReturnRef(empty_file_path)); | 190 .WillByDefault(ReturnRef(empty_file_path)); |
| 174 | 191 |
| 175 OpenNewTab(); | 192 OpenNewTab(); |
| 176 | 193 |
| 177 // Clicking the Accept button should invoke the ACCEPT action. | 194 // Clicking the Accept button should invoke the ACCEPT action. |
| 178 SetUpExpectations(DownloadDangerPrompt::ACCEPT, | 195 SetUpExpectations(DownloadDangerPrompt::ACCEPT, |
| 179 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, | 196 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 180 ClientDownloadResponse::DANGEROUS); | 197 ClientDownloadResponse::DANGEROUS, |
| 198 kDownloadResponseToken, |
| 199 false); |
| 181 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); | 200 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); |
| 182 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); | 201 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
| 183 VerifyExpectations(true); | 202 VerifyExpectations(true); |
| 184 | 203 |
| 185 // Clicking the Cancel button should invoke the CANCEL action. | 204 // Clicking the Cancel button should invoke the CANCEL action. |
| 186 SetUpExpectations(DownloadDangerPrompt::CANCEL, | 205 SetUpExpectations(DownloadDangerPrompt::CANCEL, |
| 187 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT, | 206 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT, |
| 188 ClientDownloadResponse::UNCOMMON); | 207 ClientDownloadResponse::UNCOMMON, |
| 208 base::EmptyString(), |
| 209 false); |
| 189 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); | 210 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); |
| 190 SimulatePromptAction(DownloadDangerPrompt::CANCEL); | 211 SimulatePromptAction(DownloadDangerPrompt::CANCEL); |
| 191 VerifyExpectations(true); | 212 VerifyExpectations(true); |
| 192 | 213 |
| 193 // If the download is no longer dangerous (because it was accepted), the | 214 // If the download is no longer dangerous (because it was accepted), the |
| 194 // dialog should DISMISS itself. | 215 // dialog should DISMISS itself. |
| 195 SetUpExpectations(DownloadDangerPrompt::DISMISS, | 216 SetUpExpectations(DownloadDangerPrompt::DISMISS, |
| 196 content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED, | 217 content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED, |
| 197 ClientDownloadResponse::POTENTIALLY_UNWANTED); | 218 ClientDownloadResponse::POTENTIALLY_UNWANTED, |
| 219 kDownloadResponseToken, |
| 220 false); |
| 198 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(false)); | 221 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(false)); |
| 199 download().NotifyObserversDownloadUpdated(); | 222 download().NotifyObserversDownloadUpdated(); |
| 200 VerifyExpectations(false); | 223 VerifyExpectations(false); |
| 201 | 224 |
| 202 // If the download is in a terminal state then the dialog should DISMISS | 225 // If the download is in a terminal state then the dialog should DISMISS |
| 203 // itself. | 226 // itself. |
| 204 SetUpExpectations(DownloadDangerPrompt::DISMISS, | 227 SetUpExpectations(DownloadDangerPrompt::DISMISS, |
| 205 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST, | 228 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST, |
| 206 ClientDownloadResponse::DANGEROUS_HOST); | 229 ClientDownloadResponse::DANGEROUS_HOST, |
| 230 kDownloadResponseToken, |
| 231 false); |
| 207 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); | 232 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); |
| 208 EXPECT_CALL(download(), IsDone()).WillRepeatedly(Return(true)); | 233 EXPECT_CALL(download(), IsDone()).WillRepeatedly(Return(true)); |
| 209 download().NotifyObserversDownloadUpdated(); | 234 download().NotifyObserversDownloadUpdated(); |
| 210 VerifyExpectations(false); | 235 VerifyExpectations(false); |
| 211 | 236 |
| 212 // If the download is dangerous and is not in a terminal state, don't dismiss | 237 // If the download is dangerous and is not in a terminal state, don't dismiss |
| 213 // the dialog. | 238 // the dialog. |
| 214 SetUpExpectations(DownloadDangerPrompt::ACCEPT, | 239 SetUpExpectations(DownloadDangerPrompt::ACCEPT, |
| 215 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT, | 240 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT, |
| 216 ClientDownloadResponse::DANGEROUS); | 241 ClientDownloadResponse::DANGEROUS, |
| 242 kDownloadResponseToken, |
| 243 false); |
| 217 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); | 244 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); |
| 218 EXPECT_CALL(download(), IsDone()).WillRepeatedly(Return(false)); | 245 EXPECT_CALL(download(), IsDone()).WillRepeatedly(Return(false)); |
| 219 download().NotifyObserversDownloadUpdated(); | 246 download().NotifyObserversDownloadUpdated(); |
| 220 EXPECT_TRUE(prompt()); | 247 EXPECT_TRUE(prompt()); |
| 221 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); | 248 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
| 222 VerifyExpectations(true); | 249 VerifyExpectations(true); |
| 223 | 250 |
| 224 // If the download is not dangerous, no report will be sent. | 251 // If the download is not dangerous, no report will be sent. |
| 225 SetUpExpectations(DownloadDangerPrompt::ACCEPT, | 252 SetUpExpectations(DownloadDangerPrompt::ACCEPT, |
| 226 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 253 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 227 ClientDownloadResponse::SAFE); | 254 ClientDownloadResponse::SAFE, |
| 255 kDownloadResponseToken, |
| 256 false); |
| 228 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); | 257 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
| 229 VerifyExpectations(false); | 258 VerifyExpectations(false); |
| 230 | 259 |
| 231 // If the containing tab is closed, the dialog should DISMISS itself. | 260 // If the containing tab is closed, the dialog should DISMISS itself. |
| 232 OpenNewTab(); | 261 OpenNewTab(); |
| 233 SetUpExpectations(DownloadDangerPrompt::DISMISS, | 262 SetUpExpectations(DownloadDangerPrompt::DISMISS, |
| 234 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, | 263 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 235 ClientDownloadResponse::DANGEROUS); | 264 ClientDownloadResponse::DANGEROUS, |
| 265 kDownloadResponseToken, |
| 266 false); |
| 236 chrome::CloseTab(browser()); | 267 chrome::CloseTab(browser()); |
| 237 VerifyExpectations(false); | 268 VerifyExpectations(false); |
| 269 |
| 270 // If file is downloaded through download api, a confirm download dialog |
| 271 // instead of a recovery dialog is shown. Clicking the Accept button should |
| 272 // invoke the ACCEPT action, a report will be sent with type |
| 273 // DANGEROUS_DOWNLOAD_BY_API. |
| 274 SetUpExpectations(DownloadDangerPrompt::ACCEPT, |
| 275 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 276 ClientDownloadResponse::DANGEROUS, |
| 277 kDownloadResponseToken, |
| 278 true); |
| 279 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); |
| 280 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
| 281 VerifyExpectations(true); |
| 282 |
| 283 // If file is downloaded through download api, a confirm download dialog |
| 284 // instead of a recovery dialog is shown. Clicking the Cancel button should |
| 285 // invoke the CANCEL action, a report will be sent with type |
| 286 // DANGEROUS_DOWNLOAD_BY_API. |
| 287 SetUpExpectations(DownloadDangerPrompt::CANCEL, |
| 288 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT, |
| 289 ClientDownloadResponse::UNCOMMON, |
| 290 base::EmptyString(), |
| 291 true); |
| 292 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); |
| 293 SimulatePromptAction(DownloadDangerPrompt::CANCEL); |
| 294 VerifyExpectations(true); |
| 238 } | 295 } |
| OLD | NEW |