| 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_matches_(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_matches_ ? full_hash : HashPrefix(); |
| 34 } |
| 35 |
| 36 void set_hash_prefix_matches(bool hash_prefix_matches) { |
| 37 hash_prefix_matches_ = hash_prefix_matches; |
| 38 } |
| 39 |
| 30 private: | 40 private: |
| 41 bool hash_prefix_matches_; |
| 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_matches_(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_matches_); |
| 48 } | 61 } |
| 49 | 62 |
| 50 private: | 63 private: |
| 64 bool hash_prefix_matches_; |
| 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() : task_runner_(new base::TestSimpleTaskRunner) {} |
| 57 | 71 |
| 58 void SetUp() override { | 72 void SetUp() override { |
| 59 PlatformTest::SetUp(); | 73 PlatformTest::SetUp(); |
| 60 | 74 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 73 base::Unretained(this)); | 87 base::Unretained(this)); |
| 74 | 88 |
| 75 SetupInfoMapAndExpectedState(); | 89 SetupInfoMapAndExpectedState(); |
| 76 } | 90 } |
| 77 | 91 |
| 78 void TearDown() override { | 92 void TearDown() override { |
| 79 V4Database::RegisterStoreFactoryForTest(nullptr); | 93 V4Database::RegisterStoreFactoryForTest(nullptr); |
| 80 PlatformTest::TearDown(); | 94 PlatformTest::TearDown(); |
| 81 } | 95 } |
| 82 | 96 |
| 83 void RegisterFactory(bool fails_first_reset) { | 97 void RegisterFactory(bool fails_first_reset, |
| 84 factory_.reset(new FakeV4StoreFactory(fails_first_reset)); | 98 bool hash_prefix_matches = true) { |
| 99 factory_.reset( |
| 100 new FakeV4StoreFactory(fails_first_reset, hash_prefix_matches)); |
| 85 V4Database::RegisterStoreFactoryForTest(factory_.get()); | 101 V4Database::RegisterStoreFactoryForTest(factory_.get()); |
| 86 } | 102 } |
| 87 | 103 |
| 88 void SetupInfoMapAndExpectedState() { | 104 void SetupInfoMapAndExpectedState() { |
| 89 UpdateListIdentifier win_malware_id(WINDOWS_PLATFORM, URL, MALWARE_THREAT); | 105 UpdateListIdentifier win_malware_id(WINDOWS_PLATFORM, URL, MALWARE_THREAT); |
| 90 store_file_name_map_[win_malware_id] = "win_url_malware"; | 106 store_file_name_map_[win_malware_id] = "win_url_malware"; |
| 91 expected_identifiers_.push_back(win_malware_id); | 107 expected_identifiers_.push_back(win_malware_id); |
| 92 expected_store_paths_.push_back( | 108 expected_store_paths_.push_back( |
| 93 database_dirname_.AppendASCII("win_url_malware.fake")); | 109 database_dirname_.AppendASCII("win_url_malware.fake")); |
| 94 | 110 |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 | 355 |
| 340 v4_database_->ApplyUpdate( | 356 v4_database_->ApplyUpdate( |
| 341 CreateFakeServerResponse(expected_store_state_map_, false), | 357 CreateFakeServerResponse(expected_store_state_map_, false), |
| 342 callback_db_updated_); | 358 callback_db_updated_); |
| 343 task_runner_->RunPendingTasks(); | 359 task_runner_->RunPendingTasks(); |
| 344 base::RunLoop().RunUntilIdle(); | 360 base::RunLoop().RunUntilIdle(); |
| 345 | 361 |
| 346 VerifyExpectedStoresState(false); | 362 VerifyExpectedStoresState(false); |
| 347 } | 363 } |
| 348 | 364 |
| 365 // Test to ensure the case that all stores match a given full hash. |
| 366 TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) { |
| 367 bool hash_prefix_matches = true; |
| 368 expected_resets_successfully_ = true; |
| 369 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); |
| 370 |
| 371 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 372 callback_db_ready_); |
| 373 created_but_not_called_back_ = true; |
| 374 task_runner_->RunPendingTasks(); |
| 375 |
| 376 base::RunLoop().RunUntilIdle(); |
| 377 EXPECT_EQ(true, created_and_called_back_); |
| 378 |
| 379 StoresMatched stores_matched; |
| 380 v4_database_->GetStoresMatchingFullHash("anything", &stores_matched); |
| 381 EXPECT_EQ(2u, stores_matched.size()); |
| 382 EXPECT_TRUE( |
| 383 stores_matched.end() != |
| 384 std::find(stores_matched.begin(), stores_matched.end(), |
| 385 UpdateListIdentifier(WINDOWS_PLATFORM, URL, MALWARE_THREAT))); |
| 386 EXPECT_TRUE( |
| 387 stores_matched.end() != |
| 388 std::find(stores_matched.begin(), stores_matched.end(), |
| 389 UpdateListIdentifier(LINUX_PLATFORM, URL, MALWARE_THREAT))); |
| 390 } |
| 391 |
| 392 // Test to ensure the case that no stores match a given full hash. |
| 393 TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { |
| 394 bool hash_prefix_matches = false; |
| 395 expected_resets_successfully_ = true; |
| 396 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); |
| 397 |
| 398 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 399 callback_db_ready_); |
| 400 created_but_not_called_back_ = true; |
| 401 task_runner_->RunPendingTasks(); |
| 402 |
| 403 base::RunLoop().RunUntilIdle(); |
| 404 EXPECT_EQ(true, created_and_called_back_); |
| 405 |
| 406 StoresMatched stores_matched; |
| 407 v4_database_->GetStoresMatchingFullHash("anything", &stores_matched); |
| 408 EXPECT_TRUE(stores_matched.empty()); |
| 409 } |
| 410 |
| 411 // Test to ensure the case that some stores match a given full hash. |
| 412 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { |
| 413 // Setup stores to not match the full hash. |
| 414 bool hash_prefix_matches = false; |
| 415 expected_resets_successfully_ = true; |
| 416 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); |
| 417 |
| 418 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 419 callback_db_ready_); |
| 420 created_but_not_called_back_ = true; |
| 421 task_runner_->RunPendingTasks(); |
| 422 |
| 423 base::RunLoop().RunUntilIdle(); |
| 424 EXPECT_EQ(true, created_and_called_back_); |
| 425 |
| 426 UpdateListIdentifier identifier(LINUX_PLATFORM, URL, MALWARE_THREAT); |
| 427 FakeV4Store* store = |
| 428 static_cast<FakeV4Store*>(v4_database_->store_map_->at(identifier).get()); |
| 429 // Set the store corresponding to identifier to match the full hash. |
| 430 store->set_hash_prefix_matches(true); |
| 431 |
| 432 StoresMatched stores_matched; |
| 433 v4_database_->GetStoresMatchingFullHash("anything", &stores_matched); |
| 434 EXPECT_EQ(1u, stores_matched.size()); |
| 435 EXPECT_EQ(identifier, stores_matched[0]); |
| 436 } |
| 437 |
| 349 } // namespace safe_browsing | 438 } // namespace safe_browsing |
| OLD | NEW |