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

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

Issue 2690413002: Refactor safe browsing service tests. (Closed)
Patch Set: addressed comments Created 3 years, 9 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 | « chrome/browser/BUILD.gn ('k') | chrome/browser/safe_browsing/v4_test_utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // This test creates a safebrowsing service using test safebrowsing database 5 // This test creates a safebrowsing service using test safebrowsing database
6 // and a test protocol manager. It is used to test logics in safebrowsing 6 // and a test protocol manager. It is used to test logics in safebrowsing
7 // service. 7 // service.
8 8
9 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 9 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
10 10
(...skipping 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/subresource_filter/test_ruleset_publisher.h" 43 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h"
43 #include "chrome/browser/ui/browser.h" 44 #include "chrome/browser/ui/browser.h"
44 #include "chrome/browser/ui/browser_navigator_params.h" 45 #include "chrome/browser/ui/browser_navigator_params.h"
45 #include "chrome/browser/ui/tabs/tab_strip_model.h" 46 #include "chrome/browser/ui/tabs/tab_strip_model.h"
46 #include "chrome/common/chrome_paths.h" 47 #include "chrome/common/chrome_paths.h"
47 #include "chrome/common/pref_names.h" 48 #include "chrome/common/pref_names.h"
48 #include "chrome/test/base/in_process_browser_test.h" 49 #include "chrome/test/base/in_process_browser_test.h"
49 #include "chrome/test/base/ui_test_utils.h" 50 #include "chrome/test/base/ui_test_utils.h"
50 #include "components/bookmarks/browser/startup_task_runner_service.h" 51 #include "components/bookmarks/browser/startup_task_runner_service.h"
51 #include "components/content_settings/core/browser/host_content_settings_map.h" 52 #include "components/content_settings/core/browser/host_content_settings_map.h"
(...skipping 1720 matching lines...) Expand 10 before | Expand all | Expand 10 after
1772 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, 1773 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE,
1773 content::Source<SafeBrowsingDatabaseManager>( 1774 content::Source<SafeBrowsingDatabaseManager>(
1774 sb_factory_->test_safe_browsing_service()->database_manager().get())); 1775 sb_factory_->test_safe_browsing_service()->database_manager().get()));
1775 BrowserThread::PostTask( 1776 BrowserThread::PostTask(
1776 BrowserThread::IO, FROM_HERE, 1777 BrowserThread::IO, FROM_HERE,
1777 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, 1778 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate,
1778 base::Unretained(this))); 1779 base::Unretained(this)));
1779 observer.Wait(); 1780 observer.Wait();
1780 } 1781 }
1781 1782
1782 class TestV4Store : public V4Store {
1783 public:
1784 TestV4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner,
1785 const base::FilePath& store_path)
1786 : V4Store(task_runner, store_path, 0) {}
1787
1788 bool HasValidData() const override { return true; }
1789
1790 void MarkPrefixAsBad(HashPrefix prefix) {
1791 hash_prefix_map_[prefix.size()] = prefix;
1792 }
1793 };
1794
1795 class TestV4StoreFactory : public V4StoreFactory {
1796 public:
1797 V4Store* CreateV4Store(
1798 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
1799 const base::FilePath& store_path) override {
1800 V4Store* new_store = new TestV4Store(task_runner, store_path);
1801 new_store->Initialize();
1802 return new_store;
1803 }
1804 };
1805
1806 class TestV4Database : public V4Database {
1807 public:
1808 TestV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
1809 std::unique_ptr<StoreMap> store_map)
1810 : V4Database(db_task_runner, std::move(store_map)) {}
1811
1812 void MarkPrefixAsBad(ListIdentifier list_id, HashPrefix prefix) {
1813 V4Store* base_store = store_map_->at(list_id).get();
1814 TestV4Store* test_store = static_cast<TestV4Store*>(base_store);
1815 test_store->MarkPrefixAsBad(prefix);
1816 }
1817 };
1818
1819 class TestV4DatabaseFactory : public V4DatabaseFactory {
1820 public:
1821 TestV4DatabaseFactory() : v4_db_(nullptr) {}
1822
1823 std::unique_ptr<V4Database> Create(
1824 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
1825 std::unique_ptr<StoreMap> store_map) override {
1826 v4_db_ = new TestV4Database(db_task_runner, std::move(store_map));
1827 return base::WrapUnique(v4_db_);
1828 }
1829
1830 void MarkPrefixAsBad(ListIdentifier list_id, HashPrefix prefix) {
1831 v4_db_->MarkPrefixAsBad(list_id, prefix);
1832 }
1833
1834 private:
1835 // Owned by V4LocalDatabaseManager. Each test in the V4SafeBrowsingServiceTest
1836 // class instantiates a new SafebrowsingService instance, which instantiates a
1837 // new V4LocalDatabaseManager, which instantiates a new V4Database using this
1838 // method so use-after-free isn't possible.
1839 TestV4Database* v4_db_;
1840 };
1841
1842 class TestV4GetHashProtocolManager : public V4GetHashProtocolManager {
1843 public:
1844 TestV4GetHashProtocolManager(
1845 net::URLRequestContextGetter* request_context_getter,
1846 const StoresToCheck& stores_to_check,
1847 const V4ProtocolConfig& config)
1848 : V4GetHashProtocolManager(request_context_getter,
1849 stores_to_check,
1850 config) {}
1851
1852 void AddToFullHashCache(FullHashInfo fhi) {
1853 full_hash_cache_[fhi.full_hash].full_hash_infos.push_back(fhi);
1854 }
1855 };
1856
1857 class TestV4GetHashProtocolManagerFactory
1858 : public V4GetHashProtocolManagerFactory {
1859 public:
1860 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager(
1861 net::URLRequestContextGetter* request_context_getter,
1862 const StoresToCheck& stores_to_check,
1863 const V4ProtocolConfig& config) override {
1864 pm_ = new TestV4GetHashProtocolManager(request_context_getter,
1865 stores_to_check, config);
1866 return base::WrapUnique(pm_);
1867 }
1868
1869 void AddToFullHashCache(FullHashInfo fhi) { pm_->AddToFullHashCache(fhi); }
1870
1871 private:
1872 // Owned by the SafeBrowsingService.
1873 TestV4GetHashProtocolManager* pm_;
1874 };
1875
1876 // Tests the safe browsing blocking page in a browser. 1783 // Tests the safe browsing blocking page in a browser.
1877 class V4SafeBrowsingServiceTest : public SafeBrowsingServiceTest { 1784 class V4SafeBrowsingServiceTest : public SafeBrowsingServiceTest {
1878 public: 1785 public:
1879 V4SafeBrowsingServiceTest() : SafeBrowsingServiceTest() {} 1786 V4SafeBrowsingServiceTest() : SafeBrowsingServiceTest() {}
1880 1787
1881 void SetUp() override { 1788 void SetUp() override {
1882 sb_factory_ = base::MakeUnique<TestSafeBrowsingServiceFactory>( 1789 sb_factory_ = base::MakeUnique<TestSafeBrowsingServiceFactory>(
1883 V4FeatureList::V4UsageStatus::V4_ONLY); 1790 V4FeatureList::V4UsageStatus::V4_ONLY);
1884 sb_factory_->SetTestUIManager(new FakeSafeBrowsingUIManager()); 1791 sb_factory_->SetTestUIManager(new FakeSafeBrowsingUIManager());
1885 SafeBrowsingService::RegisterFactory(sb_factory_.get()); 1792 SafeBrowsingService::RegisterFactory(sb_factory_.get());
(...skipping 16 matching lines...) Expand all
1902 InProcessBrowserTest::TearDown(); 1809 InProcessBrowserTest::TearDown();
1903 1810
1904 // Unregister test factories after InProcessBrowserTest::TearDown 1811 // Unregister test factories after InProcessBrowserTest::TearDown
1905 // (which destructs SafeBrowsingService). 1812 // (which destructs SafeBrowsingService).
1906 V4GetHashProtocolManager::RegisterFactory(nullptr); 1813 V4GetHashProtocolManager::RegisterFactory(nullptr);
1907 V4Database::RegisterDatabaseFactoryForTest(nullptr); 1814 V4Database::RegisterDatabaseFactoryForTest(nullptr);
1908 V4Database::RegisterStoreFactoryForTest(nullptr); 1815 V4Database::RegisterStoreFactoryForTest(nullptr);
1909 SafeBrowsingService::RegisterFactory(nullptr); 1816 SafeBrowsingService::RegisterFactory(nullptr);
1910 } 1817 }
1911 1818
1912 // Returns a FullHash for the basic host+path pattern for a given URL after 1819 void MarkUrlForListIdUnexpired(const GURL& bad_url,
1913 // canonicalization. 1820 const ListIdentifier& list_id,
1914 FullHash GetFullHash(const GURL& url) { 1821 ThreatPatternType threat_pattern_type) {
1915 std::string host; 1822 FullHashInfo full_hash_info =
1916 std::string path; 1823 GetFullHashInfoWithMetadata(bad_url, list_id, threat_pattern_type);
1917 V4ProtocolManagerUtil::CanonicalizeUrl(url, &host, &path, nullptr); 1824 v4_db_factory_->MarkPrefixAsBad(list_id, full_hash_info.full_hash);
1918 1825 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1919 return crypto::SHA256HashString(host + path);
1920 }
1921
1922 // Returns FullHashInfo object for the basic host+path pattern for a given URL
1923 // after canonicalization.
1924 FullHashInfo GetFullHashInfo(const GURL& url, const ListIdentifier& list_id) {
1925 return FullHashInfo(GetFullHash(url), list_id,
1926 base::Time::Now() + base::TimeDelta::FromMinutes(5));
1927 }
1928
1929 // Returns a FullHashInfo info for the basic host+path pattern for a given URL
1930 // after canonicalization. Also adds metadata information to the FullHashInfo
1931 // object.
1932 FullHashInfo GetFullHashInfoWithMetadata(
1933 const GURL& url,
1934 const ListIdentifier& list_id,
1935 ThreatPatternType threat_pattern_type) {
1936 FullHashInfo fhi = GetFullHashInfo(url, list_id);
1937 fhi.metadata.threat_pattern_type = threat_pattern_type;
1938 return fhi;
1939 } 1826 }
1940 1827
1941 // Sets up the prefix database and the full hash cache to match one of the 1828 // Sets up the prefix database and the full hash cache to match one of the
1942 // prefixes for the given URL and metadata. 1829 // prefixes for the given URL and metadata.
1943 void MarkUrlForMalwareUnexpired( 1830 void MarkUrlForMalwareUnexpired(
1944 const GURL& bad_url, 1831 const GURL& bad_url,
1945 ThreatPatternType threat_pattern_type = ThreatPatternType::NONE) { 1832 ThreatPatternType threat_pattern_type = ThreatPatternType::NONE) {
1946 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata( 1833 MarkUrlForListIdUnexpired(bad_url, GetUrlMalwareId(), threat_pattern_type);
1947 bad_url, GetUrlMalwareId(), threat_pattern_type);
1948
1949 v4_db_factory_->MarkPrefixAsBad(GetUrlMalwareId(),
1950 full_hash_info.full_hash);
1951 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1952 } 1834 }
1953 1835
1954 // Sets up the prefix database and the full hash cache to match one of the 1836 // Sets up the prefix database and the full hash cache to match one of the
1955 // prefixes for the given URL in the UwS store. 1837 // prefixes for the given URL in the UwS store.
1956 void MarkUrlForUwsUnexpired(const GURL& bad_url) { 1838 void MarkUrlForUwsUnexpired(const GURL& bad_url) {
1957 FullHashInfo full_hash_info = GetFullHashInfo(bad_url, GetUrlUwsId()); 1839 MarkUrlForListIdUnexpired(bad_url, GetUrlUwsId(), ThreatPatternType::NONE);
1958 v4_db_factory_->MarkPrefixAsBad(GetUrlUwsId(), full_hash_info.full_hash);
1959 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1960 } 1840 }
1961 1841
1962 // Sets up the prefix database and the full hash cache to match one of the 1842 // Sets up the prefix database and the full hash cache to match one of the
1963 // prefixes for the given URL in the phishing store. 1843 // prefixes for the given URL in the phishing store.
1964 void MarkUrlForPhishingUnexpired(const GURL& bad_url, 1844 void MarkUrlForPhishingUnexpired(const GURL& bad_url,
1965 ThreatPatternType threat_pattern_type) { 1845 ThreatPatternType threat_pattern_type) {
1966 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata( 1846 MarkUrlForListIdUnexpired(bad_url, GetUrlSocEngId(), threat_pattern_type);
1967 bad_url, GetUrlSocEngId(), threat_pattern_type);
1968
1969 v4_db_factory_->MarkPrefixAsBad(GetUrlSocEngId(), full_hash_info.full_hash);
1970 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1971 } 1847 }
1972 1848
1973 // Sets up the prefix database and the full hash cache to match one of the 1849 // Sets up the prefix database and the full hash cache to match one of the
1974 // prefixes for the given URL in the malware binary store. 1850 // prefixes for the given URL in the malware binary store.
1975 void MarkUrlForMalwareBinaryUnexpired(const GURL& bad_url) { 1851 void MarkUrlForMalwareBinaryUnexpired(const GURL& bad_url) {
1976 FullHashInfo full_hash_info = GetFullHashInfo(bad_url, GetUrlMalBinId()); 1852 MarkUrlForListIdUnexpired(bad_url, GetUrlMalBinId(),
1977 v4_db_factory_->MarkPrefixAsBad(GetUrlMalBinId(), full_hash_info.full_hash); 1853 ThreatPatternType::NONE);
1978 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1979 } 1854 }
1980 1855
1981 // Sets up the prefix database and the full hash cache to match one of the 1856 // Sets up the prefix database and the full hash cache to match one of the
1982 // prefixes for the given URL in the client incident store. 1857 // prefixes for the given URL in the client incident store.
1983 void MarkUrlForResourceUnexpired(const GURL& bad_url) { 1858 void MarkUrlForResourceUnexpired(const GURL& bad_url) {
1984 FullHashInfo full_hash_info = 1859 MarkUrlForListIdUnexpired(bad_url, GetChromeUrlClientIncidentId(),
1985 GetFullHashInfo(bad_url, GetChromeUrlClientIncidentId()); 1860 ThreatPatternType::NONE);
1986 v4_db_factory_->MarkPrefixAsBad(GetChromeUrlClientIncidentId(),
1987 full_hash_info.full_hash);
1988 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
1989 } 1861 }
1990 1862
1991 private: 1863 private:
1992 // Owned by the V4Database. 1864 // Owned by the V4Database.
1993 TestV4DatabaseFactory* v4_db_factory_; 1865 TestV4DatabaseFactory* v4_db_factory_;
1994 // Owned by the V4GetHashProtocolManager. 1866 // Owned by the V4GetHashProtocolManager.
1995 TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; 1867 TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_;
1996 // Owned by the V4Database. 1868 // Owned by the V4Database.
1997 TestV4StoreFactory* store_factory_; 1869 TestV4StoreFactory* store_factory_;
1998 1870
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after
2615 } 2487 }
2616 2488
2617 INSTANTIATE_TEST_CASE_P( 2489 INSTANTIATE_TEST_CASE_P(
2618 MaybeSetMetadata, 2490 MaybeSetMetadata,
2619 V4SafeBrowsingServiceMetadataTest, 2491 V4SafeBrowsingServiceMetadataTest,
2620 testing::Values(ThreatPatternType::NONE, 2492 testing::Values(ThreatPatternType::NONE,
2621 ThreatPatternType::MALWARE_LANDING, 2493 ThreatPatternType::MALWARE_LANDING,
2622 ThreatPatternType::MALWARE_DISTRIBUTION)); 2494 ThreatPatternType::MALWARE_DISTRIBUTION));
2623 2495
2624 } // namespace safe_browsing 2496 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/BUILD.gn ('k') | chrome/browser/safe_browsing/v4_test_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698