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

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

Issue 2675063002: Browser tests for using the new SafeBrowsing protocol (v4) (Closed)
Patch Set: Simplify V4DB creation in product code and tests 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
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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 #include "chrome/test/base/in_process_browser_test.h" 47 #include "chrome/test/base/in_process_browser_test.h"
48 #include "chrome/test/base/ui_test_utils.h" 48 #include "chrome/test/base/ui_test_utils.h"
49 #include "components/bookmarks/browser/startup_task_runner_service.h" 49 #include "components/bookmarks/browser/startup_task_runner_service.h"
50 #include "components/content_settings/core/browser/host_content_settings_map.h" 50 #include "components/content_settings/core/browser/host_content_settings_map.h"
51 #include "components/prefs/pref_service.h" 51 #include "components/prefs/pref_service.h"
52 #include "components/safe_browsing/common/safebrowsing_switches.h" 52 #include "components/safe_browsing/common/safebrowsing_switches.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_database.h"
58 #include "components/safe_browsing_db/v4_feature_list.h"
59 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
57 #include "components/safe_browsing_db/v4_protocol_manager_util.h" 60 #include "components/safe_browsing_db/v4_protocol_manager_util.h"
58 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver.h" 61 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver.h"
59 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h" 62 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h"
60 #include "components/subresource_filter/core/browser/subresource_filter_features .h" 63 #include "components/subresource_filter/core/browser/subresource_filter_features .h"
61 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h" 64 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h"
62 #include "content/public/browser/interstitial_page.h" 65 #include "content/public/browser/interstitial_page.h"
63 #include "content/public/browser/navigation_entry.h" 66 #include "content/public/browser/navigation_entry.h"
64 #include "content/public/browser/render_frame_host.h" 67 #include "content/public/browser/render_frame_host.h"
65 #include "content/public/browser/web_contents.h" 68 #include "content/public/browser/web_contents.h"
66 #include "content/public/test/browser_test_utils.h" 69 #include "content/public/test/browser_test_utils.h"
70 #include "crypto/sha2.h"
67 #include "net/cookies/cookie_store.h" 71 #include "net/cookies/cookie_store.h"
68 #include "net/cookies/cookie_util.h" 72 #include "net/cookies/cookie_util.h"
69 #include "net/test/embedded_test_server/embedded_test_server.h" 73 #include "net/test/embedded_test_server/embedded_test_server.h"
70 #include "net/test/embedded_test_server/http_request.h" 74 #include "net/test/embedded_test_server/http_request.h"
71 #include "net/test/embedded_test_server/http_response.h" 75 #include "net/test/embedded_test_server/http_response.h"
72 #include "sql/connection.h" 76 #include "sql/connection.h"
73 #include "sql/statement.h" 77 #include "sql/statement.h"
74 #include "testing/gmock/include/gmock/gmock.h" 78 #include "testing/gmock/include/gmock/gmock.h"
75 #include "url/gurl.h" 79 #include "url/gurl.h"
76 80
(...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 686
683 // Waits for pending tasks on the IO thread to complete and check if the 687 // Waits for pending tasks on the IO thread to complete and check if the
684 // SafeBrowsingService enabled state matches |enabled|. 688 // SafeBrowsingService enabled state matches |enabled|.
685 void WaitForIOAndCheckEnabled(SafeBrowsingService* service, bool enabled) { 689 void WaitForIOAndCheckEnabled(SafeBrowsingService* service, bool enabled) {
686 scoped_refptr<ServiceEnabledHelper> enabled_helper(new ServiceEnabledHelper( 690 scoped_refptr<ServiceEnabledHelper> enabled_helper(new ServiceEnabledHelper(
687 service, enabled, 691 service, enabled,
688 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get())); 692 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get()));
689 ASSERT_TRUE(enabled_helper->Run()); 693 ASSERT_TRUE(enabled_helper->Run());
690 } 694 }
691 695
692 private: 696 protected:
693 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_; 697 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_;
694 TestSafeBrowsingDatabaseFactory db_factory_; 698 TestSafeBrowsingDatabaseFactory db_factory_;
695 TestSBProtocolManagerFactory pm_factory_; 699 TestSBProtocolManagerFactory pm_factory_;
696 700
697 // Owned by ContentSubresourceFilterFactory. 701 // Owned by ContentSubresourceFilterFactory.
698 MockSubresourceFilterDriver* driver_; 702 MockSubresourceFilterDriver* driver_;
699 703
704 private:
700 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); 705 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest);
701 }; 706 };
702 707
703 class SafeBrowsingServiceMetadataTest 708 class SafeBrowsingServiceMetadataTest
704 : public SafeBrowsingServiceTest, 709 : public SafeBrowsingServiceTest,
705 public ::testing::WithParamInterface<ThreatPatternType> { 710 public ::testing::WithParamInterface<ThreatPatternType> {
706 public: 711 public:
707 SafeBrowsingServiceMetadataTest() {} 712 SafeBrowsingServiceMetadataTest() {}
708 713
709 void GenUrlFullHashResultWithMetadata(const GURL& url, 714 void GenUrlFullHashResultWithMetadata(const GURL& url,
(...skipping 27 matching lines...) Expand all
737 EXPECT_FALSE(hit_report().is_subresource); 742 EXPECT_FALSE(hit_report().is_subresource);
738 } 743 }
739 744
740 IN_PROC_BROWSER_TEST_P(SafeBrowsingServiceMetadataTest, MalwareIFrame) { 745 IN_PROC_BROWSER_TEST_P(SafeBrowsingServiceMetadataTest, MalwareIFrame) {
741 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); 746 GURL main_url = embedded_test_server()->GetURL(kMalwarePage);
742 GURL iframe_url = embedded_test_server()->GetURL(kMalwareIFrame); 747 GURL iframe_url = embedded_test_server()->GetURL(kMalwareIFrame);
743 748
744 // Add the iframe url as malware and then load the parent page. 749 // Add the iframe url as malware and then load the parent page.
745 SBFullHashResult malware_full_hash; 750 SBFullHashResult malware_full_hash;
746 GenUrlFullHashResultWithMetadata(iframe_url, &malware_full_hash); 751 GenUrlFullHashResultWithMetadata(iframe_url, &malware_full_hash);
747 EXPECT_CALL(observer_, 752 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(iframe_url)))
748 OnSafeBrowsingHit(IsUnsafeResourceFor(iframe_url))).Times(1); 753 .Times(1);
749 SetupResponseForUrl(iframe_url, malware_full_hash); 754 SetupResponseForUrl(iframe_url, malware_full_hash);
750 ui_test_utils::NavigateToURL(browser(), main_url); 755 ui_test_utils::NavigateToURL(browser(), main_url);
751 // All types should show the interstitial. 756 // All types should show the interstitial.
752 EXPECT_TRUE(ShowingInterstitialPage()); 757 EXPECT_TRUE(ShowingInterstitialPage());
753 758
754 EXPECT_TRUE(got_hit_report()); 759 EXPECT_TRUE(got_hit_report());
755 EXPECT_EQ(iframe_url, hit_report().malicious_url); 760 EXPECT_EQ(iframe_url, hit_report().malicious_url);
756 EXPECT_EQ(main_url, hit_report().page_url); 761 EXPECT_EQ(main_url, hit_report().page_url);
757 EXPECT_EQ(GURL(), hit_report().referrer_url); 762 EXPECT_EQ(GURL(), hit_report().referrer_url);
758 EXPECT_TRUE(hit_report().is_subresource); 763 EXPECT_TRUE(hit_report().is_subresource);
(...skipping 1068 matching lines...) Expand 10 before | Expand all | Expand 10 after
1827 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, 1832 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE,
1828 content::Source<SafeBrowsingDatabaseManager>( 1833 content::Source<SafeBrowsingDatabaseManager>(
1829 sb_factory_->test_safe_browsing_service()->database_manager().get())); 1834 sb_factory_->test_safe_browsing_service()->database_manager().get()));
1830 BrowserThread::PostTask( 1835 BrowserThread::PostTask(
1831 BrowserThread::IO, FROM_HERE, 1836 BrowserThread::IO, FROM_HERE,
1832 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, 1837 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate,
1833 base::Unretained(this))); 1838 base::Unretained(this)));
1834 observer.Wait(); 1839 observer.Wait();
1835 } 1840 }
1836 1841
1842 class TestV4Store : public V4Store {
1843 public:
1844 TestV4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner,
1845 const base::FilePath& store_path)
1846 : V4Store(task_runner, store_path, 0) {
1847 LOG(INFO) << "TestV4Store::TestV4Store: store_path: "
1848 << store_path.AsUTF8Unsafe();
Scott Hess - ex-Googler 2017/02/06 22:46:35 Is there any reason to see this on the bots? You
vakh (use Gerrit instead) 2017/02/06 23:30:12 Done.
1849 }
1850
1851 void MarkPrefixAsBad(HashPrefix prefix) {
1852 hash_prefix_map_[prefix.size()] = prefix;
1853 }
1854 };
1855
1856 class TestV4StoreFactory : public V4StoreFactory {
1857 public:
1858 V4Store* CreateV4Store(
1859 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
1860 const base::FilePath& store_path) override {
1861 V4Store* new_store = new TestV4Store(task_runner, store_path);
1862 new_store->Initialize();
1863 return new_store;
1864 }
1865 };
1866
1867 class TestV4Database : public V4Database {
1868 public:
1869 TestV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
1870 std::unique_ptr<StoreMap> store_map)
1871 : V4Database(db_task_runner, std::move(store_map)) {}
1872
1873 void MarkPrefixAsBad(ListIdentifier list_id, HashPrefix prefix) {
1874 V4Store* base_store = store_map_->at(list_id).get();
1875 TestV4Store* test_store = static_cast<TestV4Store*>(base_store);
1876 if (test_store) {
1877 test_store->MarkPrefixAsBad(prefix);
1878 }
1879 }
1880 };
1881
1882 class TestV4DatabaseFactory : public V4DatabaseFactory {
1883 public:
1884 TestV4DatabaseFactory() : v4_db_(nullptr) {}
1885
1886 TestV4Database* Create(
1887 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
1888 std::unique_ptr<StoreMap> store_map) override {
1889 v4_db_ = new TestV4Database(db_task_runner, std::move(store_map));
1890 return v4_db_;
1891 }
1892
1893 void MarkPrefixAsBad(ListIdentifier list_id, HashPrefix prefix) {
1894 v4_db_->MarkPrefixAsBad(list_id, prefix);
1895 }
1896
1897 private:
1898 TestV4Database* v4_db_;
1899 };
1900
1901 class TestV4GetHashProtocolManager : public V4GetHashProtocolManager {
1902 public:
1903 TestV4GetHashProtocolManager(
1904 net::URLRequestContextGetter* request_context_getter,
1905 const StoresToCheck& stores_to_check,
1906 const V4ProtocolConfig& config)
1907 : V4GetHashProtocolManager(request_context_getter,
1908 stores_to_check,
1909 config) {}
1910
1911 void AddToFullHashCache(FullHashInfo fhi) {
1912 full_hash_cache_[fhi.full_hash].full_hash_infos.push_back(fhi);
1913 }
1914 };
1915
1916 class TestV4GetHashProtocolManagerFactory
1917 : public V4GetHashProtocolManagerFactory {
1918 public:
1919 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager(
1920 net::URLRequestContextGetter* request_context_getter,
1921 const StoresToCheck& stores_to_check,
1922 const V4ProtocolConfig& config) override {
1923 pm_ = new TestV4GetHashProtocolManager(request_context_getter,
1924 stores_to_check, config);
1925 return base::WrapUnique(pm_);
Scott Hess - ex-Googler 2017/02/06 22:46:35 Man, this gives me the heebie-jeebies. But ... I
vakh (use Gerrit instead) 2017/02/06 23:30:12 Acknowledged.
1926 }
1927
1928 void AddToFullHashCache(FullHashInfo fhi) { pm_->AddToFullHashCache(fhi); }
1929
1930 private:
1931 TestV4GetHashProtocolManager* pm_;
1932 };
1933
1934 // Tests the safe browsing blocking page in a browser.
1935 class V4SafeBrowsingServiceTest : public SafeBrowsingServiceTest {
1936 public:
1937 V4SafeBrowsingServiceTest() : SafeBrowsingServiceTest() {}
1938
1939 void SetUp() override {
1940 sb_factory_.reset(new TestSafeBrowsingServiceFactory(
1941 true /* v4_enabled */, true /* use_v4_only */));
1942 sb_factory_->SetTestUIManager(new FakeSafeBrowsingUIManager());
1943 SafeBrowsingService::RegisterFactory(sb_factory_.get());
1944 V4Database::RegisterFactory(&v4_db_factory_);
1945 std::unique_ptr<TestV4GetHashProtocolManagerFactory> v4_get_hash_factory =
1946 base::MakeUnique<TestV4GetHashProtocolManagerFactory>();
1947 v4_get_hash_factory_ = v4_get_hash_factory.get();
1948 V4GetHashProtocolManager::RegisterFactory(std::move(v4_get_hash_factory));
1949 InProcessBrowserTest::SetUp();
1950 }
1951
1952 FullHashInfo GetFullHashInfo(const GURL& url, const ListIdentifier& list_id) {
1953 std::string host;
1954 std::string path;
1955 V4ProtocolManagerUtil::CanonicalizeUrl(url, &host, &path, nullptr);
1956
1957 return FullHashInfo(crypto::SHA256HashString(host + path), list_id,
1958 base::Time::Now() + base::TimeDelta::FromMinutes(5));
1959 }
1960
1961 FullHashInfo GetFullHashInfoWithMetadata(
1962 const GURL& url,
1963 const ListIdentifier& list_id,
1964 ThreatPatternType threat_pattern_type) {
1965 FullHashInfo fhi = GetFullHashInfo(url, list_id);
1966 fhi.metadata.threat_pattern_type = threat_pattern_type;
1967 return fhi;
1968 }
1969
1970 void MarkUrlForMalwareUnexpired(const GURL& bad_url,
1971 ThreatPatternType threat_pattern_type) {
1972 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata(
1973 bad_url, GetUrlMalwareId(), threat_pattern_type);
1974
1975 v4_db_factory_.MarkPrefixAsBad(GetUrlMalwareId(), full_hash_info.full_hash);
1976 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1977 }
1978
1979 void MarkUrlForUwsUnexpired(const GURL& bad_url) {
1980 FullHashInfo full_hash_info = GetFullHashInfo(bad_url, GetUrlUwsId());
1981 v4_db_factory_.MarkPrefixAsBad(GetUrlUwsId(), full_hash_info.full_hash);
1982 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1983 }
1984
1985 private:
1986 TestV4DatabaseFactory v4_db_factory_;
1987 TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_;
1988
1989 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceTest);
1990 };
1991
1992 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, UnwantedImgIgnored) {
1993 GURL main_url = embedded_test_server()->GetURL(kMalwarePage);
1994 GURL img_url = embedded_test_server()->GetURL(kMalwareImg);
1995
1996 // Add the img url as coming from a site serving UwS and then load the parent
1997 // page.
1998 MarkUrlForUwsUnexpired(img_url);
1999
2000 ui_test_utils::NavigateToURL(browser(), main_url);
2001
2002 EXPECT_FALSE(ShowingInterstitialPage());
2003 EXPECT_FALSE(got_hit_report());
2004 }
2005
2006 class V4SafeBrowsingServiceMetadataTest
2007 : public V4SafeBrowsingServiceTest,
2008 public ::testing::WithParamInterface<ThreatPatternType> {
2009 public:
2010 V4SafeBrowsingServiceMetadataTest() {}
2011
2012 private:
2013 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceMetadataTest);
2014 };
2015
2016 IN_PROC_BROWSER_TEST_P(V4SafeBrowsingServiceMetadataTest, MalwareMainFrame) {
2017 GURL url = embedded_test_server()->GetURL(kEmptyPage);
2018 MarkUrlForMalwareUnexpired(url, GetParam());
2019
2020 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(1);
2021
2022 ui_test_utils::NavigateToURL(browser(), url);
2023 // All types should show the interstitial.
2024 EXPECT_TRUE(ShowingInterstitialPage());
2025
2026 EXPECT_TRUE(got_hit_report());
2027 EXPECT_EQ(url, hit_report().malicious_url);
2028 EXPECT_EQ(url, hit_report().page_url);
2029 EXPECT_EQ(GURL(), hit_report().referrer_url);
2030 EXPECT_FALSE(hit_report().is_subresource);
2031 }
2032
2033 IN_PROC_BROWSER_TEST_P(V4SafeBrowsingServiceMetadataTest, MalwareIFrame) {
2034 GURL main_url = embedded_test_server()->GetURL(kMalwarePage);
2035 GURL iframe_url = embedded_test_server()->GetURL(kMalwareIFrame);
2036
2037 // Add the iframe url as malware and then load the parent page.
2038 MarkUrlForMalwareUnexpired(iframe_url, GetParam());
2039
2040 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(iframe_url)))
2041 .Times(1);
2042
2043 ui_test_utils::NavigateToURL(browser(), main_url);
2044 // All types should show the interstitial.
2045 EXPECT_TRUE(ShowingInterstitialPage());
2046
2047 EXPECT_TRUE(got_hit_report());
2048 EXPECT_EQ(iframe_url, hit_report().malicious_url);
2049 EXPECT_EQ(main_url, hit_report().page_url);
2050 EXPECT_EQ(GURL(), hit_report().referrer_url);
2051 EXPECT_TRUE(hit_report().is_subresource);
2052 }
2053
2054 IN_PROC_BROWSER_TEST_P(V4SafeBrowsingServiceMetadataTest, MalwareImg) {
2055 GURL main_url = embedded_test_server()->GetURL(kMalwarePage);
2056 GURL img_url = embedded_test_server()->GetURL(kMalwareImg);
2057
2058 // Add the img url as malware and then load the parent page.
2059 MarkUrlForMalwareUnexpired(img_url, GetParam());
2060
2061 switch (GetParam()) {
2062 case ThreatPatternType::NONE: // Falls through.
2063 case ThreatPatternType::MALWARE_DISTRIBUTION:
2064 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(img_url)))
2065 .Times(1);
2066 break;
2067 case ThreatPatternType::MALWARE_LANDING:
2068 // No interstitial shown, so no notifications expected.
2069 break;
2070 default:
2071 break;
2072 }
2073
2074 ui_test_utils::NavigateToURL(browser(), main_url);
2075
2076 // Subresource which is tagged as a landing page should not show an
2077 // interstitial, the other types should.
2078 switch (GetParam()) {
2079 case ThreatPatternType::NONE: // Falls through.
2080 case ThreatPatternType::MALWARE_DISTRIBUTION:
2081 EXPECT_TRUE(ShowingInterstitialPage());
2082 EXPECT_TRUE(got_hit_report());
2083 EXPECT_EQ(img_url, hit_report().malicious_url);
2084 EXPECT_EQ(main_url, hit_report().page_url);
2085 EXPECT_EQ(GURL(), hit_report().referrer_url);
2086 EXPECT_TRUE(hit_report().is_subresource);
2087 break;
2088 case ThreatPatternType::MALWARE_LANDING:
2089 EXPECT_FALSE(ShowingInterstitialPage());
2090 EXPECT_FALSE(got_hit_report());
2091 break;
2092 default:
2093 break;
2094 }
Scott Hess - ex-Googler 2017/02/06 22:46:35 I think this is fine, but I'd be wary of going too
vakh (use Gerrit instead) 2017/02/06 23:30:13 I agree, but these new tests are an exact replica
2095 }
2096
2097 INSTANTIATE_TEST_CASE_P(
2098 MaybeSetMetadata,
2099 V4SafeBrowsingServiceMetadataTest,
2100 testing::Values(ThreatPatternType::NONE,
2101 ThreatPatternType::MALWARE_LANDING,
2102 ThreatPatternType::MALWARE_DISTRIBUTION));
2103
1837 } // namespace safe_browsing 2104 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698