| 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> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/base_paths.h" | 14 #include "base/base_paths.h" |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| 19 #include "base/files/file_util.h" | 19 #include "base/files/file_util.h" |
| 20 #include "base/files/scoped_temp_dir.h" | 20 #include "base/files/scoped_temp_dir.h" |
| 21 #include "base/location.h" |
| 21 #include "base/macros.h" | 22 #include "base/macros.h" |
| 22 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
| 23 #include "base/message_loop/message_loop.h" | 24 #include "base/message_loop/message_loop.h" |
| 24 #include "base/path_service.h" | 25 #include "base/path_service.h" |
| 25 #include "base/run_loop.h" | 26 #include "base/run_loop.h" |
| 27 #include "base/single_thread_task_runner.h" |
| 26 #include "base/strings/string_number_conversions.h" | 28 #include "base/strings/string_number_conversions.h" |
| 27 #include "base/threading/sequenced_worker_pool.h" | 29 #include "base/threading/sequenced_worker_pool.h" |
| 30 #include "base/threading/thread_task_runner_handle.h" |
| 28 #include "chrome/browser/history/history_service_factory.h" | 31 #include "chrome/browser/history/history_service_factory.h" |
| 29 #include "chrome/browser/safe_browsing/download_feedback_service.h" | 32 #include "chrome/browser/safe_browsing/download_feedback_service.h" |
| 30 #include "chrome/browser/safe_browsing/incident_reporting/incident_reporting_ser
vice.h" | 33 #include "chrome/browser/safe_browsing/incident_reporting/incident_reporting_ser
vice.h" |
| 31 #include "chrome/browser/safe_browsing/local_database_manager.h" | 34 #include "chrome/browser/safe_browsing/local_database_manager.h" |
| 32 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 35 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 33 #include "chrome/common/chrome_switches.h" | 36 #include "chrome/common/chrome_switches.h" |
| 34 #include "chrome/common/pref_names.h" | 37 #include "chrome/common/pref_names.h" |
| 35 #include "chrome/common/safe_browsing/binary_feature_extractor.h" | 38 #include "chrome/common/safe_browsing/binary_feature_extractor.h" |
| 36 #include "chrome/common/safe_browsing/csd.pb.h" | 39 #include "chrome/common/safe_browsing/csd.pb.h" |
| 37 #include "chrome/test/base/testing_profile.h" | 40 #include "chrome/test/base/testing_profile.h" |
| (...skipping 1292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1330 ASSERT_EQ(1, chain.element_size()); | 1333 ASSERT_EQ(1, chain.element_size()); |
| 1331 EXPECT_EQ("dummy cert data", chain.element(0).certificate()); | 1334 EXPECT_EQ("dummy cert data", chain.element(0).certificate()); |
| 1332 EXPECT_TRUE(request.has_image_headers()); | 1335 EXPECT_TRUE(request.has_image_headers()); |
| 1333 const ClientDownloadRequest_ImageHeaders& headers = | 1336 const ClientDownloadRequest_ImageHeaders& headers = |
| 1334 request.image_headers(); | 1337 request.image_headers(); |
| 1335 EXPECT_TRUE(headers.has_pe_headers()); | 1338 EXPECT_TRUE(headers.has_pe_headers()); |
| 1336 EXPECT_TRUE(headers.pe_headers().has_dos_header()); | 1339 EXPECT_TRUE(headers.pe_headers().has_dos_header()); |
| 1337 EXPECT_EQ("dummy dos header", headers.pe_headers().dos_header()); | 1340 EXPECT_EQ("dummy dos header", headers.pe_headers().dos_header()); |
| 1338 | 1341 |
| 1339 // Simulate the request finishing. | 1342 // Simulate the request finishing. |
| 1340 base::MessageLoop::current()->PostTask( | 1343 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1341 FROM_HERE, | 1344 FROM_HERE, |
| 1342 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, | 1345 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, |
| 1343 base::Unretained(this), fetcher)); | 1346 base::Unretained(this), fetcher)); |
| 1344 MessageLoop::current()->Run(); | 1347 MessageLoop::current()->Run(); |
| 1345 } | 1348 } |
| 1346 | 1349 |
| 1347 // Similar to above, but with an unsigned binary. | 1350 // Similar to above, but with an unsigned binary. |
| 1348 TEST_F(DownloadProtectionServiceTest, | 1351 TEST_F(DownloadProtectionServiceTest, |
| 1349 CheckClientDownloadValidateRequestNoSignature) { | 1352 CheckClientDownloadValidateRequestNoSignature) { |
| 1350 net::TestURLFetcherFactory factory; | 1353 net::TestURLFetcherFactory factory; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1390 ClientDownloadRequest::DOWNLOAD_REDIRECT, | 1393 ClientDownloadRequest::DOWNLOAD_REDIRECT, |
| 1391 "http://www.google.com/", "")); | 1394 "http://www.google.com/", "")); |
| 1392 EXPECT_TRUE(RequestContainsResource(request, | 1395 EXPECT_TRUE(RequestContainsResource(request, |
| 1393 ClientDownloadRequest::DOWNLOAD_URL, | 1396 ClientDownloadRequest::DOWNLOAD_URL, |
| 1394 "ftp://www.google.com/bla.exe", | 1397 "ftp://www.google.com/bla.exe", |
| 1395 referrer_.spec())); | 1398 referrer_.spec())); |
| 1396 EXPECT_TRUE(request.has_signature()); | 1399 EXPECT_TRUE(request.has_signature()); |
| 1397 EXPECT_EQ(0, request.signature().certificate_chain_size()); | 1400 EXPECT_EQ(0, request.signature().certificate_chain_size()); |
| 1398 | 1401 |
| 1399 // Simulate the request finishing. | 1402 // Simulate the request finishing. |
| 1400 base::MessageLoop::current()->PostTask( | 1403 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1401 FROM_HERE, | 1404 FROM_HERE, |
| 1402 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, | 1405 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, |
| 1403 base::Unretained(this), fetcher)); | 1406 base::Unretained(this), fetcher)); |
| 1404 MessageLoop::current()->Run(); | 1407 MessageLoop::current()->Run(); |
| 1405 } | 1408 } |
| 1406 | 1409 |
| 1407 // Similar to above, but with tab history. | 1410 // Similar to above, but with tab history. |
| 1408 TEST_F(DownloadProtectionServiceTest, | 1411 TEST_F(DownloadProtectionServiceTest, |
| 1409 CheckClientDownloadValidateRequestTabHistory) { | 1412 CheckClientDownloadValidateRequestTabHistory) { |
| 1410 net::TestURLFetcherFactory factory; | 1413 net::TestURLFetcherFactory factory; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1478 ASSERT_EQ(1, chain.element_size()); | 1481 ASSERT_EQ(1, chain.element_size()); |
| 1479 EXPECT_EQ("dummy cert data", chain.element(0).certificate()); | 1482 EXPECT_EQ("dummy cert data", chain.element(0).certificate()); |
| 1480 EXPECT_TRUE(request.has_image_headers()); | 1483 EXPECT_TRUE(request.has_image_headers()); |
| 1481 const ClientDownloadRequest_ImageHeaders& headers = | 1484 const ClientDownloadRequest_ImageHeaders& headers = |
| 1482 request.image_headers(); | 1485 request.image_headers(); |
| 1483 EXPECT_TRUE(headers.has_pe_headers()); | 1486 EXPECT_TRUE(headers.has_pe_headers()); |
| 1484 EXPECT_TRUE(headers.pe_headers().has_dos_header()); | 1487 EXPECT_TRUE(headers.pe_headers().has_dos_header()); |
| 1485 EXPECT_EQ("dummy dos header", headers.pe_headers().dos_header()); | 1488 EXPECT_EQ("dummy dos header", headers.pe_headers().dos_header()); |
| 1486 | 1489 |
| 1487 // Simulate the request finishing. | 1490 // Simulate the request finishing. |
| 1488 base::MessageLoop::current()->PostTask( | 1491 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1489 FROM_HERE, | 1492 FROM_HERE, |
| 1490 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, | 1493 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, |
| 1491 base::Unretained(this), | 1494 base::Unretained(this), fetcher)); |
| 1492 fetcher)); | |
| 1493 MessageLoop::current()->Run(); | 1495 MessageLoop::current()->Run(); |
| 1494 } | 1496 } |
| 1495 | 1497 |
| 1496 // Now try with a history match. | 1498 // Now try with a history match. |
| 1497 { | 1499 { |
| 1498 history::RedirectList redirects; | 1500 history::RedirectList redirects; |
| 1499 redirects.push_back(GURL("http://tab.com/ref1")); | 1501 redirects.push_back(GURL("http://tab.com/ref1")); |
| 1500 redirects.push_back(GURL("http://tab.com/ref2")); | 1502 redirects.push_back(GURL("http://tab.com/ref2")); |
| 1501 redirects.push_back(tab_url); | 1503 redirects.push_back(tab_url); |
| 1502 HistoryServiceFactory::GetForProfile(profile_.get(), | 1504 HistoryServiceFactory::GetForProfile(profile_.get(), |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1551 tab_url.spec(), | 1553 tab_url.spec(), |
| 1552 tab_referrer.spec())); | 1554 tab_referrer.spec())); |
| 1553 EXPECT_TRUE(request.has_signature()); | 1555 EXPECT_TRUE(request.has_signature()); |
| 1554 ASSERT_EQ(1, request.signature().certificate_chain_size()); | 1556 ASSERT_EQ(1, request.signature().certificate_chain_size()); |
| 1555 const ClientDownloadRequest_CertificateChain& chain = | 1557 const ClientDownloadRequest_CertificateChain& chain = |
| 1556 request.signature().certificate_chain(0); | 1558 request.signature().certificate_chain(0); |
| 1557 ASSERT_EQ(1, chain.element_size()); | 1559 ASSERT_EQ(1, chain.element_size()); |
| 1558 EXPECT_EQ("dummy cert data", chain.element(0).certificate()); | 1560 EXPECT_EQ("dummy cert data", chain.element(0).certificate()); |
| 1559 | 1561 |
| 1560 // Simulate the request finishing. | 1562 // Simulate the request finishing. |
| 1561 base::MessageLoop::current()->PostTask( | 1563 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1562 FROM_HERE, | 1564 FROM_HERE, |
| 1563 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, | 1565 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, |
| 1564 base::Unretained(this), | 1566 base::Unretained(this), fetcher)); |
| 1565 fetcher)); | |
| 1566 MessageLoop::current()->Run(); | 1567 MessageLoop::current()->Run(); |
| 1567 } | 1568 } |
| 1568 } | 1569 } |
| 1569 | 1570 |
| 1570 TEST_F(DownloadProtectionServiceTest, TestCheckDownloadUrl) { | 1571 TEST_F(DownloadProtectionServiceTest, TestCheckDownloadUrl) { |
| 1571 net::TestURLFetcherFactory factory; | 1572 net::TestURLFetcherFactory factory; |
| 1572 | 1573 |
| 1573 std::vector<GURL> url_chain; | 1574 std::vector<GURL> url_chain; |
| 1574 url_chain.push_back(GURL("http://www.google.com/")); | 1575 url_chain.push_back(GURL("http://www.google.com/")); |
| 1575 url_chain.push_back(GURL("http://www.google.com/bla.exe")); | 1576 url_chain.push_back(GURL("http://www.google.com/bla.exe")); |
| (...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2134 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2135 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 2135 base::Unretained(this))); | 2136 base::Unretained(this))); |
| 2136 MessageLoop::current()->Run(); | 2137 MessageLoop::current()->Run(); |
| 2137 | 2138 |
| 2138 EXPECT_FALSE(HasClientDownloadRequest()); | 2139 EXPECT_FALSE(HasClientDownloadRequest()); |
| 2139 // Overriden by flag: | 2140 // Overriden by flag: |
| 2140 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); | 2141 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
| 2141 } | 2142 } |
| 2142 | 2143 |
| 2143 } // namespace safe_browsing | 2144 } // namespace safe_browsing |
| OLD | NEW |