OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/test_simple_task_runner.h" |
10 #include "components/safe_browsing_db/v4_database.h" | 10 #include "components/safe_browsing_db/v4_database.h" |
11 #include "components/safe_browsing_db/v4_local_database_manager.h" | 11 #include "components/safe_browsing_db/v4_local_database_manager.h" |
12 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "net/url_request/test_url_fetcher_factory.h" |
13 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
14 | 15 |
15 namespace safe_browsing { | 16 namespace safe_browsing { |
16 | 17 |
17 class FakeV4Database : public V4Database { | 18 class FakeV4Database : public V4Database { |
18 public: | 19 public: |
19 FakeV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 20 FakeV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
20 std::unique_ptr<StoreMap> store_map, | 21 std::unique_ptr<StoreMap> store_map, |
21 const MatchedHashPrefixMap& matched_hash_prefix_map) | 22 const StoreAndHashPrefixes& store_and_hash_prefixes) |
22 : V4Database(db_task_runner, std::move(store_map)), | 23 : V4Database(db_task_runner, std::move(store_map)), |
23 matched_hash_prefix_map_(matched_hash_prefix_map) {} | 24 store_and_hash_prefixes_(store_and_hash_prefixes) {} |
24 | 25 |
25 void GetStoresMatchingFullHash( | 26 void GetStoresMatchingFullHash( |
26 const FullHash& full_hash, | 27 const FullHash& full_hash, |
27 const base::hash_set<UpdateListIdentifier>& stores_to_look, | 28 const base::hash_set<UpdateListIdentifier>& stores_to_look, |
28 MatchedHashPrefixMap* matched_hash_prefix_map) override { | 29 StoreAndHashPrefixes* store_and_hash_prefixes) override { |
29 *matched_hash_prefix_map = matched_hash_prefix_map_; | 30 *store_and_hash_prefixes = store_and_hash_prefixes_; |
30 } | 31 } |
31 | 32 |
32 private: | 33 private: |
33 const MatchedHashPrefixMap& matched_hash_prefix_map_; | 34 const StoreAndHashPrefixes& store_and_hash_prefixes_; |
34 }; | 35 }; |
35 | 36 |
36 class V4LocalDatabaseManagerTest : public PlatformTest { | 37 class V4LocalDatabaseManagerTest : public PlatformTest { |
37 public: | 38 public: |
38 V4LocalDatabaseManagerTest() : task_runner_(new base::TestSimpleTaskRunner) {} | 39 V4LocalDatabaseManagerTest() : task_runner_(new base::TestSimpleTaskRunner) {} |
39 | 40 |
40 void SetUp() override { | 41 void SetUp() override { |
41 PlatformTest::SetUp(); | 42 PlatformTest::SetUp(); |
42 | 43 |
43 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); | 44 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); |
(...skipping 16 matching lines...) Expand all Loading... |
60 PlatformTest::TearDown(); | 61 PlatformTest::TearDown(); |
61 } | 62 } |
62 | 63 |
63 void SetupLocalDatabaseManager() { | 64 void SetupLocalDatabaseManager() { |
64 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); | 65 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); |
65 | 66 |
66 task_runner_->RunPendingTasks(); | 67 task_runner_->RunPendingTasks(); |
67 base::RunLoop().RunUntilIdle(); | 68 base::RunLoop().RunUntilIdle(); |
68 } | 69 } |
69 | 70 |
70 void ReplaceV4Database(const MatchedHashPrefixMap& matched_hash_prefix_map) { | 71 void ReplaceV4Database(const StoreAndHashPrefixes& store_and_hash_prefixes) { |
71 v4_local_database_manager_->v4_database_.reset(new FakeV4Database( | 72 v4_local_database_manager_->v4_database_.reset(new FakeV4Database( |
72 task_runner_, base::MakeUnique<StoreMap>(), matched_hash_prefix_map)); | 73 task_runner_, base::MakeUnique<StoreMap>(), store_and_hash_prefixes)); |
73 } | 74 } |
74 | 75 |
75 void ForceDisableLocalDatabaseManager() { | 76 void ForceDisableLocalDatabaseManager() { |
76 v4_local_database_manager_->enabled_ = false; | 77 v4_local_database_manager_->enabled_ = false; |
77 } | 78 } |
78 | 79 |
79 base::ScopedTempDir base_dir_; | 80 base::ScopedTempDir base_dir_; |
80 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 81 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
81 content::TestBrowserThreadBundle thread_bundle_; | 82 content::TestBrowserThreadBundle thread_bundle_; |
82 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_; | 83 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_; |
(...skipping 20 matching lines...) Expand all Loading... |
103 } | 104 } |
104 | 105 |
105 TEST_F(V4LocalDatabaseManagerTest, | 106 TEST_F(V4LocalDatabaseManagerTest, |
106 TestCheckBrowseUrlWithEmptyStoresReturnsNoMatch) { | 107 TestCheckBrowseUrlWithEmptyStoresReturnsNoMatch) { |
107 // Both the stores are empty right now so CheckBrowseUrl should return true. | 108 // Both the stores are empty right now so CheckBrowseUrl should return true. |
108 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( | 109 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( |
109 GURL("http://example.com/a/"), nullptr)); | 110 GURL("http://example.com/a/"), nullptr)); |
110 } | 111 } |
111 | 112 |
112 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) { | 113 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) { |
113 MatchedHashPrefixMap matched_hash_prefix_map; | 114 net::TestURLFetcherFactory factory; |
114 matched_hash_prefix_map[GetUrlMalwareId()] = HashPrefix("aaaa"); | 115 |
115 ReplaceV4Database(matched_hash_prefix_map); | 116 StoreAndHashPrefixes store_and_hash_prefixes; |
| 117 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); |
| 118 ReplaceV4Database(store_and_hash_prefixes); |
116 | 119 |
117 // The fake database returns a matched hash prefix. | 120 // The fake database returns a matched hash prefix. |
118 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( | 121 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( |
119 GURL("http://example.com/a/"), nullptr)); | 122 GURL("http://example.com/a/"), nullptr)); |
120 } | 123 } |
121 | 124 |
122 TEST_F(V4LocalDatabaseManagerTest, | 125 TEST_F(V4LocalDatabaseManagerTest, |
123 TestCheckBrowseUrlReturnsNoMatchWhenDisabled) { | 126 TestCheckBrowseUrlReturnsNoMatchWhenDisabled) { |
124 MatchedHashPrefixMap matched_hash_prefix_map; | 127 StoreAndHashPrefixes store_and_hash_prefixes; |
125 matched_hash_prefix_map[GetUrlMalwareId()] = HashPrefix("aaaa"); | 128 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); |
126 ReplaceV4Database(matched_hash_prefix_map); | 129 ReplaceV4Database(store_and_hash_prefixes); |
127 | 130 |
128 // The same URL returns |false| in the previous test because | 131 // The same URL returns |false| in the previous test because |
129 // v4_local_database_manager_ is enabled. | 132 // v4_local_database_manager_ is enabled. |
130 ForceDisableLocalDatabaseManager(); | 133 ForceDisableLocalDatabaseManager(); |
131 | 134 |
132 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( | 135 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( |
133 GURL("http://example.com/a/"), nullptr)); | 136 GURL("http://example.com/a/"), nullptr)); |
134 } | 137 } |
135 | 138 |
136 } // namespace safe_browsing | 139 } // namespace safe_browsing |
OLD | NEW |