Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: chrome/browser/download/download_danger_prompt_browsertest.cc

Issue 1943993006: Create test fixture for SafeBrowsingService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_(
83 report_sent_(false) {} 44 new safe_browsing::TestSafeBrowsingServiceFactory()) {}
84 45
85 ~DownloadDangerPromptTest() override {} 46 ~DownloadDangerPromptTest() override {}
86 47
87 void SetUp() override { 48 void SetUp() override {
88 SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get()); 49 SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get());
89 InProcessBrowserTest::SetUp(); 50 InProcessBrowserTest::SetUp();
90 } 51 }
91 52
92 void TearDown() override { 53 void TearDown() override {
93 SafeBrowsingService::RegisterFactory(nullptr); 54 SafeBrowsingService::RegisterFactory(nullptr);
(...skipping 14 matching lines...) Expand all
108 void SetUpExpectations( 69 void SetUpExpectations(
109 const DownloadDangerPrompt::Action& expected_action, 70 const DownloadDangerPrompt::Action& expected_action,
110 const content::DownloadDangerType& danger_type, 71 const content::DownloadDangerType& danger_type,
111 const ClientDownloadResponse::Verdict& download_verdict) { 72 const ClientDownloadResponse::Verdict& download_verdict) {
112 did_receive_callback_ = false; 73 did_receive_callback_ = false;
113 expected_action_ = expected_action; 74 expected_action_ = expected_action;
114 SetUpDownloadItemExpectations(danger_type); 75 SetUpDownloadItemExpectations(danger_type);
115 SetUpSafeBrowsingReportExpectations( 76 SetUpSafeBrowsingReportExpectations(
116 expected_action == DownloadDangerPrompt::ACCEPT, download_verdict); 77 expected_action == DownloadDangerPrompt::ACCEPT, download_verdict);
117 CreatePrompt(); 78 CreatePrompt();
118 report_sent_ = false;
119 } 79 }
120 80
121 void VerifyExpectations() { 81 void VerifyExpectations(bool should_send_report) {
122 content::RunAllPendingInMessageLoop(); 82 content::RunAllPendingInMessageLoop();
123 // At the end of each test, we expect no more activity from the prompt. The 83 // At the end of each test, we expect no more activity from the prompt. The
124 // prompt shouldn't exist anymore either. 84 // prompt shouldn't exist anymore either.
125 EXPECT_TRUE(did_receive_callback_); 85 EXPECT_TRUE(did_receive_callback_);
126 EXPECT_FALSE(prompt_); 86 EXPECT_FALSE(prompt_);
87
88 if (should_send_report) {
89 EXPECT_EQ(expected_serialized_report_,
90 test_safe_browsing_factory_->test_safe_browsing_service()
91 ->serilized_download_report());
92 } else {
93 EXPECT_TRUE(test_safe_browsing_factory_->test_safe_browsing_service()
94 ->serilized_download_report()
95 .empty());
96 }
127 testing::Mock::VerifyAndClearExpectations(&download_); 97 testing::Mock::VerifyAndClearExpectations(&download_);
128 if (report_sent_) { 98 test_safe_browsing_factory_->test_safe_browsing_service()
129 EXPECT_EQ(expected_serialized_report_, 99 ->ClearDownloadReport();
130 test_safe_browsing_factory_->fake_safe_browsing_service()
131 ->GetDownloadRecoveryReport());
132 }
133 } 100 }
134 101
135 void SimulatePromptAction(DownloadDangerPrompt::Action action) { 102 void SimulatePromptAction(DownloadDangerPrompt::Action action) {
136 prompt_->InvokeActionForTesting(action); 103 prompt_->InvokeActionForTesting(action);
137 report_sent_ = true;
138 } 104 }
139 105
140 content::MockDownloadItem& download() { return download_; } 106 content::MockDownloadItem& download() { return download_; }
141 107
142 DownloadDangerPrompt* prompt() { return prompt_; } 108 DownloadDangerPrompt* prompt() { return prompt_; }
143 109
144 private: 110 private:
145 void SetUpDownloadItemExpectations( 111 void SetUpDownloadItemExpectations(
146 const content::DownloadDangerType& danger_type) { 112 const content::DownloadDangerType& danger_type) {
147 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( 113 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return(
(...skipping 26 matching lines...) Expand all
174 EXPECT_FALSE(did_receive_callback_); 140 EXPECT_FALSE(did_receive_callback_);
175 EXPECT_EQ(expected_action_, action); 141 EXPECT_EQ(expected_action_, action);
176 did_receive_callback_ = true; 142 did_receive_callback_ = true;
177 prompt_ = nullptr; 143 prompt_ = nullptr;
178 } 144 }
179 145
180 content::MockDownloadItem download_; 146 content::MockDownloadItem download_;
181 DownloadDangerPrompt* prompt_; 147 DownloadDangerPrompt* prompt_;
182 DownloadDangerPrompt::Action expected_action_; 148 DownloadDangerPrompt::Action expected_action_;
183 bool did_receive_callback_; 149 bool did_receive_callback_;
184 std::unique_ptr<TestSafeBrowsingServiceFactory> test_safe_browsing_factory_; 150 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory>
151 test_safe_browsing_factory_;
185 std::string expected_serialized_report_; 152 std::string expected_serialized_report_;
186 bool report_sent_;
187 153
188 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); 154 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest);
189 }; 155 };
190 156
191 // Disabled for flaky timeouts on Windows. crbug.com/446696 157 // Disabled for flaky timeouts on Windows. crbug.com/446696
192 #if defined(OS_WIN) 158 #if defined(OS_WIN)
193 #define MAYBE_TestAll DISABLED_TestAll 159 #define MAYBE_TestAll DISABLED_TestAll
194 #else 160 #else
195 #define MAYBE_TestAll TestAll 161 #define MAYBE_TestAll TestAll
196 #endif 162 #endif
197 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) { 163 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) {
198 // ExperienceSampling: Set default actions for DownloadItem methods we need. 164 // ExperienceSampling: Set default actions for DownloadItem methods we need.
199 GURL download_url(kTestDownloadUrl); 165 GURL download_url(kTestDownloadUrl);
200 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(download_url)); 166 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(download_url));
201 ON_CALL(download(), GetReferrerUrl()) 167 ON_CALL(download(), GetReferrerUrl())
202 .WillByDefault(ReturnRef(GURL::EmptyGURL())); 168 .WillByDefault(ReturnRef(GURL::EmptyGURL()));
203 ON_CALL(download(), GetBrowserContext()) 169 ON_CALL(download(), GetBrowserContext())
204 .WillByDefault(Return(browser()->profile())); 170 .WillByDefault(Return(browser()->profile()));
205 base::FilePath empty_file_path; 171 base::FilePath empty_file_path;
206 ON_CALL(download(), GetTargetFilePath()) 172 ON_CALL(download(), GetTargetFilePath())
207 .WillByDefault(ReturnRef(empty_file_path)); 173 .WillByDefault(ReturnRef(empty_file_path));
208 174
209 OpenNewTab(); 175 OpenNewTab();
210 176
211 // Clicking the Accept button should invoke the ACCEPT action. 177 // Clicking the Accept button should invoke the ACCEPT action.
212 SetUpExpectations(DownloadDangerPrompt::ACCEPT, 178 SetUpExpectations(DownloadDangerPrompt::ACCEPT,
213 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, 179 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
214 ClientDownloadResponse::DANGEROUS); 180 ClientDownloadResponse::DANGEROUS);
181 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true));
215 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); 182 SimulatePromptAction(DownloadDangerPrompt::ACCEPT);
216 VerifyExpectations(); 183 VerifyExpectations(true);
217 184
218 // Clicking the Cancel button should invoke the CANCEL action. 185 // Clicking the Cancel button should invoke the CANCEL action.
219 SetUpExpectations(DownloadDangerPrompt::CANCEL, 186 SetUpExpectations(DownloadDangerPrompt::CANCEL,
220 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT, 187 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT,
221 ClientDownloadResponse::UNCOMMON); 188 ClientDownloadResponse::UNCOMMON);
189 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true));
222 SimulatePromptAction(DownloadDangerPrompt::CANCEL); 190 SimulatePromptAction(DownloadDangerPrompt::CANCEL);
223 VerifyExpectations(); 191 VerifyExpectations(true);
224 192
225 // If the download is no longer dangerous (because it was accepted), the 193 // If the download is no longer dangerous (because it was accepted), the
226 // dialog should DISMISS itself. 194 // dialog should DISMISS itself.
227 SetUpExpectations(DownloadDangerPrompt::DISMISS, 195 SetUpExpectations(DownloadDangerPrompt::DISMISS,
228 content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED, 196 content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED,
229 ClientDownloadResponse::POTENTIALLY_UNWANTED); 197 ClientDownloadResponse::POTENTIALLY_UNWANTED);
230 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(false)); 198 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(false));
231 download().NotifyObserversDownloadUpdated(); 199 download().NotifyObserversDownloadUpdated();
232 VerifyExpectations(); 200 VerifyExpectations(false);
233 201
234 // If the download is in a terminal state then the dialog should DISMISS 202 // If the download is in a terminal state then the dialog should DISMISS
235 // itself. 203 // itself.
236 SetUpExpectations(DownloadDangerPrompt::DISMISS, 204 SetUpExpectations(DownloadDangerPrompt::DISMISS,
237 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST, 205 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST,
238 ClientDownloadResponse::DANGEROUS_HOST); 206 ClientDownloadResponse::DANGEROUS_HOST);
239 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true)); 207 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true));
240 EXPECT_CALL(download(), IsDone()).WillOnce(Return(true)); 208 EXPECT_CALL(download(), IsDone()).WillRepeatedly(Return(true));
241 download().NotifyObserversDownloadUpdated(); 209 download().NotifyObserversDownloadUpdated();
242 VerifyExpectations(); 210 VerifyExpectations(false);
243 211
244 // If the download is dangerous and is not in a terminal state, don't dismiss 212 // If the download is dangerous and is not in a terminal state, don't dismiss
245 // the dialog. 213 // the dialog.
246 SetUpExpectations(DownloadDangerPrompt::ACCEPT, 214 SetUpExpectations(DownloadDangerPrompt::ACCEPT,
247 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT, 215 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT,
248 ClientDownloadResponse::DANGEROUS); 216 ClientDownloadResponse::DANGEROUS);
249 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true)); 217 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true));
250 EXPECT_CALL(download(), IsDone()).WillOnce(Return(false)); 218 EXPECT_CALL(download(), IsDone()).WillRepeatedly(Return(false));
251 download().NotifyObserversDownloadUpdated(); 219 download().NotifyObserversDownloadUpdated();
220 EXPECT_TRUE(prompt());
252 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); 221 SimulatePromptAction(DownloadDangerPrompt::ACCEPT);
253 VerifyExpectations(); 222 VerifyExpectations(true);
223
224 // If the download is not dangerous, no report will be sent.
225 SetUpExpectations(DownloadDangerPrompt::ACCEPT,
226 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
227 ClientDownloadResponse::SAFE);
228 SimulatePromptAction(DownloadDangerPrompt::ACCEPT);
229 VerifyExpectations(false);
254 230
255 // If the containing tab is closed, the dialog should DISMISS itself. 231 // If the containing tab is closed, the dialog should DISMISS itself.
256 OpenNewTab(); 232 OpenNewTab();
257 SetUpExpectations(DownloadDangerPrompt::DISMISS, 233 SetUpExpectations(DownloadDangerPrompt::DISMISS,
258 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, 234 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
259 ClientDownloadResponse::DANGEROUS); 235 ClientDownloadResponse::DANGEROUS);
260 chrome::CloseTab(browser()); 236 chrome::CloseTab(browser());
261 VerifyExpectations(); 237 VerifyExpectations(false);
262 } 238 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_danger_prompt.cc ('k') | chrome/browser/prerender/prerender_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698