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

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

Issue 2146703002: Sample 1% url whitelisted PPAPI downloads to ping safe browsing server (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: refactor unit tests Created 4 years, 5 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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <map> 10 #include <map>
11 #include <memory> 11 #include <memory>
12 #include <string> 12 #include <string>
13 13
14 #include "base/base_paths.h" 14 #include "base/base_paths.h"
15 #include "base/bind.h" 15 #include "base/bind.h"
16 #include "base/callback.h" 16 #include "base/callback.h"
17 #include "base/command_line.h" 17 #include "base/command_line.h"
18 #include "base/files/file_path.h" 18 #include "base/files/file_path.h"
19 #include "base/files/file_util.h" 19 #include "base/files/file_util.h"
20 #include "base/files/scoped_temp_dir.h" 20 #include "base/files/scoped_temp_dir.h"
21 #include "base/location.h" 21 #include "base/location.h"
22 #include "base/macros.h" 22 #include "base/macros.h"
23 #include "base/memory/ref_counted.h" 23 #include "base/memory/ref_counted.h"
24 #include "base/message_loop/message_loop.h"
25 #include "base/path_service.h" 24 #include "base/path_service.h"
26 #include "base/run_loop.h" 25 #include "base/run_loop.h"
27 #include "base/sha1.h" 26 #include "base/sha1.h"
28 #include "base/single_thread_task_runner.h" 27 #include "base/single_thread_task_runner.h"
29 #include "base/strings/string_number_conversions.h" 28 #include "base/strings/string_number_conversions.h"
30 #include "base/threading/sequenced_worker_pool.h" 29 #include "base/threading/sequenced_worker_pool.h"
31 #include "base/threading/thread_task_runner_handle.h" 30 #include "base/threading/thread_task_runner_handle.h"
32 #include "chrome/browser/history/history_service_factory.h" 31 #include "chrome/browser/history/history_service_factory.h"
33 #include "chrome/browser/safe_browsing/download_feedback_service.h" 32 #include "chrome/browser/safe_browsing/download_feedback_service.h"
34 #include "chrome/browser/safe_browsing/incident_reporting/incident_reporting_ser vice.h" 33 #include "chrome/browser/safe_browsing/incident_reporting/incident_reporting_ser vice.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 using ::testing::DoAll; 65 using ::testing::DoAll;
67 using ::testing::ElementsAre; 66 using ::testing::ElementsAre;
68 using ::testing::Invoke; 67 using ::testing::Invoke;
69 using ::testing::Mock; 68 using ::testing::Mock;
70 using ::testing::NotNull; 69 using ::testing::NotNull;
71 using ::testing::Return; 70 using ::testing::Return;
72 using ::testing::ReturnRef; 71 using ::testing::ReturnRef;
73 using ::testing::SaveArg; 72 using ::testing::SaveArg;
74 using ::testing::StrictMock; 73 using ::testing::StrictMock;
75 using ::testing::_; 74 using ::testing::_;
76 using base::MessageLoop; 75 using base::RunLoop;
77 using content::BrowserThread; 76 using content::BrowserThread;
78 namespace safe_browsing { 77 namespace safe_browsing {
79 namespace { 78 namespace {
80 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for 79 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for
81 // a given URL. 80 // a given URL.
82 class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { 81 class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager {
83 public: 82 public:
84 MockSafeBrowsingDatabaseManager() {} 83 MockSafeBrowsingDatabaseManager() {}
85 84
86 MOCK_METHOD1(MatchDownloadWhitelistUrl, bool(const GURL&)); 85 MOCK_METHOD1(MatchDownloadWhitelistUrl, bool(const GURL&));
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 void OnRequestEnd(int fetcher_id) override {} 180 void OnRequestEnd(int fetcher_id) override {}
182 181
183 int WaitForRequest() { 182 int WaitForRequest() {
184 run_loop_.Run(); 183 run_loop_.Run();
185 return fetcher_id_; 184 return fetcher_id_;
186 } 185 }
187 186
188 private: 187 private:
189 net::TestURLFetcherFactory* factory_; 188 net::TestURLFetcherFactory* factory_;
190 int fetcher_id_; 189 int fetcher_id_;
191 base::RunLoop run_loop_; 190 RunLoop run_loop_;
192 }; 191 };
193 } // namespace 192 } // namespace
194 193
195 ACTION_P(SetCertificateContents, contents) { 194 ACTION_P(SetCertificateContents, contents) {
196 arg1->add_certificate_chain()->add_element()->set_certificate(contents); 195 arg1->add_certificate_chain()->add_element()->set_certificate(contents);
197 } 196 }
198 197
199 ACTION_P(SetDosHeaderContents, contents) { 198 ACTION_P(SetDosHeaderContents, contents) {
200 arg2->mutable_pe_headers()->set_dos_header(contents); 199 arg2->mutable_pe_headers()->set_dos_header(contents);
201 return true; 200 return true;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 binary_feature_extractor_ = new StrictMock<MockBinaryFeatureExtractor>(); 250 binary_feature_extractor_ = new StrictMock<MockBinaryFeatureExtractor>();
252 ON_CALL(*binary_feature_extractor_, ExtractImageFeatures(_, _, _, _)) 251 ON_CALL(*binary_feature_extractor_, ExtractImageFeatures(_, _, _, _))
253 .WillByDefault(Return(true)); 252 .WillByDefault(Return(true));
254 download_service_ = sb_service_->download_protection_service(); 253 download_service_ = sb_service_->download_protection_service();
255 download_service_->binary_feature_extractor_ = binary_feature_extractor_; 254 download_service_->binary_feature_extractor_ = binary_feature_extractor_;
256 download_service_->SetEnabled(true); 255 download_service_->SetEnabled(true);
257 client_download_request_subscription_ = 256 client_download_request_subscription_ =
258 download_service_->RegisterClientDownloadRequestCallback( 257 download_service_->RegisterClientDownloadRequestCallback(
259 base::Bind(&DownloadProtectionServiceTest::OnClientDownloadRequest, 258 base::Bind(&DownloadProtectionServiceTest::OnClientDownloadRequest,
260 base::Unretained(this))); 259 base::Unretained(this)));
261 base::RunLoop().RunUntilIdle(); 260 RunLoop().RunUntilIdle();
262 has_result_ = false; 261 has_result_ = false;
263 262
264 base::FilePath source_path; 263 base::FilePath source_path;
265 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_path)); 264 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_path));
266 testdata_path_ = source_path 265 testdata_path_ = source_path
267 .AppendASCII("chrome") 266 .AppendASCII("chrome")
268 .AppendASCII("test") 267 .AppendASCII("test")
269 .AppendASCII("data") 268 .AppendASCII("data")
270 .AppendASCII("safe_browsing") 269 .AppendASCII("safe_browsing")
271 .AppendASCII("download_protection"); 270 .AppendASCII("download_protection");
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 if (archived_binary.file_basename() == file_basename) 338 if (archived_binary.file_basename() == file_basename)
340 return &archived_binary; 339 return &archived_binary;
341 } 340 }
342 return nullptr; 341 return nullptr;
343 } 342 }
344 343
345 // Flushes any pending tasks in the message loops of all threads. 344 // Flushes any pending tasks in the message loops of all threads.
346 void FlushThreadMessageLoops() { 345 void FlushThreadMessageLoops() {
347 BrowserThread::GetBlockingPool()->FlushForTesting(); 346 BrowserThread::GetBlockingPool()->FlushForTesting();
348 FlushMessageLoop(BrowserThread::IO); 347 FlushMessageLoop(BrowserThread::IO);
349 base::RunLoop().RunUntilIdle(); 348 RunLoop().RunUntilIdle();
350 } 349 }
351 350
352 // Proxy for private method. 351 // Proxy for private method.
353 static void GetCertificateWhitelistStrings( 352 static void GetCertificateWhitelistStrings(
354 const net::X509Certificate& certificate, 353 const net::X509Certificate& certificate,
355 const net::X509Certificate& issuer, 354 const net::X509Certificate& issuer,
356 std::vector<std::string>* whitelist_strings) { 355 std::vector<std::string>* whitelist_strings) {
357 DownloadProtectionService::GetCertificateWhitelistStrings( 356 DownloadProtectionService::GetCertificateWhitelistStrings(
358 certificate, issuer, whitelist_strings); 357 certificate, issuer, whitelist_strings);
359 } 358 }
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 EXPECT_CALL(*item, GetTabReferrerUrl()) 433 EXPECT_CALL(*item, GetTabReferrerUrl())
435 .WillRepeatedly(ReturnRef(GURL::EmptyGURL())); 434 .WillRepeatedly(ReturnRef(GURL::EmptyGURL()));
436 EXPECT_CALL(*item, GetHash()).WillRepeatedly(ReturnRef(hash_)); 435 EXPECT_CALL(*item, GetHash()).WillRepeatedly(ReturnRef(hash_));
437 EXPECT_CALL(*item, GetReceivedBytes()).WillRepeatedly(Return(100)); 436 EXPECT_CALL(*item, GetReceivedBytes()).WillRepeatedly(Return(100));
438 EXPECT_CALL(*item, HasUserGesture()).WillRepeatedly(Return(true)); 437 EXPECT_CALL(*item, HasUserGesture()).WillRepeatedly(Return(true));
439 EXPECT_CALL(*item, GetRemoteAddress()).WillRepeatedly(Return("")); 438 EXPECT_CALL(*item, GetRemoteAddress()).WillRepeatedly(Return(""));
440 } 439 }
441 440
442 private: 441 private:
443 // Helper functions for FlushThreadMessageLoops. 442 // Helper functions for FlushThreadMessageLoops.
444 void RunAllPendingAndQuitUI() { 443 void RunAllPendingAndQuitUI(base::Closure quit_closure) {
445 base::RunLoop().RunUntilIdle(); 444 RunLoop().RunUntilIdle();
446 BrowserThread::PostTask( 445 BrowserThread::PostTask(
447 BrowserThread::UI, 446 BrowserThread::UI, FROM_HERE,
448 FROM_HERE,
449 base::Bind(&DownloadProtectionServiceTest::QuitMessageLoop, 447 base::Bind(&DownloadProtectionServiceTest::QuitMessageLoop,
450 base::Unretained(this))); 448 base::Unretained(this), quit_closure));
451 } 449 }
452 450
453 void QuitMessageLoop() { base::MessageLoop::current()->QuitWhenIdle(); } 451 void QuitMessageLoop(base::Closure quit_closure) { quit_closure.Run(); }
454 452
455 void PostRunMessageLoopTask(BrowserThread::ID thread) { 453 void PostRunMessageLoopTask(base::Closure quit_closure,
454 BrowserThread::ID thread) {
456 BrowserThread::PostTask( 455 BrowserThread::PostTask(
457 thread, 456 thread, FROM_HERE,
458 FROM_HERE,
459 base::Bind(&DownloadProtectionServiceTest::RunAllPendingAndQuitUI, 457 base::Bind(&DownloadProtectionServiceTest::RunAllPendingAndQuitUI,
460 base::Unretained(this))); 458 base::Unretained(this), quit_closure));
461 } 459 }
462 460
463 void FlushMessageLoop(BrowserThread::ID thread) { 461 void FlushMessageLoop(BrowserThread::ID thread) {
462 RunLoop run_loop;
464 BrowserThread::PostTask( 463 BrowserThread::PostTask(
465 BrowserThread::UI, 464 BrowserThread::UI, FROM_HERE,
466 FROM_HERE,
467 base::Bind(&DownloadProtectionServiceTest::PostRunMessageLoopTask, 465 base::Bind(&DownloadProtectionServiceTest::PostRunMessageLoopTask,
468 base::Unretained(this), thread)); 466 base::Unretained(this), run_loop.QuitClosure(), thread));
469 base::RunLoop().Run(); 467 run_loop.Run();
470 } 468 }
471 469
472 void OnClientDownloadRequest(content::DownloadItem* download, 470 void OnClientDownloadRequest(content::DownloadItem* download,
473 const ClientDownloadRequest* request) { 471 const ClientDownloadRequest* request) {
474 if (request) 472 if (request)
475 last_client_download_request_.reset(new ClientDownloadRequest(*request)); 473 last_client_download_request_.reset(new ClientDownloadRequest(*request));
476 else 474 else
477 last_client_download_request_.reset(); 475 last_client_download_request_.reset();
478 } 476 }
479 477
480 public: 478 public:
481 enum ArchiveType { ZIP, DMG }; 479 enum ArchiveType { ZIP, DMG };
482 480
483 void CheckDoneCallback( 481 void CheckDoneCallback(
482 base::Closure quit_closure,
484 DownloadProtectionService::DownloadCheckResult result) { 483 DownloadProtectionService::DownloadCheckResult result) {
485 result_ = result; 484 result_ = result;
486 has_result_ = true; 485 has_result_ = true;
487 MessageLoop::current()->QuitWhenIdle(); 486 quit_closure.Run();
488 } 487 }
489 488
490 void SyncCheckDoneCallback( 489 void SyncCheckDoneCallback(
491 DownloadProtectionService::DownloadCheckResult result) { 490 DownloadProtectionService::DownloadCheckResult result) {
492 result_ = result; 491 result_ = result;
493 has_result_ = true; 492 has_result_ = true;
494 } 493 }
495 494
496 void SendURLFetchComplete(net::TestURLFetcher* fetcher) { 495 void SendURLFetchComplete(net::TestURLFetcher* fetcher) {
497 fetcher->delegate()->OnURLFetchComplete(fetcher); 496 fetcher->delegate()->OnURLFetchComplete(fetcher);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 548
550 void DownloadProtectionServiceTest::CheckClientDownloadReportCorruptArchive( 549 void DownloadProtectionServiceTest::CheckClientDownloadReportCorruptArchive(
551 ArchiveType type, 550 ArchiveType type,
552 bool is_extended_reporting, 551 bool is_extended_reporting,
553 bool is_incognito) { 552 bool is_incognito) {
554 net::FakeURLFetcherFactory factory(NULL); 553 net::FakeURLFetcherFactory factory(NULL);
555 PrepareResponse( 554 PrepareResponse(
556 &factory, ClientDownloadResponse::SAFE, net::HTTP_OK, 555 &factory, ClientDownloadResponse::SAFE, net::HTTP_OK,
557 net::URLRequestStatus::SUCCESS); 556 net::URLRequestStatus::SUCCESS);
558 557
559 profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled, 558 SetExtendedReportingPreference(is_extended_reporting);
560 is_extended_reporting);
561 content::MockDownloadItem item; 559 content::MockDownloadItem item;
562 if (type == ZIP) { 560 if (type == ZIP) {
563 PrepareBasicDownloadItem(&item, {"http://www.evil.com/a.zip"}, // url_chain 561 PrepareBasicDownloadItem(&item, {"http://www.evil.com/a.zip"}, // url_chain
564 "http://www.google.com/", // referrer 562 "http://www.google.com/", // referrer
565 FILE_PATH_LITERAL("a.tmp"), // tmp_path 563 FILE_PATH_LITERAL("a.tmp"), // tmp_path
566 FILE_PATH_LITERAL("a.zip")); // final_path 564 FILE_PATH_LITERAL("a.zip")); // final_path
567 } else if (type == DMG) { 565 } else if (type == DMG) {
568 PrepareBasicDownloadItem(&item, {"http://www.evil.com/a.dmg"}, // url_chain 566 PrepareBasicDownloadItem(&item, {"http://www.evil.com/a.dmg"}, // url_chain
569 "http://www.google.com/", // referrer 567 "http://www.google.com/", // referrer
570 FILE_PATH_LITERAL("a.tmp"), // tmp_path 568 FILE_PATH_LITERAL("a.tmp"), // tmp_path
571 FILE_PATH_LITERAL("a.dmg")); // final_path 569 FILE_PATH_LITERAL("a.dmg")); // final_path
572 } 570 }
573 571
574 if (is_incognito) { 572 if (is_incognito) {
575 EXPECT_CALL(item, GetBrowserContext()) 573 EXPECT_CALL(item, GetBrowserContext())
576 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile())); 574 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile()));
577 } else { 575 } else {
578 EXPECT_CALL(item, GetBrowserContext()) 576 EXPECT_CALL(item, GetBrowserContext())
579 .WillRepeatedly(Return(profile_.get())); 577 .WillRepeatedly(Return(profile_.get()));
580 } 578 }
581 579
582 std::string file_contents = "corrupt archive file"; 580 std::string file_contents = "corrupt archive file";
583 ASSERT_EQ(static_cast<int>(file_contents.size()), base::WriteFile( 581 ASSERT_EQ(static_cast<int>(file_contents.size()), base::WriteFile(
584 tmp_path_, file_contents.data(), file_contents.size())); 582 tmp_path_, file_contents.data(), file_contents.size()));
585 583
584 RunLoop run_loop;
586 download_service_->CheckClientDownload( 585 download_service_->CheckClientDownload(
587 &item, 586 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
588 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 587 base::Unretained(this), run_loop.QuitClosure()));
589 base::Unretained(this))); 588 run_loop.Run();
590 base::RunLoop().Run();
591 589
592 const bool expect_request = 590 const bool expect_request =
593 type == ZIP ? is_extended_reporting && !is_incognito : true; 591 type == ZIP ? is_extended_reporting && !is_incognito : true;
594 592
595 if (expect_request) { 593 if (expect_request) {
596 ASSERT_TRUE(HasClientDownloadRequest()); 594 ASSERT_TRUE(HasClientDownloadRequest());
597 EXPECT_EQ(0, GetClientDownloadRequest()->archived_binary_size()); 595 EXPECT_EQ(0, GetClientDownloadRequest()->archived_binary_size());
598 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); 596 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type());
599 ClientDownloadRequest::DownloadType expected_type = 597 ClientDownloadRequest::DownloadType expected_type =
600 type == ZIP ? ClientDownloadRequest_DownloadType_INVALID_ZIP 598 type == ZIP ? ClientDownloadRequest_DownloadType_INVALID_ZIP
601 : ClientDownloadRequest_DownloadType_INVALID_MAC_ARCHIVE; 599 : ClientDownloadRequest_DownloadType_INVALID_MAC_ARCHIVE;
602 EXPECT_EQ(expected_type, GetClientDownloadRequest()->download_type()); 600 EXPECT_EQ(expected_type, GetClientDownloadRequest()->download_type());
603 ClearClientDownloadRequest(); 601 ClearClientDownloadRequest();
604 } else { 602 } else {
605 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 603 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
606 EXPECT_FALSE(HasClientDownloadRequest()); 604 EXPECT_FALSE(HasClientDownloadRequest());
607 } 605 }
608 606
609 Mock::VerifyAndClearExpectations(sb_service_.get()); 607 Mock::VerifyAndClearExpectations(sb_service_.get());
610 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); 608 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
611 } 609 }
612 610
613 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { 611 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) {
614 content::MockDownloadItem item; 612 content::MockDownloadItem item;
615 PrepareBasicDownloadItem( 613 {
616 &item, 614 PrepareBasicDownloadItem(&item,
617 std::vector<std::string>(), // empty url_chain 615 std::vector<std::string>(), // empty url_chain
618 "http://www.google.com/", // referrer 616 "http://www.google.com/", // referrer
619 FILE_PATH_LITERAL("a.tmp"), // tmp_path 617 FILE_PATH_LITERAL("a.tmp"), // tmp_path
620 FILE_PATH_LITERAL("a.exe")); // final_path 618 FILE_PATH_LITERAL("a.exe")); // final_path
621 download_service_->CheckClientDownload( 619 RunLoop run_loop;
622 &item, 620 download_service_->CheckClientDownload(
623 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 621 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
624 base::Unretained(this))); 622 base::Unretained(this), run_loop.QuitClosure()));
625 base::RunLoop().Run(); 623 run_loop.Run();
626 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 624 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
627 EXPECT_FALSE(HasClientDownloadRequest()); 625 EXPECT_FALSE(HasClientDownloadRequest());
628 Mock::VerifyAndClearExpectations(&item); 626 Mock::VerifyAndClearExpectations(&item);
627 }
629 628
630 PrepareBasicDownloadItem( 629 {
631 &item, 630 PrepareBasicDownloadItem(&item, {"file://www.google.com/"}, // url_chain
632 {"file://www.google.com/"}, // url_chain 631 "http://www.google.com/", // referrer
633 "http://www.google.com/", // referrer 632 FILE_PATH_LITERAL("a.tmp"), // tmp_path
634 FILE_PATH_LITERAL("a.tmp"), // tmp_path 633 FILE_PATH_LITERAL("a.exe")); // final_path
635 FILE_PATH_LITERAL("a.exe")); // final_path 634 RunLoop run_loop;
636 download_service_->CheckClientDownload( 635 download_service_->CheckClientDownload(
637 &item, 636 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
638 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 637 base::Unretained(this), run_loop.QuitClosure()));
639 base::Unretained(this))); 638 run_loop.Run();
640 base::RunLoop().Run(); 639 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
641 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 640 EXPECT_FALSE(HasClientDownloadRequest());
642 EXPECT_FALSE(HasClientDownloadRequest()); 641 }
643 } 642 }
644 643
645 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadNotABinary) { 644 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadNotABinary) {
646 content::MockDownloadItem item; 645 content::MockDownloadItem item;
647 PrepareBasicDownloadItem( 646 PrepareBasicDownloadItem(
648 &item, 647 &item,
649 std::vector<std::string>(), // empty url_chain 648 std::vector<std::string>(), // empty url_chain
650 "http://www.google.com/", // referrer 649 "http://www.google.com/", // referrer
651 FILE_PATH_LITERAL("a.tmp"), // tmp_path 650 FILE_PATH_LITERAL("a.tmp"), // tmp_path
652 FILE_PATH_LITERAL("a.txt")); // final_path 651 FILE_PATH_LITERAL("a.txt")); // final_path
652 RunLoop run_loop;
653 download_service_->CheckClientDownload( 653 download_service_->CheckClientDownload(
654 &item, 654 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
655 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 655 base::Unretained(this), run_loop.QuitClosure()));
656 base::Unretained(this))); 656 run_loop.Run();
657 base::RunLoop().Run();
658 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 657 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
659 EXPECT_FALSE(HasClientDownloadRequest()); 658 EXPECT_FALSE(HasClientDownloadRequest());
660 } 659 }
661 660
662 TEST_F(DownloadProtectionServiceTest, 661 TEST_F(DownloadProtectionServiceTest,
663 CheckClientDownloadWhitelistedUrlWithoutSampling) { 662 CheckClientDownloadWhitelistedUrlWithoutSampling) {
664 // Response to any requests will be DANGEROUS. 663 // Response to any requests will be DANGEROUS.
665 net::FakeURLFetcherFactory factory(NULL); 664 net::FakeURLFetcherFactory factory(NULL);
666 PrepareResponse( 665 PrepareResponse(
667 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, 666 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
(...skipping 16 matching lines...) Expand all
684 683
685 // We should not get whilelist checks for other URLs than specified below. 684 // We should not get whilelist checks for other URLs than specified below.
686 EXPECT_CALL(*sb_service_->mock_database_manager(), 685 EXPECT_CALL(*sb_service_->mock_database_manager(),
687 MatchDownloadWhitelistUrl(_)).Times(0); 686 MatchDownloadWhitelistUrl(_)).Times(0);
688 EXPECT_CALL(*sb_service_->mock_database_manager(), 687 EXPECT_CALL(*sb_service_->mock_database_manager(),
689 MatchDownloadWhitelistUrl(GURL("http://www.evil.com/bla.exe"))) 688 MatchDownloadWhitelistUrl(GURL("http://www.evil.com/bla.exe")))
690 .WillRepeatedly(Return(false)); 689 .WillRepeatedly(Return(false));
691 EXPECT_CALL(*sb_service_->mock_database_manager(), 690 EXPECT_CALL(*sb_service_->mock_database_manager(),
692 MatchDownloadWhitelistUrl(GURL("http://www.google.com/a.exe"))) 691 MatchDownloadWhitelistUrl(GURL("http://www.google.com/a.exe")))
693 .WillRepeatedly(Return(true)); 692 .WillRepeatedly(Return(true));
694
695 // With no referrer and just the bad url, should be marked DANGEROUS.
696 url_chain_.push_back(GURL("http://www.evil.com/bla.exe"));
697 // Set sample rate to 0 to prevent sampling. 693 // Set sample rate to 0 to prevent sampling.
698 SetWhitelistedDownloadSampleRate(0); 694 SetWhitelistedDownloadSampleRate(0);
699 download_service_->CheckClientDownload(
700 &item,
701 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
702 base::Unretained(this)));
703 base::RunLoop().Run();
704 695
705 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 696 {
706 EXPECT_TRUE(HasClientDownloadRequest()); 697 // With no referrer and just the bad url, should be marked DANGEROUS.
707 EXPECT_FALSE(GetClientDownloadRequest()->skipped_url_whitelist()); 698 url_chain_.push_back(GURL("http://www.evil.com/bla.exe"));
708 EXPECT_FALSE(GetClientDownloadRequest()->skipped_certificate_whitelist()); 699 RunLoop run_loop;
709 ClearClientDownloadRequest(); 700 download_service_->CheckClientDownload(
701 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
702 base::Unretained(this), run_loop.QuitClosure()));
703 run_loop.Run();
710 704
711 // Check that the referrer is not matched against the whitelist. 705 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
712 referrer_ = GURL("http://www.google.com/"); 706 EXPECT_TRUE(HasClientDownloadRequest());
713 download_service_->CheckClientDownload( 707 EXPECT_FALSE(GetClientDownloadRequest()->skipped_url_whitelist());
714 &item, 708 EXPECT_FALSE(GetClientDownloadRequest()->skipped_certificate_whitelist());
715 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 709 ClearClientDownloadRequest();
716 base::Unretained(this))); 710 }
717 base::RunLoop().Run();
718 711
719 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 712 {
720 EXPECT_TRUE(HasClientDownloadRequest()); 713 // Check that the referrer is not matched against the whitelist.
721 EXPECT_FALSE(GetClientDownloadRequest()->skipped_url_whitelist()); 714 referrer_ = GURL("http://www.google.com/");
722 EXPECT_FALSE(GetClientDownloadRequest()->skipped_certificate_whitelist()); 715 RunLoop run_loop;
723 ClearClientDownloadRequest(); 716 download_service_->CheckClientDownload(
717 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
718 base::Unretained(this), run_loop.QuitClosure()));
719 run_loop.Run();
724 720
725 // Redirect from a site shouldn't be checked either. 721 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
726 url_chain_.insert(url_chain_.begin(), GURL("http://www.google.com/redirect")); 722 EXPECT_TRUE(HasClientDownloadRequest());
727 download_service_->CheckClientDownload( 723 EXPECT_FALSE(GetClientDownloadRequest()->skipped_url_whitelist());
728 &item, 724 EXPECT_FALSE(GetClientDownloadRequest()->skipped_certificate_whitelist());
729 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 725 ClearClientDownloadRequest();
730 base::Unretained(this))); 726 }
731 base::RunLoop().Run();
732 727
733 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 728 {
734 EXPECT_TRUE(HasClientDownloadRequest()); 729 // Redirect from a site shouldn't be checked either.
735 EXPECT_FALSE(GetClientDownloadRequest()->skipped_url_whitelist()); 730 url_chain_.insert(url_chain_.begin(),
736 EXPECT_FALSE(GetClientDownloadRequest()->skipped_certificate_whitelist()); 731 GURL("http://www.google.com/redirect"));
737 ClearClientDownloadRequest(); 732 RunLoop run_loop;
733 download_service_->CheckClientDownload(
734 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
735 base::Unretained(this), run_loop.QuitClosure()));
736 run_loop.Run();
738 737
739 // Only if the final url is whitelisted should it be SAFE. 738 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
740 url_chain_.push_back(GURL("http://www.google.com/a.exe")); 739 EXPECT_TRUE(HasClientDownloadRequest());
741 download_service_->CheckClientDownload( 740 EXPECT_FALSE(GetClientDownloadRequest()->skipped_url_whitelist());
742 &item, 741 EXPECT_FALSE(GetClientDownloadRequest()->skipped_certificate_whitelist());
743 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 742 ClearClientDownloadRequest();
744 base::Unretained(this))); 743 }
745 base::RunLoop().Run(); 744
746 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 745 {
747 // TODO(grt): Make the service produce the request even when the URL is 746 // Only if the final url is whitelisted should it be SAFE.
748 // whitelisted. 747 url_chain_.push_back(GURL("http://www.google.com/a.exe"));
749 EXPECT_FALSE(HasClientDownloadRequest()); 748 RunLoop run_loop;
749 download_service_->CheckClientDownload(
750 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
751 base::Unretained(this), run_loop.QuitClosure()));
752 run_loop.Run();
753 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
754 // TODO(grt): Make the service produce the request even when the URL is
755 // whitelisted.
756 EXPECT_FALSE(HasClientDownloadRequest());
757 }
750 } 758 }
751 759
752 TEST_F(DownloadProtectionServiceTest, 760 TEST_F(DownloadProtectionServiceTest,
753 CheckClientDownloadWhitelistedUrlWithSampling) { 761 CheckClientDownloadWhitelistedUrlWithSampling) {
754 // Server responses "SAFE" to every requests coming from whitelisted 762 // Server responses "SAFE" to every requests coming from whitelisted
755 // download. 763 // download.
756 net::FakeURLFetcherFactory factory(NULL); 764 net::FakeURLFetcherFactory factory(NULL);
757 PrepareResponse( 765 PrepareResponse(
758 &factory, ClientDownloadResponse::SAFE, net::HTTP_OK, 766 &factory, ClientDownloadResponse::SAFE, net::HTTP_OK,
759 net::URLRequestStatus::SUCCESS); 767 net::URLRequestStatus::SUCCESS);
(...skipping 14 matching lines...) Expand all
774 // Assume http://www.whitelist.com/a.exe is on the whitelist. 782 // Assume http://www.whitelist.com/a.exe is on the whitelist.
775 EXPECT_CALL(*sb_service_->mock_database_manager(), 783 EXPECT_CALL(*sb_service_->mock_database_manager(),
776 MatchDownloadWhitelistUrl(_)).Times(0); 784 MatchDownloadWhitelistUrl(_)).Times(0);
777 EXPECT_CALL(*sb_service_->mock_database_manager(), 785 EXPECT_CALL(*sb_service_->mock_database_manager(),
778 MatchDownloadWhitelistUrl(GURL("http://www.whitelist.com/a.exe"))) 786 MatchDownloadWhitelistUrl(GURL("http://www.whitelist.com/a.exe")))
779 .WillRepeatedly(Return(true)); 787 .WillRepeatedly(Return(true));
780 url_chain_.push_back(GURL("http://www.whitelist.com/a.exe")); 788 url_chain_.push_back(GURL("http://www.whitelist.com/a.exe"));
781 // Set sample rate to 1.00, so download_service_ will always send download 789 // Set sample rate to 1.00, so download_service_ will always send download
782 // pings for whitelisted downloads. 790 // pings for whitelisted downloads.
783 SetWhitelistedDownloadSampleRate(1.00); 791 SetWhitelistedDownloadSampleRate(1.00);
784 // Case (1): is_extended_reporting && is_incognito.
785 // ClientDownloadRequest should NOT be sent.
786 SetExtendedReportingPreference(true);
787 EXPECT_CALL(item, GetBrowserContext())
788 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile()));
789 download_service_->CheckClientDownload(
790 &item,
791 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
792 base::Unretained(this)));
793 base::RunLoop().Run();
794 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
795 EXPECT_FALSE(HasClientDownloadRequest());
796 792
797 // Case (2): is_extended_reporting && !is_incognito. 793 {
798 // ClientDownloadRequest should be sent. 794 // Case (1): is_extended_reporting && is_incognito.
799 EXPECT_CALL(item, GetBrowserContext()) 795 // ClientDownloadRequest should NOT be sent.
800 .WillRepeatedly(Return(profile_.get())); 796 SetExtendedReportingPreference(true);
801 download_service_->CheckClientDownload( 797 EXPECT_CALL(item, GetBrowserContext())
802 &item, 798 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile()));
803 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 799 RunLoop run_loop;
804 base::Unretained(this))); 800 download_service_->CheckClientDownload(
805 base::RunLoop().Run(); 801 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
806 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 802 base::Unretained(this), run_loop.QuitClosure()));
807 EXPECT_TRUE(HasClientDownloadRequest()); 803 run_loop.Run();
808 EXPECT_TRUE(GetClientDownloadRequest()->skipped_url_whitelist()); 804 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
809 ClearClientDownloadRequest(); 805 EXPECT_FALSE(HasClientDownloadRequest());
806 }
810 807
811 // Case (3): !is_extended_reporting && is_incognito. 808 {
812 // ClientDownloadRequest should NOT be sent. 809 // Case (2): is_extended_reporting && !is_incognito.
813 SetExtendedReportingPreference(false); 810 // ClientDownloadRequest should be sent.
814 EXPECT_CALL(item, GetBrowserContext()) 811 EXPECT_CALL(item, GetBrowserContext())
815 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile())); 812 .WillRepeatedly(Return(profile_.get()));
816 download_service_->CheckClientDownload( 813 RunLoop run_loop;
817 &item, 814 download_service_->CheckClientDownload(
818 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 815 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
819 base::Unretained(this))); 816 base::Unretained(this), run_loop.QuitClosure()));
820 base::RunLoop().Run(); 817 run_loop.Run();
821 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 818 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
822 EXPECT_FALSE(HasClientDownloadRequest()); 819 EXPECT_TRUE(HasClientDownloadRequest());
820 EXPECT_TRUE(GetClientDownloadRequest()->skipped_url_whitelist());
821 ClearClientDownloadRequest();
822 }
823 823
824 // Case (4): !is_extended_reporting && !is_incognito. 824 {
825 // ClientDownloadRequest should NOT be sent. 825 // Case (3): !is_extended_reporting && is_incognito.
826 EXPECT_CALL(item, GetBrowserContext()) 826 // ClientDownloadRequest should NOT be sent.
827 .WillRepeatedly(Return(profile_.get())); 827 SetExtendedReportingPreference(false);
828 download_service_->CheckClientDownload( 828 EXPECT_CALL(item, GetBrowserContext())
829 &item, 829 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile()));
830 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 830 RunLoop run_loop;
831 base::Unretained(this))); 831 download_service_->CheckClientDownload(
832 base::RunLoop().Run(); 832 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
833 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 833 base::Unretained(this), run_loop.QuitClosure()));
834 EXPECT_FALSE(HasClientDownloadRequest()); 834 run_loop.Run();
835 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
836 EXPECT_FALSE(HasClientDownloadRequest());
837 }
838
839 {
840 // Case (4): !is_extended_reporting && !is_incognito.
841 // ClientDownloadRequest should NOT be sent.
842 EXPECT_CALL(item, GetBrowserContext())
843 .WillRepeatedly(Return(profile_.get()));
844 RunLoop run_loop;
845 download_service_->CheckClientDownload(
846 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
847 base::Unretained(this), run_loop.QuitClosure()));
848 run_loop.Run();
849 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
850 EXPECT_FALSE(HasClientDownloadRequest());
851 }
835 } 852 }
836 853
837 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSampledFile) { 854 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSampledFile) {
838 // Server response will be discarded. 855 // Server response will be discarded.
839 net::FakeURLFetcherFactory factory(NULL); 856 net::FakeURLFetcherFactory factory(NULL);
840 PrepareResponse( 857 PrepareResponse(
841 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, 858 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
842 net::URLRequestStatus::SUCCESS); 859 net::URLRequestStatus::SUCCESS);
843 860
844 content::MockDownloadItem item; 861 content::MockDownloadItem item;
845 PrepareBasicDownloadItem( 862 PrepareBasicDownloadItem(
846 &item, 863 &item,
847 std::vector<std::string>(), // empty url_chain 864 std::vector<std::string>(), // empty url_chain
848 "http://www.google.com/", // referrer 865 "http://www.google.com/", // referrer
849 FILE_PATH_LITERAL("a.tmp"), // tmp_path 866 FILE_PATH_LITERAL("a.tmp"), // tmp_path
850 FILE_PATH_LITERAL("a.foobar_unknown_ype")); // final_path 867 FILE_PATH_LITERAL("a.foobar_unknown_ype")); // final_path
851 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)) 868 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _))
852 .Times(1); 869 .Times(1);
853 EXPECT_CALL(*binary_feature_extractor_.get(), 870 EXPECT_CALL(*binary_feature_extractor_.get(),
854 ExtractImageFeatures( 871 ExtractImageFeatures(
855 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _)) 872 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _))
856 .Times(1); 873 .Times(1);
857 url_chain_.push_back(GURL("http://www.whitelist.com/a.foobar_unknown_type")); 874 url_chain_.push_back(GURL("http://www.whitelist.com/a.foobar_unknown_type"));
858 875
859 // Set ping sample rate to 1.00 so download_service_ will always send a 876 // Set ping sample rate to 1.00 so download_service_ will always send a
860 // "light" ping for unknown types if allowed. 877 // "light" ping for unknown types if allowed.
861 SetBinarySamplingProbability(1.0); 878 SetBinarySamplingProbability(1.0);
879 {
880 // Case (1): is_extended_reporting && is_incognito.
881 // ClientDownloadRequest should NOT be sent.
882 SetExtendedReportingPreference(true);
883 EXPECT_CALL(item, GetBrowserContext())
884 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile()));
885 RunLoop run_loop;
886 download_service_->CheckClientDownload(
887 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
888 base::Unretained(this), run_loop.QuitClosure()));
889 run_loop.Run();
890 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
891 EXPECT_FALSE(HasClientDownloadRequest());
892 }
862 893
863 // Case (1): is_extended_reporting && is_incognito. 894 {
864 // ClientDownloadRequest should NOT be sent. 895 // Case (2): is_extended_reporting && !is_incognito.
865 SetExtendedReportingPreference(true); 896 // A "light" ClientDownloadRequest should be sent.
866 EXPECT_CALL(item, GetBrowserContext()) 897 EXPECT_CALL(item, GetBrowserContext())
867 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile())); 898 .WillRepeatedly(Return(profile_.get()));
868 download_service_->CheckClientDownload( 899 RunLoop run_loop;
869 &item, 900 download_service_->CheckClientDownload(
870 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 901 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
871 base::Unretained(this))); 902 base::Unretained(this), run_loop.QuitClosure()));
872 base::RunLoop().Run(); 903 run_loop.Run();
873 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 904 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
874 EXPECT_FALSE(HasClientDownloadRequest()); 905 EXPECT_TRUE(HasClientDownloadRequest());
875 906 // Verify it's a "light" ping, check that URLs don't have paths, and
876 // Case (2): is_extended_reporting && !is_incognito. 907 // and verify filename is just an extension.
877 // A "light" ClientDownloadRequest should be sent. 908 auto* req = GetClientDownloadRequest();
878 EXPECT_CALL(item, GetBrowserContext()) 909 EXPECT_EQ(ClientDownloadRequest::SAMPLED_UNSUPPORTED_FILE,
879 .WillRepeatedly(Return(profile_.get())); 910 req->download_type());
880 download_service_->CheckClientDownload( 911 EXPECT_EQ(GURL(req->url()).GetOrigin().spec(), req->url());
881 &item, 912 for (auto resource : req->resources()) {
882 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 913 EXPECT_EQ(GURL(resource.url()).GetOrigin().spec(), resource.url());
883 base::Unretained(this))); 914 EXPECT_EQ(GURL(resource.referrer()).GetOrigin().spec(),
884 base::RunLoop().Run(); 915 resource.referrer());
885 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 916 }
886 EXPECT_TRUE(HasClientDownloadRequest()); 917 EXPECT_EQ('.', req->file_basename()[0]);
887 // Verify it's a "light" ping, check that URLs don't have paths, and
888 // and verify filename is just an extension.
889 auto* req = GetClientDownloadRequest();
890 EXPECT_EQ(ClientDownloadRequest::SAMPLED_UNSUPPORTED_FILE,
891 req->download_type());
892 EXPECT_EQ(GURL(req->url()).GetOrigin().spec(), req->url());
893 for (auto resource : req->resources()) {
894 EXPECT_EQ(GURL(resource.url()).GetOrigin().spec(), resource.url());
895 EXPECT_EQ(GURL(resource.referrer()).GetOrigin().spec(),
896 resource.referrer());
897 } 918 }
898 EXPECT_EQ('.', req->file_basename()[0]);
899 919
900 ClearClientDownloadRequest(); 920 ClearClientDownloadRequest();
901 921
902 // Case (3): !is_extended_reporting && is_incognito. 922 {
903 // ClientDownloadRequest should NOT be sent. 923 // Case (3): !is_extended_reporting && is_incognito.
904 SetExtendedReportingPreference(false); 924 // ClientDownloadRequest should NOT be sent.
905 EXPECT_CALL(item, GetBrowserContext()) 925 SetExtendedReportingPreference(false);
906 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile())); 926 EXPECT_CALL(item, GetBrowserContext())
907 download_service_->CheckClientDownload( 927 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile()));
908 &item, 928 RunLoop run_loop;
909 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 929 download_service_->CheckClientDownload(
910 base::Unretained(this))); 930 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
911 base::RunLoop().Run(); 931 base::Unretained(this), run_loop.QuitClosure()));
912 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 932 run_loop.Run();
913 EXPECT_FALSE(HasClientDownloadRequest()); 933 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
934 EXPECT_FALSE(HasClientDownloadRequest());
935 }
914 936
915 // Case (4): !is_extended_reporting && !is_incognito. 937 {
916 // ClientDownloadRequest should NOT be sent. 938 // Case (4): !is_extended_reporting && !is_incognito.
917 EXPECT_CALL(item, GetBrowserContext()) 939 // ClientDownloadRequest should NOT be sent.
918 .WillRepeatedly(Return(profile_.get())); 940 EXPECT_CALL(item, GetBrowserContext())
919 download_service_->CheckClientDownload( 941 .WillRepeatedly(Return(profile_.get()));
920 &item, 942 RunLoop run_loop;
921 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 943 download_service_->CheckClientDownload(
922 base::Unretained(this))); 944 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
923 base::RunLoop().Run(); 945 base::Unretained(this), run_loop.QuitClosure()));
924 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 946 run_loop.Run();
925 EXPECT_FALSE(HasClientDownloadRequest()); 947 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
948 EXPECT_FALSE(HasClientDownloadRequest());
949 }
926 } 950 }
927 951
928 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) { 952 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) {
929 // HTTP request will fail. 953 // HTTP request will fail.
930 net::FakeURLFetcherFactory factory(NULL); 954 net::FakeURLFetcherFactory factory(NULL);
931 PrepareResponse( 955 PrepareResponse(
932 &factory, ClientDownloadResponse::SAFE, net::HTTP_INTERNAL_SERVER_ERROR, 956 &factory, ClientDownloadResponse::SAFE, net::HTTP_INTERNAL_SERVER_ERROR,
933 net::URLRequestStatus::FAILED); 957 net::URLRequestStatus::FAILED);
934 958
935 content::MockDownloadItem item; 959 content::MockDownloadItem item;
936 PrepareBasicDownloadItem( 960 PrepareBasicDownloadItem(
937 &item, 961 &item,
938 {"http://www.evil.com/a.exe"}, // url_chain 962 {"http://www.evil.com/a.exe"}, // url_chain
939 "http://www.google.com/", // referrer 963 "http://www.google.com/", // referrer
940 FILE_PATH_LITERAL("a.tmp"), // tmp_path 964 FILE_PATH_LITERAL("a.tmp"), // tmp_path
941 FILE_PATH_LITERAL("a.exe")); // final_path 965 FILE_PATH_LITERAL("a.exe")); // final_path
942 966
943 EXPECT_CALL(*sb_service_->mock_database_manager(), 967 EXPECT_CALL(*sb_service_->mock_database_manager(),
944 MatchDownloadWhitelistUrl(_)) 968 MatchDownloadWhitelistUrl(_))
945 .WillRepeatedly(Return(false)); 969 .WillRepeatedly(Return(false));
946 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)); 970 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _));
947 EXPECT_CALL( 971 EXPECT_CALL(
948 *binary_feature_extractor_.get(), 972 *binary_feature_extractor_.get(),
949 ExtractImageFeatures(tmp_path_, BinaryFeatureExtractor::kDefaultOptions, 973 ExtractImageFeatures(tmp_path_, BinaryFeatureExtractor::kDefaultOptions,
950 _, _)); 974 _, _));
951 975
976 RunLoop run_loop;
952 download_service_->CheckClientDownload( 977 download_service_->CheckClientDownload(
953 &item, 978 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
954 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 979 base::Unretained(this), run_loop.QuitClosure()));
955 base::Unretained(this))); 980 run_loop.Run();
956 base::RunLoop().Run();
957 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 981 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
958 } 982 }
959 983
960 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSuccess) { 984 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSuccess) {
961 net::FakeURLFetcherFactory factory(NULL); 985 net::FakeURLFetcherFactory factory(NULL);
962 PrepareResponse( 986 PrepareResponse(
963 &factory, ClientDownloadResponse::SAFE, net::HTTP_OK, 987 &factory, ClientDownloadResponse::SAFE, net::HTTP_OK,
964 net::URLRequestStatus::SUCCESS); 988 net::URLRequestStatus::SUCCESS);
965 989
966 content::MockDownloadItem item; 990 content::MockDownloadItem item;
967 PrepareBasicDownloadItem( 991 PrepareBasicDownloadItem(
968 &item, 992 &item,
969 {"http://www.evil.com/a.exe"}, // url_chain 993 {"http://www.evil.com/a.exe"}, // url_chain
970 "http://www.google.com/", // referrer 994 "http://www.google.com/", // referrer
971 FILE_PATH_LITERAL("a.tmp"), // tmp_path 995 FILE_PATH_LITERAL("a.tmp"), // tmp_path
972 FILE_PATH_LITERAL("a.exe")); // final_path 996 FILE_PATH_LITERAL("a.exe")); // final_path
973 997
974 EXPECT_CALL(*sb_service_->mock_database_manager(), 998 EXPECT_CALL(*sb_service_->mock_database_manager(),
975 MatchDownloadWhitelistUrl(_)) 999 MatchDownloadWhitelistUrl(_))
976 .WillRepeatedly(Return(false)); 1000 .WillRepeatedly(Return(false));
977 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)) 1001 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _))
978 .Times(7); 1002 .Times(7);
979 EXPECT_CALL(*binary_feature_extractor_.get(), 1003 EXPECT_CALL(*binary_feature_extractor_.get(),
980 ExtractImageFeatures( 1004 ExtractImageFeatures(
981 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _)) 1005 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _))
982 .Times(7); 1006 .Times(7);
983
984 download_service_->CheckClientDownload(
985 &item,
986 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
987 base::Unretained(this)));
988 base::RunLoop().Run();
989
990 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
991 EXPECT_TRUE(HasClientDownloadRequest());
992 ClearClientDownloadRequest();
993
994 // Invalid response should result in UNKNOWN.
995 ClientDownloadResponse invalid_response;
996 factory.SetFakeResponse(
997 DownloadProtectionService::GetDownloadRequestUrl(),
998 invalid_response.SerializePartialAsString(),
999 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1000
1001 download_service_->CheckClientDownload(
1002 &item,
1003 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1004 base::Unretained(this)));
1005 base::RunLoop().Run();
1006 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
1007 EXPECT_TRUE(HasClientDownloadRequest());
1008 ClearClientDownloadRequest();
1009 std::string feedback_ping; 1007 std::string feedback_ping;
1010 std::string feedback_response; 1008 std::string feedback_response;
1011 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting( 1009 ClientDownloadResponse expected_response;
1012 item, &feedback_ping, &feedback_response));
1013 1010
1014 // If the response is dangerous the result should also be marked as dangerous. 1011 {
1015 PrepareResponse( 1012 RunLoop run_loop;
1016 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, 1013 download_service_->CheckClientDownload(
1017 net::URLRequestStatus::SUCCESS); 1014 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1015 base::Unretained(this), run_loop.QuitClosure()));
1016 run_loop.Run();
1018 1017
1019 download_service_->CheckClientDownload( 1018 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
1020 &item, 1019 EXPECT_TRUE(HasClientDownloadRequest());
1021 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1020 ClearClientDownloadRequest();
1022 base::Unretained(this))); 1021 }
1023 base::RunLoop().Run();
1024 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting(
1025 item, &feedback_ping, &feedback_response));
1026 1022
1027 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 1023 {
1028 EXPECT_TRUE(HasClientDownloadRequest()); 1024 // Invalid response should result in UNKNOWN.
1029 ClearClientDownloadRequest(); 1025 ClientDownloadResponse invalid_response;
1026 factory.SetFakeResponse(DownloadProtectionService::GetDownloadRequestUrl(),
1027 invalid_response.SerializePartialAsString(),
1028 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1030 1029
1031 // If the response is uncommon the result should also be marked as uncommon. 1030 RunLoop run_loop;
1032 PrepareResponse( 1031 download_service_->CheckClientDownload(
1033 &factory, ClientDownloadResponse::UNCOMMON, net::HTTP_OK, 1032 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1034 net::URLRequestStatus::SUCCESS); 1033 base::Unretained(this), run_loop.QuitClosure()));
1034 run_loop.Run();
1035 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
1036 EXPECT_TRUE(HasClientDownloadRequest());
1037 ClearClientDownloadRequest();
1035 1038
1036 download_service_->CheckClientDownload( 1039 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting(
1037 &item, 1040 item, &feedback_ping, &feedback_response));
1038 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1041 }
1039 base::Unretained(this)));
1040 base::RunLoop().Run();
1041 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON));
1042 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting(
1043 item, &feedback_ping, &feedback_response));
1044 ClientDownloadRequest decoded_request;
1045 EXPECT_TRUE(decoded_request.ParseFromString(feedback_ping));
1046 EXPECT_EQ(url_chain_.back().spec(), decoded_request.url());
1047 ClientDownloadResponse expected_response;
1048 expected_response.set_verdict(ClientDownloadResponse::UNCOMMON);
1049 EXPECT_EQ(expected_response.SerializeAsString(), feedback_response);
1050 EXPECT_TRUE(HasClientDownloadRequest());
1051 ClearClientDownloadRequest();
1052 1042
1053 // If the response is dangerous_host the result should also be marked as 1043 {
1054 // dangerous_host. 1044 // If the response is dangerous the result should also be marked as
1055 PrepareResponse( 1045 // dangerous.
1056 &factory, ClientDownloadResponse::DANGEROUS_HOST, net::HTTP_OK, 1046 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
1057 net::URLRequestStatus::SUCCESS); 1047 net::URLRequestStatus::SUCCESS);
1048 RunLoop run_loop;
1049 download_service_->CheckClientDownload(
1050 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1051 base::Unretained(this), run_loop.QuitClosure()));
1052 run_loop.Run();
1053 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting(
1054 item, &feedback_ping, &feedback_response));
1058 1055
1059 download_service_->CheckClientDownload( 1056 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
1060 &item, 1057 EXPECT_TRUE(HasClientDownloadRequest());
1061 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1058 ClearClientDownloadRequest();
1062 base::Unretained(this))); 1059 }
1063 base::RunLoop().Run();
1064 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS_HOST));
1065 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting(
1066 item, &feedback_ping, &feedback_response));
1067 expected_response.set_verdict(ClientDownloadResponse::DANGEROUS_HOST);
1068 EXPECT_EQ(expected_response.SerializeAsString(), feedback_response);
1069 EXPECT_TRUE(HasClientDownloadRequest());
1070 ClearClientDownloadRequest();
1071 1060
1072 // If the response is POTENTIALLY_UNWANTED the result should also be marked as 1061 {
1073 // POTENTIALLY_UNWANTED. 1062 // If the response is uncommon the result should also be marked as uncommon.
1074 PrepareResponse( 1063 PrepareResponse(&factory, ClientDownloadResponse::UNCOMMON, net::HTTP_OK,
1075 &factory, ClientDownloadResponse::POTENTIALLY_UNWANTED, net::HTTP_OK, 1064 net::URLRequestStatus::SUCCESS);
1076 net::URLRequestStatus::SUCCESS);
1077 1065
1078 download_service_->CheckClientDownload( 1066 RunLoop run_loop;
1079 &item, 1067 download_service_->CheckClientDownload(
1080 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1068 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1081 base::Unretained(this))); 1069 base::Unretained(this), run_loop.QuitClosure()));
1082 base::RunLoop().Run(); 1070 run_loop.Run();
1071 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON));
1072 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting(
1073 item, &feedback_ping, &feedback_response));
1074 ClientDownloadRequest decoded_request;
1075 EXPECT_TRUE(decoded_request.ParseFromString(feedback_ping));
1076 EXPECT_EQ(url_chain_.back().spec(), decoded_request.url());
1077 expected_response.set_verdict(ClientDownloadResponse::UNCOMMON);
1078 EXPECT_EQ(expected_response.SerializeAsString(), feedback_response);
1079 EXPECT_TRUE(HasClientDownloadRequest());
1080 ClearClientDownloadRequest();
1081 }
1083 1082
1084 EXPECT_TRUE(IsResult(DownloadProtectionService::POTENTIALLY_UNWANTED)); 1083 {
1085 EXPECT_TRUE(HasClientDownloadRequest()); 1084 // If the response is dangerous_host the result should also be marked as
1086 ClearClientDownloadRequest(); 1085 // dangerous_host.
1086 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS_HOST,
1087 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1088 RunLoop run_loop;
1089 download_service_->CheckClientDownload(
1090 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1091 base::Unretained(this), run_loop.QuitClosure()));
1092 run_loop.Run();
1093 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS_HOST));
1094 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting(
1095 item, &feedback_ping, &feedback_response));
1096 expected_response.set_verdict(ClientDownloadResponse::DANGEROUS_HOST);
1097 EXPECT_EQ(expected_response.SerializeAsString(), feedback_response);
1098 EXPECT_TRUE(HasClientDownloadRequest());
1099 ClearClientDownloadRequest();
1100 }
1087 1101
1088 // If the response is UNKNOWN the result should also be marked as 1102 {
1089 // UNKNOWN 1103 // If the response is POTENTIALLY_UNWANTED the result should also be marked
1090 PrepareResponse( 1104 // as
1091 &factory, ClientDownloadResponse::UNKNOWN, net::HTTP_OK, 1105 // POTENTIALLY_UNWANTED.
1092 net::URLRequestStatus::SUCCESS); 1106 PrepareResponse(&factory, ClientDownloadResponse::POTENTIALLY_UNWANTED,
1107 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1093 1108
1094 download_service_->CheckClientDownload( 1109 RunLoop run_loop;
1095 &item, 1110 download_service_->CheckClientDownload(
1096 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1111 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1097 base::Unretained(this))); 1112 base::Unretained(this), run_loop.QuitClosure()));
1098 base::RunLoop().Run(); 1113 run_loop.Run();
1099 1114
1100 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 1115 EXPECT_TRUE(IsResult(DownloadProtectionService::POTENTIALLY_UNWANTED));
1101 EXPECT_TRUE(HasClientDownloadRequest()); 1116 EXPECT_TRUE(HasClientDownloadRequest());
1102 ClearClientDownloadRequest(); 1117 ClearClientDownloadRequest();
1118 }
1119
1120 {
1121 // If the response is UNKNOWN the result should also be marked as
1122 // UNKNOWN
1123 PrepareResponse(&factory, ClientDownloadResponse::UNKNOWN, net::HTTP_OK,
1124 net::URLRequestStatus::SUCCESS);
1125
1126 RunLoop run_loop;
1127 download_service_->CheckClientDownload(
1128 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1129 base::Unretained(this), run_loop.QuitClosure()));
1130 run_loop.Run();
1131
1132 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
1133 EXPECT_TRUE(HasClientDownloadRequest());
1134 ClearClientDownloadRequest();
1135 }
1103 } 1136 }
1104 1137
1105 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadHTTPS) { 1138 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadHTTPS) {
1106 net::FakeURLFetcherFactory factory(NULL); 1139 net::FakeURLFetcherFactory factory(NULL);
1107 PrepareResponse( 1140 PrepareResponse(
1108 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, 1141 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
1109 net::URLRequestStatus::SUCCESS); 1142 net::URLRequestStatus::SUCCESS);
1110 1143
1111 content::MockDownloadItem item; 1144 content::MockDownloadItem item;
1112 PrepareBasicDownloadItem(&item, 1145 PrepareBasicDownloadItem(&item,
1113 {"http://www.evil.com/a.exe"}, // url_chain 1146 {"http://www.evil.com/a.exe"}, // url_chain
1114 "http://www.google.com/", // referrer 1147 "http://www.google.com/", // referrer
1115 FILE_PATH_LITERAL("a.tmp"), // tmp_path 1148 FILE_PATH_LITERAL("a.tmp"), // tmp_path
1116 FILE_PATH_LITERAL("a.exe")); // final_path 1149 FILE_PATH_LITERAL("a.exe")); // final_path
1117 1150
1118 EXPECT_CALL(*sb_service_->mock_database_manager(), 1151 EXPECT_CALL(*sb_service_->mock_database_manager(),
1119 MatchDownloadWhitelistUrl(_)) 1152 MatchDownloadWhitelistUrl(_))
1120 .WillRepeatedly(Return(false)); 1153 .WillRepeatedly(Return(false));
1121 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)) 1154 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _))
1122 .Times(1); 1155 .Times(1);
1123 EXPECT_CALL(*binary_feature_extractor_.get(), 1156 EXPECT_CALL(*binary_feature_extractor_.get(),
1124 ExtractImageFeatures( 1157 ExtractImageFeatures(
1125 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _)) 1158 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _))
1126 .Times(1); 1159 .Times(1);
1127 1160
1161 RunLoop run_loop;
1128 download_service_->CheckClientDownload( 1162 download_service_->CheckClientDownload(
1129 &item, 1163 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1130 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1164 base::Unretained(this), run_loop.QuitClosure()));
1131 base::Unretained(this))); 1165 run_loop.Run();
1132 base::RunLoop().Run();
1133 1166
1134 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 1167 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
1135 EXPECT_TRUE(HasClientDownloadRequest()); 1168 EXPECT_TRUE(HasClientDownloadRequest());
1136 ClearClientDownloadRequest(); 1169 ClearClientDownloadRequest();
1137 } 1170 }
1138 1171
1139 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadBlob) { 1172 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadBlob) {
1140 net::FakeURLFetcherFactory factory(NULL); 1173 net::FakeURLFetcherFactory factory(NULL);
1141 PrepareResponse( 1174 PrepareResponse(
1142 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, 1175 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
1143 net::URLRequestStatus::SUCCESS); 1176 net::URLRequestStatus::SUCCESS);
1144 1177
1145 content::MockDownloadItem item; 1178 content::MockDownloadItem item;
1146 PrepareBasicDownloadItem( 1179 PrepareBasicDownloadItem(
1147 &item, 1180 &item,
1148 {"blob:http://www.evil.com/50b85f60-71e4-11e4-82f8-0800200c9a66"}, 1181 {"blob:http://www.evil.com/50b85f60-71e4-11e4-82f8-0800200c9a66"},
1149 "http://www.google.com/", // referrer 1182 "http://www.google.com/", // referrer
1150 FILE_PATH_LITERAL("a.tmp"), // tmp_path 1183 FILE_PATH_LITERAL("a.tmp"), // tmp_path
1151 FILE_PATH_LITERAL("a.exe")); // final_path 1184 FILE_PATH_LITERAL("a.exe")); // final_path
1152 1185
1153 EXPECT_CALL(*sb_service_->mock_database_manager(), 1186 EXPECT_CALL(*sb_service_->mock_database_manager(),
1154 MatchDownloadWhitelistUrl(_)).WillRepeatedly(Return(false)); 1187 MatchDownloadWhitelistUrl(_)).WillRepeatedly(Return(false));
1155 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)) 1188 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _))
1156 .Times(1); 1189 .Times(1);
1157 EXPECT_CALL(*binary_feature_extractor_.get(), 1190 EXPECT_CALL(*binary_feature_extractor_.get(),
1158 ExtractImageFeatures( 1191 ExtractImageFeatures(
1159 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _)) 1192 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _))
1160 .Times(1); 1193 .Times(1);
1161 1194
1195 RunLoop run_loop;
1162 download_service_->CheckClientDownload( 1196 download_service_->CheckClientDownload(
1163 &item, 1197 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1164 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1198 base::Unretained(this), run_loop.QuitClosure()));
1165 base::Unretained(this))); 1199 run_loop.Run();
1166 base::RunLoop().Run();
1167 1200
1168 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 1201 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
1169 EXPECT_TRUE(HasClientDownloadRequest()); 1202 EXPECT_TRUE(HasClientDownloadRequest());
1170 ClearClientDownloadRequest(); 1203 ClearClientDownloadRequest();
1171 } 1204 }
1172 1205
1173 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadData) { 1206 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadData) {
1174 net::FakeURLFetcherFactory factory(NULL); 1207 net::FakeURLFetcherFactory factory(NULL);
1175 PrepareResponse( 1208 PrepareResponse(
1176 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, 1209 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
(...skipping 10 matching lines...) Expand all
1187 1220
1188 EXPECT_CALL(*sb_service_->mock_database_manager(), 1221 EXPECT_CALL(*sb_service_->mock_database_manager(),
1189 MatchDownloadWhitelistUrl(_)).WillRepeatedly(Return(false)); 1222 MatchDownloadWhitelistUrl(_)).WillRepeatedly(Return(false));
1190 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)) 1223 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _))
1191 .Times(1); 1224 .Times(1);
1192 EXPECT_CALL(*binary_feature_extractor_.get(), 1225 EXPECT_CALL(*binary_feature_extractor_.get(),
1193 ExtractImageFeatures( 1226 ExtractImageFeatures(
1194 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _)) 1227 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _))
1195 .Times(1); 1228 .Times(1);
1196 1229
1230 RunLoop run_loop;
1197 download_service_->CheckClientDownload( 1231 download_service_->CheckClientDownload(
1198 &item, 1232 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1199 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1233 base::Unretained(this), run_loop.QuitClosure()));
1200 base::Unretained(this))); 1234 run_loop.Run();
1201 base::RunLoop().Run();
1202 1235
1203 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 1236 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
1204 ASSERT_TRUE(HasClientDownloadRequest()); 1237 ASSERT_TRUE(HasClientDownloadRequest());
1205 const ClientDownloadRequest& request = *GetClientDownloadRequest(); 1238 const ClientDownloadRequest& request = *GetClientDownloadRequest();
1206 const char kExpectedUrl[] = 1239 const char kExpectedUrl[] =
1207 "data:application/octet-stream:base64," 1240 "data:application/octet-stream:base64,"
1208 "ACBF6DFC6F907662F566CA0241DFE8690C48661F440BA1BBD0B86C582845CCC8"; 1241 "ACBF6DFC6F907662F566CA0241DFE8690C48661F440BA1BBD0B86C582845CCC8";
1209 const char kExpectedRedirect1[] = "data:text/html:base64,"; 1242 const char kExpectedRedirect1[] = "data:text/html:base64,";
1210 const char kExpectedRedirect2[] = 1243 const char kExpectedRedirect2[] =
1211 "data:text/html:base64," 1244 "data:text/html:base64,"
(...skipping 23 matching lines...) Expand all
1235 net::URLRequestStatus::SUCCESS); 1268 net::URLRequestStatus::SUCCESS);
1236 1269
1237 content::MockDownloadItem item; 1270 content::MockDownloadItem item;
1238 PrepareBasicDownloadItem( 1271 PrepareBasicDownloadItem(
1239 &item, 1272 &item,
1240 {"http://www.evil.com/a.zip"}, // url_chain 1273 {"http://www.evil.com/a.zip"}, // url_chain
1241 "http://www.google.com/", // referrer 1274 "http://www.google.com/", // referrer
1242 FILE_PATH_LITERAL("a.tmp"), // tmp_path 1275 FILE_PATH_LITERAL("a.tmp"), // tmp_path
1243 FILE_PATH_LITERAL("a.zip")); // final_path 1276 FILE_PATH_LITERAL("a.zip")); // final_path
1244 1277
1245 // Write out a zip archive to the temporary file. In this case, it 1278 // Write out a zip archive to the temporary file.
1246 // only contains a text file.
1247 base::ScopedTempDir zip_source_dir; 1279 base::ScopedTempDir zip_source_dir;
1248 ASSERT_TRUE(zip_source_dir.CreateUniqueTempDir()); 1280 ASSERT_TRUE(zip_source_dir.CreateUniqueTempDir());
1249 std::string file_contents = "dummy file"; 1281 std::string file_contents = "dummy file";
1250 ASSERT_EQ(static_cast<int>(file_contents.size()), base::WriteFile(
1251 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.txt")),
1252 file_contents.data(), file_contents.size()));
1253 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false));
1254 1282
1255 download_service_->CheckClientDownload( 1283 {
1256 &item, 1284 // In this case, it only contains a text file.
1257 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1285 ASSERT_EQ(static_cast<int>(file_contents.size()),
1258 base::Unretained(this))); 1286 base::WriteFile(
1259 base::RunLoop().Run(); 1287 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.txt")),
1260 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 1288 file_contents.data(), file_contents.size()));
1261 EXPECT_FALSE(HasClientDownloadRequest()); 1289 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false));
1262 Mock::VerifyAndClearExpectations(sb_service_.get());
1263 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
1264 1290
1265 // Now check with an executable in the zip file as well. 1291 RunLoop run_loop;
1266 ASSERT_EQ(static_cast<int>(file_contents.size()), base::WriteFile( 1292 download_service_->CheckClientDownload(
1267 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.exe")), 1293 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1268 file_contents.data(), file_contents.size())); 1294 base::Unretained(this), run_loop.QuitClosure()));
1269 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false)); 1295 run_loop.Run();
1296 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
1297 EXPECT_FALSE(HasClientDownloadRequest());
1298 Mock::VerifyAndClearExpectations(sb_service_.get());
1299 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
1300 }
1270 1301
1271 EXPECT_CALL(*sb_service_->mock_database_manager(), 1302 {
1272 MatchDownloadWhitelistUrl(_)) 1303 // Now check with an executable in the zip file as well.
1273 .WillRepeatedly(Return(false)); 1304 ASSERT_EQ(static_cast<int>(file_contents.size()),
1305 base::WriteFile(
1306 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.exe")),
1307 file_contents.data(), file_contents.size()));
1308 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false));
1274 1309
1275 download_service_->CheckClientDownload( 1310 EXPECT_CALL(*sb_service_->mock_database_manager(),
1276 &item, 1311 MatchDownloadWhitelistUrl(_))
1277 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1312 .WillRepeatedly(Return(false));
1278 base::Unretained(this)));
1279 base::RunLoop().Run();
1280 1313
1281 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 1314 RunLoop run_loop;
1282 EXPECT_TRUE(HasClientDownloadRequest()); 1315 download_service_->CheckClientDownload(
1283 const ClientDownloadRequest& request = *GetClientDownloadRequest(); 1316 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1284 EXPECT_TRUE(request.has_download_type()); 1317 base::Unretained(this), run_loop.QuitClosure()));
1285 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_EXECUTABLE, 1318 run_loop.Run();
1286 request.download_type());
1287 EXPECT_EQ(1, request.archived_binary_size());
1288 const ClientDownloadRequest_ArchivedBinary* archived_binary =
1289 GetRequestArchivedBinary(request, "file.exe");
1290 ASSERT_NE(nullptr, archived_binary);
1291 EXPECT_EQ(ClientDownloadRequest_DownloadType_WIN_EXECUTABLE,
1292 archived_binary->download_type());
1293 EXPECT_EQ(static_cast<int64_t>(file_contents.size()),
1294 archived_binary->length());
1295 ClearClientDownloadRequest();
1296 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
1297 1319
1298 // If the response is dangerous the result should also be marked as 1320 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
1299 // dangerous. 1321 EXPECT_TRUE(HasClientDownloadRequest());
1300 PrepareResponse( 1322 const ClientDownloadRequest& request = *GetClientDownloadRequest();
1301 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, 1323 EXPECT_TRUE(request.has_download_type());
1302 net::URLRequestStatus::SUCCESS); 1324 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_EXECUTABLE,
1325 request.download_type());
1326 EXPECT_EQ(1, request.archived_binary_size());
1327 const ClientDownloadRequest_ArchivedBinary* archived_binary =
1328 GetRequestArchivedBinary(request, "file.exe");
1329 ASSERT_NE(nullptr, archived_binary);
1330 EXPECT_EQ(ClientDownloadRequest_DownloadType_WIN_EXECUTABLE,
1331 archived_binary->download_type());
1332 EXPECT_EQ(static_cast<int64_t>(file_contents.size()),
1333 archived_binary->length());
1334 ClearClientDownloadRequest();
1335 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
1336 }
1303 1337
1304 download_service_->CheckClientDownload( 1338 {
1305 &item, 1339 // If the response is dangerous the result should also be marked as
1306 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1340 // dangerous.
1307 base::Unretained(this))); 1341 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
1308 base::RunLoop().Run(); 1342 net::URLRequestStatus::SUCCESS);
1309 1343
1310 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 1344 RunLoop run_loop;
1311 EXPECT_TRUE(HasClientDownloadRequest()); 1345 download_service_->CheckClientDownload(
1312 ClearClientDownloadRequest(); 1346 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1313 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); 1347 base::Unretained(this), run_loop.QuitClosure()));
1348 run_loop.Run();
1314 1349
1315 // Repeat the test with an archive inside the zip file in addition to the 1350 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
1316 // executable. 1351 EXPECT_TRUE(HasClientDownloadRequest());
1317 ASSERT_EQ(static_cast<int>(file_contents.size()), 1352 ClearClientDownloadRequest();
1318 base::WriteFile( 1353 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
1319 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.rar")), 1354 }
1320 file_contents.data(), file_contents.size()));
1321 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false));
1322 1355
1323 download_service_->CheckClientDownload( 1356 {
1324 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1357 // Repeat the test with an archive inside the zip file in addition to the
1325 base::Unretained(this))); 1358 // executable.
1326 base::RunLoop().Run(); 1359 ASSERT_EQ(static_cast<int>(file_contents.size()),
1360 base::WriteFile(
1361 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.rar")),
1362 file_contents.data(), file_contents.size()));
1363 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false));
1327 1364
1328 ASSERT_TRUE(HasClientDownloadRequest()); 1365 RunLoop run_loop;
1329 EXPECT_EQ(1, GetClientDownloadRequest()->archived_binary_size()); 1366 download_service_->CheckClientDownload(
1330 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); 1367 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1331 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_EXECUTABLE, 1368 base::Unretained(this), run_loop.QuitClosure()));
1332 GetClientDownloadRequest()->download_type()); 1369 run_loop.Run();
1333 ClearClientDownloadRequest();
1334 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
1335 1370
1336 // Repeat the test with just the archive inside the zip file. 1371 ASSERT_TRUE(HasClientDownloadRequest());
1337 ASSERT_TRUE( 1372 EXPECT_EQ(1, GetClientDownloadRequest()->archived_binary_size());
1338 base::DeleteFile(zip_source_dir.path().AppendASCII("file.exe"), false)); 1373 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type());
1339 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false)); 1374 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_EXECUTABLE,
1375 GetClientDownloadRequest()->download_type());
1376 ClearClientDownloadRequest();
1377 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
1378 }
1340 1379
1341 download_service_->CheckClientDownload( 1380 {
1342 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1381 // Repeat the test with just the archive inside the zip file.
1343 base::Unretained(this))); 1382 ASSERT_TRUE(
1344 base::RunLoop().Run(); 1383 base::DeleteFile(zip_source_dir.path().AppendASCII("file.exe"), false));
1384 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false));
1345 1385
1346 ASSERT_TRUE(HasClientDownloadRequest()); 1386 RunLoop run_loop;
1347 EXPECT_EQ(0, GetClientDownloadRequest()->archived_binary_size()); 1387 download_service_->CheckClientDownload(
1348 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); 1388 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1349 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_ARCHIVE, 1389 base::Unretained(this), run_loop.QuitClosure()));
1350 GetClientDownloadRequest()->download_type()); 1390 run_loop.Run();
1351 ClearClientDownloadRequest(); 1391
1352 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); 1392 ASSERT_TRUE(HasClientDownloadRequest());
1393 EXPECT_EQ(0, GetClientDownloadRequest()->archived_binary_size());
1394 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type());
1395 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_ARCHIVE,
1396 GetClientDownloadRequest()->download_type());
1397 ClearClientDownloadRequest();
1398 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get());
1399 }
1353 } 1400 }
1354 1401
1355 TEST_F(DownloadProtectionServiceTest, 1402 TEST_F(DownloadProtectionServiceTest,
1356 CheckClientDownloadReportCorruptZipNormal) { 1403 CheckClientDownloadReportCorruptZipNormal) {
1357 // !is_extended_reporting && !is_incognito 1404 // !is_extended_reporting && !is_incognito
1358 CheckClientDownloadReportCorruptArchive(ZIP, false, false); 1405 CheckClientDownloadReportCorruptArchive(ZIP, false, false);
1359 } 1406 }
1360 1407
1361 TEST_F(DownloadProtectionServiceTest, 1408 TEST_F(DownloadProtectionServiceTest,
1362 CheckClientDownloadReportCorruptZipExtended) { 1409 CheckClientDownloadReportCorruptZipExtended) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1408 EXPECT_CALL(*sb_service_->mock_database_manager(), 1455 EXPECT_CALL(*sb_service_->mock_database_manager(),
1409 MatchDownloadWhitelistUrl(_)) 1456 MatchDownloadWhitelistUrl(_))
1410 .WillRepeatedly(Return(false)); 1457 .WillRepeatedly(Return(false));
1411 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)) 1458 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _))
1412 .WillOnce(SetCertificateContents("dummy cert data")); 1459 .WillOnce(SetCertificateContents("dummy cert data"));
1413 EXPECT_CALL( 1460 EXPECT_CALL(
1414 *binary_feature_extractor_.get(), 1461 *binary_feature_extractor_.get(),
1415 ExtractImageFeatures(tmp_path_, BinaryFeatureExtractor::kDefaultOptions, 1462 ExtractImageFeatures(tmp_path_, BinaryFeatureExtractor::kDefaultOptions,
1416 _, _)) 1463 _, _))
1417 .WillOnce(SetDosHeaderContents("dummy dos header")); 1464 .WillOnce(SetDosHeaderContents("dummy dos header"));
1465 RunLoop run_loop;
1418 download_service_->CheckClientDownload( 1466 download_service_->CheckClientDownload(
1419 &item, 1467 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1420 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1468 base::Unretained(this), run_loop.QuitClosure()));
1421 base::Unretained(this)));
1422 1469
1423 // Run the message loop(s) until SendRequest is called. 1470 // Run the message loop(s) until SendRequest is called.
1424 FlushThreadMessageLoops(); 1471 FlushThreadMessageLoops();
1425 EXPECT_TRUE(HasClientDownloadRequest()); 1472 EXPECT_TRUE(HasClientDownloadRequest());
1426 ClearClientDownloadRequest(); 1473 ClearClientDownloadRequest();
1427 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1474 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1428 ASSERT_TRUE(fetcher); 1475 ASSERT_TRUE(fetcher);
1429 ClientDownloadRequest request; 1476 ClientDownloadRequest request;
1430 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data())); 1477 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data()));
1431 EXPECT_EQ("http://www.google.com/bla.exe", request.url()); 1478 EXPECT_EQ("http://www.google.com/bla.exe", request.url());
(...skipping 16 matching lines...) Expand all
1448 ASSERT_EQ(1, chain.element_size()); 1495 ASSERT_EQ(1, chain.element_size());
1449 EXPECT_EQ("dummy cert data", chain.element(0).certificate()); 1496 EXPECT_EQ("dummy cert data", chain.element(0).certificate());
1450 EXPECT_TRUE(request.has_image_headers()); 1497 EXPECT_TRUE(request.has_image_headers());
1451 const ClientDownloadRequest_ImageHeaders& headers = 1498 const ClientDownloadRequest_ImageHeaders& headers =
1452 request.image_headers(); 1499 request.image_headers();
1453 EXPECT_TRUE(headers.has_pe_headers()); 1500 EXPECT_TRUE(headers.has_pe_headers());
1454 EXPECT_TRUE(headers.pe_headers().has_dos_header()); 1501 EXPECT_TRUE(headers.pe_headers().has_dos_header());
1455 EXPECT_EQ("dummy dos header", headers.pe_headers().dos_header()); 1502 EXPECT_EQ("dummy dos header", headers.pe_headers().dos_header());
1456 1503
1457 // Simulate the request finishing. 1504 // Simulate the request finishing.
1505
1458 base::ThreadTaskRunnerHandle::Get()->PostTask( 1506 base::ThreadTaskRunnerHandle::Get()->PostTask(
1459 FROM_HERE, 1507 FROM_HERE,
1460 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, 1508 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete,
1461 base::Unretained(this), fetcher)); 1509 base::Unretained(this), fetcher));
1462 base::RunLoop().Run(); 1510 run_loop.Run();
1463 } 1511 }
1464 1512
1465 // Similar to above, but with an unsigned binary. 1513 // Similar to above, but with an unsigned binary.
1466 TEST_F(DownloadProtectionServiceTest, 1514 TEST_F(DownloadProtectionServiceTest,
1467 CheckClientDownloadValidateRequestNoSignature) { 1515 CheckClientDownloadValidateRequestNoSignature) {
1468 net::TestURLFetcherFactory factory; 1516 net::TestURLFetcherFactory factory;
1469 1517
1470 content::MockDownloadItem item; 1518 content::MockDownloadItem item;
1471 PrepareBasicDownloadItem( 1519 PrepareBasicDownloadItem(
1472 &item, 1520 &item,
1473 {"http://www.google.com/", 1521 {"http://www.google.com/",
1474 "ftp://www.google.com/bla.exe"}, // url_chain 1522 "ftp://www.google.com/bla.exe"}, // url_chain
1475 "http://www.google.com/", // referrer 1523 "http://www.google.com/", // referrer
1476 FILE_PATH_LITERAL("bla.tmp"), // tmp_path 1524 FILE_PATH_LITERAL("bla.tmp"), // tmp_path
1477 FILE_PATH_LITERAL("bla.exe")); // final_path 1525 FILE_PATH_LITERAL("bla.exe")); // final_path
1478 std::string remote_address = "10.11.12.13"; 1526 std::string remote_address = "10.11.12.13";
1479 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(remote_address)); 1527 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(remote_address));
1480 1528
1481 EXPECT_CALL(*sb_service_->mock_database_manager(), 1529 EXPECT_CALL(*sb_service_->mock_database_manager(),
1482 MatchDownloadWhitelistUrl(_)) 1530 MatchDownloadWhitelistUrl(_))
1483 .WillRepeatedly(Return(false)); 1531 .WillRepeatedly(Return(false));
1484 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)); 1532 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _));
1485 EXPECT_CALL(*binary_feature_extractor_.get(), 1533 EXPECT_CALL(*binary_feature_extractor_.get(),
1486 ExtractImageFeatures(tmp_path_, 1534 ExtractImageFeatures(tmp_path_,
1487 BinaryFeatureExtractor::kDefaultOptions, 1535 BinaryFeatureExtractor::kDefaultOptions,
1488 _, _)); 1536 _, _));
1537 RunLoop run_loop;
1489 download_service_->CheckClientDownload( 1538 download_service_->CheckClientDownload(
1490 &item, 1539 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1491 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1540 base::Unretained(this), run_loop.QuitClosure()));
1492 base::Unretained(this)));
1493 1541
1494 // Run the message loop(s) until SendRequest is called. 1542 // Run the message loop(s) until SendRequest is called.
1495 FlushThreadMessageLoops(); 1543 FlushThreadMessageLoops();
1496 EXPECT_TRUE(HasClientDownloadRequest()); 1544 EXPECT_TRUE(HasClientDownloadRequest());
1497 ClearClientDownloadRequest(); 1545 ClearClientDownloadRequest();
1498 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1546 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1499 ASSERT_TRUE(fetcher); 1547 ASSERT_TRUE(fetcher);
1500 ClientDownloadRequest request; 1548 ClientDownloadRequest request;
1501 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data())); 1549 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data()));
1502 EXPECT_EQ("ftp://www.google.com/bla.exe", request.url()); 1550 EXPECT_EQ("ftp://www.google.com/bla.exe", request.url());
1503 EXPECT_EQ(hash_, request.digests().sha256()); 1551 EXPECT_EQ(hash_, request.digests().sha256());
1504 EXPECT_EQ(item.GetReceivedBytes(), request.length()); 1552 EXPECT_EQ(item.GetReceivedBytes(), request.length());
1505 EXPECT_EQ(item.HasUserGesture(), request.user_initiated()); 1553 EXPECT_EQ(item.HasUserGesture(), request.user_initiated());
1506 EXPECT_EQ(2, request.resources_size()); 1554 EXPECT_EQ(2, request.resources_size());
1507 EXPECT_TRUE(RequestContainsResource(request, 1555 EXPECT_TRUE(RequestContainsResource(request,
1508 ClientDownloadRequest::DOWNLOAD_REDIRECT, 1556 ClientDownloadRequest::DOWNLOAD_REDIRECT,
1509 "http://www.google.com/", "")); 1557 "http://www.google.com/", ""));
1510 EXPECT_TRUE(RequestContainsResource(request, 1558 EXPECT_TRUE(RequestContainsResource(request,
1511 ClientDownloadRequest::DOWNLOAD_URL, 1559 ClientDownloadRequest::DOWNLOAD_URL,
1512 "ftp://www.google.com/bla.exe", 1560 "ftp://www.google.com/bla.exe",
1513 referrer_.spec())); 1561 referrer_.spec()));
1514 EXPECT_TRUE(request.has_signature()); 1562 EXPECT_TRUE(request.has_signature());
1515 EXPECT_EQ(0, request.signature().certificate_chain_size()); 1563 EXPECT_EQ(0, request.signature().certificate_chain_size());
1516 1564
1517 // Simulate the request finishing. 1565 // Simulate the request finishing.
1518 base::ThreadTaskRunnerHandle::Get()->PostTask( 1566 base::ThreadTaskRunnerHandle::Get()->PostTask(
1519 FROM_HERE, 1567 FROM_HERE,
1520 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, 1568 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete,
1521 base::Unretained(this), fetcher)); 1569 base::Unretained(this), fetcher));
1522 base::RunLoop().Run(); 1570 run_loop.Run();
1523 } 1571 }
1524 1572
1525 // Similar to above, but with tab history. 1573 // Similar to above, but with tab history.
1526 TEST_F(DownloadProtectionServiceTest, 1574 TEST_F(DownloadProtectionServiceTest,
1527 CheckClientDownloadValidateRequestTabHistory) { 1575 CheckClientDownloadValidateRequestTabHistory) {
1528 net::TestURLFetcherFactory factory; 1576 net::TestURLFetcherFactory factory;
1529 1577
1530 content::MockDownloadItem item; 1578 content::MockDownloadItem item;
1531 PrepareBasicDownloadItem( 1579 PrepareBasicDownloadItem(
1532 &item, 1580 &item,
(...skipping 18 matching lines...) Expand all
1551 .WillRepeatedly(SetCertificateContents("dummy cert data")); 1599 .WillRepeatedly(SetCertificateContents("dummy cert data"));
1552 EXPECT_CALL( 1600 EXPECT_CALL(
1553 *binary_feature_extractor_.get(), 1601 *binary_feature_extractor_.get(),
1554 ExtractImageFeatures(tmp_path_, BinaryFeatureExtractor::kDefaultOptions, 1602 ExtractImageFeatures(tmp_path_, BinaryFeatureExtractor::kDefaultOptions,
1555 _, _)) 1603 _, _))
1556 .WillRepeatedly(SetDosHeaderContents("dummy dos header")); 1604 .WillRepeatedly(SetDosHeaderContents("dummy dos header"));
1557 1605
1558 // First test with no history match for the tab URL. 1606 // First test with no history match for the tab URL.
1559 { 1607 {
1560 TestURLFetcherWatcher fetcher_watcher(&factory); 1608 TestURLFetcherWatcher fetcher_watcher(&factory);
1609 RunLoop run_loop;
1561 download_service_->CheckClientDownload( 1610 download_service_->CheckClientDownload(
1562 &item, 1611 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1563 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1612 base::Unretained(this), run_loop.QuitClosure()));
1564 base::Unretained(this)));
1565 1613
1566 EXPECT_EQ(0, fetcher_watcher.WaitForRequest()); 1614 EXPECT_EQ(0, fetcher_watcher.WaitForRequest());
1567 EXPECT_TRUE(HasClientDownloadRequest()); 1615 EXPECT_TRUE(HasClientDownloadRequest());
1568 ClearClientDownloadRequest(); 1616 ClearClientDownloadRequest();
1569 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1617 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1570 ASSERT_TRUE(fetcher); 1618 ASSERT_TRUE(fetcher);
1571 ClientDownloadRequest request; 1619 ClientDownloadRequest request;
1572 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data())); 1620 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data()));
1573 EXPECT_EQ("http://www.google.com/bla.exe", request.url()); 1621 EXPECT_EQ("http://www.google.com/bla.exe", request.url());
1574 EXPECT_EQ(hash_, request.digests().sha256()); 1622 EXPECT_EQ(hash_, request.digests().sha256());
(...skipping 25 matching lines...) Expand all
1600 request.image_headers(); 1648 request.image_headers();
1601 EXPECT_TRUE(headers.has_pe_headers()); 1649 EXPECT_TRUE(headers.has_pe_headers());
1602 EXPECT_TRUE(headers.pe_headers().has_dos_header()); 1650 EXPECT_TRUE(headers.pe_headers().has_dos_header());
1603 EXPECT_EQ("dummy dos header", headers.pe_headers().dos_header()); 1651 EXPECT_EQ("dummy dos header", headers.pe_headers().dos_header());
1604 1652
1605 // Simulate the request finishing. 1653 // Simulate the request finishing.
1606 base::ThreadTaskRunnerHandle::Get()->PostTask( 1654 base::ThreadTaskRunnerHandle::Get()->PostTask(
1607 FROM_HERE, 1655 FROM_HERE,
1608 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, 1656 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete,
1609 base::Unretained(this), fetcher)); 1657 base::Unretained(this), fetcher));
1610 base::RunLoop().Run(); 1658 run_loop.Run();
1611 } 1659 }
1612 1660
1613 // Now try with a history match. 1661 // Now try with a history match.
1614 { 1662 {
1615 history::RedirectList redirects; 1663 history::RedirectList redirects;
1616 redirects.push_back(GURL("http://tab.com/ref1")); 1664 redirects.push_back(GURL("http://tab.com/ref1"));
1617 redirects.push_back(GURL("http://tab.com/ref2")); 1665 redirects.push_back(GURL("http://tab.com/ref2"));
1618 redirects.push_back(tab_url); 1666 redirects.push_back(tab_url);
1619 HistoryServiceFactory::GetForProfile(profile_.get(), 1667 HistoryServiceFactory::GetForProfile(profile_.get(),
1620 ServiceAccessType::EXPLICIT_ACCESS) 1668 ServiceAccessType::EXPLICIT_ACCESS)
1621 ->AddPage(tab_url, 1669 ->AddPage(tab_url,
1622 base::Time::Now(), 1670 base::Time::Now(),
1623 reinterpret_cast<history::ContextID>(1), 1671 reinterpret_cast<history::ContextID>(1),
1624 0, 1672 0,
1625 GURL(), 1673 GURL(),
1626 redirects, 1674 redirects,
1627 ui::PAGE_TRANSITION_TYPED, 1675 ui::PAGE_TRANSITION_TYPED,
1628 history::SOURCE_BROWSED, 1676 history::SOURCE_BROWSED,
1629 false); 1677 false);
1630 1678
1631 TestURLFetcherWatcher fetcher_watcher(&factory); 1679 TestURLFetcherWatcher fetcher_watcher(&factory);
1680 RunLoop run_loop;
1632 download_service_->CheckClientDownload( 1681 download_service_->CheckClientDownload(
1633 &item, 1682 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1634 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1683 base::Unretained(this), run_loop.QuitClosure()));
1635 base::Unretained(this))); 1684
1636 EXPECT_EQ(0, fetcher_watcher.WaitForRequest()); 1685 EXPECT_EQ(0, fetcher_watcher.WaitForRequest());
1637 EXPECT_TRUE(HasClientDownloadRequest()); 1686 EXPECT_TRUE(HasClientDownloadRequest());
1638 ClearClientDownloadRequest(); 1687 ClearClientDownloadRequest();
1639 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 1688 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
1640 ASSERT_TRUE(fetcher); 1689 ASSERT_TRUE(fetcher);
1641 ClientDownloadRequest request; 1690 ClientDownloadRequest request;
1642 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data())); 1691 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data()));
1643 EXPECT_EQ("http://www.google.com/bla.exe", request.url()); 1692 EXPECT_EQ("http://www.google.com/bla.exe", request.url());
1644 EXPECT_EQ(hash_, request.digests().sha256()); 1693 EXPECT_EQ(hash_, request.digests().sha256());
1645 EXPECT_EQ(item.GetReceivedBytes(), request.length()); 1694 EXPECT_EQ(item.GetReceivedBytes(), request.length());
(...skipping 26 matching lines...) Expand all
1672 const ClientDownloadRequest_CertificateChain& chain = 1721 const ClientDownloadRequest_CertificateChain& chain =
1673 request.signature().certificate_chain(0); 1722 request.signature().certificate_chain(0);
1674 ASSERT_EQ(1, chain.element_size()); 1723 ASSERT_EQ(1, chain.element_size());
1675 EXPECT_EQ("dummy cert data", chain.element(0).certificate()); 1724 EXPECT_EQ("dummy cert data", chain.element(0).certificate());
1676 1725
1677 // Simulate the request finishing. 1726 // Simulate the request finishing.
1678 base::ThreadTaskRunnerHandle::Get()->PostTask( 1727 base::ThreadTaskRunnerHandle::Get()->PostTask(
1679 FROM_HERE, 1728 FROM_HERE,
1680 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, 1729 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete,
1681 base::Unretained(this), fetcher)); 1730 base::Unretained(this), fetcher));
1682 base::RunLoop().Run(); 1731 run_loop.Run();
1683 } 1732 }
1684 } 1733 }
1685 1734
1686 TEST_F(DownloadProtectionServiceTest, TestCheckDownloadUrl) { 1735 TEST_F(DownloadProtectionServiceTest, TestCheckDownloadUrl) {
1687 net::TestURLFetcherFactory factory; 1736 net::TestURLFetcherFactory factory;
1688 1737
1689 std::vector<GURL> url_chain; 1738 std::vector<GURL> url_chain;
1690 url_chain.push_back(GURL("http://www.google.com/")); 1739 url_chain.push_back(GURL("http://www.google.com/"));
1691 url_chain.push_back(GURL("http://www.google.com/bla.exe")); 1740 url_chain.push_back(GURL("http://www.google.com/bla.exe"));
1692 GURL referrer("http://www.google.com/"); 1741 GURL referrer("http://www.google.com/");
1693 std::string hash = "hash"; 1742 std::string hash = "hash";
1694 1743
1695 content::MockDownloadItem item; 1744 content::MockDownloadItem item;
1696 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); 1745 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
1697 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); 1746 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
1698 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash)); 1747 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
1699 1748
1700 // CheckDownloadURL returns immediately which means the client object callback 1749 {
1701 // will never be called. Nevertheless the callback provided to 1750 // CheckDownloadURL returns immediately which means the client object
1702 // CheckClientDownload must still be called. 1751 // callback
1703 EXPECT_CALL(*sb_service_->mock_database_manager(), 1752 // will never be called. Nevertheless the callback provided to
1704 CheckDownloadUrl(ContainerEq(url_chain), NotNull())) 1753 // CheckClientDownload must still be called.
1705 .WillOnce(Return(true)); 1754 EXPECT_CALL(*sb_service_->mock_database_manager(),
1706 download_service_->CheckDownloadUrl( 1755 CheckDownloadUrl(ContainerEq(url_chain), NotNull()))
1707 item, 1756 .WillOnce(Return(true));
1708 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1757 RunLoop run_loop;
1709 base::Unretained(this))); 1758 download_service_->CheckDownloadUrl(
1710 base::RunLoop().Run(); 1759 item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1711 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 1760 base::Unretained(this), run_loop.QuitClosure()));
1712 Mock::VerifyAndClearExpectations(sb_service_.get()); 1761 run_loop.Run();
1762 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
1763 Mock::VerifyAndClearExpectations(sb_service_.get());
1764 }
1713 1765
1714 EXPECT_CALL(*sb_service_->mock_database_manager(), 1766 {
1715 CheckDownloadUrl(ContainerEq(url_chain), NotNull())) 1767 EXPECT_CALL(*sb_service_->mock_database_manager(),
1716 .WillOnce(DoAll(CheckDownloadUrlDone(SB_THREAT_TYPE_SAFE), 1768 CheckDownloadUrl(ContainerEq(url_chain), NotNull()))
1717 Return(false))); 1769 .WillOnce(
1718 download_service_->CheckDownloadUrl( 1770 DoAll(CheckDownloadUrlDone(SB_THREAT_TYPE_SAFE), Return(false)));
1719 item, 1771 RunLoop run_loop;
1720 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1772 download_service_->CheckDownloadUrl(
1721 base::Unretained(this))); 1773 item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1722 base::RunLoop().Run(); 1774 base::Unretained(this), run_loop.QuitClosure()));
1723 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 1775 run_loop.Run();
1724 Mock::VerifyAndClearExpectations(sb_service_.get()); 1776 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
1777 Mock::VerifyAndClearExpectations(sb_service_.get());
1778 }
1725 1779
1726 EXPECT_CALL(*sb_service_->mock_database_manager(), 1780 {
1727 CheckDownloadUrl(ContainerEq(url_chain), NotNull())) 1781 EXPECT_CALL(*sb_service_->mock_database_manager(),
1728 .WillOnce(DoAll( 1782 CheckDownloadUrl(ContainerEq(url_chain), NotNull()))
1729 CheckDownloadUrlDone(SB_THREAT_TYPE_URL_MALWARE), 1783 .WillOnce(DoAll(CheckDownloadUrlDone(SB_THREAT_TYPE_URL_MALWARE),
1730 Return(false))); 1784 Return(false)));
1731 download_service_->CheckDownloadUrl( 1785 RunLoop run_loop;
1732 item, 1786 download_service_->CheckDownloadUrl(
1733 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1787 item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1734 base::Unretained(this))); 1788 base::Unretained(this), run_loop.QuitClosure()));
1735 base::RunLoop().Run(); 1789 run_loop.Run();
1736 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 1790 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
1737 Mock::VerifyAndClearExpectations(sb_service_.get()); 1791 Mock::VerifyAndClearExpectations(sb_service_.get());
1792 }
1738 1793
1739 EXPECT_CALL(*sb_service_->mock_database_manager(), 1794 {
1740 CheckDownloadUrl(ContainerEq(url_chain), 1795 EXPECT_CALL(*sb_service_->mock_database_manager(),
1741 NotNull())) 1796 CheckDownloadUrl(ContainerEq(url_chain), NotNull()))
1742 .WillOnce(DoAll( 1797 .WillOnce(DoAll(CheckDownloadUrlDone(SB_THREAT_TYPE_BINARY_MALWARE_URL),
1743 CheckDownloadUrlDone(SB_THREAT_TYPE_BINARY_MALWARE_URL), 1798 Return(false)));
1744 Return(false))); 1799 RunLoop run_loop;
1745 download_service_->CheckDownloadUrl( 1800 download_service_->CheckDownloadUrl(
1746 item, 1801 item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1747 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1802 base::Unretained(this), run_loop.QuitClosure()));
1748 base::Unretained(this))); 1803 run_loop.Run();
1749 base::RunLoop().Run(); 1804 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
1750 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 1805 }
1751 } 1806 }
1752 1807
1753 TEST_F(DownloadProtectionServiceTest, TestDownloadRequestTimeout) { 1808 TEST_F(DownloadProtectionServiceTest, TestDownloadRequestTimeout) {
1754 net::TestURLFetcherFactory factory; 1809 net::TestURLFetcherFactory factory;
1755 1810
1756 content::MockDownloadItem item; 1811 content::MockDownloadItem item;
1757 PrepareBasicDownloadItem( 1812 PrepareBasicDownloadItem(
1758 &item, 1813 &item,
1759 {"http://www.evil.com/bla.exe"}, // url_chain 1814 {"http://www.evil.com/bla.exe"}, // url_chain
1760 "http://www.google.com/", // referrer 1815 "http://www.google.com/", // referrer
1761 FILE_PATH_LITERAL("a.tmp"), // tmp_path 1816 FILE_PATH_LITERAL("a.tmp"), // tmp_path
1762 FILE_PATH_LITERAL("a.exe")); // final_path 1817 FILE_PATH_LITERAL("a.exe")); // final_path
1763 1818
1764 EXPECT_CALL(*sb_service_->mock_database_manager(), 1819 EXPECT_CALL(*sb_service_->mock_database_manager(),
1765 MatchDownloadWhitelistUrl(_)) 1820 MatchDownloadWhitelistUrl(_))
1766 .WillRepeatedly(Return(false)); 1821 .WillRepeatedly(Return(false));
1767 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)); 1822 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _));
1768 EXPECT_CALL(*binary_feature_extractor_.get(), 1823 EXPECT_CALL(*binary_feature_extractor_.get(),
1769 ExtractImageFeatures(tmp_path_, 1824 ExtractImageFeatures(tmp_path_,
1770 BinaryFeatureExtractor::kDefaultOptions, 1825 BinaryFeatureExtractor::kDefaultOptions,
1771 _, _)); 1826 _, _));
1772 1827
1773 download_service_->download_request_timeout_ms_ = 10; 1828 download_service_->download_request_timeout_ms_ = 10;
1829 RunLoop run_loop;
1774 download_service_->CheckClientDownload( 1830 download_service_->CheckClientDownload(
1775 &item, 1831 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1776 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1832 base::Unretained(this), run_loop.QuitClosure()));
1777 base::Unretained(this)));
1778 1833
1779 // The request should time out because the HTTP request hasn't returned 1834 // The request should time out because the HTTP request hasn't returned
1780 // anything yet. 1835 // anything yet.
1781 base::RunLoop().Run(); 1836 run_loop.Run();
1782 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 1837 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
1783 EXPECT_TRUE(HasClientDownloadRequest()); 1838 EXPECT_TRUE(HasClientDownloadRequest());
1784 ClearClientDownloadRequest(); 1839 ClearClientDownloadRequest();
1785 } 1840 }
1786 1841
1787 TEST_F(DownloadProtectionServiceTest, TestDownloadItemDestroyed) { 1842 TEST_F(DownloadProtectionServiceTest, TestDownloadItemDestroyed) {
1788 { 1843 {
1789 content::MockDownloadItem item; 1844 content::MockDownloadItem item;
1790 PrepareBasicDownloadItem( 1845 PrepareBasicDownloadItem(
1791 &item, 1846 &item,
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1834 .WillRepeatedly(Invoke([&item](const GURL&) { 1889 .WillRepeatedly(Invoke([&item](const GURL&) {
1835 item.reset(); 1890 item.reset();
1836 return false; 1891 return false;
1837 })); 1892 }));
1838 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)); 1893 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _));
1839 EXPECT_CALL(*binary_feature_extractor_.get(), 1894 EXPECT_CALL(*binary_feature_extractor_.get(),
1840 ExtractImageFeatures(tmp_path_, 1895 ExtractImageFeatures(tmp_path_,
1841 BinaryFeatureExtractor::kDefaultOptions, 1896 BinaryFeatureExtractor::kDefaultOptions,
1842 _, _)); 1897 _, _));
1843 1898
1899 RunLoop run_loop;
1844 download_service_->CheckClientDownload( 1900 download_service_->CheckClientDownload(
1845 item.get(), 1901 item.get(), base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1846 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1902 base::Unretained(this), run_loop.QuitClosure()));
1847 base::Unretained(this)));
1848 1903
1849 base::RunLoop().Run(); 1904 run_loop.Run();
1850 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 1905 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
1851 EXPECT_FALSE(HasClientDownloadRequest()); 1906 EXPECT_FALSE(HasClientDownloadRequest());
1852 } 1907 }
1853 1908
1854 TEST_F(DownloadProtectionServiceTest, GetCertificateWhitelistStrings) { 1909 TEST_F(DownloadProtectionServiceTest, GetCertificateWhitelistStrings) {
1855 // We'll pass this cert in as the "issuer", even though it isn't really 1910 // We'll pass this cert in as the "issuer", even though it isn't really
1856 // used to sign the certs below. GetCertificateWhitelistStirngs doesn't care 1911 // used to sign the certs below. GetCertificateWhitelistStirngs doesn't care
1857 // about this. 1912 // about this.
1858 scoped_refptr<net::X509Certificate> issuer_cert( 1913 scoped_refptr<net::X509Certificate> issuer_cert(
1859 ReadTestCertificate("issuer.pem")); 1914 ReadTestCertificate("issuer.pem"));
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
1980 EXPECT_EQ(token, DownloadProtectionService::GetDownloadPingToken(&item)); 2035 EXPECT_EQ(token, DownloadProtectionService::GetDownloadPingToken(&item));
1981 2036
1982 DownloadProtectionService::SetDownloadPingToken(&item, std::string()); 2037 DownloadProtectionService::SetDownloadPingToken(&item, std::string());
1983 EXPECT_TRUE(DownloadProtectionService::GetDownloadPingToken(&item).empty()); 2038 EXPECT_TRUE(DownloadProtectionService::GetDownloadPingToken(&item).empty());
1984 } 2039 }
1985 2040
1986 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_Unsupported) { 2041 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_Unsupported) {
1987 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.txt")); 2042 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.txt"));
1988 std::vector<base::FilePath::StringType> alternate_extensions{ 2043 std::vector<base::FilePath::StringType> alternate_extensions{
1989 FILE_PATH_LITERAL(".tmp"), FILE_PATH_LITERAL(".asdfasdf")}; 2044 FILE_PATH_LITERAL(".tmp"), FILE_PATH_LITERAL(".asdfasdf")};
2045 SetExtendedReportingPreference(false);
1990 download_service_->CheckPPAPIDownloadRequest( 2046 download_service_->CheckPPAPIDownloadRequest(
1991 GURL("http://example.com/foo"), default_file_path, alternate_extensions, 2047 GURL("http://example.com/foo"), default_file_path, alternate_extensions,
2048 profile_.get(),
1992 base::Bind(&DownloadProtectionServiceTest::SyncCheckDoneCallback, 2049 base::Bind(&DownloadProtectionServiceTest::SyncCheckDoneCallback,
1993 base::Unretained(this))); 2050 base::Unretained(this)));
1994 ASSERT_TRUE(IsResult(DownloadProtectionService::SAFE)); 2051 ASSERT_TRUE(IsResult(DownloadProtectionService::SAFE));
1995 } 2052 }
1996 2053
1997 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_SupportedDefault) { 2054 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_SupportedDefault) {
1998 net::FakeURLFetcherFactory factory(nullptr); 2055 net::FakeURLFetcherFactory factory(nullptr);
1999 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); 2056 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx"));
2000 std::vector<base::FilePath::StringType> alternate_extensions; 2057 std::vector<base::FilePath::StringType> alternate_extensions;
2001 EXPECT_CALL(*sb_service_->mock_database_manager(), 2058 EXPECT_CALL(*sb_service_->mock_database_manager(),
(...skipping 10 matching lines...) Expand all
2012 DownloadProtectionService::DANGEROUS_HOST}, 2069 DownloadProtectionService::DANGEROUS_HOST},
2013 {ClientDownloadResponse::POTENTIALLY_UNWANTED, 2070 {ClientDownloadResponse::POTENTIALLY_UNWANTED,
2014 DownloadProtectionService::POTENTIALLY_UNWANTED}, 2071 DownloadProtectionService::POTENTIALLY_UNWANTED},
2015 {ClientDownloadResponse::UNKNOWN, DownloadProtectionService::UNKNOWN}, 2072 {ClientDownloadResponse::UNKNOWN, DownloadProtectionService::UNKNOWN},
2016 }; 2073 };
2017 2074
2018 for (const auto& test_case : kExpectedResults) { 2075 for (const auto& test_case : kExpectedResults) {
2019 factory.ClearFakeResponses(); 2076 factory.ClearFakeResponses();
2020 PrepareResponse(&factory, test_case.verdict, net::HTTP_OK, 2077 PrepareResponse(&factory, test_case.verdict, net::HTTP_OK,
2021 net::URLRequestStatus::SUCCESS); 2078 net::URLRequestStatus::SUCCESS);
2079 SetExtendedReportingPreference(false);
2080 RunLoop run_loop;
2022 download_service_->CheckPPAPIDownloadRequest( 2081 download_service_->CheckPPAPIDownloadRequest(
2023 GURL("http://example.com/foo"), default_file_path, alternate_extensions, 2082 GURL("http://example.com/foo"), default_file_path, alternate_extensions,
2083 profile_.get(),
2024 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2084 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2025 base::Unretained(this))); 2085 base::Unretained(this), run_loop.QuitClosure()));
2026 base::RunLoop().Run(); 2086 run_loop.Run();
2027 ASSERT_TRUE(IsResult(test_case.expected_result)); 2087 ASSERT_TRUE(IsResult(test_case.expected_result));
2028 } 2088 }
2029 } 2089 }
2030 2090
2031 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_SupportedAlternate) { 2091 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_SupportedAlternate) {
2032 net::FakeURLFetcherFactory factory(nullptr); 2092 net::FakeURLFetcherFactory factory(nullptr);
2033 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.txt")); 2093 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.txt"));
2034 std::vector<base::FilePath::StringType> alternate_extensions{ 2094 std::vector<base::FilePath::StringType> alternate_extensions{
2035 FILE_PATH_LITERAL(".tmp"), FILE_PATH_LITERAL(".crx")}; 2095 FILE_PATH_LITERAL(".tmp"), FILE_PATH_LITERAL(".crx")};
2036 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, 2096 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
2037 net::URLRequestStatus::SUCCESS); 2097 net::URLRequestStatus::SUCCESS);
2038 EXPECT_CALL(*sb_service_->mock_database_manager(), 2098 EXPECT_CALL(*sb_service_->mock_database_manager(),
2039 MatchDownloadWhitelistUrl(_)) 2099 MatchDownloadWhitelistUrl(_))
2040 .WillRepeatedly(Return(false)); 2100 .WillRepeatedly(Return(false));
2101 SetExtendedReportingPreference(false);
2102 RunLoop run_loop;
2041 download_service_->CheckPPAPIDownloadRequest( 2103 download_service_->CheckPPAPIDownloadRequest(
2042 GURL("http://example.com/foo"), default_file_path, alternate_extensions, 2104 GURL("http://example.com/foo"), default_file_path, alternate_extensions,
2105 profile_.get(),
2043 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2106 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2044 base::Unretained(this))); 2107 base::Unretained(this), run_loop.QuitClosure()));
2045 base::RunLoop().Run(); 2108 run_loop.Run();
2046 2109
2047 ASSERT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 2110 ASSERT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
2048 } 2111 }
2049 2112
2050 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_WhitelistedURL) { 2113 TEST_F(DownloadProtectionServiceTest,
2114 PPAPIDownloadRequest_WhitelistedURLNotExtendedReporting) {
2051 net::FakeURLFetcherFactory factory(nullptr); 2115 net::FakeURLFetcherFactory factory(nullptr);
2052 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); 2116 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx"));
2053 std::vector<base::FilePath::StringType> alternate_extensions; 2117 std::vector<base::FilePath::StringType> alternate_extensions;
2118 // Configs "server" side response to DANGEROUS, in case a request is send out.
2119 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
2120 net::URLRequestStatus::SUCCESS);
2054 EXPECT_CALL(*sb_service_->mock_database_manager(), 2121 EXPECT_CALL(*sb_service_->mock_database_manager(),
2055 MatchDownloadWhitelistUrl(_)) 2122 MatchDownloadWhitelistUrl(_))
2056 .WillRepeatedly(Return(true)); 2123 .WillRepeatedly(Return(true));
2124 SetExtendedReportingPreference(false);
2125 // Sets the sample rate to 1.00.
2126 SetWhitelistedDownloadSampleRate(1.00);
2127 RunLoop run_loop;
2057 download_service_->CheckPPAPIDownloadRequest( 2128 download_service_->CheckPPAPIDownloadRequest(
2058 GURL("http://example.com/foo"), default_file_path, alternate_extensions, 2129 GURL("http://example.com/foo"), default_file_path, alternate_extensions,
2130 profile_.get(),
2059 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2131 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2060 base::Unretained(this))); 2132 base::Unretained(this), run_loop.QuitClosure()));
2061 base::RunLoop().Run(); 2133 run_loop.Run();
2062 2134
2135 // Result should be SAFE, since download matches whitelist but user is not in
2136 // extended reporting group (a.k.a no request sents out).
2063 ASSERT_TRUE(IsResult(DownloadProtectionService::SAFE)); 2137 ASSERT_TRUE(IsResult(DownloadProtectionService::SAFE));
2064 } 2138 }
2065 2139
2140 TEST_F(DownloadProtectionServiceTest,
2141 PPAPIDownloadRequest_WhitelistedURLIncognito) {
2142 net::FakeURLFetcherFactory factory(nullptr);
2143 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx"));
2144 std::vector<base::FilePath::StringType> alternate_extensions;
2145 // Configs "server" side response to DANGEROUS, in case a request is send out.
2146 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
2147 net::URLRequestStatus::SUCCESS);
2148 EXPECT_CALL(*sb_service_->mock_database_manager(),
2149 MatchDownloadWhitelistUrl(_))
2150 .WillRepeatedly(Return(true));
2151 SetExtendedReportingPreference(true);
2152 RunLoop run_loop;
2153 download_service_->CheckPPAPIDownloadRequest(
2154 GURL("http://example.com/foo"), default_file_path, alternate_extensions,
2155 profile_->GetOffTheRecordProfile(),
2156 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2157 base::Unretained(this), run_loop.QuitClosure()));
2158 run_loop.Run();
2159
2160 // Result should still be SAFE, since download matches whitelist and user is
2161 // in incognito mode.
2162 ASSERT_TRUE(IsResult(DownloadProtectionService::SAFE));
2163 }
2164
2165 TEST_F(DownloadProtectionServiceTest,
2166 PPAPIDownloadRequest_SampleWhitelistedURL) {
2167 net::FakeURLFetcherFactory factory(nullptr);
2168 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx"));
2169 std::vector<base::FilePath::StringType> alternate_extensions;
2170 // Configs "server" side response to DANGEROUS.
2171 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
2172 net::URLRequestStatus::SUCCESS);
2173 EXPECT_CALL(*sb_service_->mock_database_manager(),
2174 MatchDownloadWhitelistUrl(_))
2175 .WillRepeatedly(Return(true));
2176 // Sets the sample rate to 1.00 to trigger ClientDownloadRequest.
2177 SetWhitelistedDownloadSampleRate(1.00);
2178 SetExtendedReportingPreference(true);
2179 RunLoop run_loop;
2180 download_service_->CheckPPAPIDownloadRequest(
2181 GURL("http://example.com/foo"), default_file_path, alternate_extensions,
2182 profile_.get(),
2183 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2184 base::Unretained(this), run_loop.QuitClosure()));
2185 run_loop.Run();
2186
2187 // Result should be Dangerous, since we will sample this download and ping SB
2188 // Server (server returns DANGEROUS verdict).
2189 ASSERT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
2190 }
2191
2066 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_FetchFailed) { 2192 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_FetchFailed) {
2067 net::FakeURLFetcherFactory factory(nullptr); 2193 net::FakeURLFetcherFactory factory(nullptr);
2068 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); 2194 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx"));
2069 std::vector<base::FilePath::StringType> alternate_extensions; 2195 std::vector<base::FilePath::StringType> alternate_extensions;
2070 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, 2196 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
2071 net::URLRequestStatus::FAILED); 2197 net::URLRequestStatus::FAILED);
2072 EXPECT_CALL(*sb_service_->mock_database_manager(), 2198 EXPECT_CALL(*sb_service_->mock_database_manager(),
2073 MatchDownloadWhitelistUrl(_)) 2199 MatchDownloadWhitelistUrl(_))
2074 .WillRepeatedly(Return(false)); 2200 .WillRepeatedly(Return(false));
2201 SetExtendedReportingPreference(false);
2202 RunLoop run_loop;
2075 download_service_->CheckPPAPIDownloadRequest( 2203 download_service_->CheckPPAPIDownloadRequest(
2076 GURL("http://example.com/foo"), default_file_path, alternate_extensions, 2204 GURL("http://example.com/foo"), default_file_path, alternate_extensions,
2205 profile_.get(),
2077 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2206 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2078 base::Unretained(this))); 2207 base::Unretained(this), run_loop.QuitClosure()));
2079 base::RunLoop().Run(); 2208 run_loop.Run();
2080 2209
2081 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 2210 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
2082 } 2211 }
2083 2212
2084 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_InvalidResponse) { 2213 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_InvalidResponse) {
2085 net::FakeURLFetcherFactory factory(nullptr); 2214 net::FakeURLFetcherFactory factory(nullptr);
2086 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); 2215 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx"));
2087 std::vector<base::FilePath::StringType> alternate_extensions; 2216 std::vector<base::FilePath::StringType> alternate_extensions;
2088 factory.SetFakeResponse(DownloadProtectionService::GetDownloadRequestUrl(), 2217 factory.SetFakeResponse(DownloadProtectionService::GetDownloadRequestUrl(),
2089 "Hello world!", net::HTTP_OK, 2218 "Hello world!", net::HTTP_OK,
2090 net::URLRequestStatus::SUCCESS); 2219 net::URLRequestStatus::SUCCESS);
2091 EXPECT_CALL(*sb_service_->mock_database_manager(), 2220 EXPECT_CALL(*sb_service_->mock_database_manager(),
2092 MatchDownloadWhitelistUrl(_)) 2221 MatchDownloadWhitelistUrl(_))
2093 .WillRepeatedly(Return(false)); 2222 .WillRepeatedly(Return(false));
2223 SetExtendedReportingPreference(false);
2224 RunLoop run_loop;
2094 download_service_->CheckPPAPIDownloadRequest( 2225 download_service_->CheckPPAPIDownloadRequest(
2095 GURL("http://example.com/foo"), default_file_path, alternate_extensions, 2226 GURL("http://example.com/foo"), default_file_path, alternate_extensions,
2227 profile_.get(),
2096 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2228 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2097 base::Unretained(this))); 2229 base::Unretained(this), run_loop.QuitClosure()));
2098 base::RunLoop().Run(); 2230 run_loop.Run();
2099 2231
2100 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 2232 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
2101 } 2233 }
2102 2234
2103 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_Timeout) { 2235 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_Timeout) {
2104 net::FakeURLFetcherFactory factory(nullptr); 2236 net::FakeURLFetcherFactory factory(nullptr);
2105 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); 2237 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx"));
2106 std::vector<base::FilePath::StringType> alternate_extensions; 2238 std::vector<base::FilePath::StringType> alternate_extensions;
2107 EXPECT_CALL(*sb_service_->mock_database_manager(), 2239 EXPECT_CALL(*sb_service_->mock_database_manager(),
2108 MatchDownloadWhitelistUrl(_)) 2240 MatchDownloadWhitelistUrl(_))
2109 .WillRepeatedly(Return(false)); 2241 .WillRepeatedly(Return(false));
2110 PrepareResponse(&factory, ClientDownloadResponse::SAFE, net::HTTP_OK, 2242 PrepareResponse(&factory, ClientDownloadResponse::SAFE, net::HTTP_OK,
2111 net::URLRequestStatus::SUCCESS); 2243 net::URLRequestStatus::SUCCESS);
2112 download_service_->download_request_timeout_ms_ = 0; 2244 download_service_->download_request_timeout_ms_ = 0;
2245 SetExtendedReportingPreference(false);
2246 RunLoop run_loop;
2113 download_service_->CheckPPAPIDownloadRequest( 2247 download_service_->CheckPPAPIDownloadRequest(
2114 GURL("http://example.com/foo"), default_file_path, alternate_extensions, 2248 GURL("http://example.com/foo"), default_file_path, alternate_extensions,
2249 profile_.get(),
2115 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2250 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2116 base::Unretained(this))); 2251 base::Unretained(this), run_loop.QuitClosure()));
2117 base::RunLoop().Run(); 2252 run_loop.Run();
2118 2253
2119 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); 2254 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
2120 } 2255 }
2121 2256
2122 namespace { 2257 namespace {
2123 2258
2124 std::unique_ptr<net::FakeURLFetcher> FakeURLFetcherCreatorFunc( 2259 std::unique_ptr<net::FakeURLFetcher> FakeURLFetcherCreatorFunc(
2125 std::string* upload_data_receiver, 2260 std::string* upload_data_receiver,
2126 const GURL& url, 2261 const GURL& url,
2127 net::URLFetcherDelegate* delegate, 2262 net::URLFetcherDelegate* delegate,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2163 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); 2298 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx"));
2164 std::vector<base::FilePath::StringType> alternate_extensions{ 2299 std::vector<base::FilePath::StringType> alternate_extensions{
2165 FILE_PATH_LITERAL(".txt"), FILE_PATH_LITERAL(".abc"), 2300 FILE_PATH_LITERAL(".txt"), FILE_PATH_LITERAL(".abc"),
2166 FILE_PATH_LITERAL(""), FILE_PATH_LITERAL(".sdF")}; 2301 FILE_PATH_LITERAL(""), FILE_PATH_LITERAL(".sdF")};
2167 EXPECT_CALL(*sb_service_->mock_database_manager(), 2302 EXPECT_CALL(*sb_service_->mock_database_manager(),
2168 MatchDownloadWhitelistUrl(_)) 2303 MatchDownloadWhitelistUrl(_))
2169 .WillRepeatedly(Return(false)); 2304 .WillRepeatedly(Return(false));
2170 PrepareResponse(&factory, ClientDownloadResponse::SAFE, net::HTTP_OK, 2305 PrepareResponse(&factory, ClientDownloadResponse::SAFE, net::HTTP_OK,
2171 net::URLRequestStatus::SUCCESS); 2306 net::URLRequestStatus::SUCCESS);
2172 const GURL kRequestorUrl("http://example.com/foo"); 2307 const GURL kRequestorUrl("http://example.com/foo");
2308 SetExtendedReportingPreference(false);
2309 RunLoop run_loop;
2173 download_service_->CheckPPAPIDownloadRequest( 2310 download_service_->CheckPPAPIDownloadRequest(
2174 kRequestorUrl, default_file_path, alternate_extensions, 2311 kRequestorUrl, default_file_path, alternate_extensions, profile_.get(),
2175 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2312 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2176 base::Unretained(this))); 2313 base::Unretained(this), run_loop.QuitClosure()));
2177 base::RunLoop().Run(); 2314 run_loop.Run();
2178 2315
2179 ASSERT_FALSE(upload_data.empty()); 2316 ASSERT_FALSE(upload_data.empty());
2180 2317
2181 ClientDownloadRequest request; 2318 ClientDownloadRequest request;
2182 ASSERT_TRUE(request.ParseFromString(upload_data)); 2319 ASSERT_TRUE(request.ParseFromString(upload_data));
2183 2320
2184 EXPECT_EQ(ClientDownloadRequest::PPAPI_SAVE_REQUEST, request.download_type()); 2321 EXPECT_EQ(ClientDownloadRequest::PPAPI_SAVE_REQUEST, request.download_type());
2185 EXPECT_EQ(kRequestorUrl.spec(), request.url()); 2322 EXPECT_EQ(kRequestorUrl.spec(), request.url());
2186 EXPECT_EQ("test.crx", request.file_basename()); 2323 EXPECT_EQ("test.crx", request.file_basename());
2187 ASSERT_EQ(3, request.alternate_extensions_size()); 2324 ASSERT_EQ(3, request.alternate_extensions_size());
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2226 FILE_PATH_LITERAL("a.exe")); // final_path 2363 FILE_PATH_LITERAL("a.exe")); // final_path
2227 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(blacklisted_hash_)); 2364 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(blacklisted_hash_));
2228 EXPECT_CALL(*sb_service_->mock_database_manager(), 2365 EXPECT_CALL(*sb_service_->mock_database_manager(),
2229 MatchDownloadWhitelistUrl(_)) 2366 MatchDownloadWhitelistUrl(_))
2230 .WillRepeatedly(Return(false)); 2367 .WillRepeatedly(Return(false));
2231 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)); 2368 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _));
2232 EXPECT_CALL(*binary_feature_extractor_.get(), 2369 EXPECT_CALL(*binary_feature_extractor_.get(),
2233 ExtractImageFeatures( 2370 ExtractImageFeatures(
2234 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _)); 2371 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _));
2235 2372
2373 RunLoop run_loop;
2236 download_service_->CheckClientDownload( 2374 download_service_->CheckClientDownload(
2237 &item, 2375 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2238 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2376 base::Unretained(this), run_loop.QuitClosure()));
2239 base::Unretained(this))); 2377 run_loop.Run();
2240 base::RunLoop().Run();
2241 2378
2242 EXPECT_FALSE(HasClientDownloadRequest()); 2379 EXPECT_FALSE(HasClientDownloadRequest());
2243 // Overriden by flag: 2380 // Overriden by flag:
2244 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 2381 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
2245 } 2382 }
2246 2383
2247 // Test a real .zip with a real .exe in it, where the .exe is manually 2384 // Test a real .zip with a real .exe in it, where the .exe is manually
2248 // blacklisted by hash. 2385 // blacklisted by hash.
2249 TEST_F(DownloadProtectionServiceFlagTest, 2386 TEST_F(DownloadProtectionServiceFlagTest,
2250 CheckClientDownloadZipOverridenByFlag) { 2387 CheckClientDownloadZipOverridenByFlag) {
2251 content::MockDownloadItem item; 2388 content::MockDownloadItem item;
2252 2389
2253 PrepareBasicDownloadItemWithFullPaths( 2390 PrepareBasicDownloadItemWithFullPaths(
2254 &item, {"http://www.evil.com/a.exe"}, // url_chain 2391 &item, {"http://www.evil.com/a.exe"}, // url_chain
2255 "http://www.google.com/", // referrer 2392 "http://www.google.com/", // referrer
2256 testdata_path_.AppendASCII( 2393 testdata_path_.AppendASCII(
2257 "zipfile_one_unsigned_binary.zip"), // tmp_path 2394 "zipfile_one_unsigned_binary.zip"), // tmp_path
2258 temp_dir_.path().Append(FILE_PATH_LITERAL("a.zip"))); // final_path 2395 temp_dir_.path().Append(FILE_PATH_LITERAL("a.zip"))); // final_path
2259 2396
2260 EXPECT_CALL(*sb_service_->mock_database_manager(), 2397 EXPECT_CALL(*sb_service_->mock_database_manager(),
2261 MatchDownloadWhitelistUrl(_)) 2398 MatchDownloadWhitelistUrl(_))
2262 .WillRepeatedly(Return(false)); 2399 .WillRepeatedly(Return(false));
2263 2400
2401 RunLoop run_loop;
2264 download_service_->CheckClientDownload( 2402 download_service_->CheckClientDownload(
2265 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2403 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2266 base::Unretained(this))); 2404 base::Unretained(this), run_loop.QuitClosure()));
2267 base::RunLoop().Run(); 2405 run_loop.Run();
2268 2406
2269 EXPECT_FALSE(HasClientDownloadRequest()); 2407 EXPECT_FALSE(HasClientDownloadRequest());
2270 // Overriden by flag: 2408 // Overriden by flag:
2271 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 2409 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
2272 } 2410 }
2273 2411
2274 } // namespace safe_browsing 2412 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/download_protection_service.cc ('k') | chrome/test/ppapi/ppapi_filechooser_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698