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" |
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 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 SafeBrowsingProtocolManagerDelegate* delegate, | 451 SafeBrowsingProtocolManagerDelegate* delegate, |
448 net::URLRequestContextGetter* request_context_getter, | 452 net::URLRequestContextGetter* request_context_getter, |
449 const SafeBrowsingProtocolConfig& config) override { | 453 const SafeBrowsingProtocolConfig& config) override { |
450 pm_ = new TestProtocolManager(delegate, request_context_getter, config); | 454 pm_ = new TestProtocolManager(delegate, request_context_getter, config); |
451 return base::WrapUnique(pm_); | 455 return base::WrapUnique(pm_); |
452 } | 456 } |
453 | 457 |
454 TestProtocolManager* GetProtocolManager() { return pm_; } | 458 TestProtocolManager* GetProtocolManager() { return pm_; } |
455 | 459 |
456 private: | 460 private: |
457 // Owned by the SafebrowsingService. | 461 // Owned by the SafeBrowsingService. |
458 TestProtocolManager* pm_; | 462 TestProtocolManager* pm_; |
459 }; | 463 }; |
460 | 464 |
461 class MockObserver : public SafeBrowsingUIManager::Observer { | 465 class MockObserver : public SafeBrowsingUIManager::Observer { |
462 public: | 466 public: |
463 MockObserver() {} | 467 MockObserver() {} |
464 virtual ~MockObserver() {} | 468 virtual ~MockObserver() {} |
465 MOCK_METHOD1(OnSafeBrowsingHit, | 469 MOCK_METHOD1(OnSafeBrowsingHit, |
466 void(const security_interstitials::UnsafeResource&)); | 470 void(const security_interstitials::UnsafeResource&)); |
467 }; | 471 }; |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 Loading... |
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 924 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1683 class SafeBrowsingDatabaseManagerCookieTest : public InProcessBrowserTest { | 1688 class SafeBrowsingDatabaseManagerCookieTest : public InProcessBrowserTest { |
1684 public: | 1689 public: |
1685 SafeBrowsingDatabaseManagerCookieTest() {} | 1690 SafeBrowsingDatabaseManagerCookieTest() {} |
1686 | 1691 |
1687 void SetUp() override { | 1692 void SetUp() override { |
1688 // We need to start the test server to get the host&port in the url. | 1693 // We need to start the test server to get the host&port in the url. |
1689 embedded_test_server()->RegisterRequestHandler( | 1694 embedded_test_server()->RegisterRequestHandler( |
1690 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::HandleRequest)); | 1695 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::HandleRequest)); |
1691 ASSERT_TRUE(embedded_test_server()->Start()); | 1696 ASSERT_TRUE(embedded_test_server()->Start()); |
1692 | 1697 |
1693 sb_factory_.reset(new TestSafeBrowsingServiceFactory()); | 1698 sb_factory_ = base::MakeUnique<TestSafeBrowsingServiceFactory>(); |
1694 SetProtocolConfigURLPrefix( | 1699 SetProtocolConfigURLPrefix( |
1695 embedded_test_server()->GetURL("/testpath").spec(), sb_factory_.get()); | 1700 embedded_test_server()->GetURL("/testpath").spec(), sb_factory_.get()); |
1696 SafeBrowsingService::RegisterFactory(sb_factory_.get()); | 1701 SafeBrowsingService::RegisterFactory(sb_factory_.get()); |
1697 | 1702 |
1698 InProcessBrowserTest::SetUp(); | 1703 InProcessBrowserTest::SetUp(); |
1699 } | 1704 } |
1700 | 1705 |
1701 void TearDown() override { | 1706 void TearDown() override { |
1702 InProcessBrowserTest::TearDown(); | 1707 InProcessBrowserTest::TearDown(); |
1703 | 1708 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
| 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::RegisterDatabaseFactoryForTest( |
| 1947 base::WrapUnique(v4_db_factory_)); |
| 1948 |
| 1949 v4_get_hash_factory_ = new TestV4GetHashProtocolManagerFactory(); |
| 1950 V4GetHashProtocolManager::RegisterFactory( |
| 1951 base::WrapUnique(v4_get_hash_factory_)); |
| 1952 InProcessBrowserTest::SetUp(); |
| 1953 } |
| 1954 |
| 1955 void TearDown() override { |
| 1956 InProcessBrowserTest::TearDown(); |
| 1957 |
| 1958 // Unregister test factories after InProcessBrowserTest::TearDown |
| 1959 // (which destructs SafeBrowsingService). |
| 1960 V4GetHashProtocolManager::RegisterFactory(nullptr); |
| 1961 V4Database::RegisterDatabaseFactoryForTest(nullptr); |
| 1962 SafeBrowsingService::RegisterFactory(nullptr); |
| 1963 } |
| 1964 |
| 1965 // Returns a FullHashInfo info for the basic host+path pattern for a given URL |
| 1966 // after canonicalization. |
| 1967 FullHashInfo GetFullHashInfo(const GURL& url, const ListIdentifier& list_id) { |
| 1968 std::string host; |
| 1969 std::string path; |
| 1970 V4ProtocolManagerUtil::CanonicalizeUrl(url, &host, &path, nullptr); |
| 1971 |
| 1972 return FullHashInfo(crypto::SHA256HashString(host + path), list_id, |
| 1973 base::Time::Now() + base::TimeDelta::FromMinutes(5)); |
| 1974 } |
| 1975 |
| 1976 // Returns a FullHashInfo info for the basic host+path pattern for a given URL |
| 1977 // after canonicalization. Also adds metadata information to the FullHashInfo |
| 1978 // object. |
| 1979 FullHashInfo GetFullHashInfoWithMetadata( |
| 1980 const GURL& url, |
| 1981 const ListIdentifier& list_id, |
| 1982 ThreatPatternType threat_pattern_type) { |
| 1983 FullHashInfo fhi = GetFullHashInfo(url, list_id); |
| 1984 fhi.metadata.threat_pattern_type = threat_pattern_type; |
| 1985 return fhi; |
| 1986 } |
| 1987 |
| 1988 // Sets up the prefix database and the full hash cache to match one of the |
| 1989 // prefixes for the given URL and metadata. |
| 1990 void MarkUrlForMalwareUnexpired(const GURL& bad_url, |
| 1991 ThreatPatternType threat_pattern_type) { |
| 1992 FullHashInfo full_hash_info = GetFullHashInfoWithMetadata( |
| 1993 bad_url, GetUrlMalwareId(), threat_pattern_type); |
| 1994 |
| 1995 v4_db_factory_->MarkPrefixAsBad(GetUrlMalwareId(), |
| 1996 full_hash_info.full_hash); |
| 1997 v4_get_hash_factory_->AddToFullHashCache(full_hash_info); |
| 1998 } |
| 1999 |
| 2000 // Sets up the prefix database and the full hash cache to match one of the |
| 2001 // prefixes for the given URL. |
| 2002 void MarkUrlForUwsUnexpired(const GURL& bad_url) { |
| 2003 FullHashInfo full_hash_info = GetFullHashInfo(bad_url, GetUrlUwsId()); |
| 2004 v4_db_factory_->MarkPrefixAsBad(GetUrlUwsId(), full_hash_info.full_hash); |
| 2005 v4_get_hash_factory_->AddToFullHashCache(full_hash_info); |
| 2006 } |
| 2007 |
| 2008 private: |
| 2009 // Owned by the V4Database. |
| 2010 TestV4DatabaseFactory* v4_db_factory_; |
| 2011 // Owned by the V4GetHashProtocolManager. |
| 2012 TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; |
| 2013 |
| 2014 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceTest); |
| 2015 }; |
| 2016 |
| 2017 // Ensures that if an image is marked as UwS, the main page doesn't show an |
| 2018 // interstitial. |
| 2019 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, UnwantedImgIgnored) { |
| 2020 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); |
| 2021 GURL img_url = embedded_test_server()->GetURL(kMalwareImg); |
| 2022 |
| 2023 // Add the img url as coming from a site serving UwS and then load the parent |
| 2024 // page. |
| 2025 MarkUrlForUwsUnexpired(img_url); |
| 2026 |
| 2027 ui_test_utils::NavigateToURL(browser(), main_url); |
| 2028 |
| 2029 EXPECT_FALSE(ShowingInterstitialPage()); |
| 2030 EXPECT_FALSE(got_hit_report()); |
| 2031 } |
| 2032 |
| 2033 // TODO(vakh): Add test for UnwantedMainFrame. |
| 2034 |
| 2035 class V4SafeBrowsingServiceMetadataTest |
| 2036 : public V4SafeBrowsingServiceTest, |
| 2037 public ::testing::WithParamInterface<ThreatPatternType> { |
| 2038 public: |
| 2039 V4SafeBrowsingServiceMetadataTest() {} |
| 2040 |
| 2041 private: |
| 2042 DISALLOW_COPY_AND_ASSIGN(V4SafeBrowsingServiceMetadataTest); |
| 2043 }; |
| 2044 |
| 2045 // Irrespective of the threat_type classification, if the main frame URL is |
| 2046 // marked as Malware, an interstitial should be shown. |
| 2047 IN_PROC_BROWSER_TEST_P(V4SafeBrowsingServiceMetadataTest, MalwareMainFrame) { |
| 2048 GURL url = embedded_test_server()->GetURL(kEmptyPage); |
| 2049 MarkUrlForMalwareUnexpired(url, GetParam()); |
| 2050 |
| 2051 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(1); |
| 2052 |
| 2053 ui_test_utils::NavigateToURL(browser(), url); |
| 2054 // All types should show the interstitial. |
| 2055 EXPECT_TRUE(ShowingInterstitialPage()); |
| 2056 |
| 2057 EXPECT_TRUE(got_hit_report()); |
| 2058 EXPECT_EQ(url, hit_report().malicious_url); |
| 2059 EXPECT_EQ(url, hit_report().page_url); |
| 2060 EXPECT_EQ(GURL(), hit_report().referrer_url); |
| 2061 EXPECT_FALSE(hit_report().is_subresource); |
| 2062 } |
| 2063 |
| 2064 // Irrespective of the threat_type classification, if the iframe URL is marked |
| 2065 // as Malware, an interstitial should be shown. |
| 2066 IN_PROC_BROWSER_TEST_P(V4SafeBrowsingServiceMetadataTest, MalwareIFrame) { |
| 2067 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); |
| 2068 GURL iframe_url = embedded_test_server()->GetURL(kMalwareIFrame); |
| 2069 |
| 2070 // Add the iframe url as malware and then load the parent page. |
| 2071 MarkUrlForMalwareUnexpired(iframe_url, GetParam()); |
| 2072 |
| 2073 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(iframe_url))) |
| 2074 .Times(1); |
| 2075 |
| 2076 ui_test_utils::NavigateToURL(browser(), main_url); |
| 2077 // All types should show the interstitial. |
| 2078 EXPECT_TRUE(ShowingInterstitialPage()); |
| 2079 |
| 2080 EXPECT_TRUE(got_hit_report()); |
| 2081 EXPECT_EQ(iframe_url, hit_report().malicious_url); |
| 2082 EXPECT_EQ(main_url, hit_report().page_url); |
| 2083 EXPECT_EQ(GURL(), hit_report().referrer_url); |
| 2084 EXPECT_TRUE(hit_report().is_subresource); |
| 2085 } |
| 2086 |
| 2087 // Depending on the threat_type classification, if an embedded resource is |
| 2088 // marked as Malware, an interstitial may be shown. |
| 2089 IN_PROC_BROWSER_TEST_P(V4SafeBrowsingServiceMetadataTest, MalwareImg) { |
| 2090 GURL main_url = embedded_test_server()->GetURL(kMalwarePage); |
| 2091 GURL img_url = embedded_test_server()->GetURL(kMalwareImg); |
| 2092 |
| 2093 // Add the img url as malware and then load the parent page. |
| 2094 MarkUrlForMalwareUnexpired(img_url, GetParam()); |
| 2095 |
| 2096 switch (GetParam()) { |
| 2097 case ThreatPatternType::NONE: // Falls through. |
| 2098 case ThreatPatternType::MALWARE_DISTRIBUTION: |
| 2099 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(img_url))) |
| 2100 .Times(1); |
| 2101 break; |
| 2102 case ThreatPatternType::MALWARE_LANDING: |
| 2103 // No interstitial shown, so no notifications expected. |
| 2104 break; |
| 2105 default: |
| 2106 break; |
| 2107 } |
| 2108 |
| 2109 ui_test_utils::NavigateToURL(browser(), main_url); |
| 2110 |
| 2111 // Subresource which is tagged as a landing page should not show an |
| 2112 // interstitial, the other types should. |
| 2113 switch (GetParam()) { |
| 2114 case ThreatPatternType::NONE: // Falls through. |
| 2115 case ThreatPatternType::MALWARE_DISTRIBUTION: |
| 2116 EXPECT_TRUE(ShowingInterstitialPage()); |
| 2117 EXPECT_TRUE(got_hit_report()); |
| 2118 EXPECT_EQ(img_url, hit_report().malicious_url); |
| 2119 EXPECT_EQ(main_url, hit_report().page_url); |
| 2120 EXPECT_EQ(GURL(), hit_report().referrer_url); |
| 2121 EXPECT_TRUE(hit_report().is_subresource); |
| 2122 break; |
| 2123 case ThreatPatternType::MALWARE_LANDING: |
| 2124 EXPECT_FALSE(ShowingInterstitialPage()); |
| 2125 EXPECT_FALSE(got_hit_report()); |
| 2126 break; |
| 2127 default: |
| 2128 break; |
| 2129 } |
| 2130 } |
| 2131 |
| 2132 INSTANTIATE_TEST_CASE_P( |
| 2133 MaybeSetMetadata, |
| 2134 V4SafeBrowsingServiceMetadataTest, |
| 2135 testing::Values(ThreatPatternType::NONE, |
| 2136 ThreatPatternType::MALWARE_LANDING, |
| 2137 ThreatPatternType::MALWARE_DISTRIBUTION)); |
| 2138 |
1837 } // namespace safe_browsing | 2139 } // namespace safe_browsing |
OLD | NEW |