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 <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 #if defined(OS_MACOSX) | 44 #if defined(OS_MACOSX) |
45 #include "base/metrics/field_trial.h" | 45 #include "base/metrics/field_trial.h" |
46 #include "components/variations/entropy_provider.h" | 46 #include "components/variations/entropy_provider.h" |
47 #endif | 47 #endif |
48 | 48 |
49 using ::testing::Assign; | 49 using ::testing::Assign; |
50 using ::testing::ContainerEq; | 50 using ::testing::ContainerEq; |
51 using ::testing::DoAll; | 51 using ::testing::DoAll; |
52 using ::testing::ElementsAre; | 52 using ::testing::ElementsAre; |
| 53 using ::testing::Invoke; |
53 using ::testing::Mock; | 54 using ::testing::Mock; |
54 using ::testing::NotNull; | 55 using ::testing::NotNull; |
55 using ::testing::Return; | 56 using ::testing::Return; |
56 using ::testing::ReturnRef; | 57 using ::testing::ReturnRef; |
57 using ::testing::SaveArg; | 58 using ::testing::SaveArg; |
58 using ::testing::StrictMock; | 59 using ::testing::StrictMock; |
59 using ::testing::_; | 60 using ::testing::_; |
60 using base::MessageLoop; | 61 using base::MessageLoop; |
61 using content::BrowserThread; | 62 using content::BrowserThread; |
62 namespace safe_browsing { | 63 namespace safe_browsing { |
(...skipping 1529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1592 EXPECT_FALSE(HasClientDownloadRequest()); | 1593 EXPECT_FALSE(HasClientDownloadRequest()); |
1593 #endif | 1594 #endif |
1594 } | 1595 } |
1595 | 1596 |
1596 TEST_F(DownloadProtectionServiceTest, TestDownloadItemDestroyed) { | 1597 TEST_F(DownloadProtectionServiceTest, TestDownloadItemDestroyed) { |
1597 net::TestURLFetcherFactory factory; | 1598 net::TestURLFetcherFactory factory; |
1598 | 1599 |
1599 std::vector<GURL> url_chain; | 1600 std::vector<GURL> url_chain; |
1600 url_chain.push_back(GURL("http://www.evil.com/bla.exe")); | 1601 url_chain.push_back(GURL("http://www.evil.com/bla.exe")); |
1601 GURL referrer("http://www.google.com/"); | 1602 GURL referrer("http://www.google.com/"); |
| 1603 GURL tab_url("http://www.google.com/tab"); |
1602 base::FilePath tmp_path(FILE_PATH_LITERAL("a.tmp")); | 1604 base::FilePath tmp_path(FILE_PATH_LITERAL("a.tmp")); |
1603 base::FilePath final_path(FILE_PATH_LITERAL("a.exe")); | 1605 base::FilePath final_path(FILE_PATH_LITERAL("a.exe")); |
1604 std::string hash = "hash"; | 1606 std::string hash = "hash"; |
1605 | 1607 |
1606 { | 1608 { |
1607 content::MockDownloadItem item; | 1609 content::MockDownloadItem item; |
1608 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path)); | 1610 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path)); |
1609 EXPECT_CALL(item, GetTargetFilePath()) | 1611 EXPECT_CALL(item, GetTargetFilePath()) |
1610 .WillRepeatedly(ReturnRef(final_path)); | 1612 .WillRepeatedly(ReturnRef(final_path)); |
1611 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); | 1613 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); |
1612 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); | 1614 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); |
1613 EXPECT_CALL(item, GetTabUrl()).WillRepeatedly(ReturnRef(GURL::EmptyGURL())); | 1615 EXPECT_CALL(item, GetTabUrl()).WillRepeatedly(ReturnRef(tab_url)); |
1614 EXPECT_CALL(item, GetTabReferrerUrl()) | 1616 EXPECT_CALL(item, GetTabReferrerUrl()) |
1615 .WillRepeatedly(ReturnRef(GURL::EmptyGURL())); | 1617 .WillRepeatedly(ReturnRef(GURL::EmptyGURL())); |
1616 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash)); | 1618 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash)); |
1617 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100)); | 1619 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100)); |
1618 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true)); | 1620 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true)); |
1619 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return("")); | 1621 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return("")); |
1620 | 1622 |
1621 EXPECT_CALL(*sb_service_->mock_database_manager(), | 1623 EXPECT_CALL(*sb_service_->mock_database_manager(), |
1622 MatchDownloadWhitelistUrl(_)) | 1624 MatchDownloadWhitelistUrl(_)) |
1623 .WillRepeatedly(Return(false)); | 1625 .WillRepeatedly(Return(false)); |
1624 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path, _)); | 1626 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path, _)); |
1625 EXPECT_CALL(*binary_feature_extractor_.get(), | 1627 EXPECT_CALL(*binary_feature_extractor_.get(), |
1626 ExtractImageHeaders(tmp_path, _)); | 1628 ExtractImageHeaders(tmp_path, _)); |
1627 | 1629 |
1628 download_service_->CheckClientDownload( | 1630 download_service_->CheckClientDownload( |
1629 &item, | 1631 &item, |
1630 base::Bind(&DownloadProtectionServiceTest::SyncCheckDoneCallback, | 1632 base::Bind(&DownloadProtectionServiceTest::SyncCheckDoneCallback, |
1631 base::Unretained(this))); | 1633 base::Unretained(this))); |
1632 // MockDownloadItem going out of scope triggers the OnDownloadDestroyed | 1634 // MockDownloadItem going out of scope triggers the OnDownloadDestroyed |
1633 // notification. | 1635 // notification. |
1634 } | 1636 } |
1635 | 1637 |
1636 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); | 1638 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
1637 EXPECT_FALSE(HasClientDownloadRequest()); | 1639 EXPECT_FALSE(HasClientDownloadRequest()); |
1638 } | 1640 } |
1639 | 1641 |
| 1642 TEST_F(DownloadProtectionServiceTest, |
| 1643 TestDownloadItemDestroyedDuringWhitelistCheck) { |
| 1644 net::TestURLFetcherFactory factory; |
| 1645 |
| 1646 std::vector<GURL> url_chain; |
| 1647 url_chain.push_back(GURL("http://www.evil.com/bla.exe")); |
| 1648 GURL referrer("http://www.google.com/"); |
| 1649 GURL tab_url("http://www.google.com/tab"); |
| 1650 base::FilePath tmp_path(FILE_PATH_LITERAL("a.tmp")); |
| 1651 base::FilePath final_path(FILE_PATH_LITERAL("a.exe")); |
| 1652 std::string hash = "hash"; |
| 1653 |
| 1654 scoped_ptr<content::MockDownloadItem> item(new content::MockDownloadItem); |
| 1655 EXPECT_CALL(*item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path)); |
| 1656 EXPECT_CALL(*item, GetTargetFilePath()) |
| 1657 .WillRepeatedly(ReturnRef(final_path)); |
| 1658 EXPECT_CALL(*item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); |
| 1659 EXPECT_CALL(*item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); |
| 1660 EXPECT_CALL(*item, GetTabUrl()).WillRepeatedly(ReturnRef(tab_url)); |
| 1661 EXPECT_CALL(*item, GetTabReferrerUrl()) |
| 1662 .WillRepeatedly(ReturnRef(GURL::EmptyGURL())); |
| 1663 EXPECT_CALL(*item, GetHash()).WillRepeatedly(ReturnRef(hash)); |
| 1664 EXPECT_CALL(*item, GetReceivedBytes()).WillRepeatedly(Return(100)); |
| 1665 EXPECT_CALL(*item, HasUserGesture()).WillRepeatedly(Return(true)); |
| 1666 EXPECT_CALL(*item, GetRemoteAddress()).WillRepeatedly(Return("")); |
| 1667 |
| 1668 EXPECT_CALL(*sb_service_->mock_database_manager(), |
| 1669 MatchDownloadWhitelistUrl(_)) |
| 1670 .WillRepeatedly(Invoke([&item](const GURL&) { |
| 1671 item.reset(); |
| 1672 return false; |
| 1673 })); |
| 1674 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path, _)); |
| 1675 EXPECT_CALL(*binary_feature_extractor_.get(), |
| 1676 ExtractImageHeaders(tmp_path, _)); |
| 1677 |
| 1678 download_service_->CheckClientDownload( |
| 1679 item.get(), |
| 1680 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 1681 base::Unretained(this))); |
| 1682 |
| 1683 MessageLoop::current()->Run(); |
| 1684 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
| 1685 EXPECT_FALSE(HasClientDownloadRequest()); |
| 1686 } |
| 1687 |
1640 TEST_F(DownloadProtectionServiceTest, GetCertificateWhitelistStrings) { | 1688 TEST_F(DownloadProtectionServiceTest, GetCertificateWhitelistStrings) { |
1641 // We'll pass this cert in as the "issuer", even though it isn't really | 1689 // We'll pass this cert in as the "issuer", even though it isn't really |
1642 // used to sign the certs below. GetCertificateWhitelistStirngs doesn't care | 1690 // used to sign the certs below. GetCertificateWhitelistStirngs doesn't care |
1643 // about this. | 1691 // about this. |
1644 scoped_refptr<net::X509Certificate> issuer_cert( | 1692 scoped_refptr<net::X509Certificate> issuer_cert( |
1645 ReadTestCertificate("issuer.pem")); | 1693 ReadTestCertificate("issuer.pem")); |
1646 ASSERT_TRUE(issuer_cert.get()); | 1694 ASSERT_TRUE(issuer_cert.get()); |
1647 std::string cert_base = "cert/" + base::HexEncode( | 1695 std::string cert_base = "cert/" + base::HexEncode( |
1648 issuer_cert->fingerprint().data, | 1696 issuer_cert->fingerprint().data, |
1649 sizeof(issuer_cert->fingerprint().data)); | 1697 sizeof(issuer_cert->fingerprint().data)); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1716 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); | 1764 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); |
1717 | 1765 |
1718 cert = ReadTestCertificate("test_c.pem"); | 1766 cert = ReadTestCertificate("test_c.pem"); |
1719 ASSERT_TRUE(cert.get()); | 1767 ASSERT_TRUE(cert.get()); |
1720 whitelist_strings.clear(); | 1768 whitelist_strings.clear(); |
1721 GetCertificateWhitelistStrings( | 1769 GetCertificateWhitelistStrings( |
1722 *cert.get(), *issuer_cert.get(), &whitelist_strings); | 1770 *cert.get(), *issuer_cert.get(), &whitelist_strings); |
1723 EXPECT_THAT(whitelist_strings, ElementsAre()); | 1771 EXPECT_THAT(whitelist_strings, ElementsAre()); |
1724 } | 1772 } |
1725 } // namespace safe_browsing | 1773 } // namespace safe_browsing |
OLD | NEW |