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

Side by Side Diff: chrome/browser/safe_browsing/download_protection_service_unittest.cc

Issue 663023007: Include high-fidelity metadata about a download in incident reports. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git/+/master
Patch Set: robert comments (and sync to r300494, oops) Created 6 years, 2 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 "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/base_paths.h" 10 #include "base/base_paths.h"
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 "Enabled")); 210 "Enabled"));
211 #endif 211 #endif
212 // Start real threads for the IO and File threads so that the DCHECKs 212 // Start real threads for the IO and File threads so that the DCHECKs
213 // to test that we're on the correct thread work. 213 // to test that we're on the correct thread work.
214 sb_service_ = new StrictMock<FakeSafeBrowsingService>(); 214 sb_service_ = new StrictMock<FakeSafeBrowsingService>();
215 sb_service_->Initialize(); 215 sb_service_->Initialize();
216 binary_feature_extractor_ = new StrictMock<MockBinaryFeatureExtractor>(); 216 binary_feature_extractor_ = new StrictMock<MockBinaryFeatureExtractor>();
217 download_service_ = sb_service_->download_protection_service(); 217 download_service_ = sb_service_->download_protection_service();
218 download_service_->binary_feature_extractor_ = binary_feature_extractor_; 218 download_service_->binary_feature_extractor_ = binary_feature_extractor_;
219 download_service_->SetEnabled(true); 219 download_service_->SetEnabled(true);
220 client_download_request_subscription_ =
221 download_service_->RegisterClientDownloadRequestCallback(
222 base::Bind(&DownloadProtectionServiceTest::OnClientDownloadRequest,
223 base::Unretained(this)));
220 base::RunLoop().RunUntilIdle(); 224 base::RunLoop().RunUntilIdle();
221 has_result_ = false; 225 has_result_ = false;
222 226
223 base::FilePath source_path; 227 base::FilePath source_path;
224 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_path)); 228 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_path));
225 testdata_path_ = source_path 229 testdata_path_ = source_path
226 .AppendASCII("chrome") 230 .AppendASCII("chrome")
227 .AppendASCII("test") 231 .AppendASCII("test")
228 .AppendASCII("data") 232 .AppendASCII("data")
229 .AppendASCII("safe_browsing") 233 .AppendASCII("safe_browsing")
230 .AppendASCII("download_protection"); 234 .AppendASCII("download_protection");
231 } 235 }
232 236
233 virtual void TearDown() { 237 virtual void TearDown() {
238 client_download_request_subscription_.reset();
234 sb_service_->ShutDown(); 239 sb_service_->ShutDown();
235 // Flush all of the thread message loops to ensure that there are no 240 // Flush all of the thread message loops to ensure that there are no
236 // tasks currently running. 241 // tasks currently running.
237 FlushThreadMessageLoops(); 242 FlushThreadMessageLoops();
238 sb_service_ = NULL; 243 sb_service_ = NULL;
239 } 244 }
240 245
241 bool RequestContainsResource(const ClientDownloadRequest& request, 246 bool RequestContainsResource(const ClientDownloadRequest& request,
242 ClientDownloadRequest::ResourceType type, 247 ClientDownloadRequest::ResourceType type,
243 const std::string& url, 248 const std::string& url,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 return NULL; 298 return NULL;
294 } 299 }
295 net::CertificateList certs = 300 net::CertificateList certs =
296 net::X509Certificate::CreateCertificateListFromBytes( 301 net::X509Certificate::CreateCertificateListFromBytes(
297 cert_data.data(), 302 cert_data.data(),
298 cert_data.size(), 303 cert_data.size(),
299 net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE); 304 net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE);
300 return certs.empty() ? NULL : certs[0]; 305 return certs.empty() ? NULL : certs[0];
301 } 306 }
302 307
308 bool HasClientDownloadRequest() const {
309 return last_client_download_request_.get() != NULL;
310 }
311
312 void ClearClientDownloadRequest() { last_client_download_request_.reset(); }
313
303 private: 314 private:
304 // Helper functions for FlushThreadMessageLoops. 315 // Helper functions for FlushThreadMessageLoops.
305 void RunAllPendingAndQuitUI() { 316 void RunAllPendingAndQuitUI() {
306 base::MessageLoop::current()->RunUntilIdle(); 317 base::MessageLoop::current()->RunUntilIdle();
307 BrowserThread::PostTask( 318 BrowserThread::PostTask(
308 BrowserThread::UI, 319 BrowserThread::UI,
309 FROM_HERE, 320 FROM_HERE,
310 base::Bind(&DownloadProtectionServiceTest::QuitMessageLoop, 321 base::Bind(&DownloadProtectionServiceTest::QuitMessageLoop,
311 base::Unretained(this))); 322 base::Unretained(this)));
312 } 323 }
(...skipping 12 matching lines...) Expand all
325 336
326 void FlushMessageLoop(BrowserThread::ID thread) { 337 void FlushMessageLoop(BrowserThread::ID thread) {
327 BrowserThread::PostTask( 338 BrowserThread::PostTask(
328 BrowserThread::UI, 339 BrowserThread::UI,
329 FROM_HERE, 340 FROM_HERE,
330 base::Bind(&DownloadProtectionServiceTest::PostRunMessageLoopTask, 341 base::Bind(&DownloadProtectionServiceTest::PostRunMessageLoopTask,
331 base::Unretained(this), thread)); 342 base::Unretained(this), thread));
332 MessageLoop::current()->Run(); 343 MessageLoop::current()->Run();
333 } 344 }
334 345
346 void OnClientDownloadRequest(content::DownloadItem* download,
347 const ClientDownloadRequest* request) {
348 if (request)
349 last_client_download_request_.reset(new ClientDownloadRequest(*request));
350 else
351 last_client_download_request_.reset();
352 }
353
335 public: 354 public:
336 void CheckDoneCallback( 355 void CheckDoneCallback(
337 DownloadProtectionService::DownloadCheckResult result) { 356 DownloadProtectionService::DownloadCheckResult result) {
338 result_ = result; 357 result_ = result;
339 has_result_ = true; 358 has_result_ = true;
340 MessageLoop::current()->Quit(); 359 MessageLoop::current()->Quit();
341 } 360 }
342 361
343 void SyncCheckDoneCallback( 362 void SyncCheckDoneCallback(
344 DownloadProtectionService::DownloadCheckResult result) { 363 DownloadProtectionService::DownloadCheckResult result) {
(...skipping 21 matching lines...) Expand all
366 scoped_refptr<MockBinaryFeatureExtractor> binary_feature_extractor_; 385 scoped_refptr<MockBinaryFeatureExtractor> binary_feature_extractor_;
367 DownloadProtectionService* download_service_; 386 DownloadProtectionService* download_service_;
368 DownloadProtectionService::DownloadCheckResult result_; 387 DownloadProtectionService::DownloadCheckResult result_;
369 bool has_result_; 388 bool has_result_;
370 content::TestBrowserThreadBundle test_browser_thread_bundle_; 389 content::TestBrowserThreadBundle test_browser_thread_bundle_;
371 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_; 390 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_;
372 base::FilePath testdata_path_; 391 base::FilePath testdata_path_;
373 #if defined(OS_MACOSX) 392 #if defined(OS_MACOSX)
374 scoped_ptr<base::FieldTrialList> field_trial_list_; 393 scoped_ptr<base::FieldTrialList> field_trial_list_;
375 #endif 394 #endif
395 DownloadProtectionService::ClientDownloadRequestSubscription
396 client_download_request_subscription_;
397 scoped_ptr<ClientDownloadRequest> last_client_download_request_;
376 }; 398 };
377 399
378 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { 400 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) {
379 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp")); 401 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp"));
380 base::FilePath a_exe(FILE_PATH_LITERAL("a.exe")); 402 base::FilePath a_exe(FILE_PATH_LITERAL("a.exe"));
381 std::vector<GURL> url_chain; 403 std::vector<GURL> url_chain;
382 GURL referrer("http://www.google.com/"); 404 GURL referrer("http://www.google.com/");
383 405
384 content::MockDownloadItem item; 406 content::MockDownloadItem item;
385 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); 407 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
386 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe)); 408 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
387 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); 409 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
388 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); 410 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
389 EXPECT_CALL(item, GetTabUrl()).WillRepeatedly(ReturnRef(GURL::EmptyGURL())); 411 EXPECT_CALL(item, GetTabUrl()).WillRepeatedly(ReturnRef(GURL::EmptyGURL()));
390 EXPECT_CALL(item, GetTabReferrerUrl()) 412 EXPECT_CALL(item, GetTabReferrerUrl())
391 .WillRepeatedly(ReturnRef(GURL::EmptyGURL())); 413 .WillRepeatedly(ReturnRef(GURL::EmptyGURL()));
392 download_service_->CheckClientDownload( 414 download_service_->CheckClientDownload(
393 &item, 415 &item,
394 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 416 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
395 base::Unretained(this))); 417 base::Unretained(this)));
396 MessageLoop::current()->Run(); 418 MessageLoop::current()->Run();
397 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 419 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
420 EXPECT_FALSE(HasClientDownloadRequest());
398 Mock::VerifyAndClearExpectations(&item); 421 Mock::VerifyAndClearExpectations(&item);
399 422
400 url_chain.push_back(GURL("file://www.google.com/")); 423 url_chain.push_back(GURL("file://www.google.com/"));
401 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); 424 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
402 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe)); 425 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
403 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); 426 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
404 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); 427 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
405 EXPECT_CALL(item, GetTabUrl()).WillRepeatedly(ReturnRef(GURL::EmptyGURL())); 428 EXPECT_CALL(item, GetTabUrl()).WillRepeatedly(ReturnRef(GURL::EmptyGURL()));
406 EXPECT_CALL(item, GetTabReferrerUrl()) 429 EXPECT_CALL(item, GetTabReferrerUrl())
407 .WillRepeatedly(ReturnRef(GURL::EmptyGURL())); 430 .WillRepeatedly(ReturnRef(GURL::EmptyGURL()));
408 download_service_->CheckClientDownload( 431 download_service_->CheckClientDownload(
409 &item, 432 &item,
410 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 433 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
411 base::Unretained(this))); 434 base::Unretained(this)));
412 MessageLoop::current()->Run(); 435 MessageLoop::current()->Run();
413 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 436 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
437 EXPECT_FALSE(HasClientDownloadRequest());
414 } 438 }
415 439
416 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadNotABinary) { 440 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadNotABinary) {
417 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp")); 441 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp"));
418 base::FilePath a_txt(FILE_PATH_LITERAL("a.txt")); 442 base::FilePath a_txt(FILE_PATH_LITERAL("a.txt"));
419 std::vector<GURL> url_chain; 443 std::vector<GURL> url_chain;
420 GURL referrer("http://www.google.com/"); 444 GURL referrer("http://www.google.com/");
421 445
422 content::MockDownloadItem item; 446 content::MockDownloadItem item;
423 url_chain.push_back(GURL("http://www.example.com/foo")); 447 url_chain.push_back(GURL("http://www.example.com/foo"));
424 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); 448 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
425 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_txt)); 449 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_txt));
426 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); 450 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
427 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); 451 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
428 EXPECT_CALL(item, GetTabUrl()).WillRepeatedly(ReturnRef(GURL::EmptyGURL())); 452 EXPECT_CALL(item, GetTabUrl()).WillRepeatedly(ReturnRef(GURL::EmptyGURL()));
429 EXPECT_CALL(item, GetTabReferrerUrl()) 453 EXPECT_CALL(item, GetTabReferrerUrl())
430 .WillRepeatedly(ReturnRef(GURL::EmptyGURL())); 454 .WillRepeatedly(ReturnRef(GURL::EmptyGURL()));
431 download_service_->CheckClientDownload( 455 download_service_->CheckClientDownload(
432 &item, 456 &item,
433 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 457 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
434 base::Unretained(this))); 458 base::Unretained(this)));
435 MessageLoop::current()->Run(); 459 MessageLoop::current()->Run();
436 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 460 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
461 EXPECT_FALSE(HasClientDownloadRequest());
437 } 462 }
438 463
439 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) { 464 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) {
440 // Response to any requests will be DANGEROUS. 465 // Response to any requests will be DANGEROUS.
441 ClientDownloadResponse response; 466 ClientDownloadResponse response;
442 response.set_verdict(ClientDownloadResponse::DANGEROUS); 467 response.set_verdict(ClientDownloadResponse::DANGEROUS);
443 net::FakeURLFetcherFactory factory(NULL); 468 net::FakeURLFetcherFactory factory(NULL);
444 factory.SetFakeResponse( 469 factory.SetFakeResponse(
445 DownloadProtectionService::GetDownloadRequestUrl(), 470 DownloadProtectionService::GetDownloadRequestUrl(),
446 response.SerializeAsString(), 471 response.SerializeAsString(),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 506
482 // With no referrer and just the bad url, should be marked DANGEROUS. 507 // With no referrer and just the bad url, should be marked DANGEROUS.
483 url_chain.push_back(GURL("http://www.evil.com/bla.exe")); 508 url_chain.push_back(GURL("http://www.evil.com/bla.exe"));
484 download_service_->CheckClientDownload( 509 download_service_->CheckClientDownload(
485 &item, 510 &item,
486 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 511 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
487 base::Unretained(this))); 512 base::Unretained(this)));
488 MessageLoop::current()->Run(); 513 MessageLoop::current()->Run();
489 #if defined(OS_WIN) 514 #if defined(OS_WIN)
490 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 515 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
516 EXPECT_TRUE(HasClientDownloadRequest());
517 ClearClientDownloadRequest();
491 #else 518 #else
492 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 519 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
520 #if defined(OS_MACOSX)
mattm 2014/10/24 02:11:10 These nested/repetitious ifdefs are a little ick.
grt (UTC plus 2) 2014/10/30 19:07:35 Looks better. Done.
521 // OSX sends pings for evaluation purposes.
522 EXPECT_TRUE(HasClientDownloadRequest());
523 ClearClientDownloadRequest();
524 #else
525 EXPECT_FALSE(HasClientDownloadRequest());
526 #endif
493 #endif 527 #endif
494 528
495 // Check that the referrer is not matched against the whitelist. 529 // Check that the referrer is not matched against the whitelist.
496 referrer = GURL("http://www.google.com/"); 530 referrer = GURL("http://www.google.com/");
497 download_service_->CheckClientDownload( 531 download_service_->CheckClientDownload(
498 &item, 532 &item,
499 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 533 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
500 base::Unretained(this))); 534 base::Unretained(this)));
501 MessageLoop::current()->Run(); 535 MessageLoop::current()->Run();
502 #if defined(OS_WIN) 536 #if defined(OS_WIN)
503 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 537 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
538 EXPECT_TRUE(HasClientDownloadRequest());
539 ClearClientDownloadRequest();
504 #else 540 #else
505 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 541 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
542 #if defined(OS_MACOSX)
543 // OSX sends pings for evaluation purposes.
544 EXPECT_TRUE(HasClientDownloadRequest());
545 ClearClientDownloadRequest();
546 #else
547 EXPECT_FALSE(HasClientDownloadRequest());
548 #endif
506 #endif 549 #endif
507 550
508 // Redirect from a site shouldn't be checked either. 551 // Redirect from a site shouldn't be checked either.
509 url_chain.insert(url_chain.begin(), GURL("http://www.google.com/redirect")); 552 url_chain.insert(url_chain.begin(), GURL("http://www.google.com/redirect"));
510 download_service_->CheckClientDownload( 553 download_service_->CheckClientDownload(
511 &item, 554 &item,
512 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 555 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
513 base::Unretained(this))); 556 base::Unretained(this)));
514 MessageLoop::current()->Run(); 557 MessageLoop::current()->Run();
515 #if defined(OS_WIN) 558 #if defined(OS_WIN)
516 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 559 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
560 EXPECT_TRUE(HasClientDownloadRequest());
561 ClearClientDownloadRequest();
517 #else 562 #else
518 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 563 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
564 #if defined(OS_MACOSX)
565 // OSX sends pings for evaluation purposes.
566 EXPECT_TRUE(HasClientDownloadRequest());
567 ClearClientDownloadRequest();
568 #else
569 EXPECT_FALSE(HasClientDownloadRequest());
570 #endif
519 #endif 571 #endif
520 572
521 // Only if the final url is whitelisted should it be SAFE. 573 // Only if the final url is whitelisted should it be SAFE.
522 url_chain.push_back(GURL("http://www.google.com/a.exe")); 574 url_chain.push_back(GURL("http://www.google.com/a.exe"));
523 download_service_->CheckClientDownload( 575 download_service_->CheckClientDownload(
524 &item, 576 &item,
525 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 577 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
526 base::Unretained(this))); 578 base::Unretained(this)));
527 MessageLoop::current()->Run(); 579 MessageLoop::current()->Run();
528 #if defined(OS_MACOSX) 580 #if defined(OS_MACOSX)
529 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 581 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
582 EXPECT_FALSE(HasClientDownloadRequest());
530 #else 583 #else
531 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 584 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
585 EXPECT_FALSE(HasClientDownloadRequest());
586 #if defined(OS_MACOSX)
587 // OSX sends pings for evaluation purposes.
588 EXPECT_TRUE(HasClientDownloadRequest());
589 ClearClientDownloadRequest();
590 #else
591 // TODO(grt): Make the service produce the request even when the URL is
592 // whitelisted.
593 EXPECT_FALSE(HasClientDownloadRequest());
594 #endif
532 #endif 595 #endif
533 } 596 }
534 597
535 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) { 598 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) {
536 net::FakeURLFetcherFactory factory(NULL); 599 net::FakeURLFetcherFactory factory(NULL);
537 // HTTP request will fail. 600 // HTTP request will fail.
538 factory.SetFakeResponse( 601 factory.SetFakeResponse(
539 DownloadProtectionService::GetDownloadRequestUrl(), std::string(), 602 DownloadProtectionService::GetDownloadRequestUrl(), std::string(),
540 net::HTTP_INTERNAL_SERVER_ERROR, net::URLRequestStatus::FAILED); 603 net::HTTP_INTERNAL_SERVER_ERROR, net::URLRequestStatus::FAILED);
541 604
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 EXPECT_CALL(*binary_feature_extractor_.get(), ExtractImageHeaders(a_tmp, _)) 674 EXPECT_CALL(*binary_feature_extractor_.get(), ExtractImageHeaders(a_tmp, _))
612 .Times(6); 675 .Times(6);
613 676
614 download_service_->CheckClientDownload( 677 download_service_->CheckClientDownload(
615 &item, 678 &item,
616 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 679 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
617 base::Unretained(this))); 680 base::Unretained(this)));
618 MessageLoop::current()->Run(); 681 MessageLoop::current()->Run();
619 #if defined(OS_WIN) 682 #if defined(OS_WIN)
620 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 683 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
684 EXPECT_TRUE(HasClientDownloadRequest());
685 ClearClientDownloadRequest();
621 #else 686 #else
622 // On !OS_WIN, no file types are currently supported. Hence all erquests to 687 // On !OS_WIN, no file types are currently supported. Hence all requests to
623 // CheckClientDownload() result in a verdict of UNKNOWN. 688 // CheckClientDownload() result in a verdict of UNKNOWN.
624 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 689 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
690 #if defined(OS_MACOSX)
691 // OSX sends pings for evaluation purposes.
692 EXPECT_TRUE(HasClientDownloadRequest());
693 ClearClientDownloadRequest();
694 #else
695 EXPECT_FALSE(HasClientDownloadRequest());
696 #endif
625 #endif 697 #endif
626 698
627 // Invalid response should result in UNKNOWN. 699 // Invalid response should result in UNKNOWN.
628 response.Clear(); 700 response.Clear();
629 factory.SetFakeResponse( 701 factory.SetFakeResponse(
630 DownloadProtectionService::GetDownloadRequestUrl(), 702 DownloadProtectionService::GetDownloadRequestUrl(),
631 response.SerializePartialAsString(), 703 response.SerializePartialAsString(),
632 net::HTTP_OK, net::URLRequestStatus::SUCCESS); 704 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
633 705
634 download_service_->CheckClientDownload( 706 download_service_->CheckClientDownload(
635 &item, 707 &item,
636 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 708 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
637 base::Unretained(this))); 709 base::Unretained(this)));
638 MessageLoop::current()->Run(); 710 MessageLoop::current()->Run();
639 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 711 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
712 #if defined(OS_WIN) || defined(OS_MACOSX)
713 EXPECT_TRUE(HasClientDownloadRequest());
714 ClearClientDownloadRequest();
715 #else
716 EXPECT_FALSE(HasClientDownloadRequest());
717 #endif
640 std::string feedback_ping; 718 std::string feedback_ping;
641 std::string feedback_response; 719 std::string feedback_response;
642 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting( 720 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting(
643 item, &feedback_ping, &feedback_response)); 721 item, &feedback_ping, &feedback_response));
644 722
645 // If the response is dangerous the result should also be marked as dangerous. 723 // If the response is dangerous the result should also be marked as dangerous.
646 response.set_verdict(ClientDownloadResponse::DANGEROUS); 724 response.set_verdict(ClientDownloadResponse::DANGEROUS);
647 factory.SetFakeResponse( 725 factory.SetFakeResponse(
648 DownloadProtectionService::GetDownloadRequestUrl(), 726 DownloadProtectionService::GetDownloadRequestUrl(),
649 response.SerializeAsString(), 727 response.SerializeAsString(),
650 net::HTTP_OK, net::URLRequestStatus::SUCCESS); 728 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
651 729
652 download_service_->CheckClientDownload( 730 download_service_->CheckClientDownload(
653 &item, 731 &item,
654 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 732 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
655 base::Unretained(this))); 733 base::Unretained(this)));
656 MessageLoop::current()->Run(); 734 MessageLoop::current()->Run();
657 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting( 735 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting(
658 item, &feedback_ping, &feedback_response)); 736 item, &feedback_ping, &feedback_response));
659 #if defined(OS_WIN) 737 #if defined(OS_WIN)
660 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 738 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
739 EXPECT_TRUE(HasClientDownloadRequest());
740 ClearClientDownloadRequest();
661 #else 741 #else
662 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 742 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
743 #if defined(OS_MACOSX)
744 // OSX sends pings for evaluation purposes.
745 EXPECT_TRUE(HasClientDownloadRequest());
746 ClearClientDownloadRequest();
747 #else
748 EXPECT_FALSE(HasClientDownloadRequest());
749 #endif
663 #endif 750 #endif
664 751
665 // If the response is uncommon the result should also be marked as uncommon. 752 // If the response is uncommon the result should also be marked as uncommon.
666 response.set_verdict(ClientDownloadResponse::UNCOMMON); 753 response.set_verdict(ClientDownloadResponse::UNCOMMON);
667 factory.SetFakeResponse( 754 factory.SetFakeResponse(
668 DownloadProtectionService::GetDownloadRequestUrl(), 755 DownloadProtectionService::GetDownloadRequestUrl(),
669 response.SerializeAsString(), 756 response.SerializeAsString(),
670 net::HTTP_OK, net::URLRequestStatus::SUCCESS); 757 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
671 758
672 download_service_->CheckClientDownload( 759 download_service_->CheckClientDownload(
673 &item, 760 &item,
674 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 761 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
675 base::Unretained(this))); 762 base::Unretained(this)));
676 MessageLoop::current()->Run(); 763 MessageLoop::current()->Run();
677 #if defined(OS_WIN) 764 #if defined(OS_WIN)
678 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON)); 765 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON));
679 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting( 766 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting(
680 item, &feedback_ping, &feedback_response)); 767 item, &feedback_ping, &feedback_response));
681 ClientDownloadRequest decoded_request; 768 ClientDownloadRequest decoded_request;
682 EXPECT_TRUE(decoded_request.ParseFromString(feedback_ping)); 769 EXPECT_TRUE(decoded_request.ParseFromString(feedback_ping));
683 EXPECT_EQ(url_chain.back().spec(), decoded_request.url()); 770 EXPECT_EQ(url_chain.back().spec(), decoded_request.url());
684 EXPECT_EQ(response.SerializeAsString(), feedback_response); 771 EXPECT_EQ(response.SerializeAsString(), feedback_response);
772 EXPECT_TRUE(HasClientDownloadRequest());
773 ClearClientDownloadRequest();
685 #else 774 #else
686 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 775 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
687 #endif 776 #endif
688 777
689 // If the response is dangerous_host the result should also be marked as 778 // If the response is dangerous_host the result should also be marked as
690 // dangerous_host. 779 // dangerous_host.
691 response.set_verdict(ClientDownloadResponse::DANGEROUS_HOST); 780 response.set_verdict(ClientDownloadResponse::DANGEROUS_HOST);
692 factory.SetFakeResponse( 781 factory.SetFakeResponse(
693 DownloadProtectionService::GetDownloadRequestUrl(), 782 DownloadProtectionService::GetDownloadRequestUrl(),
694 response.SerializeAsString(), 783 response.SerializeAsString(),
695 net::HTTP_OK, net::URLRequestStatus::SUCCESS); 784 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
696 785
697 download_service_->CheckClientDownload( 786 download_service_->CheckClientDownload(
698 &item, 787 &item,
699 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 788 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
700 base::Unretained(this))); 789 base::Unretained(this)));
701 MessageLoop::current()->Run(); 790 MessageLoop::current()->Run();
702 #if defined(OS_WIN) 791 #if defined(OS_WIN)
703 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS_HOST)); 792 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS_HOST));
704 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting( 793 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting(
705 item, &feedback_ping, &feedback_response)); 794 item, &feedback_ping, &feedback_response));
706 EXPECT_EQ(response.SerializeAsString(), feedback_response); 795 EXPECT_EQ(response.SerializeAsString(), feedback_response);
796 EXPECT_TRUE(HasClientDownloadRequest());
797 ClearClientDownloadRequest();
707 #else 798 #else
708 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 799 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
709 #endif 800 #endif
710 801
711 // If the response is POTENTIALLY_UNWANTED the result should also be marked as 802 // If the response is POTENTIALLY_UNWANTED the result should also be marked as
712 // POTENTIALLY_UNWANTED. 803 // POTENTIALLY_UNWANTED.
713 response.set_verdict(ClientDownloadResponse::POTENTIALLY_UNWANTED); 804 response.set_verdict(ClientDownloadResponse::POTENTIALLY_UNWANTED);
714 factory.SetFakeResponse( 805 factory.SetFakeResponse(
715 DownloadProtectionService::GetDownloadRequestUrl(), 806 DownloadProtectionService::GetDownloadRequestUrl(),
716 response.SerializeAsString(), 807 response.SerializeAsString(),
717 net::HTTP_OK, net::URLRequestStatus::SUCCESS); 808 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
718 809
719 download_service_->CheckClientDownload( 810 download_service_->CheckClientDownload(
720 &item, 811 &item,
721 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 812 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
722 base::Unretained(this))); 813 base::Unretained(this)));
723 MessageLoop::current()->Run(); 814 MessageLoop::current()->Run();
724 #if defined(OS_WIN) 815 #if defined(OS_WIN)
725 EXPECT_TRUE(IsResult(DownloadProtectionService::POTENTIALLY_UNWANTED)); 816 EXPECT_TRUE(IsResult(DownloadProtectionService::POTENTIALLY_UNWANTED));
817 EXPECT_TRUE(HasClientDownloadRequest());
818 ClearClientDownloadRequest();
726 #else 819 #else
727 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 820 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
821 #if defined(OS_MACOSX)
822 // OSX sends pings for evaluation purposes.
823 EXPECT_TRUE(HasClientDownloadRequest());
824 ClearClientDownloadRequest();
825 #else
826 EXPECT_FALSE(HasClientDownloadRequest());
827 #endif
728 #endif 828 #endif
729 } 829 }
730 830
731 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadHTTPS) { 831 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadHTTPS) {
732 ClientDownloadResponse response; 832 ClientDownloadResponse response;
733 response.set_verdict(ClientDownloadResponse::DANGEROUS); 833 response.set_verdict(ClientDownloadResponse::DANGEROUS);
734 net::FakeURLFetcherFactory factory(NULL); 834 net::FakeURLFetcherFactory factory(NULL);
735 factory.SetFakeResponse( 835 factory.SetFakeResponse(
736 DownloadProtectionService::GetDownloadRequestUrl(), 836 DownloadProtectionService::GetDownloadRequestUrl(),
737 response.SerializeAsString(), 837 response.SerializeAsString(),
(...skipping 27 matching lines...) Expand all
765 EXPECT_CALL(*binary_feature_extractor_.get(), ExtractImageHeaders(a_tmp, _)) 865 EXPECT_CALL(*binary_feature_extractor_.get(), ExtractImageHeaders(a_tmp, _))
766 .Times(1); 866 .Times(1);
767 867
768 download_service_->CheckClientDownload( 868 download_service_->CheckClientDownload(
769 &item, 869 &item,
770 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 870 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
771 base::Unretained(this))); 871 base::Unretained(this)));
772 MessageLoop::current()->Run(); 872 MessageLoop::current()->Run();
773 #if defined(OS_WIN) 873 #if defined(OS_WIN)
774 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 874 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
875 EXPECT_TRUE(HasClientDownloadRequest());
876 ClearClientDownloadRequest();
775 #else 877 #else
776 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 878 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
879 #if defined(OS_MACOSX)
880 // OSX sends pings for evaluation purposes.
881 EXPECT_TRUE(HasClientDownloadRequest());
882 ClearClientDownloadRequest();
883 #else
884 EXPECT_FALSE(HasClientDownloadRequest());
885 #endif
777 #endif 886 #endif
778 } 887 }
779 888
780 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadZip) { 889 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadZip) {
781 ClientDownloadResponse response; 890 ClientDownloadResponse response;
782 response.set_verdict(ClientDownloadResponse::SAFE); 891 response.set_verdict(ClientDownloadResponse::SAFE);
783 net::FakeURLFetcherFactory factory(NULL); 892 net::FakeURLFetcherFactory factory(NULL);
784 // Empty response means SAFE. 893 // Empty response means SAFE.
785 factory.SetFakeResponse( 894 factory.SetFakeResponse(
786 DownloadProtectionService::GetDownloadRequestUrl(), 895 DownloadProtectionService::GetDownloadRequestUrl(),
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
819 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.txt")), 928 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.txt")),
820 file_contents.data(), file_contents.size())); 929 file_contents.data(), file_contents.size()));
821 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), a_tmp, false)); 930 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), a_tmp, false));
822 931
823 download_service_->CheckClientDownload( 932 download_service_->CheckClientDownload(
824 &item, 933 &item,
825 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 934 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
826 base::Unretained(this))); 935 base::Unretained(this)));
827 MessageLoop::current()->Run(); 936 MessageLoop::current()->Run();
828 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 937 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
938 EXPECT_FALSE(HasClientDownloadRequest());
829 Mock::VerifyAndClearExpectations(sb_service_.get()); 939 Mock::VerifyAndClearExpectations(sb_service_.get());
830 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); 940 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
831 941
832 // Now check with an executable in the zip file as well. 942 // Now check with an executable in the zip file as well.
833 ASSERT_EQ(static_cast<int>(file_contents.size()), base::WriteFile( 943 ASSERT_EQ(static_cast<int>(file_contents.size()), base::WriteFile(
834 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.exe")), 944 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.exe")),
835 file_contents.data(), file_contents.size())); 945 file_contents.data(), file_contents.size()));
836 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), a_tmp, false)); 946 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), a_tmp, false));
837 947
838 EXPECT_CALL(*sb_service_->mock_database_manager(), 948 EXPECT_CALL(*sb_service_->mock_database_manager(),
839 MatchDownloadWhitelistUrl(_)) 949 MatchDownloadWhitelistUrl(_))
840 .WillRepeatedly(Return(false)); 950 .WillRepeatedly(Return(false));
841 951
842 download_service_->CheckClientDownload( 952 download_service_->CheckClientDownload(
843 &item, 953 &item,
844 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 954 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
845 base::Unretained(this))); 955 base::Unretained(this)));
846 MessageLoop::current()->Run(); 956 MessageLoop::current()->Run();
847 #if defined(OS_WIN) 957 #if defined(OS_WIN)
848 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 958 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
959 EXPECT_TRUE(HasClientDownloadRequest());
960 ClearClientDownloadRequest();
849 #else 961 #else
850 // For !OS_WIN, no file types are currently supported. Hence the resulting 962 // For !OS_WIN, no file types are currently supported. Hence the resulting
851 // verdict is UNKNOWN. 963 // verdict is UNKNOWN.
852 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 964 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
965 #if defined(OS_MACOSX)
966 // OSX sends pings for evaluation purposes.
967 EXPECT_TRUE(HasClientDownloadRequest());
968 ClearClientDownloadRequest();
969 #else
970 EXPECT_FALSE(HasClientDownloadRequest());
971 #endif
853 #endif 972 #endif
854 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); 973 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
855 974
856 // If the response is dangerous the result should also be marked as 975 // If the response is dangerous the result should also be marked as
857 // dangerous. 976 // dangerous.
858 response.set_verdict(ClientDownloadResponse::DANGEROUS); 977 response.set_verdict(ClientDownloadResponse::DANGEROUS);
859 factory.SetFakeResponse( 978 factory.SetFakeResponse(
860 DownloadProtectionService::GetDownloadRequestUrl(), 979 DownloadProtectionService::GetDownloadRequestUrl(),
861 response.SerializeAsString(), 980 response.SerializeAsString(),
862 net::HTTP_OK, net::URLRequestStatus::SUCCESS); 981 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
863 982
864 download_service_->CheckClientDownload( 983 download_service_->CheckClientDownload(
865 &item, 984 &item,
866 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 985 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
867 base::Unretained(this))); 986 base::Unretained(this)));
868 MessageLoop::current()->Run(); 987 MessageLoop::current()->Run();
869 #if defined(OS_WIN) 988 #if defined(OS_WIN)
870 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 989 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
990 EXPECT_TRUE(HasClientDownloadRequest());
991 ClearClientDownloadRequest();
871 #else 992 #else
872 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 993 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
994 #if defined(OS_MACOSX)
995 // OSX sends pings for evaluation purposes.
996 EXPECT_TRUE(HasClientDownloadRequest());
997 ClearClientDownloadRequest();
998 #else
999 EXPECT_FALSE(HasClientDownloadRequest());
1000 #endif
873 #endif 1001 #endif
874 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); 1002 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
875 } 1003 }
876 1004
877 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadCorruptZip) { 1005 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadCorruptZip) {
878 base::ScopedTempDir download_dir; 1006 base::ScopedTempDir download_dir;
879 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); 1007 ASSERT_TRUE(download_dir.CreateUniqueTempDir());
880 1008
881 base::FilePath a_tmp(download_dir.path().Append(FILE_PATH_LITERAL("a.tmp"))); 1009 base::FilePath a_tmp(download_dir.path().Append(FILE_PATH_LITERAL("a.tmp")));
882 base::FilePath a_zip(FILE_PATH_LITERAL("a.zip")); 1010 base::FilePath a_zip(FILE_PATH_LITERAL("a.zip"));
(...skipping 18 matching lines...) Expand all
901 std::string file_contents = "corrupt zip file"; 1029 std::string file_contents = "corrupt zip file";
902 ASSERT_EQ(static_cast<int>(file_contents.size()), base::WriteFile( 1030 ASSERT_EQ(static_cast<int>(file_contents.size()), base::WriteFile(
903 a_tmp, file_contents.data(), file_contents.size())); 1031 a_tmp, file_contents.data(), file_contents.size()));
904 1032
905 download_service_->CheckClientDownload( 1033 download_service_->CheckClientDownload(
906 &item, 1034 &item,
907 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1035 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
908 base::Unretained(this))); 1036 base::Unretained(this)));
909 MessageLoop::current()->Run(); 1037 MessageLoop::current()->Run();
910 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 1038 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
1039 EXPECT_FALSE(HasClientDownloadRequest());
911 Mock::VerifyAndClearExpectations(sb_service_.get()); 1040 Mock::VerifyAndClearExpectations(sb_service_.get());
912 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); 1041 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
913 } 1042 }
914 1043
915 TEST_F(DownloadProtectionServiceTest, CheckClientCrxDownloadSuccess) { 1044 TEST_F(DownloadProtectionServiceTest, CheckClientCrxDownloadSuccess) {
916 ClientDownloadResponse response; 1045 ClientDownloadResponse response;
917 // Even if the server verdict is dangerous we should return SAFE because 1046 // Even if the server verdict is dangerous we should return SAFE because
918 // DownloadProtectionService::IsSupportedDownload() will return false 1047 // DownloadProtectionService::IsSupportedDownload() will return false
919 // for crx downloads. 1048 // for crx downloads.
920 response.set_verdict(ClientDownloadResponse::DANGEROUS); 1049 response.set_verdict(ClientDownloadResponse::DANGEROUS);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1003 .WillOnce(SetDosHeaderContents("dummy dos header")); 1132 .WillOnce(SetDosHeaderContents("dummy dos header"));
1004 download_service_->CheckClientDownload( 1133 download_service_->CheckClientDownload(
1005 &item, 1134 &item,
1006 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1135 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1007 base::Unretained(this))); 1136 base::Unretained(this)));
1008 1137
1009 // SendRequest is not called. Wait for FinishRequest to call our callback. 1138 // SendRequest is not called. Wait for FinishRequest to call our callback.
1010 MessageLoop::current()->Run(); 1139 MessageLoop::current()->Run();
1011 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1140 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1012 EXPECT_EQ(NULL, fetcher); 1141 EXPECT_EQ(NULL, fetcher);
1142 EXPECT_FALSE(HasClientDownloadRequest());
1013 } 1143 }
1014 #endif 1144 #endif
1015 1145
1016 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadValidateRequest) { 1146 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadValidateRequest) {
1017 net::TestURLFetcherFactory factory; 1147 net::TestURLFetcherFactory factory;
1018 1148
1019 base::FilePath tmp_path(FILE_PATH_LITERAL("bla.tmp")); 1149 base::FilePath tmp_path(FILE_PATH_LITERAL("bla.tmp"));
1020 base::FilePath final_path(FILE_PATH_LITERAL("bla.exe")); 1150 base::FilePath final_path(FILE_PATH_LITERAL("bla.exe"));
1021 std::vector<GURL> url_chain; 1151 std::vector<GURL> url_chain;
1022 url_chain.push_back(GURL("http://www.google.com/")); 1152 url_chain.push_back(GURL("http://www.google.com/"));
(...skipping 26 matching lines...) Expand all
1049 download_service_->CheckClientDownload( 1179 download_service_->CheckClientDownload(
1050 &item, 1180 &item,
1051 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1181 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1052 base::Unretained(this))); 1182 base::Unretained(this)));
1053 1183
1054 #if !defined(OS_WIN) && !defined(OS_MACOSX) 1184 #if !defined(OS_WIN) && !defined(OS_MACOSX)
1055 // SendRequest is not called. Wait for FinishRequest to call our callback. 1185 // SendRequest is not called. Wait for FinishRequest to call our callback.
1056 MessageLoop::current()->Run(); 1186 MessageLoop::current()->Run();
1057 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1187 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1058 EXPECT_EQ(NULL, fetcher); 1188 EXPECT_EQ(NULL, fetcher);
1189 EXPECT_FALSE(HasClientDownloadRequest());
1059 #else 1190 #else
1060 // Run the message loop(s) until SendRequest is called. 1191 // Run the message loop(s) until SendRequest is called.
1061 FlushThreadMessageLoops(); 1192 FlushThreadMessageLoops();
1193 EXPECT_TRUE(HasClientDownloadRequest());
1194 ClearClientDownloadRequest();
1062 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1195 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1063 ASSERT_TRUE(fetcher); 1196 ASSERT_TRUE(fetcher);
1064 ClientDownloadRequest request; 1197 ClientDownloadRequest request;
1065 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data())); 1198 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data()));
1066 EXPECT_EQ("http://www.google.com/bla.exe", request.url()); 1199 EXPECT_EQ("http://www.google.com/bla.exe", request.url());
1067 EXPECT_EQ(hash, request.digests().sha256()); 1200 EXPECT_EQ(hash, request.digests().sha256());
1068 EXPECT_EQ(item.GetReceivedBytes(), request.length()); 1201 EXPECT_EQ(item.GetReceivedBytes(), request.length());
1069 EXPECT_EQ(item.HasUserGesture(), request.user_initiated()); 1202 EXPECT_EQ(item.HasUserGesture(), request.user_initiated());
1070 EXPECT_TRUE(RequestContainsServerIp(request, remote_address)); 1203 EXPECT_TRUE(RequestContainsServerIp(request, remote_address));
1071 EXPECT_EQ(2, request.resources_size()); 1204 EXPECT_EQ(2, request.resources_size());
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
1134 download_service_->CheckClientDownload( 1267 download_service_->CheckClientDownload(
1135 &item, 1268 &item,
1136 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1269 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1137 base::Unretained(this))); 1270 base::Unretained(this)));
1138 1271
1139 #if !defined(OS_WIN) && !defined(OS_MACOSX) 1272 #if !defined(OS_WIN) && !defined(OS_MACOSX)
1140 // SendRequest is not called. Wait for FinishRequest to call our callback. 1273 // SendRequest is not called. Wait for FinishRequest to call our callback.
1141 MessageLoop::current()->Run(); 1274 MessageLoop::current()->Run();
1142 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1275 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1143 EXPECT_EQ(NULL, fetcher); 1276 EXPECT_EQ(NULL, fetcher);
1277 EXPECT_FALSE(HasClientDownloadRequest());
1144 #else 1278 #else
1145 // Run the message loop(s) until SendRequest is called. 1279 // Run the message loop(s) until SendRequest is called.
1146 FlushThreadMessageLoops(); 1280 FlushThreadMessageLoops();
1281 EXPECT_TRUE(HasClientDownloadRequest());
1282 ClearClientDownloadRequest();
1147 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1283 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1148 ASSERT_TRUE(fetcher); 1284 ASSERT_TRUE(fetcher);
1149 ClientDownloadRequest request; 1285 ClientDownloadRequest request;
1150 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data())); 1286 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data()));
1151 EXPECT_EQ("ftp://www.google.com/bla.exe", request.url()); 1287 EXPECT_EQ("ftp://www.google.com/bla.exe", request.url());
1152 EXPECT_EQ(hash, request.digests().sha256()); 1288 EXPECT_EQ(hash, request.digests().sha256());
1153 EXPECT_EQ(item.GetReceivedBytes(), request.length()); 1289 EXPECT_EQ(item.GetReceivedBytes(), request.length());
1154 EXPECT_EQ(item.HasUserGesture(), request.user_initiated()); 1290 EXPECT_EQ(item.HasUserGesture(), request.user_initiated());
1155 EXPECT_EQ(2, request.resources_size()); 1291 EXPECT_EQ(2, request.resources_size());
1156 EXPECT_TRUE(RequestContainsResource(request, 1292 EXPECT_TRUE(RequestContainsResource(request,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1222 download_service_->CheckClientDownload( 1358 download_service_->CheckClientDownload(
1223 &item, 1359 &item,
1224 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1360 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1225 base::Unretained(this))); 1361 base::Unretained(this)));
1226 1362
1227 #if !defined(OS_WIN) && !defined(OS_MACOSX) 1363 #if !defined(OS_WIN) && !defined(OS_MACOSX)
1228 // SendRequest is not called. Wait for FinishRequest to call our callback. 1364 // SendRequest is not called. Wait for FinishRequest to call our callback.
1229 MessageLoop::current()->Run(); 1365 MessageLoop::current()->Run();
1230 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1366 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1231 EXPECT_EQ(NULL, fetcher); 1367 EXPECT_EQ(NULL, fetcher);
1368 EXPECT_FALSE(HasClientDownloadRequest());
1232 #else 1369 #else
1233 EXPECT_EQ(0, fetcher_watcher.WaitForRequest()); 1370 EXPECT_EQ(0, fetcher_watcher.WaitForRequest());
1371 EXPECT_TRUE(HasClientDownloadRequest());
1372 ClearClientDownloadRequest();
1234 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1373 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1235 ASSERT_TRUE(fetcher); 1374 ASSERT_TRUE(fetcher);
1236 ClientDownloadRequest request; 1375 ClientDownloadRequest request;
1237 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data())); 1376 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data()));
1238 EXPECT_EQ("http://www.google.com/bla.exe", request.url()); 1377 EXPECT_EQ("http://www.google.com/bla.exe", request.url());
1239 EXPECT_EQ(hash, request.digests().sha256()); 1378 EXPECT_EQ(hash, request.digests().sha256());
1240 EXPECT_EQ(item.GetReceivedBytes(), request.length()); 1379 EXPECT_EQ(item.GetReceivedBytes(), request.length());
1241 EXPECT_EQ(item.HasUserGesture(), request.user_initiated()); 1380 EXPECT_EQ(item.HasUserGesture(), request.user_initiated());
1242 EXPECT_TRUE(RequestContainsServerIp(request, remote_address)); 1381 EXPECT_TRUE(RequestContainsServerIp(request, remote_address));
1243 EXPECT_EQ(3, request.resources_size()); 1382 EXPECT_EQ(3, request.resources_size());
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1297 TestURLFetcherWatcher fetcher_watcher(&factory); 1436 TestURLFetcherWatcher fetcher_watcher(&factory);
1298 download_service_->CheckClientDownload( 1437 download_service_->CheckClientDownload(
1299 &item, 1438 &item,
1300 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1439 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1301 base::Unretained(this))); 1440 base::Unretained(this)));
1302 #if !defined(OS_WIN) && !defined(OS_MACOSX) 1441 #if !defined(OS_WIN) && !defined(OS_MACOSX)
1303 // SendRequest is not called. Wait for FinishRequest to call our callback. 1442 // SendRequest is not called. Wait for FinishRequest to call our callback.
1304 MessageLoop::current()->Run(); 1443 MessageLoop::current()->Run();
1305 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1444 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1306 EXPECT_EQ(NULL, fetcher); 1445 EXPECT_EQ(NULL, fetcher);
1446 EXPECT_FALSE(HasClientDownloadRequest());
1307 #else 1447 #else
1308 EXPECT_EQ(0, fetcher_watcher.WaitForRequest()); 1448 EXPECT_EQ(0, fetcher_watcher.WaitForRequest());
1449 EXPECT_TRUE(HasClientDownloadRequest());
1450 ClearClientDownloadRequest();
1309 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1451 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1310 ASSERT_TRUE(fetcher); 1452 ASSERT_TRUE(fetcher);
1311 ClientDownloadRequest request; 1453 ClientDownloadRequest request;
1312 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data())); 1454 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data()));
1313 EXPECT_EQ("http://www.google.com/bla.exe", request.url()); 1455 EXPECT_EQ("http://www.google.com/bla.exe", request.url());
1314 EXPECT_EQ(hash, request.digests().sha256()); 1456 EXPECT_EQ(hash, request.digests().sha256());
1315 EXPECT_EQ(item.GetReceivedBytes(), request.length()); 1457 EXPECT_EQ(item.GetReceivedBytes(), request.length());
1316 EXPECT_EQ(item.HasUserGesture(), request.user_initiated()); 1458 EXPECT_EQ(item.HasUserGesture(), request.user_initiated());
1317 EXPECT_TRUE(RequestContainsServerIp(request, remote_address)); 1459 EXPECT_TRUE(RequestContainsServerIp(request, remote_address));
1318 EXPECT_EQ(5, request.resources_size()); 1460 EXPECT_EQ(5, request.resources_size());
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1453 download_service_->download_request_timeout_ms_ = 10; 1595 download_service_->download_request_timeout_ms_ = 10;
1454 download_service_->CheckClientDownload( 1596 download_service_->CheckClientDownload(
1455 &item, 1597 &item,
1456 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1598 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1457 base::Unretained(this))); 1599 base::Unretained(this)));
1458 1600
1459 // The request should time out because the HTTP request hasn't returned 1601 // The request should time out because the HTTP request hasn't returned
1460 // anything yet. 1602 // anything yet.
1461 MessageLoop::current()->Run(); 1603 MessageLoop::current()->Run();
1462 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 1604 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
1605 #if defined(OS_WIN) || defined(OS_MACOSX)
1606 EXPECT_TRUE(HasClientDownloadRequest());
1607 ClearClientDownloadRequest();
1608 #else
1609 EXPECT_FALSE(HasClientDownloadRequest());
1610 #endif
1463 } 1611 }
1464 1612
1465 TEST_F(DownloadProtectionServiceTest, TestDownloadItemDestroyed) { 1613 TEST_F(DownloadProtectionServiceTest, TestDownloadItemDestroyed) {
1466 net::TestURLFetcherFactory factory; 1614 net::TestURLFetcherFactory factory;
1467 1615
1468 std::vector<GURL> url_chain; 1616 std::vector<GURL> url_chain;
1469 url_chain.push_back(GURL("http://www.evil.com/bla.exe")); 1617 url_chain.push_back(GURL("http://www.evil.com/bla.exe"));
1470 GURL referrer("http://www.google.com/"); 1618 GURL referrer("http://www.google.com/");
1471 base::FilePath tmp_path(FILE_PATH_LITERAL("a.tmp")); 1619 base::FilePath tmp_path(FILE_PATH_LITERAL("a.tmp"));
1472 base::FilePath final_path(FILE_PATH_LITERAL("a.exe")); 1620 base::FilePath final_path(FILE_PATH_LITERAL("a.exe"));
(...skipping 23 matching lines...) Expand all
1496 1644
1497 download_service_->CheckClientDownload( 1645 download_service_->CheckClientDownload(
1498 &item, 1646 &item,
1499 base::Bind(&DownloadProtectionServiceTest::SyncCheckDoneCallback, 1647 base::Bind(&DownloadProtectionServiceTest::SyncCheckDoneCallback,
1500 base::Unretained(this))); 1648 base::Unretained(this)));
1501 // MockDownloadItem going out of scope triggers the OnDownloadDestroyed 1649 // MockDownloadItem going out of scope triggers the OnDownloadDestroyed
1502 // notification. 1650 // notification.
1503 } 1651 }
1504 1652
1505 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 1653 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
1654 EXPECT_FALSE(HasClientDownloadRequest());
1506 } 1655 }
1507 1656
1508 TEST_F(DownloadProtectionServiceTest, GetCertificateWhitelistStrings) { 1657 TEST_F(DownloadProtectionServiceTest, GetCertificateWhitelistStrings) {
1509 // We'll pass this cert in as the "issuer", even though it isn't really 1658 // We'll pass this cert in as the "issuer", even though it isn't really
1510 // used to sign the certs below. GetCertificateWhitelistStirngs doesn't care 1659 // used to sign the certs below. GetCertificateWhitelistStirngs doesn't care
1511 // about this. 1660 // about this.
1512 scoped_refptr<net::X509Certificate> issuer_cert( 1661 scoped_refptr<net::X509Certificate> issuer_cert(
1513 ReadTestCertificate("issuer.pem")); 1662 ReadTestCertificate("issuer.pem"));
1514 ASSERT_TRUE(issuer_cert.get()); 1663 ASSERT_TRUE(issuer_cert.get());
1515 std::string cert_base = "cert/" + base::HexEncode( 1664 std::string cert_base = "cert/" + base::HexEncode(
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1584 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); 1733 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit"));
1585 1734
1586 cert = ReadTestCertificate("test_c.pem"); 1735 cert = ReadTestCertificate("test_c.pem");
1587 ASSERT_TRUE(cert.get()); 1736 ASSERT_TRUE(cert.get());
1588 whitelist_strings.clear(); 1737 whitelist_strings.clear();
1589 GetCertificateWhitelistStrings( 1738 GetCertificateWhitelistStrings(
1590 *cert.get(), *issuer_cert.get(), &whitelist_strings); 1739 *cert.get(), *issuer_cert.get(), &whitelist_strings);
1591 EXPECT_THAT(whitelist_strings, ElementsAre()); 1740 EXPECT_THAT(whitelist_strings, ElementsAre());
1592 } 1741 }
1593 } // namespace safe_browsing 1742 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698