OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_protection_service.h" | 5 #include "chrome/browser/safe_browsing/download_protection_service.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 BrowserThread::UI, | 183 BrowserThread::UI, |
184 FROM_HERE, | 184 FROM_HERE, |
185 base::Bind(&DownloadProtectionServiceTest::PostRunMessageLoopTask, | 185 base::Bind(&DownloadProtectionServiceTest::PostRunMessageLoopTask, |
186 base::Unretained(this), thread)); | 186 base::Unretained(this), thread)); |
187 msg_loop_.Run(); | 187 msg_loop_.Run(); |
188 } | 188 } |
189 | 189 |
190 public: | 190 public: |
191 void CheckDoneCallback( | 191 void CheckDoneCallback( |
192 DownloadProtectionService::DownloadCheckResult result) { | 192 DownloadProtectionService::DownloadCheckResult result) { |
193 result_ = result; | 193 result_.reset(new DownloadProtectionService::DownloadCheckResult(result)); |
194 msg_loop_.Quit(); | 194 msg_loop_.Quit(); |
195 } | 195 } |
196 | 196 |
197 void SendURLFetchComplete(TestURLFetcher* fetcher) { | 197 void SendURLFetchComplete(TestURLFetcher* fetcher) { |
198 fetcher->delegate()->OnURLFetchComplete(fetcher); | 198 fetcher->delegate()->OnURLFetchComplete(fetcher); |
199 } | 199 } |
200 | 200 |
| 201 void ExpectResult(DownloadProtectionService::DownloadCheckResult expected) { |
| 202 ASSERT_TRUE(result_.get()); |
| 203 EXPECT_EQ(expected, *result_); |
| 204 } |
| 205 |
201 protected: | 206 protected: |
202 scoped_refptr<MockSafeBrowsingService> sb_service_; | 207 scoped_refptr<MockSafeBrowsingService> sb_service_; |
203 scoped_refptr<MockSignatureUtil> signature_util_; | 208 scoped_refptr<MockSignatureUtil> signature_util_; |
204 DownloadProtectionService* download_service_; | 209 DownloadProtectionService* download_service_; |
205 MessageLoop msg_loop_; | 210 MessageLoop msg_loop_; |
206 DownloadProtectionService::DownloadCheckResult result_; | 211 scoped_ptr<DownloadProtectionService::DownloadCheckResult> result_; |
207 scoped_ptr<content::TestBrowserThread> io_thread_; | 212 scoped_ptr<content::TestBrowserThread> io_thread_; |
208 scoped_ptr<content::TestBrowserThread> file_thread_; | 213 scoped_ptr<content::TestBrowserThread> file_thread_; |
209 scoped_ptr<content::TestBrowserThread> ui_thread_; | 214 scoped_ptr<content::TestBrowserThread> ui_thread_; |
210 }; | 215 }; |
211 | 216 |
212 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { | 217 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { |
213 DownloadProtectionService::DownloadInfo info; | 218 DownloadProtectionService::DownloadInfo info; |
214 download_service_->CheckClientDownload( | 219 download_service_->CheckClientDownload( |
215 info, | 220 info, |
216 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 221 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
217 base::Unretained(this))); | 222 base::Unretained(this))); |
218 msg_loop_.Run(); | 223 msg_loop_.Run(); |
219 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 224 ExpectResult(DownloadProtectionService::SAFE); |
220 | 225 |
221 // Only https is not supported for now for privacy reasons. | 226 // Only https is not supported for now for privacy reasons. |
222 info.local_file = FilePath(FILE_PATH_LITERAL("a.tmp")); | 227 info.local_file = FilePath(FILE_PATH_LITERAL("a.tmp")); |
223 info.target_file = FilePath(FILE_PATH_LITERAL("a.exe")); | 228 info.target_file = FilePath(FILE_PATH_LITERAL("a.exe")); |
224 info.download_url_chain.push_back(GURL("https://www.google.com/")); | 229 info.download_url_chain.push_back(GURL("https://www.google.com/")); |
225 download_service_->CheckClientDownload( | 230 download_service_->CheckClientDownload( |
226 info, | 231 info, |
227 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 232 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
228 base::Unretained(this))); | 233 base::Unretained(this))); |
229 msg_loop_.Run(); | 234 msg_loop_.Run(); |
230 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 235 ExpectResult(DownloadProtectionService::SAFE); |
231 } | 236 } |
232 | 237 |
233 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) { | 238 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) { |
234 DownloadProtectionService::DownloadInfo info; | 239 DownloadProtectionService::DownloadInfo info; |
235 info.local_file = FilePath(FILE_PATH_LITERAL("a.tmp")); | 240 info.local_file = FilePath(FILE_PATH_LITERAL("a.tmp")); |
236 info.target_file = FilePath(FILE_PATH_LITERAL("a.exe")); | 241 info.target_file = FilePath(FILE_PATH_LITERAL("a.exe")); |
237 info.download_url_chain.push_back(GURL("http://www.evil.com/bla.exe")); | 242 info.download_url_chain.push_back(GURL("http://www.evil.com/bla.exe")); |
238 info.download_url_chain.push_back(GURL("http://www.google.com/a.exe")); | 243 info.download_url_chain.push_back(GURL("http://www.google.com/a.exe")); |
239 info.referrer_url = GURL("http://www.google.com/"); | 244 info.referrer_url = GURL("http://www.google.com/"); |
240 | 245 |
241 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) | 246 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) |
242 .WillRepeatedly(Return(false)); | 247 .WillRepeatedly(Return(false)); |
243 EXPECT_CALL(*sb_service_, | 248 EXPECT_CALL(*sb_service_, |
244 MatchDownloadWhitelistUrl(GURL("http://www.google.com/a.exe"))) | 249 MatchDownloadWhitelistUrl(GURL("http://www.google.com/a.exe"))) |
245 .WillRepeatedly(Return(true)); | 250 .WillRepeatedly(Return(true)); |
246 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)).Times(2); | 251 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)).Times(2); |
247 | 252 |
248 download_service_->CheckClientDownload( | 253 download_service_->CheckClientDownload( |
249 info, | 254 info, |
250 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 255 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
251 base::Unretained(this))); | 256 base::Unretained(this))); |
252 msg_loop_.Run(); | 257 msg_loop_.Run(); |
253 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 258 ExpectResult(DownloadProtectionService::SAFE); |
254 | 259 |
255 // Check that the referrer is matched against the whitelist. | 260 // Check that the referrer is matched against the whitelist. |
256 info.download_url_chain.pop_back(); | 261 info.download_url_chain.pop_back(); |
257 info.referrer_url = GURL("http://www.google.com/a.exe"); | 262 info.referrer_url = GURL("http://www.google.com/a.exe"); |
258 download_service_->CheckClientDownload( | 263 download_service_->CheckClientDownload( |
259 info, | 264 info, |
260 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 265 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
261 base::Unretained(this))); | 266 base::Unretained(this))); |
262 msg_loop_.Run(); | 267 msg_loop_.Run(); |
263 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 268 ExpectResult(DownloadProtectionService::SAFE); |
264 } | 269 } |
265 | 270 |
266 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) { | 271 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) { |
267 FakeURLFetcherFactory factory; | 272 FakeURLFetcherFactory factory; |
268 // HTTP request will fail. | 273 // HTTP request will fail. |
269 factory.SetFakeResponse( | 274 factory.SetFakeResponse( |
270 DownloadProtectionService::kDownloadRequestUrl, "", false); | 275 DownloadProtectionService::kDownloadRequestUrl, "", false); |
271 | 276 |
272 DownloadProtectionService::DownloadInfo info; | 277 DownloadProtectionService::DownloadInfo info; |
273 info.local_file = FilePath(FILE_PATH_LITERAL("a.tmp")); | 278 info.local_file = FilePath(FILE_PATH_LITERAL("a.tmp")); |
274 info.target_file = FilePath(FILE_PATH_LITERAL("a.exe")); | 279 info.target_file = FilePath(FILE_PATH_LITERAL("a.exe")); |
275 info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe")); | 280 info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe")); |
276 info.referrer_url = GURL("http://www.google.com/"); | 281 info.referrer_url = GURL("http://www.google.com/"); |
277 | 282 |
278 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) | 283 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) |
279 .WillRepeatedly(Return(false)); | 284 .WillRepeatedly(Return(false)); |
280 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)); | 285 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)); |
281 | 286 |
282 download_service_->CheckClientDownload( | 287 download_service_->CheckClientDownload( |
283 info, | 288 info, |
284 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 289 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
285 base::Unretained(this))); | 290 base::Unretained(this))); |
286 msg_loop_.Run(); | 291 msg_loop_.Run(); |
287 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 292 ExpectResult(DownloadProtectionService::SAFE); |
288 } | 293 } |
289 | 294 |
290 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSuccess) { | 295 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSuccess) { |
291 ClientDownloadResponse response; | 296 ClientDownloadResponse response; |
292 response.set_verdict(ClientDownloadResponse::SAFE); | 297 response.set_verdict(ClientDownloadResponse::SAFE); |
293 FakeURLFetcherFactory factory; | 298 FakeURLFetcherFactory factory; |
294 // Empty response means SAFE. | 299 // Empty response means SAFE. |
295 factory.SetFakeResponse( | 300 factory.SetFakeResponse( |
296 DownloadProtectionService::kDownloadRequestUrl, | 301 DownloadProtectionService::kDownloadRequestUrl, |
297 response.SerializeAsString(), | 302 response.SerializeAsString(), |
298 true); | 303 true); |
299 | 304 |
300 DownloadProtectionService::DownloadInfo info; | 305 DownloadProtectionService::DownloadInfo info; |
301 info.local_file = FilePath(FILE_PATH_LITERAL("a.tmp")); | 306 info.local_file = FilePath(FILE_PATH_LITERAL("a.tmp")); |
302 info.target_file = FilePath(FILE_PATH_LITERAL("a.exe")); | 307 info.target_file = FilePath(FILE_PATH_LITERAL("a.exe")); |
303 info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe")); | 308 info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe")); |
304 info.referrer_url = GURL("http://www.google.com/"); | 309 info.referrer_url = GURL("http://www.google.com/"); |
305 | 310 |
306 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) | 311 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) |
307 .WillRepeatedly(Return(false)); | 312 .WillRepeatedly(Return(false)); |
308 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)).Times(3); | 313 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)).Times(3); |
309 | 314 |
310 download_service_->CheckClientDownload( | 315 download_service_->CheckClientDownload( |
311 info, | 316 info, |
312 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 317 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
313 base::Unretained(this))); | 318 base::Unretained(this))); |
314 msg_loop_.Run(); | 319 msg_loop_.Run(); |
315 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 320 ExpectResult(DownloadProtectionService::SAFE); |
316 | 321 |
317 // Invalid response should be safe too. | 322 // Invalid response should be safe too. |
318 response.Clear(); | 323 response.Clear(); |
319 factory.SetFakeResponse( | 324 factory.SetFakeResponse( |
320 DownloadProtectionService::kDownloadRequestUrl, | 325 DownloadProtectionService::kDownloadRequestUrl, |
321 response.SerializePartialAsString(), | 326 response.SerializePartialAsString(), |
322 true); | 327 true); |
323 | 328 |
324 download_service_->CheckClientDownload( | 329 download_service_->CheckClientDownload( |
325 info, | 330 info, |
326 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 331 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
327 base::Unretained(this))); | 332 base::Unretained(this))); |
328 msg_loop_.Run(); | 333 msg_loop_.Run(); |
329 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 334 ExpectResult(DownloadProtectionService::SAFE); |
330 | 335 |
331 // If the response is dangerous the result should also be marked as dangerous. | 336 // If the response is dangerous the result should also be marked as dangerous. |
332 response.set_verdict(ClientDownloadResponse::DANGEROUS); | 337 response.set_verdict(ClientDownloadResponse::DANGEROUS); |
333 factory.SetFakeResponse( | 338 factory.SetFakeResponse( |
334 DownloadProtectionService::kDownloadRequestUrl, | 339 DownloadProtectionService::kDownloadRequestUrl, |
335 response.SerializeAsString(), | 340 response.SerializeAsString(), |
336 true); | 341 true); |
337 | 342 |
338 download_service_->CheckClientDownload( | 343 download_service_->CheckClientDownload( |
339 info, | 344 info, |
340 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 345 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
341 base::Unretained(this))); | 346 base::Unretained(this))); |
342 msg_loop_.Run(); | 347 msg_loop_.Run(); |
343 EXPECT_EQ(DownloadProtectionService::DANGEROUS, result_); | 348 ExpectResult(DownloadProtectionService::DANGEROUS); |
344 } | 349 } |
345 | 350 |
346 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadValidateRequest) { | 351 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadValidateRequest) { |
347 TestURLFetcherFactory factory; | 352 TestURLFetcherFactory factory; |
348 | 353 |
349 DownloadProtectionService::DownloadInfo info; | 354 DownloadProtectionService::DownloadInfo info; |
350 info.local_file = FilePath(FILE_PATH_LITERAL("bla.tmp")); | 355 info.local_file = FilePath(FILE_PATH_LITERAL("bla.tmp")); |
351 info.target_file = FilePath(FILE_PATH_LITERAL("bla.exe")); | 356 info.target_file = FilePath(FILE_PATH_LITERAL("bla.exe")); |
352 info.download_url_chain.push_back(GURL("http://www.google.com/")); | 357 info.download_url_chain.push_back(GURL("http://www.google.com/")); |
353 info.download_url_chain.push_back(GURL("http://www.google.com/bla.exe")); | 358 info.download_url_chain.push_back(GURL("http://www.google.com/bla.exe")); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 // CheckDownloadHash returns immediately which means the hash is not | 471 // CheckDownloadHash returns immediately which means the hash is not |
467 // malicious. | 472 // malicious. |
468 EXPECT_CALL(*sb_service_, | 473 EXPECT_CALL(*sb_service_, |
469 CheckDownloadHash(info.sha256_hash, NotNull())) | 474 CheckDownloadHash(info.sha256_hash, NotNull())) |
470 .WillOnce(Return(true)); | 475 .WillOnce(Return(true)); |
471 download_service_->CheckClientDownload( | 476 download_service_->CheckClientDownload( |
472 info, | 477 info, |
473 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 478 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
474 base::Unretained(this))); | 479 base::Unretained(this))); |
475 msg_loop_.Run(); | 480 msg_loop_.Run(); |
476 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 481 ExpectResult(DownloadProtectionService::SAFE); |
477 Mock::VerifyAndClearExpectations(sb_service_); | 482 Mock::VerifyAndClearExpectations(sb_service_); |
478 | 483 |
479 // The hash does not match the bad binary digest list. | 484 // The hash does not match the bad binary digest list. |
480 EXPECT_CALL(*sb_service_, | 485 EXPECT_CALL(*sb_service_, |
481 CheckDownloadHash(info.sha256_hash, NotNull())) | 486 CheckDownloadHash(info.sha256_hash, NotNull())) |
482 .WillOnce(DoAll(CheckDownloadHashDone(SafeBrowsingService::SAFE), | 487 .WillOnce(DoAll(CheckDownloadHashDone(SafeBrowsingService::SAFE), |
483 Return(false))); | 488 Return(false))); |
484 download_service_->CheckClientDownload( | 489 download_service_->CheckClientDownload( |
485 info, | 490 info, |
486 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 491 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
487 base::Unretained(this))); | 492 base::Unretained(this))); |
488 msg_loop_.Run(); | 493 msg_loop_.Run(); |
489 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 494 ExpectResult(DownloadProtectionService::SAFE); |
490 Mock::VerifyAndClearExpectations(sb_service_); | 495 Mock::VerifyAndClearExpectations(sb_service_); |
491 | 496 |
492 // The hash matches the bad binary URL list but not the bad binary digest | 497 // The hash matches the bad binary URL list but not the bad binary digest |
493 // list. This is an artificial example to make sure we really check the | 498 // list. This is an artificial example to make sure we really check the |
494 // result value in the code. | 499 // result value in the code. |
495 EXPECT_CALL(*sb_service_, | 500 EXPECT_CALL(*sb_service_, |
496 CheckDownloadHash(info.sha256_hash, NotNull())) | 501 CheckDownloadHash(info.sha256_hash, NotNull())) |
497 .WillOnce(DoAll( | 502 .WillOnce(DoAll( |
498 CheckDownloadHashDone(SafeBrowsingService::BINARY_MALWARE_URL), | 503 CheckDownloadHashDone(SafeBrowsingService::BINARY_MALWARE_URL), |
499 Return(false))); | 504 Return(false))); |
500 download_service_->CheckClientDownload( | 505 download_service_->CheckClientDownload( |
501 info, | 506 info, |
502 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 507 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
503 base::Unretained(this))); | 508 base::Unretained(this))); |
504 msg_loop_.Run(); | 509 msg_loop_.Run(); |
505 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 510 ExpectResult(DownloadProtectionService::SAFE); |
506 Mock::VerifyAndClearExpectations(sb_service_); | 511 Mock::VerifyAndClearExpectations(sb_service_); |
507 | 512 |
508 // A match is found with the bad binary digest list. We currently do not | 513 // A match is found with the bad binary digest list. We currently do not |
509 // warn based on the digest list. Hence we should always return SAFE. | 514 // warn based on the digest list. Hence we should always return SAFE. |
510 EXPECT_CALL(*sb_service_, | 515 EXPECT_CALL(*sb_service_, |
511 CheckDownloadHash(info.sha256_hash, NotNull())) | 516 CheckDownloadHash(info.sha256_hash, NotNull())) |
512 .WillOnce(DoAll( | 517 .WillOnce(DoAll( |
513 CheckDownloadHashDone(SafeBrowsingService::BINARY_MALWARE_HASH), | 518 CheckDownloadHashDone(SafeBrowsingService::BINARY_MALWARE_HASH), |
514 Return(false))); | 519 Return(false))); |
515 download_service_->CheckClientDownload( | 520 download_service_->CheckClientDownload( |
516 info, | 521 info, |
517 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 522 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
518 base::Unretained(this))); | 523 base::Unretained(this))); |
519 msg_loop_.Run(); | 524 msg_loop_.Run(); |
520 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 525 ExpectResult(DownloadProtectionService::SAFE); |
521 Mock::VerifyAndClearExpectations(sb_service_); | 526 Mock::VerifyAndClearExpectations(sb_service_); |
522 | 527 |
523 // If the download is not an executable we do not send a server ping but we | 528 // If the download is not an executable we do not send a server ping but we |
524 // still want to lookup the binary digest list. | 529 // still want to lookup the binary digest list. |
525 info.target_file = FilePath(FILE_PATH_LITERAL("a.pdf")); | 530 info.target_file = FilePath(FILE_PATH_LITERAL("a.pdf")); |
526 info.download_url_chain[0] = GURL("http://www.evil.com/a.pdf"); | 531 info.download_url_chain[0] = GURL("http://www.evil.com/a.pdf"); |
527 EXPECT_CALL(*sb_service_, | 532 EXPECT_CALL(*sb_service_, |
528 CheckDownloadHash(info.sha256_hash, NotNull())) | 533 CheckDownloadHash(info.sha256_hash, NotNull())) |
529 .WillOnce(DoAll( | 534 .WillOnce(DoAll( |
530 CheckDownloadHashDone(SafeBrowsingService::BINARY_MALWARE_HASH), | 535 CheckDownloadHashDone(SafeBrowsingService::BINARY_MALWARE_HASH), |
531 Return(false))); | 536 Return(false))); |
532 download_service_->CheckClientDownload( | 537 download_service_->CheckClientDownload( |
533 info, | 538 info, |
534 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 539 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
535 base::Unretained(this))); | 540 base::Unretained(this))); |
536 msg_loop_.Run(); | 541 msg_loop_.Run(); |
537 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 542 ExpectResult(DownloadProtectionService::SAFE); |
538 Mock::VerifyAndClearExpectations(sb_service_); | 543 Mock::VerifyAndClearExpectations(sb_service_); |
539 | 544 |
540 // If the URL or the referrer matches the download whitelist we cannot send | 545 // If the URL or the referrer matches the download whitelist we cannot send |
541 // a server ping for privacy reasons but we still match the digest against | 546 // a server ping for privacy reasons but we still match the digest against |
542 // the bad binary digest list. | 547 // the bad binary digest list. |
543 info.target_file = FilePath(FILE_PATH_LITERAL("a.exe")); | 548 info.target_file = FilePath(FILE_PATH_LITERAL("a.exe")); |
544 info.download_url_chain[0] = GURL("http://www.evil.com/a.exe"); | 549 info.download_url_chain[0] = GURL("http://www.evil.com/a.exe"); |
545 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) | 550 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) |
546 .WillRepeatedly(Return(true)); | 551 .WillRepeatedly(Return(true)); |
547 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)); | 552 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)); |
548 EXPECT_CALL(*sb_service_, | 553 EXPECT_CALL(*sb_service_, |
549 CheckDownloadHash(info.sha256_hash, NotNull())) | 554 CheckDownloadHash(info.sha256_hash, NotNull())) |
550 .WillOnce(DoAll( | 555 .WillOnce(DoAll( |
551 CheckDownloadHashDone(SafeBrowsingService::BINARY_MALWARE_HASH), | 556 CheckDownloadHashDone(SafeBrowsingService::BINARY_MALWARE_HASH), |
552 Return(false))); | 557 Return(false))); |
553 download_service_->CheckClientDownload( | 558 download_service_->CheckClientDownload( |
554 info, | 559 info, |
555 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 560 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
556 base::Unretained(this))); | 561 base::Unretained(this))); |
557 msg_loop_.Run(); | 562 msg_loop_.Run(); |
558 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 563 ExpectResult(DownloadProtectionService::SAFE); |
559 Mock::VerifyAndClearExpectations(sb_service_); | 564 Mock::VerifyAndClearExpectations(sb_service_); |
560 | 565 |
561 // If the binary is a trusted executable we will not ping the server but | 566 // If the binary is a trusted executable we will not ping the server but |
562 // we will still lookup the digest list. | 567 // we will still lookup the digest list. |
563 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) | 568 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) |
564 .WillRepeatedly(Return(false)); | 569 .WillRepeatedly(Return(false)); |
565 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)) | 570 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)) |
566 .WillOnce(TrustSignature()); | 571 .WillOnce(TrustSignature()); |
567 EXPECT_CALL(*sb_service_, | 572 EXPECT_CALL(*sb_service_, |
568 CheckDownloadHash(info.sha256_hash, NotNull())) | 573 CheckDownloadHash(info.sha256_hash, NotNull())) |
569 .WillOnce(DoAll(CheckDownloadHashDone(SafeBrowsingService::SAFE), | 574 .WillOnce(DoAll(CheckDownloadHashDone(SafeBrowsingService::SAFE), |
570 Return(false))); | 575 Return(false))); |
571 download_service_->CheckClientDownload( | 576 download_service_->CheckClientDownload( |
572 info, | 577 info, |
573 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 578 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
574 base::Unretained(this))); | 579 base::Unretained(this))); |
575 msg_loop_.Run(); | 580 msg_loop_.Run(); |
576 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 581 ExpectResult(DownloadProtectionService::SAFE); |
577 } | 582 } |
578 | 583 |
579 TEST_F(DownloadProtectionServiceTest, TestCheckDownloadUrl) { | 584 TEST_F(DownloadProtectionServiceTest, TestCheckDownloadUrl) { |
580 DownloadProtectionService::DownloadInfo info; | 585 DownloadProtectionService::DownloadInfo info; |
581 info.download_url_chain.push_back(GURL("http://www.google.com/")); | 586 info.download_url_chain.push_back(GURL("http://www.google.com/")); |
582 info.download_url_chain.push_back(GURL("http://www.google.com/bla.exe")); | 587 info.download_url_chain.push_back(GURL("http://www.google.com/bla.exe")); |
583 info.referrer_url = GURL("http://www.google.com/"); | 588 info.referrer_url = GURL("http://www.google.com/"); |
584 | 589 |
585 // CheckDownloadURL returns immediately which means the client object callback | 590 // CheckDownloadURL returns immediately which means the client object callback |
586 // will never be called. Nevertheless the callback provided to | 591 // will never be called. Nevertheless the callback provided to |
587 // CheckClientDownload must still be called. | 592 // CheckClientDownload must still be called. |
588 EXPECT_CALL(*sb_service_, | 593 EXPECT_CALL(*sb_service_, |
589 CheckDownloadUrl(ContainerEq(info.download_url_chain), | 594 CheckDownloadUrl(ContainerEq(info.download_url_chain), |
590 NotNull())) | 595 NotNull())) |
591 .WillOnce(Return(true)); | 596 .WillOnce(Return(true)); |
592 download_service_->CheckDownloadUrl( | 597 download_service_->CheckDownloadUrl( |
593 info, | 598 info, |
594 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 599 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
595 base::Unretained(this))); | 600 base::Unretained(this))); |
596 msg_loop_.Run(); | 601 msg_loop_.Run(); |
597 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 602 ExpectResult(DownloadProtectionService::SAFE); |
598 Mock::VerifyAndClearExpectations(sb_service_); | 603 Mock::VerifyAndClearExpectations(sb_service_); |
599 | 604 |
600 EXPECT_CALL(*sb_service_, | 605 EXPECT_CALL(*sb_service_, |
601 CheckDownloadUrl(ContainerEq(info.download_url_chain), | 606 CheckDownloadUrl(ContainerEq(info.download_url_chain), |
602 NotNull())) | 607 NotNull())) |
603 .WillOnce(DoAll(CheckDownloadUrlDone(SafeBrowsingService::SAFE), | 608 .WillOnce(DoAll(CheckDownloadUrlDone(SafeBrowsingService::SAFE), |
604 Return(false))); | 609 Return(false))); |
605 download_service_->CheckDownloadUrl( | 610 download_service_->CheckDownloadUrl( |
606 info, | 611 info, |
607 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 612 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
608 base::Unretained(this))); | 613 base::Unretained(this))); |
609 msg_loop_.Run(); | 614 msg_loop_.Run(); |
610 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 615 ExpectResult(DownloadProtectionService::SAFE); |
611 Mock::VerifyAndClearExpectations(sb_service_); | 616 Mock::VerifyAndClearExpectations(sb_service_); |
612 | 617 |
613 EXPECT_CALL(*sb_service_, | 618 EXPECT_CALL(*sb_service_, |
614 CheckDownloadUrl(ContainerEq(info.download_url_chain), | 619 CheckDownloadUrl(ContainerEq(info.download_url_chain), |
615 NotNull())) | 620 NotNull())) |
616 .WillOnce(DoAll( | 621 .WillOnce(DoAll( |
617 CheckDownloadUrlDone(SafeBrowsingService::URL_MALWARE), | 622 CheckDownloadUrlDone(SafeBrowsingService::URL_MALWARE), |
618 Return(false))); | 623 Return(false))); |
619 download_service_->CheckDownloadUrl( | 624 download_service_->CheckDownloadUrl( |
620 info, | 625 info, |
621 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 626 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
622 base::Unretained(this))); | 627 base::Unretained(this))); |
623 msg_loop_.Run(); | 628 msg_loop_.Run(); |
624 EXPECT_EQ(DownloadProtectionService::SAFE, result_); | 629 ExpectResult(DownloadProtectionService::SAFE); |
625 Mock::VerifyAndClearExpectations(sb_service_); | 630 Mock::VerifyAndClearExpectations(sb_service_); |
626 | 631 |
627 EXPECT_CALL(*sb_service_, | 632 EXPECT_CALL(*sb_service_, |
628 CheckDownloadUrl(ContainerEq(info.download_url_chain), | 633 CheckDownloadUrl(ContainerEq(info.download_url_chain), |
629 NotNull())) | 634 NotNull())) |
630 .WillOnce(DoAll( | 635 .WillOnce(DoAll( |
631 CheckDownloadUrlDone(SafeBrowsingService::BINARY_MALWARE_URL), | 636 CheckDownloadUrlDone(SafeBrowsingService::BINARY_MALWARE_URL), |
632 Return(false))); | 637 Return(false))); |
633 download_service_->CheckDownloadUrl( | 638 download_service_->CheckDownloadUrl( |
634 info, | 639 info, |
635 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 640 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
636 base::Unretained(this))); | 641 base::Unretained(this))); |
637 msg_loop_.Run(); | 642 msg_loop_.Run(); |
638 EXPECT_EQ(DownloadProtectionService::DANGEROUS, result_); | 643 ExpectResult(DownloadProtectionService::DANGEROUS); |
| 644 } |
| 645 |
| 646 TEST_F(DownloadProtectionServiceTest, TestDownloadRequestTimeout) { |
| 647 TestURLFetcherFactory factory; |
| 648 |
| 649 DownloadProtectionService::DownloadInfo info; |
| 650 info.download_url_chain.push_back(GURL("http://www.evil.com/bla.exe")); |
| 651 info.referrer_url = GURL("http://www.google.com/"); |
| 652 info.local_file = FilePath(FILE_PATH_LITERAL("a.tmp")); |
| 653 info.target_file = FilePath(FILE_PATH_LITERAL("a.exe")); |
| 654 |
| 655 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) |
| 656 .WillRepeatedly(Return(false)); |
| 657 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)); |
| 658 |
| 659 download_service_->download_request_timeout_ms_ = 10; |
| 660 download_service_->CheckClientDownload( |
| 661 info, |
| 662 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 663 base::Unretained(this))); |
| 664 // Run the message loop(s) until SendRequest is called. |
| 665 FlushThreadMessageLoops(); |
| 666 |
| 667 // The request should time out because the HTTP request hasn't returned |
| 668 // anything yet. |
| 669 msg_loop_.Run(); |
| 670 ExpectResult(DownloadProtectionService::SAFE); |
639 } | 671 } |
640 } // namespace safe_browsing | 672 } // namespace safe_browsing |
OLD | NEW |