| 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 "components/safe_browsing_db/v4_local_database_manager.h" | 5 #include "components/safe_browsing_db/v4_local_database_manager.h" |
| 6 #include "base/base64.h" | 6 #include "base/base64.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 on_check_resource_url_result_called_ = true; | 184 on_check_resource_url_result_called_ = true; |
| 185 } | 185 } |
| 186 | 186 |
| 187 SBThreatType expected_sb_threat_type; | 187 SBThreatType expected_sb_threat_type; |
| 188 GURL expected_url; | 188 GURL expected_url; |
| 189 bool on_check_browse_url_result_called_; | 189 bool on_check_browse_url_result_called_; |
| 190 bool on_check_resource_url_result_called_; | 190 bool on_check_resource_url_result_called_; |
| 191 V4LocalDatabaseManager* manager_to_cancel_; | 191 V4LocalDatabaseManager* manager_to_cancel_; |
| 192 }; | 192 }; |
| 193 | 193 |
| 194 class TestExtensionClient : public SafeBrowsingDatabaseManager::Client { |
| 195 public: |
| 196 TestExtensionClient(const std::set<FullHash>& expected_bad_crxs) |
| 197 : expected_bad_crxs(expected_bad_crxs), |
| 198 on_check_extensions_result_called_(false) {} |
| 199 |
| 200 void OnCheckExtensionsResult(const std::set<FullHash>& bad_crxs) override { |
| 201 EXPECT_EQ(expected_bad_crxs, bad_crxs); |
| 202 on_check_extensions_result_called_ = true; |
| 203 } |
| 204 |
| 205 const std::set<FullHash> expected_bad_crxs; |
| 206 bool on_check_extensions_result_called_; |
| 207 }; |
| 208 |
| 194 class FakeV4LocalDatabaseManager : public V4LocalDatabaseManager { | 209 class FakeV4LocalDatabaseManager : public V4LocalDatabaseManager { |
| 195 public: | 210 public: |
| 196 void PerformFullHashCheck(std::unique_ptr<PendingCheck> check, | 211 void PerformFullHashCheck(std::unique_ptr<PendingCheck> check, |
| 197 const FullHashToStoreAndHashPrefixesMap& | 212 const FullHashToStoreAndHashPrefixesMap& |
| 198 full_hash_to_store_and_hash_prefixes) override { | 213 full_hash_to_store_and_hash_prefixes) override { |
| 199 perform_full_hash_check_called_ = true; | 214 perform_full_hash_check_called_ = true; |
| 200 } | 215 } |
| 201 | 216 |
| 202 FakeV4LocalDatabaseManager( | 217 FakeV4LocalDatabaseManager( |
| 203 const base::FilePath& base_path, | 218 const base::FilePath& base_path, |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 // v4_local_database_manager_ is enabled. | 412 // v4_local_database_manager_ is enabled. |
| 398 ForceDisableLocalDatabaseManager(); | 413 ForceDisableLocalDatabaseManager(); |
| 399 | 414 |
| 400 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( | 415 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( |
| 401 GURL("http://example.com/a/"), nullptr)); | 416 GURL("http://example.com/a/"), nullptr)); |
| 402 } | 417 } |
| 403 | 418 |
| 404 TEST_F(V4LocalDatabaseManagerTest, TestGetSeverestThreatTypeAndMetadata) { | 419 TEST_F(V4LocalDatabaseManagerTest, TestGetSeverestThreatTypeAndMetadata) { |
| 405 WaitForTasksOnTaskRunner(); | 420 WaitForTasksOnTaskRunner(); |
| 406 | 421 |
| 407 FullHash full_hash("Malware"); | 422 FullHash fh_malware("Malware"); |
| 408 FullHashInfo fhi_malware(full_hash, GetUrlMalwareId(), base::Time::Now()); | 423 FullHashInfo fhi_malware(fh_malware, GetUrlMalwareId(), base::Time::Now()); |
| 409 fhi_malware.metadata.population_id = "malware_popid"; | 424 fhi_malware.metadata.population_id = "malware_popid"; |
| 410 | 425 |
| 411 FullHashInfo fhi_api(FullHash("api"), GetChromeUrlApiId(), base::Time::Now()); | 426 FullHash fh_api("api"); |
| 427 FullHashInfo fhi_api(fh_api, GetChromeUrlApiId(), base::Time::Now()); |
| 412 fhi_api.metadata.population_id = "api_popid"; | 428 fhi_api.metadata.population_id = "api_popid"; |
| 413 | 429 |
| 430 FullHash fh_example("example"); |
| 414 std::vector<FullHashInfo> fhis({fhi_malware, fhi_api}); | 431 std::vector<FullHashInfo> fhis({fhi_malware, fhi_api}); |
| 432 std::vector<FullHash> full_hashes({fh_malware, fh_example, fh_api}); |
| 415 | 433 |
| 434 std::vector<SBThreatType> full_hash_threat_types(full_hashes.size(), |
| 435 SB_THREAT_TYPE_SAFE); |
| 416 SBThreatType result_threat_type; | 436 SBThreatType result_threat_type; |
| 417 ThreatMetadata metadata; | 437 ThreatMetadata metadata; |
| 418 FullHash matching_full_hash; | 438 FullHash matching_full_hash; |
| 419 | 439 |
| 440 const std::vector<SBThreatType> expected_full_hash_threat_types( |
| 441 {SB_THREAT_TYPE_URL_MALWARE, SB_THREAT_TYPE_SAFE, |
| 442 SB_THREAT_TYPE_API_ABUSE}); |
| 443 |
| 420 v4_local_database_manager_->GetSeverestThreatTypeAndMetadata( | 444 v4_local_database_manager_->GetSeverestThreatTypeAndMetadata( |
| 421 &result_threat_type, &metadata, &matching_full_hash, fhis); | 445 fhis, full_hashes, &full_hash_threat_types, &result_threat_type, |
| 446 &metadata, &matching_full_hash); |
| 447 EXPECT_EQ(expected_full_hash_threat_types, full_hash_threat_types); |
| 448 |
| 422 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type); | 449 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type); |
| 423 EXPECT_EQ("malware_popid", metadata.population_id); | 450 EXPECT_EQ("malware_popid", metadata.population_id); |
| 424 EXPECT_EQ(full_hash, matching_full_hash); | 451 EXPECT_EQ(fh_malware, matching_full_hash); |
| 425 | 452 |
| 426 // Reversing the list has no effect. | 453 // Reversing the list has no effect. |
| 427 std::reverse(std::begin(fhis), std::end(fhis)); | 454 std::reverse(std::begin(fhis), std::end(fhis)); |
| 455 full_hash_threat_types.assign(full_hashes.size(), SB_THREAT_TYPE_SAFE); |
| 456 |
| 428 v4_local_database_manager_->GetSeverestThreatTypeAndMetadata( | 457 v4_local_database_manager_->GetSeverestThreatTypeAndMetadata( |
| 429 &result_threat_type, &metadata, &matching_full_hash, fhis); | 458 fhis, full_hashes, &full_hash_threat_types, &result_threat_type, |
| 459 &metadata, &matching_full_hash); |
| 460 EXPECT_EQ(expected_full_hash_threat_types, full_hash_threat_types); |
| 430 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type); | 461 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type); |
| 431 EXPECT_EQ("malware_popid", metadata.population_id); | 462 EXPECT_EQ("malware_popid", metadata.population_id); |
| 432 EXPECT_EQ(full_hash, matching_full_hash); | 463 EXPECT_EQ(fh_malware, matching_full_hash); |
| 433 } | 464 } |
| 434 | 465 |
| 435 TEST_F(V4LocalDatabaseManagerTest, TestChecksAreQueued) { | 466 TEST_F(V4LocalDatabaseManagerTest, TestChecksAreQueued) { |
| 436 const GURL url("https://www.example.com/"); | 467 const GURL url("https://www.example.com/"); |
| 437 TestClient client(SB_THREAT_TYPE_SAFE, url); | 468 TestClient client(SB_THREAT_TYPE_SAFE, url); |
| 438 EXPECT_TRUE(GetQueuedChecks().empty()); | 469 EXPECT_TRUE(GetQueuedChecks().empty()); |
| 439 v4_local_database_manager_->CheckBrowseUrl(url, &client); | 470 v4_local_database_manager_->CheckBrowseUrl(url, &client); |
| 440 // The database is unavailable so the check should get queued. | 471 // The database is unavailable so the check should get queued. |
| 441 EXPECT_EQ(1ul, GetQueuedChecks().size()); | 472 EXPECT_EQ(1ul, GetQueuedChecks().size()); |
| 442 | 473 |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 hash_prefix); | 825 hash_prefix); |
| 795 ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */); | 826 ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */); |
| 796 | 827 |
| 797 TestClient client(SB_THREAT_TYPE_BLACKLISTED_RESOURCE, url); | 828 TestClient client(SB_THREAT_TYPE_BLACKLISTED_RESOURCE, url); |
| 798 EXPECT_FALSE(v4_local_database_manager_->CheckResourceUrl(url, &client)); | 829 EXPECT_FALSE(v4_local_database_manager_->CheckResourceUrl(url, &client)); |
| 799 EXPECT_FALSE(client.on_check_resource_url_result_called_); | 830 EXPECT_FALSE(client.on_check_resource_url_result_called_); |
| 800 WaitForTasksOnTaskRunner(); | 831 WaitForTasksOnTaskRunner(); |
| 801 EXPECT_TRUE(client.on_check_resource_url_result_called_); | 832 EXPECT_TRUE(client.on_check_resource_url_result_called_); |
| 802 } | 833 } |
| 803 | 834 |
| 804 // TODO(nparker): Add tests for | 835 TEST_F(V4LocalDatabaseManagerTest, TestCheckExtensionIDsNothingBlacklisted) { |
| 836 // Setup to receive full-hash misses. |
| 837 ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({})); |
| 838 |
| 839 // Reset the database manager so it picks up the replacement protocol manager. |
| 840 ResetLocalDatabaseManager(); |
| 841 WaitForTasksOnTaskRunner(); |
| 842 |
| 843 // bad_extension_id is in the local DB but the full hash won't match. |
| 844 const FullHash bad_extension_id("aaaabbbbccccdddd"), |
| 845 good_extension_id("ddddccccbbbbaaaa"); |
| 846 |
| 847 // Put a match in the db that will cause a protocol-manager request. |
| 848 StoreAndHashPrefixes store_and_hash_prefixes; |
| 849 store_and_hash_prefixes.emplace_back(GetChromeExtMalwareId(), |
| 850 bad_extension_id); |
| 851 ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */); |
| 852 |
| 853 const std::set<FullHash> expected_bad_crxs({}); |
| 854 const std::set<FullHash> extension_ids({good_extension_id, bad_extension_id}); |
| 855 TestExtensionClient client(expected_bad_crxs); |
| 856 EXPECT_FALSE( |
| 857 v4_local_database_manager_->CheckExtensionIDs(extension_ids, &client)); |
| 858 EXPECT_FALSE(client.on_check_extensions_result_called_); |
| 859 WaitForTasksOnTaskRunner(); |
| 860 EXPECT_TRUE(client.on_check_extensions_result_called_); |
| 861 } |
| 862 |
| 863 TEST_F(V4LocalDatabaseManagerTest, TestCheckExtensionIDsOneIsBlacklisted) { |
| 864 // bad_extension_id is in the local DB and the full hash will match. |
| 865 const FullHash bad_extension_id("aaaabbbbccccdddd"), |
| 866 good_extension_id("ddddccccbbbbaaaa"); |
| 867 FullHashInfo fhi(bad_extension_id, GetChromeExtMalwareId(), base::Time()); |
| 868 |
| 869 // Setup to receive full-hash hit. |
| 870 ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({fhi})); |
| 871 |
| 872 // Reset the database manager so it picks up the replacement protocol manager. |
| 873 ResetLocalDatabaseManager(); |
| 874 WaitForTasksOnTaskRunner(); |
| 875 |
| 876 // Put a match in the db that will cause a protocol-manager request. |
| 877 StoreAndHashPrefixes store_and_hash_prefixes; |
| 878 store_and_hash_prefixes.emplace_back(GetChromeExtMalwareId(), |
| 879 bad_extension_id); |
| 880 ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */); |
| 881 |
| 882 const std::set<FullHash> expected_bad_crxs({bad_extension_id}); |
| 883 const std::set<FullHash> extension_ids({good_extension_id, bad_extension_id}); |
| 884 TestExtensionClient client(expected_bad_crxs); |
| 885 EXPECT_FALSE( |
| 886 v4_local_database_manager_->CheckExtensionIDs(extension_ids, &client)); |
| 887 EXPECT_FALSE(client.on_check_extensions_result_called_); |
| 888 WaitForTasksOnTaskRunner(); |
| 889 EXPECT_TRUE(client.on_check_extensions_result_called_); |
| 890 } |
| 891 |
| 892 // TODO(vakh): By 03/15/2017: Add tests for |
| 805 // CheckDownloadUrl() | 893 // CheckDownloadUrl() |
| 806 // CheckExtensionIDs() | |
| 807 | 894 |
| 808 } // namespace safe_browsing | 895 } // namespace safe_browsing |
| OLD | NEW |