| 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 20 matching lines...) Expand all Loading... |
| 58 using ::testing::ReturnRef; | 60 using ::testing::ReturnRef; |
| 59 using ::testing::SaveArg; | 61 using ::testing::SaveArg; |
| 60 using ::testing::StrictMock; | 62 using ::testing::StrictMock; |
| 61 using ::testing::_; | 63 using ::testing::_; |
| 62 using base::MessageLoop; | 64 using base::MessageLoop; |
| 63 using content::BrowserThread; | 65 using content::BrowserThread; |
| 64 namespace safe_browsing { | 66 namespace safe_browsing { |
| 65 namespace { | 67 namespace { |
| 66 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for | 68 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for |
| 67 // a given URL. | 69 // a given URL. |
| 68 class MockSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager { | 70 class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { |
| 69 public: | 71 public: |
| 70 explicit MockSafeBrowsingDatabaseManager(SafeBrowsingService* service) | 72 MockSafeBrowsingDatabaseManager() {} |
| 71 : SafeBrowsingDatabaseManager(service) { } | |
| 72 | 73 |
| 73 MOCK_METHOD1(MatchDownloadWhitelistUrl, bool(const GURL&)); | 74 MOCK_METHOD1(MatchDownloadWhitelistUrl, bool(const GURL&)); |
| 74 MOCK_METHOD1(MatchDownloadWhitelistString, bool(const std::string&)); | 75 MOCK_METHOD1(MatchDownloadWhitelistString, bool(const std::string&)); |
| 75 MOCK_METHOD2(CheckDownloadUrl, bool( | 76 MOCK_METHOD2(CheckDownloadUrl, bool( |
| 76 const std::vector<GURL>& url_chain, | 77 const std::vector<GURL>& url_chain, |
| 77 SafeBrowsingDatabaseManager::Client* client)); | 78 SafeBrowsingDatabaseManager::Client* client)); |
| 78 | 79 |
| 79 private: | 80 private: |
| 80 virtual ~MockSafeBrowsingDatabaseManager() {} | 81 virtual ~MockSafeBrowsingDatabaseManager() {} |
| 81 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); | 82 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); |
| 82 }; | 83 }; |
| 83 | 84 |
| 84 class FakeSafeBrowsingService : public SafeBrowsingService { | 85 class FakeSafeBrowsingService : public SafeBrowsingService { |
| 85 public: | 86 public: |
| 86 FakeSafeBrowsingService() { } | 87 FakeSafeBrowsingService() { } |
| 87 | 88 |
| 88 // Returned pointer has the same lifespan as the database_manager_ refcounted | 89 // Returned pointer has the same lifespan as the database_manager_ refcounted |
| 89 // object. | 90 // object. |
| 90 MockSafeBrowsingDatabaseManager* mock_database_manager() { | 91 MockSafeBrowsingDatabaseManager* mock_database_manager() { |
| 91 return mock_database_manager_; | 92 return mock_database_manager_; |
| 92 } | 93 } |
| 93 | 94 |
| 94 protected: | 95 protected: |
| 95 ~FakeSafeBrowsingService() override {} | 96 ~FakeSafeBrowsingService() override {} |
| 96 | 97 |
| 97 SafeBrowsingDatabaseManager* CreateDatabaseManager() override { | 98 SafeBrowsingDatabaseManager* CreateDatabaseManager() override { |
| 98 mock_database_manager_ = new MockSafeBrowsingDatabaseManager(this); | 99 mock_database_manager_ = new MockSafeBrowsingDatabaseManager(); |
| 99 return mock_database_manager_; | 100 return mock_database_manager_; |
| 100 } | 101 } |
| 101 | 102 |
| 102 void RegisterAllDelayedAnalysis() override {} | 103 void RegisterAllDelayedAnalysis() override {} |
| 103 | 104 |
| 104 private: | 105 private: |
| 105 MockSafeBrowsingDatabaseManager* mock_database_manager_; | 106 MockSafeBrowsingDatabaseManager* mock_database_manager_; |
| 106 | 107 |
| 107 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService); | 108 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService); |
| 108 }; | 109 }; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 arg1->add_certificate_chain(); | 175 arg1->add_certificate_chain(); |
| 175 chain->add_element()->set_certificate(cert_data); | 176 chain->add_element()->set_certificate(cert_data); |
| 176 chain->add_element()->set_certificate(cert_data); | 177 chain->add_element()->set_certificate(cert_data); |
| 177 } | 178 } |
| 178 | 179 |
| 179 // We can't call OnSafeBrowsingResult directly because SafeBrowsingCheck does | 180 // We can't call OnSafeBrowsingResult directly because SafeBrowsingCheck does |
| 180 // 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 |
| 181 // easily. Note: check will be deleted automatically when the callback is | 182 // easily. Note: check will be deleted automatically when the callback is |
| 182 // deleted. | 183 // deleted. |
| 183 void OnSafeBrowsingResult( | 184 void OnSafeBrowsingResult( |
| 184 SafeBrowsingDatabaseManager::SafeBrowsingCheck* check) { | 185 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check) { |
| 185 check->client->OnSafeBrowsingResult(*check); | 186 check->OnSafeBrowsingResult(); |
| 186 } | 187 } |
| 187 | 188 |
| 188 ACTION_P(CheckDownloadUrlDone, threat_type) { | 189 ACTION_P(CheckDownloadUrlDone, threat_type) { |
| 189 SafeBrowsingDatabaseManager::SafeBrowsingCheck* check = | 190 // TODO(nparker): Remove use of SafeBrowsingCheck and instead call |
| 190 new SafeBrowsingDatabaseManager::SafeBrowsingCheck( | 191 // client->OnCheckDownloadUrlResult(..) directly. |
| 192 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check = |
| 193 new LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck( |
| 191 arg0, | 194 arg0, |
| 192 std::vector<SBFullHash>(), | 195 std::vector<SBFullHash>(), |
| 193 arg1, | 196 arg1, |
| 194 safe_browsing_util::BINURL, | 197 safe_browsing_util::BINURL, |
| 195 std::vector<SBThreatType>(1, SB_THREAT_TYPE_BINARY_MALWARE_URL)); | 198 std::vector<SBThreatType>(1, SB_THREAT_TYPE_BINARY_MALWARE_URL)); |
| 196 for (size_t i = 0; i < check->url_results.size(); ++i) | 199 for (size_t i = 0; i < check->url_results.size(); ++i) |
| 197 check->url_results[i] = threat_type; | 200 check->url_results[i] = threat_type; |
| 198 BrowserThread::PostTask(BrowserThread::IO, | 201 BrowserThread::PostTask(BrowserThread::IO, |
| 199 FROM_HERE, | 202 FROM_HERE, |
| 200 base::Bind(&OnSafeBrowsingResult, | 203 base::Bind(&OnSafeBrowsingResult, |
| (...skipping 1761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1962 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); | 1965 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); |
| 1963 | 1966 |
| 1964 cert = ReadTestCertificate("test_c.pem"); | 1967 cert = ReadTestCertificate("test_c.pem"); |
| 1965 ASSERT_TRUE(cert.get()); | 1968 ASSERT_TRUE(cert.get()); |
| 1966 whitelist_strings.clear(); | 1969 whitelist_strings.clear(); |
| 1967 GetCertificateWhitelistStrings( | 1970 GetCertificateWhitelistStrings( |
| 1968 *cert.get(), *issuer_cert.get(), &whitelist_strings); | 1971 *cert.get(), *issuer_cert.get(), &whitelist_strings); |
| 1969 EXPECT_THAT(whitelist_strings, ElementsAre()); | 1972 EXPECT_THAT(whitelist_strings, ElementsAre()); |
| 1970 } | 1973 } |
| 1971 } // namespace safe_browsing | 1974 } // namespace safe_browsing |
| OLD | NEW |