Chromium Code Reviews| Index: chrome/browser/safe_browsing/download_protection_service_unittest.cc |
| diff --git a/chrome/browser/safe_browsing/download_protection_service_unittest.cc b/chrome/browser/safe_browsing/download_protection_service_unittest.cc |
| index 5ce10f64260640069ddbc89c2a0431c7a443b8e1..6b31f77cfc4e15c592993bde24f125874bd7880e 100644 |
| --- a/chrome/browser/safe_browsing/download_protection_service_unittest.cc |
| +++ b/chrome/browser/safe_browsing/download_protection_service_unittest.cc |
| @@ -37,6 +37,7 @@ |
| #include "chrome/common/pref_names.h" |
| #include "chrome/common/safe_browsing/binary_feature_extractor.h" |
| #include "chrome/common/safe_browsing/csd.pb.h" |
| +#include "chrome/common/safe_browsing/file_type_policies_test_util.h" |
| #include "chrome/test/base/testing_profile.h" |
| #include "components/history/core/browser/history_service.h" |
| #include "components/prefs/pref_service.h" |
| @@ -276,6 +277,9 @@ class DownloadProtectionServiceTest : public testing::Test { |
| // Setup a directory to place test files in. |
| ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + |
| + // Turn off binary sampling by default. |
| + SetBinarySamplingProbability(0.0); |
| } |
| void TearDown() override { |
| @@ -291,6 +295,12 @@ class DownloadProtectionServiceTest : public testing::Test { |
| download_service_->whitelist_sample_rate_ = target_rate; |
| } |
| + void SetBinarySamplingProbability(double target_rate) { |
| + DownloadFileTypeConfig config = policies.GetConfig(); |
| + config.set_sampled_ping_probability(target_rate); |
| + policies.SetConfig(config); |
| + } |
| + |
| bool RequestContainsResource(const ClientDownloadRequest& request, |
| ClientDownloadRequest::ResourceType type, |
| const std::string& url, |
| @@ -507,6 +517,9 @@ class DownloadProtectionServiceTest : public testing::Test { |
| protected: |
| + // This will mask the global Singleton while this is in scope. |
| + FileTypePoliciesTestOverlay policies; |
|
asanka
2016/06/20 18:15:46
policies_
Nathan Parker
2016/06/20 22:47:31
Done.
|
| + |
| scoped_refptr<FakeSafeBrowsingService> sb_service_; |
| scoped_refptr<MockBinaryFeatureExtractor> binary_feature_extractor_; |
| DownloadProtectionService* download_service_; |
| @@ -810,6 +823,88 @@ TEST_F(DownloadProtectionServiceTest, |
| EXPECT_FALSE(HasClientDownloadRequest()); |
| } |
| +TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSampledFile) { |
| + // Server response will be discarded. |
| + net::FakeURLFetcherFactory factory(NULL); |
| + PrepareResponse( |
| + &factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + content::MockDownloadItem item; |
| + PrepareBasicDownloadItem( |
| + &item, |
| + std::vector<std::string>(), // empty url_chain |
| + "http://www.google.com/", // referrer |
| + FILE_PATH_LITERAL("a.tmp"), // tmp_path |
| + FILE_PATH_LITERAL("a.foobar_unknown_ype")); // final_path |
| + EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)) |
| + .Times(1); |
| + EXPECT_CALL(*binary_feature_extractor_.get(), |
| + ExtractImageFeatures( |
| + tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _)) |
| + .Times(1); |
| + url_chain_.push_back(GURL("http://www.whitelist.com/a.foobar_unknown_type")); |
| + |
| + // Set ping sample rate to 1.00 so download_service_ will always send a |
| + // "light" ping for unknown types if allowed. |
| + SetBinarySamplingProbability(1.0); |
| + |
| + // Case (1): is_extended_reporting && is_incognito. |
| + // ClientDownloadRequest should NOT be sent. |
| + SetExtendedReportingPreference(true); |
| + EXPECT_CALL(item, GetBrowserContext()) |
| + .WillRepeatedly(Return(profile_->GetOffTheRecordProfile())); |
| + download_service_->CheckClientDownload( |
| + &item, |
| + base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| + base::Unretained(this))); |
| + MessageLoop::current()->Run(); |
| + EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
| + EXPECT_FALSE(HasClientDownloadRequest()); |
| + |
| + // Case (2): is_extended_reporting && !is_incognito. |
| + // A "light" ClientDownloadRequest should be sent. |
| + EXPECT_CALL(item, GetBrowserContext()) |
| + .WillRepeatedly(Return(profile_.get())); |
| + download_service_->CheckClientDownload( |
| + &item, |
| + base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| + base::Unretained(this))); |
| + MessageLoop::current()->Run(); |
| + EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
| + EXPECT_TRUE(HasClientDownloadRequest()); |
| + // Verify it's a "light" ping |
| + EXPECT_EQ(ClientDownloadRequest::SAMPLED_UNSUPPORTED_FILE, |
| + GetClientDownloadRequest()->download_type()); |
| + // 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.
|
| + ClearClientDownloadRequest(); |
| + |
| + // Case (3): !is_extended_reporting && is_incognito. |
| + // ClientDownloadRequest should NOT be sent. |
| + SetExtendedReportingPreference(false); |
| + EXPECT_CALL(item, GetBrowserContext()) |
| + .WillRepeatedly(Return(profile_->GetOffTheRecordProfile())); |
| + download_service_->CheckClientDownload( |
| + &item, |
| + base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| + base::Unretained(this))); |
| + MessageLoop::current()->Run(); |
| + EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
| + EXPECT_FALSE(HasClientDownloadRequest()); |
| + |
| + // Case (4): !is_extended_reporting && !is_incognito. |
| + // ClientDownloadRequest should NOT be sent. |
| + EXPECT_CALL(item, GetBrowserContext()) |
| + .WillRepeatedly(Return(profile_.get())); |
| + download_service_->CheckClientDownload( |
| + &item, |
| + base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| + base::Unretained(this))); |
| + MessageLoop::current()->Run(); |
| + EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
| + EXPECT_FALSE(HasClientDownloadRequest()); |
| +} |
| + |
| TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) { |
| // HTTP request will fail. |
| net::FakeURLFetcherFactory factory(NULL); |