| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/base_paths.h" | 11 #include "base/base_paths.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
| 20 #include "base/path_service.h" | 20 #include "base/path_service.h" |
| 21 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
| 22 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 23 #include "base/threading/sequenced_worker_pool.h" | 23 #include "base/threading/sequenced_worker_pool.h" |
| 24 #include "chrome/browser/history/history_service_factory.h" | 24 #include "chrome/browser/history/history_service_factory.h" |
| 25 #include "chrome/browser/safe_browsing/database_manager.h" | 25 #include "chrome/browser/safe_browsing/database_manager.h" |
| 26 #include "chrome/browser/safe_browsing/download_feedback_service.h" | 26 #include "chrome/browser/safe_browsing/download_feedback_service.h" |
| 27 #include "chrome/browser/safe_browsing/local_database_manager.h" |
| 27 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 28 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 29 #include "chrome/browser/safe_browsing/test_database_manager.h" |
| 28 #include "chrome/common/safe_browsing/binary_feature_extractor.h" | 30 #include "chrome/common/safe_browsing/binary_feature_extractor.h" |
| 29 #include "chrome/common/safe_browsing/csd.pb.h" | 31 #include "chrome/common/safe_browsing/csd.pb.h" |
| 30 #include "chrome/test/base/testing_profile.h" | 32 #include "chrome/test/base/testing_profile.h" |
| 31 #include "components/history/core/browser/history_service.h" | 33 #include "components/history/core/browser/history_service.h" |
| 32 #include "content/public/test/mock_download_item.h" | 34 #include "content/public/test/mock_download_item.h" |
| 33 #include "content/public/test/test_browser_thread_bundle.h" | 35 #include "content/public/test/test_browser_thread_bundle.h" |
| 34 #include "content/public/test/test_utils.h" | 36 #include "content/public/test/test_utils.h" |
| 35 #include "net/cert/x509_certificate.h" | 37 #include "net/cert/x509_certificate.h" |
| 36 #include "net/http/http_status_code.h" | 38 #include "net/http/http_status_code.h" |
| 37 #include "net/url_request/test_url_fetcher_factory.h" | 39 #include "net/url_request/test_url_fetcher_factory.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 53 using ::testing::ReturnRef; | 55 using ::testing::ReturnRef; |
| 54 using ::testing::SaveArg; | 56 using ::testing::SaveArg; |
| 55 using ::testing::StrictMock; | 57 using ::testing::StrictMock; |
| 56 using ::testing::_; | 58 using ::testing::_; |
| 57 using base::MessageLoop; | 59 using base::MessageLoop; |
| 58 using content::BrowserThread; | 60 using content::BrowserThread; |
| 59 namespace safe_browsing { | 61 namespace safe_browsing { |
| 60 namespace { | 62 namespace { |
| 61 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for | 63 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for |
| 62 // a given URL. | 64 // a given URL. |
| 63 class MockSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager { | 65 class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { |
| 64 public: | 66 public: |
| 65 explicit MockSafeBrowsingDatabaseManager(SafeBrowsingService* service) | 67 MockSafeBrowsingDatabaseManager() {} |
| 66 : SafeBrowsingDatabaseManager(service) { } | |
| 67 | 68 |
| 68 MOCK_METHOD1(MatchDownloadWhitelistUrl, bool(const GURL&)); | 69 MOCK_METHOD1(MatchDownloadWhitelistUrl, bool(const GURL&)); |
| 69 MOCK_METHOD1(MatchDownloadWhitelistString, bool(const std::string&)); | 70 MOCK_METHOD1(MatchDownloadWhitelistString, bool(const std::string&)); |
| 70 MOCK_METHOD2(CheckDownloadUrl, bool( | 71 MOCK_METHOD2(CheckDownloadUrl, bool( |
| 71 const std::vector<GURL>& url_chain, | 72 const std::vector<GURL>& url_chain, |
| 72 SafeBrowsingDatabaseManager::Client* client)); | 73 SafeBrowsingDatabaseManager::Client* client)); |
| 73 | 74 |
| 74 private: | 75 private: |
| 75 virtual ~MockSafeBrowsingDatabaseManager() {} | 76 virtual ~MockSafeBrowsingDatabaseManager() {} |
| 76 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); | 77 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); |
| 77 }; | 78 }; |
| 78 | 79 |
| 79 class FakeSafeBrowsingService : public SafeBrowsingService { | 80 class FakeSafeBrowsingService : public SafeBrowsingService { |
| 80 public: | 81 public: |
| 81 FakeSafeBrowsingService() { } | 82 FakeSafeBrowsingService() { } |
| 82 | 83 |
| 83 // Returned pointer has the same lifespan as the database_manager_ refcounted | 84 // Returned pointer has the same lifespan as the database_manager_ refcounted |
| 84 // object. | 85 // object. |
| 85 MockSafeBrowsingDatabaseManager* mock_database_manager() { | 86 MockSafeBrowsingDatabaseManager* mock_database_manager() { |
| 86 return mock_database_manager_; | 87 return mock_database_manager_; |
| 87 } | 88 } |
| 88 | 89 |
| 89 protected: | 90 protected: |
| 90 ~FakeSafeBrowsingService() override {} | 91 ~FakeSafeBrowsingService() override {} |
| 91 | 92 |
| 92 SafeBrowsingDatabaseManager* CreateDatabaseManager() override { | 93 SafeBrowsingDatabaseManager* CreateDatabaseManager() override { |
| 93 mock_database_manager_ = new MockSafeBrowsingDatabaseManager(this); | 94 mock_database_manager_ = new MockSafeBrowsingDatabaseManager(); |
| 94 return mock_database_manager_; | 95 return mock_database_manager_; |
| 95 } | 96 } |
| 96 | 97 |
| 98 SafeBrowsingProtocolManagerDelegate* GetProtocolManagerDelegate() override { |
| 99 // Our SafeBrowsingDatabaseManager doesn't implement this delegate. |
| 100 return NULL; |
| 101 } |
| 102 |
| 97 void RegisterAllDelayedAnalysis() override {} | 103 void RegisterAllDelayedAnalysis() override {} |
| 98 | 104 |
| 99 private: | 105 private: |
| 100 MockSafeBrowsingDatabaseManager* mock_database_manager_; | 106 MockSafeBrowsingDatabaseManager* mock_database_manager_; |
| 101 | 107 |
| 102 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService); | 108 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService); |
| 103 }; | 109 }; |
| 104 | 110 |
| 105 class MockBinaryFeatureExtractor : public BinaryFeatureExtractor { | 111 class MockBinaryFeatureExtractor : public BinaryFeatureExtractor { |
| 106 public: | 112 public: |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 arg1->add_certificate_chain(); | 175 arg1->add_certificate_chain(); |
| 170 chain->add_element()->set_certificate(cert_data); | 176 chain->add_element()->set_certificate(cert_data); |
| 171 chain->add_element()->set_certificate(cert_data); | 177 chain->add_element()->set_certificate(cert_data); |
| 172 } | 178 } |
| 173 | 179 |
| 174 // We can't call OnSafeBrowsingResult directly because SafeBrowsingCheck does | 180 // We can't call OnSafeBrowsingResult directly because SafeBrowsingCheck does |
| 175 // not have any copy constructor which means it can't be stored in a callback | 181 // not have any copy constructor which means it can't be stored in a callback |
| 176 // easily. Note: check will be deleted automatically when the callback is | 182 // easily. Note: check will be deleted automatically when the callback is |
| 177 // deleted. | 183 // deleted. |
| 178 void OnSafeBrowsingResult( | 184 void OnSafeBrowsingResult( |
| 179 SafeBrowsingDatabaseManager::SafeBrowsingCheck* check) { | 185 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check) { |
| 180 check->client->OnSafeBrowsingResult(*check); | 186 check->OnSafeBrowsingResult(); |
| 181 } | 187 } |
| 182 | 188 |
| 183 ACTION_P(CheckDownloadUrlDone, threat_type) { | 189 ACTION_P(CheckDownloadUrlDone, threat_type) { |
| 184 SafeBrowsingDatabaseManager::SafeBrowsingCheck* check = | 190 // TODO(nparker): Remove use of SafeBrowsingCheck and instead call |
| 185 new SafeBrowsingDatabaseManager::SafeBrowsingCheck( | 191 // client->OnCheckDownloadUrlResult(..) directly. |
| 192 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check = |
| 193 new LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck( |
| 186 arg0, | 194 arg0, |
| 187 std::vector<SBFullHash>(), | 195 std::vector<SBFullHash>(), |
| 188 arg1, | 196 arg1, |
| 189 safe_browsing_util::BINURL, | 197 safe_browsing_util::BINURL, |
| 190 std::vector<SBThreatType>(1, SB_THREAT_TYPE_BINARY_MALWARE_URL)); | 198 std::vector<SBThreatType>(1, SB_THREAT_TYPE_BINARY_MALWARE_URL)); |
| 191 for (size_t i = 0; i < check->url_results.size(); ++i) | 199 for (size_t i = 0; i < check->url_results.size(); ++i) |
| 192 check->url_results[i] = threat_type; | 200 check->url_results[i] = threat_type; |
| 193 BrowserThread::PostTask(BrowserThread::IO, | 201 BrowserThread::PostTask(BrowserThread::IO, |
| 194 FROM_HERE, | 202 FROM_HERE, |
| 195 base::Bind(&OnSafeBrowsingResult, | 203 base::Bind(&OnSafeBrowsingResult, |
| (...skipping 1649 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1845 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); | 1853 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); |
| 1846 | 1854 |
| 1847 cert = ReadTestCertificate("test_c.pem"); | 1855 cert = ReadTestCertificate("test_c.pem"); |
| 1848 ASSERT_TRUE(cert.get()); | 1856 ASSERT_TRUE(cert.get()); |
| 1849 whitelist_strings.clear(); | 1857 whitelist_strings.clear(); |
| 1850 GetCertificateWhitelistStrings( | 1858 GetCertificateWhitelistStrings( |
| 1851 *cert.get(), *issuer_cert.get(), &whitelist_strings); | 1859 *cert.get(), *issuer_cert.get(), &whitelist_strings); |
| 1852 EXPECT_THAT(whitelist_strings, ElementsAre()); | 1860 EXPECT_THAT(whitelist_strings, ElementsAre()); |
| 1853 } | 1861 } |
| 1854 } // namespace safe_browsing | 1862 } // namespace safe_browsing |
| OLD | NEW |