| OLD | NEW |
| 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 Loading... |
| 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" | |
| 60 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | 57 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 61 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver.h" | 58 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver.h" |
| 62 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" | 59 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" |
| 63 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" | 60 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" |
| 64 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" | 61 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" |
| 65 #include "content/public/browser/interstitial_page.h" | 62 #include "content/public/browser/interstitial_page.h" |
| 66 #include "content/public/browser/navigation_entry.h" | 63 #include "content/public/browser/navigation_entry.h" |
| 67 #include "content/public/browser/render_frame_host.h" | 64 #include "content/public/browser/render_frame_host.h" |
| 68 #include "content/public/browser/web_contents.h" | 65 #include "content/public/browser/web_contents.h" |
| 69 #include "content/public/test/browser_test_utils.h" | 66 #include "content/public/test/browser_test_utils.h" |
| 70 #include "crypto/sha2.h" | |
| 71 #include "net/cookies/cookie_store.h" | 67 #include "net/cookies/cookie_store.h" |
| 72 #include "net/cookies/cookie_util.h" | 68 #include "net/cookies/cookie_util.h" |
| 73 #include "net/test/embedded_test_server/embedded_test_server.h" | 69 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 74 #include "net/test/embedded_test_server/http_request.h" | 70 #include "net/test/embedded_test_server/http_request.h" |
| 75 #include "net/test/embedded_test_server/http_response.h" | 71 #include "net/test/embedded_test_server/http_response.h" |
| 76 #include "sql/connection.h" | 72 #include "sql/connection.h" |
| 77 #include "sql/statement.h" | 73 #include "sql/statement.h" |
| 78 #include "testing/gmock/include/gmock/gmock.h" | 74 #include "testing/gmock/include/gmock/gmock.h" |
| 79 #include "url/gurl.h" | 75 #include "url/gurl.h" |
| 80 | 76 |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 SafeBrowsingProtocolManagerDelegate* delegate, | 447 SafeBrowsingProtocolManagerDelegate* delegate, |
| 452 net::URLRequestContextGetter* request_context_getter, | 448 net::URLRequestContextGetter* request_context_getter, |
| 453 const SafeBrowsingProtocolConfig& config) override { | 449 const SafeBrowsingProtocolConfig& config) override { |
| 454 pm_ = new TestProtocolManager(delegate, request_context_getter, config); | 450 pm_ = new TestProtocolManager(delegate, request_context_getter, config); |
| 455 return base::WrapUnique(pm_); | 451 return base::WrapUnique(pm_); |
| 456 } | 452 } |
| 457 | 453 |
| 458 TestProtocolManager* GetProtocolManager() { return pm_; } | 454 TestProtocolManager* GetProtocolManager() { return pm_; } |
| 459 | 455 |
| 460 private: | 456 private: |
| 461 // Owned by the SafeBrowsingService. | 457 // Owned by the SafebrowsingService. |
| 462 TestProtocolManager* pm_; | 458 TestProtocolManager* pm_; |
| 463 }; | 459 }; |
| 464 | 460 |
| 465 class MockObserver : public SafeBrowsingUIManager::Observer { | 461 class MockObserver : public SafeBrowsingUIManager::Observer { |
| 466 public: | 462 public: |
| 467 MockObserver() {} | 463 MockObserver() {} |
| 468 virtual ~MockObserver() {} | 464 virtual ~MockObserver() {} |
| 469 MOCK_METHOD1(OnSafeBrowsingHit, | 465 MOCK_METHOD1(OnSafeBrowsingHit, |
| 470 void(const security_interstitials::UnsafeResource&)); | 466 void(const security_interstitials::UnsafeResource&)); |
| 471 }; | 467 }; |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 | 682 |
| 687 // Waits for pending tasks on the IO thread to complete and check if the | 683 // Waits for pending tasks on the IO thread to complete and check if the |
| 688 // SafeBrowsingService enabled state matches |enabled|. | 684 // SafeBrowsingService enabled state matches |enabled|. |
| 689 void WaitForIOAndCheckEnabled(SafeBrowsingService* service, bool enabled) { | 685 void WaitForIOAndCheckEnabled(SafeBrowsingService* service, bool enabled) { |
| 690 scoped_refptr<ServiceEnabledHelper> enabled_helper(new ServiceEnabledHelper( | 686 scoped_refptr<ServiceEnabledHelper> enabled_helper(new ServiceEnabledHelper( |
| 691 service, enabled, | 687 service, enabled, |
| 692 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get())); | 688 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get())); |
| 693 ASSERT_TRUE(enabled_helper->Run()); | 689 ASSERT_TRUE(enabled_helper->Run()); |
| 694 } | 690 } |
| 695 | 691 |
| 696 protected: | 692 private: |
| 697 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_; | 693 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_; |
| 698 TestSafeBrowsingDatabaseFactory db_factory_; | 694 TestSafeBrowsingDatabaseFactory db_factory_; |
| 699 TestSBProtocolManagerFactory pm_factory_; | 695 TestSBProtocolManagerFactory pm_factory_; |
| 700 | 696 |
| 701 // Owned by ContentSubresourceFilterFactory. | 697 // Owned by ContentSubresourceFilterFactory. |
| 702 MockSubresourceFilterDriver* driver_; | 698 MockSubresourceFilterDriver* driver_; |
| 703 | 699 |
| 704 private: | |
| 705 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); | 700 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); |
| 706 }; | 701 }; |
| 707 | 702 |
| 708 class SafeBrowsingServiceMetadataTest | 703 class SafeBrowsingServiceMetadataTest |
| 709 : public SafeBrowsingServiceTest, | 704 : public SafeBrowsingServiceTest, |
| 710 public ::testing::WithParamInterface<ThreatPatternType> { | 705 public ::testing::WithParamInterface<ThreatPatternType> { |
| 711 public: | 706 public: |
| 712 SafeBrowsingServiceMetadataTest() {} | 707 SafeBrowsingServiceMetadataTest() {} |
| 713 | 708 |
| 714 void GenUrlFullHashResultWithMetadata(const GURL& url, | 709 void GenUrlFullHashResultWithMetadata(const GURL& url, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 742 EXPECT_FALSE(hit_report().is_subresource); | 737 EXPECT_FALSE(hit_report().is_subresource); |
| 743 } | 738 } |
| 744 | 739 |
| 745 IN_PROC_BROWSER_TEST_P(SafeBrowsingServiceMetadataTest, MalwareIFrame) { | 740 IN_PROC_BROWSER_TEST_P(SafeBrowsingServiceMetadataTest, MalwareIFrame) { |
| 746 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); | 741 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); |
| 747 GURL iframe_url = embedded_test_server()->GetURL(kMalwareIFrame); | 742 GURL iframe_url = embedded_test_server()->GetURL(kMalwareIFrame); |
| 748 | 743 |
| 749 // Add the iframe url as malware and then load the parent page. | 744 // Add the iframe url as malware and then load the parent page. |
| 750 SBFullHashResult malware_full_hash; | 745 SBFullHashResult malware_full_hash; |
| 751 GenUrlFullHashResultWithMetadata(iframe_url, &malware_full_hash); | 746 GenUrlFullHashResultWithMetadata(iframe_url, &malware_full_hash); |
| 752 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(iframe_url))) | 747 EXPECT_CALL(observer_, |
| 753 .Times(1); | 748 OnSafeBrowsingHit(IsUnsafeResourceFor(iframe_url))).Times(1); |
| 754 SetupResponseForUrl(iframe_url, malware_full_hash); | 749 SetupResponseForUrl(iframe_url, malware_full_hash); |
| 755 ui_test_utils::NavigateToURL(browser(), main_url); | 750 ui_test_utils::NavigateToURL(browser(), main_url); |
| 756 // All types should show the interstitial. | 751 // All types should show the interstitial. |
| 757 EXPECT_TRUE(ShowingInterstitialPage()); | 752 EXPECT_TRUE(ShowingInterstitialPage()); |
| 758 | 753 |
| 759 EXPECT_TRUE(got_hit_report()); | 754 EXPECT_TRUE(got_hit_report()); |
| 760 EXPECT_EQ(iframe_url, hit_report().malicious_url); | 755 EXPECT_EQ(iframe_url, hit_report().malicious_url); |
| 761 EXPECT_EQ(main_url, hit_report().page_url); | 756 EXPECT_EQ(main_url, hit_report().page_url); |
| 762 EXPECT_EQ(GURL(), hit_report().referrer_url); | 757 EXPECT_EQ(GURL(), hit_report().referrer_url); |
| 763 EXPECT_TRUE(hit_report().is_subresource); | 758 EXPECT_TRUE(hit_report().is_subresource); |
| (...skipping 924 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1688 class SafeBrowsingDatabaseManagerCookieTest : public InProcessBrowserTest { | 1683 class SafeBrowsingDatabaseManagerCookieTest : public InProcessBrowserTest { |
| 1689 public: | 1684 public: |
| 1690 SafeBrowsingDatabaseManagerCookieTest() {} | 1685 SafeBrowsingDatabaseManagerCookieTest() {} |
| 1691 | 1686 |
| 1692 void SetUp() override { | 1687 void SetUp() override { |
| 1693 // We need to start the test server to get the host&port in the url. | 1688 // We need to start the test server to get the host&port in the url. |
| 1694 embedded_test_server()->RegisterRequestHandler( | 1689 embedded_test_server()->RegisterRequestHandler( |
| 1695 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::HandleRequest)); | 1690 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::HandleRequest)); |
| 1696 ASSERT_TRUE(embedded_test_server()->Start()); | 1691 ASSERT_TRUE(embedded_test_server()->Start()); |
| 1697 | 1692 |
| 1698 sb_factory_ = base::MakeUnique<TestSafeBrowsingServiceFactory>(); | 1693 sb_factory_.reset(new TestSafeBrowsingServiceFactory()); |
| 1699 SetProtocolConfigURLPrefix( | 1694 SetProtocolConfigURLPrefix( |
| 1700 embedded_test_server()->GetURL("/testpath").spec(), sb_factory_.get()); | 1695 embedded_test_server()->GetURL("/testpath").spec(), sb_factory_.get()); |
| 1701 SafeBrowsingService::RegisterFactory(sb_factory_.get()); | 1696 SafeBrowsingService::RegisterFactory(sb_factory_.get()); |
| 1702 | 1697 |
| 1703 InProcessBrowserTest::SetUp(); | 1698 InProcessBrowserTest::SetUp(); |
| 1704 } | 1699 } |
| 1705 | 1700 |
| 1706 void TearDown() override { | 1701 void TearDown() override { |
| 1707 InProcessBrowserTest::TearDown(); | 1702 InProcessBrowserTest::TearDown(); |
| 1708 | 1703 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1832 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | 1827 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
| 1833 content::Source<SafeBrowsingDatabaseManager>( | 1828 content::Source<SafeBrowsingDatabaseManager>( |
| 1834 sb_factory_->test_safe_browsing_service()->database_manager().get())); | 1829 sb_factory_->test_safe_browsing_service()->database_manager().get())); |
| 1835 BrowserThread::PostTask( | 1830 BrowserThread::PostTask( |
| 1836 BrowserThread::IO, FROM_HERE, | 1831 BrowserThread::IO, FROM_HERE, |
| 1837 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, | 1832 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, |
| 1838 base::Unretained(this))); | 1833 base::Unretained(this))); |
| 1839 observer.Wait(); | 1834 observer.Wait(); |
| 1840 } | 1835 } |
| 1841 | 1836 |
| 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 | |
| 1848 void MarkPrefixAsBad(HashPrefix prefix) { | |
| 1849 hash_prefix_map_[prefix.size()] = prefix; | |
| 1850 } | |
| 1851 }; | |
| 1852 | |
| 1853 class TestV4StoreFactory : public V4StoreFactory { | |
| 1854 public: | |
| 1855 V4Store* CreateV4Store( | |
| 1856 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
| 1857 const base::FilePath& store_path) override { | |
| 1858 V4Store* new_store = new TestV4Store(task_runner, store_path); | |
| 1859 new_store->Initialize(); | |
| 1860 return new_store; | |
| 1861 } | |
| 1862 }; | |
| 1863 | |
| 1864 class TestV4Database : public V4Database { | |
| 1865 public: | |
| 1866 TestV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | |
| 1867 std::unique_ptr<StoreMap> store_map) | |
| 1868 : V4Database(db_task_runner, std::move(store_map)) {} | |
| 1869 | |
| 1870 void MarkPrefixAsBad(ListIdentifier list_id, HashPrefix prefix) { | |
| 1871 V4Store* base_store = store_map_->at(list_id).get(); | |
| 1872 TestV4Store* test_store = static_cast<TestV4Store*>(base_store); | |
| 1873 test_store->MarkPrefixAsBad(prefix); | |
| 1874 } | |
| 1875 }; | |
| 1876 | |
| 1877 class TestV4DatabaseFactory : public V4DatabaseFactory { | |
| 1878 public: | |
| 1879 TestV4DatabaseFactory() : v4_db_(nullptr) {} | |
| 1880 | |
| 1881 std::unique_ptr<V4Database> Create( | |
| 1882 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | |
| 1883 std::unique_ptr<StoreMap> store_map) override { | |
| 1884 v4_db_ = new TestV4Database(db_task_runner, std::move(store_map)); | |
| 1885 return base::WrapUnique(v4_db_); | |
| 1886 } | |
| 1887 | |
| 1888 void MarkPrefixAsBad(ListIdentifier list_id, HashPrefix prefix) { | |
| 1889 v4_db_->MarkPrefixAsBad(list_id, prefix); | |
| 1890 } | |
| 1891 | |
| 1892 private: | |
| 1893 // Owned by V4LocalDatabaseManager. Each test in the V4SafeBrowsingServiceTest | |
| 1894 // class instantiates a new SafebrowsingService instance, which instantiates a | |
| 1895 // new V4LocalDatabaseManager, which instantiates a new V4Database using this | |
| 1896 // method so use-after-free isn't possible. | |
| 1897 TestV4Database* v4_db_; | |
| 1898 }; | |
| 1899 | |
| 1900 class TestV4GetHashProtocolManager : public V4GetHashProtocolManager { | |
| 1901 public: | |
| 1902 TestV4GetHashProtocolManager( | |
| 1903 net::URLRequestContextGetter* request_context_getter, | |
| 1904 const StoresToCheck& stores_to_check, | |
| 1905 const V4ProtocolConfig& config) | |
| 1906 : V4GetHashProtocolManager(request_context_getter, | |
| 1907 stores_to_check, | |
| 1908 config) {} | |
| 1909 | |
| 1910 void AddToFullHashCache(FullHashInfo fhi) { | |
| 1911 full_hash_cache_[fhi.full_hash].full_hash_infos.push_back(fhi); | |
| 1912 } | |
| 1913 }; | |
| 1914 | |
| 1915 class TestV4GetHashProtocolManagerFactory | |
| 1916 : public V4GetHashProtocolManagerFactory { | |
| 1917 public: | |
| 1918 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager( | |
| 1919 net::URLRequestContextGetter* request_context_getter, | |
| 1920 const StoresToCheck& stores_to_check, | |
| 1921 const V4ProtocolConfig& config) override { | |
| 1922 pm_ = new TestV4GetHashProtocolManager(request_context_getter, | |
| 1923 stores_to_check, config); | |
| 1924 return base::WrapUnique(pm_); | |
| 1925 } | |
| 1926 | |
| 1927 void AddToFullHashCache(FullHashInfo fhi) { pm_->AddToFullHashCache(fhi); } | |
| 1928 | |
| 1929 private: | |
| 1930 // Owned by the SafeBrowsingService. | |
| 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_ = base::MakeUnique<TestSafeBrowsingServiceFactory>( | |
| 1941 V4FeatureList::V4UsageStatus::V4_ONLY); | |
| 1942 sb_factory_->SetTestUIManager(new FakeSafeBrowsingUIManager()); | |
| 1943 SafeBrowsingService::RegisterFactory(sb_factory_.get()); | |
| 1944 | |
| 1945 v4_db_factory_ = new TestV4DatabaseFactory(); | |
| 1946 V4Database::RegisterFactory(base::WrapUnique(v4_db_factory_)); | |
| 1947 | |
| 1948 v4_get_hash_factory_ = new TestV4GetHashProtocolManagerFactory(); | |
| 1949 V4GetHashProtocolManager::RegisterFactory( | |
| 1950 base::WrapUnique(v4_get_hash_factory_)); | |
| 1951 InProcessBrowserTest::SetUp(); | |
| 1952 } | |
| 1953 | |
| 1954 void TearDown() override { | |
| 1955 InProcessBrowserTest::TearDown(); | |
| 1956 | |
| 1957 // Unregister test factories after InProcessBrowserTest::TearDown | |
| 1958 // (which destructs SafeBrowsingService). | |
| 1959 V4GetHashProtocolManager::RegisterFactory(nullptr); | |
| 1960 V4Database::RegisterFactory(nullptr); | |
| 1961 SafeBrowsingService::RegisterFactory(nullptr); | |
| 1962 } | |
| 1963 | |
| 1964 // Returns a FullHashInfo info for the basic host+path pattern for a given URL | |
| 1965 // after canonicalization. | |
| 1966 FullHashInfo GetFullHashInfo(const GURL& url, const ListIdentifier& list_id) { | |
| 1967 std::string host; | |
| 1968 std::string path; | |
| 1969 V4ProtocolManagerUtil::CanonicalizeUrl(url, &host, &path, nullptr); | |
| 1970 | |
| 1971 return FullHashInfo(crypto::SHA256HashString(host + path), list_id, | |
| 1972 base::Time::Now() + base::TimeDelta::FromMinutes(5)); | |
| 1973 } | |
| 1974 | |
| 1975 // Returns a FullHashInfo info for the basic host+path pattern for a given URL | |
| 1976 // after canonicalization. Also adds metadata information to the FullHashInfo | |
| 1977 // object. | |
| 1978 FullHashInfo GetFullHashInfoWithMetadata( | |
| 1979 const GURL& url, | |
| 1980 const ListIdentifier& list_id, | |
| 1981 ThreatPatternType threat_pattern_type) { | |
| 1982 FullHashInfo fhi = GetFullHashInfo(url, list_id); | |
| 1983 fhi.metadata.threat_pattern_type = threat_pattern_type; | |
| 1984 return fhi; | |
| 1985 } | |
| 1986 | |
| 1987 // Sets up the prefix database and the full hash cache to match one of the | |
| 1988 // prefixes for the given URL and metadata. | |
| 1989 void MarkUrlForMalwareUnexpired(const GURL& bad_url, | |
| 1990 ThreatPatternType threat_pattern_type) { | |
| 1991 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata( | |
| 1992 bad_url, GetUrlMalwareId(), threat_pattern_type); | |
| 1993 | |
| 1994 v4_db_factory_->MarkPrefixAsBad(GetUrlMalwareId(), | |
| 1995 full_hash_info.full_hash); | |
| 1996 v4_get_hash_factory_->AddToFullHashCache(full_hash_info); | |
| 1997 } | |
| 1998 | |
| 1999 // Sets up the prefix database and the full hash cache to match one of the | |
| 2000 // prefixes for the given URL. | |
| 2001 void MarkUrlForUwsUnexpired(const GURL& bad_url) { | |
| 2002 FullHashInfo full_hash_info = GetFullHashInfo(bad_url, GetUrlUwsId()); | |
| 2003 v4_db_factory_->MarkPrefixAsBad(GetUrlUwsId(), full_hash_info.full_hash); | |
| 2004 v4_get_hash_factory_->AddToFullHashCache(full_hash_info); | |
| 2005 } | |
| 2006 | |
| 2007 private: | |
| 2008 // Owned by the V4Database. | |
| 2009 TestV4DatabaseFactory* v4_db_factory_; | |
| 2010 // Owned by the V4GetHashProtocolManager. | |
| 2011 TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; | |
| 2012 | |
| 2013 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceTest); | |
| 2014 }; | |
| 2015 | |
| 2016 // Ensures that if an image is marked as UwS, the main page doesn't show an | |
| 2017 // interstitial. | |
| 2018 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, UnwantedImgIgnored) { | |
| 2019 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); | |
| 2020 GURL img_url = embedded_test_server()->GetURL(kMalwareImg); | |
| 2021 | |
| 2022 // Add the img url as coming from a site serving UwS and then load the parent | |
| 2023 // page. | |
| 2024 MarkUrlForUwsUnexpired(img_url); | |
| 2025 | |
| 2026 ui_test_utils::NavigateToURL(browser(), main_url); | |
| 2027 | |
| 2028 EXPECT_FALSE(ShowingInterstitialPage()); | |
| 2029 EXPECT_FALSE(got_hit_report()); | |
| 2030 } | |
| 2031 | |
| 2032 // TODO(vakh): Add test for UnwantedMainFrame. | |
| 2033 | |
| 2034 class V4SafeBrowsingServiceMetadataTest | |
| 2035 : public V4SafeBrowsingServiceTest, | |
| 2036 public ::testing::WithParamInterface<ThreatPatternType> { | |
| 2037 public: | |
| 2038 V4SafeBrowsingServiceMetadataTest() {} | |
| 2039 | |
| 2040 private: | |
| 2041 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceMetadataTest); | |
| 2042 }; | |
| 2043 | |
| 2044 // Irrespective of the threat_type classification, if the main frame URL is | |
| 2045 // marked as Malware, an interstitial should be shown. | |
| 2046 IN_PROC_BROWSER_TEST_P(V4SafeBrowsingServiceMetadataTest, MalwareMainFrame) { | |
| 2047 GURL url = embedded_test_server()->GetURL(kEmptyPage); | |
| 2048 MarkUrlForMalwareUnexpired(url, GetParam()); | |
| 2049 | |
| 2050 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(1); | |
| 2051 | |
| 2052 ui_test_utils::NavigateToURL(browser(), url); | |
| 2053 // All types should show the interstitial. | |
| 2054 EXPECT_TRUE(ShowingInterstitialPage()); | |
| 2055 | |
| 2056 EXPECT_TRUE(got_hit_report()); | |
| 2057 EXPECT_EQ(url, hit_report().malicious_url); | |
| 2058 EXPECT_EQ(url, hit_report().page_url); | |
| 2059 EXPECT_EQ(GURL(), hit_report().referrer_url); | |
| 2060 EXPECT_FALSE(hit_report().is_subresource); | |
| 2061 } | |
| 2062 | |
| 2063 // Irrespective of the threat_type classification, if the iframe URL is marked | |
| 2064 // as Malware, an interstitial should be shown. | |
| 2065 IN_PROC_BROWSER_TEST_P(V4SafeBrowsingServiceMetadataTest, MalwareIFrame) { | |
| 2066 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); | |
| 2067 GURL iframe_url = embedded_test_server()->GetURL(kMalwareIFrame); | |
| 2068 | |
| 2069 // Add the iframe url as malware and then load the parent page. | |
| 2070 MarkUrlForMalwareUnexpired(iframe_url, GetParam()); | |
| 2071 | |
| 2072 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(iframe_url))) | |
| 2073 .Times(1); | |
| 2074 | |
| 2075 ui_test_utils::NavigateToURL(browser(), main_url); | |
| 2076 // All types should show the interstitial. | |
| 2077 EXPECT_TRUE(ShowingInterstitialPage()); | |
| 2078 | |
| 2079 EXPECT_TRUE(got_hit_report()); | |
| 2080 EXPECT_EQ(iframe_url, hit_report().malicious_url); | |
| 2081 EXPECT_EQ(main_url, hit_report().page_url); | |
| 2082 EXPECT_EQ(GURL(), hit_report().referrer_url); | |
| 2083 EXPECT_TRUE(hit_report().is_subresource); | |
| 2084 } | |
| 2085 | |
| 2086 // Depending on the threat_type classification, if an embedded resource is | |
| 2087 // marked as Malware, an interstitial may be shown. | |
| 2088 IN_PROC_BROWSER_TEST_P(V4SafeBrowsingServiceMetadataTest, MalwareImg) { | |
| 2089 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); | |
| 2090 GURL img_url = embedded_test_server()->GetURL(kMalwareImg); | |
| 2091 | |
| 2092 // Add the img url as malware and then load the parent page. | |
| 2093 MarkUrlForMalwareUnexpired(img_url, GetParam()); | |
| 2094 | |
| 2095 switch (GetParam()) { | |
| 2096 case ThreatPatternType::NONE: // Falls through. | |
| 2097 case ThreatPatternType::MALWARE_DISTRIBUTION: | |
| 2098 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(img_url))) | |
| 2099 .Times(1); | |
| 2100 break; | |
| 2101 case ThreatPatternType::MALWARE_LANDING: | |
| 2102 // No interstitial shown, so no notifications expected. | |
| 2103 break; | |
| 2104 default: | |
| 2105 break; | |
| 2106 } | |
| 2107 | |
| 2108 ui_test_utils::NavigateToURL(browser(), main_url); | |
| 2109 | |
| 2110 // Subresource which is tagged as a landing page should not show an | |
| 2111 // interstitial, the other types should. | |
| 2112 switch (GetParam()) { | |
| 2113 case ThreatPatternType::NONE: // Falls through. | |
| 2114 case ThreatPatternType::MALWARE_DISTRIBUTION: | |
| 2115 EXPECT_TRUE(ShowingInterstitialPage()); | |
| 2116 EXPECT_TRUE(got_hit_report()); | |
| 2117 EXPECT_EQ(img_url, hit_report().malicious_url); | |
| 2118 EXPECT_EQ(main_url, hit_report().page_url); | |
| 2119 EXPECT_EQ(GURL(), hit_report().referrer_url); | |
| 2120 EXPECT_TRUE(hit_report().is_subresource); | |
| 2121 break; | |
| 2122 case ThreatPatternType::MALWARE_LANDING: | |
| 2123 EXPECT_FALSE(ShowingInterstitialPage()); | |
| 2124 EXPECT_FALSE(got_hit_report()); | |
| 2125 break; | |
| 2126 default: | |
| 2127 break; | |
| 2128 } | |
| 2129 } | |
| 2130 | |
| 2131 INSTANTIATE_TEST_CASE_P( | |
| 2132 MaybeSetMetadata, | |
| 2133 V4SafeBrowsingServiceMetadataTest, | |
| 2134 testing::Values(ThreatPatternType::NONE, | |
| 2135 ThreatPatternType::MALWARE_LANDING, | |
| 2136 ThreatPatternType::MALWARE_DISTRIBUTION)); | |
| 2137 | |
| 2138 } // namespace safe_browsing | 1837 } // namespace safe_browsing |
| OLD | NEW |