Chromium Code Reviews| 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 task_runner_->RunPendingTasks(); |
| 179 base::RunLoop().RunUntilIdle(); | 181 base::RunLoop().RunUntilIdle(); |
| 180 } | 182 } |
| 181 | 183 |
| 182 void WaitForTasksOnTaskRunner() { | 184 void WaitForTasksOnTaskRunner() { |
| 183 // Wait for tasks on the task runner so we're sure that the | 185 // Wait for tasks on the task runner so we're sure that the |
| 184 // V4LocalDatabaseManager has read the data from disk. | 186 // V4LocalDatabaseManager has read the data from disk. |
| 185 task_runner_->RunPendingTasks(); | 187 task_runner_->RunPendingTasks(); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( | 327 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( |
| 326 v4_local_database_manager_)); | 328 v4_local_database_manager_)); |
| 327 | 329 |
| 328 // Wait for PerformFullHashCheck to complete. | 330 // Wait for PerformFullHashCheck to complete. |
| 329 WaitForTasksOnTaskRunner(); | 331 WaitForTasksOnTaskRunner(); |
| 330 | 332 |
| 331 EXPECT_TRUE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( | 333 EXPECT_TRUE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( |
| 332 v4_local_database_manager_)); | 334 v4_local_database_manager_)); |
| 333 } | 335 } |
| 334 | 336 |
| 337 TEST_F(V4LocalDatabaseManagerTest, UsingWeakPtrDropsCallback) { | |
| 338 // StopLocalDatabaseManager before resetting it because that's what | |
| 339 // ~V4LocalDatabaseManager expects. | |
| 340 StopLocalDatabaseManager(); | |
| 341 v4_local_database_manager_ = | |
| 342 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath())); | |
| 343 SetTaskRunnerForTest(); | |
| 344 StartLocalDatabaseManager(); | |
| 345 WaitForTasksOnTaskRunner(); | |
| 346 net::TestURLFetcherFactory factory; | |
| 347 | |
| 348 StoreAndHashPrefixes store_and_hash_prefixes; | |
| 349 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); | |
| 350 ReplaceV4Database(store_and_hash_prefixes); | |
| 351 | |
| 352 // The fake database returns a matched hash prefix. | |
| 353 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( | |
| 354 GURL("http://example.com/a/"), nullptr)); | |
| 355 v4_local_database_manager_->StopOnIOThread(true); | |
| 356 | |
| 357 // Release the V4LocalDatabaseManager object right away before the callback | |
| 358 // gets called. When the callback gets called, without using a weak-ptr | |
| 359 // factory, this leads to a use after free. However, using the weak-ptr means | |
| 360 // that the callback is simply dropped. | |
| 361 v4_local_database_manager_ = nullptr; | |
| 362 } | |
|
Scott Hess - ex-Googler
2016/10/18 03:13:00
This seems like StopOnIOThread() maybe has posted
vakh (use Gerrit instead)
2016/10/18 17:37:44
Good idea. Done.
It is already done in TearDown, b
Scott Hess - ex-Googler
2016/10/18 20:09:56
Aha, I see what you mean, I hadn't connected the h
| |
| 363 | |
| 335 } // namespace safe_browsing | 364 } // namespace safe_browsing |
| OLD | NEW |