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 upload_requested : upload_requests) { |
177 EXPECT_TRUE(WillStorePings(DownloadProtectionService::UNCOMMON, ok_size)); | 179 // SAFE will never upload |
178 EXPECT_TRUE( | 180 EXPECT_FALSE(WillStorePings(DownloadProtectionService::SAFE, |
179 WillStorePings(DownloadProtectionService::DANGEROUS_HOST, ok_size)); | 181 upload_requested, ok_size)); |
180 EXPECT_TRUE( | 182 // Others will upload if requested. |
181 WillStorePings(DownloadProtectionService::POTENTIALLY_UNWANTED, ok_size)); | 183 EXPECT_EQ(upload_requested, |
| 184 WillStorePings(DownloadProtectionService::UNKNOWN, |
| 185 upload_requested, ok_size)); |
| 186 EXPECT_EQ(upload_requested, |
| 187 WillStorePings(DownloadProtectionService::DANGEROUS, |
| 188 upload_requested, ok_size)); |
| 189 EXPECT_EQ(upload_requested, |
| 190 WillStorePings(DownloadProtectionService::UNCOMMON, |
| 191 upload_requested, ok_size)); |
| 192 EXPECT_EQ(upload_requested, |
| 193 WillStorePings(DownloadProtectionService::DANGEROUS_HOST, |
| 194 upload_requested, ok_size)); |
| 195 EXPECT_EQ(upload_requested, |
| 196 WillStorePings(DownloadProtectionService::POTENTIALLY_UNWANTED, |
| 197 upload_requested, ok_size)); |
182 | 198 |
183 EXPECT_FALSE(WillStorePings(DownloadProtectionService::SAFE, bad_size)); | 199 // Bad sizes never upload |
184 EXPECT_FALSE(WillStorePings(DownloadProtectionService::DANGEROUS, bad_size)); | 200 EXPECT_FALSE(WillStorePings(DownloadProtectionService::SAFE, |
185 EXPECT_FALSE(WillStorePings(DownloadProtectionService::UNCOMMON, bad_size)); | 201 upload_requested, bad_size)); |
186 EXPECT_FALSE( | 202 EXPECT_FALSE(WillStorePings(DownloadProtectionService::UNKNOWN, |
187 WillStorePings(DownloadProtectionService::DANGEROUS_HOST, bad_size)); | 203 upload_requested, bad_size)); |
188 EXPECT_FALSE(WillStorePings(DownloadProtectionService::POTENTIALLY_UNWANTED, | 204 EXPECT_FALSE(WillStorePings(DownloadProtectionService::DANGEROUS, |
189 bad_size)); | 205 upload_requested, bad_size)); |
| 206 EXPECT_FALSE(WillStorePings(DownloadProtectionService::UNCOMMON, |
| 207 upload_requested, bad_size)); |
| 208 EXPECT_FALSE(WillStorePings(DownloadProtectionService::DANGEROUS_HOST, |
| 209 upload_requested, bad_size)); |
| 210 EXPECT_FALSE(WillStorePings(DownloadProtectionService::POTENTIALLY_UNWANTED, |
| 211 upload_requested, bad_size)); |
| 212 } |
190 } | 213 } |
191 | 214 |
192 TEST_F(DownloadFeedbackServiceTest, SingleFeedbackCompleteAndDiscardDownload) { | 215 TEST_F(DownloadFeedbackServiceTest, SingleFeedbackCompleteAndDiscardDownload) { |
193 const base::FilePath file_path(CreateTestFile(0)); | 216 const base::FilePath file_path(CreateTestFile(0)); |
194 const std::string ping_request = "ping"; | 217 const std::string ping_request = "ping"; |
195 const std::string ping_response = "resp"; | 218 const std::string ping_response = "resp"; |
196 | 219 |
197 content::DownloadItem::AcquireFileCallback download_discarded_callback; | 220 content::DownloadItem::AcquireFileCallback download_discarded_callback; |
198 | 221 |
199 content::MockDownloadItem item; | 222 content::MockDownloadItem item; |
200 EXPECT_CALL(item, GetDangerType()) | 223 EXPECT_CALL(item, GetDangerType()) |
201 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); | 224 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); |
202 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(1000)); | 225 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(1000)); |
203 EXPECT_CALL(item, | 226 EXPECT_CALL(item, |
204 StealDangerousDownload(true /*delete_file_after_feedback*/, _)) | 227 StealDangerousDownload(true /*delete_file_after_feedback*/, _)) |
205 .WillOnce(SaveArg<1>(&download_discarded_callback)); | 228 .WillOnce(SaveArg<1>(&download_discarded_callback)); |
206 | 229 |
207 DownloadFeedbackService service(request_context_getter_.get(), | 230 DownloadFeedbackService service(request_context_getter_.get(), |
208 file_task_runner_.get()); | 231 file_task_runner_.get()); |
209 service.MaybeStorePingsForDownload( | 232 service.MaybeStorePingsForDownload(DownloadProtectionService::UNCOMMON, |
210 DownloadProtectionService::UNCOMMON, &item, ping_request, ping_response); | 233 true /* upload_requested */, &item, |
| 234 ping_request, ping_response); |
211 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item)); | 235 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item)); |
212 service.BeginFeedbackForDownload(&item, DownloadCommands::DISCARD); | 236 service.BeginFeedbackForDownload(&item, DownloadCommands::DISCARD); |
213 ASSERT_FALSE(download_discarded_callback.is_null()); | 237 ASSERT_FALSE(download_discarded_callback.is_null()); |
214 EXPECT_EQ(0U, num_feedbacks()); | 238 EXPECT_EQ(0U, num_feedbacks()); |
215 | 239 |
216 download_discarded_callback.Run(file_path); | 240 download_discarded_callback.Run(file_path); |
217 ASSERT_EQ(1U, num_feedbacks()); | 241 ASSERT_EQ(1U, num_feedbacks()); |
218 ASSERT_TRUE(feedback(0)); | 242 ASSERT_TRUE(feedback(0)); |
219 EXPECT_TRUE(feedback(0)->start_called()); | 243 EXPECT_TRUE(feedback(0)->start_called()); |
220 EXPECT_EQ(ping_request, feedback(0)->GetPingRequestForTesting()); | 244 EXPECT_EQ(ping_request, feedback(0)->GetPingRequestForTesting()); |
(...skipping 19 matching lines...) Expand all Loading... |
240 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(1000)); | 264 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(1000)); |
241 EXPECT_CALL(item, | 265 EXPECT_CALL(item, |
242 StealDangerousDownload(false /*delete_file_after_feedback*/, _)) | 266 StealDangerousDownload(false /*delete_file_after_feedback*/, _)) |
243 .WillOnce(SaveArg<1>(&download_discarded_callback)); | 267 .WillOnce(SaveArg<1>(&download_discarded_callback)); |
244 EXPECT_CALL(item, ValidateDangerousDownload()).Times(1); | 268 EXPECT_CALL(item, ValidateDangerousDownload()).Times(1); |
245 GURL empty_url; | 269 GURL empty_url; |
246 EXPECT_CALL(item, GetURL()).WillOnce(ReturnRef(empty_url)); | 270 EXPECT_CALL(item, GetURL()).WillOnce(ReturnRef(empty_url)); |
247 | 271 |
248 DownloadFeedbackService service(request_context_getter_.get(), | 272 DownloadFeedbackService service(request_context_getter_.get(), |
249 file_task_runner_.get()); | 273 file_task_runner_.get()); |
250 service.MaybeStorePingsForDownload(DownloadProtectionService::UNCOMMON, &item, | 274 service.MaybeStorePingsForDownload(DownloadProtectionService::UNCOMMON, |
| 275 true /* upload_requested */, &item, |
251 ping_request, ping_response); | 276 ping_request, ping_response); |
252 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item)); | 277 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item)); |
253 service.BeginFeedbackForDownload(&item, DownloadCommands::KEEP); | 278 service.BeginFeedbackForDownload(&item, DownloadCommands::KEEP); |
254 ASSERT_FALSE(download_discarded_callback.is_null()); | 279 ASSERT_FALSE(download_discarded_callback.is_null()); |
255 EXPECT_EQ(0U, num_feedbacks()); | 280 EXPECT_EQ(0U, num_feedbacks()); |
256 | 281 |
257 download_discarded_callback.Run(file_path); | 282 download_discarded_callback.Run(file_path); |
258 ASSERT_EQ(1U, num_feedbacks()); | 283 ASSERT_EQ(1U, num_feedbacks()); |
259 ASSERT_TRUE(feedback(0)); | 284 ASSERT_TRUE(feedback(0)); |
260 EXPECT_TRUE(feedback(0)->start_called()); | 285 EXPECT_TRUE(feedback(0)->start_called()); |
(...skipping 18 matching lines...) Expand all Loading... |
279 base::FilePath file_path[kNumDownloads]; | 304 base::FilePath file_path[kNumDownloads]; |
280 content::MockDownloadItem item[kNumDownloads]; | 305 content::MockDownloadItem item[kNumDownloads]; |
281 for (size_t i = 0; i < kNumDownloads; ++i) { | 306 for (size_t i = 0; i < kNumDownloads; ++i) { |
282 file_path[i] = CreateTestFile(i); | 307 file_path[i] = CreateTestFile(i); |
283 EXPECT_CALL(item[i], GetDangerType()) | 308 EXPECT_CALL(item[i], GetDangerType()) |
284 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); | 309 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); |
285 EXPECT_CALL(item[i], GetReceivedBytes()).WillRepeatedly(Return(1000)); | 310 EXPECT_CALL(item[i], GetReceivedBytes()).WillRepeatedly(Return(1000)); |
286 EXPECT_CALL(item[i], StealDangerousDownload(true, _)) | 311 EXPECT_CALL(item[i], StealDangerousDownload(true, _)) |
287 .WillOnce(SaveArg<1>(&download_discarded_callback[i])); | 312 .WillOnce(SaveArg<1>(&download_discarded_callback[i])); |
288 DownloadFeedbackService::MaybeStorePingsForDownload( | 313 DownloadFeedbackService::MaybeStorePingsForDownload( |
289 DownloadProtectionService::UNCOMMON, &item[i], ping_request, | 314 DownloadProtectionService::UNCOMMON, true /* upload_requested */, |
290 ping_response); | 315 &item[i], ping_request, ping_response); |
291 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item[i])); | 316 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item[i])); |
292 } | 317 } |
293 | 318 |
294 { | 319 { |
295 DownloadFeedbackService service(request_context_getter_.get(), | 320 DownloadFeedbackService service(request_context_getter_.get(), |
296 file_task_runner_.get()); | 321 file_task_runner_.get()); |
297 for (size_t i = 0; i < kNumDownloads; ++i) { | 322 for (size_t i = 0; i < kNumDownloads; ++i) { |
298 SCOPED_TRACE(i); | 323 SCOPED_TRACE(i); |
299 service.BeginFeedbackForDownload(&item[i], DownloadCommands::DISCARD); | 324 service.BeginFeedbackForDownload(&item[i], DownloadCommands::DISCARD); |
300 ASSERT_FALSE(download_discarded_callback[i].is_null()); | 325 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]; | 373 base::FilePath file_path[kNumDownloads]; |
349 content::MockDownloadItem item[kNumDownloads]; | 374 content::MockDownloadItem item[kNumDownloads]; |
350 for (size_t i = 0; i < kNumDownloads; ++i) { | 375 for (size_t i = 0; i < kNumDownloads; ++i) { |
351 file_path[i] = CreateTestFile(i); | 376 file_path[i] = CreateTestFile(i); |
352 EXPECT_CALL(item[i], GetDangerType()) | 377 EXPECT_CALL(item[i], GetDangerType()) |
353 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); | 378 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT)); |
354 EXPECT_CALL(item[i], GetReceivedBytes()).WillRepeatedly(Return(1000)); | 379 EXPECT_CALL(item[i], GetReceivedBytes()).WillRepeatedly(Return(1000)); |
355 EXPECT_CALL(item[i], StealDangerousDownload(true, _)) | 380 EXPECT_CALL(item[i], StealDangerousDownload(true, _)) |
356 .WillOnce(SaveArg<1>(&download_discarded_callback[i])); | 381 .WillOnce(SaveArg<1>(&download_discarded_callback[i])); |
357 DownloadFeedbackService::MaybeStorePingsForDownload( | 382 DownloadFeedbackService::MaybeStorePingsForDownload( |
358 DownloadProtectionService::UNCOMMON, &item[i], ping_request, | 383 DownloadProtectionService::UNCOMMON, true /* upload_requested */, |
359 ping_response); | 384 &item[i], ping_request, ping_response); |
360 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item[i])); | 385 ASSERT_TRUE(DownloadFeedbackService::IsEnabledForDownload(item[i])); |
361 } | 386 } |
362 | 387 |
363 { | 388 { |
364 DownloadFeedbackService service(request_context_getter_.get(), | 389 DownloadFeedbackService service(request_context_getter_.get(), |
365 file_task_runner_.get()); | 390 file_task_runner_.get()); |
366 for (size_t i = 0; i < kNumDownloads; ++i) { | 391 for (size_t i = 0; i < kNumDownloads; ++i) { |
367 SCOPED_TRACE(i); | 392 SCOPED_TRACE(i); |
368 service.BeginFeedbackForDownload(&item[i], DownloadCommands::DISCARD); | 393 service.BeginFeedbackForDownload(&item[i], DownloadCommands::DISCARD); |
369 ASSERT_FALSE(download_discarded_callback[i].is_null()); | 394 ASSERT_FALSE(download_discarded_callback[i].is_null()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 // was deleted. | 429 // was deleted. |
405 EXPECT_FALSE(base::PathExists(file_path[2])); | 430 EXPECT_FALSE(base::PathExists(file_path[2])); |
406 | 431 |
407 // These files should still exist since the FakeDownloadFeedback does not | 432 // These files should still exist since the FakeDownloadFeedback does not |
408 // delete them. | 433 // delete them. |
409 EXPECT_TRUE(base::PathExists(file_path[0])); | 434 EXPECT_TRUE(base::PathExists(file_path[0])); |
410 EXPECT_TRUE(base::PathExists(file_path[1])); | 435 EXPECT_TRUE(base::PathExists(file_path[1])); |
411 } | 436 } |
412 | 437 |
413 } // namespace safe_browsing | 438 } // namespace safe_browsing |
OLD | NEW |