| 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 "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "components/safe_browsing_db/v4_database.h" | 11 #include "components/safe_browsing_db/v4_database.h" |
| 12 #include "components/safe_browsing_db/v4_local_database_manager.h" | 12 #include "components/safe_browsing_db/v4_local_database_manager.h" |
| 13 #include "components/safe_browsing_db/v4_test_util.h" | 13 #include "components/safe_browsing_db/v4_test_util.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "crypto/sha2.h" | 15 #include "crypto/sha2.h" |
| 16 #include "net/url_request/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.h" |
| 17 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
| 18 | 18 |
| 19 namespace safe_browsing { | 19 namespace safe_browsing { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 typedef base::Callback<void()> NullCallback; |
| 24 |
| 23 // Utility function for populating hashes. | 25 // Utility function for populating hashes. |
| 24 FullHash HashForUrl(const GURL& url) { | 26 FullHash HashForUrl(const GURL& url) { |
| 25 std::vector<FullHash> full_hashes; | 27 std::vector<FullHash> full_hashes; |
| 26 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); | 28 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); |
| 27 // ASSERT_GE(full_hashes.size(), 1u); | 29 // ASSERT_GE(full_hashes.size(), 1u); |
| 28 return full_hashes[0]; | 30 return full_hashes[0]; |
| 29 } | 31 } |
| 30 | 32 |
| 31 // Always returns misses from GetFullHashes(). | 33 // Always returns misses from GetFullHashes(). |
| 32 class FakeGetHashProtocolManager : public V4GetHashProtocolManager { | 34 class FakeGetHashProtocolManager : public V4GetHashProtocolManager { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 }; | 183 }; |
| 182 | 184 |
| 183 class FakeV4LocalDatabaseManager : public V4LocalDatabaseManager { | 185 class FakeV4LocalDatabaseManager : public V4LocalDatabaseManager { |
| 184 public: | 186 public: |
| 185 void PerformFullHashCheck(std::unique_ptr<PendingCheck> check, | 187 void PerformFullHashCheck(std::unique_ptr<PendingCheck> check, |
| 186 const FullHashToStoreAndHashPrefixesMap& | 188 const FullHashToStoreAndHashPrefixesMap& |
| 187 full_hash_to_store_and_hash_prefixes) override { | 189 full_hash_to_store_and_hash_prefixes) override { |
| 188 perform_full_hash_check_called_ = true; | 190 perform_full_hash_check_called_ = true; |
| 189 } | 191 } |
| 190 | 192 |
| 191 FakeV4LocalDatabaseManager(const base::FilePath& base_path) | 193 FakeV4LocalDatabaseManager( |
| 192 : V4LocalDatabaseManager(base_path), | 194 const base::FilePath& base_path, |
| 195 ExtendedReportingLevelCallback extended_reporting_level_callback) |
| 196 : V4LocalDatabaseManager(base_path, extended_reporting_level_callback), |
| 193 perform_full_hash_check_called_(false) {} | 197 perform_full_hash_check_called_(false) {} |
| 194 | 198 |
| 195 static bool PerformFullHashCheckCalled( | 199 static bool PerformFullHashCheckCalled( |
| 196 scoped_refptr<safe_browsing::V4LocalDatabaseManager>& v4_ldbm) { | 200 scoped_refptr<safe_browsing::V4LocalDatabaseManager>& v4_ldbm) { |
| 197 FakeV4LocalDatabaseManager* fake = | 201 FakeV4LocalDatabaseManager* fake = |
| 198 static_cast<FakeV4LocalDatabaseManager*>(v4_ldbm.get()); | 202 static_cast<FakeV4LocalDatabaseManager*>(v4_ldbm.get()); |
| 199 return fake->perform_full_hash_check_called_; | 203 return fake->perform_full_hash_check_called_; |
| 200 } | 204 } |
| 201 | 205 |
| 202 private: | 206 private: |
| 203 ~FakeV4LocalDatabaseManager() override {} | 207 ~FakeV4LocalDatabaseManager() override {} |
| 204 | 208 |
| 205 bool perform_full_hash_check_called_; | 209 bool perform_full_hash_check_called_; |
| 206 }; | 210 }; |
| 207 | 211 |
| 208 class V4LocalDatabaseManagerTest : public PlatformTest { | 212 class V4LocalDatabaseManagerTest : public PlatformTest { |
| 209 public: | 213 public: |
| 210 V4LocalDatabaseManagerTest() : task_runner_(new base::TestSimpleTaskRunner) {} | 214 V4LocalDatabaseManagerTest() : task_runner_(new base::TestSimpleTaskRunner) {} |
| 211 | 215 |
| 212 void SetUp() override { | 216 void SetUp() override { |
| 213 PlatformTest::SetUp(); | 217 PlatformTest::SetUp(); |
| 214 | 218 |
| 215 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); | 219 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); |
| 216 DVLOG(1) << "base_dir_: " << base_dir_.GetPath().value(); | 220 DVLOG(1) << "base_dir_: " << base_dir_.GetPath().value(); |
| 217 | 221 |
| 218 v4_local_database_manager_ = | 222 extended_reporting_level_ = SBER_LEVEL_OFF; |
| 219 make_scoped_refptr(new V4LocalDatabaseManager(base_dir_.GetPath())); | 223 erl_callback_ = |
| 224 base::Bind(&V4LocalDatabaseManagerTest::GetExtendedReportingLevel, |
| 225 base::Unretained(this)); |
| 226 |
| 227 v4_local_database_manager_ = make_scoped_refptr( |
| 228 new V4LocalDatabaseManager(base_dir_.GetPath(), erl_callback_)); |
| 220 SetTaskRunnerForTest(); | 229 SetTaskRunnerForTest(); |
| 221 | 230 |
| 222 StartLocalDatabaseManager(); | 231 StartLocalDatabaseManager(); |
| 223 } | 232 } |
| 224 | 233 |
| 225 void TearDown() override { | 234 void TearDown() override { |
| 226 StopLocalDatabaseManager(); | 235 StopLocalDatabaseManager(); |
| 227 | 236 |
| 228 PlatformTest::TearDown(); | 237 PlatformTest::TearDown(); |
| 229 } | 238 } |
| 230 | 239 |
| 231 void ForceDisableLocalDatabaseManager() { | 240 void ForceDisableLocalDatabaseManager() { |
| 232 v4_local_database_manager_->enabled_ = false; | 241 v4_local_database_manager_->enabled_ = false; |
| 233 } | 242 } |
| 234 | 243 |
| 235 void ForceEnableLocalDatabaseManager() { | 244 void ForceEnableLocalDatabaseManager() { |
| 236 v4_local_database_manager_->enabled_ = true; | 245 v4_local_database_manager_->enabled_ = true; |
| 237 } | 246 } |
| 238 | 247 |
| 239 const V4LocalDatabaseManager::QueuedChecks& GetQueuedChecks() { | 248 const V4LocalDatabaseManager::QueuedChecks& GetQueuedChecks() { |
| 240 return v4_local_database_manager_->queued_checks_; | 249 return v4_local_database_manager_->queued_checks_; |
| 241 } | 250 } |
| 242 | 251 |
| 252 ExtendedReportingLevel GetExtendedReportingLevel() { |
| 253 return extended_reporting_level_; |
| 254 } |
| 255 |
| 243 void ReplaceV4Database(const StoreAndHashPrefixes& store_and_hash_prefixes, | 256 void ReplaceV4Database(const StoreAndHashPrefixes& store_and_hash_prefixes, |
| 244 bool stores_available = false) { | 257 bool stores_available = false) { |
| 245 // Disable the V4LocalDatabaseManager first so that if the callback to | 258 // Disable the V4LocalDatabaseManager first so that if the callback to |
| 246 // verify checksum has been scheduled, then it doesn't do anything when it | 259 // verify checksum has been scheduled, then it doesn't do anything when it |
| 247 // is called back. | 260 // is called back. |
| 248 ForceDisableLocalDatabaseManager(); | 261 ForceDisableLocalDatabaseManager(); |
| 249 // Wait to make sure that the callback gets executed if it has already been | 262 // Wait to make sure that the callback gets executed if it has already been |
| 250 // scheduled. | 263 // scheduled. |
| 251 WaitForTasksOnTaskRunner(); | 264 WaitForTasksOnTaskRunner(); |
| 252 // Re-enable the V4LocalDatabaseManager otherwise the checks won't work and | 265 // Re-enable the V4LocalDatabaseManager otherwise the checks won't work and |
| 253 // the fake database won't be set either. | 266 // the fake database won't be set either. |
| 254 ForceEnableLocalDatabaseManager(); | 267 ForceEnableLocalDatabaseManager(); |
| 255 | 268 |
| 256 NewDatabaseReadyCallback db_ready_callback = | 269 NewDatabaseReadyCallback db_ready_callback = |
| 257 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForChecks, | 270 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForChecks, |
| 258 base::Unretained(v4_local_database_manager_.get())); | 271 base::Unretained(v4_local_database_manager_.get())); |
| 259 FakeV4Database::Create(task_runner_, base::MakeUnique<StoreMap>(), | 272 FakeV4Database::Create(task_runner_, base::MakeUnique<StoreMap>(), |
| 260 store_and_hash_prefixes, db_ready_callback, | 273 store_and_hash_prefixes, db_ready_callback, |
| 261 stores_available); | 274 stores_available); |
| 262 WaitForTasksOnTaskRunner(); | 275 WaitForTasksOnTaskRunner(); |
| 263 } | 276 } |
| 264 | 277 |
| 265 void ResetLocalDatabaseManager() { | 278 void ResetLocalDatabaseManager() { |
| 266 StopLocalDatabaseManager(); | 279 StopLocalDatabaseManager(); |
| 267 v4_local_database_manager_ = | 280 v4_local_database_manager_ = make_scoped_refptr( |
| 268 make_scoped_refptr(new V4LocalDatabaseManager(base_dir_.GetPath())); | 281 new V4LocalDatabaseManager(base_dir_.GetPath(), erl_callback_)); |
| 269 SetTaskRunnerForTest(); | 282 SetTaskRunnerForTest(); |
| 270 StartLocalDatabaseManager(); | 283 StartLocalDatabaseManager(); |
| 271 } | 284 } |
| 272 | 285 |
| 273 void ResetV4Database() { | 286 void ResetV4Database() { |
| 274 V4Database::Destroy(std::move(v4_local_database_manager_->v4_database_)); | 287 V4Database::Destroy(std::move(v4_local_database_manager_->v4_database_)); |
| 275 } | 288 } |
| 276 | 289 |
| 277 void SetTaskRunnerForTest() { | 290 void SetTaskRunnerForTest() { |
| 278 v4_local_database_manager_->SetTaskRunnerForTest(task_runner_); | 291 v4_local_database_manager_->SetTaskRunnerForTest(task_runner_); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 297 // V4LocalDatabaseManager has read the data from disk. | 310 // V4LocalDatabaseManager has read the data from disk. |
| 298 task_runner_->RunPendingTasks(); | 311 task_runner_->RunPendingTasks(); |
| 299 base::RunLoop().RunUntilIdle(); | 312 base::RunLoop().RunUntilIdle(); |
| 300 } | 313 } |
| 301 | 314 |
| 302 // For those tests that need the fake manager | 315 // For those tests that need the fake manager |
| 303 void SetupFakeManager() { | 316 void SetupFakeManager() { |
| 304 // StopLocalDatabaseManager before resetting it because that's what | 317 // StopLocalDatabaseManager before resetting it because that's what |
| 305 // ~V4LocalDatabaseManager expects. | 318 // ~V4LocalDatabaseManager expects. |
| 306 StopLocalDatabaseManager(); | 319 StopLocalDatabaseManager(); |
| 307 v4_local_database_manager_ = | 320 v4_local_database_manager_ = make_scoped_refptr( |
| 308 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath())); | 321 new FakeV4LocalDatabaseManager(base_dir_.GetPath(), erl_callback_)); |
| 309 SetTaskRunnerForTest(); | 322 SetTaskRunnerForTest(); |
| 310 StartLocalDatabaseManager(); | 323 StartLocalDatabaseManager(); |
| 311 WaitForTasksOnTaskRunner(); | 324 WaitForTasksOnTaskRunner(); |
| 312 } | 325 } |
| 313 | 326 |
| 314 base::ScopedTempDir base_dir_; | 327 base::ScopedTempDir base_dir_; |
| 328 ExtendedReportingLevel extended_reporting_level_; |
| 329 ExtendedReportingLevelCallback erl_callback_; |
| 315 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 330 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 316 content::TestBrowserThreadBundle thread_bundle_; | 331 content::TestBrowserThreadBundle thread_bundle_; |
| 317 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_; | 332 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_; |
| 318 }; | 333 }; |
| 319 | 334 |
| 320 TEST_F(V4LocalDatabaseManagerTest, TestGetThreatSource) { | 335 TEST_F(V4LocalDatabaseManagerTest, TestGetThreatSource) { |
| 321 WaitForTasksOnTaskRunner(); | 336 WaitForTasksOnTaskRunner(); |
| 322 EXPECT_EQ(ThreatSource::LOCAL_PVER4, | 337 EXPECT_EQ(ThreatSource::LOCAL_PVER4, |
| 323 v4_local_database_manager_->GetThreatSource()); | 338 v4_local_database_manager_->GetThreatSource()); |
| 324 } | 339 } |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 EXPECT_FALSE(client.on_check_resource_url_result_called_); | 730 EXPECT_FALSE(client.on_check_resource_url_result_called_); |
| 716 WaitForTasksOnTaskRunner(); | 731 WaitForTasksOnTaskRunner(); |
| 717 EXPECT_TRUE(client.on_check_resource_url_result_called_); | 732 EXPECT_TRUE(client.on_check_resource_url_result_called_); |
| 718 } | 733 } |
| 719 | 734 |
| 720 // TODO(nparker): Add tests for | 735 // TODO(nparker): Add tests for |
| 721 // CheckDownloadUrl() | 736 // CheckDownloadUrl() |
| 722 // CheckExtensionIDs() | 737 // CheckExtensionIDs() |
| 723 | 738 |
| 724 } // namespace safe_browsing | 739 } // namespace safe_browsing |
| OLD | NEW |