| 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 <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/command_line.h" |
| 13 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 15 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
| 16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/message_loop.h" | 19 #include "base/message_loop.h" |
| 19 #include "base/path_service.h" | 20 #include "base/path_service.h" |
| 20 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/threading/sequenced_worker_pool.h" | 22 #include "base/threading/sequenced_worker_pool.h" |
| 22 #include "chrome/browser/safe_browsing/database_manager.h" | 23 #include "chrome/browser/safe_browsing/database_manager.h" |
| 24 #include "chrome/browser/safe_browsing/download_feedback_service.h" |
| 23 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 25 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 24 #include "chrome/browser/safe_browsing/signature_util.h" | 26 #include "chrome/browser/safe_browsing/signature_util.h" |
| 27 #include "chrome/common/chrome_switches.h" |
| 25 #include "chrome/common/safe_browsing/csd.pb.h" | 28 #include "chrome/common/safe_browsing/csd.pb.h" |
| 26 #include "content/public/test/mock_download_item.h" | 29 #include "content/public/test/mock_download_item.h" |
| 27 #include "content/public/test/test_browser_thread.h" | 30 #include "content/public/test/test_browser_thread.h" |
| 28 #include "googleurl/src/gurl.h" | 31 #include "googleurl/src/gurl.h" |
| 29 #include "net/cert/x509_certificate.h" | 32 #include "net/cert/x509_certificate.h" |
| 30 #include "net/url_request/test_url_fetcher_factory.h" | 33 #include "net/url_request/test_url_fetcher_factory.h" |
| 31 #include "net/url_request/url_fetcher_delegate.h" | 34 #include "net/url_request/url_fetcher_delegate.h" |
| 32 #include "testing/gmock/include/gmock/gmock.h" | 35 #include "testing/gmock/include/gmock/gmock.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "third_party/zlib/google/zip.h" | 37 #include "third_party/zlib/google/zip.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 check->url_results[i] = threat_type; | 142 check->url_results[i] = threat_type; |
| 140 BrowserThread::PostTask(BrowserThread::IO, | 143 BrowserThread::PostTask(BrowserThread::IO, |
| 141 FROM_HERE, | 144 FROM_HERE, |
| 142 base::Bind(&OnSafeBrowsingResult, | 145 base::Bind(&OnSafeBrowsingResult, |
| 143 base::Owned(check))); | 146 base::Owned(check))); |
| 144 } | 147 } |
| 145 | 148 |
| 146 class DownloadProtectionServiceTest : public testing::Test { | 149 class DownloadProtectionServiceTest : public testing::Test { |
| 147 protected: | 150 protected: |
| 148 virtual void SetUp() { | 151 virtual void SetUp() { |
| 152 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 153 switches::kSbEnableDownloadFeedback); |
| 149 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | 154 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, |
| 150 &msg_loop_)); | 155 &msg_loop_)); |
| 151 // Start real threads for the IO and File threads so that the DCHECKs | 156 // Start real threads for the IO and File threads so that the DCHECKs |
| 152 // to test that we're on the correct thread work. | 157 // to test that we're on the correct thread work. |
| 153 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); | 158 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); |
| 154 ASSERT_TRUE(io_thread_->Start()); | 159 ASSERT_TRUE(io_thread_->Start()); |
| 155 sb_service_ = new StrictMock<FakeSafeBrowsingService>(); | 160 sb_service_ = new StrictMock<FakeSafeBrowsingService>(); |
| 156 sb_service_->Initialize(); | 161 sb_service_->Initialize(); |
| 157 signature_util_ = new StrictMock<MockSignatureUtil>(); | 162 signature_util_ = new StrictMock<MockSignatureUtil>(); |
| 158 download_service_ = sb_service_->download_protection_service(); | 163 download_service_ = sb_service_->download_protection_service(); |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 DownloadProtectionService::GetDownloadRequestUrl(), | 483 DownloadProtectionService::GetDownloadRequestUrl(), |
| 479 response.SerializePartialAsString(), | 484 response.SerializePartialAsString(), |
| 480 true); | 485 true); |
| 481 | 486 |
| 482 download_service_->CheckClientDownload( | 487 download_service_->CheckClientDownload( |
| 483 &item, | 488 &item, |
| 484 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 489 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 485 base::Unretained(this))); | 490 base::Unretained(this))); |
| 486 msg_loop_.Run(); | 491 msg_loop_.Run(); |
| 487 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); | 492 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
| 493 std::string feedback_ping; |
| 494 std::string feedback_response; |
| 495 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting( |
| 496 item, &feedback_ping, &feedback_response)); |
| 488 | 497 |
| 489 // If the response is dangerous the result should also be marked as dangerous. | 498 // If the response is dangerous the result should also be marked as dangerous. |
| 490 response.set_verdict(ClientDownloadResponse::DANGEROUS); | 499 response.set_verdict(ClientDownloadResponse::DANGEROUS); |
| 491 factory.SetFakeResponse( | 500 factory.SetFakeResponse( |
| 492 DownloadProtectionService::GetDownloadRequestUrl(), | 501 DownloadProtectionService::GetDownloadRequestUrl(), |
| 493 response.SerializeAsString(), | 502 response.SerializeAsString(), |
| 494 true); | 503 true); |
| 495 | 504 |
| 496 download_service_->CheckClientDownload( | 505 download_service_->CheckClientDownload( |
| 497 &item, | 506 &item, |
| 498 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 507 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 499 base::Unretained(this))); | 508 base::Unretained(this))); |
| 500 msg_loop_.Run(); | 509 msg_loop_.Run(); |
| 510 EXPECT_FALSE(DownloadFeedbackService::GetPingsForDownloadForTesting( |
| 511 item, &feedback_ping, &feedback_response)); |
| 501 #if defined(OS_WIN) | 512 #if defined(OS_WIN) |
| 502 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); | 513 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
| 503 #else | 514 #else |
| 504 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); | 515 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
| 505 #endif | 516 #endif |
| 506 | 517 |
| 507 // If the response is uncommon the result should also be marked as uncommon. | 518 // If the response is uncommon the result should also be marked as uncommon. |
| 508 response.set_verdict(ClientDownloadResponse::UNCOMMON); | 519 response.set_verdict(ClientDownloadResponse::UNCOMMON); |
| 509 factory.SetFakeResponse( | 520 factory.SetFakeResponse( |
| 510 DownloadProtectionService::GetDownloadRequestUrl(), | 521 DownloadProtectionService::GetDownloadRequestUrl(), |
| 511 response.SerializeAsString(), | 522 response.SerializeAsString(), |
| 512 true); | 523 true); |
| 513 | 524 |
| 514 download_service_->CheckClientDownload( | 525 download_service_->CheckClientDownload( |
| 515 &item, | 526 &item, |
| 516 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 527 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 517 base::Unretained(this))); | 528 base::Unretained(this))); |
| 518 msg_loop_.Run(); | 529 msg_loop_.Run(); |
| 519 #if defined(OS_WIN) | 530 #if defined(OS_WIN) |
| 520 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON)); | 531 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON)); |
| 532 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting( |
| 533 item, &feedback_ping, &feedback_response)); |
| 534 ClientDownloadRequest decoded_request; |
| 535 EXPECT_TRUE(decoded_request.ParseFromString(feedback_ping)); |
| 536 EXPECT_EQ(url_chain.back().spec(), decoded_request.url()); |
| 537 EXPECT_EQ(response.SerializeAsString(), feedback_response); |
| 521 #else | 538 #else |
| 522 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); | 539 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
| 523 #endif | 540 #endif |
| 524 | 541 |
| 525 // If the response is dangerous_host the result should also be marked as | 542 // If the response is dangerous_host the result should also be marked as |
| 526 // dangerous_host. | 543 // dangerous_host. |
| 527 response.set_verdict(ClientDownloadResponse::DANGEROUS_HOST); | 544 response.set_verdict(ClientDownloadResponse::DANGEROUS_HOST); |
| 528 factory.SetFakeResponse( | 545 factory.SetFakeResponse( |
| 529 DownloadProtectionService::GetDownloadRequestUrl(), | 546 DownloadProtectionService::GetDownloadRequestUrl(), |
| 530 response.SerializeAsString(), | 547 response.SerializeAsString(), |
| 531 true); | 548 true); |
| 532 | 549 |
| 533 download_service_->CheckClientDownload( | 550 download_service_->CheckClientDownload( |
| 534 &item, | 551 &item, |
| 535 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 552 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 536 base::Unretained(this))); | 553 base::Unretained(this))); |
| 537 msg_loop_.Run(); | 554 msg_loop_.Run(); |
| 538 #if defined(OS_WIN) | 555 #if defined(OS_WIN) |
| 539 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS_HOST)); | 556 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS_HOST)); |
| 557 EXPECT_TRUE(DownloadFeedbackService::GetPingsForDownloadForTesting( |
| 558 item, &feedback_ping, &feedback_response)); |
| 559 EXPECT_EQ(response.SerializeAsString(), feedback_response); |
| 540 #else | 560 #else |
| 541 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); | 561 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
| 542 #endif | 562 #endif |
| 543 } | 563 } |
| 544 | 564 |
| 545 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadHTTPS) { | 565 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadHTTPS) { |
| 546 ClientDownloadResponse response; | 566 ClientDownloadResponse response; |
| 547 response.set_verdict(ClientDownloadResponse::DANGEROUS); | 567 response.set_verdict(ClientDownloadResponse::DANGEROUS); |
| 548 net::FakeURLFetcherFactory factory(NULL); | 568 net::FakeURLFetcherFactory factory(NULL); |
| 549 factory.SetFakeResponse( | 569 factory.SetFakeResponse( |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1130 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); | 1150 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); |
| 1131 | 1151 |
| 1132 cert = ReadTestCertificate("test_c.pem"); | 1152 cert = ReadTestCertificate("test_c.pem"); |
| 1133 ASSERT_TRUE(cert.get()); | 1153 ASSERT_TRUE(cert.get()); |
| 1134 whitelist_strings.clear(); | 1154 whitelist_strings.clear(); |
| 1135 GetCertificateWhitelistStrings( | 1155 GetCertificateWhitelistStrings( |
| 1136 *cert.get(), *issuer_cert.get(), &whitelist_strings); | 1156 *cert.get(), *issuer_cert.get(), &whitelist_strings); |
| 1137 EXPECT_THAT(whitelist_strings, ElementsAre()); | 1157 EXPECT_THAT(whitelist_strings, ElementsAre()); |
| 1138 } | 1158 } |
| 1139 } // namespace safe_browsing | 1159 } // namespace safe_browsing |
| OLD | NEW |