| 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/command_line.h" |
| 6 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 7 #include "base/macros.h" | 8 #include "base/macros.h" |
| 8 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 9 #include "chrome/browser/download/download_danger_prompt.h" | 10 #include "chrome/browser/download/download_danger_prompt.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/safe_browsing/download_protection_service.h" | 12 #include "chrome/browser/safe_browsing/download_protection_service.h" |
| 12 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" | 13 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| 13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_commands.h" | 15 #include "chrome/browser/ui/browser_commands.h" |
| 15 #include "chrome/browser/ui/browser_tabstrip.h" | 16 #include "chrome/browser/ui/browser_tabstrip.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/common/safe_browsing/csd.pb.h" | 18 #include "chrome/common/safe_browsing/csd.pb.h" |
| 18 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
| 20 #include "components/safe_browsing_db/database_manager.h" | 21 #include "components/safe_browsing_db/database_manager.h" |
| 21 #include "content/public/test/mock_download_item.h" | 22 #include "content/public/test/mock_download_item.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "ui/base/ui_base_switches.h" |
| 24 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 25 | 27 |
| 26 using ::testing::_; | 28 using ::testing::_; |
| 27 using ::testing::ByRef; | 29 using ::testing::ByRef; |
| 28 using ::testing::Eq; | 30 using ::testing::Eq; |
| 29 using ::testing::Return; | 31 using ::testing::Return; |
| 30 using ::testing::ReturnRef; | 32 using ::testing::ReturnRef; |
| 31 using ::testing::SaveArg; | 33 using ::testing::SaveArg; |
| 32 using safe_browsing::ClientDownloadResponse; | |
| 33 using safe_browsing::ClientSafeBrowsingReportRequest; | |
| 34 using safe_browsing::SafeBrowsingService; | |
| 35 | 34 |
| 36 namespace safe_browsing { | 35 namespace safe_browsing { |
| 37 | 36 |
| 37 namespace { |
| 38 |
| 38 const char kTestDownloadUrl[] = "http://evildownload.com"; | 39 const char kTestDownloadUrl[] = "http://evildownload.com"; |
| 39 const char kDownloadResponseToken[] = "default_token"; | 40 const char kDownloadResponseToken[] = "default_token"; |
| 40 | 41 |
| 41 class DownloadDangerPromptTest : public InProcessBrowserTest { | 42 // An enum to parameterize the tests so that the tests can be run with and |
| 43 // without the secondary-ui-md flag. |
| 44 enum class SecondaryUiMd { |
| 45 ENABLED, |
| 46 DISABLED, |
| 47 }; |
| 48 |
| 49 // Generates the test name suffix depending on the value of the SecondaryUiMd |
| 50 // param. |
| 51 std::string SecondaryUiMdStatusToString( |
| 52 const ::testing::TestParamInfo<SecondaryUiMd>& info) { |
| 53 switch (info.param) { |
| 54 case SecondaryUiMd::ENABLED: |
| 55 return "SecondaryUiMdEnabled"; |
| 56 case SecondaryUiMd::DISABLED: |
| 57 return "SecondaryUiMdDisabled"; |
| 58 } |
| 59 NOTREACHED(); |
| 60 return std::string(); |
| 61 } |
| 62 |
| 63 } // namespace |
| 64 |
| 65 class DownloadDangerPromptTest |
| 66 : public InProcessBrowserTest, |
| 67 public ::testing::WithParamInterface<SecondaryUiMd> { |
| 42 public: | 68 public: |
| 43 DownloadDangerPromptTest() | 69 DownloadDangerPromptTest() |
| 44 : prompt_(nullptr), | 70 : prompt_(nullptr), |
| 45 expected_action_(DownloadDangerPrompt::CANCEL), | 71 expected_action_(DownloadDangerPrompt::CANCEL), |
| 46 did_receive_callback_(false), | 72 did_receive_callback_(false), |
| 47 test_safe_browsing_factory_( | 73 test_safe_browsing_factory_( |
| 48 new safe_browsing::TestSafeBrowsingServiceFactory()) {} | 74 base::MakeUnique<TestSafeBrowsingServiceFactory>()) {} |
| 49 | 75 |
| 50 ~DownloadDangerPromptTest() override {} | 76 ~DownloadDangerPromptTest() override {} |
| 51 | 77 |
| 52 void SetUp() override { | 78 void SetUp() override { |
| 53 SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get()); | 79 SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get()); |
| 54 InProcessBrowserTest::SetUp(); | 80 InProcessBrowserTest::SetUp(); |
| 55 } | 81 } |
| 56 | 82 |
| 57 void TearDown() override { | 83 void TearDown() override { |
| 58 SafeBrowsingService::RegisterFactory(nullptr); | 84 SafeBrowsingService::RegisterFactory(nullptr); |
| 59 InProcessBrowserTest::TearDown(); | 85 InProcessBrowserTest::TearDown(); |
| 60 } | 86 } |
| 61 | 87 |
| 88 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 89 // TODO(crbug.com/630357): Remove parameterized testing for this class when |
| 90 // secondary-ui-md is enabled by default on all platforms. |
| 91 if (GetParam() == SecondaryUiMd::ENABLED) |
| 92 command_line->AppendSwitch(switches::kExtendMdToSecondaryUi); |
| 93 } |
| 94 |
| 62 // Opens a new tab and waits for navigations to finish. If there are pending | 95 // Opens a new tab and waits for navigations to finish. If there are pending |
| 63 // navigations, the constrained prompt might be dismissed when the navigation | 96 // navigations, the constrained prompt might be dismissed when the navigation |
| 64 // completes. | 97 // completes. |
| 65 void OpenNewTab() { | 98 void OpenNewTab() { |
| 66 ui_test_utils::NavigateToURLWithDisposition( | 99 ui_test_utils::NavigateToURLWithDisposition( |
| 67 browser(), GURL("about:blank"), | 100 browser(), GURL("about:blank"), |
| 68 WindowOpenDisposition::NEW_FOREGROUND_TAB, | 101 WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 69 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | 102 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | |
| 70 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 103 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 71 } | 104 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 149 |
| 117 DownloadDangerPrompt* prompt() { return prompt_; } | 150 DownloadDangerPrompt* prompt() { return prompt_; } |
| 118 | 151 |
| 119 private: | 152 private: |
| 120 void SetUpDownloadItemExpectations( | 153 void SetUpDownloadItemExpectations( |
| 121 const content::DownloadDangerType& danger_type, | 154 const content::DownloadDangerType& danger_type, |
| 122 const std::string& token) { | 155 const std::string& token) { |
| 123 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( | 156 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( |
| 124 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); | 157 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); |
| 125 EXPECT_CALL(download_, GetDangerType()).WillRepeatedly(Return(danger_type)); | 158 EXPECT_CALL(download_, GetDangerType()).WillRepeatedly(Return(danger_type)); |
| 126 safe_browsing::DownloadProtectionService::DownloadPingToken* token_obj | 159 DownloadProtectionService::DownloadPingToken* token_obj = |
| 127 = new safe_browsing::DownloadProtectionService::DownloadPingToken( | 160 new DownloadProtectionService::DownloadPingToken(token); |
| 128 token); | 161 download_.SetUserData(DownloadProtectionService::kDownloadPingTokenKey, |
| 129 download_.SetUserData( | 162 token_obj); |
| 130 safe_browsing::DownloadProtectionService::kDownloadPingTokenKey, | |
| 131 token_obj); | |
| 132 } | 163 } |
| 133 | 164 |
| 134 void SetUpSafeBrowsingReportExpectations( | 165 void SetUpSafeBrowsingReportExpectations( |
| 135 bool did_proceed, | 166 bool did_proceed, |
| 136 const ClientDownloadResponse::Verdict& download_verdict, | 167 const ClientDownloadResponse::Verdict& download_verdict, |
| 137 const std::string& token, | 168 const std::string& token, |
| 138 bool from_download_api) { | 169 bool from_download_api) { |
| 139 ClientSafeBrowsingReportRequest expected_report; | 170 ClientSafeBrowsingReportRequest expected_report; |
| 140 expected_report.set_url(GURL(kTestDownloadUrl).spec()); | 171 expected_report.set_url(GURL(kTestDownloadUrl).spec()); |
| 141 if (from_download_api) | 172 if (from_download_api) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 165 EXPECT_FALSE(did_receive_callback_); | 196 EXPECT_FALSE(did_receive_callback_); |
| 166 EXPECT_EQ(expected_action_, action); | 197 EXPECT_EQ(expected_action_, action); |
| 167 did_receive_callback_ = true; | 198 did_receive_callback_ = true; |
| 168 prompt_ = nullptr; | 199 prompt_ = nullptr; |
| 169 } | 200 } |
| 170 | 201 |
| 171 content::MockDownloadItem download_; | 202 content::MockDownloadItem download_; |
| 172 DownloadDangerPrompt* prompt_; | 203 DownloadDangerPrompt* prompt_; |
| 173 DownloadDangerPrompt::Action expected_action_; | 204 DownloadDangerPrompt::Action expected_action_; |
| 174 bool did_receive_callback_; | 205 bool did_receive_callback_; |
| 175 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> | 206 std::unique_ptr<TestSafeBrowsingServiceFactory> test_safe_browsing_factory_; |
| 176 test_safe_browsing_factory_; | |
| 177 std::string expected_serialized_report_; | 207 std::string expected_serialized_report_; |
| 178 | 208 |
| 179 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); | 209 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); |
| 180 }; | 210 }; |
| 181 | 211 |
| 182 // Disabled for flaky timeouts on Windows. crbug.com/446696 | 212 // Disabled for flaky timeouts on Windows. crbug.com/446696 |
| 183 #if defined(OS_WIN) | 213 #if defined(OS_WIN) |
| 184 #define MAYBE_TestAll DISABLED_TestAll | 214 #define MAYBE_TestAll DISABLED_TestAll |
| 185 #else | 215 #else |
| 186 #define MAYBE_TestAll TestAll | 216 #define MAYBE_TestAll TestAll |
| 187 #endif | 217 #endif |
| 188 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) { | 218 IN_PROC_BROWSER_TEST_P(DownloadDangerPromptTest, MAYBE_TestAll) { |
| 189 // ExperienceSampling: Set default actions for DownloadItem methods we need. | 219 // ExperienceSampling: Set default actions for DownloadItem methods we need. |
| 190 GURL download_url(kTestDownloadUrl); | 220 GURL download_url(kTestDownloadUrl); |
| 191 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(download_url)); | 221 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(download_url)); |
| 192 ON_CALL(download(), GetReferrerUrl()) | 222 ON_CALL(download(), GetReferrerUrl()) |
| 193 .WillByDefault(ReturnRef(GURL::EmptyGURL())); | 223 .WillByDefault(ReturnRef(GURL::EmptyGURL())); |
| 194 ON_CALL(download(), GetBrowserContext()) | 224 ON_CALL(download(), GetBrowserContext()) |
| 195 .WillByDefault(Return(browser()->profile())); | 225 .WillByDefault(Return(browser()->profile())); |
| 196 base::FilePath empty_file_path; | 226 base::FilePath empty_file_path; |
| 197 ON_CALL(download(), GetTargetFilePath()) | 227 ON_CALL(download(), GetTargetFilePath()) |
| 198 .WillByDefault(ReturnRef(empty_file_path)); | 228 .WillByDefault(ReturnRef(empty_file_path)); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 SetUpExpectations(DownloadDangerPrompt::CANCEL, | 325 SetUpExpectations(DownloadDangerPrompt::CANCEL, |
| 296 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT, | 326 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT, |
| 297 ClientDownloadResponse::UNCOMMON, | 327 ClientDownloadResponse::UNCOMMON, |
| 298 std::string(), | 328 std::string(), |
| 299 true); | 329 true); |
| 300 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); | 330 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); |
| 301 SimulatePromptAction(DownloadDangerPrompt::CANCEL); | 331 SimulatePromptAction(DownloadDangerPrompt::CANCEL); |
| 302 VerifyExpectations(true); | 332 VerifyExpectations(true); |
| 303 } | 333 } |
| 304 | 334 |
| 335 // Prefix for test instantiations intentionally left blank since the test |
| 336 // fixture class has a single parameterization. |
| 337 INSTANTIATE_TEST_CASE_P(, |
| 338 DownloadDangerPromptTest, |
| 339 ::testing::Values(SecondaryUiMd::ENABLED, |
| 340 SecondaryUiMd::DISABLED), |
| 341 &SecondaryUiMdStatusToString); |
| 342 |
| 305 } // namespace safe_browsing | 343 } // namespace safe_browsing |
| OLD | NEW |