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

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

Powered by Google App Engine
This is Rietveld 408576698