| 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 if (v4_local_database_manager_) { |
| 176 v4_local_database_manager_->StopOnIOThread(true); |
| 177 } |
| 147 | 178 |
| 148 // Force destruction of the database. | 179 // Force destruction of the database. |
| 149 task_runner_->RunPendingTasks(); | 180 WaitForTasksOnTaskRunner(); |
| 150 base::RunLoop().RunUntilIdle(); | |
| 151 } | 181 } |
| 152 | 182 |
| 153 void WaitForTasksOnTaskRunner() { | 183 void WaitForTasksOnTaskRunner() { |
| 154 // Wait for tasks on the task runner so we're sure that the | 184 // Wait for tasks on the task runner so we're sure that the |
| 155 // V4LocalDatabaseManager has read the data from disk. | 185 // V4LocalDatabaseManager has read the data from disk. |
| 156 task_runner_->RunPendingTasks(); | 186 task_runner_->RunPendingTasks(); |
| 157 base::RunLoop().RunUntilIdle(); | 187 base::RunLoop().RunUntilIdle(); |
| 158 } | 188 } |
| 159 | 189 |
| 160 base::ScopedTempDir base_dir_; | 190 base::ScopedTempDir base_dir_; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 WaitForTasksOnTaskRunner(); | 228 WaitForTasksOnTaskRunner(); |
| 199 net::TestURLFetcherFactory factory; | 229 net::TestURLFetcherFactory factory; |
| 200 | 230 |
| 201 StoreAndHashPrefixes store_and_hash_prefixes; | 231 StoreAndHashPrefixes store_and_hash_prefixes; |
| 202 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); | 232 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); |
| 203 ReplaceV4Database(store_and_hash_prefixes); | 233 ReplaceV4Database(store_and_hash_prefixes); |
| 204 | 234 |
| 205 // The fake database returns a matched hash prefix. | 235 // The fake database returns a matched hash prefix. |
| 206 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( | 236 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( |
| 207 GURL("http://example.com/a/"), nullptr)); | 237 GURL("http://example.com/a/"), nullptr)); |
| 238 |
| 239 // Wait for PerformFullHashCheck to complete. |
| 240 WaitForTasksOnTaskRunner(); |
| 208 } | 241 } |
| 209 | 242 |
| 210 TEST_F(V4LocalDatabaseManagerTest, | 243 TEST_F(V4LocalDatabaseManagerTest, |
| 211 TestCheckBrowseUrlReturnsNoMatchWhenDisabled) { | 244 TestCheckBrowseUrlReturnsNoMatchWhenDisabled) { |
| 212 WaitForTasksOnTaskRunner(); | 245 WaitForTasksOnTaskRunner(); |
| 213 | 246 |
| 214 // The same URL returns |false| in the previous test because | 247 // The same URL returns |false| in the previous test because |
| 215 // v4_local_database_manager_ is enabled. | 248 // v4_local_database_manager_ is enabled. |
| 216 ForceDisableLocalDatabaseManager(); | 249 ForceDisableLocalDatabaseManager(); |
| 217 | 250 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 | 294 |
| 262 ResetV4Database(); | 295 ResetV4Database(); |
| 263 v4_local_database_manager_->CheckBrowseUrl(url, &client); | 296 v4_local_database_manager_->CheckBrowseUrl(url, &client); |
| 264 // The database is unavailable so the check should get queued. | 297 // The database is unavailable so the check should get queued. |
| 265 EXPECT_EQ(1ul, GetQueuedChecks().size()); | 298 EXPECT_EQ(1ul, GetQueuedChecks().size()); |
| 266 | 299 |
| 267 StopLocalDatabaseManager(); | 300 StopLocalDatabaseManager(); |
| 268 EXPECT_TRUE(GetQueuedChecks().empty()); | 301 EXPECT_TRUE(GetQueuedChecks().empty()); |
| 269 } | 302 } |
| 270 | 303 |
| 304 // This test is somewhat similar to TestCheckBrowseUrlWithFakeDbReturnsMatch but |
| 305 // it uses a fake V4LocalDatabaseManager to assert that PerformFullHashCheck is |
| 306 // called async. |
| 307 TEST_F(V4LocalDatabaseManagerTest, PerformFullHashCheckCalledAsync) { |
| 308 // StopLocalDatabaseManager before resetting it because that's what |
| 309 // ~V4LocalDatabaseManager expects. |
| 310 StopLocalDatabaseManager(); |
| 311 v4_local_database_manager_ = |
| 312 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath())); |
| 313 SetTaskRunnerForTest(); |
| 314 StartLocalDatabaseManager(); |
| 315 WaitForTasksOnTaskRunner(); |
| 316 net::TestURLFetcherFactory factory; |
| 317 |
| 318 StoreAndHashPrefixes store_and_hash_prefixes; |
| 319 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); |
| 320 ReplaceV4Database(store_and_hash_prefixes); |
| 321 |
| 322 // The fake database returns a matched hash prefix. |
| 323 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( |
| 324 GURL("http://example.com/a/"), nullptr)); |
| 325 |
| 326 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( |
| 327 v4_local_database_manager_)); |
| 328 |
| 329 // Wait for PerformFullHashCheck to complete. |
| 330 WaitForTasksOnTaskRunner(); |
| 331 |
| 332 EXPECT_TRUE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( |
| 333 v4_local_database_manager_)); |
| 334 } |
| 335 |
| 336 TEST_F(V4LocalDatabaseManagerTest, UsingWeakPtrDropsCallback) { |
| 337 // StopLocalDatabaseManager before resetting it because that's what |
| 338 // ~V4LocalDatabaseManager expects. |
| 339 StopLocalDatabaseManager(); |
| 340 v4_local_database_manager_ = |
| 341 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath())); |
| 342 SetTaskRunnerForTest(); |
| 343 StartLocalDatabaseManager(); |
| 344 WaitForTasksOnTaskRunner(); |
| 345 net::TestURLFetcherFactory factory; |
| 346 |
| 347 StoreAndHashPrefixes store_and_hash_prefixes; |
| 348 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); |
| 349 ReplaceV4Database(store_and_hash_prefixes); |
| 350 |
| 351 // The fake database returns a matched hash prefix. |
| 352 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( |
| 353 GURL("http://example.com/a/"), nullptr)); |
| 354 v4_local_database_manager_->StopOnIOThread(true); |
| 355 |
| 356 // Release the V4LocalDatabaseManager object right away before the callback |
| 357 // gets called. When the callback gets called, without using a weak-ptr |
| 358 // factory, this leads to a use after free. However, using the weak-ptr means |
| 359 // that the callback is simply dropped. |
| 360 v4_local_database_manager_ = nullptr; |
| 361 |
| 362 // Wait for the tasks scheduled by StopOnIOThread to complete. |
| 363 WaitForTasksOnTaskRunner(); |
| 364 } |
| 365 |
| 271 } // namespace safe_browsing | 366 } // namespace safe_browsing |
| OLD | NEW |