| 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 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 .AppendASCII("test") | 263 .AppendASCII("test") |
| 264 .AppendASCII("data") | 264 .AppendASCII("data") |
| 265 .AppendASCII("safe_browsing") | 265 .AppendASCII("safe_browsing") |
| 266 .AppendASCII("download_protection"); | 266 .AppendASCII("download_protection"); |
| 267 | 267 |
| 268 // Setup a profile | 268 // Setup a profile |
| 269 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 269 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
| 270 profile_.reset(new TestingProfile(profile_dir_.path())); | 270 profile_.reset(new TestingProfile(profile_dir_.path())); |
| 271 ASSERT_TRUE(profile_->CreateHistoryService(true /* delete_file */, | 271 ASSERT_TRUE(profile_->CreateHistoryService(true /* delete_file */, |
| 272 false /* no_db */)); | 272 false /* no_db */)); |
| 273 |
| 274 // Setup a directory to place test files in. |
| 275 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 273 } | 276 } |
| 274 | 277 |
| 275 void TearDown() override { | 278 void TearDown() override { |
| 276 client_download_request_subscription_.reset(); | 279 client_download_request_subscription_.reset(); |
| 277 sb_service_->ShutDown(); | 280 sb_service_->ShutDown(); |
| 278 // Flush all of the thread message loops to ensure that there are no | 281 // Flush all of the thread message loops to ensure that there are no |
| 279 // tasks currently running. | 282 // tasks currently running. |
| 280 FlushThreadMessageLoops(); | 283 FlushThreadMessageLoops(); |
| 281 sb_service_ = NULL; | 284 sb_service_ = NULL; |
| 282 } | 285 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 response.SerializeAsString(), | 381 response.SerializeAsString(), |
| 379 response_code, status); | 382 response_code, status); |
| 380 } | 383 } |
| 381 | 384 |
| 382 void PrepareBasicDownloadItem( | 385 void PrepareBasicDownloadItem( |
| 383 content::MockDownloadItem* item, | 386 content::MockDownloadItem* item, |
| 384 const std::vector<std::string> url_chain_items, | 387 const std::vector<std::string> url_chain_items, |
| 385 const std::string& referrer_url, | 388 const std::string& referrer_url, |
| 386 const base::FilePath::StringType& tmp_path_literal, | 389 const base::FilePath::StringType& tmp_path_literal, |
| 387 const base::FilePath::StringType& final_path_literal) { | 390 const base::FilePath::StringType& final_path_literal) { |
| 391 base::FilePath tmp_path = temp_dir_.path().Append(tmp_path_literal); |
| 392 base::FilePath final_path = temp_dir_.path().Append(final_path_literal); |
| 393 |
| 394 PrepareBasicDownloadItemWithFullPaths(item, url_chain_items, referrer_url, |
| 395 tmp_path, final_path); |
| 396 } |
| 397 |
| 398 void PrepareBasicDownloadItemWithFullPaths( |
| 399 content::MockDownloadItem* item, |
| 400 const std::vector<std::string> url_chain_items, |
| 401 const std::string& referrer_url, |
| 402 const base::FilePath& tmp_full_path, |
| 403 const base::FilePath& final_full_path) { |
| 388 url_chain_.clear(); | 404 url_chain_.clear(); |
| 389 for (std::string item: url_chain_items) | 405 for (std::string item: url_chain_items) |
| 390 url_chain_.push_back(GURL(item)); | 406 url_chain_.push_back(GURL(item)); |
| 391 referrer_ = GURL(referrer_url); | 407 referrer_ = GURL(referrer_url); |
| 392 tmp_path_ = base::FilePath( | 408 tmp_path_ = tmp_full_path; |
| 393 base::FilePath::StringPieceType(tmp_path_literal)); | 409 final_path_ = final_full_path; |
| 394 final_path_ = base::FilePath( | |
| 395 base::FilePath::StringPieceType(final_path_literal)); | |
| 396 hash_ = "hash"; | 410 hash_ = "hash"; |
| 397 | 411 |
| 398 EXPECT_CALL(*item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path_)); | 412 EXPECT_CALL(*item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path_)); |
| 399 EXPECT_CALL(*item, GetTargetFilePath()) | 413 EXPECT_CALL(*item, GetTargetFilePath()) |
| 400 .WillRepeatedly(ReturnRef(final_path_)); | 414 .WillRepeatedly(ReturnRef(final_path_)); |
| 401 EXPECT_CALL(*item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain_)); | 415 EXPECT_CALL(*item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain_)); |
| 402 EXPECT_CALL(*item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer_)); | 416 EXPECT_CALL(*item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer_)); |
| 403 EXPECT_CALL(*item, GetTabUrl()) | 417 EXPECT_CALL(*item, GetTabUrl()) |
| 404 .WillRepeatedly(ReturnRef(GURL::EmptyGURL())); | 418 .WillRepeatedly(ReturnRef(GURL::EmptyGURL())); |
| 405 EXPECT_CALL(*item, GetTabReferrerUrl()) | 419 EXPECT_CALL(*item, GetTabReferrerUrl()) |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 std::unique_ptr<ClientDownloadRequest> last_client_download_request_; | 517 std::unique_ptr<ClientDownloadRequest> last_client_download_request_; |
| 504 base::ScopedTempDir profile_dir_; | 518 base::ScopedTempDir profile_dir_; |
| 505 std::unique_ptr<TestingProfile> profile_; | 519 std::unique_ptr<TestingProfile> profile_; |
| 506 // The following 5 fields are used by PrepareBasicDownloadItem() function to | 520 // The following 5 fields are used by PrepareBasicDownloadItem() function to |
| 507 // store attributes of the last download item. | 521 // store attributes of the last download item. |
| 508 std::vector<GURL> url_chain_; | 522 std::vector<GURL> url_chain_; |
| 509 GURL referrer_; | 523 GURL referrer_; |
| 510 base::FilePath tmp_path_; | 524 base::FilePath tmp_path_; |
| 511 base::FilePath final_path_; | 525 base::FilePath final_path_; |
| 512 std::string hash_; | 526 std::string hash_; |
| 527 base::ScopedTempDir temp_dir_; |
| 513 }; | 528 }; |
| 514 | 529 |
| 515 | 530 |
| 516 void DownloadProtectionServiceTest::CheckClientDownloadReportCorruptZip( | 531 void DownloadProtectionServiceTest::CheckClientDownloadReportCorruptZip( |
| 517 bool is_extended_reporting, | 532 bool is_extended_reporting, |
| 518 bool is_incognito) { | 533 bool is_incognito) { |
| 519 net::FakeURLFetcherFactory factory(NULL); | 534 net::FakeURLFetcherFactory factory(NULL); |
| 520 PrepareResponse( | 535 PrepareResponse( |
| 521 &factory, ClientDownloadResponse::SAFE, net::HTTP_OK, | 536 &factory, ClientDownloadResponse::SAFE, net::HTTP_OK, |
| 522 net::URLRequestStatus::SUCCESS); | 537 net::URLRequestStatus::SUCCESS); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 EXPECT_EQ(ClientDownloadRequest_DownloadType_INVALID_ZIP, | 573 EXPECT_EQ(ClientDownloadRequest_DownloadType_INVALID_ZIP, |
| 559 GetClientDownloadRequest()->download_type()); | 574 GetClientDownloadRequest()->download_type()); |
| 560 ClearClientDownloadRequest(); | 575 ClearClientDownloadRequest(); |
| 561 } else { | 576 } else { |
| 562 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); | 577 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
| 563 EXPECT_FALSE(HasClientDownloadRequest()); | 578 EXPECT_FALSE(HasClientDownloadRequest()); |
| 564 } | 579 } |
| 565 | 580 |
| 566 Mock::VerifyAndClearExpectations(sb_service_.get()); | 581 Mock::VerifyAndClearExpectations(sb_service_.get()); |
| 567 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); | 582 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); |
| 568 | |
| 569 base::DeleteFile(tmp_path_, false); | |
| 570 } | 583 } |
| 571 | 584 |
| 572 | 585 |
| 573 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { | 586 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { |
| 574 content::MockDownloadItem item; | 587 content::MockDownloadItem item; |
| 575 PrepareBasicDownloadItem( | 588 PrepareBasicDownloadItem( |
| 576 &item, | 589 &item, |
| 577 std::vector<std::string>(), // empty url_chain | 590 std::vector<std::string>(), // empty url_chain |
| 578 "http://www.google.com/", // referrer | 591 "http://www.google.com/", // referrer |
| 579 FILE_PATH_LITERAL("a.tmp"), // tmp_path | 592 FILE_PATH_LITERAL("a.tmp"), // tmp_path |
| (...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1196 base::Unretained(this))); | 1209 base::Unretained(this))); |
| 1197 MessageLoop::current()->Run(); | 1210 MessageLoop::current()->Run(); |
| 1198 | 1211 |
| 1199 ASSERT_TRUE(HasClientDownloadRequest()); | 1212 ASSERT_TRUE(HasClientDownloadRequest()); |
| 1200 EXPECT_EQ(0, GetClientDownloadRequest()->archived_binary_size()); | 1213 EXPECT_EQ(0, GetClientDownloadRequest()->archived_binary_size()); |
| 1201 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); | 1214 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); |
| 1202 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_ARCHIVE, | 1215 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_ARCHIVE, |
| 1203 GetClientDownloadRequest()->download_type()); | 1216 GetClientDownloadRequest()->download_type()); |
| 1204 ClearClientDownloadRequest(); | 1217 ClearClientDownloadRequest(); |
| 1205 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); | 1218 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); |
| 1206 | |
| 1207 base::DeleteFile(tmp_path_, false); | |
| 1208 } | 1219 } |
| 1209 | 1220 |
| 1210 TEST_F(DownloadProtectionServiceTest, | 1221 TEST_F(DownloadProtectionServiceTest, |
| 1211 CheckClientDownloadReportCorruptZipNormal) { | 1222 CheckClientDownloadReportCorruptZipNormal) { |
| 1212 // !is_extended_reporting && !is_incognito | 1223 // !is_extended_reporting && !is_incognito |
| 1213 CheckClientDownloadReportCorruptZip(false, false); | 1224 CheckClientDownloadReportCorruptZip(false, false); |
| 1214 } | 1225 } |
| 1215 | 1226 |
| 1216 TEST_F(DownloadProtectionServiceTest, | 1227 TEST_F(DownloadProtectionServiceTest, |
| 1217 CheckClientDownloadReportCorruptZipExtended) { | 1228 CheckClientDownloadReportCorruptZipExtended) { |
| (...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1895 // Overriden by flag: | 1906 // Overriden by flag: |
| 1896 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); | 1907 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
| 1897 } | 1908 } |
| 1898 | 1909 |
| 1899 // Test a real .zip with a real .exe in it, where the .exe is manually | 1910 // Test a real .zip with a real .exe in it, where the .exe is manually |
| 1900 // blacklisted by hash. | 1911 // blacklisted by hash. |
| 1901 TEST_F(DownloadProtectionServiceFlagTest, | 1912 TEST_F(DownloadProtectionServiceFlagTest, |
| 1902 CheckClientDownloadZipOverridenByFlag) { | 1913 CheckClientDownloadZipOverridenByFlag) { |
| 1903 content::MockDownloadItem item; | 1914 content::MockDownloadItem item; |
| 1904 | 1915 |
| 1905 PrepareBasicDownloadItem( | 1916 PrepareBasicDownloadItemWithFullPaths( |
| 1906 &item, | 1917 &item, {"http://www.evil.com/a.exe"}, // url_chain |
| 1907 {"http://www.evil.com/a.exe"}, // url_chain | 1918 "http://www.google.com/", // referrer |
| 1908 "http://www.google.com/", // referrer | 1919 testdata_path_.AppendASCII( |
| 1909 testdata_path_.AppendASCII( | 1920 "zipfile_one_unsigned_binary.zip"), // tmp_path |
| 1910 "zipfile_one_unsigned_binary.zip").value(), // tmp_path | 1921 temp_dir_.path().Append(FILE_PATH_LITERAL("a.zip"))); // final_path |
| 1911 FILE_PATH_LITERAL("a.zip")); // final_path | |
| 1912 | 1922 |
| 1913 EXPECT_CALL(*sb_service_->mock_database_manager(), | 1923 EXPECT_CALL(*sb_service_->mock_database_manager(), |
| 1914 MatchDownloadWhitelistUrl(_)) | 1924 MatchDownloadWhitelistUrl(_)) |
| 1915 .WillRepeatedly(Return(false)); | 1925 .WillRepeatedly(Return(false)); |
| 1916 | 1926 |
| 1917 download_service_->CheckClientDownload( | 1927 download_service_->CheckClientDownload( |
| 1918 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 1928 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 1919 base::Unretained(this))); | 1929 base::Unretained(this))); |
| 1920 MessageLoop::current()->Run(); | 1930 MessageLoop::current()->Run(); |
| 1921 | 1931 |
| 1922 EXPECT_FALSE(HasClientDownloadRequest()); | 1932 EXPECT_FALSE(HasClientDownloadRequest()); |
| 1923 // Overriden by flag: | 1933 // Overriden by flag: |
| 1924 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); | 1934 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
| 1925 } | 1935 } |
| 1926 | 1936 |
| 1927 } // namespace safe_browsing | 1937 } // namespace safe_browsing |
| OLD | NEW |