| 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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 | 165 |
| 166 void SetTaskRunnerForTest() { | 166 void SetTaskRunnerForTest() { |
| 167 v4_local_database_manager_->SetTaskRunnerForTest(task_runner_); | 167 v4_local_database_manager_->SetTaskRunnerForTest(task_runner_); |
| 168 } | 168 } |
| 169 | 169 |
| 170 void StartLocalDatabaseManager() { | 170 void StartLocalDatabaseManager() { |
| 171 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); | 171 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); |
| 172 } | 172 } |
| 173 | 173 |
| 174 void StopLocalDatabaseManager() { | 174 void StopLocalDatabaseManager() { |
| 175 v4_local_database_manager_->StopOnIOThread(true); | 175 if (v4_local_database_manager_) { |
| 176 v4_local_database_manager_->StopOnIOThread(true); |
| 177 } |
| 176 | 178 |
| 177 // Force destruction of the database. | 179 // Force destruction of the database. |
| 178 task_runner_->RunPendingTasks(); | 180 WaitForTasksOnTaskRunner(); |
| 179 base::RunLoop().RunUntilIdle(); | |
| 180 } | 181 } |
| 181 | 182 |
| 182 void WaitForTasksOnTaskRunner() { | 183 void WaitForTasksOnTaskRunner() { |
| 183 // 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 |
| 184 // V4LocalDatabaseManager has read the data from disk. | 185 // V4LocalDatabaseManager has read the data from disk. |
| 185 task_runner_->RunPendingTasks(); | 186 task_runner_->RunPendingTasks(); |
| 186 base::RunLoop().RunUntilIdle(); | 187 base::RunLoop().RunUntilIdle(); |
| 187 } | 188 } |
| 188 | 189 |
| 189 base::ScopedTempDir base_dir_; | 190 base::ScopedTempDir base_dir_; |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( | 326 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( |
| 326 v4_local_database_manager_)); | 327 v4_local_database_manager_)); |
| 327 | 328 |
| 328 // Wait for PerformFullHashCheck to complete. | 329 // Wait for PerformFullHashCheck to complete. |
| 329 WaitForTasksOnTaskRunner(); | 330 WaitForTasksOnTaskRunner(); |
| 330 | 331 |
| 331 EXPECT_TRUE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( | 332 EXPECT_TRUE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( |
| 332 v4_local_database_manager_)); | 333 v4_local_database_manager_)); |
| 333 } | 334 } |
| 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 |
| 335 } // namespace safe_browsing | 366 } // namespace safe_browsing |
| OLD | NEW |