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

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

Issue 1943993006: Create test fixture for SafeBrowsingService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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 19 matching lines...) Expand all
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
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 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 int list_id, 485 int list_id,
514 SBFullHashResult* full_hash) { 486 SBFullHashResult* full_hash) {
515 std::string host; 487 std::string host;
516 std::string path; 488 std::string path;
517 CanonicalizeUrl(url, &host, &path, NULL); 489 CanonicalizeUrl(url, &host, &path, NULL);
518 full_hash->hash = SBFullHashForString(host + path); 490 full_hash->hash = SBFullHashForString(host + path);
519 full_hash->list_id = list_id; 491 full_hash->list_id = list_id;
520 } 492 }
521 493
522 void SetUp() override { 494 void SetUp() override {
523 // InProcessBrowserTest::SetUp() instantiates SafebrowsingService and 495 // InProcessBrowserTest::SetUp() instantiates SafebrowsingService.
524 // RegisterFactory has to be called before SafeBrowsingService is created. 496 // RegisterFactory and plugging test UI manager / protocol config have to
525 sb_factory_.reset(new TestSafeBrowsingServiceFactory( 497 // be called before SafeBrowsingService is created.
526 "https://definatelynotarealdomain/safebrowsing")); 498 sb_factory_.reset(new TestSafeBrowsingServiceFactory());
499 sb_factory_->SetTestUIManager(new FakeSafeBrowsingUIManager());
500 SetProtocolConfigURLPrefix("https://definatelynotarealdomain/safebrowsing",
501 sb_factory_.get());
527 SafeBrowsingService::RegisterFactory(sb_factory_.get()); 502 SafeBrowsingService::RegisterFactory(sb_factory_.get());
528 SafeBrowsingDatabase::RegisterFactory(&db_factory_); 503 SafeBrowsingDatabase::RegisterFactory(&db_factory_);
529 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_); 504 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_);
530 InProcessBrowserTest::SetUp(); 505 InProcessBrowserTest::SetUp();
531 } 506 }
532 507
533 void TearDown() override { 508 void TearDown() override {
534 InProcessBrowserTest::TearDown(); 509 InProcessBrowserTest::TearDown();
535 510
536 // Unregister test factories after InProcessBrowserTest::TearDown 511 // Unregister test factories after InProcessBrowserTest::TearDown
(...skipping 1034 matching lines...) Expand 10 before | Expand all | Expand 10 after
1571 class SafeBrowsingDatabaseManagerCookieTest : public InProcessBrowserTest { 1546 class SafeBrowsingDatabaseManagerCookieTest : public InProcessBrowserTest {
1572 public: 1547 public:
1573 SafeBrowsingDatabaseManagerCookieTest() {} 1548 SafeBrowsingDatabaseManagerCookieTest() {}
1574 1549
1575 void SetUp() override { 1550 void SetUp() override {
1576 // We need to start the test server to get the host&port in the url. 1551 // We need to start the test server to get the host&port in the url.
1577 ASSERT_TRUE(embedded_test_server()->Start()); 1552 ASSERT_TRUE(embedded_test_server()->Start());
1578 embedded_test_server()->RegisterRequestHandler( 1553 embedded_test_server()->RegisterRequestHandler(
1579 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::HandleRequest)); 1554 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::HandleRequest));
1580 1555
1581 // Point to the testing server for all SafeBrowsing requests. 1556 sb_factory_.reset(new TestSafeBrowsingServiceFactory());
1582 GURL url_prefix = embedded_test_server()->GetURL("/testpath"); 1557 SetProtocolConfigURLPrefix(
1583 sb_factory_.reset(new TestSafeBrowsingServiceFactory(url_prefix.spec())); 1558 embedded_test_server()->GetURL("/testpath").spec(), sb_factory_.get());
1584 SafeBrowsingService::RegisterFactory(sb_factory_.get()); 1559 SafeBrowsingService::RegisterFactory(sb_factory_.get());
1585 1560
1586 InProcessBrowserTest::SetUp(); 1561 InProcessBrowserTest::SetUp();
1587 } 1562 }
1588 1563
1589 void TearDown() override { 1564 void TearDown() override {
1590 InProcessBrowserTest::TearDown(); 1565 InProcessBrowserTest::TearDown();
1591 1566
1592 SafeBrowsingService::RegisterFactory(NULL); 1567 SafeBrowsingService::RegisterFactory(NULL);
1593 } 1568 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1627 return false; 1602 return false;
1628 } 1603 }
1629 if (!smt.BindString(0, embedded_test_server()->base_url().host())) { 1604 if (!smt.BindString(0, embedded_test_server()->base_url().host())) {
1630 EXPECT_TRUE(false); 1605 EXPECT_TRUE(false);
1631 return false; 1606 return false;
1632 } 1607 }
1633 if (!smt.Run()) { 1608 if (!smt.Run()) {
1634 EXPECT_TRUE(false); 1609 EXPECT_TRUE(false);
1635 return false; 1610 return false;
1636 } 1611 }
1637
1638 return InProcessBrowserTest::SetUpUserDataDirectory(); 1612 return InProcessBrowserTest::SetUpUserDataDirectory();
1639 } 1613 }
1640 1614
1641 void TearDownInProcessBrowserTestFixture() override { 1615 void TearDownInProcessBrowserTestFixture() override {
1642 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); 1616 InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
1643 1617
1644 sql::Connection db; 1618 sql::Connection db;
1645 base::FilePath cookie_path( 1619 base::FilePath cookie_path(
1646 SafeBrowsingService::GetCookieFilePathForTesting()); 1620 SafeBrowsingService::GetCookieFilePathForTesting());
1647 ASSERT_TRUE(db.Open(cookie_path)); 1621 ASSERT_TRUE(db.Open(cookie_path));
1648 1622
1649 sql::Statement smt( 1623 sql::Statement smt(
1650 db.GetUniqueStatement("SELECT name, value FROM cookies ORDER BY name")); 1624 db.GetUniqueStatement("SELECT name, value FROM cookies ORDER BY name"));
1651 ASSERT_TRUE(smt.is_valid()); 1625 ASSERT_TRUE(smt.is_valid());
1652
1653 ASSERT_TRUE(smt.Step()); 1626 ASSERT_TRUE(smt.Step());
1654 ASSERT_EQ("a", smt.ColumnString(0)); 1627 ASSERT_EQ("a", smt.ColumnString(0));
1655 ASSERT_EQ("b", smt.ColumnString(1)); 1628 ASSERT_EQ("b", smt.ColumnString(1));
1656 ASSERT_TRUE(smt.Step()); 1629 ASSERT_TRUE(smt.Step());
1657 ASSERT_EQ("c", smt.ColumnString(0)); 1630 ASSERT_EQ("c", smt.ColumnString(0));
1658 ASSERT_EQ("d", smt.ColumnString(1)); 1631 ASSERT_EQ("d", smt.ColumnString(1));
1659 EXPECT_FALSE(smt.Step()); 1632 EXPECT_FALSE(smt.Step());
1660 } 1633 }
1661 1634
1662 void SetUpOnMainThread() override { 1635 void ForceUpdate() {
1663 sb_service_ = g_browser_process->safe_browsing_service(); 1636 sb_factory_->test_safe_browsing_service()->protocol_manager()
1664 ASSERT_TRUE(sb_service_.get() != NULL); 1637 ->ForceScheduleNextUpdate(base::TimeDelta::FromSeconds(0));
1665 } 1638 }
1666 1639
1667 void TearDownOnMainThread() override { sb_service_ = NULL; } 1640 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_;
1668 1641
1669 void ForceUpdate() { 1642 protected:
1670 sb_service_->protocol_manager()->ForceScheduleNextUpdate(
1671 base::TimeDelta::FromSeconds(0));
1672 }
1673
1674 scoped_refptr<SafeBrowsingService> sb_service_;
1675
1676 private:
1677 static std::unique_ptr<net::test_server::HttpResponse> HandleRequest( 1643 static std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
1678 const net::test_server::HttpRequest& request) { 1644 const net::test_server::HttpRequest& request) {
1679 if (!base::StartsWith(request.relative_url, "/testpath/", 1645 if (!base::StartsWith(request.relative_url, "/testpath/",
1680 base::CompareCase::SENSITIVE)) { 1646 base::CompareCase::SENSITIVE)) {
1681 ADD_FAILURE() << "bad path"; 1647 ADD_FAILURE() << "bad path";
1682 return nullptr; 1648 return nullptr;
1683 } 1649 }
1684 1650
1685 auto cookie_it = request.headers.find("Cookie"); 1651 auto cookie_it = request.headers.find("Cookie");
1686 if (cookie_it == request.headers.end()) { 1652 if (cookie_it == request.headers.end()) {
(...skipping 17 matching lines...) Expand all
1704 1670
1705 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( 1671 std::unique_ptr<net::test_server::BasicHttpResponse> http_response(
1706 new net::test_server::BasicHttpResponse()); 1672 new net::test_server::BasicHttpResponse());
1707 http_response->set_content("foo"); 1673 http_response->set_content("foo");
1708 http_response->set_content_type("text/plain"); 1674 http_response->set_content_type("text/plain");
1709 http_response->AddCustomHeader( 1675 http_response->AddCustomHeader(
1710 "Set-Cookie", "c=d; Expires=Fri, 01 Jan 2038 01:01:01 GMT"); 1676 "Set-Cookie", "c=d; Expires=Fri, 01 Jan 2038 01:01:01 GMT");
1711 return std::move(http_response); 1677 return std::move(http_response);
1712 } 1678 }
1713 1679
1714 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_;
1715
1716 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManagerCookieTest); 1680 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManagerCookieTest);
1717 }; 1681 };
1718 1682
1719 // Test that a Local Safe Browsing database update request both sends cookies 1683 // Test that a Local Safe Browsing database update request both sends cookies
1720 // and can save cookies. 1684 // and can save cookies.
1721 IN_PROC_BROWSER_TEST_F(SafeBrowsingDatabaseManagerCookieTest, 1685 IN_PROC_BROWSER_TEST_F(SafeBrowsingDatabaseManagerCookieTest,
1722 TestSBUpdateCookies) { 1686 TestSBUpdateCookies) {
1723 content::WindowedNotificationObserver observer( 1687 content::WindowedNotificationObserver observer(
1724 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, 1688 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE,
1725 content::Source<SafeBrowsingDatabaseManager>( 1689 content::Source<SafeBrowsingDatabaseManager>(
1726 sb_service_->database_manager().get())); 1690 sb_factory_->test_safe_browsing_service()->database_manager().get()));
1727 BrowserThread::PostTask( 1691 BrowserThread::PostTask(
1728 BrowserThread::IO, FROM_HERE, 1692 BrowserThread::IO, FROM_HERE,
1729 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); 1693 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this));
1730 observer.Wait(); 1694 observer.Wait();
1731 } 1695 }
1732 1696
1733 } // namespace safe_browsing 1697 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698