Chromium Code Reviews| 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 struct FakeV4DatabaseConfig { | |
| 18 MatchedHashPrefixMap matched_hash_prefix_map_; | |
|
Nathan Parker
2016/08/15 19:06:41
If this is the only member, you could just pass th
vakh (use Gerrit instead)
2016/08/17 18:23:06
I think we might need more fields later, but since
| |
| 19 }; | |
| 20 | |
| 21 class FakeV4Database : public V4Database { | |
| 22 public: | |
| 23 FakeV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | |
| 24 std::unique_ptr<StoreMap> store_map, | |
| 25 const FakeV4DatabaseConfig& config) | |
| 26 : V4Database(db_task_runner, std::move(store_map)), config_(config) {} | |
| 27 | |
| 28 void GetStoresMatchingFullHash( | |
| 29 const FullHash& full_hash, | |
| 30 const base::hash_set<UpdateListIdentifier>& stores_to_look, | |
| 31 MatchedHashPrefixMap* matched_hash_prefix_map) override { | |
| 32 for (const auto it : config_.matched_hash_prefix_map_) { | |
|
Nathan Parker
2016/08/15 19:06:41
I think the whole loop could be:
matched_hash_pre
Scott Hess - ex-Googler
2016/08/15 20:57:40
Wouldn't the erase() be redundant?
vakh (use Gerrit instead)
2016/08/17 18:23:06
Done.
| |
| 33 (*matched_hash_prefix_map)[it.first] = it.second; | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 private: | |
| 38 const FakeV4DatabaseConfig& config_; | |
| 39 }; | |
| 40 | |
| 41 class V4LocalDatabaseManagerTest : public PlatformTest { | |
| 42 public: | |
| 43 V4LocalDatabaseManagerTest() : task_runner_(new base::TestSimpleTaskRunner) {} | |
| 44 | |
| 45 void SetUp() override { | |
| 46 PlatformTest::SetUp(); | |
| 47 | |
| 48 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); | |
| 49 DVLOG(1) << "base_dir_: " << base_dir_.path().value(); | |
| 50 | |
| 51 v4_local_database_manager_ = | |
| 52 make_scoped_refptr(new V4LocalDatabaseManager(base_dir_.path())); | |
| 53 v4_local_database_manager_->SetTaskRunnerForTest(task_runner_); | |
| 54 | |
| 55 SetupLocalDatabaseManager(); | |
| 56 } | |
| 57 | |
| 58 void TearDown() override { | |
| 59 v4_local_database_manager_->StopOnIOThread(true); | |
| 60 | |
| 61 // Force destruction of the database. | |
| 62 task_runner_->RunPendingTasks(); | |
| 63 base::RunLoop().RunUntilIdle(); | |
| 64 | |
| 65 PlatformTest::TearDown(); | |
| 66 } | |
| 67 | |
| 68 void SetupLocalDatabaseManager() { | |
| 69 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); | |
| 70 | |
| 71 task_runner_->RunPendingTasks(); | |
| 72 base::RunLoop().RunUntilIdle(); | |
| 73 } | |
| 74 | |
| 75 void ReplaceV4Database(const FakeV4DatabaseConfig& config) { | |
| 76 v4_local_database_manager_->v4_database_.reset( | |
| 77 new FakeV4Database(task_runner_, base::MakeUnique<StoreMap>(), config)); | |
| 78 } | |
| 79 | |
| 80 void ForceDisableLocalDatabaseManager() { | |
| 81 v4_local_database_manager_->enabled_ = false; | |
| 82 } | |
| 83 | |
| 84 base::ScopedTempDir base_dir_; | |
| 85 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 86 content::TestBrowserThreadBundle thread_bundle_; | |
| 87 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_; | |
| 88 }; | |
| 89 | |
| 90 TEST_F(V4LocalDatabaseManagerTest, TestGetThreatSource) { | |
| 91 EXPECT_EQ(ThreatSource::LOCAL_PVER4, | |
| 92 v4_local_database_manager_->GetThreatSource()); | |
| 93 } | |
| 94 | |
| 95 TEST_F(V4LocalDatabaseManagerTest, TestIsSupported) { | |
| 96 EXPECT_TRUE(v4_local_database_manager_->IsSupported()); | |
| 97 } | |
| 98 | |
| 99 TEST_F(V4LocalDatabaseManagerTest, TestCanCheckUrl) { | |
| 100 EXPECT_TRUE(v4_local_database_manager_->CanCheckUrl(GURL("http://a/"))); | |
|
Scott Hess - ex-Googler
2016/08/15 20:57:39
Just wondering ... is there any possibility that a
vakh (use Gerrit instead)
2016/08/17 18:23:06
Done.
| |
| 101 EXPECT_TRUE(v4_local_database_manager_->CanCheckUrl(GURL("https://a/"))); | |
| 102 EXPECT_TRUE(v4_local_database_manager_->CanCheckUrl(GURL("ftp://a/"))); | |
| 103 EXPECT_FALSE(v4_local_database_manager_->CanCheckUrl(GURL("adp://a/"))); | |
| 104 } | |
| 105 | |
| 106 TEST_F(V4LocalDatabaseManagerTest, | |
| 107 TestCheckBrowseUrlWithEmptyStoresReturnsNoMatch) { | |
| 108 // Both the stores are empty right now so CheckBrowseUrl should return true. | |
| 109 EXPECT_TRUE( | |
| 110 v4_local_database_manager_->CheckBrowseUrl(GURL("http://a/"), nullptr)); | |
| 111 } | |
| 112 | |
| 113 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) { | |
| 114 FakeV4DatabaseConfig config; | |
| 115 config.matched_hash_prefix_map_[GetUrlMalwareId()] = HashPrefix("aaaa"); | |
| 116 ReplaceV4Database(config); | |
| 117 | |
| 118 // The fake database returns a matched hash prefix. | |
| 119 EXPECT_FALSE( | |
| 120 v4_local_database_manager_->CheckBrowseUrl(GURL("http://a/"), nullptr)); | |
|
Scott Hess - ex-Googler
2016/08/15 20:57:40
Looking at this line ... it appears non-self-docum
vakh (use Gerrit instead)
2016/08/17 18:23:06
I agree with that sentiment, but at the moment, I
Scott Hess - ex-Googler
2016/08/17 23:04:38
Do you mean that the fake will always hit? OK, I
vakh (use Gerrit instead)
2016/08/17 23:51:07
Yes, that's the case this test is targeting.
| |
| 121 } | |
| 122 | |
| 123 TEST_F(V4LocalDatabaseManagerTest, | |
| 124 TestCheckBrowseUrlReturnsNoMatchWhenDisabled) { | |
| 125 FakeV4DatabaseConfig config; | |
| 126 config.matched_hash_prefix_map_[GetUrlMalwareId()] = HashPrefix("aaaa"); | |
| 127 ReplaceV4Database(config); | |
| 128 | |
| 129 // The same URL returns |false| in the previous test because | |
| 130 // v4_local_database_manager_ is enabled. | |
| 131 ForceDisableLocalDatabaseManager(); | |
| 132 | |
| 133 EXPECT_TRUE( | |
| 134 v4_local_database_manager_->CheckBrowseUrl(GURL("http://a/"), nullptr)); | |
| 135 } | |
| 136 | |
| 137 } // namespace safe_browsing | |
| OLD | NEW |