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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/debug/leak_annotations.h" | 6 #include "base/debug/leak_annotations.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/test/test_simple_task_runner.h" | 10 #include "base/test/test_simple_task_runner.h" |
11 #include "components/safe_browsing_db/v4_database.h" | 11 #include "components/safe_browsing_db/v4_database.h" |
12 #include "components/safe_browsing_db/v4_store.h" | 12 #include "components/safe_browsing_db/v4_store.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.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 FakeV4Store : public V4Store { | 18 class FakeV4Store : public V4Store { |
19 public: | 19 public: |
20 FakeV4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 20 FakeV4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
21 const base::FilePath& store_path, | 21 const base::FilePath& store_path, |
22 const bool reset_succeeds, | |
23 const bool hash_prefix_matches) | 22 const bool hash_prefix_matches) |
24 : V4Store( | 23 : V4Store( |
25 task_runner, | 24 task_runner, |
26 base::FilePath(store_path.value() + FILE_PATH_LITERAL(".fake"))), | 25 base::FilePath(store_path.value() + FILE_PATH_LITERAL(".fake"))), |
27 hash_prefix_should_match_(hash_prefix_matches), | 26 hash_prefix_should_match_(hash_prefix_matches) {} |
28 reset_succeeds_(reset_succeeds) {} | |
29 | |
30 bool Reset() override { return reset_succeeds_; } | |
31 | 27 |
32 HashPrefix GetMatchingHashPrefix(const FullHash& full_hash) override { | 28 HashPrefix GetMatchingHashPrefix(const FullHash& full_hash) override { |
33 return hash_prefix_should_match_ ? full_hash : HashPrefix(); | 29 return hash_prefix_should_match_ ? full_hash : HashPrefix(); |
34 } | 30 } |
35 | 31 |
36 void set_hash_prefix_matches(bool hash_prefix_matches) { | 32 void set_hash_prefix_matches(bool hash_prefix_matches) { |
37 hash_prefix_should_match_ = hash_prefix_matches; | 33 hash_prefix_should_match_ = hash_prefix_matches; |
38 } | 34 } |
39 | 35 |
40 private: | 36 private: |
41 bool hash_prefix_should_match_; | 37 bool hash_prefix_should_match_; |
42 bool reset_succeeds_; | |
43 }; | 38 }; |
44 | 39 |
45 // This factory creates a "fake" store. It allows the caller to specify that the | 40 // This factory creates a "fake" store. It allows the caller to specify whether |
46 // first store should fail on Reset() i.e. return false. All subsequent stores | 41 // the store has a hash prefix matching a full hash. This is used to test the |
47 // always return true. This is used to test the Reset() method in V4Database. | 42 // |GetStoresMatchingFullHash()| method in |V4Database|. |
48 class FakeV4StoreFactory : public V4StoreFactory { | 43 class FakeV4StoreFactory : public V4StoreFactory { |
49 public: | 44 public: |
50 FakeV4StoreFactory(bool next_store_reset_fails, bool hash_prefix_matches) | 45 FakeV4StoreFactory(bool hash_prefix_matches) |
51 : hash_prefix_should_match_(hash_prefix_matches), | 46 : hash_prefix_should_match_(hash_prefix_matches) {} |
52 next_store_reset_fails_(next_store_reset_fails) {} | |
53 | 47 |
54 V4Store* CreateV4Store( | 48 V4Store* CreateV4Store( |
55 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 49 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
56 const base::FilePath& store_path) override { | 50 const base::FilePath& store_path) override { |
57 bool reset_succeeds = !next_store_reset_fails_; | 51 return new FakeV4Store(task_runner, store_path, hash_prefix_should_match_); |
58 next_store_reset_fails_ = false; | |
59 return new FakeV4Store(task_runner, store_path, reset_succeeds, | |
60 hash_prefix_should_match_); | |
61 } | 52 } |
62 | 53 |
63 private: | 54 private: |
64 bool hash_prefix_should_match_; | 55 bool hash_prefix_should_match_; |
65 bool next_store_reset_fails_; | |
66 }; | 56 }; |
67 | 57 |
68 class V4DatabaseTest : public PlatformTest { | 58 class V4DatabaseTest : public PlatformTest { |
69 public: | 59 public: |
70 V4DatabaseTest() | 60 V4DatabaseTest() |
71 : task_runner_(new base::TestSimpleTaskRunner), | 61 : task_runner_(new base::TestSimpleTaskRunner), |
72 linux_malware_id_(LINUX_PLATFORM, URL, MALWARE_THREAT), | 62 linux_malware_id_(LINUX_PLATFORM, URL, MALWARE_THREAT), |
73 win_malware_id_(WINDOWS_PLATFORM, URL, MALWARE_THREAT) {} | 63 win_malware_id_(WINDOWS_PLATFORM, URL, MALWARE_THREAT) {} |
74 | 64 |
75 void SetUp() override { | 65 void SetUp() override { |
76 PlatformTest::SetUp(); | 66 PlatformTest::SetUp(); |
77 | 67 |
78 // Setup a database in a temporary directory. | 68 // Setup a database in a temporary directory. |
79 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
80 database_dirname_ = temp_dir_.GetPath().AppendASCII("V4DatabaseTest"); | 70 database_dirname_ = temp_dir_.GetPath().AppendASCII("V4DatabaseTest"); |
81 | 71 |
82 created_but_not_called_back_ = false; | 72 created_but_not_called_back_ = false; |
83 created_and_called_back_ = false; | 73 created_and_called_back_ = false; |
| 74 verify_checksum_called_back_ = false; |
84 | 75 |
85 callback_db_updated_ = | 76 callback_db_updated_ = |
86 base::Bind(&V4DatabaseTest::DatabaseUpdated, base::Unretained(this)); | 77 base::Bind(&V4DatabaseTest::DatabaseUpdated, base::Unretained(this)); |
87 | 78 |
88 callback_db_ready_ = base::Bind( | 79 callback_db_ready_ = base::Bind( |
89 &V4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, | 80 &V4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, |
90 base::Unretained(this)); | 81 base::Unretained(this)); |
91 | 82 |
92 SetupInfoMapAndExpectedState(); | 83 SetupInfoMapAndExpectedState(); |
93 } | 84 } |
94 | 85 |
95 void TearDown() override { | 86 void TearDown() override { |
96 V4Database::RegisterStoreFactoryForTest(nullptr); | 87 V4Database::RegisterStoreFactoryForTest(nullptr); |
97 PlatformTest::TearDown(); | 88 PlatformTest::TearDown(); |
98 } | 89 } |
99 | 90 |
100 void RegisterFactory(bool fails_first_reset, | 91 void RegisterFactory(bool hash_prefix_matches = true) { |
101 bool hash_prefix_matches = true) { | 92 factory_.reset(new FakeV4StoreFactory(hash_prefix_matches)); |
102 factory_.reset( | |
103 new FakeV4StoreFactory(fails_first_reset, hash_prefix_matches)); | |
104 V4Database::RegisterStoreFactoryForTest(factory_.get()); | 93 V4Database::RegisterStoreFactoryForTest(factory_.get()); |
105 } | 94 } |
106 | 95 |
107 void SetupInfoMapAndExpectedState() { | 96 void SetupInfoMapAndExpectedState() { |
108 list_infos_.emplace_back(true, "win_url_malware", win_malware_id_, | 97 list_infos_.emplace_back(true, "win_url_malware", win_malware_id_, |
109 SB_THREAT_TYPE_URL_MALWARE); | 98 SB_THREAT_TYPE_URL_MALWARE); |
110 expected_identifiers_.push_back(win_malware_id_); | 99 expected_identifiers_.push_back(win_malware_id_); |
111 expected_store_paths_.push_back( | 100 expected_store_paths_.push_back( |
112 database_dirname_.AppendASCII("win_url_malware.fake")); | 101 database_dirname_.AppendASCII("win_url_malware.fake")); |
113 | 102 |
(...skipping 16 matching lines...) Expand all Loading... |
130 ASSERT_EQ(expected_store_paths_.size(), v4_database->store_map_->size()); | 119 ASSERT_EQ(expected_store_paths_.size(), v4_database->store_map_->size()); |
131 ASSERT_EQ(expected_identifiers_.size(), v4_database->store_map_->size()); | 120 ASSERT_EQ(expected_identifiers_.size(), v4_database->store_map_->size()); |
132 for (size_t i = 0; i < expected_identifiers_.size(); i++) { | 121 for (size_t i = 0; i < expected_identifiers_.size(); i++) { |
133 const auto& expected_identifier = expected_identifiers_[i]; | 122 const auto& expected_identifier = expected_identifiers_[i]; |
134 const auto& store = (*v4_database->store_map_)[expected_identifier]; | 123 const auto& store = (*v4_database->store_map_)[expected_identifier]; |
135 ASSERT_TRUE(store); | 124 ASSERT_TRUE(store); |
136 const auto& expected_store_path = expected_store_paths_[i]; | 125 const auto& expected_store_path = expected_store_paths_[i]; |
137 EXPECT_EQ(expected_store_path, store->store_path()); | 126 EXPECT_EQ(expected_store_path, store->store_path()); |
138 } | 127 } |
139 | 128 |
140 EXPECT_EQ(expected_resets_successfully_, v4_database->ResetDatabase()); | |
141 | |
142 EXPECT_FALSE(created_and_called_back_); | 129 EXPECT_FALSE(created_and_called_back_); |
143 created_and_called_back_ = true; | 130 created_and_called_back_ = true; |
144 | 131 |
145 v4_database_ = std::move(v4_database); | 132 v4_database_ = std::move(v4_database); |
146 } | 133 } |
147 | 134 |
148 std::unique_ptr<ParsedServerResponse> CreateFakeServerResponse( | 135 std::unique_ptr<ParsedServerResponse> CreateFakeServerResponse( |
149 StoreStateMap store_state_map, | 136 StoreStateMap store_state_map, |
150 bool use_valid_response_type) { | 137 bool use_valid_response_type) { |
151 std::unique_ptr<ParsedServerResponse> parsed_server_response( | 138 std::unique_ptr<ParsedServerResponse> parsed_server_response( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 EXPECT_NE(old_stores_map_.at(identifier), | 175 EXPECT_NE(old_stores_map_.at(identifier), |
189 new_store_map->at(identifier).get()); | 176 new_store_map->at(identifier).get()); |
190 } else { | 177 } else { |
191 // Verify that NO new store was created. | 178 // Verify that NO new store was created. |
192 EXPECT_EQ(old_stores_map_.at(identifier), | 179 EXPECT_EQ(old_stores_map_.at(identifier), |
193 new_store_map->at(identifier).get()); | 180 new_store_map->at(identifier).get()); |
194 } | 181 } |
195 } | 182 } |
196 } | 183 } |
197 | 184 |
| 185 void VerifyChecksumCallback(const std::vector<ListIdentifier>& stores) { |
| 186 EXPECT_FALSE(verify_checksum_called_back_); |
| 187 verify_checksum_called_back_ = true; |
| 188 } |
| 189 |
198 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 190 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
199 std::unique_ptr<V4Database> v4_database_; | 191 std::unique_ptr<V4Database> v4_database_; |
200 base::FilePath database_dirname_; | 192 base::FilePath database_dirname_; |
201 base::ScopedTempDir temp_dir_; | 193 base::ScopedTempDir temp_dir_; |
202 content::TestBrowserThreadBundle thread_bundle_; | 194 content::TestBrowserThreadBundle thread_bundle_; |
203 bool created_but_not_called_back_; | 195 bool created_but_not_called_back_; |
204 bool created_and_called_back_; | 196 bool created_and_called_back_; |
| 197 bool verify_checksum_called_back_; |
205 ListInfos list_infos_; | 198 ListInfos list_infos_; |
206 std::vector<ListIdentifier> expected_identifiers_; | 199 std::vector<ListIdentifier> expected_identifiers_; |
207 std::vector<base::FilePath> expected_store_paths_; | 200 std::vector<base::FilePath> expected_store_paths_; |
208 bool expected_resets_successfully_; | |
209 std::unique_ptr<FakeV4StoreFactory> factory_; | 201 std::unique_ptr<FakeV4StoreFactory> factory_; |
210 DatabaseUpdatedCallback callback_db_updated_; | 202 DatabaseUpdatedCallback callback_db_updated_; |
211 NewDatabaseReadyCallback callback_db_ready_; | 203 NewDatabaseReadyCallback callback_db_ready_; |
212 StoreStateMap expected_store_state_map_; | 204 StoreStateMap expected_store_state_map_; |
213 base::hash_map<ListIdentifier, V4Store*> old_stores_map_; | 205 base::hash_map<ListIdentifier, V4Store*> old_stores_map_; |
214 const ListIdentifier linux_malware_id_, win_malware_id_; | 206 const ListIdentifier linux_malware_id_, win_malware_id_; |
215 }; | 207 }; |
216 | 208 |
217 // Test to set up the database with fake stores. | 209 // Test to set up the database with fake stores. |
218 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) { | 210 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) { |
219 expected_resets_successfully_ = true; | 211 RegisterFactory(); |
220 RegisterFactory(!expected_resets_successfully_); | |
221 | 212 |
222 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 213 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
223 callback_db_ready_); | 214 callback_db_ready_); |
224 created_but_not_called_back_ = true; | |
225 task_runner_->RunPendingTasks(); | |
226 | |
227 base::RunLoop().RunUntilIdle(); | |
228 EXPECT_EQ(true, created_and_called_back_); | |
229 } | |
230 | |
231 // Test to set up the database with fake stores that fail to reset. | |
232 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) { | |
233 expected_resets_successfully_ = false; | |
234 RegisterFactory(!expected_resets_successfully_); | |
235 | |
236 V4Database::Create(task_runner_, database_dirname_, list_infos_, | |
237 callback_db_ready_); | |
238 created_but_not_called_back_ = true; | 215 created_but_not_called_back_ = true; |
239 task_runner_->RunPendingTasks(); | 216 task_runner_->RunPendingTasks(); |
240 | 217 |
241 base::RunLoop().RunUntilIdle(); | 218 base::RunLoop().RunUntilIdle(); |
242 EXPECT_EQ(true, created_and_called_back_); | 219 EXPECT_EQ(true, created_and_called_back_); |
243 } | 220 } |
244 | 221 |
245 // Test to check database updates as expected. | 222 // Test to check database updates as expected. |
246 TEST_F(V4DatabaseTest, TestApplyUpdateWithNewStates) { | 223 TEST_F(V4DatabaseTest, TestApplyUpdateWithNewStates) { |
247 expected_resets_successfully_ = true; | 224 RegisterFactory(); |
248 RegisterFactory(!expected_resets_successfully_); | |
249 | 225 |
250 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 226 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
251 callback_db_ready_); | 227 callback_db_ready_); |
252 created_but_not_called_back_ = true; | 228 created_but_not_called_back_ = true; |
253 task_runner_->RunPendingTasks(); | 229 task_runner_->RunPendingTasks(); |
254 base::RunLoop().RunUntilIdle(); | 230 base::RunLoop().RunUntilIdle(); |
255 | 231 |
256 // The database has now been created. Time to try to update it. | 232 // The database has now been created. Time to try to update it. |
257 EXPECT_TRUE(v4_database_); | 233 EXPECT_TRUE(v4_database_); |
258 const StoreMap* db_stores = v4_database_->store_map_.get(); | 234 const StoreMap* db_stores = v4_database_->store_map_.get(); |
259 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | 235 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
260 for (const auto& store_iter : *db_stores) { | 236 for (const auto& store_iter : *db_stores) { |
261 V4Store* store = store_iter.second.get(); | 237 V4Store* store = store_iter.second.get(); |
262 expected_store_state_map_[store_iter.first] = store->state() + "_fake"; | 238 expected_store_state_map_[store_iter.first] = store->state() + "_fake"; |
263 old_stores_map_[store_iter.first] = store; | 239 old_stores_map_[store_iter.first] = store; |
264 } | 240 } |
265 | 241 |
266 v4_database_->ApplyUpdate( | 242 v4_database_->ApplyUpdate( |
267 CreateFakeServerResponse(expected_store_state_map_, true), | 243 CreateFakeServerResponse(expected_store_state_map_, true), |
268 callback_db_updated_); | 244 callback_db_updated_); |
269 | 245 |
270 task_runner_->RunPendingTasks(); | 246 task_runner_->RunPendingTasks(); |
271 base::RunLoop().RunUntilIdle(); | 247 base::RunLoop().RunUntilIdle(); |
272 | 248 |
273 VerifyExpectedStoresState(true); | 249 VerifyExpectedStoresState(true); |
274 } | 250 } |
275 | 251 |
276 // Test to ensure no state updates leads to no store updates. | 252 // Test to ensure no state updates leads to no store updates. |
277 TEST_F(V4DatabaseTest, TestApplyUpdateWithNoNewState) { | 253 TEST_F(V4DatabaseTest, TestApplyUpdateWithNoNewState) { |
278 expected_resets_successfully_ = true; | 254 RegisterFactory(); |
279 RegisterFactory(!expected_resets_successfully_); | |
280 | 255 |
281 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 256 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
282 callback_db_ready_); | 257 callback_db_ready_); |
283 created_but_not_called_back_ = true; | 258 created_but_not_called_back_ = true; |
284 task_runner_->RunPendingTasks(); | 259 task_runner_->RunPendingTasks(); |
285 base::RunLoop().RunUntilIdle(); | 260 base::RunLoop().RunUntilIdle(); |
286 | 261 |
287 // The database has now been created. Time to try to update it. | 262 // The database has now been created. Time to try to update it. |
288 EXPECT_TRUE(v4_database_); | 263 EXPECT_TRUE(v4_database_); |
289 const StoreMap* db_stores = v4_database_->store_map_.get(); | 264 const StoreMap* db_stores = v4_database_->store_map_.get(); |
290 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | 265 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
291 for (const auto& store_iter : *db_stores) { | 266 for (const auto& store_iter : *db_stores) { |
292 V4Store* store = store_iter.second.get(); | 267 V4Store* store = store_iter.second.get(); |
293 expected_store_state_map_[store_iter.first] = store->state(); | 268 expected_store_state_map_[store_iter.first] = store->state(); |
294 old_stores_map_[store_iter.first] = store; | 269 old_stores_map_[store_iter.first] = store; |
295 } | 270 } |
296 | 271 |
297 v4_database_->ApplyUpdate( | 272 v4_database_->ApplyUpdate( |
298 CreateFakeServerResponse(expected_store_state_map_, true), | 273 CreateFakeServerResponse(expected_store_state_map_, true), |
299 callback_db_updated_); | 274 callback_db_updated_); |
300 | 275 |
301 task_runner_->RunPendingTasks(); | 276 task_runner_->RunPendingTasks(); |
302 base::RunLoop().RunUntilIdle(); | 277 base::RunLoop().RunUntilIdle(); |
303 | 278 |
304 VerifyExpectedStoresState(false); | 279 VerifyExpectedStoresState(false); |
305 } | 280 } |
306 | 281 |
307 // Test to ensure no updates leads to no store updates. | 282 // Test to ensure no updates leads to no store updates. |
308 TEST_F(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { | 283 TEST_F(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { |
309 expected_resets_successfully_ = true; | 284 RegisterFactory(); |
310 RegisterFactory(!expected_resets_successfully_); | |
311 | 285 |
312 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 286 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
313 callback_db_ready_); | 287 callback_db_ready_); |
314 created_but_not_called_back_ = true; | 288 created_but_not_called_back_ = true; |
315 task_runner_->RunPendingTasks(); | 289 task_runner_->RunPendingTasks(); |
316 base::RunLoop().RunUntilIdle(); | 290 base::RunLoop().RunUntilIdle(); |
317 | 291 |
318 // The database has now been created. Time to try to update it. | 292 // The database has now been created. Time to try to update it. |
319 EXPECT_TRUE(v4_database_); | 293 EXPECT_TRUE(v4_database_); |
320 const StoreMap* db_stores = v4_database_->store_map_.get(); | 294 const StoreMap* db_stores = v4_database_->store_map_.get(); |
(...skipping 10 matching lines...) Expand all Loading... |
331 callback_db_updated_); | 305 callback_db_updated_); |
332 | 306 |
333 task_runner_->RunPendingTasks(); | 307 task_runner_->RunPendingTasks(); |
334 base::RunLoop().RunUntilIdle(); | 308 base::RunLoop().RunUntilIdle(); |
335 | 309 |
336 VerifyExpectedStoresState(false); | 310 VerifyExpectedStoresState(false); |
337 } | 311 } |
338 | 312 |
339 // Test to ensure invalid update leads to no store changes. | 313 // Test to ensure invalid update leads to no store changes. |
340 TEST_F(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate) { | 314 TEST_F(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate) { |
341 expected_resets_successfully_ = true; | 315 RegisterFactory(); |
342 RegisterFactory(!expected_resets_successfully_); | |
343 | 316 |
344 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 317 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
345 callback_db_ready_); | 318 callback_db_ready_); |
346 created_but_not_called_back_ = true; | 319 created_but_not_called_back_ = true; |
347 task_runner_->RunPendingTasks(); | 320 task_runner_->RunPendingTasks(); |
348 base::RunLoop().RunUntilIdle(); | 321 base::RunLoop().RunUntilIdle(); |
349 | 322 |
350 // The database has now been created. Time to try to update it. | 323 // The database has now been created. Time to try to update it. |
351 EXPECT_TRUE(v4_database_); | 324 EXPECT_TRUE(v4_database_); |
352 const StoreMap* db_stores = v4_database_->store_map_.get(); | 325 const StoreMap* db_stores = v4_database_->store_map_.get(); |
353 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | 326 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
354 for (const auto& store_iter : *db_stores) { | 327 for (const auto& store_iter : *db_stores) { |
355 V4Store* store = store_iter.second.get(); | 328 V4Store* store = store_iter.second.get(); |
356 expected_store_state_map_[store_iter.first] = store->state(); | 329 expected_store_state_map_[store_iter.first] = store->state(); |
357 old_stores_map_[store_iter.first] = store; | 330 old_stores_map_[store_iter.first] = store; |
358 } | 331 } |
359 | 332 |
360 v4_database_->ApplyUpdate( | 333 v4_database_->ApplyUpdate( |
361 CreateFakeServerResponse(expected_store_state_map_, false), | 334 CreateFakeServerResponse(expected_store_state_map_, false), |
362 callback_db_updated_); | 335 callback_db_updated_); |
363 task_runner_->RunPendingTasks(); | 336 task_runner_->RunPendingTasks(); |
364 base::RunLoop().RunUntilIdle(); | 337 base::RunLoop().RunUntilIdle(); |
365 | 338 |
366 VerifyExpectedStoresState(false); | 339 VerifyExpectedStoresState(false); |
367 } | 340 } |
368 | 341 |
369 // Test to ensure the case that all stores match a given full hash. | 342 // Test to ensure the case that all stores match a given full hash. |
370 TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) { | 343 TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) { |
371 bool hash_prefix_matches = true; | 344 bool hash_prefix_matches = true; |
372 expected_resets_successfully_ = true; | 345 RegisterFactory(hash_prefix_matches); |
373 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
374 | 346 |
375 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 347 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
376 callback_db_ready_); | 348 callback_db_ready_); |
377 created_but_not_called_back_ = true; | 349 created_but_not_called_back_ = true; |
378 task_runner_->RunPendingTasks(); | 350 task_runner_->RunPendingTasks(); |
379 | 351 |
380 base::RunLoop().RunUntilIdle(); | 352 base::RunLoop().RunUntilIdle(); |
381 EXPECT_EQ(true, created_and_called_back_); | 353 EXPECT_EQ(true, created_and_called_back_); |
382 | 354 |
383 StoresToCheck stores_to_check({linux_malware_id_, win_malware_id_}); | 355 StoresToCheck stores_to_check({linux_malware_id_, win_malware_id_}); |
384 StoreAndHashPrefixes store_and_hash_prefixes; | 356 StoreAndHashPrefixes store_and_hash_prefixes; |
385 v4_database_->GetStoresMatchingFullHash("anything", stores_to_check, | 357 v4_database_->GetStoresMatchingFullHash("anything", stores_to_check, |
386 &store_and_hash_prefixes); | 358 &store_and_hash_prefixes); |
387 EXPECT_EQ(2u, store_and_hash_prefixes.size()); | 359 EXPECT_EQ(2u, store_and_hash_prefixes.size()); |
388 StoresToCheck stores_found; | 360 StoresToCheck stores_found; |
389 for (const auto& it : store_and_hash_prefixes) { | 361 for (const auto& it : store_and_hash_prefixes) { |
390 stores_found.insert(it.list_id); | 362 stores_found.insert(it.list_id); |
391 } | 363 } |
392 EXPECT_EQ(stores_to_check, stores_found); | 364 EXPECT_EQ(stores_to_check, stores_found); |
393 } | 365 } |
394 | 366 |
395 // Test to ensure the case that no stores match a given full hash. | 367 // Test to ensure the case that no stores match a given full hash. |
396 TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { | 368 TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { |
397 bool hash_prefix_matches = false; | 369 bool hash_prefix_matches = false; |
398 expected_resets_successfully_ = true; | 370 RegisterFactory(hash_prefix_matches); |
399 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
400 | 371 |
401 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 372 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
402 callback_db_ready_); | 373 callback_db_ready_); |
403 created_but_not_called_back_ = true; | 374 created_but_not_called_back_ = true; |
404 task_runner_->RunPendingTasks(); | 375 task_runner_->RunPendingTasks(); |
405 | 376 |
406 base::RunLoop().RunUntilIdle(); | 377 base::RunLoop().RunUntilIdle(); |
407 EXPECT_EQ(true, created_and_called_back_); | 378 EXPECT_EQ(true, created_and_called_back_); |
408 | 379 |
409 StoreAndHashPrefixes store_and_hash_prefixes; | 380 StoreAndHashPrefixes store_and_hash_prefixes; |
410 v4_database_->GetStoresMatchingFullHash( | 381 v4_database_->GetStoresMatchingFullHash( |
411 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}), | 382 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}), |
412 &store_and_hash_prefixes); | 383 &store_and_hash_prefixes); |
413 EXPECT_TRUE(store_and_hash_prefixes.empty()); | 384 EXPECT_TRUE(store_and_hash_prefixes.empty()); |
414 } | 385 } |
415 | 386 |
416 // Test to ensure the case that some stores match a given full hash. | 387 // Test to ensure the case that some stores match a given full hash. |
417 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { | 388 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { |
418 // Setup stores to not match the full hash. | 389 // Setup stores to not match the full hash. |
419 bool hash_prefix_matches = false; | 390 bool hash_prefix_matches = false; |
420 expected_resets_successfully_ = true; | 391 RegisterFactory(hash_prefix_matches); |
421 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
422 | 392 |
423 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 393 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
424 callback_db_ready_); | 394 callback_db_ready_); |
425 created_but_not_called_back_ = true; | 395 created_but_not_called_back_ = true; |
426 task_runner_->RunPendingTasks(); | 396 task_runner_->RunPendingTasks(); |
427 | 397 |
428 base::RunLoop().RunUntilIdle(); | 398 base::RunLoop().RunUntilIdle(); |
429 EXPECT_EQ(true, created_and_called_back_); | 399 EXPECT_EQ(true, created_and_called_back_); |
430 | 400 |
431 // Set the store corresponding to linux_malware_id_ to match the full hash. | 401 // Set the store corresponding to linux_malware_id_ to match the full hash. |
432 FakeV4Store* store = static_cast<FakeV4Store*>( | 402 FakeV4Store* store = static_cast<FakeV4Store*>( |
433 v4_database_->store_map_->at(win_malware_id_).get()); | 403 v4_database_->store_map_->at(win_malware_id_).get()); |
434 store->set_hash_prefix_matches(true); | 404 store->set_hash_prefix_matches(true); |
435 | 405 |
436 StoreAndHashPrefixes store_and_hash_prefixes; | 406 StoreAndHashPrefixes store_and_hash_prefixes; |
437 v4_database_->GetStoresMatchingFullHash( | 407 v4_database_->GetStoresMatchingFullHash( |
438 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}), | 408 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}), |
439 &store_and_hash_prefixes); | 409 &store_and_hash_prefixes); |
440 EXPECT_EQ(1u, store_and_hash_prefixes.size()); | 410 EXPECT_EQ(1u, store_and_hash_prefixes.size()); |
441 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, win_malware_id_); | 411 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, win_malware_id_); |
442 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); | 412 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); |
443 } | 413 } |
444 | 414 |
445 // Test to ensure the case that only some stores are reported to match a given | 415 // Test to ensure the case that only some stores are reported to match a given |
446 // full hash because of StoresToCheck. | 416 // full hash because of StoresToCheck. |
447 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) { | 417 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) { |
448 // Setup all stores to match the full hash. | 418 // Setup all stores to match the full hash. |
449 bool hash_prefix_matches = true; | 419 bool hash_prefix_matches = true; |
450 expected_resets_successfully_ = true; | 420 RegisterFactory(hash_prefix_matches); |
451 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
452 | 421 |
453 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 422 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
454 callback_db_ready_); | 423 callback_db_ready_); |
455 created_but_not_called_back_ = true; | 424 created_but_not_called_back_ = true; |
456 task_runner_->RunPendingTasks(); | 425 task_runner_->RunPendingTasks(); |
457 | 426 |
458 base::RunLoop().RunUntilIdle(); | 427 base::RunLoop().RunUntilIdle(); |
459 EXPECT_EQ(true, created_and_called_back_); | 428 EXPECT_EQ(true, created_and_called_back_); |
460 | 429 |
461 // Don't add win_malware_id_ to the StoresToCheck. | 430 // Don't add win_malware_id_ to the StoresToCheck. |
462 StoreAndHashPrefixes store_and_hash_prefixes; | 431 StoreAndHashPrefixes store_and_hash_prefixes; |
463 v4_database_->GetStoresMatchingFullHash( | 432 v4_database_->GetStoresMatchingFullHash( |
464 "anything", StoresToCheck({linux_malware_id_}), &store_and_hash_prefixes); | 433 "anything", StoresToCheck({linux_malware_id_}), &store_and_hash_prefixes); |
465 EXPECT_EQ(1u, store_and_hash_prefixes.size()); | 434 EXPECT_EQ(1u, store_and_hash_prefixes.size()); |
466 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, linux_malware_id_); | 435 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, linux_malware_id_); |
467 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); | 436 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); |
468 } | 437 } |
469 | 438 |
| 439 TEST_F(V4DatabaseTest, VerifyChecksumCalledAsync) { |
| 440 bool hash_prefix_matches = true; |
| 441 RegisterFactory(hash_prefix_matches); |
| 442 |
| 443 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
| 444 callback_db_ready_); |
| 445 created_but_not_called_back_ = true; |
| 446 task_runner_->RunPendingTasks(); |
| 447 |
| 448 base::RunLoop().RunUntilIdle(); |
| 449 EXPECT_EQ(true, created_and_called_back_); |
| 450 |
| 451 // verify_checksum_called_back_ set to false in the constructor. |
| 452 EXPECT_FALSE(verify_checksum_called_back_); |
| 453 // Now call VerifyChecksum and pass the callback that sets |
| 454 // verify_checksum_called_back_ to true. |
| 455 v4_database_->VerifyChecksum(base::Bind( |
| 456 &V4DatabaseTest::VerifyChecksumCallback, base::Unretained(this))); |
| 457 // verify_checksum_called_back_ should still be false since the checksum |
| 458 // verification is async. |
| 459 EXPECT_FALSE(verify_checksum_called_back_); |
| 460 task_runner_->RunPendingTasks(); |
| 461 base::RunLoop().RunUntilIdle(); |
| 462 EXPECT_TRUE(verify_checksum_called_back_); |
| 463 } |
| 464 |
470 } // namespace safe_browsing | 465 } // namespace safe_browsing |
OLD | NEW |