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

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

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

Powered by Google App Engine
This is Rietveld 408576698