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

Side by Side Diff: chrome/browser/safe_browsing/download_protection_service_unittest.cc

Issue 2201213007: Skip cert whitelist checking if download URL matches whitelist and sampled (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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 | « chrome/browser/safe_browsing/download_protection_service.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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/safe_browsing/download_protection_service.h" 5 #include "chrome/browser/safe_browsing/download_protection_service.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <map> 10 #include <map>
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 using ::testing::Invoke; 67 using ::testing::Invoke;
68 using ::testing::Mock; 68 using ::testing::Mock;
69 using ::testing::NotNull; 69 using ::testing::NotNull;
70 using ::testing::Return; 70 using ::testing::Return;
71 using ::testing::ReturnRef; 71 using ::testing::ReturnRef;
72 using ::testing::SaveArg; 72 using ::testing::SaveArg;
73 using ::testing::StrictMock; 73 using ::testing::StrictMock;
74 using ::testing::_; 74 using ::testing::_;
75 using base::RunLoop; 75 using base::RunLoop;
76 using content::BrowserThread; 76 using content::BrowserThread;
77
77 namespace safe_browsing { 78 namespace safe_browsing {
79
78 namespace { 80 namespace {
81
79 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for 82 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for
80 // a given URL. 83 // a given URL.
81 class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { 84 class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager {
82 public: 85 public:
83 MockSafeBrowsingDatabaseManager() {} 86 MockSafeBrowsingDatabaseManager() {}
84 87
85 MOCK_METHOD1(MatchDownloadWhitelistUrl, bool(const GURL&)); 88 MOCK_METHOD1(MatchDownloadWhitelistUrl, bool(const GURL&));
86 MOCK_METHOD1(MatchDownloadWhitelistString, bool(const std::string&)); 89 MOCK_METHOD1(MatchDownloadWhitelistString, bool(const std::string&));
87 MOCK_METHOD2(CheckDownloadUrl, bool( 90 MOCK_METHOD2(CheckDownloadUrl, bool(
88 const std::vector<GURL>& url_chain, 91 const std::vector<GURL>& url_chain,
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 196
194 ACTION_P(SetCertificateContents, contents) { 197 ACTION_P(SetCertificateContents, contents) {
195 arg1->add_certificate_chain()->add_element()->set_certificate(contents); 198 arg1->add_certificate_chain()->add_element()->set_certificate(contents);
196 } 199 }
197 200
198 ACTION_P(SetDosHeaderContents, contents) { 201 ACTION_P(SetDosHeaderContents, contents) {
199 arg2->mutable_pe_headers()->set_dos_header(contents); 202 arg2->mutable_pe_headers()->set_dos_header(contents);
200 return true; 203 return true;
201 } 204 }
202 205
203 ACTION_P(TrustSignature, certificate_file) { 206 ACTION_P(TrustSignature, contents) {
204 arg1->set_trusted(true); 207 arg1->set_trusted(true);
205 // Add a certificate chain. Note that we add the certificate twice so that 208 // Add a certificate chain. Note that we add the certificate twice so that
206 // it appears as its own issuer. 209 // it appears as its own issuer.
207 std::string cert_data; 210
208 ASSERT_TRUE(base::ReadFileToString(certificate_file, &cert_data));
209 ClientDownloadRequest_CertificateChain* chain = 211 ClientDownloadRequest_CertificateChain* chain =
210 arg1->add_certificate_chain(); 212 arg1->add_certificate_chain();
211 chain->add_element()->set_certificate(cert_data); 213 chain->add_element()->set_certificate(contents.data(), contents.size());
212 chain->add_element()->set_certificate(cert_data); 214 chain->add_element()->set_certificate(contents.data(), contents.size());
213 } 215 }
214 216
215 // We can't call OnSafeBrowsingResult directly because SafeBrowsingCheck does 217 // We can't call OnSafeBrowsingResult directly because SafeBrowsingCheck does
216 // not have any copy constructor which means it can't be stored in a callback 218 // not have any copy constructor which means it can't be stored in a callback
217 // easily. Note: check will be deleted automatically when the callback is 219 // easily. Note: check will be deleted automatically when the callback is
218 // deleted. 220 // deleted.
219 void OnSafeBrowsingResult( 221 void OnSafeBrowsingResult(
220 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check) { 222 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check) {
221 check->OnSafeBrowsingResult(); 223 check->OnSafeBrowsingResult();
222 } 224 }
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after
777 &item, 779 &item,
778 std::vector<std::string>(), // empty url_chain 780 std::vector<std::string>(), // empty url_chain
779 "http://www.google.com/", // referrer 781 "http://www.google.com/", // referrer
780 FILE_PATH_LITERAL("a.tmp"), // tmp_path 782 FILE_PATH_LITERAL("a.tmp"), // tmp_path
781 FILE_PATH_LITERAL("a.exe")); // final_path 783 FILE_PATH_LITERAL("a.exe")); // final_path
782 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)) 784 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _))
783 .Times(4); 785 .Times(4);
784 EXPECT_CALL(*binary_feature_extractor_.get(), 786 EXPECT_CALL(*binary_feature_extractor_.get(),
785 ExtractImageFeatures( 787 ExtractImageFeatures(
786 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _)) 788 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _))
787 .Times(4); 789 .Times(6);
788 // Assume http://www.whitelist.com/a.exe is on the whitelist. 790 // Assume http://www.whitelist.com/a.exe is on the whitelist.
789 EXPECT_CALL(*sb_service_->mock_database_manager(), 791 EXPECT_CALL(*sb_service_->mock_database_manager(),
790 MatchDownloadWhitelistUrl(_)).Times(0); 792 MatchDownloadWhitelistUrl(_)).Times(0);
791 EXPECT_CALL(*sb_service_->mock_database_manager(), 793 EXPECT_CALL(*sb_service_->mock_database_manager(),
792 MatchDownloadWhitelistUrl(GURL("http://www.whitelist.com/a.exe"))) 794 MatchDownloadWhitelistUrl(GURL("http://www.whitelist.com/a.exe")))
793 .WillRepeatedly(Return(true)); 795 .WillRepeatedly(Return(true));
794 url_chain_.push_back(GURL("http://www.whitelist.com/a.exe")); 796 url_chain_.push_back(GURL("http://www.whitelist.com/a.exe"));
795 // Set sample rate to 1.00, so download_service_ will always send download 797 // Set sample rate to 1.00, so download_service_ will always send download
796 // pings for whitelisted downloads. 798 // pings for whitelisted downloads.
797 SetWhitelistedDownloadSampleRate(1.00); 799 SetWhitelistedDownloadSampleRate(1.00);
798 800
799 { 801 {
800 // Case (1): is_extended_reporting && is_incognito. 802 // Case (1): is_extended_reporting && is_incognito.
801 // ClientDownloadRequest should NOT be sent. 803 // ClientDownloadRequest should NOT be sent.
802 SetExtendedReportingPreference(true); 804 SetExtendedReportingPreference(true);
803 EXPECT_CALL(item, GetBrowserContext()) 805 EXPECT_CALL(item, GetBrowserContext())
804 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile())); 806 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile()));
805 RunLoop run_loop; 807 RunLoop run_loop;
806 download_service_->CheckClientDownload( 808 download_service_->CheckClientDownload(
807 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 809 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
808 base::Unretained(this), run_loop.QuitClosure())); 810 base::Unretained(this), run_loop.QuitClosure()));
809 run_loop.Run(); 811 run_loop.Run();
810 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 812 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
811 EXPECT_FALSE(HasClientDownloadRequest()); 813 EXPECT_FALSE(HasClientDownloadRequest());
812 } 814 }
813 { 815 {
814 // Case (2): is_extended_reporting && !is_incognito. 816 // Case (2): !is_extended_reporting && is_incognito.
815 // ClientDownloadRequest should be sent. 817 // ClientDownloadRequest should NOT be sent.
818 SetExtendedReportingPreference(false);
819 EXPECT_CALL(item, GetBrowserContext())
820 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile()));
821 RunLoop run_loop;
822 download_service_->CheckClientDownload(
823 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
824 base::Unretained(this), run_loop.QuitClosure()));
825 run_loop.Run();
826 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
827 EXPECT_FALSE(HasClientDownloadRequest());
828 }
829 {
830 // Case (3): !is_extended_reporting && !is_incognito.
831 // ClientDownloadRequest should NOT be sent.
816 EXPECT_CALL(item, GetBrowserContext()) 832 EXPECT_CALL(item, GetBrowserContext())
817 .WillRepeatedly(Return(profile_.get())); 833 .WillRepeatedly(Return(profile_.get()));
818 RunLoop run_loop; 834 RunLoop run_loop;
835 download_service_->CheckClientDownload(
836 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
837 base::Unretained(this), run_loop.QuitClosure()));
838 run_loop.Run();
839 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
840 EXPECT_FALSE(HasClientDownloadRequest());
841 }
842 {
843 // Case (4): is_extended_reporting && !is_incognito &&
844 // Only URL matches whitelist.
845 // ClientDownloadRequest should be sent.
846 SetExtendedReportingPreference(true);
847 EXPECT_CALL(item, GetBrowserContext())
848 .WillRepeatedly(Return(profile_.get()));
849 RunLoop run_loop;
819 download_service_->CheckClientDownload( 850 download_service_->CheckClientDownload(
820 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 851 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
821 base::Unretained(this), run_loop.QuitClosure())); 852 base::Unretained(this), run_loop.QuitClosure()));
822 run_loop.Run(); 853 run_loop.Run();
823 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 854 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
824 ASSERT_TRUE(HasClientDownloadRequest()); 855 ASSERT_TRUE(HasClientDownloadRequest());
825 EXPECT_TRUE(GetClientDownloadRequest()->skipped_url_whitelist()); 856 EXPECT_TRUE(GetClientDownloadRequest()->skipped_url_whitelist());
857 EXPECT_FALSE(GetClientDownloadRequest()->skipped_certificate_whitelist());
826 ClearClientDownloadRequest(); 858 ClearClientDownloadRequest();
827 } 859 }
860
Jialiu Lin 2016/08/04 18:16:39 test case (1) - (4) are the same (just reordered).
861 // Set up trusted and whitelisted certificate for test cases (5) and (6).
862 scoped_refptr<net::X509Certificate> test_cert(
863 ReadTestCertificate("test_cn.pem"));
864 ASSERT_TRUE(test_cert.get());
865 std::string test_cert_der;
866 net::X509Certificate::GetDEREncoded(test_cert->os_cert_handle(),
867 &test_cert_der);
868 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _))
869 .WillRepeatedly(TrustSignature(test_cert_der));
870
828 { 871 {
829 // Case (3): !is_extended_reporting && is_incognito. 872 // Case (5): is_extended_reporting && !is_incognito &&
830 // ClientDownloadRequest should NOT be sent. 873 // Certificate matches whitelist.
831 SetExtendedReportingPreference(false); 874 // ClientDownloadRequest should be sent.
832 EXPECT_CALL(item, GetBrowserContext()) 875 EXPECT_CALL(item, GetBrowserContext())
833 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile())); 876 .WillRepeatedly(Return(profile_.get()));
877 EXPECT_CALL(*sb_service_->mock_database_manager(),
878 MatchDownloadWhitelistUrl(GURL("http://www.whitelist.com/a.exe") ))
879 .WillRepeatedly(Return(false));
880 EXPECT_CALL(*sb_service_->mock_database_manager(),
881 MatchDownloadWhitelistString(_))
882 .WillRepeatedly(Return(true));
834 RunLoop run_loop; 883 RunLoop run_loop;
835 download_service_->CheckClientDownload( 884 download_service_->CheckClientDownload(
836 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 885 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
837 base::Unretained(this), run_loop.QuitClosure())); 886 base::Unretained(this), run_loop.QuitClosure()));
838 run_loop.Run(); 887 run_loop.Run();
839 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 888 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
840 EXPECT_FALSE(HasClientDownloadRequest()); 889 ASSERT_TRUE(HasClientDownloadRequest());
890 EXPECT_FALSE(GetClientDownloadRequest()->skipped_url_whitelist());
891 EXPECT_TRUE(GetClientDownloadRequest()->skipped_certificate_whitelist());
892 ClearClientDownloadRequest();
841 } 893 }
842 { 894 {
843 // Case (4): !is_extended_reporting && !is_incognito. 895 // Case (6): is_extended_reporting && !is_incognito &&
844 // ClientDownloadRequest should NOT be sent. 896 // Both URL and Certificate match whitelist.
897 // ClientDownloadRequest should be sent.
845 EXPECT_CALL(item, GetBrowserContext()) 898 EXPECT_CALL(item, GetBrowserContext())
846 .WillRepeatedly(Return(profile_.get())); 899 .WillRepeatedly(Return(profile_.get()));
900 EXPECT_CALL(*sb_service_->mock_database_manager(),
901 MatchDownloadWhitelistUrl(GURL("http://www.whitelist.com/a.exe") ))
902 .WillRepeatedly(Return(true));
903 EXPECT_CALL(*sb_service_->mock_database_manager(),
904 MatchDownloadWhitelistString(_))
905 .WillRepeatedly(Return(true));
847 RunLoop run_loop; 906 RunLoop run_loop;
848 download_service_->CheckClientDownload( 907 download_service_->CheckClientDownload(
849 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 908 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
850 base::Unretained(this), run_loop.QuitClosure())); 909 base::Unretained(this), run_loop.QuitClosure()));
851 run_loop.Run(); 910 run_loop.Run();
852 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 911 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
853 EXPECT_FALSE(HasClientDownloadRequest()); 912 ASSERT_TRUE(HasClientDownloadRequest());
913 EXPECT_TRUE(GetClientDownloadRequest()->skipped_url_whitelist());
914 // Since URL matches whitelist and gets sampled, we don't need to check
915 // certificate whitelist any more.
916 EXPECT_FALSE(GetClientDownloadRequest()->skipped_certificate_whitelist());
917 ClearClientDownloadRequest();
854 } 918 }
855 } 919 }
856 920
857 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSampledFile) { 921 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSampledFile) {
858 // Server response will be discarded. 922 // Server response will be discarded.
859 net::FakeURLFetcherFactory factory(NULL); 923 net::FakeURLFetcherFactory factory(NULL);
860 PrepareResponse( 924 PrepareResponse(
861 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, 925 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
862 net::URLRequestStatus::SUCCESS); 926 net::URLRequestStatus::SUCCESS);
863 927
(...skipping 1436 matching lines...) Expand 10 before | Expand all | Expand 10 after
2300 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2364 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2301 base::Unretained(this), run_loop.QuitClosure())); 2365 base::Unretained(this), run_loop.QuitClosure()));
2302 run_loop.Run(); 2366 run_loop.Run();
2303 2367
2304 EXPECT_FALSE(HasClientDownloadRequest()); 2368 EXPECT_FALSE(HasClientDownloadRequest());
2305 // Overriden by flag: 2369 // Overriden by flag:
2306 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 2370 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
2307 } 2371 }
2308 2372
2309 } // namespace safe_browsing 2373 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/download_protection_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698