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" |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 const bool stores_available_; | 140 const bool stores_available_; |
141 }; | 141 }; |
142 | 142 |
143 class TestClient : public SafeBrowsingDatabaseManager::Client { | 143 class TestClient : public SafeBrowsingDatabaseManager::Client { |
144 public: | 144 public: |
145 TestClient(SBThreatType sb_threat_type, | 145 TestClient(SBThreatType sb_threat_type, |
146 const GURL& url, | 146 const GURL& url, |
147 V4LocalDatabaseManager* manager_to_cancel = nullptr) | 147 V4LocalDatabaseManager* manager_to_cancel = nullptr) |
148 : expected_sb_threat_type(sb_threat_type), | 148 : expected_sb_threat_type(sb_threat_type), |
149 expected_url(url), | 149 expected_url(url), |
150 result_received_(false), | 150 on_check_browse_url_result_called_(false), |
| 151 on_check_resource_url_result_called_(false), |
151 manager_to_cancel_(manager_to_cancel) {} | 152 manager_to_cancel_(manager_to_cancel) {} |
152 | 153 |
153 void OnCheckBrowseUrlResult(const GURL& url, | 154 void OnCheckBrowseUrlResult(const GURL& url, |
154 SBThreatType threat_type, | 155 SBThreatType threat_type, |
155 const ThreatMetadata& metadata) override { | 156 const ThreatMetadata& metadata) override { |
156 DCHECK_EQ(expected_url, url); | 157 DCHECK_EQ(expected_url, url); |
157 DCHECK_EQ(expected_sb_threat_type, threat_type); | 158 DCHECK_EQ(expected_sb_threat_type, threat_type); |
158 result_received_ = true; | 159 on_check_browse_url_result_called_ = true; |
159 if (manager_to_cancel_) { | 160 if (manager_to_cancel_) { |
160 manager_to_cancel_->CancelCheck(this); | 161 manager_to_cancel_->CancelCheck(this); |
161 } | 162 } |
162 } | 163 } |
163 | 164 |
| 165 void OnCheckResourceUrlResult(const GURL& url, |
| 166 SBThreatType threat_type, |
| 167 const std::string& threat_hash) override { |
| 168 DCHECK_EQ(expected_url, url); |
| 169 DCHECK_EQ(expected_sb_threat_type, threat_type); |
| 170 // |threat_hash| is empty because GetFullHashes calls back with empty |
| 171 // |full_hash_infos|. |
| 172 DCHECK(threat_hash.empty()); |
| 173 on_check_resource_url_result_called_ = true; |
| 174 } |
| 175 |
164 SBThreatType expected_sb_threat_type; | 176 SBThreatType expected_sb_threat_type; |
165 GURL expected_url; | 177 GURL expected_url; |
166 bool result_received_; | 178 bool on_check_browse_url_result_called_; |
| 179 bool on_check_resource_url_result_called_; |
167 V4LocalDatabaseManager* manager_to_cancel_; | 180 V4LocalDatabaseManager* manager_to_cancel_; |
168 }; | 181 }; |
169 | 182 |
170 class FakeV4LocalDatabaseManager : public V4LocalDatabaseManager { | 183 class FakeV4LocalDatabaseManager : public V4LocalDatabaseManager { |
171 public: | 184 public: |
172 void PerformFullHashCheck(std::unique_ptr<PendingCheck> check, | 185 void PerformFullHashCheck(std::unique_ptr<PendingCheck> check, |
173 const FullHashToStoreAndHashPrefixesMap& | 186 const FullHashToStoreAndHashPrefixesMap& |
174 full_hash_to_store_and_hash_prefixes) override { | 187 full_hash_to_store_and_hash_prefixes) override { |
175 perform_full_hash_check_called_ = true; | 188 perform_full_hash_check_called_ = true; |
176 } | 189 } |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 // v4_local_database_manager_ is enabled. | 373 // v4_local_database_manager_ is enabled. |
361 ForceDisableLocalDatabaseManager(); | 374 ForceDisableLocalDatabaseManager(); |
362 | 375 |
363 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( | 376 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( |
364 GURL("http://example.com/a/"), nullptr)); | 377 GURL("http://example.com/a/"), nullptr)); |
365 } | 378 } |
366 | 379 |
367 TEST_F(V4LocalDatabaseManagerTest, TestGetSeverestThreatTypeAndMetadata) { | 380 TEST_F(V4LocalDatabaseManagerTest, TestGetSeverestThreatTypeAndMetadata) { |
368 WaitForTasksOnTaskRunner(); | 381 WaitForTasksOnTaskRunner(); |
369 | 382 |
370 FullHashInfo fhi_malware(FullHash("Malware"), GetUrlMalwareId(), | 383 FullHash full_hash("Malware"); |
371 base::Time::Now()); | 384 FullHashInfo fhi_malware(full_hash, GetUrlMalwareId(), base::Time::Now()); |
372 fhi_malware.metadata.population_id = "malware_popid"; | 385 fhi_malware.metadata.population_id = "malware_popid"; |
373 | 386 |
374 FullHashInfo fhi_api(FullHash("api"), GetChromeUrlApiId(), base::Time::Now()); | 387 FullHashInfo fhi_api(FullHash("api"), GetChromeUrlApiId(), base::Time::Now()); |
375 fhi_api.metadata.population_id = "api_popid"; | 388 fhi_api.metadata.population_id = "api_popid"; |
376 | 389 |
377 std::vector<FullHashInfo> fhis({fhi_malware, fhi_api}); | 390 std::vector<FullHashInfo> fhis({fhi_malware, fhi_api}); |
378 | 391 |
379 SBThreatType result_threat_type; | 392 SBThreatType result_threat_type; |
380 ThreatMetadata metadata; | 393 ThreatMetadata metadata; |
| 394 FullHash matching_full_hash; |
381 | 395 |
382 v4_local_database_manager_->GetSeverestThreatTypeAndMetadata( | 396 v4_local_database_manager_->GetSeverestThreatTypeAndMetadata( |
383 &result_threat_type, &metadata, fhis); | 397 &result_threat_type, &metadata, &matching_full_hash, fhis); |
384 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type); | 398 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type); |
385 EXPECT_EQ("malware_popid", metadata.population_id); | 399 EXPECT_EQ("malware_popid", metadata.population_id); |
| 400 EXPECT_EQ(full_hash, matching_full_hash); |
386 | 401 |
387 // Reversing the list has no effect. | 402 // Reversing the list has no effect. |
388 std::reverse(std::begin(fhis), std::end(fhis)); | 403 std::reverse(std::begin(fhis), std::end(fhis)); |
389 v4_local_database_manager_->GetSeverestThreatTypeAndMetadata( | 404 v4_local_database_manager_->GetSeverestThreatTypeAndMetadata( |
390 &result_threat_type, &metadata, fhis); | 405 &result_threat_type, &metadata, &matching_full_hash, fhis); |
391 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type); | 406 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type); |
392 EXPECT_EQ("malware_popid", metadata.population_id); | 407 EXPECT_EQ("malware_popid", metadata.population_id); |
| 408 EXPECT_EQ(full_hash, matching_full_hash); |
393 } | 409 } |
394 | 410 |
395 TEST_F(V4LocalDatabaseManagerTest, TestChecksAreQueued) { | 411 TEST_F(V4LocalDatabaseManagerTest, TestChecksAreQueued) { |
396 const GURL url("https://www.example.com/"); | 412 const GURL url("https://www.example.com/"); |
397 TestClient client(SB_THREAT_TYPE_SAFE, url); | 413 TestClient client(SB_THREAT_TYPE_SAFE, url); |
398 EXPECT_TRUE(GetQueuedChecks().empty()); | 414 EXPECT_TRUE(GetQueuedChecks().empty()); |
399 v4_local_database_manager_->CheckBrowseUrl(url, &client); | 415 v4_local_database_manager_->CheckBrowseUrl(url, &client); |
400 // The database is unavailable so the check should get queued. | 416 // The database is unavailable so the check should get queued. |
401 EXPECT_EQ(1ul, GetQueuedChecks().size()); | 417 EXPECT_EQ(1ul, GetQueuedChecks().size()); |
402 | 418 |
(...skipping 25 matching lines...) Expand all Loading... |
428 | 444 |
429 // Put a match in the db that will cause a protocol-manager request. | 445 // Put a match in the db that will cause a protocol-manager request. |
430 StoreAndHashPrefixes store_and_hash_prefixes; | 446 StoreAndHashPrefixes store_and_hash_prefixes; |
431 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), hash_prefix); | 447 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), hash_prefix); |
432 ReplaceV4Database(store_and_hash_prefixes); | 448 ReplaceV4Database(store_and_hash_prefixes); |
433 | 449 |
434 // Test that a request flows through to the callback. | 450 // Test that a request flows through to the callback. |
435 { | 451 { |
436 TestClient client(SB_THREAT_TYPE_SAFE, url); | 452 TestClient client(SB_THREAT_TYPE_SAFE, url); |
437 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(url, &client)); | 453 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(url, &client)); |
438 EXPECT_FALSE(client.result_received_); | 454 EXPECT_FALSE(client.on_check_browse_url_result_called_); |
439 WaitForTasksOnTaskRunner(); | 455 WaitForTasksOnTaskRunner(); |
440 EXPECT_TRUE(client.result_received_); | 456 EXPECT_TRUE(client.on_check_browse_url_result_called_); |
441 } | 457 } |
442 | 458 |
443 // Test that cancel prevents the callback from being called. | 459 // Test that cancel prevents the callback from being called. |
444 { | 460 { |
445 TestClient client(SB_THREAT_TYPE_SAFE, url); | 461 TestClient client(SB_THREAT_TYPE_SAFE, url); |
446 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(url, &client)); | 462 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(url, &client)); |
447 v4_local_database_manager_->CancelCheck(&client); | 463 v4_local_database_manager_->CancelCheck(&client); |
448 EXPECT_FALSE(client.result_received_); | 464 EXPECT_FALSE(client.on_check_browse_url_result_called_); |
449 WaitForTasksOnTaskRunner(); | 465 WaitForTasksOnTaskRunner(); |
450 EXPECT_FALSE(client.result_received_); | 466 EXPECT_FALSE(client.on_check_browse_url_result_called_); |
451 } | 467 } |
452 } | 468 } |
453 | 469 |
454 // When the database load flushes the queued requests, make sure that | 470 // When the database load flushes the queued requests, make sure that |
455 // CancelCheck() is not fatal in the client callback. | 471 // CancelCheck() is not fatal in the client callback. |
456 TEST_F(V4LocalDatabaseManagerTest, CancelQueued) { | 472 TEST_F(V4LocalDatabaseManagerTest, CancelQueued) { |
457 const GURL url("http://example.com/a/"); | 473 const GURL url("http://example.com/a/"); |
458 | 474 |
459 TestClient client1(SB_THREAT_TYPE_SAFE, url, | 475 TestClient client1(SB_THREAT_TYPE_SAFE, url, |
460 v4_local_database_manager_.get()); | 476 v4_local_database_manager_.get()); |
461 TestClient client2(SB_THREAT_TYPE_SAFE, url); | 477 TestClient client2(SB_THREAT_TYPE_SAFE, url); |
462 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(url, &client1)); | 478 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(url, &client1)); |
463 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(url, &client2)); | 479 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(url, &client2)); |
464 EXPECT_EQ(2ul, GetQueuedChecks().size()); | 480 EXPECT_EQ(2ul, GetQueuedChecks().size()); |
465 EXPECT_FALSE(client1.result_received_); | 481 EXPECT_FALSE(client1.on_check_browse_url_result_called_); |
466 EXPECT_FALSE(client2.result_received_); | 482 EXPECT_FALSE(client2.on_check_browse_url_result_called_); |
467 WaitForTasksOnTaskRunner(); | 483 WaitForTasksOnTaskRunner(); |
468 EXPECT_TRUE(client1.result_received_); | 484 EXPECT_TRUE(client1.on_check_browse_url_result_called_); |
469 EXPECT_TRUE(client2.result_received_); | 485 EXPECT_TRUE(client2.on_check_browse_url_result_called_); |
470 } | 486 } |
471 | 487 |
472 // This test is somewhat similar to TestCheckBrowseUrlWithFakeDbReturnsMatch but | 488 // This test is somewhat similar to TestCheckBrowseUrlWithFakeDbReturnsMatch but |
473 // it uses a fake V4LocalDatabaseManager to assert that PerformFullHashCheck is | 489 // it uses a fake V4LocalDatabaseManager to assert that PerformFullHashCheck is |
474 // called async. | 490 // called async. |
475 TEST_F(V4LocalDatabaseManagerTest, PerformFullHashCheckCalledAsync) { | 491 TEST_F(V4LocalDatabaseManagerTest, PerformFullHashCheckCalledAsync) { |
476 SetupFakeManager(); | 492 SetupFakeManager(); |
477 net::TestURLFetcherFactory factory; | 493 net::TestURLFetcherFactory factory; |
478 | 494 |
479 StoreAndHashPrefixes store_and_hash_prefixes; | 495 StoreAndHashPrefixes store_and_hash_prefixes; |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 // That check gets queued. Now, let's cancel the check. After this, we should | 678 // That check gets queued. Now, let's cancel the check. After this, we should |
663 // not receive a call for |OnCheckBrowseUrlResult| with |first_url|. | 679 // not receive a call for |OnCheckBrowseUrlResult| with |first_url|. |
664 v4_local_database_manager_->CancelCheck(&client); | 680 v4_local_database_manager_->CancelCheck(&client); |
665 | 681 |
666 // Now, re-use that client but for |second_url|. | 682 // Now, re-use that client but for |second_url|. |
667 client.expected_url = second_url; | 683 client.expected_url = second_url; |
668 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(second_url, &client)); | 684 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(second_url, &client)); |
669 | 685 |
670 // Wait for PerformFullHashCheck to complete. | 686 // Wait for PerformFullHashCheck to complete. |
671 WaitForTasksOnTaskRunner(); | 687 WaitForTasksOnTaskRunner(); |
672 // |result_received_| is true only if OnCheckBrowseUrlResult gets called with | 688 // |on_check_browse_url_result_called_| is true only if OnCheckBrowseUrlResult |
673 // the |url| equal to |expected_url|, which is |second_url| in this test. | 689 // gets called with the |url| equal to |expected_url|, which is |second_url| |
674 EXPECT_TRUE(client.result_received_); | 690 // in |
| 691 // this test. |
| 692 EXPECT_TRUE(client.on_check_browse_url_result_called_); |
| 693 } |
| 694 |
| 695 TEST_F(V4LocalDatabaseManagerTest, TestCheckResourceUrl) { |
| 696 // Setup to receive full-hash misses. |
| 697 ScopedFakeGetHashProtocolManagerFactory pin; |
| 698 |
| 699 // Reset the database manager so it picks up the replacement protocol manager. |
| 700 ResetLocalDatabaseManager(); |
| 701 WaitForTasksOnTaskRunner(); |
| 702 |
| 703 // An URL and matching prefix. |
| 704 const GURL url("http://example.com/a/"); |
| 705 const HashPrefix hash_prefix("eW\x1A\xF\xA9"); |
| 706 |
| 707 // Put a match in the db that will cause a protocol-manager request. |
| 708 StoreAndHashPrefixes store_and_hash_prefixes; |
| 709 store_and_hash_prefixes.emplace_back(GetChromeUrlClientIncidentId(), |
| 710 hash_prefix); |
| 711 ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */); |
| 712 |
| 713 TestClient client(SB_THREAT_TYPE_SAFE, url); |
| 714 EXPECT_FALSE(v4_local_database_manager_->CheckResourceUrl(url, &client)); |
| 715 EXPECT_FALSE(client.on_check_resource_url_result_called_); |
| 716 WaitForTasksOnTaskRunner(); |
| 717 EXPECT_TRUE(client.on_check_resource_url_result_called_); |
675 } | 718 } |
676 | 719 |
677 // TODO(nparker): Add tests for | 720 // TODO(nparker): Add tests for |
678 // CheckDownloadUrl() | 721 // CheckDownloadUrl() |
679 // CheckExtensionIDs() | 722 // CheckExtensionIDs() |
680 // CheckResourceUrl() | |
681 | 723 |
682 } // namespace safe_browsing | 724 } // namespace safe_browsing |
OLD | NEW |