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" |
11 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 11 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/browser_commands.h" | 13 #include "chrome/browser/ui/browser_commands.h" |
14 #include "chrome/browser/ui/browser_tabstrip.h" | 14 #include "chrome/browser/ui/browser_tabstrip.h" |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
16 #include "chrome/common/safe_browsing/csd.pb.h" | 16 #include "chrome/common/safe_browsing/csd.pb.h" |
17 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
18 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
19 #include "components/safe_browsing_db/database_manager.h" | 19 #include "components/safe_browsing_db/database_manager.h" |
20 #include "content/public/test/mock_download_item.h" | 20 #include "content/public/test/mock_download_item.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "url/gurl.h" | 23 #include "url/gurl.h" |
24 | 24 |
25 using ::testing::_; | 25 using ::testing::_; |
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 | 36 |
37 class FakeSafeBrowsingService : public SafeBrowsingService { | |
38 public: | |
39 FakeSafeBrowsingService() {} | |
40 | |
41 void SendSerializedDownloadReport(const std::string& report) override { | |
42 report_ = report; | |
43 } | |
44 | |
45 std::string GetDownloadRecoveryReport() const { return report_; } | |
46 | |
47 protected: | |
48 ~FakeSafeBrowsingService() override {} | |
49 | |
50 private: | |
51 std::string report_; | |
52 }; | |
53 | |
54 // Factory that creates FakeSafeBrowsingService instances. | |
55 class TestSafeBrowsingServiceFactory | |
56 : public safe_browsing::SafeBrowsingServiceFactory { | |
57 public: | |
58 TestSafeBrowsingServiceFactory() : fake_safe_browsing_service_(nullptr) {} | |
59 ~TestSafeBrowsingServiceFactory() override {} | |
60 | |
61 SafeBrowsingService* CreateSafeBrowsingService() override { | |
62 if (!fake_safe_browsing_service_) { | |
63 fake_safe_browsing_service_ = new FakeSafeBrowsingService(); | |
64 } | |
65 return fake_safe_browsing_service_.get(); | |
66 } | |
67 | |
68 scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service() { | |
69 return fake_safe_browsing_service_; | |
70 } | |
71 | |
72 private: | |
73 scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service_; | |
74 }; | |
75 | |
76 class DownloadDangerPromptTest : public InProcessBrowserTest { | 37 class DownloadDangerPromptTest : public InProcessBrowserTest { |
77 public: | 38 public: |
78 DownloadDangerPromptTest() | 39 DownloadDangerPromptTest() |
79 : prompt_(nullptr), | 40 : prompt_(nullptr), |
80 expected_action_(DownloadDangerPrompt::CANCEL), | 41 expected_action_(DownloadDangerPrompt::CANCEL), |
81 did_receive_callback_(false), | 42 did_receive_callback_(false), |
82 test_safe_browsing_factory_(new TestSafeBrowsingServiceFactory()), | 43 test_safe_browsing_factory_( |
| 44 new safe_browsing::TestSafeBrowsingServiceFactory()), |
83 report_sent_(false) {} | 45 report_sent_(false) {} |
84 | 46 |
85 ~DownloadDangerPromptTest() override {} | 47 ~DownloadDangerPromptTest() override {} |
86 | 48 |
87 void SetUp() override { | 49 void SetUp() override { |
88 SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get()); | 50 SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get()); |
89 InProcessBrowserTest::SetUp(); | 51 InProcessBrowserTest::SetUp(); |
90 } | 52 } |
91 | 53 |
92 void TearDown() override { | 54 void TearDown() override { |
(...skipping 27 matching lines...) Expand all Loading... |
120 | 82 |
121 void VerifyExpectations() { | 83 void VerifyExpectations() { |
122 content::RunAllPendingInMessageLoop(); | 84 content::RunAllPendingInMessageLoop(); |
123 // At the end of each test, we expect no more activity from the prompt. The | 85 // At the end of each test, we expect no more activity from the prompt. The |
124 // prompt shouldn't exist anymore either. | 86 // prompt shouldn't exist anymore either. |
125 EXPECT_TRUE(did_receive_callback_); | 87 EXPECT_TRUE(did_receive_callback_); |
126 EXPECT_FALSE(prompt_); | 88 EXPECT_FALSE(prompt_); |
127 testing::Mock::VerifyAndClearExpectations(&download_); | 89 testing::Mock::VerifyAndClearExpectations(&download_); |
128 if (report_sent_) { | 90 if (report_sent_) { |
129 EXPECT_EQ(expected_serialized_report_, | 91 EXPECT_EQ(expected_serialized_report_, |
130 test_safe_browsing_factory_->fake_safe_browsing_service() | 92 test_safe_browsing_factory_->test_safe_browsing_service() |
131 ->GetDownloadRecoveryReport()); | 93 ->serilized_download_report()); |
132 } | 94 } |
133 } | 95 } |
134 | 96 |
135 void SimulatePromptAction(DownloadDangerPrompt::Action action) { | 97 void SimulatePromptAction(DownloadDangerPrompt::Action action) { |
136 prompt_->InvokeActionForTesting(action); | 98 prompt_->InvokeActionForTesting(action); |
137 report_sent_ = true; | 99 report_sent_ = true; |
138 } | 100 } |
139 | 101 |
140 content::MockDownloadItem& download() { return download_; } | 102 content::MockDownloadItem& download() { return download_; } |
141 | 103 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 EXPECT_FALSE(did_receive_callback_); | 136 EXPECT_FALSE(did_receive_callback_); |
175 EXPECT_EQ(expected_action_, action); | 137 EXPECT_EQ(expected_action_, action); |
176 did_receive_callback_ = true; | 138 did_receive_callback_ = true; |
177 prompt_ = nullptr; | 139 prompt_ = nullptr; |
178 } | 140 } |
179 | 141 |
180 content::MockDownloadItem download_; | 142 content::MockDownloadItem download_; |
181 DownloadDangerPrompt* prompt_; | 143 DownloadDangerPrompt* prompt_; |
182 DownloadDangerPrompt::Action expected_action_; | 144 DownloadDangerPrompt::Action expected_action_; |
183 bool did_receive_callback_; | 145 bool did_receive_callback_; |
184 std::unique_ptr<TestSafeBrowsingServiceFactory> test_safe_browsing_factory_; | 146 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> |
| 147 test_safe_browsing_factory_; |
185 std::string expected_serialized_report_; | 148 std::string expected_serialized_report_; |
186 bool report_sent_; | 149 bool report_sent_; |
187 | 150 |
188 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); | 151 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); |
189 }; | 152 }; |
190 | 153 |
191 // Disabled for flaky timeouts on Windows. crbug.com/446696 | 154 // Disabled for flaky timeouts on Windows. crbug.com/446696 |
192 #if defined(OS_WIN) | 155 #if defined(OS_WIN) |
193 #define MAYBE_TestAll DISABLED_TestAll | 156 #define MAYBE_TestAll DISABLED_TestAll |
194 #else | 157 #else |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 VerifyExpectations(); | 216 VerifyExpectations(); |
254 | 217 |
255 // If the containing tab is closed, the dialog should DISMISS itself. | 218 // If the containing tab is closed, the dialog should DISMISS itself. |
256 OpenNewTab(); | 219 OpenNewTab(); |
257 SetUpExpectations(DownloadDangerPrompt::DISMISS, | 220 SetUpExpectations(DownloadDangerPrompt::DISMISS, |
258 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, | 221 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
259 ClientDownloadResponse::DANGEROUS); | 222 ClientDownloadResponse::DANGEROUS); |
260 chrome::CloseTab(browser()); | 223 chrome::CloseTab(browser()); |
261 VerifyExpectations(); | 224 VerifyExpectations(); |
262 } | 225 } |
OLD | NEW |