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

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: rebase + fix failures after rebase 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 532 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 return static_cast<FakeSafeBrowsingUIManager*>( 652 return static_cast<FakeSafeBrowsingUIManager*>(
652 g_browser_process->safe_browsing_service()->ui_manager().get()); 653 g_browser_process->safe_browsing_service()->ui_manager().get());
653 } 654 }
654 bool got_hit_report() { return ui_manager()->got_hit_report_; } 655 bool got_hit_report() { return ui_manager()->got_hit_report_; }
655 const safe_browsing::HitReport& hit_report() { 656 const safe_browsing::HitReport& hit_report() {
656 return ui_manager()->hit_report_; 657 return ui_manager()->hit_report_;
657 } 658 }
658 659
659 MockSubresourceFilterDriver* driver() { return driver_; } 660 MockSubresourceFilterDriver* driver() { return driver_; }
660 661
662 class SetPrefetchForTest {
663 public:
664 explicit SetPrefetchForTest(bool prefetch)
665 : old_prerender_mode_(prerender::PrerenderManager::GetMode()) {
666 std::string exp_group = prefetch ? "ExperimentYes" : "ExperimentNo";
Nathan Parker 2017/02/10 01:05:21 I realize you just moved this code, but this seems
667 base::FieldTrialList::CreateFieldTrial("Prefetch", exp_group);
668
669 prerender::PrerenderManager::SetMode(
670 prerender::PrerenderManager::PRERENDER_MODE_DISABLED);
671 }
672
673 ~SetPrefetchForTest() {
674 prerender::PrerenderManager::SetMode(old_prerender_mode_);
675 }
676
677 private:
678 prerender::PrerenderManager::PrerenderManagerMode old_prerender_mode_;
679 };
680
661 protected: 681 protected:
662 StrictMock<MockObserver> observer_; 682 StrictMock<MockObserver> observer_;
663 683
664 // Temporary profile dir for test cases that create a second profile. This is 684 // Temporary profile dir for test cases that create a second profile. This is
665 // owned by the SafeBrowsingServiceTest object so that it will not get 685 // owned by the SafeBrowsingServiceTest object so that it will not get
666 // destructed until after the test Browser has been torn down, since the 686 // destructed until after the test Browser has been torn down, since the
667 // ImportantFileWriter may still be modifying it after the Profile object has 687 // ImportantFileWriter may still be modifying it after the Profile object has
668 // been destroyed. 688 // been destroyed.
669 base::ScopedTempDir temp_profile_dir_; 689 base::ScopedTempDir temp_profile_dir_;
670 690
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
852 interstitial_page->Proceed(); 872 interstitial_page->Proceed();
853 load_stop_observer.Wait(); 873 load_stop_observer.Wait();
854 EXPECT_FALSE(ShowingInterstitialPage()); 874 EXPECT_FALSE(ShowingInterstitialPage());
855 875
856 // Navigate to kEmptyPage again -- should hit the whitelist this time. 876 // Navigate to kEmptyPage again -- should hit the whitelist this time.
857 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(0); 877 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(0);
858 ui_test_utils::NavigateToURL(browser(), url); 878 ui_test_utils::NavigateToURL(browser(), url);
859 EXPECT_FALSE(ShowingInterstitialPage()); 879 EXPECT_FALSE(ShowingInterstitialPage());
860 } 880 }
861 881
862 const char kPrefetchMalwarePage[] = "/safe_browsing/prefetch_malware.html";
863
864 // This test confirms that prefetches don't themselves get the 882 // This test confirms that prefetches don't themselves get the
865 // interstitial treatment. 883 // interstitial treatment.
866 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Prefetch) { 884 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Prefetch) {
867 GURL url = embedded_test_server()->GetURL(kPrefetchMalwarePage); 885 GURL url = embedded_test_server()->GetURL(kPrefetchMalwarePage);
868 GURL malware_url = embedded_test_server()->GetURL(kMalwarePage); 886 GURL malware_url = embedded_test_server()->GetURL(kMalwarePage);
869 887
870 class SetPrefetchForTest { 888 SetPrefetchForTest instance(true);
Nathan Parker 2017/02/10 01:05:21 nit: maybe scoped_prefetch_setting(true) so it loo
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
889 // Even though we have added this uri to the safebrowsing database and 890 // Even though we have added this uri to the safebrowsing database and
890 // getfullhash result, we should not see the interstitial page since the 891 // getfullhash result, we should not see the interstitial page since the
891 // only malware was a prefetch target. 892 // only malware was a prefetch target.
892 SBFullHashResult malware_full_hash; 893 SBFullHashResult malware_full_hash;
893 GenUrlFullHashResult(malware_url, MALWARE, &malware_full_hash); 894 GenUrlFullHashResult(malware_url, MALWARE, &malware_full_hash);
894 SetupResponseForUrl(malware_url, malware_full_hash); 895 SetupResponseForUrl(malware_url, malware_full_hash);
895 ui_test_utils::NavigateToURL(browser(), url); 896 ui_test_utils::NavigateToURL(browser(), url);
896 EXPECT_FALSE(ShowingInterstitialPage()); 897 EXPECT_FALSE(ShowingInterstitialPage());
897 EXPECT_FALSE(got_hit_report()); 898 EXPECT_FALSE(got_hit_report());
(...skipping 1081 matching lines...) Expand 10 before | Expand all | Expand 10 after
1979 const GURL& url, 1980 const GURL& url,
1980 const ListIdentifier& list_id, 1981 const ListIdentifier& list_id,
1981 ThreatPatternType threat_pattern_type) { 1982 ThreatPatternType threat_pattern_type) {
1982 FullHashInfo fhi = GetFullHashInfo(url, list_id); 1983 FullHashInfo fhi = GetFullHashInfo(url, list_id);
1983 fhi.metadata.threat_pattern_type = threat_pattern_type; 1984 fhi.metadata.threat_pattern_type = threat_pattern_type;
1984 return fhi; 1985 return fhi;
1985 } 1986 }
1986 1987
1987 // Sets up the prefix database and the full hash cache to match one of the 1988 // Sets up the prefix database and the full hash cache to match one of the
1988 // prefixes for the given URL and metadata. 1989 // prefixes for the given URL and metadata.
1989 void MarkUrlForMalwareUnexpired(const GURL& bad_url, 1990 void MarkUrlForMalwareUnexpired(
1990 ThreatPatternType threat_pattern_type) { 1991 const GURL& bad_url,
1992 ThreatPatternType threat_pattern_type = ThreatPatternType::NONE) {
1991 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata( 1993 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata(
1992 bad_url, GetUrlMalwareId(), threat_pattern_type); 1994 bad_url, GetUrlMalwareId(), threat_pattern_type);
1993 1995
1994 v4_db_factory_->MarkPrefixAsBad(GetUrlMalwareId(), 1996 v4_db_factory_->MarkPrefixAsBad(GetUrlMalwareId(),
1995 full_hash_info.full_hash); 1997 full_hash_info.full_hash);
1996 v4_get_hash_factory_->AddToFullHashCache(full_hash_info); 1998 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1997 } 1999 }
1998 2000
1999 // Sets up the prefix database and the full hash cache to match one of the 2001 // Sets up the prefix database and the full hash cache to match one of the
2000 // prefixes for the given URL. 2002 // prefixes for the given URL.
2001 void MarkUrlForUwsUnexpired(const GURL& bad_url) { 2003 void MarkUrlForUwsUnexpired(const GURL& bad_url) {
2002 FullHashInfo full_hash_info = GetFullHashInfo(bad_url, GetUrlUwsId()); 2004 FullHashInfo full_hash_info = GetFullHashInfo(bad_url, GetUrlUwsId());
2003 v4_db_factory_->MarkPrefixAsBad(GetUrlUwsId(), full_hash_info.full_hash); 2005 v4_db_factory_->MarkPrefixAsBad(GetUrlUwsId(), full_hash_info.full_hash);
2004 v4_get_hash_factory_->AddToFullHashCache(full_hash_info); 2006 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
2005 } 2007 }
2006 2008
2009 void MarkUrlForPhishingUnexpired(const GURL& bad_url,
2010 ThreatPatternType threat_pattern_type) {
2011 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata(
2012 bad_url, GetUrlSocEngId(), threat_pattern_type);
2013
2014 v4_db_factory_->MarkPrefixAsBad(GetUrlSocEngId(), full_hash_info.full_hash);
2015 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
2016 }
2017
2007 private: 2018 private:
2008 // Owned by the V4Database. 2019 // Owned by the V4Database.
2009 TestV4DatabaseFactory* v4_db_factory_; 2020 TestV4DatabaseFactory* v4_db_factory_;
2010 // Owned by the V4GetHashProtocolManager. 2021 // Owned by the V4GetHashProtocolManager.
2011 TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; 2022 TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_;
2012 2023
2013 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceTest); 2024 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceTest);
2014 }; 2025 };
2015 2026
2016 // Ensures that if an image is marked as UwS, the main page doesn't show an 2027 // Ensures that if an image is marked as UwS, the main page doesn't show an
2017 // interstitial. 2028 // interstitial.
2018 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, UnwantedImgIgnored) { 2029 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, UnwantedImgIgnored) {
2019 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); 2030 GURL main_url = embedded_test_server()->GetURL(kMalwarePage);
2020 GURL img_url = embedded_test_server()->GetURL(kMalwareImg); 2031 GURL img_url = embedded_test_server()->GetURL(kMalwareImg);
2021 2032
2022 // Add the img url as coming from a site serving UwS and then load the parent 2033 // Add the img url as coming from a site serving UwS and then load the parent
2023 // page. 2034 // page.
2024 MarkUrlForUwsUnexpired(img_url); 2035 MarkUrlForUwsUnexpired(img_url);
2025 2036
2026 ui_test_utils::NavigateToURL(browser(), main_url); 2037 ui_test_utils::NavigateToURL(browser(), main_url);
2027 2038
2028 EXPECT_FALSE(ShowingInterstitialPage()); 2039 EXPECT_FALSE(ShowingInterstitialPage());
2029 EXPECT_FALSE(got_hit_report()); 2040 EXPECT_FALSE(got_hit_report());
2030 } 2041 }
2031 2042
2043 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, MalwareWithWhitelist) {
2044 GURL url = embedded_test_server()->GetURL(kEmptyPage);
2045
2046 // After adding the url to safebrowsing database and getfullhash result,
2047 // we should see the interstitial page.
2048 MarkUrlForMalwareUnexpired(url);
2049 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(1);
2050
2051 ui_test_utils::NavigateToURL(browser(), url);
2052 Mock::VerifyAndClearExpectations(&observer_);
2053 // There should be an InterstitialPage.
2054 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
2055 InterstitialPage* interstitial_page = contents->GetInterstitialPage();
2056 ASSERT_TRUE(interstitial_page);
2057 // Proceed through it.
2058 content::WindowedNotificationObserver load_stop_observer(
2059 content::NOTIFICATION_LOAD_STOP,
2060 content::Source<content::NavigationController>(
2061 &contents->GetController()));
2062 interstitial_page->Proceed();
2063 load_stop_observer.Wait();
2064 EXPECT_FALSE(ShowingInterstitialPage());
2065
2066 // Navigate to kEmptyPage again -- should hit the whitelist this time.
2067 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(0);
2068 ui_test_utils::NavigateToURL(browser(), url);
2069 EXPECT_FALSE(ShowingInterstitialPage());
2070 }
2071
2072 // This test confirms that prefetches don't themselves get the
2073 // interstitial treatment.
2074 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, Prefetch) {
2075 GURL url = embedded_test_server()->GetURL(kPrefetchMalwarePage);
2076 GURL malware_url = embedded_test_server()->GetURL(kMalwarePage);
2077
2078 SetPrefetchForTest instance(true);
2079
2080 // Even though we have added this URI to the SafeBrowsing database and
2081 // full hash result, we should not see the interstitial page since the
2082 // only malware was a prefetch target.
2083 MarkUrlForMalwareUnexpired(malware_url);
2084
2085 ui_test_utils::NavigateToURL(browser(), url);
2086 EXPECT_FALSE(ShowingInterstitialPage());
2087 EXPECT_FALSE(got_hit_report());
2088 Mock::VerifyAndClear(&observer_);
2089
2090 // However, when we navigate to the malware page, we should still get
2091 // the interstitial.
2092 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(malware_url)))
2093 .Times(1);
2094 ui_test_utils::NavigateToURL(browser(), malware_url);
2095 EXPECT_TRUE(ShowingInterstitialPage());
2096 EXPECT_TRUE(got_hit_report());
2097 Mock::VerifyAndClear(&observer_);
2098 }
2099
2100 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, MainFrameHitWithReferrer) {
2101 GURL first_url = embedded_test_server()->GetURL(kEmptyPage);
2102 GURL bad_url = embedded_test_server()->GetURL(kMalwarePage);
2103
2104 MarkUrlForMalwareUnexpired(bad_url);
2105
2106 // Navigate to first, safe page.
2107 ui_test_utils::NavigateToURL(browser(), first_url);
2108 EXPECT_FALSE(ShowingInterstitialPage());
2109 EXPECT_FALSE(got_hit_report());
2110 Mock::VerifyAndClear(&observer_);
2111
2112 // Navigate to malware page, should show interstitial and have first page in
2113 // referrer.
2114 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2115 .Times(1);
2116
2117 chrome::NavigateParams params(browser(), bad_url, ui::PAGE_TRANSITION_LINK);
2118 params.referrer.url = first_url;
2119 ui_test_utils::NavigateToURL(&params);
2120
2121 EXPECT_TRUE(ShowingInterstitialPage());
2122 EXPECT_TRUE(got_hit_report());
2123 EXPECT_EQ(bad_url, hit_report().malicious_url);
2124 EXPECT_EQ(bad_url, hit_report().page_url);
2125 EXPECT_EQ(first_url, hit_report().referrer_url);
2126 EXPECT_FALSE(hit_report().is_subresource);
2127 }
2128
2129 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
2130 SocEngReportingBlacklistNotEmpty) {
2131 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
2132 scoped_feature_toggle(
2133 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
2134 subresource_filter::kActivationLevelEnabled,
2135 subresource_filter::kActivationScopeActivationList,
2136 subresource_filter::kActivationListSocialEngineeringAdsInterstitial);
2137 // Tests that when Safe Browsing gets hit which is corresponding to the
2138 // SOCIAL_ENGINEERING_ADS threat type, then URL is added to the Subresource
2139 // Filter.
2140 GURL bad_url = embedded_test_server()->GetURL(kMalwarePage);
2141 MarkUrlForPhishingUnexpired(bad_url,
2142 ThreatPatternType::SOCIAL_ENGINEERING_ADS);
2143
2144 WebContents* main_contents =
2145 browser()->tab_strip_model()->GetActiveWebContents();
2146
2147 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2148 .Times(1);
2149 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_,
2150 ::testing::_))
2151 .Times(0);
2152 ui_test_utils::NavigateToURL(browser(), bad_url);
2153 Mock::VerifyAndClearExpectations(&observer_);
2154 ASSERT_TRUE(got_hit_report());
2155
2156 content::WaitForInterstitialAttach(main_contents);
2157 EXPECT_TRUE(ShowingInterstitialPage());
2158 testing::Mock::VerifyAndClearExpectations(driver());
2159 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_,
2160 ::testing::_))
2161 .Times(1);
2162 InterstitialPage* interstitial_page = main_contents->GetInterstitialPage();
2163 ASSERT_TRUE(interstitial_page);
2164 interstitial_page->Proceed();
2165 content::WaitForInterstitialDetach(main_contents);
2166 EXPECT_FALSE(ShowingInterstitialPage());
2167 testing::Mock::VerifyAndClearExpectations(driver());
2168 }
2169
2170 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
2171 SocEngReportingBlacklistEmpty) {
2172 // Tests that URLS which doesn't belong to the SOCIAL_ENGINEERING_ADS threat
2173 // type aren't seen by the Subresource Filter.
2174 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
2175 scoped_feature_toggle(
2176 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
2177 subresource_filter::kActivationLevelEnabled,
2178 subresource_filter::kActivationScopeNoSites,
2179 subresource_filter::kActivationListSocialEngineeringAdsInterstitial);
2180
2181 GURL bad_url = embedded_test_server()->base_url().Resolve(kMalwarePage);
2182 MarkUrlForMalwareUnexpired(bad_url);
2183
2184 WebContents* main_contents =
2185 browser()->tab_strip_model()->GetActiveWebContents();
2186
2187 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2188 .Times(1);
2189 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_,
Nathan Parker 2017/02/10 01:05:21 You've already got a "using" for this, so just s/:
2190 ::testing::_))
2191 .Times(0);
2192 ui_test_utils::NavigateToURL(browser(), bad_url);
2193 testing::Mock::VerifyAndClearExpectations(driver());
2194 ASSERT_TRUE(got_hit_report());
2195
2196 content::WaitForInterstitialAttach(main_contents);
2197 EXPECT_TRUE(ShowingInterstitialPage());
2198 testing::Mock::VerifyAndClearExpectations(driver());
2199 EXPECT_CALL(*driver(), ActivateForProvisionalLoad(::testing::_, ::testing::_,
2200 ::testing::_))
2201 .Times(0);
2202 InterstitialPage* interstitial_page = main_contents->GetInterstitialPage();
2203 ASSERT_TRUE(interstitial_page);
2204 interstitial_page->Proceed();
2205 content::WaitForInterstitialDetach(main_contents);
2206 EXPECT_FALSE(ShowingInterstitialPage());
2207 testing::Mock::VerifyAndClearExpectations(driver());
2208 }
2209
2210 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
2211 SubResourceHitWithMainFrameReferrer) {
2212 GURL first_url = embedded_test_server()->GetURL(kEmptyPage);
2213 GURL second_url = embedded_test_server()->GetURL(kMalwarePage);
2214 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg);
2215
2216 MarkUrlForMalwareUnexpired(bad_url);
2217
2218 // Navigate to first, safe page.
2219 ui_test_utils::NavigateToURL(browser(), first_url);
2220 EXPECT_FALSE(ShowingInterstitialPage());
2221 EXPECT_FALSE(got_hit_report());
2222 Mock::VerifyAndClear(&observer_);
2223
2224 // Navigate to page which has malware subresource, should show interstitial
2225 // and have first page in referrer.
2226 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2227 .Times(1);
2228
2229 chrome::NavigateParams params(browser(), second_url,
2230 ui::PAGE_TRANSITION_LINK);
2231 params.referrer.url = first_url;
2232 ui_test_utils::NavigateToURL(&params);
2233
2234 EXPECT_TRUE(ShowingInterstitialPage());
2235 EXPECT_TRUE(got_hit_report());
2236 EXPECT_EQ(bad_url, hit_report().malicious_url);
2237 EXPECT_EQ(second_url, hit_report().page_url);
2238 EXPECT_EQ(first_url, hit_report().referrer_url);
2239 EXPECT_TRUE(hit_report().is_subresource);
2240 }
2241
2242 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
2243 SubResourceHitWithMainFrameRendererInitiatedSlowLoad) {
2244 GURL first_url = embedded_test_server()->GetURL(kEmptyPage);
2245 GURL second_url = embedded_test_server()->GetURL(kMalwareDelayedLoadsPage);
2246 GURL third_url = embedded_test_server()->GetURL(kNeverCompletesPath);
2247 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg);
2248
2249 MarkUrlForMalwareUnexpired(bad_url);
2250
2251 // Navigate to first, safe page.
2252 ui_test_utils::NavigateToURL(browser(), first_url);
2253 EXPECT_FALSE(ShowingInterstitialPage());
2254 EXPECT_FALSE(got_hit_report());
2255 Mock::VerifyAndClear(&observer_);
2256
2257 // Navigate to malware page. The malware subresources haven't loaded yet, so
2258 // no interstitial should show yet.
2259 chrome::NavigateParams params(browser(), second_url,
2260 ui::PAGE_TRANSITION_LINK);
2261 params.referrer.url = first_url;
2262 ui_test_utils::NavigateToURL(&params);
2263
2264 EXPECT_FALSE(ShowingInterstitialPage());
2265 EXPECT_FALSE(got_hit_report());
2266 Mock::VerifyAndClear(&observer_);
2267
2268 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2269 .Times(1);
2270
2271 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
2272 content::WindowedNotificationObserver load_stop_observer(
2273 content::NOTIFICATION_LOAD_STOP,
2274 content::Source<content::NavigationController>(
2275 &contents->GetController()));
2276 // Run javascript function in the page which starts a timer to load the
2277 // malware image, and also starts a renderer-initiated top-level navigation to
2278 // a site that does not respond. Should show interstitial and have first page
2279 // in referrer.
2280 contents->GetMainFrame()->ExecuteJavaScriptForTests(
2281 base::ASCIIToUTF16("navigateAndLoadMalwareImage()"));
2282 load_stop_observer.Wait();
2283
2284 EXPECT_TRUE(ShowingInterstitialPage());
2285 EXPECT_TRUE(got_hit_report());
2286 // Report URLs should be for the current page, not the pending load.
2287 EXPECT_EQ(bad_url, hit_report().malicious_url);
2288 EXPECT_EQ(second_url, hit_report().page_url);
2289 EXPECT_EQ(first_url, hit_report().referrer_url);
2290 EXPECT_TRUE(hit_report().is_subresource);
2291 }
2292
2293 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
2294 SubResourceHitWithMainFrameBrowserInitiatedSlowLoad) {
2295 GURL first_url = embedded_test_server()->GetURL(kEmptyPage);
2296 GURL second_url = embedded_test_server()->GetURL(kMalwareDelayedLoadsPage);
2297 GURL third_url = embedded_test_server()->GetURL(kNeverCompletesPath);
2298 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg);
2299
2300 MarkUrlForMalwareUnexpired(bad_url);
2301
2302 // Navigate to first, safe page.
2303 ui_test_utils::NavigateToURL(browser(), first_url);
2304 EXPECT_FALSE(ShowingInterstitialPage());
2305 EXPECT_FALSE(got_hit_report());
2306 Mock::VerifyAndClear(&observer_);
2307
2308 // Navigate to malware page. The malware subresources haven't loaded yet, so
2309 // no interstitial should show yet.
2310 chrome::NavigateParams params(browser(), second_url,
2311 ui::PAGE_TRANSITION_LINK);
2312 params.referrer.url = first_url;
2313 ui_test_utils::NavigateToURL(&params);
2314
2315 EXPECT_FALSE(ShowingInterstitialPage());
2316 EXPECT_FALSE(got_hit_report());
2317 Mock::VerifyAndClear(&observer_);
2318
2319 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(bad_url)))
2320 .Times(1);
2321
2322 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
2323 content::RenderFrameHost* rfh = contents->GetMainFrame();
2324 content::WindowedNotificationObserver load_stop_observer(
2325 content::NOTIFICATION_LOAD_STOP,
2326 content::Source<content::NavigationController>(
2327 &contents->GetController()));
2328 // Start a browser initiated top-level navigation to a site that does not
2329 // respond.
2330 ui_test_utils::NavigateToURLWithDisposition(
2331 browser(), third_url, WindowOpenDisposition::CURRENT_TAB,
2332 ui_test_utils::BROWSER_TEST_NONE);
2333
2334 // While the top-level navigation is pending, run javascript
2335 // function in the page which loads the malware image.
2336 rfh->ExecuteJavaScriptForTests(base::ASCIIToUTF16("loadMalwareImage()"));
2337
2338 // Wait for interstitial to show.
2339 load_stop_observer.Wait();
2340
2341 EXPECT_TRUE(ShowingInterstitialPage());
2342 EXPECT_TRUE(got_hit_report());
2343 // Report URLs should be for the current page, not the pending load.
2344 EXPECT_EQ(bad_url, hit_report().malicious_url);
2345 EXPECT_EQ(second_url, hit_report().page_url);
2346 EXPECT_EQ(first_url, hit_report().referrer_url);
2347 EXPECT_TRUE(hit_report().is_subresource);
2348 }
2349
2350 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, SubResourceHitOnFreshTab) {
2351 // Allow popups.
2352 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
2353 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS,
2354 CONTENT_SETTING_ALLOW);
2355
2356 // Add |kMalwareImg| to fake safebrowsing db.
2357 GURL img_url = embedded_test_server()->GetURL(kMalwareImg);
2358 MarkUrlForMalwareUnexpired(img_url);
2359
2360 // Have the current tab open a new tab with window.open().
2361 WebContents* main_contents =
2362 browser()->tab_strip_model()->GetActiveWebContents();
2363 content::RenderFrameHost* main_rfh = main_contents->GetMainFrame();
2364
2365 content::WebContentsAddedObserver web_contents_added_observer;
2366 main_rfh->ExecuteJavaScriptForTests(base::ASCIIToUTF16("w=window.open();"));
2367 WebContents* new_tab_contents = web_contents_added_observer.GetWebContents();
2368 content::RenderFrameHost* new_tab_rfh = new_tab_contents->GetMainFrame();
2369 // A fresh WebContents should not have any NavigationEntries yet. (See
2370 // https://crbug.com/524208.)
2371 EXPECT_EQ(nullptr, new_tab_contents->GetController().GetLastCommittedEntry());
2372 EXPECT_EQ(nullptr, new_tab_contents->GetController().GetPendingEntry());
2373
2374 // Run javascript in the blank new tab to load the malware image.
2375 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(img_url)))
2376 .Times(1);
2377 new_tab_rfh->ExecuteJavaScriptForTests(
2378 base::ASCIIToUTF16("var img=new Image();"
2379 "img.src=\"" +
2380 img_url.spec() + "\";"
2381 "document.body.appendChild(img);"));
2382
2383 // Wait for interstitial to show.
2384 content::WaitForInterstitialAttach(new_tab_contents);
2385 Mock::VerifyAndClearExpectations(&observer_);
2386 EXPECT_TRUE(ShowingInterstitialPage());
2387 EXPECT_TRUE(got_hit_report());
2388 EXPECT_EQ(img_url, hit_report().malicious_url);
2389 EXPECT_TRUE(hit_report().is_subresource);
2390 // Page report URLs should be empty, since there is no URL for this page.
2391 EXPECT_EQ(GURL(), hit_report().page_url);
2392 EXPECT_EQ(GURL(), hit_report().referrer_url);
2393
2394 // Proceed through it.
2395 InterstitialPage* interstitial_page = new_tab_contents->GetInterstitialPage();
2396 ASSERT_TRUE(interstitial_page);
2397 interstitial_page->Proceed();
2398
2399 content::WaitForInterstitialDetach(new_tab_contents);
2400 EXPECT_FALSE(ShowingInterstitialPage());
2401 }
2402
2032 // TODO(vakh): Add test for UnwantedMainFrame. 2403 // TODO(vakh): Add test for UnwantedMainFrame.
2033 2404
2034 class V4SafeBrowsingServiceMetadataTest 2405 class V4SafeBrowsingServiceMetadataTest
2035 : public V4SafeBrowsingServiceTest, 2406 : public V4SafeBrowsingServiceTest,
2036 public ::testing::WithParamInterface<ThreatPatternType> { 2407 public ::testing::WithParamInterface<ThreatPatternType> {
2037 public: 2408 public:
2038 V4SafeBrowsingServiceMetadataTest() {} 2409 V4SafeBrowsingServiceMetadataTest() {}
2039 2410
2040 private: 2411 private:
2041 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceMetadataTest); 2412 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceMetadataTest);
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
2129 } 2500 }
2130 2501
2131 INSTANTIATE_TEST_CASE_P( 2502 INSTANTIATE_TEST_CASE_P(
2132 MaybeSetMetadata, 2503 MaybeSetMetadata,
2133 V4SafeBrowsingServiceMetadataTest, 2504 V4SafeBrowsingServiceMetadataTest,
2134 testing::Values(ThreatPatternType::NONE, 2505 testing::Values(ThreatPatternType::NONE,
2135 ThreatPatternType::MALWARE_LANDING, 2506 ThreatPatternType::MALWARE_LANDING,
2136 ThreatPatternType::MALWARE_DISTRIBUTION)); 2507 ThreatPatternType::MALWARE_DISTRIBUTION));
2137 2508
2138 } // namespace safe_browsing 2509 } // 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