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

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

Issue 2072933002: Add sampling of unknown filetypes in download protection. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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
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 19 matching lines...) Expand all
30 #include "base/threading/thread_task_runner_handle.h" 30 #include "base/threading/thread_task_runner_handle.h"
31 #include "chrome/browser/history/history_service_factory.h" 31 #include "chrome/browser/history/history_service_factory.h"
32 #include "chrome/browser/safe_browsing/download_feedback_service.h" 32 #include "chrome/browser/safe_browsing/download_feedback_service.h"
33 #include "chrome/browser/safe_browsing/incident_reporting/incident_reporting_ser vice.h" 33 #include "chrome/browser/safe_browsing/incident_reporting/incident_reporting_ser vice.h"
34 #include "chrome/browser/safe_browsing/local_database_manager.h" 34 #include "chrome/browser/safe_browsing/local_database_manager.h"
35 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 35 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
36 #include "chrome/common/chrome_switches.h" 36 #include "chrome/common/chrome_switches.h"
37 #include "chrome/common/pref_names.h" 37 #include "chrome/common/pref_names.h"
38 #include "chrome/common/safe_browsing/binary_feature_extractor.h" 38 #include "chrome/common/safe_browsing/binary_feature_extractor.h"
39 #include "chrome/common/safe_browsing/csd.pb.h" 39 #include "chrome/common/safe_browsing/csd.pb.h"
40 #include "chrome/common/safe_browsing/file_type_policies_test_util.h"
40 #include "chrome/test/base/testing_profile.h" 41 #include "chrome/test/base/testing_profile.h"
41 #include "components/history/core/browser/history_service.h" 42 #include "components/history/core/browser/history_service.h"
42 #include "components/prefs/pref_service.h" 43 #include "components/prefs/pref_service.h"
43 #include "components/safe_browsing_db/database_manager.h" 44 #include "components/safe_browsing_db/database_manager.h"
44 #include "components/safe_browsing_db/test_database_manager.h" 45 #include "components/safe_browsing_db/test_database_manager.h"
45 #include "content/public/browser/download_danger_type.h" 46 #include "content/public/browser/download_danger_type.h"
46 #include "content/public/browser/page_navigator.h" 47 #include "content/public/browser/page_navigator.h"
47 #include "content/public/test/mock_download_item.h" 48 #include "content/public/test/mock_download_item.h"
48 #include "content/public/test/test_browser_thread_bundle.h" 49 #include "content/public/test/test_browser_thread_bundle.h"
49 #include "content/public/test/test_utils.h" 50 #include "content/public/test/test_utils.h"
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 .AppendASCII("download_protection"); 270 .AppendASCII("download_protection");
270 271
271 // Setup a profile 272 // Setup a profile
272 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); 273 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
273 profile_.reset(new TestingProfile(profile_dir_.path())); 274 profile_.reset(new TestingProfile(profile_dir_.path()));
274 ASSERT_TRUE(profile_->CreateHistoryService(true /* delete_file */, 275 ASSERT_TRUE(profile_->CreateHistoryService(true /* delete_file */,
275 false /* no_db */)); 276 false /* no_db */));
276 277
277 // Setup a directory to place test files in. 278 // Setup a directory to place test files in.
278 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 279 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
280
281 // Turn off binary sampling by default.
282 SetBinarySamplingProbability(0.0);
279 } 283 }
280 284
281 void TearDown() override { 285 void TearDown() override {
282 client_download_request_subscription_.reset(); 286 client_download_request_subscription_.reset();
283 sb_service_->ShutDown(); 287 sb_service_->ShutDown();
284 // Flush all of the thread message loops to ensure that there are no 288 // Flush all of the thread message loops to ensure that there are no
285 // tasks currently running. 289 // tasks currently running.
286 FlushThreadMessageLoops(); 290 FlushThreadMessageLoops();
287 sb_service_ = NULL; 291 sb_service_ = NULL;
288 } 292 }
289 293
290 void SetWhitelistedDownloadSampleRate(double target_rate) { 294 void SetWhitelistedDownloadSampleRate(double target_rate) {
291 download_service_->whitelist_sample_rate_ = target_rate; 295 download_service_->whitelist_sample_rate_ = target_rate;
292 } 296 }
293 297
298 void SetBinarySamplingProbability(double target_rate) {
299 DownloadFileTypeConfig config = policies.GetConfig();
300 config.set_sampled_ping_probability(target_rate);
301 policies.SetConfig(config);
302 }
303
294 bool RequestContainsResource(const ClientDownloadRequest& request, 304 bool RequestContainsResource(const ClientDownloadRequest& request,
295 ClientDownloadRequest::ResourceType type, 305 ClientDownloadRequest::ResourceType type,
296 const std::string& url, 306 const std::string& url,
297 const std::string& referrer) { 307 const std::string& referrer) {
298 for (int i = 0; i < request.resources_size(); ++i) { 308 for (int i = 0; i < request.resources_size(); ++i) {
299 if (request.resources(i).url() == url && 309 if (request.resources(i).url() == url &&
300 request.resources(i).type() == type && 310 request.resources(i).type() == type &&
301 (referrer.empty() || request.resources(i).referrer() == referrer)) { 311 (referrer.empty() || request.resources(i).referrer() == referrer)) {
302 return true; 312 return true;
303 } 313 }
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 is_extended_reporting); 510 is_extended_reporting);
501 } 511 }
502 512
503 // Check scenarios where we should/shouldn't send a report for 513 // Check scenarios where we should/shouldn't send a report for
504 // a corrupted zip. 514 // a corrupted zip.
505 void CheckClientDownloadReportCorruptZip(bool is_extended_reporting, 515 void CheckClientDownloadReportCorruptZip(bool is_extended_reporting,
506 bool is_incognito); 516 bool is_incognito);
507 517
508 518
509 protected: 519 protected:
520 // This will mask the global Singleton while this is in scope.
521 FileTypePoliciesTestOverlay policies;
asanka 2016/06/20 18:15:46 policies_
Nathan Parker 2016/06/20 22:47:31 Done.
522
510 scoped_refptr<FakeSafeBrowsingService> sb_service_; 523 scoped_refptr<FakeSafeBrowsingService> sb_service_;
511 scoped_refptr<MockBinaryFeatureExtractor> binary_feature_extractor_; 524 scoped_refptr<MockBinaryFeatureExtractor> binary_feature_extractor_;
512 DownloadProtectionService* download_service_; 525 DownloadProtectionService* download_service_;
513 DownloadProtectionService::DownloadCheckResult result_; 526 DownloadProtectionService::DownloadCheckResult result_;
514 bool has_result_; 527 bool has_result_;
515 content::TestBrowserThreadBundle test_browser_thread_bundle_; 528 content::TestBrowserThreadBundle test_browser_thread_bundle_;
516 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_; 529 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_;
517 base::FilePath testdata_path_; 530 base::FilePath testdata_path_;
518 DownloadProtectionService::ClientDownloadRequestSubscription 531 DownloadProtectionService::ClientDownloadRequestSubscription
519 client_download_request_subscription_; 532 client_download_request_subscription_;
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 .WillRepeatedly(Return(profile_.get())); 816 .WillRepeatedly(Return(profile_.get()));
804 download_service_->CheckClientDownload( 817 download_service_->CheckClientDownload(
805 &item, 818 &item,
806 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 819 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
807 base::Unretained(this))); 820 base::Unretained(this)));
808 MessageLoop::current()->Run(); 821 MessageLoop::current()->Run();
809 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 822 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
810 EXPECT_FALSE(HasClientDownloadRequest()); 823 EXPECT_FALSE(HasClientDownloadRequest());
811 } 824 }
812 825
826 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSampledFile) {
827 // Server response will be discarded.
828 net::FakeURLFetcherFactory factory(NULL);
829 PrepareResponse(
830 &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
831 net::URLRequestStatus::SUCCESS);
832
833 content::MockDownloadItem item;
834 PrepareBasicDownloadItem(
835 &item,
836 std::vector<std::string>(), // empty url_chain
837 "http://www.google.com/", // referrer
838 FILE_PATH_LITERAL("a.tmp"), // tmp_path
839 FILE_PATH_LITERAL("a.foobar_unknown_ype")); // final_path
840 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _))
841 .Times(1);
842 EXPECT_CALL(*binary_feature_extractor_.get(),
843 ExtractImageFeatures(
844 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _))
845 .Times(1);
846 url_chain_.push_back(GURL("http://www.whitelist.com/a.foobar_unknown_type"));
847
848 // Set ping sample rate to 1.00 so download_service_ will always send a
849 // "light" ping for unknown types if allowed.
850 SetBinarySamplingProbability(1.0);
851
852 // Case (1): is_extended_reporting && is_incognito.
853 // ClientDownloadRequest should NOT be sent.
854 SetExtendedReportingPreference(true);
855 EXPECT_CALL(item, GetBrowserContext())
856 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile()));
857 download_service_->CheckClientDownload(
858 &item,
859 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
860 base::Unretained(this)));
861 MessageLoop::current()->Run();
862 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
863 EXPECT_FALSE(HasClientDownloadRequest());
864
865 // Case (2): is_extended_reporting && !is_incognito.
866 // A "light" ClientDownloadRequest should be sent.
867 EXPECT_CALL(item, GetBrowserContext())
868 .WillRepeatedly(Return(profile_.get()));
869 download_service_->CheckClientDownload(
870 &item,
871 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
872 base::Unretained(this)));
873 MessageLoop::current()->Run();
874 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
875 EXPECT_TRUE(HasClientDownloadRequest());
876 // Verify it's a "light" ping
877 EXPECT_EQ(ClientDownloadRequest::SAMPLED_UNSUPPORTED_FILE,
878 GetClientDownloadRequest()->download_type());
879 // TODO(nparker): Check that URLs don't have paths, and has no filename.
asanka 2016/06/20 18:15:46 Were you planning on resolving this in this CL?
Nathan Parker 2016/06/20 22:47:31 yes, and done.
880 ClearClientDownloadRequest();
881
882 // Case (3): !is_extended_reporting && is_incognito.
883 // ClientDownloadRequest should NOT be sent.
884 SetExtendedReportingPreference(false);
885 EXPECT_CALL(item, GetBrowserContext())
886 .WillRepeatedly(Return(profile_->GetOffTheRecordProfile()));
887 download_service_->CheckClientDownload(
888 &item,
889 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
890 base::Unretained(this)));
891 MessageLoop::current()->Run();
892 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
893 EXPECT_FALSE(HasClientDownloadRequest());
894
895 // Case (4): !is_extended_reporting && !is_incognito.
896 // ClientDownloadRequest should NOT be sent.
897 EXPECT_CALL(item, GetBrowserContext())
898 .WillRepeatedly(Return(profile_.get()));
899 download_service_->CheckClientDownload(
900 &item,
901 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
902 base::Unretained(this)));
903 MessageLoop::current()->Run();
904 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN));
905 EXPECT_FALSE(HasClientDownloadRequest());
906 }
907
813 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) { 908 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) {
814 // HTTP request will fail. 909 // HTTP request will fail.
815 net::FakeURLFetcherFactory factory(NULL); 910 net::FakeURLFetcherFactory factory(NULL);
816 PrepareResponse( 911 PrepareResponse(
817 &factory, ClientDownloadResponse::SAFE, net::HTTP_INTERNAL_SERVER_ERROR, 912 &factory, ClientDownloadResponse::SAFE, net::HTTP_INTERNAL_SERVER_ERROR,
818 net::URLRequestStatus::FAILED); 913 net::URLRequestStatus::FAILED);
819 914
820 content::MockDownloadItem item; 915 content::MockDownloadItem item;
821 PrepareBasicDownloadItem( 916 PrepareBasicDownloadItem(
822 &item, 917 &item,
(...skipping 1323 matching lines...) Expand 10 before | Expand all | Expand 10 after
2146 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2241 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2147 base::Unretained(this))); 2242 base::Unretained(this)));
2148 MessageLoop::current()->Run(); 2243 MessageLoop::current()->Run();
2149 2244
2150 EXPECT_FALSE(HasClientDownloadRequest()); 2245 EXPECT_FALSE(HasClientDownloadRequest());
2151 // Overriden by flag: 2246 // Overriden by flag:
2152 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 2247 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
2153 } 2248 }
2154 2249
2155 } // namespace safe_browsing 2250 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698