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) | 22 const bool reset_succeeds, |
| 23 const bool hash_prefix_matches) |
23 : V4Store( | 24 : V4Store( |
24 task_runner, | 25 task_runner, |
25 base::FilePath(store_path.value() + FILE_PATH_LITERAL(".fake"))), | 26 base::FilePath(store_path.value() + FILE_PATH_LITERAL(".fake"))), |
| 27 hash_prefix_should_match_(hash_prefix_matches), |
26 reset_succeeds_(reset_succeeds) {} | 28 reset_succeeds_(reset_succeeds) {} |
27 | 29 |
28 bool Reset() override { return reset_succeeds_; } | 30 bool Reset() override { return reset_succeeds_; } |
29 | 31 |
| 32 HashPrefix GetMatchingHashPrefix(const FullHash& full_hash) override { |
| 33 return hash_prefix_should_match_ ? full_hash : HashPrefix(); |
| 34 } |
| 35 |
| 36 void set_hash_prefix_matches(bool hash_prefix_matches) { |
| 37 hash_prefix_should_match_ = hash_prefix_matches; |
| 38 } |
| 39 |
30 private: | 40 private: |
| 41 bool hash_prefix_should_match_; |
31 bool reset_succeeds_; | 42 bool reset_succeeds_; |
32 }; | 43 }; |
33 | 44 |
34 // This factory creates a "fake" store. It allows the caller to specify that the | 45 // This factory creates a "fake" store. It allows the caller to specify that the |
35 // first store should fail on Reset() i.e. return false. All subsequent stores | 46 // first store should fail on Reset() i.e. return false. All subsequent stores |
36 // always return true. This is used to test the Reset() method in V4Database. | 47 // always return true. This is used to test the Reset() method in V4Database. |
37 class FakeV4StoreFactory : public V4StoreFactory { | 48 class FakeV4StoreFactory : public V4StoreFactory { |
38 public: | 49 public: |
39 FakeV4StoreFactory(bool next_store_reset_fails) | 50 FakeV4StoreFactory(bool next_store_reset_fails, bool hash_prefix_matches) |
40 : next_store_reset_fails_(next_store_reset_fails) {} | 51 : hash_prefix_should_match_(hash_prefix_matches), |
| 52 next_store_reset_fails_(next_store_reset_fails) {} |
41 | 53 |
42 V4Store* CreateV4Store( | 54 V4Store* CreateV4Store( |
43 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 55 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
44 const base::FilePath& store_path) override { | 56 const base::FilePath& store_path) override { |
45 bool reset_succeeds = !next_store_reset_fails_; | 57 bool reset_succeeds = !next_store_reset_fails_; |
46 next_store_reset_fails_ = false; | 58 next_store_reset_fails_ = false; |
47 return new FakeV4Store(task_runner, store_path, reset_succeeds); | 59 return new FakeV4Store(task_runner, store_path, reset_succeeds, |
| 60 hash_prefix_should_match_); |
48 } | 61 } |
49 | 62 |
50 private: | 63 private: |
| 64 bool hash_prefix_should_match_; |
51 bool next_store_reset_fails_; | 65 bool next_store_reset_fails_; |
52 }; | 66 }; |
53 | 67 |
54 class V4DatabaseTest : public PlatformTest { | 68 class V4DatabaseTest : public PlatformTest { |
55 public: | 69 public: |
56 V4DatabaseTest() : task_runner_(new base::TestSimpleTaskRunner) {} | 70 V4DatabaseTest() |
| 71 : task_runner_(new base::TestSimpleTaskRunner), |
| 72 linux_malware_id_(LINUX_PLATFORM, URL, MALWARE_THREAT), |
| 73 win_malware_id_(WINDOWS_PLATFORM, URL, MALWARE_THREAT) {} |
57 | 74 |
58 void SetUp() override { | 75 void SetUp() override { |
59 PlatformTest::SetUp(); | 76 PlatformTest::SetUp(); |
60 | 77 |
61 // Setup a database in a temporary directory. | 78 // Setup a database in a temporary directory. |
62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 79 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
63 database_dirname_ = temp_dir_.path().AppendASCII("V4DatabaseTest"); | 80 database_dirname_ = temp_dir_.path().AppendASCII("V4DatabaseTest"); |
64 | 81 |
65 created_but_not_called_back_ = false; | 82 created_but_not_called_back_ = false; |
66 created_and_called_back_ = false; | 83 created_and_called_back_ = false; |
67 | 84 |
68 callback_db_updated_ = | 85 callback_db_updated_ = |
69 base::Bind(&V4DatabaseTest::DatabaseUpdated, base::Unretained(this)); | 86 base::Bind(&V4DatabaseTest::DatabaseUpdated, base::Unretained(this)); |
70 | 87 |
71 callback_db_ready_ = base::Bind( | 88 callback_db_ready_ = base::Bind( |
72 &V4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, | 89 &V4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, |
73 base::Unretained(this)); | 90 base::Unretained(this)); |
74 | 91 |
75 SetupInfoMapAndExpectedState(); | 92 SetupInfoMapAndExpectedState(); |
76 } | 93 } |
77 | 94 |
78 void TearDown() override { | 95 void TearDown() override { |
79 V4Database::RegisterStoreFactoryForTest(nullptr); | 96 V4Database::RegisterStoreFactoryForTest(nullptr); |
80 PlatformTest::TearDown(); | 97 PlatformTest::TearDown(); |
81 } | 98 } |
82 | 99 |
83 void RegisterFactory(bool fails_first_reset) { | 100 void RegisterFactory(bool fails_first_reset, |
84 factory_.reset(new FakeV4StoreFactory(fails_first_reset)); | 101 bool hash_prefix_matches = true) { |
| 102 factory_.reset( |
| 103 new FakeV4StoreFactory(fails_first_reset, hash_prefix_matches)); |
85 V4Database::RegisterStoreFactoryForTest(factory_.get()); | 104 V4Database::RegisterStoreFactoryForTest(factory_.get()); |
86 } | 105 } |
87 | 106 |
88 void SetupInfoMapAndExpectedState() { | 107 void SetupInfoMapAndExpectedState() { |
89 UpdateListIdentifier win_malware_id(WINDOWS_PLATFORM, URL, MALWARE_THREAT); | 108 store_file_name_map_[win_malware_id_] = "win_url_malware"; |
90 store_file_name_map_[win_malware_id] = "win_url_malware"; | 109 expected_identifiers_.push_back(win_malware_id_); |
91 expected_identifiers_.push_back(win_malware_id); | |
92 expected_store_paths_.push_back( | 110 expected_store_paths_.push_back( |
93 database_dirname_.AppendASCII("win_url_malware.fake")); | 111 database_dirname_.AppendASCII("win_url_malware.fake")); |
94 | 112 |
95 UpdateListIdentifier linux_malware_id(LINUX_PLATFORM, URL, MALWARE_THREAT); | 113 store_file_name_map_[linux_malware_id_] = "linux_url_malware"; |
96 store_file_name_map_[linux_malware_id] = "linux_url_malware"; | 114 expected_identifiers_.push_back(linux_malware_id_); |
97 expected_identifiers_.push_back(linux_malware_id); | |
98 expected_store_paths_.push_back( | 115 expected_store_paths_.push_back( |
99 database_dirname_.AppendASCII("linux_url_malware.fake")); | 116 database_dirname_.AppendASCII("linux_url_malware.fake")); |
100 } | 117 } |
101 | 118 |
102 void DatabaseUpdated() {} | 119 void DatabaseUpdated() {} |
103 void NewDatabaseReadyWithExpectedStorePathsAndIds( | 120 void NewDatabaseReadyWithExpectedStorePathsAndIds( |
104 std::unique_ptr<V4Database> v4_database) { | 121 std::unique_ptr<V4Database> v4_database) { |
105 ASSERT_TRUE(v4_database); | 122 ASSERT_TRUE(v4_database); |
106 ASSERT_TRUE(v4_database->store_map_); | 123 ASSERT_TRUE(v4_database->store_map_); |
107 | 124 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 bool created_and_called_back_; | 202 bool created_and_called_back_; |
186 StoreFileNameMap store_file_name_map_; | 203 StoreFileNameMap store_file_name_map_; |
187 std::vector<UpdateListIdentifier> expected_identifiers_; | 204 std::vector<UpdateListIdentifier> expected_identifiers_; |
188 std::vector<base::FilePath> expected_store_paths_; | 205 std::vector<base::FilePath> expected_store_paths_; |
189 bool expected_resets_successfully_; | 206 bool expected_resets_successfully_; |
190 std::unique_ptr<FakeV4StoreFactory> factory_; | 207 std::unique_ptr<FakeV4StoreFactory> factory_; |
191 DatabaseUpdatedCallback callback_db_updated_; | 208 DatabaseUpdatedCallback callback_db_updated_; |
192 NewDatabaseReadyCallback callback_db_ready_; | 209 NewDatabaseReadyCallback callback_db_ready_; |
193 StoreStateMap expected_store_state_map_; | 210 StoreStateMap expected_store_state_map_; |
194 base::hash_map<UpdateListIdentifier, V4Store*> old_stores_map_; | 211 base::hash_map<UpdateListIdentifier, V4Store*> old_stores_map_; |
| 212 const UpdateListIdentifier linux_malware_id_, win_malware_id_; |
195 }; | 213 }; |
196 | 214 |
197 // Test to set up the database with fake stores. | 215 // Test to set up the database with fake stores. |
198 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) { | 216 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) { |
199 expected_resets_successfully_ = true; | 217 expected_resets_successfully_ = true; |
200 RegisterFactory(!expected_resets_successfully_); | 218 RegisterFactory(!expected_resets_successfully_); |
201 | 219 |
202 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 220 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
203 callback_db_ready_); | 221 callback_db_ready_); |
204 created_but_not_called_back_ = true; | 222 created_but_not_called_back_ = true; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 | 357 |
340 v4_database_->ApplyUpdate( | 358 v4_database_->ApplyUpdate( |
341 CreateFakeServerResponse(expected_store_state_map_, false), | 359 CreateFakeServerResponse(expected_store_state_map_, false), |
342 callback_db_updated_); | 360 callback_db_updated_); |
343 task_runner_->RunPendingTasks(); | 361 task_runner_->RunPendingTasks(); |
344 base::RunLoop().RunUntilIdle(); | 362 base::RunLoop().RunUntilIdle(); |
345 | 363 |
346 VerifyExpectedStoresState(false); | 364 VerifyExpectedStoresState(false); |
347 } | 365 } |
348 | 366 |
| 367 // Test to ensure the case that all stores match a given full hash. |
| 368 TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) { |
| 369 bool hash_prefix_matches = true; |
| 370 expected_resets_successfully_ = true; |
| 371 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); |
| 372 |
| 373 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 374 callback_db_ready_); |
| 375 created_but_not_called_back_ = true; |
| 376 task_runner_->RunPendingTasks(); |
| 377 |
| 378 base::RunLoop().RunUntilIdle(); |
| 379 EXPECT_EQ(true, created_and_called_back_); |
| 380 |
| 381 base::hash_set<UpdateListIdentifier> stores_to_look( |
| 382 {linux_malware_id_, win_malware_id_}); |
| 383 MatchedHashPrefixMap matched_hash_prefix_map; |
| 384 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, |
| 385 &matched_hash_prefix_map); |
| 386 EXPECT_EQ(2u, matched_hash_prefix_map.size()); |
| 387 EXPECT_FALSE(matched_hash_prefix_map[linux_malware_id_].empty()); |
| 388 EXPECT_FALSE(matched_hash_prefix_map[win_malware_id_].empty()); |
| 389 } |
| 390 |
| 391 // Test to ensure the case that no stores match a given full hash. |
| 392 TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { |
| 393 bool hash_prefix_matches = false; |
| 394 expected_resets_successfully_ = true; |
| 395 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); |
| 396 |
| 397 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 398 callback_db_ready_); |
| 399 created_but_not_called_back_ = true; |
| 400 task_runner_->RunPendingTasks(); |
| 401 |
| 402 base::RunLoop().RunUntilIdle(); |
| 403 EXPECT_EQ(true, created_and_called_back_); |
| 404 |
| 405 base::hash_set<UpdateListIdentifier> stores_to_look( |
| 406 {linux_malware_id_, win_malware_id_}); |
| 407 MatchedHashPrefixMap matched_hash_prefix_map; |
| 408 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, |
| 409 &matched_hash_prefix_map); |
| 410 EXPECT_TRUE(matched_hash_prefix_map.empty()); |
| 411 } |
| 412 |
| 413 // Test to ensure the case that some stores match a given full hash. |
| 414 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { |
| 415 // Setup stores to not match the full hash. |
| 416 bool hash_prefix_matches = false; |
| 417 expected_resets_successfully_ = true; |
| 418 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); |
| 419 |
| 420 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 421 callback_db_ready_); |
| 422 created_but_not_called_back_ = true; |
| 423 task_runner_->RunPendingTasks(); |
| 424 |
| 425 base::RunLoop().RunUntilIdle(); |
| 426 EXPECT_EQ(true, created_and_called_back_); |
| 427 |
| 428 // Set the store corresponding to linux_malware_id_ to match the full hash. |
| 429 FakeV4Store* store = static_cast<FakeV4Store*>( |
| 430 v4_database_->store_map_->at(linux_malware_id_).get()); |
| 431 store->set_hash_prefix_matches(true); |
| 432 |
| 433 base::hash_set<UpdateListIdentifier> stores_to_look( |
| 434 {linux_malware_id_, win_malware_id_}); |
| 435 MatchedHashPrefixMap matched_hash_prefix_map; |
| 436 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, |
| 437 &matched_hash_prefix_map); |
| 438 EXPECT_EQ(1u, matched_hash_prefix_map.size()); |
| 439 EXPECT_FALSE(matched_hash_prefix_map[linux_malware_id_].empty()); |
| 440 } |
| 441 |
| 442 // Test to ensure the case that only some stores are reported to match a given |
| 443 // full hash because of stores_to_look. |
| 444 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) { |
| 445 // Setup all stores to match the full hash. |
| 446 bool hash_prefix_matches = true; |
| 447 expected_resets_successfully_ = true; |
| 448 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); |
| 449 |
| 450 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 451 callback_db_ready_); |
| 452 created_but_not_called_back_ = true; |
| 453 task_runner_->RunPendingTasks(); |
| 454 |
| 455 base::RunLoop().RunUntilIdle(); |
| 456 EXPECT_EQ(true, created_and_called_back_); |
| 457 |
| 458 base::hash_set<UpdateListIdentifier> stores_to_look({linux_malware_id_}); |
| 459 // Don't add win_malware_id_ to the stores_to_look. |
| 460 MatchedHashPrefixMap matched_hash_prefix_map; |
| 461 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, |
| 462 &matched_hash_prefix_map); |
| 463 EXPECT_EQ(1u, matched_hash_prefix_map.size()); |
| 464 EXPECT_FALSE(matched_hash_prefix_map[linux_malware_id_].empty()); |
| 465 } |
| 466 |
349 } // namespace safe_browsing | 467 } // namespace safe_browsing |
OLD | NEW |