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

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

Issue 2684663002: Part 2: Browser tests for using the new SafeBrowsing protocol (v4) (Closed)
Patch Set: nparker@'s review Created 3 years, 10 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 namespace { 99 namespace {
100 100
101 const char kEmptyPage[] = "/empty.html"; 101 const char kEmptyPage[] = "/empty.html";
102 const char kMalwareFile[] = "/downloads/dangerous/dangerous.exe"; 102 const char kMalwareFile[] = "/downloads/dangerous/dangerous.exe";
103 const char kMalwarePage[] = "/safe_browsing/malware.html"; 103 const char kMalwarePage[] = "/safe_browsing/malware.html";
104 const char kMalwareDelayedLoadsPage[] = 104 const char kMalwareDelayedLoadsPage[] =
105 "/safe_browsing/malware_delayed_loads.html"; 105 "/safe_browsing/malware_delayed_loads.html";
106 const char kMalwareIFrame[] = "/safe_browsing/malware_iframe.html"; 106 const char kMalwareIFrame[] = "/safe_browsing/malware_iframe.html";
107 const char kMalwareImg[] = "/safe_browsing/malware_image.png"; 107 const char kMalwareImg[] = "/safe_browsing/malware_image.png";
108 const char kNeverCompletesPath[] = "/never_completes"; 108 const char kNeverCompletesPath[] = "/never_completes";
109 const char kPrefetchMalwarePage[] = "/safe_browsing/prefetch_malware.html";
109 110
110 class MockSubresourceFilterDriver 111 class MockSubresourceFilterDriver
111 : public subresource_filter::ContentSubresourceFilterDriver { 112 : public subresource_filter::ContentSubresourceFilterDriver {
112 public: 113 public:
113 explicit MockSubresourceFilterDriver( 114 explicit MockSubresourceFilterDriver(
114 content::RenderFrameHost* render_frame_host) 115 content::RenderFrameHost* render_frame_host)
115 : subresource_filter::ContentSubresourceFilterDriver(render_frame_host) {} 116 : subresource_filter::ContentSubresourceFilterDriver(render_frame_host) {}
116 117
117 ~MockSubresourceFilterDriver() override = default; 118 ~MockSubresourceFilterDriver() override = default;
118 119
(...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after
852 interstitial_page->Proceed(); 853 interstitial_page->Proceed();
853 load_stop_observer.Wait(); 854 load_stop_observer.Wait();
854 EXPECT_FALSE(ShowingInterstitialPage()); 855 EXPECT_FALSE(ShowingInterstitialPage());
855 856
856 // Navigate to kEmptyPage again -- should hit the whitelist this time. 857 // Navigate to kEmptyPage again -- should hit the whitelist this time.
857 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(0); 858 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(0);
858 ui_test_utils::NavigateToURL(browser(), url); 859 ui_test_utils::NavigateToURL(browser(), url);
859 EXPECT_FALSE(ShowingInterstitialPage()); 860 EXPECT_FALSE(ShowingInterstitialPage());
860 } 861 }
861 862
862 const char kPrefetchMalwarePage[] = "/safe_browsing/prefetch_malware.html";
863
864 // This test confirms that prefetches don't themselves get the 863 // This test confirms that prefetches don't themselves get the
865 // interstitial treatment. 864 // interstitial treatment.
866 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Prefetch) { 865 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Prefetch) {
867 GURL url = embedded_test_server()->GetURL(kPrefetchMalwarePage); 866 GURL url = embedded_test_server()->GetURL(kPrefetchMalwarePage);
868 GURL malware_url = embedded_test_server()->GetURL(kMalwarePage); 867 GURL malware_url = embedded_test_server()->GetURL(kMalwarePage);
869 868
870 class SetPrefetchForTest {
871 public:
872 explicit SetPrefetchForTest(bool prefetch)
873 : old_prerender_mode_(prerender::PrerenderManager::GetMode()) {
874 std::string exp_group = prefetch ? "ExperimentYes" : "ExperimentNo";
875 base::FieldTrialList::CreateFieldTrial("Prefetch", exp_group);
876
877 prerender::PrerenderManager::SetMode(
878 prerender::PrerenderManager::PRERENDER_MODE_DISABLED);
879 }
880
881 ~SetPrefetchForTest() {
882 prerender::PrerenderManager::SetMode(old_prerender_mode_);
883 }
884
885 private:
886 prerender::PrerenderManager::PrerenderManagerMode old_prerender_mode_;
887 } set_prefetch_for_test(true);
888
889 // Even though we have added this uri to the safebrowsing database and 869 // Even though we have added this uri to the safebrowsing database and
890 // getfullhash result, we should not see the interstitial page since the 870 // getfullhash result, we should not see the interstitial page since the
891 // only malware was a prefetch target. 871 // only malware was a prefetch target.
892 SBFullHashResult malware_full_hash; 872 SBFullHashResult malware_full_hash;
893 GenUrlFullHashResult(malware_url, MALWARE, &malware_full_hash); 873 GenUrlFullHashResult(malware_url, MALWARE, &malware_full_hash);
894 SetupResponseForUrl(malware_url, malware_full_hash); 874 SetupResponseForUrl(malware_url, malware_full_hash);
895 ui_test_utils::NavigateToURL(browser(), url); 875 ui_test_utils::NavigateToURL(browser(), url);
896 EXPECT_FALSE(ShowingInterstitialPage()); 876 EXPECT_FALSE(ShowingInterstitialPage());
897 EXPECT_FALSE(got_hit_report()); 877 EXPECT_FALSE(got_hit_report());
898 Mock::VerifyAndClear(&observer_); 878 Mock::VerifyAndClear(&observer_);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
956 PHISH, 936 PHISH,
957 ThreatPatternType::SOCIAL_ENGINEERING_ADS, 937 ThreatPatternType::SOCIAL_ENGINEERING_ADS,
958 &malware_full_hash); 938 &malware_full_hash);
959 SetupResponseForUrl(bad_url, malware_full_hash); 939 SetupResponseForUrl(bad_url, malware_full_hash);
960 940
961 WebContents* main_contents = 941 WebContents* main_contents =
962 browser()->tab_strip_model()->GetActiveWebContents(); 942 browser()->tab_strip_model()->GetActiveWebContents();
963 943
964 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url))) 944 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
965 .Times(1); 945 .Times(1);
966 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_, 946 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(_, _, _)).Times(0);
967 ::testing::_))
968 .Times(0);
969 ui_test_utils::NavigateToURL(browser(), bad_url); 947 ui_test_utils::NavigateToURL(browser(), bad_url);
970 Mock::VerifyAndClearExpectations(&observer_); 948 Mock::VerifyAndClearExpectations(&observer_);
971 ASSERT_TRUE(got_hit_report()); 949 ASSERT_TRUE(got_hit_report());
972 950
973 content::WaitForInterstitialAttach(main_contents); 951 content::WaitForInterstitialAttach(main_contents);
974 EXPECT_TRUE(ShowingInterstitialPage()); 952 EXPECT_TRUE(ShowingInterstitialPage());
975 testing::Mock::VerifyAndClearExpectations(driver()); 953 testing::Mock::VerifyAndClearExpectations(driver());
976 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_, 954 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(_, _, _)).Times(1);
977 ::testing::_))
978 .Times(1);
979 InterstitialPage* interstitial_page = main_contents->GetInterstitialPage(); 955 InterstitialPage* interstitial_page = main_contents->GetInterstitialPage();
980 ASSERT_TRUE(interstitial_page); 956 ASSERT_TRUE(interstitial_page);
981 interstitial_page->Proceed(); 957 interstitial_page->Proceed();
982 content::WaitForInterstitialDetach(main_contents); 958 content::WaitForInterstitialDetach(main_contents);
983 EXPECT_FALSE(ShowingInterstitialPage()); 959 EXPECT_FALSE(ShowingInterstitialPage());
984 testing::Mock::VerifyAndClearExpectations(driver()); 960 testing::Mock::VerifyAndClearExpectations(driver());
985 } 961 }
986 962
987 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, SocEngReportingBlacklistEmpty) { 963 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, SocEngReportingBlacklistEmpty) {
988 // Tests that URLS which doesn't belong to the SOCIAL_ENGINEERING_ADS threat 964 // Tests that URLS which doesn't belong to the SOCIAL_ENGINEERING_ADS threat
989 // type aren't seen by the Subresource Filter. 965 // type aren't seen by the Subresource Filter.
990 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle 966 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
991 scoped_feature_toggle( 967 scoped_feature_toggle(
992 base::FeatureList::OVERRIDE_ENABLE_FEATURE, 968 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
993 subresource_filter::kActivationLevelEnabled, 969 subresource_filter::kActivationLevelEnabled,
994 subresource_filter::kActivationScopeNoSites, 970 subresource_filter::kActivationScopeNoSites,
995 subresource_filter::kActivationListSocialEngineeringAdsInterstitial); 971 subresource_filter::kActivationListSocialEngineeringAdsInterstitial);
996 972
997 GURL bad_url = embedded_test_server()->base_url().Resolve(kMalwarePage); 973 GURL bad_url = embedded_test_server()->base_url().Resolve(kMalwarePage);
998 974
999 SBFullHashResult malware_full_hash; 975 SBFullHashResult malware_full_hash;
1000 GenUrlFullHashResult(bad_url, MALWARE, &malware_full_hash); 976 GenUrlFullHashResult(bad_url, MALWARE, &malware_full_hash);
1001 SetupResponseForUrl(bad_url, malware_full_hash); 977 SetupResponseForUrl(bad_url, malware_full_hash);
1002 978
1003 WebContents* main_contents = 979 WebContents* main_contents =
1004 browser()->tab_strip_model()->GetActiveWebContents(); 980 browser()->tab_strip_model()->GetActiveWebContents();
1005 981
1006 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url))) 982 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
1007 .Times(1); 983 .Times(1);
1008 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_, 984 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(_, _, _)).Times(0);
1009 ::testing::_))
1010 .Times(0);
1011 ui_test_utils::NavigateToURL(browser(), bad_url); 985 ui_test_utils::NavigateToURL(browser(), bad_url);
1012 testing::Mock::VerifyAndClearExpectations(driver()); 986 testing::Mock::VerifyAndClearExpectations(driver());
1013 ASSERT_TRUE(got_hit_report()); 987 ASSERT_TRUE(got_hit_report());
1014 988
1015 content::WaitForInterstitialAttach(main_contents); 989 content::WaitForInterstitialAttach(main_contents);
1016 EXPECT_TRUE(ShowingInterstitialPage()); 990 EXPECT_TRUE(ShowingInterstitialPage());
1017 testing::Mock::VerifyAndClearExpectations(driver()); 991 testing::Mock::VerifyAndClearExpectations(driver());
1018 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_, 992 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(_, _, _)).Times(0);
1019 ::testing::_))
1020 .Times(0);
1021 InterstitialPage* interstitial_page = main_contents->GetInterstitialPage(); 993 InterstitialPage* interstitial_page = main_contents->GetInterstitialPage();
1022 ASSERT_TRUE(interstitial_page); 994 ASSERT_TRUE(interstitial_page);
1023 interstitial_page->Proceed(); 995 interstitial_page->Proceed();
1024 content::WaitForInterstitialDetach(main_contents); 996 content::WaitForInterstitialDetach(main_contents);
1025 EXPECT_FALSE(ShowingInterstitialPage()); 997 EXPECT_FALSE(ShowingInterstitialPage());
1026 testing::Mock::VerifyAndClearExpectations(driver()); 998 testing::Mock::VerifyAndClearExpectations(driver());
1027 } 999 }
1028 1000
1029 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, 1001 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest,
1030 SubResourceHitWithMainFrameReferrer) { 1002 SubResourceHitWithMainFrameReferrer) {
(...skipping 949 matching lines...) Expand 10 before | Expand all | Expand 10 after
1980 const GURL& url, 1952 const GURL& url,
1981 const ListIdentifier& list_id, 1953 const ListIdentifier& list_id,
1982 ThreatPatternType threat_pattern_type) { 1954 ThreatPatternType threat_pattern_type) {
1983 FullHashInfo fhi = GetFullHashInfo(url, list_id); 1955 FullHashInfo fhi = GetFullHashInfo(url, list_id);
1984 fhi.metadata.threat_pattern_type = threat_pattern_type; 1956 fhi.metadata.threat_pattern_type = threat_pattern_type;
1985 return fhi; 1957 return fhi;
1986 } 1958 }
1987 1959
1988 // Sets up the prefix database and the full hash cache to match one of the 1960 // Sets up the prefix database and the full hash cache to match one of the
1989 // prefixes for the given URL and metadata. 1961 // prefixes for the given URL and metadata.
1990 void MarkUrlForMalwareUnexpired(const GURL& bad_url, 1962 void MarkUrlForMalwareUnexpired(
1991 ThreatPatternType threat_pattern_type) { 1963 const GURL& bad_url,
1964 ThreatPatternType threat_pattern_type = ThreatPatternType::NONE) {
1992 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata( 1965 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata(
1993 bad_url, GetUrlMalwareId(), threat_pattern_type); 1966 bad_url, GetUrlMalwareId(), threat_pattern_type);
1994 1967
1995 v4_db_factory_->MarkPrefixAsBad(GetUrlMalwareId(), 1968 v4_db_factory_->MarkPrefixAsBad(GetUrlMalwareId(),
1996 full_hash_info.full_hash); 1969 full_hash_info.full_hash);
1997 v4_get_hash_factory_->AddToFullHashCache(full_hash_info); 1970 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1998 } 1971 }
1999 1972
2000 // Sets up the prefix database and the full hash cache to match one of the 1973 // Sets up the prefix database and the full hash cache to match one of the
2001 // prefixes for the given URL. 1974 // prefixes for the given URL.
2002 void MarkUrlForUwsUnexpired(const GURL& bad_url) { 1975 void MarkUrlForUwsUnexpired(const GURL& bad_url) {
2003 FullHashInfo full_hash_info = GetFullHashInfo(bad_url, GetUrlUwsId()); 1976 FullHashInfo full_hash_info = GetFullHashInfo(bad_url, GetUrlUwsId());
2004 v4_db_factory_->MarkPrefixAsBad(GetUrlUwsId(), full_hash_info.full_hash); 1977 v4_db_factory_->MarkPrefixAsBad(GetUrlUwsId(), full_hash_info.full_hash);
2005 v4_get_hash_factory_->AddToFullHashCache(full_hash_info); 1978 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
2006 } 1979 }
2007 1980
1981 void MarkUrlForPhishingUnexpired(const GURL& bad_url,
1982 ThreatPatternType threat_pattern_type) {
1983 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata(
1984 bad_url, GetUrlSocEngId(), threat_pattern_type);
1985
1986 v4_db_factory_->MarkPrefixAsBad(GetUrlSocEngId(), full_hash_info.full_hash);
1987 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1988 }
1989
2008 private: 1990 private:
2009 // Owned by the V4Database. 1991 // Owned by the V4Database.
2010 TestV4DatabaseFactory* v4_db_factory_; 1992 TestV4DatabaseFactory* v4_db_factory_;
2011 // Owned by the V4GetHashProtocolManager. 1993 // Owned by the V4GetHashProtocolManager.
2012 TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; 1994 TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_;
2013 1995
2014 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceTest); 1996 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceTest);
2015 }; 1997 };
2016 1998
2017 // Ensures that if an image is marked as UwS, the main page doesn't show an 1999 // Ensures that if an image is marked as UwS, the main page doesn't show an
2018 // interstitial. 2000 // interstitial.
2019 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, UnwantedImgIgnored) { 2001 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, UnwantedImgIgnored) {
2020 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); 2002 GURL main_url = embedded_test_server()->GetURL(kMalwarePage);
2021 GURL img_url = embedded_test_server()->GetURL(kMalwareImg); 2003 GURL img_url = embedded_test_server()->GetURL(kMalwareImg);
2022 2004
2023 // Add the img url as coming from a site serving UwS and then load the parent 2005 // Add the img url as coming from a site serving UwS and then load the parent
2024 // page. 2006 // page.
2025 MarkUrlForUwsUnexpired(img_url); 2007 MarkUrlForUwsUnexpired(img_url);
2026 2008
2027 ui_test_utils::NavigateToURL(browser(), main_url); 2009 ui_test_utils::NavigateToURL(browser(), main_url);
2028 2010
2029 EXPECT_FALSE(ShowingInterstitialPage()); 2011 EXPECT_FALSE(ShowingInterstitialPage());
2030 EXPECT_FALSE(got_hit_report()); 2012 EXPECT_FALSE(got_hit_report());
2031 } 2013 }
2032 2014
2015 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, MalwareWithWhitelist) {
2016 GURL url = embedded_test_server()->GetURL(kEmptyPage);
2017
2018 // After adding the url to safebrowsing database and getfullhash result,
2019 // we should see the interstitial page.
2020 MarkUrlForMalwareUnexpired(url);
2021 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(1);
2022
2023 ui_test_utils::NavigateToURL(browser(), url);
2024 Mock::VerifyAndClearExpectations(&observer_);
2025 // There should be an InterstitialPage.
2026 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
2027 InterstitialPage* interstitial_page = contents->GetInterstitialPage();
2028 ASSERT_TRUE(interstitial_page);
2029 // Proceed through it.
2030 content::WindowedNotificationObserver load_stop_observer(
2031 content::NOTIFICATION_LOAD_STOP,
2032 content::Source<content::NavigationController>(
2033 &contents->GetController()));
2034 interstitial_page->Proceed();
2035 load_stop_observer.Wait();
2036 EXPECT_FALSE(ShowingInterstitialPage());
2037
2038 // Navigate to kEmptyPage again -- should hit the whitelist this time.
2039 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(0);
2040 ui_test_utils::NavigateToURL(browser(), url);
2041 EXPECT_FALSE(ShowingInterstitialPage());
2042 }
2043
2044 // This test confirms that prefetches don't themselves get the
2045 // interstitial treatment.
2046 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, Prefetch) {
2047 GURL url = embedded_test_server()->GetURL(kPrefetchMalwarePage);
2048 GURL malware_url = embedded_test_server()->GetURL(kMalwarePage);
2049
2050 // Even though we have added this URI to the SafeBrowsing database and
2051 // full hash result, we should not see the interstitial page since the
2052 // only malware was a prefetch target.
2053 MarkUrlForMalwareUnexpired(malware_url);
2054
2055 ui_test_utils::NavigateToURL(browser(), url);
2056 EXPECT_FALSE(ShowingInterstitialPage());
2057 EXPECT_FALSE(got_hit_report());
2058 Mock::VerifyAndClear(&observer_);
2059
2060 // However, when we navigate to the malware page, we should still get
2061 // the interstitial.
2062 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(malware_url)))
2063 .Times(1);
2064 ui_test_utils::NavigateToURL(browser(), malware_url);
2065 EXPECT_TRUE(ShowingInterstitialPage());
2066 EXPECT_TRUE(got_hit_report());
2067 Mock::VerifyAndClear(&observer_);
2068 }
2069
2070 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, MainFrameHitWithReferrer) {
2071 GURL first_url = embedded_test_server()->GetURL(kEmptyPage);
2072 GURL bad_url = embedded_test_server()->GetURL(kMalwarePage);
2073
2074 MarkUrlForMalwareUnexpired(bad_url);
2075
2076 // Navigate to first, safe page.
2077 ui_test_utils::NavigateToURL(browser(), first_url);
2078 EXPECT_FALSE(ShowingInterstitialPage());
2079 EXPECT_FALSE(got_hit_report());
2080 Mock::VerifyAndClear(&observer_);
2081
2082 // Navigate to malware page, should show interstitial and have first page in
2083 // referrer.
2084 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2085 .Times(1);
2086
2087 chrome::NavigateParams params(browser(), bad_url, ui::PAGE_TRANSITION_LINK);
2088 params.referrer.url = first_url;
2089 ui_test_utils::NavigateToURL(&params);
2090
2091 EXPECT_TRUE(ShowingInterstitialPage());
2092 EXPECT_TRUE(got_hit_report());
2093 EXPECT_EQ(bad_url, hit_report().malicious_url);
2094 EXPECT_EQ(bad_url, hit_report().page_url);
2095 EXPECT_EQ(first_url, hit_report().referrer_url);
2096 EXPECT_FALSE(hit_report().is_subresource);
2097 }
2098
2099 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
2100 SocEngReportingBlacklistNotEmpty) {
2101 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
2102 scoped_feature_toggle(
2103 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
2104 subresource_filter::kActivationLevelEnabled,
2105 subresource_filter::kActivationScopeActivationList,
2106 subresource_filter::kActivationListSocialEngineeringAdsInterstitial);
2107 // Tests that when Safe Browsing gets hit which is corresponding to the
2108 // SOCIAL_ENGINEERING_ADS threat type, then URL is added to the Subresource
2109 // Filter.
2110 GURL bad_url = embedded_test_server()->GetURL(kMalwarePage);
2111 MarkUrlForPhishingUnexpired(bad_url,
2112 ThreatPatternType::SOCIAL_ENGINEERING_ADS);
2113
2114 WebContents* main_contents =
2115 browser()->tab_strip_model()->GetActiveWebContents();
2116
2117 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2118 .Times(1);
2119 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(_, _, _)).Times(0);
2120 ui_test_utils::NavigateToURL(browser(), bad_url);
2121 Mock::VerifyAndClearExpectations(&observer_);
2122 ASSERT_TRUE(got_hit_report());
2123
2124 content::WaitForInterstitialAttach(main_contents);
2125 EXPECT_TRUE(ShowingInterstitialPage());
2126 testing::Mock::VerifyAndClearExpectations(driver());
2127 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(_, _, _)).Times(1);
2128 InterstitialPage* interstitial_page = main_contents->GetInterstitialPage();
2129 ASSERT_TRUE(interstitial_page);
2130 interstitial_page->Proceed();
2131 content::WaitForInterstitialDetach(main_contents);
2132 EXPECT_FALSE(ShowingInterstitialPage());
2133 testing::Mock::VerifyAndClearExpectations(driver());
2134 }
2135
2136 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
2137 SocEngReportingBlacklistEmpty) {
2138 // Tests that URLS which doesn't belong to the SOCIAL_ENGINEERING_ADS threat
2139 // type aren't seen by the Subresource Filter.
2140 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
2141 scoped_feature_toggle(
2142 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
2143 subresource_filter::kActivationLevelEnabled,
2144 subresource_filter::kActivationScopeNoSites,
2145 subresource_filter::kActivationListSocialEngineeringAdsInterstitial);
2146
2147 GURL bad_url = embedded_test_server()->base_url().Resolve(kMalwarePage);
2148 MarkUrlForMalwareUnexpired(bad_url);
2149
2150 WebContents* main_contents =
2151 browser()->tab_strip_model()->GetActiveWebContents();
2152
2153 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2154 .Times(1);
2155 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(_, _, _)).Times(0);
2156 ui_test_utils::NavigateToURL(browser(), bad_url);
2157 testing::Mock::VerifyAndClearExpectations(driver());
2158 ASSERT_TRUE(got_hit_report());
2159
2160 content::WaitForInterstitialAttach(main_contents);
2161 EXPECT_TRUE(ShowingInterstitialPage());
2162 testing::Mock::VerifyAndClearExpectations(driver());
2163 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(_, _, _)).Times(0);
2164 InterstitialPage* interstitial_page = main_contents->GetInterstitialPage();
2165 ASSERT_TRUE(interstitial_page);
2166 interstitial_page->Proceed();
2167 content::WaitForInterstitialDetach(main_contents);
2168 EXPECT_FALSE(ShowingInterstitialPage());
2169 testing::Mock::VerifyAndClearExpectations(driver());
2170 }
2171
2172 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
2173 SubResourceHitWithMainFrameReferrer) {
2174 GURL first_url = embedded_test_server()->GetURL(kEmptyPage);
2175 GURL second_url = embedded_test_server()->GetURL(kMalwarePage);
2176 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg);
2177
2178 MarkUrlForMalwareUnexpired(bad_url);
2179
2180 // Navigate to first, safe page.
2181 ui_test_utils::NavigateToURL(browser(), first_url);
2182 EXPECT_FALSE(ShowingInterstitialPage());
2183 EXPECT_FALSE(got_hit_report());
2184 Mock::VerifyAndClear(&observer_);
2185
2186 // Navigate to page which has malware subresource, should show interstitial
2187 // and have first page in referrer.
2188 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2189 .Times(1);
2190
2191 chrome::NavigateParams params(browser(), second_url,
2192 ui::PAGE_TRANSITION_LINK);
2193 params.referrer.url = first_url;
2194 ui_test_utils::NavigateToURL(&params);
2195
2196 EXPECT_TRUE(ShowingInterstitialPage());
2197 EXPECT_TRUE(got_hit_report());
2198 EXPECT_EQ(bad_url, hit_report().malicious_url);
2199 EXPECT_EQ(second_url, hit_report().page_url);
2200 EXPECT_EQ(first_url, hit_report().referrer_url);
2201 EXPECT_TRUE(hit_report().is_subresource);
2202 }
2203
2204 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
2205 SubResourceHitWithMainFrameRendererInitiatedSlowLoad) {
2206 GURL first_url = embedded_test_server()->GetURL(kEmptyPage);
2207 GURL second_url = embedded_test_server()->GetURL(kMalwareDelayedLoadsPage);
2208 GURL third_url = embedded_test_server()->GetURL(kNeverCompletesPath);
2209 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg);
2210
2211 MarkUrlForMalwareUnexpired(bad_url);
2212
2213 // Navigate to first, safe page.
2214 ui_test_utils::NavigateToURL(browser(), first_url);
2215 EXPECT_FALSE(ShowingInterstitialPage());
2216 EXPECT_FALSE(got_hit_report());
2217 Mock::VerifyAndClear(&observer_);
2218
2219 // Navigate to malware page. The malware subresources haven't loaded yet, so
2220 // no interstitial should show yet.
2221 chrome::NavigateParams params(browser(), second_url,
2222 ui::PAGE_TRANSITION_LINK);
2223 params.referrer.url = first_url;
2224 ui_test_utils::NavigateToURL(&params);
2225
2226 EXPECT_FALSE(ShowingInterstitialPage());
2227 EXPECT_FALSE(got_hit_report());
2228 Mock::VerifyAndClear(&observer_);
2229
2230 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2231 .Times(1);
2232
2233 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
2234 content::WindowedNotificationObserver load_stop_observer(
2235 content::NOTIFICATION_LOAD_STOP,
2236 content::Source<content::NavigationController>(
2237 &contents->GetController()));
2238 // Run javascript function in the page which starts a timer to load the
2239 // malware image, and also starts a renderer-initiated top-level navigation to
2240 // a site that does not respond. Should show interstitial and have first page
2241 // in referrer.
2242 contents->GetMainFrame()->ExecuteJavaScriptForTests(
2243 base::ASCIIToUTF16("navigateAndLoadMalwareImage()"));
2244 load_stop_observer.Wait();
2245
2246 EXPECT_TRUE(ShowingInterstitialPage());
2247 EXPECT_TRUE(got_hit_report());
2248 // Report URLs should be for the current page, not the pending load.
2249 EXPECT_EQ(bad_url, hit_report().malicious_url);
2250 EXPECT_EQ(second_url, hit_report().page_url);
2251 EXPECT_EQ(first_url, hit_report().referrer_url);
2252 EXPECT_TRUE(hit_report().is_subresource);
2253 }
2254
2255 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
2256 SubResourceHitWithMainFrameBrowserInitiatedSlowLoad) {
2257 GURL first_url = embedded_test_server()->GetURL(kEmptyPage);
2258 GURL second_url = embedded_test_server()->GetURL(kMalwareDelayedLoadsPage);
2259 GURL third_url = embedded_test_server()->GetURL(kNeverCompletesPath);
2260 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg);
2261
2262 MarkUrlForMalwareUnexpired(bad_url);
2263
2264 // Navigate to first, safe page.
2265 ui_test_utils::NavigateToURL(browser(), first_url);
2266 EXPECT_FALSE(ShowingInterstitialPage());
2267 EXPECT_FALSE(got_hit_report());
2268 Mock::VerifyAndClear(&observer_);
2269
2270 // Navigate to malware page. The malware subresources haven't loaded yet, so
2271 // no interstitial should show yet.
2272 chrome::NavigateParams params(browser(), second_url,
2273 ui::PAGE_TRANSITION_LINK);
2274 params.referrer.url = first_url;
2275 ui_test_utils::NavigateToURL(&params);
2276
2277 EXPECT_FALSE(ShowingInterstitialPage());
2278 EXPECT_FALSE(got_hit_report());
2279 Mock::VerifyAndClear(&observer_);
2280
2281 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2282 .Times(1);
2283
2284 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
2285 content::RenderFrameHost* rfh = contents->GetMainFrame();
2286 content::WindowedNotificationObserver load_stop_observer(
2287 content::NOTIFICATION_LOAD_STOP,
2288 content::Source<content::NavigationController>(
2289 &contents->GetController()));
2290 // Start a browser initiated top-level navigation to a site that does not
2291 // respond.
2292 ui_test_utils::NavigateToURLWithDisposition(
2293 browser(), third_url, WindowOpenDisposition::CURRENT_TAB,
2294 ui_test_utils::BROWSER_TEST_NONE);
2295
2296 // While the top-level navigation is pending, run javascript
2297 // function in the page which loads the malware image.
2298 rfh->ExecuteJavaScriptForTests(base::ASCIIToUTF16("loadMalwareImage()"));
2299
2300 // Wait for interstitial to show.
2301 load_stop_observer.Wait();
2302
2303 EXPECT_TRUE(ShowingInterstitialPage());
2304 EXPECT_TRUE(got_hit_report());
2305 // Report URLs should be for the current page, not the pending load.
2306 EXPECT_EQ(bad_url, hit_report().malicious_url);
2307 EXPECT_EQ(second_url, hit_report().page_url);
2308 EXPECT_EQ(first_url, hit_report().referrer_url);
2309 EXPECT_TRUE(hit_report().is_subresource);
2310 }
2311
2312 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, SubResourceHitOnFreshTab) {
2313 // Allow popups.
2314 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
2315 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS,
2316 CONTENT_SETTING_ALLOW);
2317
2318 // Add |kMalwareImg| to fake safebrowsing db.
2319 GURL img_url = embedded_test_server()->GetURL(kMalwareImg);
2320 MarkUrlForMalwareUnexpired(img_url);
2321
2322 // Have the current tab open a new tab with window.open().
2323 WebContents* main_contents =
2324 browser()->tab_strip_model()->GetActiveWebContents();
2325 content::RenderFrameHost* main_rfh = main_contents->GetMainFrame();
2326
2327 content::WebContentsAddedObserver web_contents_added_observer;
2328 main_rfh->ExecuteJavaScriptForTests(base::ASCIIToUTF16("w=window.open();"));
2329 WebContents* new_tab_contents = web_contents_added_observer.GetWebContents();
2330 content::RenderFrameHost* new_tab_rfh = new_tab_contents->GetMainFrame();
2331 // A fresh WebContents should not have any NavigationEntries yet. (See
2332 // https://crbug.com/524208.)
2333 EXPECT_EQ(nullptr, new_tab_contents->GetController().GetLastCommittedEntry());
2334 EXPECT_EQ(nullptr, new_tab_contents->GetController().GetPendingEntry());
2335
2336 // Run javascript in the blank new tab to load the malware image.
2337 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(img_url)))
2338 .Times(1);
2339 new_tab_rfh->ExecuteJavaScriptForTests(
2340 base::ASCIIToUTF16("var img=new Image();"
2341 "img.src=\"" +
2342 img_url.spec() + "\";"
2343 "document.body.appendChild(img);"));
2344
2345 // Wait for interstitial to show.
2346 content::WaitForInterstitialAttach(new_tab_contents);
2347 Mock::VerifyAndClearExpectations(&observer_);
2348 EXPECT_TRUE(ShowingInterstitialPage());
2349 EXPECT_TRUE(got_hit_report());
2350 EXPECT_EQ(img_url, hit_report().malicious_url);
2351 EXPECT_TRUE(hit_report().is_subresource);
2352 // Page report URLs should be empty, since there is no URL for this page.
2353 EXPECT_EQ(GURL(), hit_report().page_url);
2354 EXPECT_EQ(GURL(), hit_report().referrer_url);
2355
2356 // Proceed through it.
2357 InterstitialPage* interstitial_page = new_tab_contents->GetInterstitialPage();
2358 ASSERT_TRUE(interstitial_page);
2359 interstitial_page->Proceed();
2360
2361 content::WaitForInterstitialDetach(new_tab_contents);
2362 EXPECT_FALSE(ShowingInterstitialPage());
2363 }
2364
2033 // TODO(vakh): Add test for UnwantedMainFrame. 2365 // TODO(vakh): Add test for UnwantedMainFrame.
2034 2366
2035 class V4SafeBrowsingServiceMetadataTest 2367 class V4SafeBrowsingServiceMetadataTest
2036 : public V4SafeBrowsingServiceTest, 2368 : public V4SafeBrowsingServiceTest,
2037 public ::testing::WithParamInterface<ThreatPatternType> { 2369 public ::testing::WithParamInterface<ThreatPatternType> {
2038 public: 2370 public:
2039 V4SafeBrowsingServiceMetadataTest() {} 2371 V4SafeBrowsingServiceMetadataTest() {}
2040 2372
2041 private: 2373 private:
2042 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceMetadataTest); 2374 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceMetadataTest);
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
2130 } 2462 }
2131 2463
2132 INSTANTIATE_TEST_CASE_P( 2464 INSTANTIATE_TEST_CASE_P(
2133 MaybeSetMetadata, 2465 MaybeSetMetadata,
2134 V4SafeBrowsingServiceMetadataTest, 2466 V4SafeBrowsingServiceMetadataTest,
2135 testing::Values(ThreatPatternType::NONE, 2467 testing::Values(ThreatPatternType::NONE,
2136 ThreatPatternType::MALWARE_LANDING, 2468 ThreatPatternType::MALWARE_LANDING,
2137 ThreatPatternType::MALWARE_DISTRIBUTION)); 2469 ThreatPatternType::MALWARE_DISTRIBUTION));
2138 2470
2139 } // namespace safe_browsing 2471 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698