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 "components/safe_browsing_db/v4_database.h" | |
10 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" | |
mattm
2017/02/17 21:49:53
Don't need to duplicate includes that are in the v
melandory
2017/02/27 15:01:48
Done.
| |
11 #include "crypto/sha2.h" | |
12 | |
13 namespace safe_browsing { | |
14 | |
15 TestV4Store::TestV4Store( | |
16 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
17 const base::FilePath& store_path) | |
18 : V4Store(task_runner, store_path, 0) {} | |
19 | |
20 bool TestV4Store::HasValidData() const { | |
21 return true; | |
22 } | |
23 | |
24 void TestV4Store::MarkPrefixAsBad(HashPrefix prefix) { | |
25 hash_prefix_map_[prefix.size()] = prefix; | |
26 } | |
27 | |
28 TestV4Database::TestV4Database( | |
29 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | |
30 std::unique_ptr<StoreMap> store_map) | |
31 : V4Database(db_task_runner, std::move(store_map)) {} | |
32 | |
33 void TestV4Database::MarkPrefixAsBad(ListIdentifier list_id, | |
34 HashPrefix prefix) { | |
35 V4Store* base_store = store_map_->at(list_id).get(); | |
36 TestV4Store* test_store = static_cast<TestV4Store*>(base_store); | |
37 test_store->MarkPrefixAsBad(prefix); | |
38 } | |
39 | |
40 V4Store* TestV4StoreFactory::CreateV4Store( | |
41 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
42 const base::FilePath& store_path) { | |
43 V4Store* new_store = new TestV4Store(task_runner, store_path); | |
44 new_store->Initialize(); | |
45 return new_store; | |
46 } | |
47 | |
48 TestV4DatabaseFactory::TestV4DatabaseFactory() : v4_db_(nullptr) {} | |
49 | |
50 std::unique_ptr<V4Database> TestV4DatabaseFactory::Create( | |
51 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | |
52 std::unique_ptr<StoreMap> store_map) { | |
53 v4_db_ = new TestV4Database(db_task_runner, std::move(store_map)); | |
54 return base::WrapUnique(v4_db_); | |
55 } | |
56 | |
57 void TestV4DatabaseFactory::MarkPrefixAsBad(ListIdentifier list_id, | |
58 HashPrefix prefix) { | |
59 v4_db_->MarkPrefixAsBad(list_id, prefix); | |
60 } | |
61 | |
62 TestV4GetHashProtocolManager::TestV4GetHashProtocolManager( | |
63 net::URLRequestContextGetter* request_context_getter, | |
64 const StoresToCheck& stores_to_check, | |
65 const V4ProtocolConfig& config) | |
66 : V4GetHashProtocolManager(request_context_getter, | |
67 stores_to_check, | |
68 config) {} | |
69 | |
70 void TestV4GetHashProtocolManager::AddToFullHashCache(FullHashInfo fhi) { | |
71 full_hash_cache_[fhi.full_hash].full_hash_infos.push_back(fhi); | |
72 } | |
73 | |
74 std::unique_ptr<V4GetHashProtocolManager> | |
75 TestV4GetHashProtocolManagerFactory::CreateProtocolManager( | |
76 net::URLRequestContextGetter* request_context_getter, | |
77 const StoresToCheck& stores_to_check, | |
78 const V4ProtocolConfig& config) { | |
79 pm_ = new TestV4GetHashProtocolManager(request_context_getter, | |
80 stores_to_check, config); | |
81 return base::WrapUnique(pm_); | |
82 } | |
83 | |
84 FullHash GetFullHash(const GURL& url) { | |
85 std::string host; | |
86 std::string path; | |
87 V4ProtocolManagerUtil::CanonicalizeUrl(url, &host, &path, nullptr); | |
88 | |
89 return crypto::SHA256HashString(host + path); | |
90 } | |
91 | |
92 FullHashInfo GetFullHashInfo(const GURL& url, const ListIdentifier& list_id) { | |
93 return FullHashInfo(GetFullHash(url), list_id, | |
94 base::Time::Now() + base::TimeDelta::FromMinutes(5)); | |
95 } | |
96 | |
97 FullHashInfo GetFullHashInfoWithMetadata( | |
98 const GURL& url, | |
99 const ListIdentifier& list_id, | |
100 ThreatPatternType threat_pattern_type) { | |
101 FullHashInfo fhi = GetFullHashInfo(url, list_id); | |
102 fhi.metadata.threat_pattern_type = threat_pattern_type; | |
103 return fhi; | |
104 } | |
105 | |
106 } // namespace safe_browsing | |
OLD | NEW |