OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/files/scoped_temp_dir.h" |
| 6 #include "base/memory/ptr_util.h" |
| 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/test/test_simple_task_runner.h" |
| 10 #include "components/safe_browsing_db/v4_database.h" |
| 11 #include "components/safe_browsing_db/v4_local_database_manager.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "testing/platform_test.h" |
| 14 |
| 15 namespace safe_browsing { |
| 16 |
| 17 class FakeV4Database : public V4Database { |
| 18 public: |
| 19 FakeV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 20 std::unique_ptr<StoreMap> store_map, |
| 21 const MatchedHashPrefixMap& matched_hash_prefix_map) |
| 22 : V4Database(db_task_runner, std::move(store_map)), |
| 23 matched_hash_prefix_map_(matched_hash_prefix_map) {} |
| 24 |
| 25 void GetStoresMatchingFullHash( |
| 26 const FullHash& full_hash, |
| 27 const base::hash_set<UpdateListIdentifier>& stores_to_look, |
| 28 MatchedHashPrefixMap* matched_hash_prefix_map) override { |
| 29 *matched_hash_prefix_map = matched_hash_prefix_map_; |
| 30 } |
| 31 |
| 32 private: |
| 33 const MatchedHashPrefixMap& matched_hash_prefix_map_; |
| 34 }; |
| 35 |
| 36 class V4LocalDatabaseManagerTest : public PlatformTest { |
| 37 public: |
| 38 V4LocalDatabaseManagerTest() : task_runner_(new base::TestSimpleTaskRunner) {} |
| 39 |
| 40 void SetUp() override { |
| 41 PlatformTest::SetUp(); |
| 42 |
| 43 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); |
| 44 DVLOG(1) << "base_dir_: " << base_dir_.path().value(); |
| 45 |
| 46 v4_local_database_manager_ = |
| 47 make_scoped_refptr(new V4LocalDatabaseManager(base_dir_.path())); |
| 48 v4_local_database_manager_->SetTaskRunnerForTest(task_runner_); |
| 49 |
| 50 SetupLocalDatabaseManager(); |
| 51 } |
| 52 |
| 53 void TearDown() override { |
| 54 v4_local_database_manager_->StopOnIOThread(true); |
| 55 |
| 56 // Force destruction of the database. |
| 57 task_runner_->RunPendingTasks(); |
| 58 base::RunLoop().RunUntilIdle(); |
| 59 |
| 60 PlatformTest::TearDown(); |
| 61 } |
| 62 |
| 63 void SetupLocalDatabaseManager() { |
| 64 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); |
| 65 |
| 66 task_runner_->RunPendingTasks(); |
| 67 base::RunLoop().RunUntilIdle(); |
| 68 } |
| 69 |
| 70 void ReplaceV4Database(const MatchedHashPrefixMap& matched_hash_prefix_map) { |
| 71 v4_local_database_manager_->v4_database_.reset(new FakeV4Database( |
| 72 task_runner_, base::MakeUnique<StoreMap>(), matched_hash_prefix_map)); |
| 73 } |
| 74 |
| 75 void ForceDisableLocalDatabaseManager() { |
| 76 v4_local_database_manager_->enabled_ = false; |
| 77 } |
| 78 |
| 79 base::ScopedTempDir base_dir_; |
| 80 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 81 content::TestBrowserThreadBundle thread_bundle_; |
| 82 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_; |
| 83 }; |
| 84 |
| 85 TEST_F(V4LocalDatabaseManagerTest, TestGetThreatSource) { |
| 86 EXPECT_EQ(ThreatSource::LOCAL_PVER4, |
| 87 v4_local_database_manager_->GetThreatSource()); |
| 88 } |
| 89 |
| 90 TEST_F(V4LocalDatabaseManagerTest, TestIsSupported) { |
| 91 EXPECT_TRUE(v4_local_database_manager_->IsSupported()); |
| 92 } |
| 93 |
| 94 TEST_F(V4LocalDatabaseManagerTest, TestCanCheckUrl) { |
| 95 EXPECT_TRUE( |
| 96 v4_local_database_manager_->CanCheckUrl(GURL("http://example.com/a/"))); |
| 97 EXPECT_TRUE( |
| 98 v4_local_database_manager_->CanCheckUrl(GURL("https://example.com/a/"))); |
| 99 EXPECT_TRUE( |
| 100 v4_local_database_manager_->CanCheckUrl(GURL("ftp://example.com/a/"))); |
| 101 EXPECT_FALSE( |
| 102 v4_local_database_manager_->CanCheckUrl(GURL("adp://example.com/a/"))); |
| 103 } |
| 104 |
| 105 TEST_F(V4LocalDatabaseManagerTest, |
| 106 TestCheckBrowseUrlWithEmptyStoresReturnsNoMatch) { |
| 107 // Both the stores are empty right now so CheckBrowseUrl should return true. |
| 108 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( |
| 109 GURL("http://example.com/a/"), nullptr)); |
| 110 } |
| 111 |
| 112 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) { |
| 113 MatchedHashPrefixMap matched_hash_prefix_map; |
| 114 matched_hash_prefix_map[GetUrlMalwareId()] = HashPrefix("aaaa"); |
| 115 ReplaceV4Database(matched_hash_prefix_map); |
| 116 |
| 117 // The fake database returns a matched hash prefix. |
| 118 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( |
| 119 GURL("http://example.com/a/"), nullptr)); |
| 120 } |
| 121 |
| 122 TEST_F(V4LocalDatabaseManagerTest, |
| 123 TestCheckBrowseUrlReturnsNoMatchWhenDisabled) { |
| 124 MatchedHashPrefixMap matched_hash_prefix_map; |
| 125 matched_hash_prefix_map[GetUrlMalwareId()] = HashPrefix("aaaa"); |
| 126 ReplaceV4Database(matched_hash_prefix_map); |
| 127 |
| 128 // The same URL returns |false| in the previous test because |
| 129 // v4_local_database_manager_ is enabled. |
| 130 ForceDisableLocalDatabaseManager(); |
| 131 |
| 132 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( |
| 133 GURL("http://example.com/a/"), nullptr)); |
| 134 } |
| 135 |
| 136 } // namespace safe_browsing |
OLD | NEW |