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 "net/url_request/test_url_fetcher_factory.h" |
14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
15 | 15 |
16 namespace safe_browsing { | 16 namespace safe_browsing { |
17 | 17 |
18 class FakeV4Database : public V4Database { | 18 class FakeV4Database : public V4Database { |
19 public: | 19 public: |
20 FakeV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 20 static void Create( |
21 std::unique_ptr<StoreMap> store_map, | 21 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
22 const StoreAndHashPrefixes& store_and_hash_prefixes) | 22 std::unique_ptr<StoreMap> store_map, |
23 : V4Database(db_task_runner, std::move(store_map)), | 23 const StoreAndHashPrefixes& store_and_hash_prefixes, |
24 store_and_hash_prefixes_(store_and_hash_prefixes) {} | 24 NewDatabaseReadyCallback new_db_callback) { |
25 // Mimics V4Database::Create | |
26 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner = | |
27 base::MessageLoop::current()->task_runner(); | |
28 db_task_runner->PostTask( | |
29 FROM_HERE, | |
30 base::Bind(&FakeV4Database::CreateOnTaskRunner, db_task_runner, | |
31 base::Passed(&store_map), store_and_hash_prefixes, | |
32 callback_task_runner, new_db_callback)); | |
33 } | |
25 | 34 |
26 void GetStoresMatchingFullHash( | 35 void GetStoresMatchingFullHash( |
27 const FullHash& full_hash, | 36 const FullHash& full_hash, |
28 const StoresToCheck& stores_to_check, | 37 const StoresToCheck& stores_to_check, |
29 StoreAndHashPrefixes* store_and_hash_prefixes) override { | 38 StoreAndHashPrefixes* store_and_hash_prefixes) override { |
30 *store_and_hash_prefixes = store_and_hash_prefixes_; | 39 *store_and_hash_prefixes = store_and_hash_prefixes_; |
31 } | 40 } |
32 | 41 |
33 private: | 42 private: |
43 static void CreateOnTaskRunner( | |
44 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | |
45 std::unique_ptr<StoreMap> store_map, | |
46 const StoreAndHashPrefixes& store_and_hash_prefixes, | |
47 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | |
48 NewDatabaseReadyCallback new_db_callback) { | |
49 // Mimics the semantics of V4Database::CreateOnTaskRunner | |
50 std::unique_ptr<FakeV4Database> fake_v4_database(new FakeV4Database( | |
51 db_task_runner, std::move(store_map), store_and_hash_prefixes)); | |
52 callback_task_runner->PostTask( | |
53 FROM_HERE, | |
54 base::Bind(new_db_callback, base::Passed(&fake_v4_database))); | |
55 } | |
56 | |
57 FakeV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | |
58 std::unique_ptr<StoreMap> store_map, | |
59 const StoreAndHashPrefixes& store_and_hash_prefixes) | |
60 : V4Database(db_task_runner, std::move(store_map)), | |
61 store_and_hash_prefixes_(store_and_hash_prefixes) {} | |
62 | |
34 const StoreAndHashPrefixes& store_and_hash_prefixes_; | 63 const StoreAndHashPrefixes& store_and_hash_prefixes_; |
35 }; | 64 }; |
36 | 65 |
37 class TestClient : public SafeBrowsingDatabaseManager::Client { | 66 class TestClient : public SafeBrowsingDatabaseManager::Client { |
38 public: | 67 public: |
39 TestClient(SBThreatType sb_threat_type, const GURL& url) | 68 TestClient(SBThreatType sb_threat_type, const GURL& url) |
40 : expected_sb_threat_type(sb_threat_type), expected_url(url) {} | 69 : expected_sb_threat_type(sb_threat_type), expected_url(url) {} |
41 | 70 |
42 void OnCheckBrowseUrlResult(const GURL& url, | 71 void OnCheckBrowseUrlResult(const GURL& url, |
43 SBThreatType threat_type, | 72 SBThreatType threat_type, |
(...skipping 26 matching lines...) Expand all Loading... | |
70 void TearDown() override { | 99 void TearDown() override { |
71 StopLocalDatabaseManager(); | 100 StopLocalDatabaseManager(); |
72 | 101 |
73 PlatformTest::TearDown(); | 102 PlatformTest::TearDown(); |
74 } | 103 } |
75 | 104 |
76 void ForceDisableLocalDatabaseManager() { | 105 void ForceDisableLocalDatabaseManager() { |
77 v4_local_database_manager_->enabled_ = false; | 106 v4_local_database_manager_->enabled_ = false; |
78 } | 107 } |
79 | 108 |
109 void ForceEnableLocalDatabaseManager() { | |
110 v4_local_database_manager_->enabled_ = true; | |
111 } | |
112 | |
80 const V4LocalDatabaseManager::QueuedChecks& GetQueuedChecks() { | 113 const V4LocalDatabaseManager::QueuedChecks& GetQueuedChecks() { |
81 return v4_local_database_manager_->queued_checks_; | 114 return v4_local_database_manager_->queued_checks_; |
82 } | 115 } |
83 | 116 |
84 void ReplaceV4Database(const StoreAndHashPrefixes& store_and_hash_prefixes) { | 117 void ReplaceV4Database(const StoreAndHashPrefixes& store_and_hash_prefixes) { |
85 v4_local_database_manager_->v4_database_.reset(new FakeV4Database( | 118 ForceDisableLocalDatabaseManager(); |
Nathan Parker
2016/10/07 23:24:29
Are the disable/enables necessary? I guess I'm not
vakh (use Gerrit instead)
2016/10/10 17:42:33
Done.
| |
86 task_runner_, base::MakeUnique<StoreMap>(), store_and_hash_prefixes)); | 119 ResetV4Database(); |
120 | |
121 ForceEnableLocalDatabaseManager(); | |
122 NewDatabaseReadyCallback db_ready_callback = | |
123 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForChecks, | |
124 base::Unretained(v4_local_database_manager_.get())); | |
125 FakeV4Database::Create(task_runner_, base::MakeUnique<StoreMap>(), | |
126 store_and_hash_prefixes, db_ready_callback); | |
87 } | 127 } |
88 | 128 |
89 void ResetV4Database() { v4_local_database_manager_->v4_database_.reset(); } | 129 void ResetV4Database() { |
130 V4Database::Destroy(std::move(v4_local_database_manager_->v4_database_)); | |
131 } | |
90 | 132 |
91 void StartLocalDatabaseManager() { | 133 void StartLocalDatabaseManager() { |
92 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); | 134 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); |
93 | |
94 task_runner_->RunPendingTasks(); | |
95 base::RunLoop().RunUntilIdle(); | |
96 } | 135 } |
97 | 136 |
98 void StopLocalDatabaseManager() { | 137 void StopLocalDatabaseManager() { |
99 v4_local_database_manager_->StopOnIOThread(true); | 138 v4_local_database_manager_->StopOnIOThread(true); |
100 | 139 |
101 // Force destruction of the database. | 140 // Force destruction of the database. |
102 task_runner_->RunPendingTasks(); | 141 task_runner_->RunPendingTasks(); |
103 base::RunLoop().RunUntilIdle(); | 142 base::RunLoop().RunUntilIdle(); |
104 } | 143 } |
105 | 144 |
145 void WaitForLocalDatabaseManagerToBeSetup() { | |
146 task_runner_->RunPendingTasks(); | |
147 base::RunLoop().RunUntilIdle(); | |
148 } | |
149 | |
106 base::ScopedTempDir base_dir_; | 150 base::ScopedTempDir base_dir_; |
107 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 151 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
108 content::TestBrowserThreadBundle thread_bundle_; | 152 content::TestBrowserThreadBundle thread_bundle_; |
109 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_; | 153 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_; |
110 }; | 154 }; |
111 | 155 |
112 TEST_F(V4LocalDatabaseManagerTest, TestGetThreatSource) { | 156 TEST_F(V4LocalDatabaseManagerTest, TestGetThreatSource) { |
113 EXPECT_EQ(ThreatSource::LOCAL_PVER4, | 157 EXPECT_EQ(ThreatSource::LOCAL_PVER4, |
114 v4_local_database_manager_->GetThreatSource()); | 158 v4_local_database_manager_->GetThreatSource()); |
115 } | 159 } |
116 | 160 |
117 TEST_F(V4LocalDatabaseManagerTest, TestIsSupported) { | 161 TEST_F(V4LocalDatabaseManagerTest, TestIsSupported) { |
118 EXPECT_TRUE(v4_local_database_manager_->IsSupported()); | 162 EXPECT_TRUE(v4_local_database_manager_->IsSupported()); |
119 } | 163 } |
120 | 164 |
121 TEST_F(V4LocalDatabaseManagerTest, TestCanCheckUrl) { | 165 TEST_F(V4LocalDatabaseManagerTest, TestCanCheckUrl) { |
122 EXPECT_TRUE( | 166 EXPECT_TRUE( |
123 v4_local_database_manager_->CanCheckUrl(GURL("http://example.com/a/"))); | 167 v4_local_database_manager_->CanCheckUrl(GURL("http://example.com/a/"))); |
124 EXPECT_TRUE( | 168 EXPECT_TRUE( |
125 v4_local_database_manager_->CanCheckUrl(GURL("https://example.com/a/"))); | 169 v4_local_database_manager_->CanCheckUrl(GURL("https://example.com/a/"))); |
126 EXPECT_TRUE( | 170 EXPECT_TRUE( |
127 v4_local_database_manager_->CanCheckUrl(GURL("ftp://example.com/a/"))); | 171 v4_local_database_manager_->CanCheckUrl(GURL("ftp://example.com/a/"))); |
128 EXPECT_FALSE( | 172 EXPECT_FALSE( |
129 v4_local_database_manager_->CanCheckUrl(GURL("adp://example.com/a/"))); | 173 v4_local_database_manager_->CanCheckUrl(GURL("adp://example.com/a/"))); |
130 } | 174 } |
131 | 175 |
132 TEST_F(V4LocalDatabaseManagerTest, | 176 TEST_F(V4LocalDatabaseManagerTest, |
133 TestCheckBrowseUrlWithEmptyStoresReturnsNoMatch) { | 177 TestCheckBrowseUrlWithEmptyStoresReturnsNoMatch) { |
178 WaitForLocalDatabaseManagerToBeSetup(); | |
134 // Both the stores are empty right now so CheckBrowseUrl should return true. | 179 // Both the stores are empty right now so CheckBrowseUrl should return true. |
135 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( | 180 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( |
136 GURL("http://example.com/a/"), nullptr)); | 181 GURL("http://example.com/a/"), nullptr)); |
137 } | 182 } |
138 | 183 |
139 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) { | 184 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) { |
140 net::TestURLFetcherFactory factory; | 185 net::TestURLFetcherFactory factory; |
141 | 186 |
142 StoreAndHashPrefixes store_and_hash_prefixes; | 187 StoreAndHashPrefixes store_and_hash_prefixes; |
143 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); | 188 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 &result_threat_type, &metadata, fhis); | 231 &result_threat_type, &metadata, fhis); |
187 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type); | 232 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type); |
188 EXPECT_EQ("malware_popid", metadata.population_id); | 233 EXPECT_EQ("malware_popid", metadata.population_id); |
189 } | 234 } |
190 | 235 |
191 TEST_F(V4LocalDatabaseManagerTest, TestChecksAreQueued) { | 236 TEST_F(V4LocalDatabaseManagerTest, TestChecksAreQueued) { |
192 const GURL url("https://www.example.com/"); | 237 const GURL url("https://www.example.com/"); |
193 TestClient client(SB_THREAT_TYPE_SAFE, url); | 238 TestClient client(SB_THREAT_TYPE_SAFE, url); |
194 EXPECT_TRUE(GetQueuedChecks().empty()); | 239 EXPECT_TRUE(GetQueuedChecks().empty()); |
195 v4_local_database_manager_->CheckBrowseUrl(url, &client); | 240 v4_local_database_manager_->CheckBrowseUrl(url, &client); |
196 // The database is available so the check shouldn't get queued. | |
197 EXPECT_TRUE(GetQueuedChecks().empty()); | |
198 | |
199 ResetV4Database(); | |
200 v4_local_database_manager_->CheckBrowseUrl(url, &client); | |
201 // The database is unavailable so the check should get queued. | 241 // The database is unavailable so the check should get queued. |
202 EXPECT_EQ(1ul, GetQueuedChecks().size()); | 242 EXPECT_EQ(1ul, GetQueuedChecks().size()); |
203 | 243 |
204 // The following function calls StartOnIOThread which should load the | 244 // The following function waits for the DB to load. |
205 // database from disk and cause the queued check to be performed. | 245 WaitForLocalDatabaseManagerToBeSetup(); |
206 StartLocalDatabaseManager(); | |
207 EXPECT_TRUE(GetQueuedChecks().empty()); | 246 EXPECT_TRUE(GetQueuedChecks().empty()); |
208 | 247 |
209 ResetV4Database(); | 248 ResetV4Database(); |
210 v4_local_database_manager_->CheckBrowseUrl(url, &client); | 249 v4_local_database_manager_->CheckBrowseUrl(url, &client); |
211 // The database is unavailable so the check should get queued. | 250 // The database is unavailable so the check should get queued. |
212 EXPECT_EQ(1ul, GetQueuedChecks().size()); | 251 EXPECT_EQ(1ul, GetQueuedChecks().size()); |
213 | 252 |
214 StopLocalDatabaseManager(); | 253 StopLocalDatabaseManager(); |
215 EXPECT_TRUE(GetQueuedChecks().empty()); | 254 EXPECT_TRUE(GetQueuedChecks().empty()); |
216 } | 255 } |
217 | 256 |
218 } // namespace safe_browsing | 257 } // namespace safe_browsing |
OLD | NEW |