Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: components/safe_browsing_db/v4_local_database_manager_unittest.cc

Issue 2680163002: S: GetThreatSeverity should handle the case of CheckResourceUrl. (Closed)
Patch Set: Nit: s/Time::Time/Time Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/safe_browsing_db/v4_local_database_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
6 #include "base/base64.h"
5 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
6 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
7 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
8 #include "base/run_loop.h" 10 #include "base/run_loop.h"
9 #include "base/test/test_simple_task_runner.h" 11 #include "base/test/test_simple_task_runner.h"
10 #include "base/threading/thread_task_runner_handle.h" 12 #include "base/threading/thread_task_runner_handle.h"
11 #include "components/safe_browsing_db/v4_database.h" 13 #include "components/safe_browsing_db/v4_database.h"
12 #include "components/safe_browsing_db/v4_local_database_manager.h"
13 #include "components/safe_browsing_db/v4_test_util.h" 14 #include "components/safe_browsing_db/v4_test_util.h"
14 #include "content/public/test/test_browser_thread_bundle.h" 15 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "crypto/sha2.h" 16 #include "crypto/sha2.h"
16 #include "net/url_request/test_url_fetcher_factory.h" 17 #include "net/url_request/test_url_fetcher_factory.h"
17 #include "testing/platform_test.h" 18 #include "testing/platform_test.h"
18 19
19 namespace safe_browsing { 20 namespace safe_browsing {
20 21
21 namespace { 22 namespace {
22 23
23 typedef base::Callback<void()> NullCallback; 24 typedef std::vector<FullHashInfo> FullHashInfos;
24 25
25 // Utility function for populating hashes. 26 // Utility function for populating hashes.
26 FullHash HashForUrl(const GURL& url) { 27 FullHash HashForUrl(const GURL& url) {
27 std::vector<FullHash> full_hashes; 28 std::vector<FullHash> full_hashes;
28 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); 29 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes);
29 // ASSERT_GE(full_hashes.size(), 1u); 30 // ASSERT_GE(full_hashes.size(), 1u);
30 return full_hashes[0]; 31 return full_hashes[0];
31 } 32 }
32 33
33 // Always returns misses from GetFullHashes(). 34 // Always returns misses from GetFullHashes().
34 class FakeGetHashProtocolManager : public V4GetHashProtocolManager { 35 class FakeGetHashProtocolManager : public V4GetHashProtocolManager {
35 public: 36 public:
36 FakeGetHashProtocolManager( 37 FakeGetHashProtocolManager(
37 net::URLRequestContextGetter* request_context_getter, 38 net::URLRequestContextGetter* request_context_getter,
38 const StoresToCheck& stores_to_check, 39 const StoresToCheck& stores_to_check,
39 const V4ProtocolConfig& config) 40 const V4ProtocolConfig& config,
41 const FullHashInfos& full_hash_infos)
40 : V4GetHashProtocolManager(request_context_getter, 42 : V4GetHashProtocolManager(request_context_getter,
41 stores_to_check, 43 stores_to_check,
42 config) {} 44 config),
45 full_hash_infos_(full_hash_infos) {}
43 46
44 void GetFullHashes(const FullHashToStoreAndHashPrefixesMap&, 47 void GetFullHashes(const FullHashToStoreAndHashPrefixesMap&,
45 FullHashCallback callback) override { 48 FullHashCallback callback) override {
46 std::vector<FullHashInfo> full_hash_infos;
47
48 // Async, since the real manager might use a fetcher. 49 // Async, since the real manager might use a fetcher.
49 base::ThreadTaskRunnerHandle::Get()->PostTask( 50 base::ThreadTaskRunnerHandle::Get()->PostTask(
50 FROM_HERE, base::Bind(callback, full_hash_infos)); 51 FROM_HERE, base::Bind(callback, full_hash_infos_));
51 } 52 }
53
54 private:
55 FullHashInfos full_hash_infos_;
52 }; 56 };
53 57
54 class FakeGetHashProtocolManagerFactory 58 class FakeGetHashProtocolManagerFactory
55 : public V4GetHashProtocolManagerFactory { 59 : public V4GetHashProtocolManagerFactory {
56 public: 60 public:
61 FakeGetHashProtocolManagerFactory(const FullHashInfos& full_hash_infos)
62 : full_hash_infos_(full_hash_infos) {}
63
57 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager( 64 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager(
58 net::URLRequestContextGetter* request_context_getter, 65 net::URLRequestContextGetter* request_context_getter,
59 const StoresToCheck& stores_to_check, 66 const StoresToCheck& stores_to_check,
60 const V4ProtocolConfig& config) override { 67 const V4ProtocolConfig& config) override {
61 return base::MakeUnique<FakeGetHashProtocolManager>( 68 return base::MakeUnique<FakeGetHashProtocolManager>(
62 request_context_getter, stores_to_check, config); 69 request_context_getter, stores_to_check, config, full_hash_infos_);
63 } 70 }
71
72 private:
73 FullHashInfos full_hash_infos_;
64 }; 74 };
65 75
66 // Use FakeGetHashProtocolManagerFactory in scope, then reset. 76 // Use FakeGetHashProtocolManagerFactory in scope, then reset.
67 class ScopedFakeGetHashProtocolManagerFactory { 77 class ScopedFakeGetHashProtocolManagerFactory {
68 public: 78 public:
69 ScopedFakeGetHashProtocolManagerFactory() { 79 ScopedFakeGetHashProtocolManagerFactory(
80 const FullHashInfos& full_hash_infos) {
70 V4GetHashProtocolManager::RegisterFactory( 81 V4GetHashProtocolManager::RegisterFactory(
71 base::MakeUnique<FakeGetHashProtocolManagerFactory>()); 82 base::MakeUnique<FakeGetHashProtocolManagerFactory>(full_hash_infos));
72 } 83 }
73 ~ScopedFakeGetHashProtocolManagerFactory() { 84 ~ScopedFakeGetHashProtocolManagerFactory() {
74 V4GetHashProtocolManager::RegisterFactory(nullptr); 85 V4GetHashProtocolManager::RegisterFactory(nullptr);
75 } 86 }
76 }; 87 };
77 88
78 } // namespace 89 } // namespace
79 90
80 class FakeV4Database : public V4Database { 91 class FakeV4Database : public V4Database {
81 public: 92 public:
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 if (manager_to_cancel_) { 173 if (manager_to_cancel_) {
163 manager_to_cancel_->CancelCheck(this); 174 manager_to_cancel_->CancelCheck(this);
164 } 175 }
165 } 176 }
166 177
167 void OnCheckResourceUrlResult(const GURL& url, 178 void OnCheckResourceUrlResult(const GURL& url,
168 SBThreatType threat_type, 179 SBThreatType threat_type,
169 const std::string& threat_hash) override { 180 const std::string& threat_hash) override {
170 DCHECK_EQ(expected_url, url); 181 DCHECK_EQ(expected_url, url);
171 DCHECK_EQ(expected_sb_threat_type, threat_type); 182 DCHECK_EQ(expected_sb_threat_type, threat_type);
172 // |threat_hash| is empty because GetFullHashes calls back with empty 183 DCHECK_EQ(threat_type == SB_THREAT_TYPE_SAFE, threat_hash.empty());
173 // |full_hash_infos|.
174 DCHECK(threat_hash.empty());
175 on_check_resource_url_result_called_ = true; 184 on_check_resource_url_result_called_ = true;
176 } 185 }
177 186
178 SBThreatType expected_sb_threat_type; 187 SBThreatType expected_sb_threat_type;
179 GURL expected_url; 188 GURL expected_url;
180 bool on_check_browse_url_result_called_; 189 bool on_check_browse_url_result_called_;
181 bool on_check_resource_url_result_called_; 190 bool on_check_resource_url_result_called_;
182 V4LocalDatabaseManager* manager_to_cancel_; 191 V4LocalDatabaseManager* manager_to_cancel_;
183 }; 192 };
184 193
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 // The database is unavailable so the check should get queued. 449 // The database is unavailable so the check should get queued.
441 EXPECT_EQ(1ul, GetQueuedChecks().size()); 450 EXPECT_EQ(1ul, GetQueuedChecks().size());
442 451
443 StopLocalDatabaseManager(); 452 StopLocalDatabaseManager();
444 EXPECT_TRUE(GetQueuedChecks().empty()); 453 EXPECT_TRUE(GetQueuedChecks().empty());
445 } 454 }
446 455
447 // Verify that a window where checks cannot be cancelled is closed. 456 // Verify that a window where checks cannot be cancelled is closed.
448 TEST_F(V4LocalDatabaseManagerTest, CancelPending) { 457 TEST_F(V4LocalDatabaseManagerTest, CancelPending) {
449 // Setup to receive full-hash misses. 458 // Setup to receive full-hash misses.
450 ScopedFakeGetHashProtocolManagerFactory pin; 459 ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({}));
451 460
452 // Reset the database manager so it picks up the replacement protocol manager. 461 // Reset the database manager so it picks up the replacement protocol manager.
453 ResetLocalDatabaseManager(); 462 ResetLocalDatabaseManager();
454 WaitForTasksOnTaskRunner(); 463 WaitForTasksOnTaskRunner();
455 464
456 // An URL and matching prefix. 465 // An URL and matching prefix.
457 const GURL url("http://example.com/a/"); 466 const GURL url("http://example.com/a/");
458 const HashPrefix hash_prefix("eW\x1A\xF\xA9"); 467 const HashPrefix hash_prefix("eW\x1A\xF\xA9");
459 468
460 // Put a match in the db that will cause a protocol-manager request. 469 // Put a match in the db that will cause a protocol-manager request.
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 // Whitelisted. 676 // Whitelisted.
668 EXPECT_TRUE( 677 EXPECT_TRUE(
669 v4_local_database_manager_->MatchModuleWhitelistString("chrome.dll")); 678 v4_local_database_manager_->MatchModuleWhitelistString("chrome.dll"));
670 679
671 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( 680 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
672 v4_local_database_manager_)); 681 v4_local_database_manager_));
673 } 682 }
674 683
675 // This verifies the fix for race in http://crbug.com/660293 684 // This verifies the fix for race in http://crbug.com/660293
676 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithSameClientAndCancel) { 685 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithSameClientAndCancel) {
677 ScopedFakeGetHashProtocolManagerFactory pin; 686 ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({}));
678 // Reset the database manager so it picks up the replacement protocol manager. 687 // Reset the database manager so it picks up the replacement protocol manager.
679 ResetLocalDatabaseManager(); 688 ResetLocalDatabaseManager();
680 WaitForTasksOnTaskRunner(); 689 WaitForTasksOnTaskRunner();
681 690
682 StoreAndHashPrefixes store_and_hash_prefixes; 691 StoreAndHashPrefixes store_and_hash_prefixes;
683 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), 692 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(),
684 HashPrefix("sن\340\t\006_")); 693 HashPrefix("sن\340\t\006_"));
685 ReplaceV4Database(store_and_hash_prefixes); 694 ReplaceV4Database(store_and_hash_prefixes);
686 695
687 GURL first_url("http://example.com/a"); 696 GURL first_url("http://example.com/a");
(...skipping 14 matching lines...) Expand all
702 WaitForTasksOnTaskRunner(); 711 WaitForTasksOnTaskRunner();
703 // |on_check_browse_url_result_called_| is true only if OnCheckBrowseUrlResult 712 // |on_check_browse_url_result_called_| is true only if OnCheckBrowseUrlResult
704 // gets called with the |url| equal to |expected_url|, which is |second_url| 713 // gets called with the |url| equal to |expected_url|, which is |second_url|
705 // in 714 // in
706 // this test. 715 // this test.
707 EXPECT_TRUE(client.on_check_browse_url_result_called_); 716 EXPECT_TRUE(client.on_check_browse_url_result_called_);
708 } 717 }
709 718
710 TEST_F(V4LocalDatabaseManagerTest, TestCheckResourceUrl) { 719 TEST_F(V4LocalDatabaseManagerTest, TestCheckResourceUrl) {
711 // Setup to receive full-hash misses. 720 // Setup to receive full-hash misses.
712 ScopedFakeGetHashProtocolManagerFactory pin; 721 ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({}));
713 722
714 // Reset the database manager so it picks up the replacement protocol manager. 723 // Reset the database manager so it picks up the replacement protocol manager.
715 ResetLocalDatabaseManager(); 724 ResetLocalDatabaseManager();
716 WaitForTasksOnTaskRunner(); 725 WaitForTasksOnTaskRunner();
717 726
718 // An URL and matching prefix. 727 // An URL and matching prefix.
719 const GURL url("http://example.com/a/"); 728 const GURL url("http://example.com/a/");
720 const HashPrefix hash_prefix("eW\x1A\xF\xA9"); 729 const HashPrefix hash_prefix("eW\x1A\xF\xA9");
721 730
722 // Put a match in the db that will cause a protocol-manager request. 731 // Put a match in the db that will cause a protocol-manager request.
723 StoreAndHashPrefixes store_and_hash_prefixes; 732 StoreAndHashPrefixes store_and_hash_prefixes;
724 store_and_hash_prefixes.emplace_back(GetChromeUrlClientIncidentId(), 733 store_and_hash_prefixes.emplace_back(GetChromeUrlClientIncidentId(),
725 hash_prefix); 734 hash_prefix);
726 ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */); 735 ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */);
727 736
728 TestClient client(SB_THREAT_TYPE_SAFE, url); 737 TestClient client(SB_THREAT_TYPE_SAFE, url);
729 EXPECT_FALSE(v4_local_database_manager_->CheckResourceUrl(url, &client)); 738 EXPECT_FALSE(v4_local_database_manager_->CheckResourceUrl(url, &client));
730 EXPECT_FALSE(client.on_check_resource_url_result_called_); 739 EXPECT_FALSE(client.on_check_resource_url_result_called_);
731 WaitForTasksOnTaskRunner(); 740 WaitForTasksOnTaskRunner();
732 EXPECT_TRUE(client.on_check_resource_url_result_called_); 741 EXPECT_TRUE(client.on_check_resource_url_result_called_);
733 } 742 }
734 743
735 TEST_F(V4LocalDatabaseManagerTest, TestSubresourceFilterCallback) { 744 TEST_F(V4LocalDatabaseManagerTest, TestSubresourceFilterCallback) {
736 // Setup to receive full-hash misses. 745 // Setup to receive full-hash misses.
737 ScopedFakeGetHashProtocolManagerFactory pin; 746 ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({}));
738 747
739 // Reset the database manager so it picks up the replacement protocol manager. 748 // Reset the database manager so it picks up the replacement protocol manager.
740 ResetLocalDatabaseManager(); 749 ResetLocalDatabaseManager();
741 WaitForTasksOnTaskRunner(); 750 WaitForTasksOnTaskRunner();
742 751
743 // An URL and matching prefix. 752 // An URL and matching prefix.
744 const GURL url("http://example.com/a/"); 753 const GURL url("http://example.com/a/");
745 const HashPrefix hash_prefix("eW\x1A\xF\xA9"); 754 const HashPrefix hash_prefix("eW\x1A\xF\xA9");
746 755
747 // Put a match in the db that will cause a protocol-manager request. 756 // Put a match in the db that will cause a protocol-manager request.
748 StoreAndHashPrefixes store_and_hash_prefixes; 757 StoreAndHashPrefixes store_and_hash_prefixes;
749 store_and_hash_prefixes.emplace_back(GetUrlSubresourceFilterId(), 758 store_and_hash_prefixes.emplace_back(GetUrlSubresourceFilterId(),
750 hash_prefix); 759 hash_prefix);
751 ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */); 760 ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */);
752 761
753 // Test that a request flows through to the callback. 762 // Test that a request flows through to the callback.
754 { 763 {
755 TestClient client(SB_THREAT_TYPE_SAFE, url); 764 TestClient client(SB_THREAT_TYPE_SAFE, url);
756 EXPECT_FALSE( 765 EXPECT_FALSE(
757 v4_local_database_manager_->CheckUrlForSubresourceFilter(url, &client)); 766 v4_local_database_manager_->CheckUrlForSubresourceFilter(url, &client));
758 EXPECT_FALSE(client.on_check_browse_url_result_called_); 767 EXPECT_FALSE(client.on_check_browse_url_result_called_);
759 WaitForTasksOnTaskRunner(); 768 WaitForTasksOnTaskRunner();
760 EXPECT_TRUE(client.on_check_browse_url_result_called_); 769 EXPECT_TRUE(client.on_check_browse_url_result_called_);
761 } 770 }
762 } 771 }
763 772
773 TEST_F(V4LocalDatabaseManagerTest, TestCheckResourceUrlReturnsBad) {
774 std::string base64_encoded = "ZVcaD6lke9GaaZEf07X3CpuEgMAqbpAyPw3sX/7eK9M=";
775 std::string base64_decoded;
776 base::Base64Decode(base64_encoded, &base64_decoded);
777 FullHashInfo fhi(base64_decoded, GetChromeUrlClientIncidentId(),
778 base::Time());
779
780 // Setup to receive full-hash hit.
781 ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({fhi}));
782
783 // Reset the database manager so it picks up the replacement protocol manager.
784 ResetLocalDatabaseManager();
785 WaitForTasksOnTaskRunner();
786
787 // An URL and matching prefix.
788 const GURL url("http://example.com/a/");
789 const HashPrefix hash_prefix("eW\x1A\xF\xA9");
790
791 // Put a match in the db that will cause a protocol-manager request.
792 StoreAndHashPrefixes store_and_hash_prefixes;
793 store_and_hash_prefixes.emplace_back(GetChromeUrlClientIncidentId(),
794 hash_prefix);
795 ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */);
796
797 TestClient client(SB_THREAT_TYPE_BLACKLISTED_RESOURCE, url);
798 EXPECT_FALSE(v4_local_database_manager_->CheckResourceUrl(url, &client));
799 EXPECT_FALSE(client.on_check_resource_url_result_called_);
800 WaitForTasksOnTaskRunner();
801 EXPECT_TRUE(client.on_check_resource_url_result_called_);
802 }
803
764 // TODO(nparker): Add tests for 804 // TODO(nparker): Add tests for
765 // CheckDownloadUrl() 805 // CheckDownloadUrl()
766 // CheckExtensionIDs() 806 // CheckExtensionIDs()
767 807
768 } // namespace safe_browsing 808 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_local_database_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698