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

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

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