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

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

Issue 2690413002: Refactor safe browsing service tests. (Closed)
Patch Set: rename 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 21 matching lines...) Expand all
32 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 32 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
33 #include "chrome/browser/prerender/prerender_manager.h" 33 #include "chrome/browser/prerender/prerender_manager.h"
34 #include "chrome/browser/profiles/profile.h" 34 #include "chrome/browser/profiles/profile.h"
35 #include "chrome/browser/profiles/profile_manager.h" 35 #include "chrome/browser/profiles/profile_manager.h"
36 #include "chrome/browser/safe_browsing/client_side_detection_service.h" 36 #include "chrome/browser/safe_browsing/client_side_detection_service.h"
37 #include "chrome/browser/safe_browsing/local_database_manager.h" 37 #include "chrome/browser/safe_browsing/local_database_manager.h"
38 #include "chrome/browser/safe_browsing/protocol_manager.h" 38 #include "chrome/browser/safe_browsing/protocol_manager.h"
39 #include "chrome/browser/safe_browsing/safe_browsing_database.h" 39 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
40 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" 40 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
41 #include "chrome/browser/safe_browsing/ui_manager.h" 41 #include "chrome/browser/safe_browsing/ui_manager.h"
42 #include "chrome/browser/safe_browsing/v4_test_utils.h"
42 #include "chrome/browser/ui/browser.h" 43 #include "chrome/browser/ui/browser.h"
43 #include "chrome/browser/ui/browser_navigator_params.h" 44 #include "chrome/browser/ui/browser_navigator_params.h"
44 #include "chrome/browser/ui/tabs/tab_strip_model.h" 45 #include "chrome/browser/ui/tabs/tab_strip_model.h"
45 #include "chrome/common/chrome_paths.h" 46 #include "chrome/common/chrome_paths.h"
46 #include "chrome/common/pref_names.h" 47 #include "chrome/common/pref_names.h"
47 #include "chrome/test/base/in_process_browser_test.h" 48 #include "chrome/test/base/in_process_browser_test.h"
48 #include "chrome/test/base/ui_test_utils.h" 49 #include "chrome/test/base/ui_test_utils.h"
49 #include "components/bookmarks/browser/startup_task_runner_service.h" 50 #include "components/bookmarks/browser/startup_task_runner_service.h"
50 #include "components/content_settings/core/browser/host_content_settings_map.h" 51 #include "components/content_settings/core/browser/host_content_settings_map.h"
51 #include "components/prefs/pref_service.h" 52 #include "components/prefs/pref_service.h"
(...skipping 1752 matching lines...) Expand 10 before | Expand all | Expand 10 after
1804 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, 1805 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE,
1805 content::Source<SafeBrowsingDatabaseManager>( 1806 content::Source<SafeBrowsingDatabaseManager>(
1806 sb_factory_->test_safe_browsing_service()->database_manager().get())); 1807 sb_factory_->test_safe_browsing_service()->database_manager().get()));
1807 BrowserThread::PostTask( 1808 BrowserThread::PostTask(
1808 BrowserThread::IO, FROM_HERE, 1809 BrowserThread::IO, FROM_HERE,
1809 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, 1810 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate,
1810 base::Unretained(this))); 1811 base::Unretained(this)));
1811 observer.Wait(); 1812 observer.Wait();
1812 } 1813 }
1813 1814
1814 class TestV4Store : public V4Store {
1815 public:
1816 TestV4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner,
1817 const base::FilePath& store_path)
1818 : V4Store(task_runner, store_path, 0) {}
1819
1820 bool HasValidData() const override { return true; }
1821
1822 void MarkPrefixAsBad(HashPrefix prefix) {
1823 hash_prefix_map_[prefix.size()] = prefix;
1824 }
1825 };
1826
1827 class TestV4StoreFactory : public V4StoreFactory {
1828 public:
1829 V4Store* CreateV4Store(
1830 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
1831 const base::FilePath& store_path) override {
1832 V4Store* new_store = new TestV4Store(task_runner, store_path);
1833 new_store->Initialize();
1834 return new_store;
1835 }
1836 };
1837
1838 class TestV4Database : public V4Database {
1839 public:
1840 TestV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
1841 std::unique_ptr<StoreMap> store_map)
1842 : V4Database(db_task_runner, std::move(store_map)) {}
1843
1844 void MarkPrefixAsBad(ListIdentifier list_id, HashPrefix prefix) {
1845 V4Store* base_store = store_map_->at(list_id).get();
1846 TestV4Store* test_store = static_cast<TestV4Store*>(base_store);
1847 test_store->MarkPrefixAsBad(prefix);
1848 }
1849 };
1850
1851 class TestV4DatabaseFactory : public V4DatabaseFactory {
1852 public:
1853 TestV4DatabaseFactory() : v4_db_(nullptr) {}
1854
1855 std::unique_ptr<V4Database> Create(
1856 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
1857 std::unique_ptr<StoreMap> store_map) override {
1858 v4_db_ = new TestV4Database(db_task_runner, std::move(store_map));
1859 return base::WrapUnique(v4_db_);
1860 }
1861
1862 void MarkPrefixAsBad(ListIdentifier list_id, HashPrefix prefix) {
1863 v4_db_->MarkPrefixAsBad(list_id, prefix);
1864 }
1865
1866 private:
1867 // Owned by V4LocalDatabaseManager. Each test in the V4SafeBrowsingServiceTest
1868 // class instantiates a new SafebrowsingService instance, which instantiates a
1869 // new V4LocalDatabaseManager, which instantiates a new V4Database using this
1870 // method so use-after-free isn't possible.
1871 TestV4Database* v4_db_;
1872 };
1873
1874 class TestV4GetHashProtocolManager : public V4GetHashProtocolManager {
1875 public:
1876 TestV4GetHashProtocolManager(
1877 net::URLRequestContextGetter* request_context_getter,
1878 const StoresToCheck& stores_to_check,
1879 const V4ProtocolConfig& config)
1880 : V4GetHashProtocolManager(request_context_getter,
1881 stores_to_check,
1882 config) {}
1883
1884 void AddToFullHashCache(FullHashInfo fhi) {
1885 full_hash_cache_[fhi.full_hash].full_hash_infos.push_back(fhi);
1886 }
1887 };
1888
1889 class TestV4GetHashProtocolManagerFactory
1890 : public V4GetHashProtocolManagerFactory {
1891 public:
1892 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager(
1893 net::URLRequestContextGetter* request_context_getter,
1894 const StoresToCheck& stores_to_check,
1895 const V4ProtocolConfig& config) override {
1896 pm_ = new TestV4GetHashProtocolManager(request_context_getter,
1897 stores_to_check, config);
1898 return base::WrapUnique(pm_);
1899 }
1900
1901 void AddToFullHashCache(FullHashInfo fhi) { pm_->AddToFullHashCache(fhi); }
1902
1903 private:
1904 // Owned by the SafeBrowsingService.
1905 TestV4GetHashProtocolManager* pm_;
1906 };
1907
1908 // Tests the safe browsing blocking page in a browser. 1815 // Tests the safe browsing blocking page in a browser.
1909 class V4SafeBrowsingServiceTest : public SafeBrowsingServiceTest { 1816 class V4SafeBrowsingServiceTest : public SafeBrowsingServiceTest {
1910 public: 1817 public:
1911 V4SafeBrowsingServiceTest() : SafeBrowsingServiceTest() {} 1818 V4SafeBrowsingServiceTest() : SafeBrowsingServiceTest() {}
1912 1819
1913 void SetUp() override { 1820 void SetUp() override {
1914 sb_factory_ = base::MakeUnique<TestSafeBrowsingServiceFactory>( 1821 sb_factory_ = base::MakeUnique<TestSafeBrowsingServiceFactory>(
1915 V4FeatureList::V4UsageStatus::V4_ONLY); 1822 V4FeatureList::V4UsageStatus::V4_ONLY);
1916 sb_factory_->SetTestUIManager(new FakeSafeBrowsingUIManager()); 1823 sb_factory_->SetTestUIManager(new FakeSafeBrowsingUIManager());
1917 SafeBrowsingService::RegisterFactory(sb_factory_.get()); 1824 SafeBrowsingService::RegisterFactory(sb_factory_.get());
(...skipping 16 matching lines...) Expand all
1934 InProcessBrowserTest::TearDown(); 1841 InProcessBrowserTest::TearDown();
1935 1842
1936 // Unregister test factories after InProcessBrowserTest::TearDown 1843 // Unregister test factories after InProcessBrowserTest::TearDown
1937 // (which destructs SafeBrowsingService). 1844 // (which destructs SafeBrowsingService).
1938 V4GetHashProtocolManager::RegisterFactory(nullptr); 1845 V4GetHashProtocolManager::RegisterFactory(nullptr);
1939 V4Database::RegisterDatabaseFactoryForTest(nullptr); 1846 V4Database::RegisterDatabaseFactoryForTest(nullptr);
1940 V4Database::RegisterStoreFactoryForTest(nullptr); 1847 V4Database::RegisterStoreFactoryForTest(nullptr);
1941 SafeBrowsingService::RegisterFactory(nullptr); 1848 SafeBrowsingService::RegisterFactory(nullptr);
1942 } 1849 }
1943 1850
1944 // Returns a FullHash for the basic host+path pattern for a given URL after 1851 void MarkUrlForListIdUnexpired(const GURL& bad_url,
1945 // canonicalization. 1852 const ListIdentifier& list_id,
1946 FullHash GetFullHash(const GURL& url) { 1853 ThreatPatternType threat_pattern_type) {
1947 std::string host; 1854 FullHashInfo full_hash_info =
1948 std::string path; 1855 GetFullHashInfoWithMetadata(bad_url, list_id, threat_pattern_type);
1949 V4ProtocolManagerUtil::CanonicalizeUrl(url, &host, &path, nullptr); 1856 v4_db_factory_->MarkPrefixAsBad(list_id, full_hash_info.full_hash);
1950 1857 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1951 return crypto::SHA256HashString(host + path);
1952 }
1953
1954 // Returns FullHashInfo object for the basic host+path pattern for a given URL
1955 // after canonicalization.
1956 FullHashInfo GetFullHashInfo(const GURL& url, const ListIdentifier& list_id) {
1957 return FullHashInfo(GetFullHash(url), list_id,
1958 base::Time::Now() + base::TimeDelta::FromMinutes(5));
1959 }
1960
1961 // Returns a FullHashInfo info for the basic host+path pattern for a given URL
1962 // after canonicalization. Also adds metadata information to the FullHashInfo
1963 // object.
1964 FullHashInfo GetFullHashInfoWithMetadata(
1965 const GURL& url,
1966 const ListIdentifier& list_id,
1967 ThreatPatternType threat_pattern_type) {
1968 FullHashInfo fhi = GetFullHashInfo(url, list_id);
1969 fhi.metadata.threat_pattern_type = threat_pattern_type;
1970 return fhi;
1971 } 1858 }
1972 1859
1973 // Sets up the prefix database and the full hash cache to match one of the 1860 // Sets up the prefix database and the full hash cache to match one of the
1974 // prefixes for the given URL and metadata. 1861 // prefixes for the given URL and metadata.
1975 void MarkUrlForMalwareUnexpired( 1862 void MarkUrlForMalwareUnexpired(
1976 const GURL& bad_url, 1863 const GURL& bad_url,
1977 ThreatPatternType threat_pattern_type = ThreatPatternType::NONE) { 1864 ThreatPatternType threat_pattern_type = ThreatPatternType::NONE) {
1978 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata( 1865 MarkUrlForListIdUnexpired(bad_url, GetUrlMalwareId(), threat_pattern_type);
1979 bad_url, GetUrlMalwareId(), threat_pattern_type);
1980
1981 v4_db_factory_->MarkPrefixAsBad(GetUrlMalwareId(),
1982 full_hash_info.full_hash);
1983 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1984 } 1866 }
1985 1867
1986 // Sets up the prefix database and the full hash cache to match one of the 1868 // Sets up the prefix database and the full hash cache to match one of the
1987 // prefixes for the given URL in the UwS store. 1869 // prefixes for the given URL in the UwS store.
1988 void MarkUrlForUwsUnexpired(const GURL& bad_url) { 1870 void MarkUrlForUwsUnexpired(const GURL& bad_url) {
1989 FullHashInfo full_hash_info = GetFullHashInfo(bad_url, GetUrlUwsId()); 1871 MarkUrlForListIdUnexpired(bad_url, GetUrlUwsId(), ThreatPatternType::NONE);
1990 v4_db_factory_->MarkPrefixAsBad(GetUrlUwsId(), full_hash_info.full_hash);
1991 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1992 } 1872 }
1993 1873
1994 // Sets up the prefix database and the full hash cache to match one of the 1874 // Sets up the prefix database and the full hash cache to match one of the
1995 // prefixes for the given URL in the phishing store. 1875 // prefixes for the given URL in the phishing store.
1996 void MarkUrlForPhishingUnexpired(const GURL& bad_url, 1876 void MarkUrlForPhishingUnexpired(const GURL& bad_url,
1997 ThreatPatternType threat_pattern_type) { 1877 ThreatPatternType threat_pattern_type) {
1998 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata( 1878 MarkUrlForListIdUnexpired(bad_url, GetUrlSocEngId(), threat_pattern_type);
1999 bad_url, GetUrlSocEngId(), threat_pattern_type);
2000
2001 v4_db_factory_->MarkPrefixAsBad(GetUrlSocEngId(), full_hash_info.full_hash);
2002 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
2003 } 1879 }
2004 1880
2005 // Sets up the prefix database and the full hash cache to match one of the 1881 // Sets up the prefix database and the full hash cache to match one of the
2006 // prefixes for the given URL in the malware binary store. 1882 // prefixes for the given URL in the malware binary store.
2007 void MarkUrlForMalwareBinaryUnexpired(const GURL& bad_url) { 1883 void MarkUrlForMalwareBinaryUnexpired(const GURL& bad_url) {
2008 FullHashInfo full_hash_info = GetFullHashInfo(bad_url, GetUrlMalBinId()); 1884 MarkUrlForListIdUnexpired(bad_url, GetUrlMalBinId(),
2009 v4_db_factory_->MarkPrefixAsBad(GetUrlMalBinId(), full_hash_info.full_hash); 1885 ThreatPatternType::NONE);
2010 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
2011 } 1886 }
2012 1887
2013 // Sets up the prefix database and the full hash cache to match one of the 1888 // Sets up the prefix database and the full hash cache to match one of the
2014 // prefixes for the given URL in the client incident store. 1889 // prefixes for the given URL in the client incident store.
2015 void MarkUrlForResourceUnexpired(const GURL& bad_url) { 1890 void MarkUrlForResourceUnexpired(const GURL& bad_url) {
2016 FullHashInfo full_hash_info = 1891 MarkUrlForListIdUnexpired(bad_url, GetChromeUrlClientIncidentId(),
2017 GetFullHashInfo(bad_url, GetChromeUrlClientIncidentId()); 1892 ThreatPatternType::NONE);
2018 v4_db_factory_->MarkPrefixAsBad(GetChromeUrlClientIncidentId(),
2019 full_hash_info.full_hash);
2020 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
2021 } 1893 }
2022 1894
2023 private: 1895 private:
2024 // Owned by the V4Database. 1896 // Owned by the V4Database.
2025 TestV4DatabaseFactory* v4_db_factory_; 1897 TestV4DatabaseFactory* v4_db_factory_;
2026 // Owned by the V4GetHashProtocolManager. 1898 // Owned by the V4GetHashProtocolManager.
2027 TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; 1899 TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_;
2028 // Owned by the V4Database. 1900 // Owned by the V4Database.
2029 TestV4StoreFactory* store_factory_; 1901 TestV4StoreFactory* store_factory_;
2030 1902
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after
2657 } 2529 }
2658 2530
2659 INSTANTIATE_TEST_CASE_P( 2531 INSTANTIATE_TEST_CASE_P(
2660 MaybeSetMetadata, 2532 MaybeSetMetadata,
2661 V4SafeBrowsingServiceMetadataTest, 2533 V4SafeBrowsingServiceMetadataTest,
2662 testing::Values(ThreatPatternType::NONE, 2534 testing::Values(ThreatPatternType::NONE,
2663 ThreatPatternType::MALWARE_LANDING, 2535 ThreatPatternType::MALWARE_LANDING,
2664 ThreatPatternType::MALWARE_DISTRIBUTION)); 2536 ThreatPatternType::MALWARE_DISTRIBUTION));
2665 2537
2666 } // namespace safe_browsing 2538 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698