OLD | NEW |
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> |
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 | 471 |
472 void OnClientDownloadRequest(content::DownloadItem* download, | 472 void OnClientDownloadRequest(content::DownloadItem* download, |
473 const ClientDownloadRequest* request) { | 473 const ClientDownloadRequest* request) { |
474 if (request) | 474 if (request) |
475 last_client_download_request_.reset(new ClientDownloadRequest(*request)); | 475 last_client_download_request_.reset(new ClientDownloadRequest(*request)); |
476 else | 476 else |
477 last_client_download_request_.reset(); | 477 last_client_download_request_.reset(); |
478 } | 478 } |
479 | 479 |
480 public: | 480 public: |
| 481 enum ArchiveType { ZIP, DMG }; |
| 482 |
481 void CheckDoneCallback( | 483 void CheckDoneCallback( |
482 DownloadProtectionService::DownloadCheckResult result) { | 484 DownloadProtectionService::DownloadCheckResult result) { |
483 result_ = result; | 485 result_ = result; |
484 has_result_ = true; | 486 has_result_ = true; |
485 MessageLoop::current()->QuitWhenIdle(); | 487 MessageLoop::current()->QuitWhenIdle(); |
486 } | 488 } |
487 | 489 |
488 void SyncCheckDoneCallback( | 490 void SyncCheckDoneCallback( |
489 DownloadProtectionService::DownloadCheckResult result) { | 491 DownloadProtectionService::DownloadCheckResult result) { |
490 result_ = result; | 492 result_ = result; |
(...skipping 16 matching lines...) Expand all Loading... |
507 } | 509 } |
508 | 510 |
509 void SetExtendedReportingPreference(bool is_extended_reporting) { | 511 void SetExtendedReportingPreference(bool is_extended_reporting) { |
510 profile_->GetPrefs()->SetBoolean( | 512 profile_->GetPrefs()->SetBoolean( |
511 prefs::kSafeBrowsingExtendedReportingEnabled, | 513 prefs::kSafeBrowsingExtendedReportingEnabled, |
512 is_extended_reporting); | 514 is_extended_reporting); |
513 } | 515 } |
514 | 516 |
515 // Check scenarios where we should/shouldn't send a report for | 517 // Check scenarios where we should/shouldn't send a report for |
516 // a corrupted zip. | 518 // a corrupted zip. |
517 void CheckClientDownloadReportCorruptZip(bool is_extended_reporting, | 519 void CheckClientDownloadReportCorruptArchive(ArchiveType type, |
518 bool is_incognito); | 520 bool is_extended_reporting, |
519 | 521 bool is_incognito); |
520 | 522 |
521 protected: | 523 protected: |
522 // This will effectivly mask the global Singleton while this is in scope. | 524 // This will effectivly mask the global Singleton while this is in scope. |
523 FileTypePoliciesTestOverlay policies_; | 525 FileTypePoliciesTestOverlay policies_; |
524 | 526 |
525 scoped_refptr<FakeSafeBrowsingService> sb_service_; | 527 scoped_refptr<FakeSafeBrowsingService> sb_service_; |
526 scoped_refptr<MockBinaryFeatureExtractor> binary_feature_extractor_; | 528 scoped_refptr<MockBinaryFeatureExtractor> binary_feature_extractor_; |
527 DownloadProtectionService* download_service_; | 529 DownloadProtectionService* download_service_; |
528 DownloadProtectionService::DownloadCheckResult result_; | 530 DownloadProtectionService::DownloadCheckResult result_; |
529 bool has_result_; | 531 bool has_result_; |
530 content::TestBrowserThreadBundle test_browser_thread_bundle_; | 532 content::TestBrowserThreadBundle test_browser_thread_bundle_; |
531 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_; | 533 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_; |
532 base::FilePath testdata_path_; | 534 base::FilePath testdata_path_; |
533 DownloadProtectionService::ClientDownloadRequestSubscription | 535 DownloadProtectionService::ClientDownloadRequestSubscription |
534 client_download_request_subscription_; | 536 client_download_request_subscription_; |
535 std::unique_ptr<ClientDownloadRequest> last_client_download_request_; | 537 std::unique_ptr<ClientDownloadRequest> last_client_download_request_; |
536 base::ScopedTempDir profile_dir_; | 538 base::ScopedTempDir profile_dir_; |
537 std::unique_ptr<TestingProfile> profile_; | 539 std::unique_ptr<TestingProfile> profile_; |
538 // The following 5 fields are used by PrepareBasicDownloadItem() function to | 540 // The following 5 fields are used by PrepareBasicDownloadItem() function to |
539 // store attributes of the last download item. | 541 // store attributes of the last download item. |
540 std::vector<GURL> url_chain_; | 542 std::vector<GURL> url_chain_; |
541 GURL referrer_; | 543 GURL referrer_; |
542 base::FilePath tmp_path_; | 544 base::FilePath tmp_path_; |
543 base::FilePath final_path_; | 545 base::FilePath final_path_; |
544 std::string hash_; | 546 std::string hash_; |
545 base::ScopedTempDir temp_dir_; | 547 base::ScopedTempDir temp_dir_; |
546 }; | 548 }; |
547 | 549 |
548 | 550 void DownloadProtectionServiceTest::CheckClientDownloadReportCorruptArchive( |
549 void DownloadProtectionServiceTest::CheckClientDownloadReportCorruptZip( | 551 ArchiveType type, |
550 bool is_extended_reporting, | 552 bool is_extended_reporting, |
551 bool is_incognito) { | 553 bool is_incognito) { |
552 net::FakeURLFetcherFactory factory(NULL); | 554 net::FakeURLFetcherFactory factory(NULL); |
553 PrepareResponse( | 555 PrepareResponse( |
554 &factory, ClientDownloadResponse::SAFE, net::HTTP_OK, | 556 &factory, ClientDownloadResponse::SAFE, net::HTTP_OK, |
555 net::URLRequestStatus::SUCCESS); | 557 net::URLRequestStatus::SUCCESS); |
556 | 558 |
557 profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled, | 559 profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled, |
558 is_extended_reporting); | 560 is_extended_reporting); |
559 content::MockDownloadItem item; | 561 content::MockDownloadItem item; |
560 PrepareBasicDownloadItem( | 562 if (type == ZIP) { |
561 &item, | 563 PrepareBasicDownloadItem(&item, {"http://www.evil.com/a.zip"}, // url_chain |
562 {"http://www.evil.com/a.zip"}, // url_chain | 564 "http://www.google.com/", // referrer |
563 "http://www.google.com/", // referrer | 565 FILE_PATH_LITERAL("a.tmp"), // tmp_path |
564 FILE_PATH_LITERAL("a.tmp"), // tmp_path | 566 FILE_PATH_LITERAL("a.zip")); // final_path |
565 FILE_PATH_LITERAL("a.zip")); // final_path | 567 } else if (type == DMG) { |
| 568 PrepareBasicDownloadItem(&item, {"http://www.evil.com/a.dmg"}, // url_chain |
| 569 "http://www.google.com/", // referrer |
| 570 FILE_PATH_LITERAL("a.tmp"), // tmp_path |
| 571 FILE_PATH_LITERAL("a.dmg")); // final_path |
| 572 } |
566 | 573 |
567 if (is_incognito) { | 574 if (is_incognito) { |
568 EXPECT_CALL(item, GetBrowserContext()) | 575 EXPECT_CALL(item, GetBrowserContext()) |
569 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile())); | 576 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile())); |
570 } else { | 577 } else { |
571 EXPECT_CALL(item, GetBrowserContext()) | 578 EXPECT_CALL(item, GetBrowserContext()) |
572 .WillRepeatedly(Return(profile_.get())); | 579 .WillRepeatedly(Return(profile_.get())); |
573 } | 580 } |
574 | 581 |
575 std::string file_contents = "corrupt zip file"; | 582 std::string file_contents = "corrupt archive file"; |
576 ASSERT_EQ(static_cast<int>(file_contents.size()), base::WriteFile( | 583 ASSERT_EQ(static_cast<int>(file_contents.size()), base::WriteFile( |
577 tmp_path_, file_contents.data(), file_contents.size())); | 584 tmp_path_, file_contents.data(), file_contents.size())); |
578 | 585 |
579 download_service_->CheckClientDownload( | 586 download_service_->CheckClientDownload( |
580 &item, | 587 &item, |
581 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 588 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
582 base::Unretained(this))); | 589 base::Unretained(this))); |
583 MessageLoop::current()->Run(); | 590 MessageLoop::current()->Run(); |
584 | 591 |
585 const bool expect_request = is_extended_reporting && !is_incognito; | 592 const bool expect_request = |
| 593 type == ZIP ? is_extended_reporting && !is_incognito : true; |
586 | 594 |
587 if (expect_request) { | 595 if (expect_request) { |
588 ASSERT_TRUE(HasClientDownloadRequest()); | 596 ASSERT_TRUE(HasClientDownloadRequest()); |
589 EXPECT_EQ(0, GetClientDownloadRequest()->archived_binary_size()); | 597 EXPECT_EQ(0, GetClientDownloadRequest()->archived_binary_size()); |
590 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); | 598 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); |
591 EXPECT_EQ(ClientDownloadRequest_DownloadType_INVALID_ZIP, | 599 ClientDownloadRequest::DownloadType expected_type = |
592 GetClientDownloadRequest()->download_type()); | 600 type == ZIP ? ClientDownloadRequest_DownloadType_INVALID_ZIP |
| 601 : ClientDownloadRequest_DownloadType_INVALID_MAC_ARCHIVE; |
| 602 EXPECT_EQ(expected_type, GetClientDownloadRequest()->download_type()); |
593 ClearClientDownloadRequest(); | 603 ClearClientDownloadRequest(); |
594 } else { | 604 } else { |
595 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); | 605 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
596 EXPECT_FALSE(HasClientDownloadRequest()); | 606 EXPECT_FALSE(HasClientDownloadRequest()); |
597 } | 607 } |
598 | 608 |
599 Mock::VerifyAndClearExpectations(sb_service_.get()); | 609 Mock::VerifyAndClearExpectations(sb_service_.get()); |
600 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); | 610 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); |
601 } | 611 } |
602 | 612 |
603 | |
604 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { | 613 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { |
605 content::MockDownloadItem item; | 614 content::MockDownloadItem item; |
606 PrepareBasicDownloadItem( | 615 PrepareBasicDownloadItem( |
607 &item, | 616 &item, |
608 std::vector<std::string>(), // empty url_chain | 617 std::vector<std::string>(), // empty url_chain |
609 "http://www.google.com/", // referrer | 618 "http://www.google.com/", // referrer |
610 FILE_PATH_LITERAL("a.tmp"), // tmp_path | 619 FILE_PATH_LITERAL("a.tmp"), // tmp_path |
611 FILE_PATH_LITERAL("a.exe")); // final_path | 620 FILE_PATH_LITERAL("a.exe")); // final_path |
612 download_service_->CheckClientDownload( | 621 download_service_->CheckClientDownload( |
613 &item, | 622 &item, |
(...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1323 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); | 1332 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); |
1324 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_ARCHIVE, | 1333 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_ARCHIVE, |
1325 GetClientDownloadRequest()->download_type()); | 1334 GetClientDownloadRequest()->download_type()); |
1326 ClearClientDownloadRequest(); | 1335 ClearClientDownloadRequest(); |
1327 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); | 1336 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); |
1328 } | 1337 } |
1329 | 1338 |
1330 TEST_F(DownloadProtectionServiceTest, | 1339 TEST_F(DownloadProtectionServiceTest, |
1331 CheckClientDownloadReportCorruptZipNormal) { | 1340 CheckClientDownloadReportCorruptZipNormal) { |
1332 // !is_extended_reporting && !is_incognito | 1341 // !is_extended_reporting && !is_incognito |
1333 CheckClientDownloadReportCorruptZip(false, false); | 1342 CheckClientDownloadReportCorruptArchive(ZIP, false, false); |
1334 } | 1343 } |
1335 | 1344 |
1336 TEST_F(DownloadProtectionServiceTest, | 1345 TEST_F(DownloadProtectionServiceTest, |
1337 CheckClientDownloadReportCorruptZipExtended) { | 1346 CheckClientDownloadReportCorruptZipExtended) { |
1338 // !is_extended_reporting && !is_incognito | 1347 // !is_extended_reporting && !is_incognito |
1339 CheckClientDownloadReportCorruptZip(true, false); | 1348 CheckClientDownloadReportCorruptArchive(ZIP, true, false); |
1340 } | 1349 } |
1341 | 1350 |
1342 TEST_F(DownloadProtectionServiceTest, | 1351 TEST_F(DownloadProtectionServiceTest, |
1343 CheckClientDownloadReportCorruptZipIncognito) { | 1352 CheckClientDownloadReportCorruptZipIncognito) { |
1344 // is_extended_reporting && is_incognito | 1353 // is_extended_reporting && is_incognito |
1345 CheckClientDownloadReportCorruptZip(true, true); | 1354 CheckClientDownloadReportCorruptArchive(ZIP, true, true); |
1346 } | 1355 } |
1347 | 1356 |
| 1357 #if defined(OS_MACOSX) |
| 1358 TEST_F(DownloadProtectionServiceTest, |
| 1359 CheckClientDownloadReportCorruptDmgNormal) { |
| 1360 // !is_extended_reporting && !is_incognito |
| 1361 CheckClientDownloadReportCorruptArchive(DMG, false, false); |
| 1362 } |
| 1363 |
| 1364 TEST_F(DownloadProtectionServiceTest, |
| 1365 CheckClientDownloadReportCorruptDmgExtended) { |
| 1366 // !is_extended_reporting && !is_incognito |
| 1367 CheckClientDownloadReportCorruptArchive(DMG, true, false); |
| 1368 } |
| 1369 |
| 1370 TEST_F(DownloadProtectionServiceTest, |
| 1371 CheckClientDownloadReportCorruptDmgIncognito) { |
| 1372 // is_extended_reporting && is_incognito |
| 1373 CheckClientDownloadReportCorruptArchive(DMG, true, true); |
| 1374 } |
| 1375 #endif |
| 1376 |
1348 TEST_F(DownloadProtectionServiceTest, CheckClientCrxDownloadSuccess) { | 1377 TEST_F(DownloadProtectionServiceTest, CheckClientCrxDownloadSuccess) { |
1349 // Even if the server verdict is dangerous we should return SAFE because | 1378 // Even if the server verdict is dangerous we should return SAFE because |
1350 // DownloadProtectionService::IsSupportedDownload() will return false | 1379 // DownloadProtectionService::IsSupportedDownload() will return false |
1351 // for crx downloads. | 1380 // for crx downloads. |
1352 net::FakeURLFetcherFactory factory(NULL); | 1381 net::FakeURLFetcherFactory factory(NULL); |
1353 PrepareResponse( | 1382 PrepareResponse( |
1354 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, | 1383 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, |
1355 net::URLRequestStatus::SUCCESS); | 1384 net::URLRequestStatus::SUCCESS); |
1356 | 1385 |
1357 content::MockDownloadItem item; | 1386 content::MockDownloadItem item; |
(...skipping 897 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2255 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2284 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
2256 base::Unretained(this))); | 2285 base::Unretained(this))); |
2257 MessageLoop::current()->Run(); | 2286 MessageLoop::current()->Run(); |
2258 | 2287 |
2259 EXPECT_FALSE(HasClientDownloadRequest()); | 2288 EXPECT_FALSE(HasClientDownloadRequest()); |
2260 // Overriden by flag: | 2289 // Overriden by flag: |
2261 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); | 2290 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
2262 } | 2291 } |
2263 | 2292 |
2264 } // namespace safe_browsing | 2293 } // namespace safe_browsing |
OLD | NEW |