Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: chrome/browser/safe_browsing/download_protection_service_unittest.cc

Issue 12313141: Use DownloadItem directly in DownloadProtectionService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review fixes Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/safe_browsing/download_protection_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/file_util.h" 13 #include "base/file_util.h"
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/files/scoped_temp_dir.h" 15 #include "base/files/scoped_temp_dir.h"
16 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop.h" 18 #include "base/message_loop.h"
19 #include "base/path_service.h" 19 #include "base/path_service.h"
20 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
21 #include "base/threading/sequenced_worker_pool.h" 21 #include "base/threading/sequenced_worker_pool.h"
22 #include "chrome/browser/safe_browsing/database_manager.h" 22 #include "chrome/browser/safe_browsing/database_manager.h"
23 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 23 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
24 #include "chrome/browser/safe_browsing/signature_util.h" 24 #include "chrome/browser/safe_browsing/signature_util.h"
25 #include "chrome/common/safe_browsing/csd.pb.h" 25 #include "chrome/common/safe_browsing/csd.pb.h"
26 #include "chrome/common/zip.h" 26 #include "chrome/common/zip.h"
27 #include "content/public/browser/download_item.h" 27 #include "content/public/test/mock_download_item.h"
28 #include "content/public/test/test_browser_thread.h" 28 #include "content/public/test/test_browser_thread.h"
29 #include "googleurl/src/gurl.h" 29 #include "googleurl/src/gurl.h"
30 #include "net/base/x509_certificate.h" 30 #include "net/base/x509_certificate.h"
31 #include "net/url_request/test_url_fetcher_factory.h" 31 #include "net/url_request/test_url_fetcher_factory.h"
32 #include "net/url_request/url_fetcher_delegate.h" 32 #include "net/url_request/url_fetcher_delegate.h"
33 #include "testing/gmock/include/gmock/gmock.h" 33 #include "testing/gmock/include/gmock/gmock.h"
34 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
35 35
36 using ::testing::ContainerEq; 36 using ::testing::ContainerEq;
37 using ::testing::DoAll; 37 using ::testing::DoAll;
38 using ::testing::ElementsAre; 38 using ::testing::ElementsAre;
39 using ::testing::Mock; 39 using ::testing::Mock;
40 using ::testing::NotNull; 40 using ::testing::NotNull;
41 using ::testing::Return; 41 using ::testing::Return;
42 using ::testing::ReturnRef;
42 using ::testing::SaveArg; 43 using ::testing::SaveArg;
43 using ::testing::StrictMock; 44 using ::testing::StrictMock;
44 using ::testing::_; 45 using ::testing::_;
45 using content::BrowserThread; 46 using content::BrowserThread;
46 namespace safe_browsing { 47 namespace safe_browsing {
47 namespace { 48 namespace {
48 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for 49 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for
49 // a given URL. 50 // a given URL.
50 class MockSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager { 51 class MockSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager {
51 public: 52 public:
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 DownloadProtectionService* download_service_; 303 DownloadProtectionService* download_service_;
303 MessageLoop msg_loop_; 304 MessageLoop msg_loop_;
304 DownloadProtectionService::DownloadCheckResult result_; 305 DownloadProtectionService::DownloadCheckResult result_;
305 bool has_result_; 306 bool has_result_;
306 scoped_ptr<content::TestBrowserThread> io_thread_; 307 scoped_ptr<content::TestBrowserThread> io_thread_;
307 scoped_ptr<content::TestBrowserThread> ui_thread_; 308 scoped_ptr<content::TestBrowserThread> ui_thread_;
308 base::FilePath testdata_path_; 309 base::FilePath testdata_path_;
309 }; 310 };
310 311
311 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { 312 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) {
312 DownloadProtectionService::DownloadInfo info; 313 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp"));
314 base::FilePath a_exe(FILE_PATH_LITERAL("a.exe"));
315 std::vector<GURL> url_chain;
316 GURL referrer("http://www.google.com/");
317
318 content::MockDownloadItem item;
319 EXPECT_CALL(item, AddObserver(_));
320 EXPECT_CALL(item, RemoveObserver(_));
321 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
322 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
323 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
324 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
313 download_service_->CheckClientDownload( 325 download_service_->CheckClientDownload(
314 info, 326 &item,
315 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 327 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
316 base::Unretained(this))); 328 base::Unretained(this)));
317 msg_loop_.Run(); 329 msg_loop_.Run();
318 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 330 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
331 Mock::VerifyAndClearExpectations(&item);
319 332
320 info.local_file = base::FilePath(FILE_PATH_LITERAL("a.tmp")); 333 url_chain.push_back(GURL("file://www.google.com/"));
321 info.target_file = base::FilePath(FILE_PATH_LITERAL("a.exe")); 334 EXPECT_CALL(item, AddObserver(_));
322 info.download_url_chain.push_back(GURL("file://www.google.com/")); 335 EXPECT_CALL(item, RemoveObserver(_));
336 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
337 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
338 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
339 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
323 download_service_->CheckClientDownload( 340 download_service_->CheckClientDownload(
324 info, 341 &item,
325 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 342 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
326 base::Unretained(this))); 343 base::Unretained(this)));
327 msg_loop_.Run(); 344 msg_loop_.Run();
328 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 345 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
329 } 346 }
330 347
331 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) { 348 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) {
332 DownloadProtectionService::DownloadInfo info; 349 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp"));
333 info.local_file = base::FilePath(FILE_PATH_LITERAL("a.tmp")); 350 base::FilePath a_exe(FILE_PATH_LITERAL("a.exe"));
334 info.target_file = base::FilePath(FILE_PATH_LITERAL("a.exe")); 351 std::vector<GURL> url_chain;
335 info.download_url_chain.push_back(GURL("http://www.evil.com/bla.exe")); 352 url_chain.push_back(GURL("http://www.evil.com/bla.exe"));
336 info.download_url_chain.push_back(GURL("http://www.google.com/a.exe")); 353 url_chain.push_back(GURL("http://www.google.com/a.exe"));
337 info.referrer_url = GURL("http://www.google.com/"); 354 GURL referrer("http://www.google.com/");
338 355
356 content::MockDownloadItem item;
357 EXPECT_CALL(item, AddObserver(_)).Times(2);
358 EXPECT_CALL(item, RemoveObserver(_)).Times(2);
359 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
360 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
361 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
362 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
339 EXPECT_CALL(*sb_service_->mock_database_manager(), 363 EXPECT_CALL(*sb_service_->mock_database_manager(),
340 MatchDownloadWhitelistUrl(_)) 364 MatchDownloadWhitelistUrl(_))
341 .WillRepeatedly(Return(false)); 365 .WillRepeatedly(Return(false));
342 EXPECT_CALL(*sb_service_->mock_database_manager(), 366 EXPECT_CALL(*sb_service_->mock_database_manager(),
343 MatchDownloadWhitelistUrl(GURL("http://www.google.com/a.exe"))) 367 MatchDownloadWhitelistUrl(GURL("http://www.google.com/a.exe")))
344 .WillRepeatedly(Return(true)); 368 .WillRepeatedly(Return(true));
345 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)).Times(2); 369 EXPECT_CALL(*signature_util_, CheckSignature(a_tmp, _)).Times(2);
346 370
347 download_service_->CheckClientDownload( 371 download_service_->CheckClientDownload(
348 info, 372 &item,
349 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 373 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
350 base::Unretained(this))); 374 base::Unretained(this)));
351 msg_loop_.Run(); 375 msg_loop_.Run();
352 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 376 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
353 377
354 // Check that the referrer is matched against the whitelist. 378 // Check that the referrer is matched against the whitelist.
355 info.download_url_chain.pop_back(); 379 url_chain.pop_back();
356 info.referrer_url = GURL("http://www.google.com/a.exe"); 380 referrer = GURL("http://www.google.com/a.exe");
357 download_service_->CheckClientDownload( 381 download_service_->CheckClientDownload(
358 info, 382 &item,
359 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 383 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
360 base::Unretained(this))); 384 base::Unretained(this)));
361 msg_loop_.Run(); 385 msg_loop_.Run();
362 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 386 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
363 } 387 }
364 388
365 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) { 389 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) {
366 net::FakeURLFetcherFactory factory(NULL); 390 net::FakeURLFetcherFactory factory(NULL);
367 // HTTP request will fail. 391 // HTTP request will fail.
368 factory.SetFakeResponse( 392 factory.SetFakeResponse(
369 DownloadProtectionService::GetDownloadRequestUrl(), "", false); 393 DownloadProtectionService::GetDownloadRequestUrl(), "", false);
370 394
371 DownloadProtectionService::DownloadInfo info; 395 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp"));
372 info.local_file = base::FilePath(FILE_PATH_LITERAL("a.tmp")); 396 base::FilePath a_exe(FILE_PATH_LITERAL("a.exe"));
373 info.target_file = base::FilePath(FILE_PATH_LITERAL("a.exe")); 397 std::vector<GURL> url_chain;
374 info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe")); 398 url_chain.push_back(GURL("http://www.evil.com/a.exe"));
375 info.referrer_url = GURL("http://www.google.com/"); 399 GURL referrer("http://www.google.com/");
400 std::string hash = "hash";
401
402 content::MockDownloadItem item;
403 EXPECT_CALL(item, AddObserver(_));
404 EXPECT_CALL(item, RemoveObserver(_));
405 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
406 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
407 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
408 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
409 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
410 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100));
411 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true));
412 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(""));
376 413
377 EXPECT_CALL(*sb_service_->mock_database_manager(), 414 EXPECT_CALL(*sb_service_->mock_database_manager(),
378 MatchDownloadWhitelistUrl(_)) 415 MatchDownloadWhitelistUrl(_))
379 .WillRepeatedly(Return(false)); 416 .WillRepeatedly(Return(false));
380 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)); 417 EXPECT_CALL(*signature_util_, CheckSignature(a_tmp, _));
381 418
382 download_service_->CheckClientDownload( 419 download_service_->CheckClientDownload(
383 info, 420 &item,
384 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 421 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
385 base::Unretained(this))); 422 base::Unretained(this)));
386 msg_loop_.Run(); 423 msg_loop_.Run();
387 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 424 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
388 } 425 }
389 426
390 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSuccess) { 427 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSuccess) {
391 ClientDownloadResponse response; 428 ClientDownloadResponse response;
392 response.set_verdict(ClientDownloadResponse::SAFE); 429 response.set_verdict(ClientDownloadResponse::SAFE);
393 net::FakeURLFetcherFactory factory(NULL); 430 net::FakeURLFetcherFactory factory(NULL);
394 // Empty response means SAFE. 431 // Empty response means SAFE.
395 factory.SetFakeResponse( 432 factory.SetFakeResponse(
396 DownloadProtectionService::GetDownloadRequestUrl(), 433 DownloadProtectionService::GetDownloadRequestUrl(),
397 response.SerializeAsString(), 434 response.SerializeAsString(),
398 true); 435 true);
399 436
400 DownloadProtectionService::DownloadInfo info; 437 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp"));
401 info.local_file = base::FilePath(FILE_PATH_LITERAL("a.tmp")); 438 base::FilePath a_exe(FILE_PATH_LITERAL("a.exe"));
402 info.target_file = base::FilePath(FILE_PATH_LITERAL("a.exe")); 439 std::vector<GURL> url_chain;
403 info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe")); 440 url_chain.push_back(GURL("http://www.evil.com/a.exe"));
404 info.referrer_url = GURL("http://www.google.com/"); 441 GURL referrer("http://www.google.com/");
442 std::string hash = "hash";
443
444 content::MockDownloadItem item;
445 EXPECT_CALL(item, AddObserver(_)).Times(5);
446 EXPECT_CALL(item, RemoveObserver(_)).Times(5);
447 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
448 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
449 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
450 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
451 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
452 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100));
453 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true));
454 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(""));
405 455
406 EXPECT_CALL(*sb_service_->mock_database_manager(), 456 EXPECT_CALL(*sb_service_->mock_database_manager(),
407 MatchDownloadWhitelistUrl(_)) 457 MatchDownloadWhitelistUrl(_))
408 .WillRepeatedly(Return(false)); 458 .WillRepeatedly(Return(false));
409 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)).Times(5); 459 EXPECT_CALL(*signature_util_, CheckSignature(a_tmp, _)).Times(5);
410 460
411 download_service_->CheckClientDownload( 461 download_service_->CheckClientDownload(
412 info, 462 &item,
413 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 463 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
414 base::Unretained(this))); 464 base::Unretained(this)));
415 msg_loop_.Run(); 465 msg_loop_.Run();
416 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 466 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
417 467
418 // Invalid response should be safe too. 468 // Invalid response should be safe too.
419 response.Clear(); 469 response.Clear();
420 factory.SetFakeResponse( 470 factory.SetFakeResponse(
421 DownloadProtectionService::GetDownloadRequestUrl(), 471 DownloadProtectionService::GetDownloadRequestUrl(),
422 response.SerializePartialAsString(), 472 response.SerializePartialAsString(),
423 true); 473 true);
424 474
425 download_service_->CheckClientDownload( 475 download_service_->CheckClientDownload(
426 info, 476 &item,
427 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 477 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
428 base::Unretained(this))); 478 base::Unretained(this)));
429 msg_loop_.Run(); 479 msg_loop_.Run();
430 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 480 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
431 481
432 // If the response is dangerous the result should also be marked as dangerous. 482 // If the response is dangerous the result should also be marked as dangerous.
433 response.set_verdict(ClientDownloadResponse::DANGEROUS); 483 response.set_verdict(ClientDownloadResponse::DANGEROUS);
434 factory.SetFakeResponse( 484 factory.SetFakeResponse(
435 DownloadProtectionService::GetDownloadRequestUrl(), 485 DownloadProtectionService::GetDownloadRequestUrl(),
436 response.SerializeAsString(), 486 response.SerializeAsString(),
437 true); 487 true);
438 488
439 download_service_->CheckClientDownload( 489 download_service_->CheckClientDownload(
440 info, 490 &item,
441 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 491 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
442 base::Unretained(this))); 492 base::Unretained(this)));
443 msg_loop_.Run(); 493 msg_loop_.Run();
444 #if defined(OS_WIN) 494 #if defined(OS_WIN)
445 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 495 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
446 #else 496 #else
447 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 497 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
448 #endif 498 #endif
449 499
450 // If the response is uncommon the result should also be marked as uncommon. 500 // If the response is uncommon the result should also be marked as uncommon.
451 response.set_verdict(ClientDownloadResponse::UNCOMMON); 501 response.set_verdict(ClientDownloadResponse::UNCOMMON);
452 factory.SetFakeResponse( 502 factory.SetFakeResponse(
453 DownloadProtectionService::GetDownloadRequestUrl(), 503 DownloadProtectionService::GetDownloadRequestUrl(),
454 response.SerializeAsString(), 504 response.SerializeAsString(),
455 true); 505 true);
456 506
457 download_service_->CheckClientDownload( 507 download_service_->CheckClientDownload(
458 info, 508 &item,
459 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 509 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
460 base::Unretained(this))); 510 base::Unretained(this)));
461 msg_loop_.Run(); 511 msg_loop_.Run();
462 #if defined(OS_WIN) 512 #if defined(OS_WIN)
463 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON)); 513 EXPECT_TRUE(IsResult(DownloadProtectionService::UNCOMMON));
464 #else 514 #else
465 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 515 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
466 #endif 516 #endif
467 517
468 // If the response is dangerous_host the result should also be marked as 518 // If the response is dangerous_host the result should also be marked as
469 // dangerous_host. 519 // dangerous_host.
470 response.set_verdict(ClientDownloadResponse::DANGEROUS_HOST); 520 response.set_verdict(ClientDownloadResponse::DANGEROUS_HOST);
471 factory.SetFakeResponse( 521 factory.SetFakeResponse(
472 DownloadProtectionService::GetDownloadRequestUrl(), 522 DownloadProtectionService::GetDownloadRequestUrl(),
473 response.SerializeAsString(), 523 response.SerializeAsString(),
474 true); 524 true);
475 525
476 download_service_->CheckClientDownload( 526 download_service_->CheckClientDownload(
477 info, 527 &item,
478 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 528 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
479 base::Unretained(this))); 529 base::Unretained(this)));
480 msg_loop_.Run(); 530 msg_loop_.Run();
481 #if defined(OS_WIN) 531 #if defined(OS_WIN)
482 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS_HOST)); 532 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS_HOST));
483 #else 533 #else
484 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 534 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
485 #endif 535 #endif
486 } 536 }
487 537
488 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadHTTPS) { 538 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadHTTPS) {
489 ClientDownloadResponse response; 539 ClientDownloadResponse response;
490 response.set_verdict(ClientDownloadResponse::DANGEROUS); 540 response.set_verdict(ClientDownloadResponse::DANGEROUS);
491 net::FakeURLFetcherFactory factory(NULL); 541 net::FakeURLFetcherFactory factory(NULL);
492 factory.SetFakeResponse( 542 factory.SetFakeResponse(
493 DownloadProtectionService::GetDownloadRequestUrl(), 543 DownloadProtectionService::GetDownloadRequestUrl(),
494 response.SerializeAsString(), 544 response.SerializeAsString(),
495 true); 545 true);
496 546
497 DownloadProtectionService::DownloadInfo info; 547 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp"));
498 info.local_file = base::FilePath(FILE_PATH_LITERAL("a.tmp")); 548 base::FilePath a_exe(FILE_PATH_LITERAL("a.exe"));
499 info.target_file = base::FilePath(FILE_PATH_LITERAL("a.exe")); 549 std::vector<GURL> url_chain;
500 info.download_url_chain.push_back(GURL("https://www.evil.com/a.exe")); 550 url_chain.push_back(GURL("http://www.evil.com/a.exe"));
501 info.referrer_url = GURL("http://www.google.com/"); 551 GURL referrer("http://www.google.com/");
552 std::string hash = "hash";
553
554 content::MockDownloadItem item;
555 EXPECT_CALL(item, AddObserver(_)).Times(1);
556 EXPECT_CALL(item, RemoveObserver(_)).Times(1);
557 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
558 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
559 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
560 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
561 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
562 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100));
563 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true));
564 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(""));
502 565
503 EXPECT_CALL(*sb_service_->mock_database_manager(), 566 EXPECT_CALL(*sb_service_->mock_database_manager(),
504 MatchDownloadWhitelistUrl(_)) 567 MatchDownloadWhitelistUrl(_))
505 .WillRepeatedly(Return(false)); 568 .WillRepeatedly(Return(false));
506 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)).Times(1); 569 EXPECT_CALL(*signature_util_, CheckSignature(a_tmp, _)).Times(1);
507 570
508 download_service_->CheckClientDownload( 571 download_service_->CheckClientDownload(
509 info, 572 &item,
510 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 573 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
511 base::Unretained(this))); 574 base::Unretained(this)));
512 msg_loop_.Run(); 575 msg_loop_.Run();
513 #if defined(OS_WIN) 576 #if defined(OS_WIN)
514 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 577 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
515 #else 578 #else
516 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 579 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
517 #endif 580 #endif
518 } 581 }
519 582
520 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadZip) { 583 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadZip) {
521 ClientDownloadResponse response; 584 ClientDownloadResponse response;
522 response.set_verdict(ClientDownloadResponse::SAFE); 585 response.set_verdict(ClientDownloadResponse::SAFE);
523 net::FakeURLFetcherFactory factory(NULL); 586 net::FakeURLFetcherFactory factory(NULL);
524 // Empty response means SAFE. 587 // Empty response means SAFE.
525 factory.SetFakeResponse( 588 factory.SetFakeResponse(
526 DownloadProtectionService::GetDownloadRequestUrl(), 589 DownloadProtectionService::GetDownloadRequestUrl(),
527 response.SerializeAsString(), 590 response.SerializeAsString(),
528 true); 591 true);
529 592
530 base::ScopedTempDir download_dir; 593 base::ScopedTempDir download_dir;
531 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); 594 ASSERT_TRUE(download_dir.CreateUniqueTempDir());
532 595
533 DownloadProtectionService::DownloadInfo info; 596 base::FilePath a_tmp(download_dir.path().Append(FILE_PATH_LITERAL("a.tmp")));
534 info.local_file = download_dir.path().Append(FILE_PATH_LITERAL("a.tmp")); 597 base::FilePath a_zip(FILE_PATH_LITERAL("a.zip"));
535 info.target_file = base::FilePath(FILE_PATH_LITERAL("a.zip")); 598 std::vector<GURL> url_chain;
536 info.download_url_chain.push_back(GURL("http://www.evil.com/a.zip")); 599 url_chain.push_back(GURL("http://www.evil.com/a.zip"));
537 info.referrer_url = GURL("http://www.google.com/"); 600 GURL referrer("http://www.google.com/");
601 std::string hash = "hash";
602
603 content::MockDownloadItem item;
604 EXPECT_CALL(item, AddObserver(_)).Times(3);
605 EXPECT_CALL(item, RemoveObserver(_)).Times(3);
606 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
607 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_zip));
608 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
609 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
610 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
611 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100));
612 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true));
613 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(""));
538 614
539 // Write out a zip archive to the temporary file. In this case, it 615 // Write out a zip archive to the temporary file. In this case, it
540 // only contains a text file. 616 // only contains a text file.
541 base::ScopedTempDir zip_source_dir; 617 base::ScopedTempDir zip_source_dir;
542 ASSERT_TRUE(zip_source_dir.CreateUniqueTempDir()); 618 ASSERT_TRUE(zip_source_dir.CreateUniqueTempDir());
543 std::string file_contents = "dummy file"; 619 std::string file_contents = "dummy file";
544 ASSERT_EQ(static_cast<int>(file_contents.size()), file_util::WriteFile( 620 ASSERT_EQ(static_cast<int>(file_contents.size()), file_util::WriteFile(
545 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.txt")), 621 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.txt")),
546 file_contents.data(), file_contents.size())); 622 file_contents.data(), file_contents.size()));
547 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), info.local_file, false)); 623 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), a_tmp, false));
548 624
549 download_service_->CheckClientDownload( 625 download_service_->CheckClientDownload(
550 info, 626 &item,
551 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 627 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
552 base::Unretained(this))); 628 base::Unretained(this)));
553 msg_loop_.Run(); 629 msg_loop_.Run();
554 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 630 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
555 Mock::VerifyAndClearExpectations(sb_service_); 631 Mock::VerifyAndClearExpectations(sb_service_);
556 Mock::VerifyAndClearExpectations(signature_util_); 632 Mock::VerifyAndClearExpectations(signature_util_);
557 633
558 // Now check with an executable in the zip file as well. 634 // Now check with an executable in the zip file as well.
559 ASSERT_EQ(static_cast<int>(file_contents.size()), file_util::WriteFile( 635 ASSERT_EQ(static_cast<int>(file_contents.size()), file_util::WriteFile(
560 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.exe")), 636 zip_source_dir.path().Append(FILE_PATH_LITERAL("file.exe")),
561 file_contents.data(), file_contents.size())); 637 file_contents.data(), file_contents.size()));
562 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), info.local_file, false)); 638 ASSERT_TRUE(zip::Zip(zip_source_dir.path(), a_tmp, false));
563 639
564 EXPECT_CALL(*sb_service_->mock_database_manager(), 640 EXPECT_CALL(*sb_service_->mock_database_manager(),
565 MatchDownloadWhitelistUrl(_)) 641 MatchDownloadWhitelistUrl(_))
566 .WillRepeatedly(Return(false)); 642 .WillRepeatedly(Return(false));
567 643
568 download_service_->CheckClientDownload( 644 download_service_->CheckClientDownload(
569 info, 645 &item,
570 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 646 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
571 base::Unretained(this))); 647 base::Unretained(this)));
572 msg_loop_.Run(); 648 msg_loop_.Run();
573 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 649 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
574 Mock::VerifyAndClearExpectations(signature_util_); 650 Mock::VerifyAndClearExpectations(signature_util_);
575 651
576 // If the response is dangerous the result should also be marked as 652 // If the response is dangerous the result should also be marked as
577 // dangerous. 653 // dangerous.
578 response.set_verdict(ClientDownloadResponse::DANGEROUS); 654 response.set_verdict(ClientDownloadResponse::DANGEROUS);
579 factory.SetFakeResponse( 655 factory.SetFakeResponse(
580 DownloadProtectionService::GetDownloadRequestUrl(), 656 DownloadProtectionService::GetDownloadRequestUrl(),
581 response.SerializeAsString(), 657 response.SerializeAsString(),
582 true); 658 true);
583 659
584 download_service_->CheckClientDownload( 660 download_service_->CheckClientDownload(
585 info, 661 &item,
586 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 662 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
587 base::Unretained(this))); 663 base::Unretained(this)));
588 msg_loop_.Run(); 664 msg_loop_.Run();
589 #if defined(OS_WIN) 665 #if defined(OS_WIN)
590 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 666 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
591 #else 667 #else
592 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 668 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
593 #endif 669 #endif
594 Mock::VerifyAndClearExpectations(signature_util_); 670 Mock::VerifyAndClearExpectations(signature_util_);
595 } 671 }
596 672
597 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadCorruptZip) { 673 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadCorruptZip) {
598 base::ScopedTempDir download_dir; 674 base::ScopedTempDir download_dir;
599 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); 675 ASSERT_TRUE(download_dir.CreateUniqueTempDir());
600 676
601 DownloadProtectionService::DownloadInfo info; 677 base::FilePath a_tmp(download_dir.path().Append(FILE_PATH_LITERAL("a.tmp")));
602 info.local_file = download_dir.path().Append(FILE_PATH_LITERAL("a.tmp")); 678 base::FilePath a_zip(FILE_PATH_LITERAL("a.zip"));
603 info.target_file = base::FilePath(FILE_PATH_LITERAL("a.zip")); 679 std::vector<GURL> url_chain;
604 info.download_url_chain.push_back(GURL("http://www.evil.com/a.zip")); 680 url_chain.push_back(GURL("http://www.evil.com/a.zip"));
605 info.referrer_url = GURL("http://www.google.com/"); 681 GURL referrer("http://www.google.com/");
682 std::string hash = "hash";
683
684 content::MockDownloadItem item;
685 EXPECT_CALL(item, AddObserver(_)).Times(1);
686 EXPECT_CALL(item, RemoveObserver(_)).Times(1);
687 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
688 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_zip));
689 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
690 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
691 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
692 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100));
693 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true));
694 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(""));
606 695
607 std::string file_contents = "corrupt zip file"; 696 std::string file_contents = "corrupt zip file";
608 ASSERT_EQ(static_cast<int>(file_contents.size()), file_util::WriteFile( 697 ASSERT_EQ(static_cast<int>(file_contents.size()), file_util::WriteFile(
609 download_dir.path().Append(FILE_PATH_LITERAL("a.tmp")), 698 a_tmp, file_contents.data(), file_contents.size()));
610 file_contents.data(), file_contents.size()));
611 699
612 download_service_->CheckClientDownload( 700 download_service_->CheckClientDownload(
613 info, 701 &item,
614 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 702 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
615 base::Unretained(this))); 703 base::Unretained(this)));
616 msg_loop_.Run(); 704 msg_loop_.Run();
617 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 705 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
618 Mock::VerifyAndClearExpectations(sb_service_); 706 Mock::VerifyAndClearExpectations(sb_service_);
619 Mock::VerifyAndClearExpectations(signature_util_); 707 Mock::VerifyAndClearExpectations(signature_util_);
620 } 708 }
621 709
622 TEST_F(DownloadProtectionServiceTest, CheckClientCrxDownloadSuccess) { 710 TEST_F(DownloadProtectionServiceTest, CheckClientCrxDownloadSuccess) {
623 ClientDownloadResponse response; 711 ClientDownloadResponse response;
624 // Even if the server verdict is dangerous we should return SAFE because 712 // Even if the server verdict is dangerous we should return SAFE because
625 // DownloadProtectionService::IsSupportedDownload() will return false 713 // DownloadProtectionService::IsSupportedDownload() will return false
626 // for crx downloads. 714 // for crx downloads.
627 response.set_verdict(ClientDownloadResponse::DANGEROUS); 715 response.set_verdict(ClientDownloadResponse::DANGEROUS);
628 net::FakeURLFetcherFactory factory(NULL); 716 net::FakeURLFetcherFactory factory(NULL);
629 // Empty response means SAFE. 717 // Empty response means SAFE.
630 factory.SetFakeResponse( 718 factory.SetFakeResponse(
631 DownloadProtectionService::GetDownloadRequestUrl(), 719 DownloadProtectionService::GetDownloadRequestUrl(),
632 response.SerializeAsString(), 720 response.SerializeAsString(),
633 true); 721 true);
634 722
635 DownloadProtectionService::DownloadInfo info; 723 base::FilePath a_tmp(FILE_PATH_LITERAL("a.tmp"));
636 info.local_file = base::FilePath(FILE_PATH_LITERAL("a.tmp")); 724 base::FilePath a_crx(FILE_PATH_LITERAL("a.crx"));
637 info.target_file = base::FilePath(FILE_PATH_LITERAL("a.crx")); 725 std::vector<GURL> url_chain;
638 info.download_url_chain.push_back(GURL("http://www.evil.com/a.crx")); 726 url_chain.push_back(GURL("http://www.evil.com/a.crx"));
639 info.referrer_url = GURL("http://www.google.com/"); 727 GURL referrer("http://www.google.com/");
728 std::string hash = "hash";
729
730 content::MockDownloadItem item;
731 EXPECT_CALL(item, AddObserver(_)).Times(1);
732 EXPECT_CALL(item, RemoveObserver(_)).Times(1);
733 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
734 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_crx));
735 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
736 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
737 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
738 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100));
739 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true));
740 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(""));
640 741
641 EXPECT_CALL(*sb_service_->mock_database_manager(), 742 EXPECT_CALL(*sb_service_->mock_database_manager(),
642 MatchDownloadWhitelistUrl(_)) 743 MatchDownloadWhitelistUrl(_))
643 .WillRepeatedly(Return(false)); 744 .WillRepeatedly(Return(false));
644 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)).Times(1); 745 EXPECT_CALL(*signature_util_, CheckSignature(a_tmp, _)).Times(1);
645 746
646 EXPECT_FALSE(download_service_->IsSupportedDownload(info)); 747 EXPECT_FALSE(download_service_->IsSupportedDownload(item, a_crx));
647 download_service_->CheckClientDownload( 748 download_service_->CheckClientDownload(
648 info, 749 &item,
649 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 750 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
650 base::Unretained(this))); 751 base::Unretained(this)));
651 msg_loop_.Run(); 752 msg_loop_.Run();
652 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 753 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
653 } 754 }
654 755
655 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadValidateRequest) { 756 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadValidateRequest) {
656 net::TestURLFetcherFactory factory; 757 net::TestURLFetcherFactory factory;
657 758
658 DownloadProtectionService::DownloadInfo info; 759 base::FilePath tmp_path(FILE_PATH_LITERAL("bla.tmp"));
659 info.local_file = base::FilePath(FILE_PATH_LITERAL("bla.tmp")); 760 base::FilePath final_path(FILE_PATH_LITERAL("bla.exe"));
660 info.target_file = base::FilePath(FILE_PATH_LITERAL("bla.exe")); 761 std::vector<GURL> url_chain;
661 info.download_url_chain.push_back(GURL("http://www.google.com/")); 762 url_chain.push_back(GURL("http://www.google.com/"));
662 info.download_url_chain.push_back(GURL("http://www.google.com/bla.exe")); 763 url_chain.push_back(GURL("http://www.google.com/bla.exe"));
663 info.referrer_url = GURL("http://www.google.com/"); 764 GURL referrer("http://www.google.com/");
664 info.sha256_hash = "hash"; 765 std::string hash = "hash";
665 info.total_bytes = 100; 766 std::string remote_address = "10.11.12.13";
666 info.user_initiated = true; 767
667 info.remote_address = "10.11.12.13"; 768 content::MockDownloadItem item;
769 EXPECT_CALL(item, AddObserver(_)).Times(1);
770 EXPECT_CALL(item, RemoveObserver(_)).Times(1);
771 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path));
772 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path));
773 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
774 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
775 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
776 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100));
777 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true));
778 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(remote_address));
668 779
669 EXPECT_CALL(*sb_service_->mock_database_manager(), 780 EXPECT_CALL(*sb_service_->mock_database_manager(),
670 MatchDownloadWhitelistUrl(_)) 781 MatchDownloadWhitelistUrl(_))
671 .WillRepeatedly(Return(false)); 782 .WillRepeatedly(Return(false));
672 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)) 783 EXPECT_CALL(*signature_util_, CheckSignature(tmp_path, _))
673 .WillOnce(SetCertificateContents("dummy cert data")); 784 .WillOnce(SetCertificateContents("dummy cert data"));
674 download_service_->CheckClientDownload( 785 download_service_->CheckClientDownload(
675 info, 786 &item,
676 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 787 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
677 base::Unretained(this))); 788 base::Unretained(this)));
678 789
679 #if !defined(OS_WIN) 790 #if !defined(OS_WIN)
680 // SendRequest is not called. Wait for FinishRequest to call our callback. 791 // SendRequest is not called. Wait for FinishRequest to call our callback.
681 msg_loop_.Run(); 792 msg_loop_.Run();
682 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 793 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
683 EXPECT_EQ(NULL, fetcher); 794 EXPECT_EQ(NULL, fetcher);
684 #else 795 #else
685 // Run the message loop(s) until SendRequest is called. 796 // Run the message loop(s) until SendRequest is called.
686 FlushThreadMessageLoops(); 797 FlushThreadMessageLoops();
687 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 798 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
688 ASSERT_TRUE(fetcher); 799 ASSERT_TRUE(fetcher);
689 ClientDownloadRequest request; 800 ClientDownloadRequest request;
690 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data())); 801 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data()));
691 EXPECT_EQ("http://www.google.com/bla.exe", request.url()); 802 EXPECT_EQ("http://www.google.com/bla.exe", request.url());
692 EXPECT_EQ(info.sha256_hash, request.digests().sha256()); 803 EXPECT_EQ(hash, request.digests().sha256());
693 EXPECT_EQ(info.total_bytes, request.length()); 804 EXPECT_EQ(item.GetReceivedBytes(), request.length());
694 EXPECT_EQ(info.user_initiated, request.user_initiated()); 805 EXPECT_EQ(item.HasUserGesture(), request.user_initiated());
695 EXPECT_TRUE(RequestContainsServerIp(request, info.remote_address)); 806 EXPECT_TRUE(RequestContainsServerIp(request, remote_address));
696 EXPECT_EQ(2, request.resources_size()); 807 EXPECT_EQ(2, request.resources_size());
697 EXPECT_TRUE(RequestContainsResource(request, 808 EXPECT_TRUE(RequestContainsResource(request,
698 ClientDownloadRequest::DOWNLOAD_REDIRECT, 809 ClientDownloadRequest::DOWNLOAD_REDIRECT,
699 "http://www.google.com/", "")); 810 "http://www.google.com/", ""));
700 EXPECT_TRUE(RequestContainsResource(request, 811 EXPECT_TRUE(RequestContainsResource(request,
701 ClientDownloadRequest::DOWNLOAD_URL, 812 ClientDownloadRequest::DOWNLOAD_URL,
702 "http://www.google.com/bla.exe", 813 "http://www.google.com/bla.exe",
703 info.referrer_url.spec())); 814 referrer.spec()));
704 EXPECT_TRUE(request.has_signature()); 815 EXPECT_TRUE(request.has_signature());
705 ASSERT_EQ(1, request.signature().certificate_chain_size()); 816 ASSERT_EQ(1, request.signature().certificate_chain_size());
706 const ClientDownloadRequest_CertificateChain& chain = 817 const ClientDownloadRequest_CertificateChain& chain =
707 request.signature().certificate_chain(0); 818 request.signature().certificate_chain(0);
708 ASSERT_EQ(1, chain.element_size()); 819 ASSERT_EQ(1, chain.element_size());
709 EXPECT_EQ("dummy cert data", chain.element(0).certificate()); 820 EXPECT_EQ("dummy cert data", chain.element(0).certificate());
710 821
711 // Simulate the request finishing. 822 // Simulate the request finishing.
712 MessageLoop::current()->PostTask( 823 MessageLoop::current()->PostTask(
713 FROM_HERE, 824 FROM_HERE,
714 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, 825 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete,
715 base::Unretained(this), fetcher)); 826 base::Unretained(this), fetcher));
716 msg_loop_.Run(); 827 msg_loop_.Run();
717 #endif 828 #endif
718 } 829 }
719 830
720 // Similar to above, but with an unsigned binary. 831 // Similar to above, but with an unsigned binary.
721 TEST_F(DownloadProtectionServiceTest, 832 TEST_F(DownloadProtectionServiceTest,
722 CheckClientDownloadValidateRequestNoSignature) { 833 CheckClientDownloadValidateRequestNoSignature) {
723 net::TestURLFetcherFactory factory; 834 net::TestURLFetcherFactory factory;
724 835
725 DownloadProtectionService::DownloadInfo info; 836 base::FilePath tmp_path(FILE_PATH_LITERAL("bla.tmp"));
726 info.local_file = base::FilePath(FILE_PATH_LITERAL("bla.tmp")); 837 base::FilePath final_path(FILE_PATH_LITERAL("bla.exe"));
727 info.target_file = base::FilePath(FILE_PATH_LITERAL("bla.exe")); 838 std::vector<GURL> url_chain;
728 info.download_url_chain.push_back(GURL("http://www.google.com/")); 839 url_chain.push_back(GURL("http://www.google.com/"));
729 info.download_url_chain.push_back(GURL("ftp://www.google.com/bla.exe")); 840 url_chain.push_back(GURL("ftp://www.google.com/bla.exe"));
730 info.referrer_url = GURL("http://www.google.com/"); 841 GURL referrer("http://www.google.com/");
731 info.sha256_hash = "hash"; 842 std::string hash = "hash";
732 info.total_bytes = 100; 843 std::string remote_address = "10.11.12.13";
733 info.user_initiated = false; 844
845 content::MockDownloadItem item;
846 EXPECT_CALL(item, AddObserver(_)).Times(1);
847 EXPECT_CALL(item, RemoveObserver(_)).Times(1);
848 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path));
849 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path));
850 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
851 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
852 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
853 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100));
854 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true));
855 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(remote_address));
734 856
735 EXPECT_CALL(*sb_service_->mock_database_manager(), 857 EXPECT_CALL(*sb_service_->mock_database_manager(),
736 MatchDownloadWhitelistUrl(_)) 858 MatchDownloadWhitelistUrl(_))
737 .WillRepeatedly(Return(false)); 859 .WillRepeatedly(Return(false));
738 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)); 860 EXPECT_CALL(*signature_util_, CheckSignature(tmp_path, _));
739 download_service_->CheckClientDownload( 861 download_service_->CheckClientDownload(
740 info, 862 &item,
741 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 863 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
742 base::Unretained(this))); 864 base::Unretained(this)));
743 865
744 #if !defined(OS_WIN) 866 #if !defined(OS_WIN)
745 // SendRequest is not called. Wait for FinishRequest to call our callback. 867 // SendRequest is not called. Wait for FinishRequest to call our callback.
746 msg_loop_.Run(); 868 msg_loop_.Run();
747 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 869 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
748 EXPECT_EQ(NULL, fetcher); 870 EXPECT_EQ(NULL, fetcher);
749 #else 871 #else
750 // Run the message loop(s) until SendRequest is called. 872 // Run the message loop(s) until SendRequest is called.
751 FlushThreadMessageLoops(); 873 FlushThreadMessageLoops();
752 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 874 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
753 ASSERT_TRUE(fetcher); 875 ASSERT_TRUE(fetcher);
754 ClientDownloadRequest request; 876 ClientDownloadRequest request;
755 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data())); 877 EXPECT_TRUE(request.ParseFromString(fetcher->upload_data()));
756 EXPECT_EQ("ftp://www.google.com/bla.exe", request.url()); 878 EXPECT_EQ("ftp://www.google.com/bla.exe", request.url());
757 EXPECT_EQ(info.sha256_hash, request.digests().sha256()); 879 EXPECT_EQ(hash, request.digests().sha256());
758 EXPECT_EQ(info.total_bytes, request.length()); 880 EXPECT_EQ(item.GetReceivedBytes(), request.length());
759 EXPECT_EQ(info.user_initiated, request.user_initiated()); 881 EXPECT_EQ(item.HasUserGesture(), request.user_initiated());
760 EXPECT_EQ(2, request.resources_size()); 882 EXPECT_EQ(2, request.resources_size());
761 EXPECT_TRUE(RequestContainsResource(request, 883 EXPECT_TRUE(RequestContainsResource(request,
762 ClientDownloadRequest::DOWNLOAD_REDIRECT, 884 ClientDownloadRequest::DOWNLOAD_REDIRECT,
763 "http://www.google.com/", "")); 885 "http://www.google.com/", ""));
764 EXPECT_TRUE(RequestContainsResource(request, 886 EXPECT_TRUE(RequestContainsResource(request,
765 ClientDownloadRequest::DOWNLOAD_URL, 887 ClientDownloadRequest::DOWNLOAD_URL,
766 "ftp://www.google.com/bla.exe", 888 "ftp://www.google.com/bla.exe",
767 info.referrer_url.spec())); 889 referrer.spec()));
768 EXPECT_TRUE(request.has_signature()); 890 EXPECT_TRUE(request.has_signature());
769 EXPECT_EQ(0, request.signature().certificate_chain_size()); 891 EXPECT_EQ(0, request.signature().certificate_chain_size());
770 892
771 // Simulate the request finishing. 893 // Simulate the request finishing.
772 MessageLoop::current()->PostTask( 894 MessageLoop::current()->PostTask(
773 FROM_HERE, 895 FROM_HERE,
774 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete, 896 base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete,
775 base::Unretained(this), fetcher)); 897 base::Unretained(this), fetcher));
776 msg_loop_.Run(); 898 msg_loop_.Run();
777 #endif 899 #endif
778 } 900 }
779 901
780 TEST_F(DownloadProtectionServiceTest, TestCheckDownloadUrl) { 902 TEST_F(DownloadProtectionServiceTest, TestCheckDownloadUrl) {
781 DownloadProtectionService::DownloadInfo info; 903 std::vector<GURL> url_chain;
782 info.download_url_chain.push_back(GURL("http://www.google.com/")); 904 url_chain.push_back(GURL("http://www.google.com/"));
783 info.download_url_chain.push_back(GURL("http://www.google.com/bla.exe")); 905 url_chain.push_back(GURL("http://www.google.com/bla.exe"));
784 info.referrer_url = GURL("http://www.google.com/"); 906 GURL referrer("http://www.google.com/");
907 std::string hash = "hash";
908
909 content::MockDownloadItem item;
910 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
911 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
912 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
785 913
786 // CheckDownloadURL returns immediately which means the client object callback 914 // CheckDownloadURL returns immediately which means the client object callback
787 // will never be called. Nevertheless the callback provided to 915 // will never be called. Nevertheless the callback provided to
788 // CheckClientDownload must still be called. 916 // CheckClientDownload must still be called.
789 EXPECT_CALL(*sb_service_->mock_database_manager(), 917 EXPECT_CALL(*sb_service_->mock_database_manager(),
790 CheckDownloadUrl(ContainerEq(info.download_url_chain), 918 CheckDownloadUrl(ContainerEq(url_chain), NotNull()))
791 NotNull()))
792 .WillOnce(Return(true)); 919 .WillOnce(Return(true));
793 download_service_->CheckDownloadUrl( 920 download_service_->CheckDownloadUrl(
794 info, 921 item,
795 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 922 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
796 base::Unretained(this))); 923 base::Unretained(this)));
797 msg_loop_.Run(); 924 msg_loop_.Run();
798 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 925 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
799 Mock::VerifyAndClearExpectations(sb_service_); 926 Mock::VerifyAndClearExpectations(sb_service_);
800 927
801 EXPECT_CALL(*sb_service_->mock_database_manager(), 928 EXPECT_CALL(*sb_service_->mock_database_manager(),
802 CheckDownloadUrl(ContainerEq(info.download_url_chain), 929 CheckDownloadUrl(ContainerEq(url_chain), NotNull()))
803 NotNull()))
804 .WillOnce(DoAll(CheckDownloadUrlDone(SB_THREAT_TYPE_SAFE), 930 .WillOnce(DoAll(CheckDownloadUrlDone(SB_THREAT_TYPE_SAFE),
805 Return(false))); 931 Return(false)));
806 download_service_->CheckDownloadUrl( 932 download_service_->CheckDownloadUrl(
807 info, 933 item,
808 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 934 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
809 base::Unretained(this))); 935 base::Unretained(this)));
810 msg_loop_.Run(); 936 msg_loop_.Run();
811 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 937 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
812 Mock::VerifyAndClearExpectations(sb_service_); 938 Mock::VerifyAndClearExpectations(sb_service_);
813 939
814 EXPECT_CALL(*sb_service_->mock_database_manager(), 940 EXPECT_CALL(*sb_service_->mock_database_manager(),
815 CheckDownloadUrl(ContainerEq(info.download_url_chain), 941 CheckDownloadUrl(ContainerEq(url_chain), NotNull()))
816 NotNull()))
817 .WillOnce(DoAll( 942 .WillOnce(DoAll(
818 CheckDownloadUrlDone(SB_THREAT_TYPE_URL_MALWARE), 943 CheckDownloadUrlDone(SB_THREAT_TYPE_URL_MALWARE),
819 Return(false))); 944 Return(false)));
820 download_service_->CheckDownloadUrl( 945 download_service_->CheckDownloadUrl(
821 info, 946 item,
822 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 947 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
823 base::Unretained(this))); 948 base::Unretained(this)));
824 msg_loop_.Run(); 949 msg_loop_.Run();
825 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 950 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
826 Mock::VerifyAndClearExpectations(sb_service_); 951 Mock::VerifyAndClearExpectations(sb_service_);
827 952
828 EXPECT_CALL(*sb_service_->mock_database_manager(), 953 EXPECT_CALL(*sb_service_->mock_database_manager(),
829 CheckDownloadUrl(ContainerEq(info.download_url_chain), 954 CheckDownloadUrl(ContainerEq(url_chain),
830 NotNull())) 955 NotNull()))
831 .WillOnce(DoAll( 956 .WillOnce(DoAll(
832 CheckDownloadUrlDone(SB_THREAT_TYPE_BINARY_MALWARE_URL), 957 CheckDownloadUrlDone(SB_THREAT_TYPE_BINARY_MALWARE_URL),
833 Return(false))); 958 Return(false)));
834 download_service_->CheckDownloadUrl( 959 download_service_->CheckDownloadUrl(
835 info, 960 item,
836 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 961 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
837 base::Unretained(this))); 962 base::Unretained(this)));
838 msg_loop_.Run(); 963 msg_loop_.Run();
839 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); 964 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS));
840 } 965 }
841 966
842 TEST_F(DownloadProtectionServiceTest, TestDownloadRequestTimeout) { 967 TEST_F(DownloadProtectionServiceTest, TestDownloadRequestTimeout) {
843 net::TestURLFetcherFactory factory; 968 net::TestURLFetcherFactory factory;
844 969
845 DownloadProtectionService::DownloadInfo info; 970 std::vector<GURL> url_chain;
846 info.download_url_chain.push_back(GURL("http://www.evil.com/bla.exe")); 971 url_chain.push_back(GURL("http://www.evil.com/bla.exe"));
847 info.referrer_url = GURL("http://www.google.com/"); 972 GURL referrer("http://www.google.com/");
848 info.local_file = base::FilePath(FILE_PATH_LITERAL("a.tmp")); 973 base::FilePath tmp_path(FILE_PATH_LITERAL("a.tmp"));
849 info.target_file = base::FilePath(FILE_PATH_LITERAL("a.exe")); 974 base::FilePath final_path(FILE_PATH_LITERAL("a.exe"));
975 std::string hash = "hash";
976
977 content::MockDownloadItem item;
978 EXPECT_CALL(item, AddObserver(_)).Times(1);
979 EXPECT_CALL(item, RemoveObserver(_)).Times(1);
980 EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path));
981 EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path));
982 EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
983 EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
984 EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
985 EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100));
986 EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true));
987 EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(""));
850 988
851 EXPECT_CALL(*sb_service_->mock_database_manager(), 989 EXPECT_CALL(*sb_service_->mock_database_manager(),
852 MatchDownloadWhitelistUrl(_)) 990 MatchDownloadWhitelistUrl(_))
853 .WillRepeatedly(Return(false)); 991 .WillRepeatedly(Return(false));
854 EXPECT_CALL(*signature_util_, CheckSignature(info.local_file, _)); 992 EXPECT_CALL(*signature_util_, CheckSignature(tmp_path, _));
855 993
856 download_service_->download_request_timeout_ms_ = 10; 994 download_service_->download_request_timeout_ms_ = 10;
857 download_service_->CheckClientDownload( 995 download_service_->CheckClientDownload(
858 info, 996 &item,
859 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 997 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
860 base::Unretained(this))); 998 base::Unretained(this)));
861 999
862 // The request should time out because the HTTP request hasn't returned 1000 // The request should time out because the HTTP request hasn't returned
863 // anything yet. 1001 // anything yet.
864 msg_loop_.Run(); 1002 msg_loop_.Run();
865 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); 1003 EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
866 } 1004 }
867 1005
868 TEST_F(DownloadProtectionServiceTest, GetCertificateWhitelistStrings) { 1006 TEST_F(DownloadProtectionServiceTest, GetCertificateWhitelistStrings) {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
936 GetCertificateWhitelistStrings(*cert, *issuer_cert, &whitelist_strings); 1074 GetCertificateWhitelistStrings(*cert, *issuer_cert, &whitelist_strings);
937 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit")); 1075 EXPECT_THAT(whitelist_strings, ElementsAre(cert_base + "/OU=unit"));
938 1076
939 cert = ReadTestCertificate("test_c.pem"); 1077 cert = ReadTestCertificate("test_c.pem");
940 ASSERT_TRUE(cert.get()); 1078 ASSERT_TRUE(cert.get());
941 whitelist_strings.clear(); 1079 whitelist_strings.clear();
942 GetCertificateWhitelistStrings(*cert, *issuer_cert, &whitelist_strings); 1080 GetCertificateWhitelistStrings(*cert, *issuer_cert, &whitelist_strings);
943 EXPECT_THAT(whitelist_strings, ElementsAre()); 1081 EXPECT_THAT(whitelist_strings, ElementsAre());
944 } 1082 }
945 } // namespace safe_browsing 1083 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/download_protection_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698