| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // This test creates a safebrowsing service using test safebrowsing database | 5 // This test creates a safebrowsing service using test safebrowsing database |
| 6 // and a test protocol manager. It is used to test logics in safebrowsing | 6 // and a test protocol manager. It is used to test logics in safebrowsing |
| 7 // service. | 7 // service. |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/metrics/field_trial.h" | 16 #include "base/metrics/field_trial.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/prefs/pref_service.h" | 18 #include "base/prefs/pref_service.h" |
| 19 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/test/thread_test_helper.h" | 22 #include "base/test/thread_test_helper.h" |
| 22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 23 #include "chrome/browser/bookmarks/startup_task_runner_service_factory.h" | 24 #include "chrome/browser/bookmarks/startup_task_runner_service_factory.h" |
| 24 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
| 25 #include "chrome/browser/chrome_notification_types.h" | 26 #include "chrome/browser/chrome_notification_types.h" |
| 26 #include "chrome/browser/prerender/prerender_manager.h" | 27 #include "chrome/browser/prerender/prerender_manager.h" |
| 27 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/profiles/profile_manager.h" | 29 #include "chrome/browser/profiles/profile_manager.h" |
| 29 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 30 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
| 30 #include "chrome/browser/safe_browsing/database_manager.h" | 31 #include "chrome/browser/safe_browsing/database_manager.h" |
| 31 #include "chrome/browser/safe_browsing/local_database_manager.h" | 32 #include "chrome/browser/safe_browsing/local_database_manager.h" |
| 32 #include "chrome/browser/safe_browsing/metadata.pb.h" | 33 #include "chrome/browser/safe_browsing/metadata.pb.h" |
| 33 #include "chrome/browser/safe_browsing/protocol_manager.h" | 34 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 34 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 35 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
| 35 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 36 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 36 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 37 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
| 37 #include "chrome/browser/safe_browsing/ui_manager.h" | 38 #include "chrome/browser/safe_browsing/ui_manager.h" |
| 38 #include "chrome/browser/ui/browser.h" | 39 #include "chrome/browser/ui/browser.h" |
| 40 #include "chrome/browser/ui/browser_navigator_params.h" |
| 39 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 41 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 40 #include "chrome/common/chrome_paths.h" | 42 #include "chrome/common/chrome_paths.h" |
| 41 #include "chrome/common/chrome_switches.h" | 43 #include "chrome/common/chrome_switches.h" |
| 42 #include "chrome/common/pref_names.h" | 44 #include "chrome/common/pref_names.h" |
| 43 #include "chrome/test/base/in_process_browser_test.h" | 45 #include "chrome/test/base/in_process_browser_test.h" |
| 44 #include "chrome/test/base/ui_test_utils.h" | 46 #include "chrome/test/base/ui_test_utils.h" |
| 45 #include "components/bookmarks/browser/startup_task_runner_service.h" | 47 #include "components/bookmarks/browser/startup_task_runner_service.h" |
| 48 #include "content/public/browser/render_frame_host.h" |
| 46 #include "content/public/browser/web_contents.h" | 49 #include "content/public/browser/web_contents.h" |
| 47 #include "net/cookies/cookie_store.h" | 50 #include "net/cookies/cookie_store.h" |
| 48 #include "net/cookies/cookie_util.h" | 51 #include "net/cookies/cookie_util.h" |
| 49 #include "net/test/embedded_test_server/embedded_test_server.h" | 52 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 50 #include "net/test/embedded_test_server/http_request.h" | 53 #include "net/test/embedded_test_server/http_request.h" |
| 51 #include "net/test/embedded_test_server/http_response.h" | 54 #include "net/test/embedded_test_server/http_response.h" |
| 52 #include "sql/connection.h" | 55 #include "sql/connection.h" |
| 53 #include "sql/statement.h" | 56 #include "sql/statement.h" |
| 54 #include "testing/gmock/include/gmock/gmock.h" | 57 #include "testing/gmock/include/gmock/gmock.h" |
| 55 #include "url/gurl.h" | 58 #include "url/gurl.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 67 using content::InterstitialPage; | 70 using content::InterstitialPage; |
| 68 using content::WebContents; | 71 using content::WebContents; |
| 69 using ::testing::_; | 72 using ::testing::_; |
| 70 using ::testing::Mock; | 73 using ::testing::Mock; |
| 71 using ::testing::StrictMock; | 74 using ::testing::StrictMock; |
| 72 | 75 |
| 73 namespace safe_browsing { | 76 namespace safe_browsing { |
| 74 | 77 |
| 75 namespace { | 78 namespace { |
| 76 | 79 |
| 80 const char kEmptyPage[] = "/empty.html"; |
| 81 const char kMalwareFile[] = "/downloads/dangerous/dangerous.exe"; |
| 82 const char kMalwarePage[] = "/safe_browsing/malware.html"; |
| 83 const char kMalwareDelayedLoadsPage[] = |
| 84 "/safe_browsing/malware_delayed_loads.html"; |
| 85 const char kMalwareIFrame[] = "/safe_browsing/malware_iframe.html"; |
| 86 const char kMalwareImg[] = "/safe_browsing/malware_image.png"; |
| 87 const char kNeverCompletesPath[] = "/never_completes"; |
| 88 |
| 89 class NeverCompletingHttpResponse : public net::test_server::HttpResponse { |
| 90 public: |
| 91 ~NeverCompletingHttpResponse() override {} |
| 92 |
| 93 void SendResponse( |
| 94 const net::test_server::SendBytesCallback& send, |
| 95 const net::test_server::SendCompleteCallback& done) override { |
| 96 // Do nothing. |done| is never called. |
| 97 } |
| 98 }; |
| 99 |
| 100 scoped_ptr<net::test_server::HttpResponse> HandleNeverCompletingRequests( |
| 101 const net::test_server::HttpRequest& request) { |
| 102 if (!base::StartsWith(request.relative_url, kNeverCompletesPath, |
| 103 base::CompareCase::SENSITIVE)) |
| 104 return scoped_ptr<net::test_server::HttpResponse>(); |
| 105 return make_scoped_ptr(new NeverCompletingHttpResponse()); |
| 106 } |
| 107 |
| 77 void InvokeFullHashCallback( | 108 void InvokeFullHashCallback( |
| 78 SafeBrowsingProtocolManager::FullHashCallback callback, | 109 SafeBrowsingProtocolManager::FullHashCallback callback, |
| 79 const std::vector<SBFullHashResult>& result) { | 110 const std::vector<SBFullHashResult>& result) { |
| 80 callback.Run(result, base::TimeDelta::FromMinutes(45)); | 111 callback.Run(result, base::TimeDelta::FromMinutes(45)); |
| 81 } | 112 } |
| 82 | 113 |
| 114 class FakeSafeBrowsingUIManager : public SafeBrowsingUIManager { |
| 115 public: |
| 116 explicit FakeSafeBrowsingUIManager(SafeBrowsingService* service) |
| 117 : SafeBrowsingUIManager(service) {} |
| 118 |
| 119 void MaybeReportSafeBrowsingHit( |
| 120 const safe_browsing::HitReport& hit_report) override { |
| 121 EXPECT_FALSE(got_hit_report_); |
| 122 got_hit_report_ = true; |
| 123 hit_report_ = hit_report; |
| 124 SafeBrowsingUIManager::MaybeReportSafeBrowsingHit(hit_report); |
| 125 } |
| 126 |
| 127 bool got_hit_report_ = false; |
| 128 safe_browsing::HitReport hit_report_; |
| 129 |
| 130 protected: |
| 131 ~FakeSafeBrowsingUIManager() override {} |
| 132 }; |
| 133 |
| 83 class FakeSafeBrowsingService : public SafeBrowsingService { | 134 class FakeSafeBrowsingService : public SafeBrowsingService { |
| 84 public: | 135 public: |
| 85 explicit FakeSafeBrowsingService(const std::string& url_prefix) | 136 explicit FakeSafeBrowsingService(const std::string& url_prefix) |
| 86 : url_prefix_(url_prefix) {} | 137 : url_prefix_(url_prefix) {} |
| 87 | 138 |
| 88 SafeBrowsingProtocolConfig GetProtocolConfig() const override { | 139 SafeBrowsingProtocolConfig GetProtocolConfig() const override { |
| 89 SafeBrowsingProtocolConfig config; | 140 SafeBrowsingProtocolConfig config; |
| 90 config.url_prefix = url_prefix_; | 141 config.url_prefix = url_prefix_; |
| 91 // Makes sure the auto update is not triggered. The tests will force the | 142 // Makes sure the auto update is not triggered. The tests will force the |
| 92 // update when needed. | 143 // update when needed. |
| 93 config.disable_auto_update = true; | 144 config.disable_auto_update = true; |
| 94 config.client_name = "browser_tests"; | 145 config.client_name = "browser_tests"; |
| 95 return config; | 146 return config; |
| 96 } | 147 } |
| 97 | 148 |
| 149 protected: |
| 150 SafeBrowsingUIManager* CreateUIManager() override { |
| 151 return new FakeSafeBrowsingUIManager(this); |
| 152 } |
| 153 |
| 98 private: | 154 private: |
| 99 ~FakeSafeBrowsingService() override {} | 155 ~FakeSafeBrowsingService() override {} |
| 100 | 156 |
| 101 std::string url_prefix_; | 157 std::string url_prefix_; |
| 102 | 158 |
| 103 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService); | 159 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService); |
| 104 }; | 160 }; |
| 105 | 161 |
| 106 // Factory that creates FakeSafeBrowsingService instances. | 162 // Factory that creates FakeSafeBrowsingService instances. |
| 107 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { | 163 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 | 507 |
| 452 void TearDownOnMainThread() override { | 508 void TearDownOnMainThread() override { |
| 453 g_browser_process->safe_browsing_service()->ui_manager()->RemoveObserver( | 509 g_browser_process->safe_browsing_service()->ui_manager()->RemoveObserver( |
| 454 &observer_); | 510 &observer_); |
| 455 InProcessBrowserTest::TearDownOnMainThread(); | 511 InProcessBrowserTest::TearDownOnMainThread(); |
| 456 } | 512 } |
| 457 | 513 |
| 458 void SetUpInProcessBrowserTestFixture() override { | 514 void SetUpInProcessBrowserTestFixture() override { |
| 459 base::FilePath test_data_dir; | 515 base::FilePath test_data_dir; |
| 460 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | 516 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
| 517 embedded_test_server()->RegisterRequestHandler( |
| 518 base::Bind(&HandleNeverCompletingRequests)); |
| 461 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | 519 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
| 462 ASSERT_TRUE(embedded_test_server()->Start()); | 520 ASSERT_TRUE(embedded_test_server()->Start()); |
| 463 } | 521 } |
| 464 | 522 |
| 465 // This will setup the "url" prefix in database and prepare protocol manager | 523 // This will setup the "url" prefix in database and prepare protocol manager |
| 466 // to respond with |full_hash|, as well as other |full_hash|es previously set | 524 // to respond with |full_hash|, as well as other |full_hash|es previously set |
| 467 // via this call, on GetFullHash requests. | 525 // via this call, on GetFullHash requests. |
| 468 void SetupResponseForUrl(const GURL& url, const SBFullHashResult& full_hash) { | 526 void SetupResponseForUrl(const GURL& url, const SBFullHashResult& full_hash) { |
| 469 std::vector<SBPrefix> prefix_hits; | 527 std::vector<SBPrefix> prefix_hits; |
| 470 prefix_hits.push_back(full_hash.hash.prefix); | 528 prefix_hits.push_back(full_hash.hash.prefix); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 std::vector<SafeBrowsingUIManager::UnsafeResource> resources; | 585 std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
| 528 resources.push_back(resource); | 586 resources.push_back(resource); |
| 529 g_browser_process->safe_browsing_service() | 587 g_browser_process->safe_browsing_service() |
| 530 ->ui_manager() | 588 ->ui_manager() |
| 531 ->OnBlockingPageDone(resources, true); | 589 ->OnBlockingPageDone(resources, true); |
| 532 if (!resource.callback.is_null()) { | 590 if (!resource.callback.is_null()) { |
| 533 WaitForThread(resource.callback_thread); | 591 WaitForThread(resource.callback_thread); |
| 534 } | 592 } |
| 535 } | 593 } |
| 536 | 594 |
| 595 FakeSafeBrowsingUIManager* ui_manager() { |
| 596 return static_cast<FakeSafeBrowsingUIManager*>( |
| 597 g_browser_process->safe_browsing_service()->ui_manager().get()); |
| 598 } |
| 599 bool got_hit_report() { return ui_manager()->got_hit_report_; } |
| 600 const safe_browsing::HitReport& hit_report() { |
| 601 return ui_manager()->hit_report_; |
| 602 } |
| 603 |
| 537 protected: | 604 protected: |
| 538 StrictMock<MockObserver> observer_; | 605 StrictMock<MockObserver> observer_; |
| 539 | 606 |
| 540 // Temporary profile dir for test cases that create a second profile. This is | 607 // Temporary profile dir for test cases that create a second profile. This is |
| 541 // owned by the SafeBrowsingServiceTest object so that it will not get | 608 // owned by the SafeBrowsingServiceTest object so that it will not get |
| 542 // destructed until after the test Browser has been torn down, since the | 609 // destructed until after the test Browser has been torn down, since the |
| 543 // ImportantFileWriter may still be modifying it after the Profile object has | 610 // ImportantFileWriter may still be modifying it after the Profile object has |
| 544 // been destroyed. | 611 // been destroyed. |
| 545 base::ScopedTempDir temp_profile_dir_; | 612 base::ScopedTempDir temp_profile_dir_; |
| 546 | 613 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 proto.set_pattern_type(MalwarePatternType::DISTRIBUTION); | 673 proto.set_pattern_type(MalwarePatternType::DISTRIBUTION); |
| 607 full_hash->metadata = proto.SerializeAsString(); | 674 full_hash->metadata = proto.SerializeAsString(); |
| 608 break; | 675 break; |
| 609 } | 676 } |
| 610 } | 677 } |
| 611 | 678 |
| 612 private: | 679 private: |
| 613 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceMetadataTest); | 680 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceMetadataTest); |
| 614 }; | 681 }; |
| 615 | 682 |
| 616 namespace { | |
| 617 | |
| 618 const char kEmptyPage[] = "/empty.html"; | |
| 619 const char kMalwareFile[] = "/downloads/dangerous/dangerous.exe"; | |
| 620 const char kMalwarePage[] = "/safe_browsing/malware.html"; | |
| 621 const char kMalwareIFrame[] = "/safe_browsing/malware_iframe.html"; | |
| 622 const char kMalwareImg[] = "/safe_browsing/malware_image.png"; | |
| 623 | |
| 624 // This test goes through DownloadResourceHandler. | |
| 625 IN_PROC_BROWSER_TEST_P(SafeBrowsingServiceMetadataTest, MalwareMainFrame) { | 683 IN_PROC_BROWSER_TEST_P(SafeBrowsingServiceMetadataTest, MalwareMainFrame) { |
| 626 GURL url = embedded_test_server()->GetURL(kEmptyPage); | 684 GURL url = embedded_test_server()->GetURL(kEmptyPage); |
| 627 | 685 |
| 628 // After adding the url to safebrowsing database and getfullhash result, | 686 // After adding the url to safebrowsing database and getfullhash result, |
| 629 // we should see the interstitial page. | 687 // we should see the interstitial page. |
| 630 SBFullHashResult malware_full_hash; | 688 SBFullHashResult malware_full_hash; |
| 631 GenUrlFullhashResultWithMetadata(url, &malware_full_hash); | 689 GenUrlFullhashResultWithMetadata(url, &malware_full_hash); |
| 632 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(1); | 690 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(1); |
| 633 SetupResponseForUrl(url, malware_full_hash); | 691 SetupResponseForUrl(url, malware_full_hash); |
| 634 ui_test_utils::NavigateToURL(browser(), url); | 692 ui_test_utils::NavigateToURL(browser(), url); |
| 635 // All types should show the interstitial. | 693 // All types should show the interstitial. |
| 636 EXPECT_TRUE(ShowingInterstitialPage()); | 694 EXPECT_TRUE(ShowingInterstitialPage()); |
| 695 |
| 696 EXPECT_TRUE(got_hit_report()); |
| 697 EXPECT_EQ(url, hit_report().malicious_url); |
| 698 EXPECT_EQ(url, hit_report().page_url); |
| 699 EXPECT_EQ(GURL(), hit_report().referrer_url); |
| 700 EXPECT_FALSE(hit_report().is_subresource); |
| 637 } | 701 } |
| 638 | 702 |
| 639 IN_PROC_BROWSER_TEST_P(SafeBrowsingServiceMetadataTest, MalwareIFrame) { | 703 IN_PROC_BROWSER_TEST_P(SafeBrowsingServiceMetadataTest, MalwareIFrame) { |
| 640 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); | 704 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); |
| 641 GURL iframe_url = embedded_test_server()->GetURL(kMalwareIFrame); | 705 GURL iframe_url = embedded_test_server()->GetURL(kMalwareIFrame); |
| 642 | 706 |
| 643 // Add the iframe url as malware and then load the parent page. | 707 // Add the iframe url as malware and then load the parent page. |
| 644 SBFullHashResult malware_full_hash; | 708 SBFullHashResult malware_full_hash; |
| 645 GenUrlFullhashResultWithMetadata(iframe_url, &malware_full_hash); | 709 GenUrlFullhashResultWithMetadata(iframe_url, &malware_full_hash); |
| 646 EXPECT_CALL(observer_, | 710 EXPECT_CALL(observer_, |
| 647 OnSafeBrowsingHit(IsUnsafeResourceFor(iframe_url))).Times(1); | 711 OnSafeBrowsingHit(IsUnsafeResourceFor(iframe_url))).Times(1); |
| 648 SetupResponseForUrl(iframe_url, malware_full_hash); | 712 SetupResponseForUrl(iframe_url, malware_full_hash); |
| 649 ui_test_utils::NavigateToURL(browser(), main_url); | 713 ui_test_utils::NavigateToURL(browser(), main_url); |
| 650 // All types should show the interstitial. | 714 // All types should show the interstitial. |
| 651 EXPECT_TRUE(ShowingInterstitialPage()); | 715 EXPECT_TRUE(ShowingInterstitialPage()); |
| 716 |
| 717 EXPECT_TRUE(got_hit_report()); |
| 718 EXPECT_EQ(iframe_url, hit_report().malicious_url); |
| 719 EXPECT_EQ(main_url, hit_report().page_url); |
| 720 EXPECT_EQ(GURL(), hit_report().referrer_url); |
| 721 EXPECT_TRUE(hit_report().is_subresource); |
| 652 } | 722 } |
| 653 | 723 |
| 654 IN_PROC_BROWSER_TEST_P(SafeBrowsingServiceMetadataTest, MalwareImg) { | 724 IN_PROC_BROWSER_TEST_P(SafeBrowsingServiceMetadataTest, MalwareImg) { |
| 655 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); | 725 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); |
| 656 GURL img_url = embedded_test_server()->GetURL(kMalwareImg); | 726 GURL img_url = embedded_test_server()->GetURL(kMalwareImg); |
| 657 | 727 |
| 658 // Add the img url as malware and then load the parent page. | 728 // Add the img url as malware and then load the parent page. |
| 659 SBFullHashResult malware_full_hash; | 729 SBFullHashResult malware_full_hash; |
| 660 GenUrlFullhashResultWithMetadata(img_url, &malware_full_hash); | 730 GenUrlFullhashResultWithMetadata(img_url, &malware_full_hash); |
| 661 switch (GetParam()) { | 731 switch (GetParam()) { |
| 662 case METADATA_NONE: // Falls through. | 732 case METADATA_NONE: // Falls through. |
| 663 case METADATA_DISTRIBUTION: | 733 case METADATA_DISTRIBUTION: |
| 664 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(img_url))) | 734 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(img_url))) |
| 665 .Times(1); | 735 .Times(1); |
| 666 break; | 736 break; |
| 667 case METADATA_LANDING: | 737 case METADATA_LANDING: |
| 668 // No interstitial shown, so no notifications expected. | 738 // No interstitial shown, so no notifications expected. |
| 669 break; | 739 break; |
| 670 } | 740 } |
| 671 SetupResponseForUrl(img_url, malware_full_hash); | 741 SetupResponseForUrl(img_url, malware_full_hash); |
| 672 ui_test_utils::NavigateToURL(browser(), main_url); | 742 ui_test_utils::NavigateToURL(browser(), main_url); |
| 673 // Subresource which is tagged as a landing page should not show an | 743 // Subresource which is tagged as a landing page should not show an |
| 674 // interstitial, the other types should. | 744 // interstitial, the other types should. |
| 675 switch (GetParam()) { | 745 switch (GetParam()) { |
| 676 case METADATA_NONE: | 746 case METADATA_NONE: |
| 677 case METADATA_DISTRIBUTION: | 747 case METADATA_DISTRIBUTION: |
| 678 EXPECT_TRUE(ShowingInterstitialPage()); | 748 EXPECT_TRUE(ShowingInterstitialPage()); |
| 749 EXPECT_TRUE(got_hit_report()); |
| 750 EXPECT_EQ(img_url, hit_report().malicious_url); |
| 751 EXPECT_EQ(main_url, hit_report().page_url); |
| 752 EXPECT_EQ(GURL(), hit_report().referrer_url); |
| 753 EXPECT_TRUE(hit_report().is_subresource); |
| 679 break; | 754 break; |
| 680 case METADATA_LANDING: | 755 case METADATA_LANDING: |
| 681 EXPECT_FALSE(ShowingInterstitialPage()); | 756 EXPECT_FALSE(ShowingInterstitialPage()); |
| 757 EXPECT_FALSE(got_hit_report()); |
| 682 break; | 758 break; |
| 683 } | 759 } |
| 684 } | 760 } |
| 685 | 761 |
| 686 INSTANTIATE_TEST_CASE_P(MaybeSetMetadata, | 762 INSTANTIATE_TEST_CASE_P(MaybeSetMetadata, |
| 687 SafeBrowsingServiceMetadataTest, | 763 SafeBrowsingServiceMetadataTest, |
| 688 testing::Values(METADATA_NONE, | 764 testing::Values(METADATA_NONE, |
| 689 METADATA_LANDING, | 765 METADATA_LANDING, |
| 690 METADATA_DISTRIBUTION)); | 766 METADATA_DISTRIBUTION)); |
| 691 | 767 |
| 692 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, UnwantedImgIgnored) { | 768 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, UnwantedImgIgnored) { |
| 693 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); | 769 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); |
| 694 GURL img_url = embedded_test_server()->GetURL(kMalwareImg); | 770 GURL img_url = embedded_test_server()->GetURL(kMalwareImg); |
| 695 | 771 |
| 696 // Add the img url as coming from a site serving UwS and then load the parent | 772 // Add the img url as coming from a site serving UwS and then load the parent |
| 697 // page. | 773 // page. |
| 698 SBFullHashResult uws_full_hash; | 774 SBFullHashResult uws_full_hash; |
| 699 GenUrlFullhashResult(img_url, UNWANTEDURL, &uws_full_hash); | 775 GenUrlFullhashResult(img_url, UNWANTEDURL, &uws_full_hash); |
| 700 SetupResponseForUrl(img_url, uws_full_hash); | 776 SetupResponseForUrl(img_url, uws_full_hash); |
| 701 | 777 |
| 702 ui_test_utils::NavigateToURL(browser(), main_url); | 778 ui_test_utils::NavigateToURL(browser(), main_url); |
| 703 | 779 |
| 704 EXPECT_FALSE(ShowingInterstitialPage()); | 780 EXPECT_FALSE(ShowingInterstitialPage()); |
| 781 EXPECT_FALSE(got_hit_report()); |
| 705 } | 782 } |
| 706 | 783 |
| 707 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, DISABLED_MalwareWithWhitelist) { | 784 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, DISABLED_MalwareWithWhitelist) { |
| 708 GURL url = embedded_test_server()->GetURL(kEmptyPage); | 785 GURL url = embedded_test_server()->GetURL(kEmptyPage); |
| 709 | 786 |
| 710 // After adding the url to safebrowsing database and getfullhash result, | 787 // After adding the url to safebrowsing database and getfullhash result, |
| 711 // we should see the interstitial page. | 788 // we should see the interstitial page. |
| 712 SBFullHashResult malware_full_hash; | 789 SBFullHashResult malware_full_hash; |
| 713 GenUrlFullhashResult(url, MALWARE, &malware_full_hash); | 790 GenUrlFullhashResult(url, MALWARE, &malware_full_hash); |
| 714 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))) | 791 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 757 } set_prefetch_for_test(true); | 834 } set_prefetch_for_test(true); |
| 758 | 835 |
| 759 // Even though we have added this uri to the safebrowsing database and | 836 // Even though we have added this uri to the safebrowsing database and |
| 760 // getfullhash result, we should not see the interstitial page since the | 837 // getfullhash result, we should not see the interstitial page since the |
| 761 // only malware was a prefetch target. | 838 // only malware was a prefetch target. |
| 762 SBFullHashResult malware_full_hash; | 839 SBFullHashResult malware_full_hash; |
| 763 GenUrlFullhashResult(malware_url, MALWARE, &malware_full_hash); | 840 GenUrlFullhashResult(malware_url, MALWARE, &malware_full_hash); |
| 764 SetupResponseForUrl(malware_url, malware_full_hash); | 841 SetupResponseForUrl(malware_url, malware_full_hash); |
| 765 ui_test_utils::NavigateToURL(browser(), url); | 842 ui_test_utils::NavigateToURL(browser(), url); |
| 766 EXPECT_FALSE(ShowingInterstitialPage()); | 843 EXPECT_FALSE(ShowingInterstitialPage()); |
| 844 EXPECT_FALSE(got_hit_report()); |
| 767 Mock::VerifyAndClear(&observer_); | 845 Mock::VerifyAndClear(&observer_); |
| 768 | 846 |
| 769 // However, when we navigate to the malware page, we should still get | 847 // However, when we navigate to the malware page, we should still get |
| 770 // the interstitial. | 848 // the interstitial. |
| 771 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(malware_url))) | 849 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(malware_url))) |
| 772 .Times(1); | 850 .Times(1); |
| 773 ui_test_utils::NavigateToURL(browser(), malware_url); | 851 ui_test_utils::NavigateToURL(browser(), malware_url); |
| 774 EXPECT_TRUE(ShowingInterstitialPage()); | 852 EXPECT_TRUE(ShowingInterstitialPage()); |
| 853 EXPECT_TRUE(got_hit_report()); |
| 775 Mock::VerifyAndClear(&observer_); | 854 Mock::VerifyAndClear(&observer_); |
| 776 } | 855 } |
| 777 | 856 |
| 778 } // namespace | 857 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, MainFrameHitWithReferrer) { |
| 858 GURL first_url = embedded_test_server()->GetURL(kEmptyPage); |
| 859 GURL bad_url = embedded_test_server()->GetURL(kMalwarePage); |
| 860 |
| 861 SBFullHashResult malware_full_hash; |
| 862 GenUrlFullhashResult(bad_url, MALWARE, &malware_full_hash); |
| 863 SetupResponseForUrl(bad_url, malware_full_hash); |
| 864 |
| 865 // Navigate to first, safe page. |
| 866 ui_test_utils::NavigateToURL(browser(), first_url); |
| 867 EXPECT_FALSE(ShowingInterstitialPage()); |
| 868 EXPECT_FALSE(got_hit_report()); |
| 869 Mock::VerifyAndClear(&observer_); |
| 870 |
| 871 // Navigate to malware page, should show interstitial and have first page in |
| 872 // referrer. |
| 873 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url))) |
| 874 .Times(1); |
| 875 |
| 876 chrome::NavigateParams params(browser(), bad_url, ui::PAGE_TRANSITION_LINK); |
| 877 params.referrer.url = first_url; |
| 878 ui_test_utils::NavigateToURL(¶ms); |
| 879 |
| 880 EXPECT_TRUE(ShowingInterstitialPage()); |
| 881 EXPECT_TRUE(got_hit_report()); |
| 882 EXPECT_EQ(bad_url, hit_report().malicious_url); |
| 883 EXPECT_EQ(bad_url, hit_report().page_url); |
| 884 EXPECT_EQ(first_url, hit_report().referrer_url); |
| 885 EXPECT_FALSE(hit_report().is_subresource); |
| 886 } |
| 887 |
| 888 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, |
| 889 SubResourceHitWithMainFrameReferrer) { |
| 890 GURL first_url = embedded_test_server()->GetURL(kEmptyPage); |
| 891 GURL second_url = embedded_test_server()->GetURL(kMalwarePage); |
| 892 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg); |
| 893 |
| 894 SBFullHashResult malware_full_hash; |
| 895 GenUrlFullhashResult(bad_url, MALWARE, &malware_full_hash); |
| 896 SetupResponseForUrl(bad_url, malware_full_hash); |
| 897 |
| 898 // Navigate to first, safe page. |
| 899 ui_test_utils::NavigateToURL(browser(), first_url); |
| 900 EXPECT_FALSE(ShowingInterstitialPage()); |
| 901 EXPECT_FALSE(got_hit_report()); |
| 902 Mock::VerifyAndClear(&observer_); |
| 903 |
| 904 // Navigate to page which has malware subresource, should show interstitial |
| 905 // and have first page in referrer. |
| 906 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url))) |
| 907 .Times(1); |
| 908 |
| 909 chrome::NavigateParams params(browser(), second_url, |
| 910 ui::PAGE_TRANSITION_LINK); |
| 911 params.referrer.url = first_url; |
| 912 ui_test_utils::NavigateToURL(¶ms); |
| 913 |
| 914 EXPECT_TRUE(ShowingInterstitialPage()); |
| 915 EXPECT_TRUE(got_hit_report()); |
| 916 EXPECT_EQ(bad_url, hit_report().malicious_url); |
| 917 EXPECT_EQ(second_url, hit_report().page_url); |
| 918 EXPECT_EQ(first_url, hit_report().referrer_url); |
| 919 EXPECT_TRUE(hit_report().is_subresource); |
| 920 } |
| 921 |
| 922 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, |
| 923 SubResourceHitWithMainFrameRendererInitiatedSlowLoad) { |
| 924 GURL first_url = embedded_test_server()->GetURL(kEmptyPage); |
| 925 GURL second_url = embedded_test_server()->GetURL(kMalwareDelayedLoadsPage); |
| 926 GURL third_url = embedded_test_server()->GetURL(kNeverCompletesPath); |
| 927 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg); |
| 928 |
| 929 SBFullHashResult malware_full_hash; |
| 930 GenUrlFullhashResult(bad_url, MALWARE, &malware_full_hash); |
| 931 SetupResponseForUrl(bad_url, malware_full_hash); |
| 932 |
| 933 // Navigate to first, safe page. |
| 934 ui_test_utils::NavigateToURL(browser(), first_url); |
| 935 EXPECT_FALSE(ShowingInterstitialPage()); |
| 936 EXPECT_FALSE(got_hit_report()); |
| 937 Mock::VerifyAndClear(&observer_); |
| 938 |
| 939 // Navigate to malware page. The malware subresources haven't loaded yet, so |
| 940 // no interstitial should show yet. |
| 941 chrome::NavigateParams params(browser(), second_url, |
| 942 ui::PAGE_TRANSITION_LINK); |
| 943 params.referrer.url = first_url; |
| 944 ui_test_utils::NavigateToURL(¶ms); |
| 945 |
| 946 EXPECT_FALSE(ShowingInterstitialPage()); |
| 947 EXPECT_FALSE(got_hit_report()); |
| 948 Mock::VerifyAndClear(&observer_); |
| 949 |
| 950 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url))) |
| 951 .Times(1); |
| 952 |
| 953 WebContents* contents = |
| 954 browser()->tab_strip_model()->GetActiveWebContents(); |
| 955 content::WindowedNotificationObserver load_stop_observer( |
| 956 content::NOTIFICATION_LOAD_STOP, |
| 957 content::Source<content::NavigationController>( |
| 958 &contents->GetController())); |
| 959 // Run javascript function in the page which starts a timer to load the |
| 960 // malware image, and also starts a renderer-initiated top-level navigation to |
| 961 // a site that does not respond. Should show interstitial and have first page |
| 962 // in referrer. |
| 963 contents->GetMainFrame()->ExecuteJavaScriptForTests( |
| 964 base::ASCIIToUTF16("navigateAndLoadMalwareImage()")); |
| 965 load_stop_observer.Wait(); |
| 966 |
| 967 EXPECT_TRUE(ShowingInterstitialPage()); |
| 968 EXPECT_TRUE(got_hit_report()); |
| 969 // Report URLs should be for the current page, not the pending load. |
| 970 EXPECT_EQ(bad_url, hit_report().malicious_url); |
| 971 EXPECT_EQ(second_url, hit_report().page_url); |
| 972 EXPECT_EQ(first_url, hit_report().referrer_url); |
| 973 EXPECT_TRUE(hit_report().is_subresource); |
| 974 } |
| 975 |
| 976 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, |
| 977 SubResourceHitWithMainFrameBrowserInitiatedSlowLoad) { |
| 978 GURL first_url = embedded_test_server()->GetURL(kEmptyPage); |
| 979 GURL second_url = embedded_test_server()->GetURL(kMalwareDelayedLoadsPage); |
| 980 GURL third_url = embedded_test_server()->GetURL(kNeverCompletesPath); |
| 981 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg); |
| 982 |
| 983 SBFullHashResult malware_full_hash; |
| 984 GenUrlFullhashResult(bad_url, MALWARE, &malware_full_hash); |
| 985 SetupResponseForUrl(bad_url, malware_full_hash); |
| 986 |
| 987 // Navigate to first, safe page. |
| 988 ui_test_utils::NavigateToURL(browser(), first_url); |
| 989 EXPECT_FALSE(ShowingInterstitialPage()); |
| 990 EXPECT_FALSE(got_hit_report()); |
| 991 Mock::VerifyAndClear(&observer_); |
| 992 |
| 993 // Navigate to malware page. The malware subresources haven't loaded yet, so |
| 994 // no interstitial should show yet. |
| 995 chrome::NavigateParams params(browser(), second_url, |
| 996 ui::PAGE_TRANSITION_LINK); |
| 997 params.referrer.url = first_url; |
| 998 ui_test_utils::NavigateToURL(¶ms); |
| 999 |
| 1000 EXPECT_FALSE(ShowingInterstitialPage()); |
| 1001 EXPECT_FALSE(got_hit_report()); |
| 1002 Mock::VerifyAndClear(&observer_); |
| 1003 |
| 1004 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url))) |
| 1005 .Times(1); |
| 1006 |
| 1007 WebContents* contents = |
| 1008 browser()->tab_strip_model()->GetActiveWebContents(); |
| 1009 content::RenderFrameHost* rfh = contents->GetMainFrame(); |
| 1010 content::WindowedNotificationObserver load_stop_observer( |
| 1011 content::NOTIFICATION_LOAD_STOP, |
| 1012 content::Source<content::NavigationController>( |
| 1013 &contents->GetController())); |
| 1014 // Start a browser initiated top-level navigation to a site that does not |
| 1015 // respond. |
| 1016 ui_test_utils::NavigateToURLWithDisposition(browser(), third_url, CURRENT_TAB, |
| 1017 ui_test_utils::BROWSER_TEST_NONE); |
| 1018 |
| 1019 // While the top-level navigation is pending, run javascript |
| 1020 // function in the page which loads the malware image. |
| 1021 rfh->ExecuteJavaScriptForTests(base::ASCIIToUTF16("loadMalwareImage()")); |
| 1022 |
| 1023 // Wait for interstitial to show. |
| 1024 load_stop_observer.Wait(); |
| 1025 |
| 1026 EXPECT_TRUE(ShowingInterstitialPage()); |
| 1027 EXPECT_TRUE(got_hit_report()); |
| 1028 // Report URLs should be for the current page, not the pending load. |
| 1029 EXPECT_EQ(bad_url, hit_report().malicious_url); |
| 1030 EXPECT_EQ(second_url, hit_report().page_url); |
| 1031 EXPECT_EQ(first_url, hit_report().referrer_url); |
| 1032 EXPECT_TRUE(hit_report().is_subresource); |
| 1033 } |
| 1034 |
| 1035 |
| 1036 namespace { |
| 779 | 1037 |
| 780 class TestSBClient : public base::RefCountedThreadSafe<TestSBClient>, | 1038 class TestSBClient : public base::RefCountedThreadSafe<TestSBClient>, |
| 781 public SafeBrowsingDatabaseManager::Client { | 1039 public SafeBrowsingDatabaseManager::Client { |
| 782 public: | 1040 public: |
| 783 TestSBClient() | 1041 TestSBClient() |
| 784 : threat_type_(SB_THREAT_TYPE_SAFE), | 1042 : threat_type_(SB_THREAT_TYPE_SAFE), |
| 785 safe_browsing_service_(g_browser_process->safe_browsing_service()) {} | 1043 safe_browsing_service_(g_browser_process->safe_browsing_service()) {} |
| 786 | 1044 |
| 787 SBThreatType GetThreatType() const { return threat_type_; } | 1045 SBThreatType GetThreatType() const { return threat_type_; } |
| 788 | 1046 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 845 } | 1103 } |
| 846 | 1104 |
| 847 void CheckDone() { base::MessageLoopForUI::current()->QuitWhenIdle(); } | 1105 void CheckDone() { base::MessageLoopForUI::current()->QuitWhenIdle(); } |
| 848 | 1106 |
| 849 SBThreatType threat_type_; | 1107 SBThreatType threat_type_; |
| 850 SafeBrowsingService* safe_browsing_service_; | 1108 SafeBrowsingService* safe_browsing_service_; |
| 851 | 1109 |
| 852 DISALLOW_COPY_AND_ASSIGN(TestSBClient); | 1110 DISALLOW_COPY_AND_ASSIGN(TestSBClient); |
| 853 }; | 1111 }; |
| 854 | 1112 |
| 1113 } // namespace |
| 1114 |
| 855 // These tests use SafeBrowsingService::Client to directly interact with | 1115 // These tests use SafeBrowsingService::Client to directly interact with |
| 856 // SafeBrowsingService. | 1116 // SafeBrowsingService. |
| 857 namespace { | |
| 858 | |
| 859 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, CheckDownloadUrl) { | 1117 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, CheckDownloadUrl) { |
| 860 GURL badbin_url = embedded_test_server()->GetURL(kMalwareFile); | 1118 GURL badbin_url = embedded_test_server()->GetURL(kMalwareFile); |
| 861 std::vector<GURL> badbin_urls(1, badbin_url); | 1119 std::vector<GURL> badbin_urls(1, badbin_url); |
| 862 | 1120 |
| 863 scoped_refptr<TestSBClient> client(new TestSBClient); | 1121 scoped_refptr<TestSBClient> client(new TestSBClient); |
| 864 client->CheckDownloadUrl(badbin_urls); | 1122 client->CheckDownloadUrl(badbin_urls); |
| 865 | 1123 |
| 866 // Since badbin_url is not in database, it is considered to be safe. | 1124 // Since badbin_url is not in database, it is considered to be safe. |
| 867 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType()); | 1125 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType()); |
| 868 | 1126 |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1082 EXPECT_TRUE(csd_service->enabled()); | 1340 EXPECT_TRUE(csd_service->enabled()); |
| 1083 | 1341 |
| 1084 // Delete the Profile. SBS stops again. | 1342 // Delete the Profile. SBS stops again. |
| 1085 pref_service2 = NULL; | 1343 pref_service2 = NULL; |
| 1086 profile2.reset(); | 1344 profile2.reset(); |
| 1087 EXPECT_FALSE(sb_service->enabled_by_prefs()); | 1345 EXPECT_FALSE(sb_service->enabled_by_prefs()); |
| 1088 WaitForIOAndCheckEnabled(sb_service, false); | 1346 WaitForIOAndCheckEnabled(sb_service, false); |
| 1089 EXPECT_FALSE(csd_service->enabled()); | 1347 EXPECT_FALSE(csd_service->enabled()); |
| 1090 } | 1348 } |
| 1091 | 1349 |
| 1092 } // namespace | |
| 1093 | |
| 1094 class SafeBrowsingServiceShutdownTest : public SafeBrowsingServiceTest { | 1350 class SafeBrowsingServiceShutdownTest : public SafeBrowsingServiceTest { |
| 1095 public: | 1351 public: |
| 1096 void TearDown() override { | 1352 void TearDown() override { |
| 1097 // Browser should be fully torn down by now, so we can safely check these | 1353 // Browser should be fully torn down by now, so we can safely check these |
| 1098 // counters. | 1354 // counters. |
| 1099 EXPECT_EQ(1, TestProtocolManager::create_count()); | 1355 EXPECT_EQ(1, TestProtocolManager::create_count()); |
| 1100 EXPECT_EQ(1, TestProtocolManager::delete_count()); | 1356 EXPECT_EQ(1, TestProtocolManager::delete_count()); |
| 1101 | 1357 |
| 1102 SafeBrowsingServiceTest::TearDown(); | 1358 SafeBrowsingServiceTest::TearDown(); |
| 1103 } | 1359 } |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1318 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | 1574 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
| 1319 content::Source<SafeBrowsingDatabaseManager>( | 1575 content::Source<SafeBrowsingDatabaseManager>( |
| 1320 sb_service_->database_manager().get())); | 1576 sb_service_->database_manager().get())); |
| 1321 BrowserThread::PostTask( | 1577 BrowserThread::PostTask( |
| 1322 BrowserThread::IO, FROM_HERE, | 1578 BrowserThread::IO, FROM_HERE, |
| 1323 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); | 1579 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); |
| 1324 observer.Wait(); | 1580 observer.Wait(); |
| 1325 } | 1581 } |
| 1326 | 1582 |
| 1327 } // namespace safe_browsing | 1583 } // namespace safe_browsing |
| OLD | NEW |