| 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 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_path)); | 271 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_path)); |
| 272 testdata_path_ = source_path | 272 testdata_path_ = source_path |
| 273 .AppendASCII("chrome") | 273 .AppendASCII("chrome") |
| 274 .AppendASCII("test") | 274 .AppendASCII("test") |
| 275 .AppendASCII("data") | 275 .AppendASCII("data") |
| 276 .AppendASCII("safe_browsing") | 276 .AppendASCII("safe_browsing") |
| 277 .AppendASCII("download_protection"); | 277 .AppendASCII("download_protection"); |
| 278 | 278 |
| 279 // Setup a profile | 279 // Setup a profile |
| 280 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 280 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
| 281 profile_.reset(new TestingProfile(profile_dir_.path())); | 281 profile_.reset(new TestingProfile(profile_dir_.GetPath())); |
| 282 ASSERT_TRUE(profile_->CreateHistoryService(true /* delete_file */, | 282 ASSERT_TRUE(profile_->CreateHistoryService(true /* delete_file */, |
| 283 false /* no_db */)); | 283 false /* no_db */)); |
| 284 | 284 |
| 285 // Setup a directory to place test files in. | 285 // Setup a directory to place test files in. |
| 286 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 286 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 287 | 287 |
| 288 // Turn off binary sampling by default. | 288 // Turn off binary sampling by default. |
| 289 SetBinarySamplingProbability(0.0); | 289 SetBinarySamplingProbability(0.0); |
| 290 } | 290 } |
| 291 | 291 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 response.SerializeAsString(), | 403 response.SerializeAsString(), |
| 404 response_code, status); | 404 response_code, status); |
| 405 } | 405 } |
| 406 | 406 |
| 407 void PrepareBasicDownloadItem( | 407 void PrepareBasicDownloadItem( |
| 408 content::MockDownloadItem* item, | 408 content::MockDownloadItem* item, |
| 409 const std::vector<std::string> url_chain_items, | 409 const std::vector<std::string> url_chain_items, |
| 410 const std::string& referrer_url, | 410 const std::string& referrer_url, |
| 411 const base::FilePath::StringType& tmp_path_literal, | 411 const base::FilePath::StringType& tmp_path_literal, |
| 412 const base::FilePath::StringType& final_path_literal) { | 412 const base::FilePath::StringType& final_path_literal) { |
| 413 base::FilePath tmp_path = temp_dir_.path().Append(tmp_path_literal); | 413 base::FilePath tmp_path = temp_dir_.GetPath().Append(tmp_path_literal); |
| 414 base::FilePath final_path = temp_dir_.path().Append(final_path_literal); | 414 base::FilePath final_path = temp_dir_.GetPath().Append(final_path_literal); |
| 415 | 415 |
| 416 PrepareBasicDownloadItemWithFullPaths(item, url_chain_items, referrer_url, | 416 PrepareBasicDownloadItemWithFullPaths(item, url_chain_items, referrer_url, |
| 417 tmp_path, final_path); | 417 tmp_path, final_path); |
| 418 } | 418 } |
| 419 | 419 |
| 420 void PrepareBasicDownloadItemWithFullPaths( | 420 void PrepareBasicDownloadItemWithFullPaths( |
| 421 content::MockDownloadItem* item, | 421 content::MockDownloadItem* item, |
| 422 const std::vector<std::string> url_chain_items, | 422 const std::vector<std::string> url_chain_items, |
| 423 const std::string& referrer_url, | 423 const std::string& referrer_url, |
| 424 const base::FilePath& tmp_full_path, | 424 const base::FilePath& tmp_full_path, |
| (...skipping 893 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1318 FILE_PATH_LITERAL("a.tmp"), // tmp_path | 1318 FILE_PATH_LITERAL("a.tmp"), // tmp_path |
| 1319 FILE_PATH_LITERAL("a.zip")); // final_path | 1319 FILE_PATH_LITERAL("a.zip")); // final_path |
| 1320 | 1320 |
| 1321 // Write out a zip archive to the temporary file. | 1321 // Write out a zip archive to the temporary file. |
| 1322 base::ScopedTempDir zip_source_dir; | 1322 base::ScopedTempDir zip_source_dir; |
| 1323 ASSERT_TRUE(zip_source_dir.CreateUniqueTempDir()); | 1323 ASSERT_TRUE(zip_source_dir.CreateUniqueTempDir()); |
| 1324 std::string file_contents = "dummy file"; | 1324 std::string file_contents = "dummy file"; |
| 1325 { | 1325 { |
| 1326 // In this case, it only contains a text file. | 1326 // In this case, it only contains a text file. |
| 1327 ASSERT_EQ(static_cast<int>(file_contents.size()), | 1327 ASSERT_EQ(static_cast<int>(file_contents.size()), |
| 1328 base::WriteFile( | 1328 base::WriteFile(zip_source_dir.GetPath().Append( |
| 1329 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.txt")), | 1329 FILE_PATH_LITERAL("file.txt")), |
| 1330 file_contents.data(), file_contents.size())); | 1330 file_contents.data(), file_contents.size())); |
| 1331 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false)); | 1331 ASSERT_TRUE(zip::Zip(zip_source_dir.GetPath(), tmp_path_, false)); |
| 1332 RunLoop run_loop; | 1332 RunLoop run_loop; |
| 1333 download_service_->CheckClientDownload( | 1333 download_service_->CheckClientDownload( |
| 1334 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 1334 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 1335 base::Unretained(this), run_loop.QuitClosure())); | 1335 base::Unretained(this), run_loop.QuitClosure())); |
| 1336 run_loop.Run(); | 1336 run_loop.Run(); |
| 1337 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); | 1337 EXPECT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
| 1338 EXPECT_FALSE(HasClientDownloadRequest()); | 1338 EXPECT_FALSE(HasClientDownloadRequest()); |
| 1339 Mock::VerifyAndClearExpectations(sb_service_.get()); | 1339 Mock::VerifyAndClearExpectations(sb_service_.get()); |
| 1340 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); | 1340 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); |
| 1341 } | 1341 } |
| 1342 { | 1342 { |
| 1343 // Now check with an executable in the zip file as well. | 1343 // Now check with an executable in the zip file as well. |
| 1344 ASSERT_EQ(static_cast<int>(file_contents.size()), | 1344 ASSERT_EQ(static_cast<int>(file_contents.size()), |
| 1345 base::WriteFile( | 1345 base::WriteFile(zip_source_dir.GetPath().Append( |
| 1346 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.exe")), | 1346 FILE_PATH_LITERAL("file.exe")), |
| 1347 file_contents.data(), file_contents.size())); | 1347 file_contents.data(), file_contents.size())); |
| 1348 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false)); | 1348 ASSERT_TRUE(zip::Zip(zip_source_dir.GetPath(), tmp_path_, false)); |
| 1349 EXPECT_CALL(*sb_service_->mock_database_manager(), | 1349 EXPECT_CALL(*sb_service_->mock_database_manager(), |
| 1350 MatchDownloadWhitelistUrl(_)) | 1350 MatchDownloadWhitelistUrl(_)) |
| 1351 .WillRepeatedly(Return(false)); | 1351 .WillRepeatedly(Return(false)); |
| 1352 RunLoop run_loop; | 1352 RunLoop run_loop; |
| 1353 download_service_->CheckClientDownload( | 1353 download_service_->CheckClientDownload( |
| 1354 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 1354 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 1355 base::Unretained(this), run_loop.QuitClosure())); | 1355 base::Unretained(this), run_loop.QuitClosure())); |
| 1356 run_loop.Run(); | 1356 run_loop.Run(); |
| 1357 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); | 1357 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
| 1358 ASSERT_TRUE(HasClientDownloadRequest()); | 1358 ASSERT_TRUE(HasClientDownloadRequest()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1383 run_loop.Run(); | 1383 run_loop.Run(); |
| 1384 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); | 1384 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
| 1385 EXPECT_TRUE(HasClientDownloadRequest()); | 1385 EXPECT_TRUE(HasClientDownloadRequest()); |
| 1386 ClearClientDownloadRequest(); | 1386 ClearClientDownloadRequest(); |
| 1387 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); | 1387 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); |
| 1388 } | 1388 } |
| 1389 { | 1389 { |
| 1390 // Repeat the test with an archive inside the zip file in addition to the | 1390 // Repeat the test with an archive inside the zip file in addition to the |
| 1391 // executable. | 1391 // executable. |
| 1392 ASSERT_EQ(static_cast<int>(file_contents.size()), | 1392 ASSERT_EQ(static_cast<int>(file_contents.size()), |
| 1393 base::WriteFile( | 1393 base::WriteFile(zip_source_dir.GetPath().Append( |
| 1394 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.rar")), | 1394 FILE_PATH_LITERAL("file.rar")), |
| 1395 file_contents.data(), file_contents.size())); | 1395 file_contents.data(), file_contents.size())); |
| 1396 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false)); | 1396 ASSERT_TRUE(zip::Zip(zip_source_dir.GetPath(), tmp_path_, false)); |
| 1397 RunLoop run_loop; | 1397 RunLoop run_loop; |
| 1398 download_service_->CheckClientDownload( | 1398 download_service_->CheckClientDownload( |
| 1399 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 1399 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 1400 base::Unretained(this), run_loop.QuitClosure())); | 1400 base::Unretained(this), run_loop.QuitClosure())); |
| 1401 run_loop.Run(); | 1401 run_loop.Run(); |
| 1402 ASSERT_TRUE(HasClientDownloadRequest()); | 1402 ASSERT_TRUE(HasClientDownloadRequest()); |
| 1403 EXPECT_EQ(1, GetClientDownloadRequest()->archived_binary_size()); | 1403 EXPECT_EQ(1, GetClientDownloadRequest()->archived_binary_size()); |
| 1404 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); | 1404 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); |
| 1405 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_EXECUTABLE, | 1405 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_EXECUTABLE, |
| 1406 GetClientDownloadRequest()->download_type()); | 1406 GetClientDownloadRequest()->download_type()); |
| 1407 ClearClientDownloadRequest(); | 1407 ClearClientDownloadRequest(); |
| 1408 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); | 1408 Mock::VerifyAndClearExpectations(binary_feature_extractor_.get()); |
| 1409 } | 1409 } |
| 1410 { | 1410 { |
| 1411 // Repeat the test with just the archive inside the zip file. | 1411 // Repeat the test with just the archive inside the zip file. |
| 1412 ASSERT_TRUE( | 1412 ASSERT_TRUE(base::DeleteFile( |
| 1413 base::DeleteFile(zip_source_dir.path().AppendASCII("file.exe"), false)); | 1413 zip_source_dir.GetPath().AppendASCII("file.exe"), false)); |
| 1414 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), tmp_path_, false)); | 1414 ASSERT_TRUE(zip::Zip(zip_source_dir.GetPath(), tmp_path_, false)); |
| 1415 RunLoop run_loop; | 1415 RunLoop run_loop; |
| 1416 download_service_->CheckClientDownload( | 1416 download_service_->CheckClientDownload( |
| 1417 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 1417 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 1418 base::Unretained(this), run_loop.QuitClosure())); | 1418 base::Unretained(this), run_loop.QuitClosure())); |
| 1419 run_loop.Run(); | 1419 run_loop.Run(); |
| 1420 ASSERT_TRUE(HasClientDownloadRequest()); | 1420 ASSERT_TRUE(HasClientDownloadRequest()); |
| 1421 EXPECT_EQ(0, GetClientDownloadRequest()->archived_binary_size()); | 1421 EXPECT_EQ(0, GetClientDownloadRequest()->archived_binary_size()); |
| 1422 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); | 1422 EXPECT_TRUE(GetClientDownloadRequest()->has_download_type()); |
| 1423 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_ARCHIVE, | 1423 EXPECT_EQ(ClientDownloadRequest_DownloadType_ZIPPED_ARCHIVE, |
| 1424 GetClientDownloadRequest()->download_type()); | 1424 GetClientDownloadRequest()->download_type()); |
| (...skipping 920 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2345 // Test a real .zip with a real .exe in it, where the .exe is manually | 2345 // Test a real .zip with a real .exe in it, where the .exe is manually |
| 2346 // blacklisted by hash. | 2346 // blacklisted by hash. |
| 2347 TEST_F(DownloadProtectionServiceFlagTest, | 2347 TEST_F(DownloadProtectionServiceFlagTest, |
| 2348 CheckClientDownloadZipOverridenByFlag) { | 2348 CheckClientDownloadZipOverridenByFlag) { |
| 2349 content::MockDownloadItem item; | 2349 content::MockDownloadItem item; |
| 2350 | 2350 |
| 2351 PrepareBasicDownloadItemWithFullPaths( | 2351 PrepareBasicDownloadItemWithFullPaths( |
| 2352 &item, {"http://www.evil.com/a.exe"}, // url_chain | 2352 &item, {"http://www.evil.com/a.exe"}, // url_chain |
| 2353 "http://www.google.com/", // referrer | 2353 "http://www.google.com/", // referrer |
| 2354 testdata_path_.AppendASCII( | 2354 testdata_path_.AppendASCII( |
| 2355 "zipfile_one_unsigned_binary.zip"), // tmp_path | 2355 "zipfile_one_unsigned_binary.zip"), // tmp_path |
| 2356 temp_dir_.path().Append(FILE_PATH_LITERAL("a.zip"))); // final_path | 2356 temp_dir_.GetPath().Append(FILE_PATH_LITERAL("a.zip"))); // final_path |
| 2357 | 2357 |
| 2358 EXPECT_CALL(*sb_service_->mock_database_manager(), | 2358 EXPECT_CALL(*sb_service_->mock_database_manager(), |
| 2359 MatchDownloadWhitelistUrl(_)) | 2359 MatchDownloadWhitelistUrl(_)) |
| 2360 .WillRepeatedly(Return(false)); | 2360 .WillRepeatedly(Return(false)); |
| 2361 | 2361 |
| 2362 RunLoop run_loop; | 2362 RunLoop run_loop; |
| 2363 download_service_->CheckClientDownload( | 2363 download_service_->CheckClientDownload( |
| 2364 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2364 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 2365 base::Unretained(this), run_loop.QuitClosure())); | 2365 base::Unretained(this), run_loop.QuitClosure())); |
| 2366 run_loop.Run(); | 2366 run_loop.Run(); |
| 2367 | 2367 |
| 2368 EXPECT_FALSE(HasClientDownloadRequest()); | 2368 EXPECT_FALSE(HasClientDownloadRequest()); |
| 2369 // Overriden by flag: | 2369 // Overriden by flag: |
| 2370 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); | 2370 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
| 2371 } | 2371 } |
| 2372 | 2372 |
| 2373 } // namespace safe_browsing | 2373 } // namespace safe_browsing |
| OLD | NEW |