| 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/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
| 6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/test_simple_task_runner.h" |
| 10 #include "components/safe_browsing_db/v4_database.h" | 10 #include "components/safe_browsing_db/v4_database.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 SBThreatType threat_type, | 72 SBThreatType threat_type, |
| 73 const ThreatMetadata& metadata) override { | 73 const ThreatMetadata& metadata) override { |
| 74 DCHECK_EQ(expected_url, url); | 74 DCHECK_EQ(expected_url, url); |
| 75 DCHECK_EQ(expected_sb_threat_type, threat_type); | 75 DCHECK_EQ(expected_sb_threat_type, threat_type); |
| 76 } | 76 } |
| 77 | 77 |
| 78 SBThreatType expected_sb_threat_type; | 78 SBThreatType expected_sb_threat_type; |
| 79 GURL expected_url; | 79 GURL expected_url; |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 class FakeV4LocalDatabaseManager : public V4LocalDatabaseManager { |
| 83 public: |
| 84 void PerformFullHashCheck(std::unique_ptr<PendingCheck> check, |
| 85 const FullHashToStoreAndHashPrefixesMap& |
| 86 full_hash_to_store_and_hash_prefixes) override { |
| 87 perform_full_hash_check_called_ = true; |
| 88 } |
| 89 |
| 90 FakeV4LocalDatabaseManager(const base::FilePath& base_path) |
| 91 : V4LocalDatabaseManager(base_path), |
| 92 perform_full_hash_check_called_(false) {} |
| 93 |
| 94 static bool PerformFullHashCheckCalled( |
| 95 scoped_refptr<safe_browsing::V4LocalDatabaseManager>& v4_ldbm) { |
| 96 FakeV4LocalDatabaseManager* fake = |
| 97 static_cast<FakeV4LocalDatabaseManager*>(v4_ldbm.get()); |
| 98 return fake->perform_full_hash_check_called_; |
| 99 } |
| 100 |
| 101 private: |
| 102 ~FakeV4LocalDatabaseManager() override {} |
| 103 |
| 104 bool perform_full_hash_check_called_; |
| 105 }; |
| 106 |
| 82 class V4LocalDatabaseManagerTest : public PlatformTest { | 107 class V4LocalDatabaseManagerTest : public PlatformTest { |
| 83 public: | 108 public: |
| 84 V4LocalDatabaseManagerTest() : task_runner_(new base::TestSimpleTaskRunner) {} | 109 V4LocalDatabaseManagerTest() : task_runner_(new base::TestSimpleTaskRunner) {} |
| 85 | 110 |
| 86 void SetUp() override { | 111 void SetUp() override { |
| 87 PlatformTest::SetUp(); | 112 PlatformTest::SetUp(); |
| 88 | 113 |
| 89 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); | 114 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); |
| 90 DVLOG(1) << "base_dir_: " << base_dir_.GetPath().value(); | 115 DVLOG(1) << "base_dir_: " << base_dir_.GetPath().value(); |
| 91 | 116 |
| 92 v4_local_database_manager_ = | 117 v4_local_database_manager_ = |
| 93 make_scoped_refptr(new V4LocalDatabaseManager(base_dir_.GetPath())); | 118 make_scoped_refptr(new V4LocalDatabaseManager(base_dir_.GetPath())); |
| 94 v4_local_database_manager_->SetTaskRunnerForTest(task_runner_); | 119 SetTaskRunnerForTest(); |
| 95 | 120 |
| 96 StartLocalDatabaseManager(); | 121 StartLocalDatabaseManager(); |
| 97 } | 122 } |
| 98 | 123 |
| 99 void TearDown() override { | 124 void TearDown() override { |
| 100 StopLocalDatabaseManager(); | 125 StopLocalDatabaseManager(); |
| 101 | 126 |
| 102 PlatformTest::TearDown(); | 127 PlatformTest::TearDown(); |
| 103 } | 128 } |
| 104 | 129 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 131 base::Unretained(v4_local_database_manager_.get())); | 156 base::Unretained(v4_local_database_manager_.get())); |
| 132 FakeV4Database::Create(task_runner_, base::MakeUnique<StoreMap>(), | 157 FakeV4Database::Create(task_runner_, base::MakeUnique<StoreMap>(), |
| 133 store_and_hash_prefixes, db_ready_callback); | 158 store_and_hash_prefixes, db_ready_callback); |
| 134 WaitForTasksOnTaskRunner(); | 159 WaitForTasksOnTaskRunner(); |
| 135 } | 160 } |
| 136 | 161 |
| 137 void ResetV4Database() { | 162 void ResetV4Database() { |
| 138 V4Database::Destroy(std::move(v4_local_database_manager_->v4_database_)); | 163 V4Database::Destroy(std::move(v4_local_database_manager_->v4_database_)); |
| 139 } | 164 } |
| 140 | 165 |
| 166 void SetTaskRunnerForTest() { |
| 167 v4_local_database_manager_->SetTaskRunnerForTest(task_runner_); |
| 168 } |
| 169 |
| 141 void StartLocalDatabaseManager() { | 170 void StartLocalDatabaseManager() { |
| 142 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); | 171 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); |
| 143 } | 172 } |
| 144 | 173 |
| 145 void StopLocalDatabaseManager() { | 174 void StopLocalDatabaseManager() { |
| 146 v4_local_database_manager_->StopOnIOThread(true); | 175 v4_local_database_manager_->StopOnIOThread(true); |
| 147 | 176 |
| 148 // Force destruction of the database. | 177 // Force destruction of the database. |
| 149 task_runner_->RunPendingTasks(); | 178 task_runner_->RunPendingTasks(); |
| 150 base::RunLoop().RunUntilIdle(); | 179 base::RunLoop().RunUntilIdle(); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 WaitForTasksOnTaskRunner(); | 227 WaitForTasksOnTaskRunner(); |
| 199 net::TestURLFetcherFactory factory; | 228 net::TestURLFetcherFactory factory; |
| 200 | 229 |
| 201 StoreAndHashPrefixes store_and_hash_prefixes; | 230 StoreAndHashPrefixes store_and_hash_prefixes; |
| 202 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); | 231 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); |
| 203 ReplaceV4Database(store_and_hash_prefixes); | 232 ReplaceV4Database(store_and_hash_prefixes); |
| 204 | 233 |
| 205 // The fake database returns a matched hash prefix. | 234 // The fake database returns a matched hash prefix. |
| 206 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( | 235 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( |
| 207 GURL("http://example.com/a/"), nullptr)); | 236 GURL("http://example.com/a/"), nullptr)); |
| 237 |
| 238 // Wait for PerformFullHashCheck to complete. |
| 239 WaitForTasksOnTaskRunner(); |
| 208 } | 240 } |
| 209 | 241 |
| 210 TEST_F(V4LocalDatabaseManagerTest, | 242 TEST_F(V4LocalDatabaseManagerTest, |
| 211 TestCheckBrowseUrlReturnsNoMatchWhenDisabled) { | 243 TestCheckBrowseUrlReturnsNoMatchWhenDisabled) { |
| 212 WaitForTasksOnTaskRunner(); | 244 WaitForTasksOnTaskRunner(); |
| 213 | 245 |
| 214 // The same URL returns |false| in the previous test because | 246 // The same URL returns |false| in the previous test because |
| 215 // v4_local_database_manager_ is enabled. | 247 // v4_local_database_manager_ is enabled. |
| 216 ForceDisableLocalDatabaseManager(); | 248 ForceDisableLocalDatabaseManager(); |
| 217 | 249 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 | 293 |
| 262 ResetV4Database(); | 294 ResetV4Database(); |
| 263 v4_local_database_manager_->CheckBrowseUrl(url, &client); | 295 v4_local_database_manager_->CheckBrowseUrl(url, &client); |
| 264 // The database is unavailable so the check should get queued. | 296 // The database is unavailable so the check should get queued. |
| 265 EXPECT_EQ(1ul, GetQueuedChecks().size()); | 297 EXPECT_EQ(1ul, GetQueuedChecks().size()); |
| 266 | 298 |
| 267 StopLocalDatabaseManager(); | 299 StopLocalDatabaseManager(); |
| 268 EXPECT_TRUE(GetQueuedChecks().empty()); | 300 EXPECT_TRUE(GetQueuedChecks().empty()); |
| 269 } | 301 } |
| 270 | 302 |
| 303 // This test is somewhat similar to TestCheckBrowseUrlWithFakeDbReturnsMatch but |
| 304 // it uses a fake V4LocalDatabaseManager to assert that PerformFullHashCheck is |
| 305 // called async. |
| 306 TEST_F(V4LocalDatabaseManagerTest, PerformFullHashCheckCalledAsync) { |
| 307 // StopLocalDatabaseManager before resetting it because that's what |
| 308 // ~V4LocalDatabaseManager expects. |
| 309 StopLocalDatabaseManager(); |
| 310 v4_local_database_manager_ = |
| 311 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath())); |
| 312 SetTaskRunnerForTest(); |
| 313 StartLocalDatabaseManager(); |
| 314 WaitForTasksOnTaskRunner(); |
| 315 net::TestURLFetcherFactory factory; |
| 316 |
| 317 StoreAndHashPrefixes store_and_hash_prefixes; |
| 318 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); |
| 319 ReplaceV4Database(store_and_hash_prefixes); |
| 320 |
| 321 // The fake database returns a matched hash prefix. |
| 322 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( |
| 323 GURL("http://example.com/a/"), nullptr)); |
| 324 |
| 325 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( |
| 326 v4_local_database_manager_)); |
| 327 |
| 328 // Wait for PerformFullHashCheck to complete. |
| 329 WaitForTasksOnTaskRunner(); |
| 330 |
| 331 EXPECT_TRUE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( |
| 332 v4_local_database_manager_)); |
| 333 } |
| 334 |
| 271 } // namespace safe_browsing | 335 } // namespace safe_browsing |
| OLD | NEW |