OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/safe_browsing/v4_test_utils.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| 9 #include "crypto/sha2.h" |
| 10 |
| 11 namespace safe_browsing { |
| 12 |
| 13 TestV4Store::TestV4Store( |
| 14 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 15 const base::FilePath& store_path) |
| 16 : V4Store(task_runner, store_path, 0) {} |
| 17 |
| 18 bool TestV4Store::HasValidData() const { |
| 19 return true; |
| 20 } |
| 21 |
| 22 void TestV4Store::MarkPrefixAsBad(HashPrefix prefix) { |
| 23 hash_prefix_map_[prefix.size()] = prefix; |
| 24 } |
| 25 |
| 26 TestV4Database::TestV4Database( |
| 27 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 28 std::unique_ptr<StoreMap> store_map) |
| 29 : V4Database(db_task_runner, std::move(store_map)) {} |
| 30 |
| 31 void TestV4Database::MarkPrefixAsBad(ListIdentifier list_id, |
| 32 HashPrefix prefix) { |
| 33 V4Store* base_store = store_map_->at(list_id).get(); |
| 34 TestV4Store* test_store = static_cast<TestV4Store*>(base_store); |
| 35 test_store->MarkPrefixAsBad(prefix); |
| 36 } |
| 37 |
| 38 V4Store* TestV4StoreFactory::CreateV4Store( |
| 39 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 40 const base::FilePath& store_path) { |
| 41 V4Store* new_store = new TestV4Store(task_runner, store_path); |
| 42 new_store->Initialize(); |
| 43 return new_store; |
| 44 } |
| 45 |
| 46 TestV4DatabaseFactory::TestV4DatabaseFactory() : v4_db_(nullptr) {} |
| 47 |
| 48 std::unique_ptr<V4Database> TestV4DatabaseFactory::Create( |
| 49 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 50 std::unique_ptr<StoreMap> store_map) { |
| 51 v4_db_ = new TestV4Database(db_task_runner, std::move(store_map)); |
| 52 return base::WrapUnique(v4_db_); |
| 53 } |
| 54 |
| 55 void TestV4DatabaseFactory::MarkPrefixAsBad(ListIdentifier list_id, |
| 56 HashPrefix prefix) { |
| 57 v4_db_->MarkPrefixAsBad(list_id, prefix); |
| 58 } |
| 59 |
| 60 TestV4GetHashProtocolManager::TestV4GetHashProtocolManager( |
| 61 net::URLRequestContextGetter* request_context_getter, |
| 62 const StoresToCheck& stores_to_check, |
| 63 const V4ProtocolConfig& config) |
| 64 : V4GetHashProtocolManager(request_context_getter, |
| 65 stores_to_check, |
| 66 config) {} |
| 67 |
| 68 void TestV4GetHashProtocolManager::AddToFullHashCache(FullHashInfo fhi) { |
| 69 full_hash_cache_[fhi.full_hash].full_hash_infos.push_back(fhi); |
| 70 } |
| 71 |
| 72 std::unique_ptr<V4GetHashProtocolManager> |
| 73 TestV4GetHashProtocolManagerFactory::CreateProtocolManager( |
| 74 net::URLRequestContextGetter* request_context_getter, |
| 75 const StoresToCheck& stores_to_check, |
| 76 const V4ProtocolConfig& config) { |
| 77 pm_ = new TestV4GetHashProtocolManager(request_context_getter, |
| 78 stores_to_check, config); |
| 79 return base::WrapUnique(pm_); |
| 80 } |
| 81 |
| 82 FullHash GetFullHash(const GURL& url) { |
| 83 std::string host; |
| 84 std::string path; |
| 85 V4ProtocolManagerUtil::CanonicalizeUrl(url, &host, &path, nullptr); |
| 86 |
| 87 return crypto::SHA256HashString(host + path); |
| 88 } |
| 89 |
| 90 FullHashInfo GetFullHashInfo(const GURL& url, const ListIdentifier& list_id) { |
| 91 return FullHashInfo(GetFullHash(url), list_id, |
| 92 base::Time::Now() + base::TimeDelta::FromMinutes(5)); |
| 93 } |
| 94 |
| 95 FullHashInfo GetFullHashInfoWithMetadata( |
| 96 const GURL& url, |
| 97 const ListIdentifier& list_id, |
| 98 ThreatPatternType threat_pattern_type) { |
| 99 FullHashInfo fhi = GetFullHashInfo(url, list_id); |
| 100 fhi.metadata.threat_pattern_type = threat_pattern_type; |
| 101 return fhi; |
| 102 } |
| 103 |
| 104 } // namespace safe_browsing |
OLD | NEW |