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

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

Issue 2696973002: Allow Safe Browsing backend to select downloads to upload. (Closed)
Patch Set: Add unittest for DownloadProtectionService 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
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 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 388
389 bool HasClientDownloadRequest() const { 389 bool HasClientDownloadRequest() const {
390 return last_client_download_request_.get() != NULL; 390 return last_client_download_request_.get() != NULL;
391 } 391 }
392 392
393 void ClearClientDownloadRequest() { last_client_download_request_.reset(); } 393 void ClearClientDownloadRequest() { last_client_download_request_.reset(); }
394 394
395 void PrepareResponse(net::FakeURLFetcherFactory* factory, 395 void PrepareResponse(net::FakeURLFetcherFactory* factory,
396 ClientDownloadResponse::Verdict verdict, 396 ClientDownloadResponse::Verdict verdict,
397 net::HttpStatusCode response_code, 397 net::HttpStatusCode response_code,
398 net::URLRequestStatus::Status status) { 398 net::URLRequestStatus::Status status,
399 bool upload_requested = false) {
399 ClientDownloadResponse response; 400 ClientDownloadResponse response;
400 response.set_verdict(verdict); 401 response.set_verdict(verdict);
402 if (upload_requested)
403 response.set_upload(true);
401 factory->SetFakeResponse( 404 factory->SetFakeResponse(
402 DownloadProtectionService::GetDownloadRequestUrl(), 405 DownloadProtectionService::GetDownloadRequestUrl(),
403 response.SerializeAsString(), 406 response.SerializeAsString(),
404 response_code, status); 407 response_code, status);
405 } 408 }
406 409
407 void PrepareBasicDownloadItem( 410 void PrepareBasicDownloadItem(
408 content::MockDownloadItem* item, 411 content::MockDownloadItem* item,
409 const std::vector<std::string> url_chain_items, 412 const std::vector<std::string> url_chain_items,
410 const std::string& referrer_url, 413 const std::string& referrer_url,
(...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 content::MockDownloadItem item; 1037 content::MockDownloadItem item;
1035 PrepareBasicDownloadItem(&item, {"http://www.evil.com/a.exe"}, // url_chain 1038 PrepareBasicDownloadItem(&item, {"http://www.evil.com/a.exe"}, // url_chain
1036 "http://www.google.com/", // referrer 1039 "http://www.google.com/", // referrer
1037 FILE_PATH_LITERAL("a.tmp"), // tmp_path 1040 FILE_PATH_LITERAL("a.tmp"), // tmp_path
1038 FILE_PATH_LITERAL("a.exe")); // final_path 1041 FILE_PATH_LITERAL("a.exe")); // final_path
1039 1042
1040 EXPECT_CALL(*sb_service_->mock_database_manager(), 1043 EXPECT_CALL(*sb_service_->mock_database_manager(),
1041 MatchDownloadWhitelistUrl(_)) 1044 MatchDownloadWhitelistUrl(_))
1042 .WillRepeatedly(Return(false)); 1045 .WillRepeatedly(Return(false));
1043 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)) 1046 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _))
1044 .Times(7); 1047 .Times(8);
1045 EXPECT_CALL(*binary_feature_extractor_.get(), 1048 EXPECT_CALL(*binary_feature_extractor_.get(),
1046 ExtractImageFeatures( 1049 ExtractImageFeatures(
1047 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _)) 1050 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _))
1048 .Times(7); 1051 .Times(8);
1049 std::string feedback_ping; 1052 std::string feedback_ping;
1050 std::string feedback_response; 1053 std::string feedback_response;
1051 ClientDownloadResponse expected_response; 1054 ClientDownloadResponse expected_response;
1052 1055
1053 { 1056 {
1054 RunLoop run_loop; 1057 RunLoop run_loop;
1055 download_service_->CheckClientDownload( 1058 download_service_->CheckClientDownload(
1056 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1059 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1057 base::Unretained(this), run_loop.QuitClosure())); 1060 base::Unretained(this), run_loop.QuitClosure()));
1058 run_loop.Run(); 1061 run_loop.Run();
(...skipping 28 matching lines...) Expand all
1087 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1090 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1088 base::Unretained(this), run_loop.QuitClosure())); 1091 base::Unretained(this), run_loop.QuitClosure()));
1089 run_loop.Run(); 1092 run_loop.Run();
1090 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting( 1093 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting(
1091 item, &feedback_ping, &feedback_response)); 1094 item, &feedback_ping, &feedback_response));
1092 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 1095 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
1093 EXPECT_TRUE(HasClientDownloadRequest()); 1096 EXPECT_TRUE(HasClientDownloadRequest());
1094 ClearClientDownloadRequest(); 1097 ClearClientDownloadRequest();
1095 } 1098 }
1096 { 1099 {
1100 // If the response is dangerous and the server requests an upload,
1101 // we should upload.
1102 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK,
1103 net::URLRequestStatus::SUCCESS,
1104 true /* upload_requested */);
1105 RunLoop run_loop;
1106 download_service_->CheckClientDownload(
1107 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1108 base::Unretained(this), run_loop.QuitClosure()));
1109 run_loop.Run();
1110 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting(
1111 item, &feedback_ping, &feedback_response));
1112 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
1113 EXPECT_TRUE(HasClientDownloadRequest());
1114 ClearClientDownloadRequest();
1115 }
1116 {
1097 // If the response is uncommon the result should also be marked as uncommon. 1117 // If the response is uncommon the result should also be marked as uncommon.
1098 PrepareResponse(&factory, ClientDownloadResponse::UNCOMMON, net::HTTP_OK, 1118 PrepareResponse(&factory, ClientDownloadResponse::UNCOMMON, net::HTTP_OK,
1099 net::URLRequestStatus::SUCCESS); 1119 net::URLRequestStatus::SUCCESS);
1100 RunLoop run_loop; 1120 RunLoop run_loop;
1101 download_service_->CheckClientDownload( 1121 download_service_->CheckClientDownload(
1102 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 1122 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
1103 base::Unretained(this), run_loop.QuitClosure())); 1123 base::Unretained(this), run_loop.QuitClosure()));
1104 run_loop.Run(); 1124 run_loop.Run();
1105 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON)); 1125 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON));
1106 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting( 1126 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting(
(...skipping 1215 matching lines...) Expand 10 before | Expand all | Expand 10 after
2322 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 2342 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
2323 base::Unretained(this), run_loop.QuitClosure())); 2343 base::Unretained(this), run_loop.QuitClosure()));
2324 run_loop.Run(); 2344 run_loop.Run();
2325 2345
2326 EXPECT_FALSE(HasClientDownloadRequest()); 2346 EXPECT_FALSE(HasClientDownloadRequest());
2327 // Overriden by flag: 2347 // Overriden by flag:
2328 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 2348 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
2329 } 2349 }
2330 2350
2331 } // namespace safe_browsing 2351 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698