OLD | NEW |
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 Loading... |
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 response.set_upload(upload_requested); |
401 factory->SetFakeResponse( | 403 factory->SetFakeResponse( |
402 DownloadProtectionService::GetDownloadRequestUrl(), | 404 DownloadProtectionService::GetDownloadRequestUrl(), |
403 response.SerializeAsString(), | 405 response.SerializeAsString(), |
404 response_code, status); | 406 response_code, status); |
405 } | 407 } |
406 | 408 |
407 void PrepareBasicDownloadItem( | 409 void PrepareBasicDownloadItem( |
408 content::MockDownloadItem* item, | 410 content::MockDownloadItem* item, |
409 const std::vector<std::string> url_chain_items, | 411 const std::vector<std::string> url_chain_items, |
410 const std::string& referrer_url, | 412 const std::string& referrer_url, |
(...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1034 content::MockDownloadItem item; | 1036 content::MockDownloadItem item; |
1035 PrepareBasicDownloadItem(&item, {"http://www.evil.com/a.exe"}, // url_chain | 1037 PrepareBasicDownloadItem(&item, {"http://www.evil.com/a.exe"}, // url_chain |
1036 "http://www.google.com/", // referrer | 1038 "http://www.google.com/", // referrer |
1037 FILE_PATH_LITERAL("a.tmp"), // tmp_path | 1039 FILE_PATH_LITERAL("a.tmp"), // tmp_path |
1038 FILE_PATH_LITERAL("a.exe")); // final_path | 1040 FILE_PATH_LITERAL("a.exe")); // final_path |
1039 | 1041 |
1040 EXPECT_CALL(*sb_service_->mock_database_manager(), | 1042 EXPECT_CALL(*sb_service_->mock_database_manager(), |
1041 MatchDownloadWhitelistUrl(_)) | 1043 MatchDownloadWhitelistUrl(_)) |
1042 .WillRepeatedly(Return(false)); | 1044 .WillRepeatedly(Return(false)); |
1043 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)) | 1045 EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path_, _)) |
1044 .Times(7); | 1046 .Times(8); |
1045 EXPECT_CALL(*binary_feature_extractor_.get(), | 1047 EXPECT_CALL(*binary_feature_extractor_.get(), |
1046 ExtractImageFeatures( | 1048 ExtractImageFeatures( |
1047 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _)) | 1049 tmp_path_, BinaryFeatureExtractor::kDefaultOptions, _, _)) |
1048 .Times(7); | 1050 .Times(8); |
1049 std::string feedback_ping; | 1051 std::string feedback_ping; |
1050 std::string feedback_response; | 1052 std::string feedback_response; |
1051 ClientDownloadResponse expected_response; | 1053 ClientDownloadResponse expected_response; |
1052 | 1054 |
1053 { | 1055 { |
1054 RunLoop run_loop; | 1056 RunLoop run_loop; |
1055 download_service_->CheckClientDownload( | 1057 download_service_->CheckClientDownload( |
1056 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 1058 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
1057 base::Unretained(this), run_loop.QuitClosure())); | 1059 base::Unretained(this), run_loop.QuitClosure())); |
1058 run_loop.Run(); | 1060 run_loop.Run(); |
(...skipping 13 matching lines...) Expand all Loading... |
1072 base::Unretained(this), run_loop.QuitClosure())); | 1074 base::Unretained(this), run_loop.QuitClosure())); |
1073 run_loop.Run(); | 1075 run_loop.Run(); |
1074 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); | 1076 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
1075 EXPECT_TRUE(HasClientDownloadRequest()); | 1077 EXPECT_TRUE(HasClientDownloadRequest()); |
1076 ClearClientDownloadRequest(); | 1078 ClearClientDownloadRequest(); |
1077 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting( | 1079 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting( |
1078 item, &feedback_ping, &feedback_response)); | 1080 item, &feedback_ping, &feedback_response)); |
1079 } | 1081 } |
1080 { | 1082 { |
1081 // If the response is dangerous the result should also be marked as | 1083 // If the response is dangerous the result should also be marked as |
1082 // dangerous. | 1084 // dangerous, and should not upload if not requested. |
1083 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, | 1085 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, |
1084 net::URLRequestStatus::SUCCESS); | 1086 net::URLRequestStatus::SUCCESS, |
| 1087 false /* upload_requested */); |
1085 RunLoop run_loop; | 1088 RunLoop run_loop; |
1086 download_service_->CheckClientDownload( | 1089 download_service_->CheckClientDownload( |
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, |
| 1120 true /* upload_requested */); |
1100 RunLoop run_loop; | 1121 RunLoop run_loop; |
1101 download_service_->CheckClientDownload( | 1122 download_service_->CheckClientDownload( |
1102 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 1123 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
1103 base::Unretained(this), run_loop.QuitClosure())); | 1124 base::Unretained(this), run_loop.QuitClosure())); |
1104 run_loop.Run(); | 1125 run_loop.Run(); |
1105 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON)); | 1126 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON)); |
1106 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting( | 1127 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting( |
1107 item, &feedback_ping, &feedback_response)); | 1128 item, &feedback_ping, &feedback_response)); |
1108 ClientDownloadRequest decoded_request; | 1129 ClientDownloadRequest decoded_request; |
1109 EXPECT_TRUE(decoded_request.ParseFromString(feedback_ping)); | 1130 EXPECT_TRUE(decoded_request.ParseFromString(feedback_ping)); |
1110 EXPECT_EQ(url_chain_.back().spec(), decoded_request.url()); | 1131 EXPECT_EQ(url_chain_.back().spec(), decoded_request.url()); |
1111 expected_response.set_verdict(ClientDownloadResponse::UNCOMMON); | 1132 expected_response.set_verdict(ClientDownloadResponse::UNCOMMON); |
| 1133 expected_response.set_upload(true); |
1112 EXPECT_EQ(expected_response.SerializeAsString(), feedback_response); | 1134 EXPECT_EQ(expected_response.SerializeAsString(), feedback_response); |
1113 EXPECT_TRUE(HasClientDownloadRequest()); | 1135 EXPECT_TRUE(HasClientDownloadRequest()); |
1114 ClearClientDownloadRequest(); | 1136 ClearClientDownloadRequest(); |
1115 } | 1137 } |
1116 { | 1138 { |
1117 // If the response is dangerous_host the result should also be marked as | 1139 // If the response is dangerous_host the result should also be marked as |
1118 // dangerous_host. | 1140 // dangerous_host. |
1119 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS_HOST, | 1141 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS_HOST, |
1120 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 1142 net::HTTP_OK, net::URLRequestStatus::SUCCESS, |
| 1143 true /* upload_requested */); |
1121 RunLoop run_loop; | 1144 RunLoop run_loop; |
1122 download_service_->CheckClientDownload( | 1145 download_service_->CheckClientDownload( |
1123 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 1146 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
1124 base::Unretained(this), run_loop.QuitClosure())); | 1147 base::Unretained(this), run_loop.QuitClosure())); |
1125 run_loop.Run(); | 1148 run_loop.Run(); |
1126 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS_HOST)); | 1149 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS_HOST)); |
1127 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting( | 1150 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting( |
1128 item, &feedback_ping, &feedback_response)); | 1151 item, &feedback_ping, &feedback_response)); |
1129 expected_response.set_verdict(ClientDownloadResponse::DANGEROUS_HOST); | 1152 expected_response.set_verdict(ClientDownloadResponse::DANGEROUS_HOST); |
| 1153 expected_response.set_upload(true); |
1130 EXPECT_EQ(expected_response.SerializeAsString(), feedback_response); | 1154 EXPECT_EQ(expected_response.SerializeAsString(), feedback_response); |
1131 EXPECT_TRUE(HasClientDownloadRequest()); | 1155 EXPECT_TRUE(HasClientDownloadRequest()); |
1132 ClearClientDownloadRequest(); | 1156 ClearClientDownloadRequest(); |
1133 } | 1157 } |
1134 { | 1158 { |
1135 // If the response is POTENTIALLY_UNWANTED the result should also be marked | 1159 // If the response is POTENTIALLY_UNWANTED the result should also be marked |
1136 // as POTENTIALLY_UNWANTED. | 1160 // as POTENTIALLY_UNWANTED. |
1137 PrepareResponse(&factory, ClientDownloadResponse::POTENTIALLY_UNWANTED, | 1161 PrepareResponse(&factory, ClientDownloadResponse::POTENTIALLY_UNWANTED, |
1138 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 1162 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
1139 RunLoop run_loop; | 1163 RunLoop run_loop; |
(...skipping 1182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2322 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2346 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
2323 base::Unretained(this), run_loop.QuitClosure())); | 2347 base::Unretained(this), run_loop.QuitClosure())); |
2324 run_loop.Run(); | 2348 run_loop.Run(); |
2325 | 2349 |
2326 EXPECT_FALSE(HasClientDownloadRequest()); | 2350 EXPECT_FALSE(HasClientDownloadRequest()); |
2327 // Overriden by flag: | 2351 // Overriden by flag: |
2328 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); | 2352 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
2329 } | 2353 } |
2330 | 2354 |
2331 } // namespace safe_browsing | 2355 } // namespace safe_browsing |
OLD | NEW |