OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/safe_browsing/download_feedback_service.h" | 5 #include "chrome/browser/safe_browsing/download_feedback_service.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 | 106 |
107 size_t num_feedbacks() const { | 107 size_t num_feedbacks() const { |
108 return feedbacks_.size(); | 108 return feedbacks_.size(); |
109 } | 109 } |
110 | 110 |
111 private: | 111 private: |
112 std::vector<FakeDownloadFeedback*> feedbacks_; | 112 std::vector<FakeDownloadFeedback*> feedbacks_; |
113 }; | 113 }; |
114 | 114 |
115 bool WillStorePings(DownloadProtectionService::DownloadCheckResult result, | 115 bool WillStorePings(DownloadProtectionService::DownloadCheckResult result, |
| 116 bool upload_requested, |
116 int64_t size) { | 117 int64_t size) { |
117 content::MockDownloadItem item; | 118 content::MockDownloadItem item; |
118 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(size)); | 119 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(size)); |
119 | 120 |
120 EXPECT_FALSE(DownloadFeedbackService::IsEnabledForDownload(item)); | 121 EXPECT_FALSE(DownloadFeedbackService::IsEnabledForDownload(item)); |
121 DownloadFeedbackService::MaybeStorePingsForDownload(result, &item, "a", "b"); | 122 DownloadFeedbackService::MaybeStorePingsForDownload(result, upload_requested, |
| 123 &item, "a", "b"); |
122 return DownloadFeedbackService::IsEnabledForDownload(item); | 124 return DownloadFeedbackService::IsEnabledForDownload(item); |
123 } | 125 } |
124 | 126 |
125 } // namespace | 127 } // namespace |
126 | 128 |
127 class DownloadFeedbackServiceTest : public testing::Test { | 129 class DownloadFeedbackServiceTest : public testing::Test { |
128 public: | 130 public: |
129 DownloadFeedbackServiceTest() | 131 DownloadFeedbackServiceTest() |
130 : file_task_runner_(content::BrowserThread::GetTaskRunnerForThread( | 132 : file_task_runner_(content::BrowserThread::GetTaskRunnerForThread( |
131 content::BrowserThread::FILE)), | 133 content::BrowserThread::FILE)), |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 167 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
166 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 168 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
167 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 169 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
168 FakeDownloadFeedbackFactory download_feedback_factory_; | 170 FakeDownloadFeedbackFactory download_feedback_factory_; |
169 }; | 171 }; |
170 | 172 |
171 TEST_F(DownloadFeedbackServiceTest, MaybeStorePingsForDownload) { | 173 TEST_F(DownloadFeedbackServiceTest, MaybeStorePingsForDownload) { |
172 const int64_t ok_size = DownloadFeedback::kMaxUploadSize; | 174 const int64_t ok_size = DownloadFeedback::kMaxUploadSize; |
173 const int64_t bad_size = DownloadFeedback::kMaxUploadSize + 1; | 175 const int64_t bad_size = DownloadFeedback::kMaxUploadSize + 1; |
174 | 176 |
175 EXPECT_FALSE(WillStorePings(DownloadProtectionService::SAFE, ok_size)); | 177 std::vector<bool> upload_requests = {false, true}; |
176 EXPECT_FALSE(WillStorePings(DownloadProtectionService::DANGEROUS, ok_size)); | 178 for (bool request_upload : upload_requests) { |
177 EXPECT_TRUE(WillStorePings(DownloadProtectionService::UNCOMMON, ok_size)); | 179 // SAFE and DANGEROUS will only upload if |request_upload| |
178 EXPECT_TRUE( | 180 EXPECT_EQ(request_upload, WillStorePings(DownloadProtectionService::SAFE, |
179 WillStorePings(DownloadProtectionService::DANGEROUS_HOST, ok_size)); | 181 request_upload, ok_size)); |
180 EXPECT_TRUE( | 182 EXPECT_EQ(request_upload, |
181 WillStorePings(DownloadProtectionService::POTENTIALLY_UNWANTED, ok_size)); | 183 WillStorePings(DownloadProtectionService::DANGEROUS, |
| 184 request_upload, ok_size)); |
182 | 185 |
183 EXPECT_FALSE(WillStorePings(DownloadProtectionService::SAFE, bad_size)); | 186 EXPECT_TRUE(WillStorePings(DownloadProtectionService::UNCOMMON, |
184 EXPECT_FALSE(WillStorePings(DownloadProtectionService::DANGEROUS, bad_size)); | 187 request_upload, ok_size)); |
185 EXPECT_FALSE(WillStorePings(DownloadProtectionService::UNCOMMON, bad_size)); | 188 EXPECT_TRUE(WillStorePings(DownloadProtectionService::DANGEROUS_HOST, |
186 EXPECT_FALSE( | 189 request_upload, ok_size)); |
187 WillStorePings(DownloadProtectionService::DANGEROUS_HOST, bad_size)); | 190 EXPECT_TRUE(WillStorePings(DownloadProtectionService::POTENTIALLY_UNWANTED, |
188 EXPECT_FALSE(WillStorePings(DownloadProtectionService::POTENTIALLY_UNWANTED, | 191 request_upload, ok_size)); |
189 bad_size)); | 192 |
| 193 // Bad sizes never upload |
| 194 EXPECT_FALSE(WillStorePings(DownloadProtectionService::SAFE, request_upload, |
| 195 bad_size)); |
| 196 EXPECT_FALSE(WillStorePings(DownloadProtectionService::DANGEROUS, |
| 197 request_upload, bad_size)); |
| 198 EXPECT_FALSE(WillStorePings(DownloadProtectionService::UNCOMMON, |
| 199 request_upload, bad_size)); |
| 200 EXPECT_FALSE(WillStorePings(DownloadProtectionService::DANGEROUS_HOST, |
| 201 request_upload, bad_size)); |
| 202 EXPECT_FALSE(WillStorePings(DownloadProtectionService::POTENTIALLY_UNWANTED, |
| 203 request_upload, bad_size)); |
| 204 } |
190 } | 205 } |
191 | 206 |
192 TEST_F(DownloadFeedbackServiceTest, SingleFeedbackCompleteAndDiscardDownload) { | 207 TEST_F(DownloadFeedbackServiceTest, SingleFeedbackCompleteAndDiscardDownload) { |
193 const base::FilePath file_path(CreateTestFile(0)); | 208 const base::FilePath file_path(CreateTestFile(0)); |
194 const std::string ping_request = "ping"; | 209 const std::string ping_request = "ping"; |
195 const std::string ping_response = "resp"; | 210 const std::string ping_response = "resp"; |
196 | 211 |
197 content::DownloadItem::AcquireFileCallback download_discarded_callback; | 212 content::DownloadItem::AcquireFileCallback download_discarded_callback; |
198 | 213 |
199 content::MockDownloadItem item; | 214 content::MockDownloadItem item; |
200 EXPECT_CALL(item, GetDangerType()) | 215 EXPECT_CALL(item, GetDangerType()) |
201 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); | 216 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); |
202 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(1000)); | 217 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(1000)); |
203 EXPECT_CALL(item, | 218 EXPECT_CALL(item, |
204 StealDangerousDownload(true /*delete_file_after_feedback*/, _)) | 219 StealDangerousDownload(true /*delete_file_after_feedback*/, _)) |
205 .WillOnce(SaveArg<1>(&download_discarded_callback)); | 220 .WillOnce(SaveArg<1>(&download_discarded_callback)); |
206 | 221 |
207 DownloadFeedbackService service(request_context_getter_.get(), | 222 DownloadFeedbackService service(request_context_getter_.get(), |
208 file_task_runner_.get()); | 223 file_task_runner_.get()); |
209 service.MaybeStorePingsForDownload( | 224 service.MaybeStorePingsForDownload(DownloadProtectionService::UNCOMMON, |
210 DownloadProtectionService::UNCOMMON, &item, ping_request, ping_response); | 225 false /* upload_requested */, &item, |
| 226 ping_request, ping_response); |
211 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item)); | 227 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item)); |
212 service.BeginFeedbackForDownload(&item, DownloadCommands::DISCARD); | 228 service.BeginFeedbackForDownload(&item, DownloadCommands::DISCARD); |
213 ASSERT_FALSE(download_discarded_callback.is_null()); | 229 ASSERT_FALSE(download_discarded_callback.is_null()); |
214 EXPECT_EQ(0U, num_feedbacks()); | 230 EXPECT_EQ(0U, num_feedbacks()); |
215 | 231 |
216 download_discarded_callback.Run(file_path); | 232 download_discarded_callback.Run(file_path); |
217 ASSERT_EQ(1U, num_feedbacks()); | 233 ASSERT_EQ(1U, num_feedbacks()); |
218 ASSERT_TRUE(feedback(0)); | 234 ASSERT_TRUE(feedback(0)); |
219 EXPECT_TRUE(feedback(0)->start_called()); | 235 EXPECT_TRUE(feedback(0)->start_called()); |
220 EXPECT_EQ(ping_request, feedback(0)->GetPingRequestForTesting()); | 236 EXPECT_EQ(ping_request, feedback(0)->GetPingRequestForTesting()); |
(...skipping 19 matching lines...) Expand all Loading... |
240 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(1000)); | 256 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(1000)); |
241 EXPECT_CALL(item, | 257 EXPECT_CALL(item, |
242 StealDangerousDownload(false /*delete_file_after_feedback*/, _)) | 258 StealDangerousDownload(false /*delete_file_after_feedback*/, _)) |
243 .WillOnce(SaveArg<1>(&download_discarded_callback)); | 259 .WillOnce(SaveArg<1>(&download_discarded_callback)); |
244 EXPECT_CALL(item, ValidateDangerousDownload()).Times(1); | 260 EXPECT_CALL(item, ValidateDangerousDownload()).Times(1); |
245 GURL empty_url; | 261 GURL empty_url; |
246 EXPECT_CALL(item, GetURL()).WillOnce(ReturnRef(empty_url)); | 262 EXPECT_CALL(item, GetURL()).WillOnce(ReturnRef(empty_url)); |
247 | 263 |
248 DownloadFeedbackService service(request_context_getter_.get(), | 264 DownloadFeedbackService service(request_context_getter_.get(), |
249 file_task_runner_.get()); | 265 file_task_runner_.get()); |
250 service.MaybeStorePingsForDownload(DownloadProtectionService::UNCOMMON, &item, | 266 service.MaybeStorePingsForDownload(DownloadProtectionService::UNCOMMON, |
| 267 false /* upload_requested */, &item, |
251 ping_request, ping_response); | 268 ping_request, ping_response); |
252 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item)); | 269 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item)); |
253 service.BeginFeedbackForDownload(&item, DownloadCommands::KEEP); | 270 service.BeginFeedbackForDownload(&item, DownloadCommands::KEEP); |
254 ASSERT_FALSE(download_discarded_callback.is_null()); | 271 ASSERT_FALSE(download_discarded_callback.is_null()); |
255 EXPECT_EQ(0U, num_feedbacks()); | 272 EXPECT_EQ(0U, num_feedbacks()); |
256 | 273 |
257 download_discarded_callback.Run(file_path); | 274 download_discarded_callback.Run(file_path); |
258 ASSERT_EQ(1U, num_feedbacks()); | 275 ASSERT_EQ(1U, num_feedbacks()); |
259 ASSERT_TRUE(feedback(0)); | 276 ASSERT_TRUE(feedback(0)); |
260 EXPECT_TRUE(feedback(0)->start_called()); | 277 EXPECT_TRUE(feedback(0)->start_called()); |
(...skipping 18 matching lines...) Expand all Loading... |
279 base::FilePath file_path[kNumDownloads]; | 296 base::FilePath file_path[kNumDownloads]; |
280 content::MockDownloadItem item[kNumDownloads]; | 297 content::MockDownloadItem item[kNumDownloads]; |
281 for (size_t i = 0; i < kNumDownloads; ++i) { | 298 for (size_t i = 0; i < kNumDownloads; ++i) { |
282 file_path[i] = CreateTestFile(i); | 299 file_path[i] = CreateTestFile(i); |
283 EXPECT_CALL(item[i], GetDangerType()) | 300 EXPECT_CALL(item[i], GetDangerType()) |
284 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); | 301 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); |
285 EXPECT_CALL(item[i], GetReceivedBytes()).WillRepeatedly(Return(1000)); | 302 EXPECT_CALL(item[i], GetReceivedBytes()).WillRepeatedly(Return(1000)); |
286 EXPECT_CALL(item[i], StealDangerousDownload(true, _)) | 303 EXPECT_CALL(item[i], StealDangerousDownload(true, _)) |
287 .WillOnce(SaveArg<1>(&download_discarded_callback[i])); | 304 .WillOnce(SaveArg<1>(&download_discarded_callback[i])); |
288 DownloadFeedbackService::MaybeStorePingsForDownload( | 305 DownloadFeedbackService::MaybeStorePingsForDownload( |
289 DownloadProtectionService::UNCOMMON, &item[i], ping_request, | 306 DownloadProtectionService::UNCOMMON, false /* upload_requested */, |
290 ping_response); | 307 &item[i], ping_request, ping_response); |
291 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item[i])); | 308 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item[i])); |
292 } | 309 } |
293 | 310 |
294 { | 311 { |
295 DownloadFeedbackService service(request_context_getter_.get(), | 312 DownloadFeedbackService service(request_context_getter_.get(), |
296 file_task_runner_.get()); | 313 file_task_runner_.get()); |
297 for (size_t i = 0; i < kNumDownloads; ++i) { | 314 for (size_t i = 0; i < kNumDownloads; ++i) { |
298 SCOPED_TRACE(i); | 315 SCOPED_TRACE(i); |
299 service.BeginFeedbackForDownload(&item[i], DownloadCommands::DISCARD); | 316 service.BeginFeedbackForDownload(&item[i], DownloadCommands::DISCARD); |
300 ASSERT_FALSE(download_discarded_callback[i].is_null()); | 317 ASSERT_FALSE(download_discarded_callback[i].is_null()); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 base::FilePath file_path[kNumDownloads]; | 365 base::FilePath file_path[kNumDownloads]; |
349 content::MockDownloadItem item[kNumDownloads]; | 366 content::MockDownloadItem item[kNumDownloads]; |
350 for (size_t i = 0; i < kNumDownloads; ++i) { | 367 for (size_t i = 0; i < kNumDownloads; ++i) { |
351 file_path[i] = CreateTestFile(i); | 368 file_path[i] = CreateTestFile(i); |
352 EXPECT_CALL(item[i], GetDangerType()) | 369 EXPECT_CALL(item[i], GetDangerType()) |
353 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); | 370 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); |
354 EXPECT_CALL(item[i], GetReceivedBytes()).WillRepeatedly(Return(1000)); | 371 EXPECT_CALL(item[i], GetReceivedBytes()).WillRepeatedly(Return(1000)); |
355 EXPECT_CALL(item[i], StealDangerousDownload(true, _)) | 372 EXPECT_CALL(item[i], StealDangerousDownload(true, _)) |
356 .WillOnce(SaveArg<1>(&download_discarded_callback[i])); | 373 .WillOnce(SaveArg<1>(&download_discarded_callback[i])); |
357 DownloadFeedbackService::MaybeStorePingsForDownload( | 374 DownloadFeedbackService::MaybeStorePingsForDownload( |
358 DownloadProtectionService::UNCOMMON, &item[i], ping_request, | 375 DownloadProtectionService::UNCOMMON, false /* upload_requested */, |
359 ping_response); | 376 &item[i], ping_request, ping_response); |
360 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item[i])); | 377 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item[i])); |
361 } | 378 } |
362 | 379 |
363 { | 380 { |
364 DownloadFeedbackService service(request_context_getter_.get(), | 381 DownloadFeedbackService service(request_context_getter_.get(), |
365 file_task_runner_.get()); | 382 file_task_runner_.get()); |
366 for (size_t i = 0; i < kNumDownloads; ++i) { | 383 for (size_t i = 0; i < kNumDownloads; ++i) { |
367 SCOPED_TRACE(i); | 384 SCOPED_TRACE(i); |
368 service.BeginFeedbackForDownload(&item[i], DownloadCommands::DISCARD); | 385 service.BeginFeedbackForDownload(&item[i], DownloadCommands::DISCARD); |
369 ASSERT_FALSE(download_discarded_callback[i].is_null()); | 386 ASSERT_FALSE(download_discarded_callback[i].is_null()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 // was deleted. | 421 // was deleted. |
405 EXPECT_FALSE(base::PathExists(file_path[2])); | 422 EXPECT_FALSE(base::PathExists(file_path[2])); |
406 | 423 |
407 // These files should still exist since the FakeDownloadFeedback does not | 424 // These files should still exist since the FakeDownloadFeedback does not |
408 // delete them. | 425 // delete them. |
409 EXPECT_TRUE(base::PathExists(file_path[0])); | 426 EXPECT_TRUE(base::PathExists(file_path[0])); |
410 EXPECT_TRUE(base::PathExists(file_path[1])); | 427 EXPECT_TRUE(base::PathExists(file_path[1])); |
411 } | 428 } |
412 | 429 |
413 } // namespace safe_browsing | 430 } // namespace safe_browsing |
OLD | NEW |