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

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

Issue 2415553002: Metrics for tracking Safe Browsing hit patterns for the Safe Browsing lists Subresource Filter. (Closed)
Patch Set: change safe browsing browser tests Created 4 years, 1 month 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
« no previous file with comments | « no previous file | components/subresource_filter/content/browser/content_subresource_filter_driver_factory.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // 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 "chrome/browser/safe_browsing/safe_browsing_service.h" 9 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
10 10
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 #include "chrome/test/base/in_process_browser_test.h" 48 #include "chrome/test/base/in_process_browser_test.h"
49 #include "chrome/test/base/ui_test_utils.h" 49 #include "chrome/test/base/ui_test_utils.h"
50 #include "components/bookmarks/browser/startup_task_runner_service.h" 50 #include "components/bookmarks/browser/startup_task_runner_service.h"
51 #include "components/content_settings/core/browser/host_content_settings_map.h" 51 #include "components/content_settings/core/browser/host_content_settings_map.h"
52 #include "components/prefs/pref_service.h" 52 #include "components/prefs/pref_service.h"
53 #include "components/safe_browsing_db/database_manager.h" 53 #include "components/safe_browsing_db/database_manager.h"
54 #include "components/safe_browsing_db/metadata.pb.h" 54 #include "components/safe_browsing_db/metadata.pb.h"
55 #include "components/safe_browsing_db/test_database_manager.h" 55 #include "components/safe_browsing_db/test_database_manager.h"
56 #include "components/safe_browsing_db/util.h" 56 #include "components/safe_browsing_db/util.h"
57 #include "components/safe_browsing_db/v4_protocol_manager_util.h" 57 #include "components/safe_browsing_db/v4_protocol_manager_util.h"
58 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver.h"
58 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h" 59 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h"
59 #include "components/subresource_filter/core/browser/subresource_filter_features .h" 60 #include "components/subresource_filter/core/browser/subresource_filter_features .h"
60 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h" 61 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h"
61 #include "content/public/browser/interstitial_page.h" 62 #include "content/public/browser/interstitial_page.h"
62 #include "content/public/browser/navigation_entry.h" 63 #include "content/public/browser/navigation_entry.h"
63 #include "content/public/browser/render_frame_host.h" 64 #include "content/public/browser/render_frame_host.h"
64 #include "content/public/browser/web_contents.h" 65 #include "content/public/browser/web_contents.h"
65 #include "content/public/test/browser_test_utils.h" 66 #include "content/public/test/browser_test_utils.h"
66 #include "net/cookies/cookie_store.h" 67 #include "net/cookies/cookie_store.h"
67 #include "net/cookies/cookie_util.h" 68 #include "net/cookies/cookie_util.h"
(...skipping 27 matching lines...) Expand all
95 96
96 const char kEmptyPage[] = "/empty.html"; 97 const char kEmptyPage[] = "/empty.html";
97 const char kMalwareFile[] = "/downloads/dangerous/dangerous.exe"; 98 const char kMalwareFile[] = "/downloads/dangerous/dangerous.exe";
98 const char kMalwarePage[] = "/safe_browsing/malware.html"; 99 const char kMalwarePage[] = "/safe_browsing/malware.html";
99 const char kMalwareDelayedLoadsPage[] = 100 const char kMalwareDelayedLoadsPage[] =
100 "/safe_browsing/malware_delayed_loads.html"; 101 "/safe_browsing/malware_delayed_loads.html";
101 const char kMalwareIFrame[] = "/safe_browsing/malware_iframe.html"; 102 const char kMalwareIFrame[] = "/safe_browsing/malware_iframe.html";
102 const char kMalwareImg[] = "/safe_browsing/malware_image.png"; 103 const char kMalwareImg[] = "/safe_browsing/malware_image.png";
103 const char kNeverCompletesPath[] = "/never_completes"; 104 const char kNeverCompletesPath[] = "/never_completes";
104 105
106 class MockSubresourceFilterDriver
107 : public subresource_filter::ContentSubresourceFilterDriver {
108 public:
109 explicit MockSubresourceFilterDriver(
110 content::RenderFrameHost* render_frame_host)
111 : subresource_filter::ContentSubresourceFilterDriver(render_frame_host) {}
112
113 ~MockSubresourceFilterDriver() override = default;
114
115 MOCK_METHOD2(ActivateForProvisionalLoad,
116 void(subresource_filter::ActivationState, const GURL&));
117
118 private:
119 DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterDriver);
120 };
121
105 class NeverCompletingHttpResponse : public net::test_server::HttpResponse { 122 class NeverCompletingHttpResponse : public net::test_server::HttpResponse {
106 public: 123 public:
107 ~NeverCompletingHttpResponse() override {} 124 ~NeverCompletingHttpResponse() override {}
108 125
109 void SendResponse( 126 void SendResponse(
110 const net::test_server::SendBytesCallback& send, 127 const net::test_server::SendBytesCallback& send,
111 const net::test_server::SendCompleteCallback& done) override { 128 const net::test_server::SendCompleteCallback& done) override {
112 // Do nothing. |done| is never called. 129 // Do nothing. |done| is never called.
113 } 130 }
114 }; 131 };
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 #if defined(OS_CHROMEOS) 549 #if defined(OS_CHROMEOS)
533 command_line->AppendSwitch( 550 command_line->AppendSwitch(
534 chromeos::switches::kIgnoreUserProfileMappingForTests); 551 chromeos::switches::kIgnoreUserProfileMappingForTests);
535 #endif 552 #endif
536 } 553 }
537 554
538 void SetUpOnMainThread() override { 555 void SetUpOnMainThread() override {
539 InProcessBrowserTest::SetUpOnMainThread(); 556 InProcessBrowserTest::SetUpOnMainThread();
540 g_browser_process->safe_browsing_service()->ui_manager()->AddObserver( 557 g_browser_process->safe_browsing_service()->ui_manager()->AddObserver(
541 &observer_); 558 &observer_);
559 WebContents* contents =
560 browser()->tab_strip_model()->GetActiveWebContents();
561 driver_ = new MockSubresourceFilterDriver(contents->GetMainFrame());
562 factory()->SetDriverForFrameHostForTesting(contents->GetMainFrame(),
563 base::WrapUnique(driver()));
564 }
565
566 subresource_filter::ContentSubresourceFilterDriverFactory* factory() {
567 WebContents* contents =
568 browser()->tab_strip_model()->GetActiveWebContents();
569 return subresource_filter::ContentSubresourceFilterDriverFactory::
570 FromWebContents(contents);
542 } 571 }
543 572
544 void TearDownOnMainThread() override { 573 void TearDownOnMainThread() override {
545 g_browser_process->safe_browsing_service()->ui_manager()->RemoveObserver( 574 g_browser_process->safe_browsing_service()->ui_manager()->RemoveObserver(
546 &observer_); 575 &observer_);
547 InProcessBrowserTest::TearDownOnMainThread(); 576 InProcessBrowserTest::TearDownOnMainThread();
548 } 577 }
549 578
550 void SetUpInProcessBrowserTestFixture() override { 579 void SetUpInProcessBrowserTestFixture() override {
551 base::FilePath test_data_dir; 580 base::FilePath test_data_dir;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 645
617 FakeSafeBrowsingUIManager* ui_manager() { 646 FakeSafeBrowsingUIManager* ui_manager() {
618 return static_cast<FakeSafeBrowsingUIManager*>( 647 return static_cast<FakeSafeBrowsingUIManager*>(
619 g_browser_process->safe_browsing_service()->ui_manager().get()); 648 g_browser_process->safe_browsing_service()->ui_manager().get());
620 } 649 }
621 bool got_hit_report() { return ui_manager()->got_hit_report_; } 650 bool got_hit_report() { return ui_manager()->got_hit_report_; }
622 const safe_browsing::HitReport& hit_report() { 651 const safe_browsing::HitReport& hit_report() {
623 return ui_manager()->hit_report_; 652 return ui_manager()->hit_report_;
624 } 653 }
625 654
655 MockSubresourceFilterDriver* driver() { return driver_; }
656
626 protected: 657 protected:
627 StrictMock<MockObserver> observer_; 658 StrictMock<MockObserver> observer_;
628 659
629 // Temporary profile dir for test cases that create a second profile. This is 660 // Temporary profile dir for test cases that create a second profile. This is
630 // owned by the SafeBrowsingServiceTest object so that it will not get 661 // owned by the SafeBrowsingServiceTest object so that it will not get
631 // destructed until after the test Browser has been torn down, since the 662 // destructed until after the test Browser has been torn down, since the
632 // ImportantFileWriter may still be modifying it after the Profile object has 663 // ImportantFileWriter may still be modifying it after the Profile object has
633 // been destroyed. 664 // been destroyed.
634 base::ScopedTempDir temp_profile_dir_; 665 base::ScopedTempDir temp_profile_dir_;
635 666
(...skipping 20 matching lines...) Expand all
656 service, enabled, 687 service, enabled,
657 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get())); 688 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get()));
658 ASSERT_TRUE(enabled_helper->Run()); 689 ASSERT_TRUE(enabled_helper->Run());
659 } 690 }
660 691
661 private: 692 private:
662 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_; 693 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_;
663 TestSafeBrowsingDatabaseFactory db_factory_; 694 TestSafeBrowsingDatabaseFactory db_factory_;
664 TestSBProtocolManagerFactory pm_factory_; 695 TestSBProtocolManagerFactory pm_factory_;
665 696
697 // Owned by ContentSubresourceFilterFactory.
698 MockSubresourceFilterDriver* driver_;
699
666 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); 700 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest);
667 }; 701 };
668 702
669 class SafeBrowsingServiceMetadataTest 703 class SafeBrowsingServiceMetadataTest
670 : public SafeBrowsingServiceTest, 704 : public SafeBrowsingServiceTest,
671 public ::testing::WithParamInterface<ThreatPatternType> { 705 public ::testing::WithParamInterface<ThreatPatternType> {
672 public: 706 public:
673 SafeBrowsingServiceMetadataTest() {} 707 SafeBrowsingServiceMetadataTest() {}
674 708
675 void GenUrlFullHashResultWithMetadata(const GURL& url, 709 void GenUrlFullHashResultWithMetadata(const GURL& url,
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 EXPECT_EQ(first_url, hit_report().referrer_url); 932 EXPECT_EQ(first_url, hit_report().referrer_url);
899 EXPECT_FALSE(hit_report().is_subresource); 933 EXPECT_FALSE(hit_report().is_subresource);
900 } 934 }
901 935
902 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, 936 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest,
903 SocEngReportingBlacklistNotEmpty) { 937 SocEngReportingBlacklistNotEmpty) {
904 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle 938 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
905 scoped_feature_toggle( 939 scoped_feature_toggle(
906 base::FeatureList::OVERRIDE_ENABLE_FEATURE, 940 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
907 subresource_filter::kActivationStateEnabled, 941 subresource_filter::kActivationStateEnabled,
908 subresource_filter::kActivationScopeNoSites, 942 subresource_filter::kActivationScopeActivationList,
909 subresource_filter::kActivationListSocialEngineeringAdsInterstitial); 943 subresource_filter::kActivationListSocialEngineeringAdsInterstitial);
910 // Tests that when Safe Browsing gets hit which is corresponding to the 944 // Tests that when Safe Browsing gets hit which is corresponding to the
911 // SOCIAL_ENGINEERING_ADS threat type, then URL is added to the Subresource 945 // SOCIAL_ENGINEERING_ADS threat type, then URL is added to the Subresource
912 // Filter. 946 // Filter.
913 GURL bad_url = embedded_test_server()->GetURL(kMalwarePage); 947 GURL bad_url = embedded_test_server()->GetURL(kMalwarePage);
914 948
915 SBFullHashResult malware_full_hash; 949 SBFullHashResult malware_full_hash;
916 GenUrlFullHashResultWithMetadata(bad_url, MALWARE, 950 GenUrlFullHashResultWithMetadata(bad_url,
951 PHISH,
917 ThreatPatternType::SOCIAL_ENGINEERING_ADS, 952 ThreatPatternType::SOCIAL_ENGINEERING_ADS,
918 &malware_full_hash); 953 &malware_full_hash);
919 SetupResponseForUrl(bad_url, malware_full_hash); 954 SetupResponseForUrl(bad_url, malware_full_hash);
920 955
956 WebContents* main_contents =
957 browser()->tab_strip_model()->GetActiveWebContents();
958
921 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url))) 959 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
922 .Times(1); 960 .Times(1);
923 content::WebContents* web_contents = 961 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_))
924 browser()->tab_strip_model()->GetActiveWebContents(); 962 .Times(0);
925 subresource_filter::ContentSubresourceFilterDriverFactory* driver_factory = 963 ui_test_utils::NavigateToURL(browser(), bad_url);
926 subresource_filter::ContentSubresourceFilterDriverFactory:: 964 Mock::VerifyAndClearExpectations(&observer_);
927 FromWebContents(web_contents); 965 ASSERT_TRUE(got_hit_report());
928 966
929 EXPECT_EQ(0U, 967 content::WaitForInterstitialAttach(main_contents);
930 driver_factory->safe_browsing_blacklisted_patterns_set().size()); 968 EXPECT_TRUE(ShowingInterstitialPage());
931 chrome::NavigateParams params(browser(), bad_url, ui::PAGE_TRANSITION_LINK); 969 testing::Mock::VerifyAndClearExpectations(driver());
932 ui_test_utils::NavigateToURL(&params); 970 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_))
933 EXPECT_EQ(1U, 971 .Times(1);
934 driver_factory->safe_browsing_blacklisted_patterns_set().size()); 972 InterstitialPage* interstitial_page = main_contents->GetInterstitialPage();
935 EXPECT_TRUE(got_hit_report()); 973 ASSERT_TRUE(interstitial_page);
974 interstitial_page->Proceed();
975 content::WaitForInterstitialDetach(main_contents);
976 EXPECT_FALSE(ShowingInterstitialPage());
977 testing::Mock::VerifyAndClearExpectations(driver());
936 } 978 }
937 979
938 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, SocEngReportingBlacklistEmpty) { 980 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, SocEngReportingBlacklistEmpty) {
939 // Tests that URLS which doesn't belong to the SOCIAL_ENGINEERING_ADS threat 981 // Tests that URLS which doesn't belong to the SOCIAL_ENGINEERING_ADS threat
940 // type aren't seen by the Subresource Filter. 982 // type aren't seen by the Subresource Filter.
941 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle 983 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
942 scoped_feature_toggle( 984 scoped_feature_toggle(
943 base::FeatureList::OVERRIDE_ENABLE_FEATURE, 985 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
944 subresource_filter::kActivationStateEnabled, 986 subresource_filter::kActivationStateEnabled,
945 subresource_filter::kActivationScopeNoSites, 987 subresource_filter::kActivationScopeNoSites,
946 subresource_filter::kActivationListSocialEngineeringAdsInterstitial); 988 subresource_filter::kActivationListSocialEngineeringAdsInterstitial);
947 989
948 GURL bad_url = embedded_test_server()->GetURL(kMalwarePage); 990 GURL bad_url = embedded_test_server()->base_url().Resolve(kMalwarePage);
949 991
950 SBFullHashResult malware_full_hash; 992 SBFullHashResult malware_full_hash;
951 GenUrlFullHashResult(bad_url, MALWARE, &malware_full_hash); 993 GenUrlFullHashResult(bad_url, MALWARE, &malware_full_hash);
952 SetupResponseForUrl(bad_url, malware_full_hash); 994 SetupResponseForUrl(bad_url, malware_full_hash);
953 995
996 WebContents* main_contents =
997 browser()->tab_strip_model()->GetActiveWebContents();
998
954 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url))) 999 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
955 .Times(1); 1000 .Times(1);
956 content::WebContents* web_contents = 1001 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_))
957 browser()->tab_strip_model()->GetActiveWebContents(); 1002 .Times(0);
958 subresource_filter::ContentSubresourceFilterDriverFactory* driver_factory = 1003 ui_test_utils::NavigateToURL(browser(), bad_url);
959 subresource_filter::ContentSubresourceFilterDriverFactory:: 1004 testing::Mock::VerifyAndClearExpectations(driver());
960 FromWebContents(web_contents); 1005 ASSERT_TRUE(got_hit_report());
961 1006
962 EXPECT_EQ(0U, 1007 content::WaitForInterstitialAttach(main_contents);
963 driver_factory->safe_browsing_blacklisted_patterns_set().size()); 1008 EXPECT_TRUE(ShowingInterstitialPage());
964 chrome::NavigateParams params(browser(), bad_url, ui::PAGE_TRANSITION_LINK); 1009 testing::Mock::VerifyAndClearExpectations(driver());
965 ui_test_utils::NavigateToURL(&params); 1010 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_))
966 EXPECT_EQ(0U, 1011 .Times(0);
967 driver_factory->safe_browsing_blacklisted_patterns_set().size()); 1012 InterstitialPage* interstitial_page = main_contents->GetInterstitialPage();
968 EXPECT_TRUE(got_hit_report()); 1013 ASSERT_TRUE(interstitial_page);
1014 interstitial_page->Proceed();
1015 content::WaitForInterstitialDetach(main_contents);
1016 EXPECT_FALSE(ShowingInterstitialPage());
1017 testing::Mock::VerifyAndClearExpectations(driver());
969 } 1018 }
970 1019
971 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, 1020 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest,
972 SubResourceHitWithMainFrameReferrer) { 1021 SubResourceHitWithMainFrameReferrer) {
973 GURL first_url = embedded_test_server()->GetURL(kEmptyPage); 1022 GURL first_url = embedded_test_server()->GetURL(kEmptyPage);
974 GURL second_url = embedded_test_server()->GetURL(kMalwarePage); 1023 GURL second_url = embedded_test_server()->GetURL(kMalwarePage);
975 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg); 1024 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg);
976 1025
977 SBFullHashResult malware_full_hash; 1026 SBFullHashResult malware_full_hash;
978 GenUrlFullHashResult(bad_url, MALWARE, &malware_full_hash); 1027 GenUrlFullHashResult(bad_url, MALWARE, &malware_full_hash);
(...skipping 796 matching lines...) Expand 10 before | Expand all | Expand 10 after
1775 content::Source<SafeBrowsingDatabaseManager>( 1824 content::Source<SafeBrowsingDatabaseManager>(
1776 sb_factory_->test_safe_browsing_service()->database_manager().get())); 1825 sb_factory_->test_safe_browsing_service()->database_manager().get()));
1777 BrowserThread::PostTask( 1826 BrowserThread::PostTask(
1778 BrowserThread::IO, FROM_HERE, 1827 BrowserThread::IO, FROM_HERE,
1779 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, 1828 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate,
1780 base::Unretained(this))); 1829 base::Unretained(this)));
1781 observer.Wait(); 1830 observer.Wait();
1782 } 1831 }
1783 1832
1784 } // namespace safe_browsing 1833 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « no previous file | components/subresource_filter/content/browser/content_subresource_filter_driver_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698