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" |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); | 350 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); |
351 download_service_->CheckClientDownload( | 351 download_service_->CheckClientDownload( |
352 &item, | 352 &item, |
353 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 353 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
354 base::Unretained(this))); | 354 base::Unretained(this))); |
355 MessageLoop::current()->Run(); | 355 MessageLoop::current()->Run(); |
356 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); | 356 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
357 } | 357 } |
358 | 358 |
359 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) { | 359 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) { |
| 360 // Response to any requests will be DANGEROUS. |
| 361 ClientDownloadResponse response; |
| 362 response.set_verdict(ClientDownloadResponse::DANGEROUS); |
| 363 net::FakeURLFetcherFactory factory(NULL); |
| 364 factory.SetFakeResponse( |
| 365 DownloadProtectionService::GetDownloadRequestUrl(), |
| 366 response.SerializeAsString(), |
| 367 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 368 |
| 369 std::string hash = "hash"; |
360 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp")); | 370 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp")); |
361 base::FilePath a_exe(FILE_PATH_LITERAL("a.exe")); | 371 base::FilePath a_exe(FILE_PATH_LITERAL("a.exe")); |
362 std::vector<GURL> url_chain; | 372 std::vector<GURL> url_chain; |
363 url_chain.push_back(GURL("http://www.evil.com/bla.exe")); | 373 GURL referrer; |
364 url_chain.push_back(GURL("http://www.google.com/a.exe")); | |
365 GURL referrer("http://www.google.com/"); | |
366 | 374 |
367 content::MockDownloadItem item; | 375 content::MockDownloadItem item; |
368 EXPECT_CALL(item, AddObserver(_)).Times(2); | 376 EXPECT_CALL(item, AddObserver(_)).Times(4); |
369 EXPECT_CALL(item, RemoveObserver(_)).Times(2); | 377 EXPECT_CALL(item, RemoveObserver(_)).Times(4); |
370 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); | 378 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); |
371 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe)); | 379 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe)); |
372 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); | 380 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); |
373 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); | 381 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); |
| 382 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash)); |
| 383 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100)); |
| 384 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true)); |
| 385 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return("")); |
| 386 EXPECT_CALL(*signature_util_.get(), CheckSignature(a_tmp, _)).Times(4); |
| 387 |
| 388 // We should not get whilelist checks for other URLs than specified below. |
374 EXPECT_CALL(*sb_service_->mock_database_manager(), | 389 EXPECT_CALL(*sb_service_->mock_database_manager(), |
375 MatchDownloadWhitelistUrl(_)) | 390 MatchDownloadWhitelistUrl(_)).Times(0); |
| 391 EXPECT_CALL(*sb_service_->mock_database_manager(), |
| 392 MatchDownloadWhitelistUrl(GURL("http://www.evil.com/bla.exe"))) |
376 .WillRepeatedly(Return(false)); | 393 .WillRepeatedly(Return(false)); |
377 EXPECT_CALL(*sb_service_->mock_database_manager(), | 394 EXPECT_CALL(*sb_service_->mock_database_manager(), |
378 MatchDownloadWhitelistUrl(GURL("http://www.google.com/a.exe"))) | 395 MatchDownloadWhitelistUrl(GURL("http://www.google.com/a.exe"))) |
379 .WillRepeatedly(Return(true)); | 396 .WillRepeatedly(Return(true)); |
380 EXPECT_CALL(*signature_util_.get(), CheckSignature(a_tmp, _)).Times(2); | |
381 | 397 |
| 398 // With no referrer and just the bad url, should be marked DANGEROUS. |
| 399 url_chain.push_back(GURL("http://www.evil.com/bla.exe")); |
382 download_service_->CheckClientDownload( | 400 download_service_->CheckClientDownload( |
383 &item, | 401 &item, |
384 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 402 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
385 base::Unretained(this))); | 403 base::Unretained(this))); |
386 MessageLoop::current()->Run(); | 404 MessageLoop::current()->Run(); |
| 405 #if defined(OS_WIN) |
| 406 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
| 407 #else |
387 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); | 408 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
| 409 #endif |
388 | 410 |
389 // Check that the referrer is matched against the whitelist. | 411 // Check that the referrer is not matched against the whitelist. |
390 url_chain.pop_back(); | 412 referrer = GURL("http://www.google.com/"); |
391 referrer = GURL("http://www.google.com/a.exe"); | |
392 download_service_->CheckClientDownload( | 413 download_service_->CheckClientDownload( |
393 &item, | 414 &item, |
394 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 415 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 416 base::Unretained(this))); |
| 417 MessageLoop::current()->Run(); |
| 418 #if defined(OS_WIN) |
| 419 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
| 420 #else |
| 421 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
| 422 #endif |
| 423 |
| 424 // Redirect from a site shouldn't be checked either. |
| 425 url_chain.insert(url_chain.begin(), GURL("http://www.google.com/redirect")); |
| 426 download_service_->CheckClientDownload( |
| 427 &item, |
| 428 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 429 base::Unretained(this))); |
| 430 MessageLoop::current()->Run(); |
| 431 #if defined(OS_WIN) |
| 432 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
| 433 #else |
| 434 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
| 435 #endif |
| 436 |
| 437 // Only if the final url is whitelisted should it be SAFE. |
| 438 url_chain.push_back(GURL("http://www.google.com/a.exe")); |
| 439 download_service_->CheckClientDownload( |
| 440 &item, |
| 441 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
395 base::Unretained(this))); | 442 base::Unretained(this))); |
396 MessageLoop::current()->Run(); | 443 MessageLoop::current()->Run(); |
397 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); | 444 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
398 } | 445 } |
399 | 446 |
400 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) { | 447 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) { |
401 net::FakeURLFetcherFactory factory(NULL); | 448 net::FakeURLFetcherFactory factory(NULL); |
402 // HTTP request will fail. | 449 // HTTP request will fail. |
403 factory.SetFakeResponse( | 450 factory.SetFakeResponse( |
404 DownloadProtectionService::GetDownloadRequestUrl(), std::string(), | 451 DownloadProtectionService::GetDownloadRequestUrl(), std::string(), |
(...skipping 764 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1169 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); | 1216 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); |
1170 | 1217 |
1171 cert = ReadTestCertificate("test_c.pem"); | 1218 cert = ReadTestCertificate("test_c.pem"); |
1172 ASSERT_TRUE(cert.get()); | 1219 ASSERT_TRUE(cert.get()); |
1173 whitelist_strings.clear(); | 1220 whitelist_strings.clear(); |
1174 GetCertificateWhitelistStrings( | 1221 GetCertificateWhitelistStrings( |
1175 *cert.get(), *issuer_cert.get(), &whitelist_strings); | 1222 *cert.get(), *issuer_cert.get(), &whitelist_strings); |
1176 EXPECT_THAT(whitelist_strings, ElementsAre()); | 1223 EXPECT_THAT(whitelist_strings, ElementsAre()); |
1177 } | 1224 } |
1178 } // namespace safe_browsing | 1225 } // namespace safe_browsing |
OLD | NEW |