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 19 matching lines...) Expand all Loading... |
30 #include "chrome/browser/browser_process.h" | 30 #include "chrome/browser/browser_process.h" |
31 #include "chrome/browser/chrome_notification_types.h" | 31 #include "chrome/browser/chrome_notification_types.h" |
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/ui_manager.h" | 41 #include "chrome/browser/safe_browsing/ui_manager.h" |
41 #include "chrome/browser/ui/browser.h" | 42 #include "chrome/browser/ui/browser.h" |
42 #include "chrome/browser/ui/browser_navigator_params.h" | 43 #include "chrome/browser/ui/browser_navigator_params.h" |
43 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 44 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
44 #include "chrome/common/chrome_paths.h" | 45 #include "chrome/common/chrome_paths.h" |
45 #include "chrome/common/chrome_switches.h" | 46 #include "chrome/common/chrome_switches.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" |
52 #include "components/safe_browsing_db/database_manager.h" | 53 #include "components/safe_browsing_db/database_manager.h" |
53 #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" |
54 #include "components/safe_browsing_db/util.h" | 56 #include "components/safe_browsing_db/util.h" |
55 #include "content/public/browser/interstitial_page.h" | 57 #include "content/public/browser/interstitial_page.h" |
56 #include "content/public/browser/navigation_entry.h" | 58 #include "content/public/browser/navigation_entry.h" |
57 #include "content/public/browser/render_frame_host.h" | 59 #include "content/public/browser/render_frame_host.h" |
58 #include "content/public/browser/web_contents.h" | 60 #include "content/public/browser/web_contents.h" |
59 #include "content/public/test/browser_test_utils.h" | 61 #include "content/public/test/browser_test_utils.h" |
60 #include "net/cookies/cookie_store.h" | 62 #include "net/cookies/cookie_store.h" |
61 #include "net/cookies/cookie_util.h" | 63 #include "net/cookies/cookie_util.h" |
62 #include "net/test/embedded_test_server/embedded_test_server.h" | 64 #include "net/test/embedded_test_server/embedded_test_server.h" |
63 #include "net/test/embedded_test_server/http_request.h" | 65 #include "net/test/embedded_test_server/http_request.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 return nullptr; | 116 return nullptr; |
115 return base::WrapUnique(new NeverCompletingHttpResponse()); | 117 return base::WrapUnique(new NeverCompletingHttpResponse()); |
116 } | 118 } |
117 | 119 |
118 void InvokeFullHashCallback( | 120 void InvokeFullHashCallback( |
119 SafeBrowsingProtocolManager::FullHashCallback callback, | 121 SafeBrowsingProtocolManager::FullHashCallback callback, |
120 const std::vector<SBFullHashResult>& result) { | 122 const std::vector<SBFullHashResult>& result) { |
121 callback.Run(result, base::TimeDelta::FromMinutes(45)); | 123 callback.Run(result, base::TimeDelta::FromMinutes(45)); |
122 } | 124 } |
123 | 125 |
124 class FakeSafeBrowsingUIManager : public SafeBrowsingUIManager { | 126 // Helper function to set up protocol config. It is used to redirects safe |
| 127 // browsing queries to embeded test server. It needs to be called before |
| 128 // SafeBrowsingService being created, therefore it is preferred to call this |
| 129 // function before InProcessBrowserTest::SetUp(). |
| 130 void SetProtocolConfigURLPrefix(const std::string& url_prefix, |
| 131 TestSafeBrowsingServiceFactory* factory) { |
| 132 SafeBrowsingProtocolConfig config; |
| 133 config.url_prefix = url_prefix; |
| 134 // Makes sure the auto update is not triggered. The tests will force the |
| 135 // update when needed. |
| 136 config.disable_auto_update = true; |
| 137 config.client_name = "browser_tests"; |
| 138 factory->SetTestProtocolConfig(config); |
| 139 } |
| 140 |
| 141 class FakeSafeBrowsingUIManager : public TestSafeBrowsingUIManager { |
125 public: | 142 public: |
126 explicit FakeSafeBrowsingUIManager(SafeBrowsingService* service) | |
127 : SafeBrowsingUIManager(service) {} | |
128 | |
129 void MaybeReportSafeBrowsingHit( | 143 void MaybeReportSafeBrowsingHit( |
130 const safe_browsing::HitReport& hit_report) override { | 144 const safe_browsing::HitReport& hit_report) override { |
131 EXPECT_FALSE(got_hit_report_); | 145 EXPECT_FALSE(got_hit_report_); |
132 got_hit_report_ = true; | 146 got_hit_report_ = true; |
133 hit_report_ = hit_report; | 147 hit_report_ = hit_report; |
134 SafeBrowsingUIManager::MaybeReportSafeBrowsingHit(hit_report); | 148 SafeBrowsingUIManager::MaybeReportSafeBrowsingHit(hit_report); |
135 } | 149 } |
136 | 150 |
137 bool got_hit_report_ = false; | 151 bool got_hit_report_ = false; |
138 safe_browsing::HitReport hit_report_; | 152 safe_browsing::HitReport hit_report_; |
139 | 153 |
140 private: | 154 private: |
141 ~FakeSafeBrowsingUIManager() override {} | 155 ~FakeSafeBrowsingUIManager() override {} |
142 }; | 156 }; |
143 | 157 |
144 class FakeSafeBrowsingService : public SafeBrowsingService { | |
145 public: | |
146 explicit FakeSafeBrowsingService(const std::string& url_prefix) | |
147 : url_prefix_(url_prefix) {} | |
148 | |
149 SafeBrowsingProtocolConfig GetProtocolConfig() const override { | |
150 SafeBrowsingProtocolConfig config; | |
151 config.url_prefix = url_prefix_; | |
152 // Makes sure the auto update is not triggered. The tests will force the | |
153 // update when needed. | |
154 config.disable_auto_update = true; | |
155 config.client_name = "browser_tests"; | |
156 return config; | |
157 } | |
158 | |
159 protected: | |
160 SafeBrowsingUIManager* CreateUIManager() override { | |
161 return new FakeSafeBrowsingUIManager(this); | |
162 } | |
163 | |
164 private: | |
165 ~FakeSafeBrowsingService() override {} | |
166 | |
167 std::string url_prefix_; | |
168 | |
169 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService); | |
170 }; | |
171 | |
172 // Factory that creates FakeSafeBrowsingService instances. | |
173 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { | |
174 public: | |
175 explicit TestSafeBrowsingServiceFactory(const std::string& url_prefix) | |
176 : url_prefix_(url_prefix) {} | |
177 | |
178 SafeBrowsingService* CreateSafeBrowsingService() override { | |
179 return new FakeSafeBrowsingService(url_prefix_); | |
180 } | |
181 | |
182 private: | |
183 std::string url_prefix_; | |
184 }; | |
185 | |
186 // A SafeBrowingDatabase class that allows us to inject the malicious URLs. | 158 // A SafeBrowingDatabase class that allows us to inject the malicious URLs. |
187 class TestSafeBrowsingDatabase : public SafeBrowsingDatabase { | 159 class TestSafeBrowsingDatabase : public SafeBrowsingDatabase { |
188 public: | 160 public: |
189 TestSafeBrowsingDatabase() {} | 161 TestSafeBrowsingDatabase() {} |
190 | 162 |
191 ~TestSafeBrowsingDatabase() override {} | 163 ~TestSafeBrowsingDatabase() override {} |
192 | 164 |
193 // Initializes the database with the given filename. | 165 // Initializes the database with the given filename. |
194 void Init(const base::FilePath& filename) override {} | 166 void Init(const base::FilePath& filename) override {} |
195 | 167 |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 int list_id, | 482 int list_id, |
511 SBFullHashResult* full_hash) { | 483 SBFullHashResult* full_hash) { |
512 std::string host; | 484 std::string host; |
513 std::string path; | 485 std::string path; |
514 CanonicalizeUrl(url, &host, &path, NULL); | 486 CanonicalizeUrl(url, &host, &path, NULL); |
515 full_hash->hash = SBFullHashForString(host + path); | 487 full_hash->hash = SBFullHashForString(host + path); |
516 full_hash->list_id = list_id; | 488 full_hash->list_id = list_id; |
517 } | 489 } |
518 | 490 |
519 void SetUp() override { | 491 void SetUp() override { |
520 // InProcessBrowserTest::SetUp() instantiates SafebrowsingService and | 492 // InProcessBrowserTest::SetUp() instantiates SafebrowsingService. |
521 // RegisterFactory has to be called before SafeBrowsingService is created. | 493 // RegisterFactory and plugging test UI manager / protocol config have to |
522 sb_factory_.reset(new TestSafeBrowsingServiceFactory( | 494 // be called before SafeBrowsingService is created. |
523 "https://definatelynotarealdomain/safebrowsing")); | 495 sb_factory_.reset(new TestSafeBrowsingServiceFactory()); |
| 496 sb_factory_->SetTestUIManager(new FakeSafeBrowsingUIManager()); |
| 497 SetProtocolConfigURLPrefix("https://definatelynotarealdomain/safebrowsing", |
| 498 sb_factory_.get()); |
524 SafeBrowsingService::RegisterFactory(sb_factory_.get()); | 499 SafeBrowsingService::RegisterFactory(sb_factory_.get()); |
525 SafeBrowsingDatabase::RegisterFactory(&db_factory_); | 500 SafeBrowsingDatabase::RegisterFactory(&db_factory_); |
526 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_); | 501 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_); |
527 InProcessBrowserTest::SetUp(); | 502 InProcessBrowserTest::SetUp(); |
528 } | 503 } |
529 | 504 |
530 void TearDown() override { | 505 void TearDown() override { |
531 InProcessBrowserTest::TearDown(); | 506 InProcessBrowserTest::TearDown(); |
532 | 507 |
533 // Unregister test factories after InProcessBrowserTest::TearDown | 508 // Unregister test factories after InProcessBrowserTest::TearDown |
(...skipping 1039 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1573 class SafeBrowsingDatabaseManagerCookieTest : public InProcessBrowserTest { | 1548 class SafeBrowsingDatabaseManagerCookieTest : public InProcessBrowserTest { |
1574 public: | 1549 public: |
1575 SafeBrowsingDatabaseManagerCookieTest() {} | 1550 SafeBrowsingDatabaseManagerCookieTest() {} |
1576 | 1551 |
1577 void SetUp() override { | 1552 void SetUp() override { |
1578 // We need to start the test server to get the host&port in the url. | 1553 // We need to start the test server to get the host&port in the url. |
1579 ASSERT_TRUE(embedded_test_server()->Start()); | 1554 ASSERT_TRUE(embedded_test_server()->Start()); |
1580 embedded_test_server()->RegisterRequestHandler( | 1555 embedded_test_server()->RegisterRequestHandler( |
1581 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::HandleRequest)); | 1556 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::HandleRequest)); |
1582 | 1557 |
1583 // Point to the testing server for all SafeBrowsing requests. | 1558 sb_factory_.reset(new TestSafeBrowsingServiceFactory()); |
1584 GURL url_prefix = embedded_test_server()->GetURL("/testpath"); | 1559 SetProtocolConfigURLPrefix( |
1585 sb_factory_.reset(new TestSafeBrowsingServiceFactory(url_prefix.spec())); | 1560 embedded_test_server()->GetURL("/testpath").spec(), sb_factory_.get()); |
1586 SafeBrowsingService::RegisterFactory(sb_factory_.get()); | 1561 SafeBrowsingService::RegisterFactory(sb_factory_.get()); |
1587 | 1562 |
1588 InProcessBrowserTest::SetUp(); | 1563 InProcessBrowserTest::SetUp(); |
1589 } | 1564 } |
1590 | 1565 |
1591 void TearDown() override { | 1566 void TearDown() override { |
1592 InProcessBrowserTest::TearDown(); | 1567 InProcessBrowserTest::TearDown(); |
1593 | 1568 |
1594 SafeBrowsingService::RegisterFactory(NULL); | 1569 SafeBrowsingService::RegisterFactory(NULL); |
1595 } | 1570 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1629 return false; | 1604 return false; |
1630 } | 1605 } |
1631 if (!smt.BindString(0, embedded_test_server()->base_url().host())) { | 1606 if (!smt.BindString(0, embedded_test_server()->base_url().host())) { |
1632 EXPECT_TRUE(false); | 1607 EXPECT_TRUE(false); |
1633 return false; | 1608 return false; |
1634 } | 1609 } |
1635 if (!smt.Run()) { | 1610 if (!smt.Run()) { |
1636 EXPECT_TRUE(false); | 1611 EXPECT_TRUE(false); |
1637 return false; | 1612 return false; |
1638 } | 1613 } |
1639 | |
1640 return InProcessBrowserTest::SetUpUserDataDirectory(); | 1614 return InProcessBrowserTest::SetUpUserDataDirectory(); |
1641 } | 1615 } |
1642 | 1616 |
1643 void TearDownInProcessBrowserTestFixture() override { | 1617 void TearDownInProcessBrowserTestFixture() override { |
1644 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); | 1618 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); |
1645 | 1619 |
1646 sql::Connection db; | 1620 sql::Connection db; |
1647 base::FilePath cookie_path( | 1621 base::FilePath cookie_path( |
1648 SafeBrowsingService::GetCookieFilePathForTesting()); | 1622 SafeBrowsingService::GetCookieFilePathForTesting()); |
1649 ASSERT_TRUE(db.Open(cookie_path)); | 1623 ASSERT_TRUE(db.Open(cookie_path)); |
1650 | 1624 |
1651 sql::Statement smt( | 1625 sql::Statement smt( |
1652 db.GetUniqueStatement("SELECT name, value FROM cookies ORDER BY name")); | 1626 db.GetUniqueStatement("SELECT name, value FROM cookies ORDER BY name")); |
1653 ASSERT_TRUE(smt.is_valid()); | 1627 ASSERT_TRUE(smt.is_valid()); |
1654 | |
1655 ASSERT_TRUE(smt.Step()); | 1628 ASSERT_TRUE(smt.Step()); |
1656 ASSERT_EQ("a", smt.ColumnString(0)); | 1629 ASSERT_EQ("a", smt.ColumnString(0)); |
1657 ASSERT_EQ("b", smt.ColumnString(1)); | 1630 ASSERT_EQ("b", smt.ColumnString(1)); |
1658 ASSERT_TRUE(smt.Step()); | 1631 ASSERT_TRUE(smt.Step()); |
1659 ASSERT_EQ("c", smt.ColumnString(0)); | 1632 ASSERT_EQ("c", smt.ColumnString(0)); |
1660 ASSERT_EQ("d", smt.ColumnString(1)); | 1633 ASSERT_EQ("d", smt.ColumnString(1)); |
1661 EXPECT_FALSE(smt.Step()); | 1634 EXPECT_FALSE(smt.Step()); |
1662 } | 1635 } |
1663 | 1636 |
1664 void SetUpOnMainThread() override { | 1637 void ForceUpdate() { |
1665 sb_service_ = g_browser_process->safe_browsing_service(); | 1638 sb_factory_->test_safe_browsing_service() |
1666 ASSERT_TRUE(sb_service_.get() != NULL); | 1639 ->protocol_manager() |
| 1640 ->ForceScheduleNextUpdate(base::TimeDelta::FromSeconds(0)); |
1667 } | 1641 } |
1668 | 1642 |
1669 void TearDownOnMainThread() override { sb_service_ = NULL; } | 1643 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_; |
1670 | 1644 |
1671 void ForceUpdate() { | 1645 protected: |
1672 sb_service_->protocol_manager()->ForceScheduleNextUpdate( | |
1673 base::TimeDelta::FromSeconds(0)); | |
1674 } | |
1675 | |
1676 scoped_refptr<SafeBrowsingService> sb_service_; | |
1677 | |
1678 private: | |
1679 static std::unique_ptr<net::test_server::HttpResponse> HandleRequest( | 1646 static std::unique_ptr<net::test_server::HttpResponse> HandleRequest( |
1680 const net::test_server::HttpRequest& request) { | 1647 const net::test_server::HttpRequest& request) { |
1681 if (!base::StartsWith(request.relative_url, "/testpath/", | 1648 if (!base::StartsWith(request.relative_url, "/testpath/", |
1682 base::CompareCase::SENSITIVE)) { | 1649 base::CompareCase::SENSITIVE)) { |
1683 ADD_FAILURE() << "bad path"; | 1650 ADD_FAILURE() << "bad path"; |
1684 return nullptr; | 1651 return nullptr; |
1685 } | 1652 } |
1686 | 1653 |
1687 auto cookie_it = request.headers.find("Cookie"); | 1654 auto cookie_it = request.headers.find("Cookie"); |
1688 if (cookie_it == request.headers.end()) { | 1655 if (cookie_it == request.headers.end()) { |
(...skipping 17 matching lines...) Expand all Loading... |
1706 | 1673 |
1707 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( | 1674 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( |
1708 new net::test_server::BasicHttpResponse()); | 1675 new net::test_server::BasicHttpResponse()); |
1709 http_response->set_content("foo"); | 1676 http_response->set_content("foo"); |
1710 http_response->set_content_type("text/plain"); | 1677 http_response->set_content_type("text/plain"); |
1711 http_response->AddCustomHeader( | 1678 http_response->AddCustomHeader( |
1712 "Set-Cookie", "c=d; Expires=Fri, 01 Jan 2038 01:01:01 GMT"); | 1679 "Set-Cookie", "c=d; Expires=Fri, 01 Jan 2038 01:01:01 GMT"); |
1713 return std::move(http_response); | 1680 return std::move(http_response); |
1714 } | 1681 } |
1715 | 1682 |
1716 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_; | |
1717 | |
1718 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManagerCookieTest); | 1683 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManagerCookieTest); |
1719 }; | 1684 }; |
1720 | 1685 |
1721 // Test that a Local Safe Browsing database update request both sends cookies | 1686 // Test that a Local Safe Browsing database update request both sends cookies |
1722 // and can save cookies. | 1687 // and can save cookies. |
1723 IN_PROC_BROWSER_TEST_F(SafeBrowsingDatabaseManagerCookieTest, | 1688 IN_PROC_BROWSER_TEST_F(SafeBrowsingDatabaseManagerCookieTest, |
1724 TestSBUpdateCookies) { | 1689 TestSBUpdateCookies) { |
1725 content::WindowedNotificationObserver observer( | 1690 content::WindowedNotificationObserver observer( |
1726 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | 1691 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
1727 content::Source<SafeBrowsingDatabaseManager>( | 1692 content::Source<SafeBrowsingDatabaseManager>( |
1728 sb_service_->database_manager().get())); | 1693 sb_factory_->test_safe_browsing_service()->database_manager().get())); |
1729 BrowserThread::PostTask( | 1694 BrowserThread::PostTask( |
1730 BrowserThread::IO, FROM_HERE, | 1695 BrowserThread::IO, FROM_HERE, |
1731 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); | 1696 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); |
1732 observer.Wait(); | 1697 observer.Wait(); |
1733 } | 1698 } |
1734 | 1699 |
1735 } // namespace safe_browsing | 1700 } // namespace safe_browsing |
OLD | NEW |