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

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

Issue 1870003002: Convert //chrome/browser/safe_browsing from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and address comments Created 4 years, 8 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
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/files/file_path.h" 15 #include "base/files/file_path.h"
16 #include "base/files/scoped_temp_dir.h" 16 #include "base/files/scoped_temp_dir.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/ptr_util.h"
18 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
19 #include "base/metrics/field_trial.h" 20 #include "base/metrics/field_trial.h"
20 #include "base/path_service.h" 21 #include "base/path_service.h"
21 #include "base/stl_util.h" 22 #include "base/stl_util.h"
22 #include "base/strings/string_split.h" 23 #include "base/strings/string_split.h"
23 #include "base/strings/string_util.h" 24 #include "base/strings/string_util.h"
24 #include "base/strings/utf_string_conversions.h" 25 #include "base/strings/utf_string_conversions.h"
25 #include "base/test/thread_test_helper.h" 26 #include "base/test/thread_test_helper.h"
26 #include "base/time/time.h" 27 #include "base/time/time.h"
27 #include "build/build_config.h" 28 #include "build/build_config.h"
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 public: 100 public:
100 ~NeverCompletingHttpResponse() override {} 101 ~NeverCompletingHttpResponse() override {}
101 102
102 void SendResponse( 103 void SendResponse(
103 const net::test_server::SendBytesCallback& send, 104 const net::test_server::SendBytesCallback& send,
104 const net::test_server::SendCompleteCallback& done) override { 105 const net::test_server::SendCompleteCallback& done) override {
105 // Do nothing. |done| is never called. 106 // Do nothing. |done| is never called.
106 } 107 }
107 }; 108 };
108 109
109 scoped_ptr<net::test_server::HttpResponse> HandleNeverCompletingRequests( 110 std::unique_ptr<net::test_server::HttpResponse> HandleNeverCompletingRequests(
110 const net::test_server::HttpRequest& request) { 111 const net::test_server::HttpRequest& request) {
111 if (!base::StartsWith(request.relative_url, kNeverCompletesPath, 112 if (!base::StartsWith(request.relative_url, kNeverCompletesPath,
112 base::CompareCase::SENSITIVE)) 113 base::CompareCase::SENSITIVE))
113 return nullptr; 114 return nullptr;
114 return make_scoped_ptr(new NeverCompletingHttpResponse()); 115 return base::WrapUnique(new NeverCompletingHttpResponse());
115 } 116 }
116 117
117 void InvokeFullHashCallback( 118 void InvokeFullHashCallback(
118 SafeBrowsingProtocolManager::FullHashCallback callback, 119 SafeBrowsingProtocolManager::FullHashCallback callback,
119 const std::vector<SBFullHashResult>& result) { 120 const std::vector<SBFullHashResult>& result) {
120 callback.Run(result, base::TimeDelta::FromMinutes(45)); 121 callback.Run(result, base::TimeDelta::FromMinutes(45));
121 } 122 }
122 123
123 class FakeSafeBrowsingUIManager : public SafeBrowsingUIManager { 124 class FakeSafeBrowsingUIManager : public SafeBrowsingUIManager {
124 public: 125 public:
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 prefix_hits->clear(); 269 prefix_hits->clear();
269 return ContainsUrlPrefixes(RESOURCEBLACKLIST, RESOURCEBLACKLIST, 270 return ContainsUrlPrefixes(RESOURCEBLACKLIST, RESOURCEBLACKLIST,
270 prefixes, prefix_hits); 271 prefixes, prefix_hits);
271 } 272 }
272 bool UpdateStarted(std::vector<SBListChunkRanges>* lists) override { 273 bool UpdateStarted(std::vector<SBListChunkRanges>* lists) override {
273 ADD_FAILURE() << "Not implemented."; 274 ADD_FAILURE() << "Not implemented.";
274 return false; 275 return false;
275 } 276 }
276 void InsertChunks( 277 void InsertChunks(
277 const std::string& list_name, 278 const std::string& list_name,
278 const std::vector<scoped_ptr<SBChunkData>>& chunks) override { 279 const std::vector<std::unique_ptr<SBChunkData>>& chunks) override {
279 ADD_FAILURE() << "Not implemented."; 280 ADD_FAILURE() << "Not implemented.";
280 } 281 }
281 void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) override { 282 void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) override {
282 ADD_FAILURE() << "Not implemented."; 283 ADD_FAILURE() << "Not implemented.";
283 } 284 }
284 void UpdateFinished(bool update_succeeded) override { 285 void UpdateFinished(bool update_succeeded) override {
285 ADD_FAILURE() << "Not implemented."; 286 ADD_FAILURE() << "Not implemented.";
286 } 287 }
287 void CacheHashResults(const std::vector<SBPrefix>& prefixes, 288 void CacheHashResults(const std::vector<SBPrefix>& prefixes,
288 const std::vector<SBFullHashResult>& cache_hits, 289 const std::vector<SBFullHashResult>& cache_hits,
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 // Waits for pending tasks on the IO thread to complete and check if the 670 // Waits for pending tasks on the IO thread to complete and check if the
670 // SafeBrowsingService enabled state matches |enabled|. 671 // SafeBrowsingService enabled state matches |enabled|.
671 void WaitForIOAndCheckEnabled(SafeBrowsingService* service, bool enabled) { 672 void WaitForIOAndCheckEnabled(SafeBrowsingService* service, bool enabled) {
672 scoped_refptr<ServiceEnabledHelper> enabled_helper(new ServiceEnabledHelper( 673 scoped_refptr<ServiceEnabledHelper> enabled_helper(new ServiceEnabledHelper(
673 service, enabled, 674 service, enabled,
674 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get())); 675 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get()));
675 ASSERT_TRUE(enabled_helper->Run()); 676 ASSERT_TRUE(enabled_helper->Run());
676 } 677 }
677 678
678 private: 679 private:
679 scoped_ptr<TestSafeBrowsingServiceFactory> sb_factory_; 680 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_;
680 TestSafeBrowsingDatabaseFactory db_factory_; 681 TestSafeBrowsingDatabaseFactory db_factory_;
681 TestSBProtocolManagerFactory pm_factory_; 682 TestSBProtocolManagerFactory pm_factory_;
682 683
683 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); 684 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest);
684 }; 685 };
685 686
686 class SafeBrowsingServiceMetadataTest 687 class SafeBrowsingServiceMetadataTest
687 : public SafeBrowsingServiceTest, 688 : public SafeBrowsingServiceTest,
688 public ::testing::WithParamInterface<ThreatPatternType> { 689 public ::testing::WithParamInterface<ThreatPatternType> {
689 public: 690 public:
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after
1440 1441
1441 EXPECT_TRUE(pref_service->GetBoolean(prefs::kSafeBrowsingEnabled)); 1442 EXPECT_TRUE(pref_service->GetBoolean(prefs::kSafeBrowsingEnabled));
1442 1443
1443 // SBS might still be starting, make sure this doesn't flake. 1444 // SBS might still be starting, make sure this doesn't flake.
1444 EXPECT_TRUE(sb_service->enabled_by_prefs()); 1445 EXPECT_TRUE(sb_service->enabled_by_prefs());
1445 WaitForIOAndCheckEnabled(sb_service, true); 1446 WaitForIOAndCheckEnabled(sb_service, true);
1446 EXPECT_TRUE(csd_service->enabled()); 1447 EXPECT_TRUE(csd_service->enabled());
1447 1448
1448 // Add a new Profile. SBS should keep running. 1449 // Add a new Profile. SBS should keep running.
1449 ASSERT_TRUE(temp_profile_dir_.CreateUniqueTempDir()); 1450 ASSERT_TRUE(temp_profile_dir_.CreateUniqueTempDir());
1450 scoped_ptr<Profile> profile2(Profile::CreateProfile( 1451 std::unique_ptr<Profile> profile2(Profile::CreateProfile(
1451 temp_profile_dir_.path(), NULL, Profile::CREATE_MODE_SYNCHRONOUS)); 1452 temp_profile_dir_.path(), NULL, Profile::CREATE_MODE_SYNCHRONOUS));
1452 ASSERT_TRUE(profile2.get() != NULL); 1453 ASSERT_TRUE(profile2.get() != NULL);
1453 StartupTaskRunnerServiceFactory::GetForProfile(profile2.get())-> 1454 StartupTaskRunnerServiceFactory::GetForProfile(profile2.get())->
1454 StartDeferredTaskRunners(); 1455 StartDeferredTaskRunners();
1455 PrefService* pref_service2 = profile2->GetPrefs(); 1456 PrefService* pref_service2 = profile2->GetPrefs();
1456 EXPECT_TRUE(pref_service2->GetBoolean(prefs::kSafeBrowsingEnabled)); 1457 EXPECT_TRUE(pref_service2->GetBoolean(prefs::kSafeBrowsingEnabled));
1457 // We don't expect the state to have changed, but if it did, wait for it. 1458 // We don't expect the state to have changed, but if it did, wait for it.
1458 EXPECT_TRUE(sb_service->enabled_by_prefs()); 1459 EXPECT_TRUE(sb_service->enabled_by_prefs());
1459 WaitForIOAndCheckEnabled(sb_service, true); 1460 WaitForIOAndCheckEnabled(sb_service, true);
1460 EXPECT_TRUE(csd_service->enabled()); 1461 EXPECT_TRUE(csd_service->enabled());
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1668 void TearDownOnMainThread() override { sb_service_ = NULL; } 1669 void TearDownOnMainThread() override { sb_service_ = NULL; }
1669 1670
1670 void ForceUpdate() { 1671 void ForceUpdate() {
1671 sb_service_->protocol_manager()->ForceScheduleNextUpdate( 1672 sb_service_->protocol_manager()->ForceScheduleNextUpdate(
1672 base::TimeDelta::FromSeconds(0)); 1673 base::TimeDelta::FromSeconds(0));
1673 } 1674 }
1674 1675
1675 scoped_refptr<SafeBrowsingService> sb_service_; 1676 scoped_refptr<SafeBrowsingService> sb_service_;
1676 1677
1677 private: 1678 private:
1678 static scoped_ptr<net::test_server::HttpResponse> HandleRequest( 1679 static std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
1679 const net::test_server::HttpRequest& request) { 1680 const net::test_server::HttpRequest& request) {
1680 if (!base::StartsWith(request.relative_url, "/testpath/", 1681 if (!base::StartsWith(request.relative_url, "/testpath/",
1681 base::CompareCase::SENSITIVE)) { 1682 base::CompareCase::SENSITIVE)) {
1682 ADD_FAILURE() << "bad path"; 1683 ADD_FAILURE() << "bad path";
1683 return nullptr; 1684 return nullptr;
1684 } 1685 }
1685 1686
1686 auto cookie_it = request.headers.find("Cookie"); 1687 auto cookie_it = request.headers.find("Cookie");
1687 if (cookie_it == request.headers.end()) { 1688 if (cookie_it == request.headers.end()) {
1688 ADD_FAILURE() << "no cookie header"; 1689 ADD_FAILURE() << "no cookie header";
1689 return nullptr; 1690 return nullptr;
1690 } 1691 }
1691 1692
1692 net::cookie_util::ParsedRequestCookies req_cookies; 1693 net::cookie_util::ParsedRequestCookies req_cookies;
1693 net::cookie_util::ParseRequestCookieLine(cookie_it->second, &req_cookies); 1694 net::cookie_util::ParseRequestCookieLine(cookie_it->second, &req_cookies);
1694 if (req_cookies.size() != 1) { 1695 if (req_cookies.size() != 1) {
1695 ADD_FAILURE() << "req_cookies.size() = " << req_cookies.size(); 1696 ADD_FAILURE() << "req_cookies.size() = " << req_cookies.size();
1696 return nullptr; 1697 return nullptr;
1697 } 1698 }
1698 const net::cookie_util::ParsedRequestCookie expected_cookie( 1699 const net::cookie_util::ParsedRequestCookie expected_cookie(
1699 std::make_pair("a", "b")); 1700 std::make_pair("a", "b"));
1700 const net::cookie_util::ParsedRequestCookie& cookie = req_cookies.front(); 1701 const net::cookie_util::ParsedRequestCookie& cookie = req_cookies.front();
1701 if (cookie != expected_cookie) { 1702 if (cookie != expected_cookie) {
1702 ADD_FAILURE() << "bad cookie " << cookie.first << "=" << cookie.second; 1703 ADD_FAILURE() << "bad cookie " << cookie.first << "=" << cookie.second;
1703 return nullptr; 1704 return nullptr;
1704 } 1705 }
1705 1706
1706 scoped_ptr<net::test_server::BasicHttpResponse> http_response( 1707 std::unique_ptr<net::test_server::BasicHttpResponse> http_response(
1707 new net::test_server::BasicHttpResponse()); 1708 new net::test_server::BasicHttpResponse());
1708 http_response->set_content("foo"); 1709 http_response->set_content("foo");
1709 http_response->set_content_type("text/plain"); 1710 http_response->set_content_type("text/plain");
1710 http_response->AddCustomHeader( 1711 http_response->AddCustomHeader(
1711 "Set-Cookie", "c=d; Expires=Fri, 01 Jan 2038 01:01:01 GMT"); 1712 "Set-Cookie", "c=d; Expires=Fri, 01 Jan 2038 01:01:01 GMT");
1712 return std::move(http_response); 1713 return std::move(http_response);
1713 } 1714 }
1714 1715
1715 scoped_ptr<TestSafeBrowsingServiceFactory> sb_factory_; 1716 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_;
1716 1717
1717 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManagerCookieTest); 1718 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManagerCookieTest);
1718 }; 1719 };
1719 1720
1720 // Test that a Local Safe Browsing database update request both sends cookies 1721 // Test that a Local Safe Browsing database update request both sends cookies
1721 // and can save cookies. 1722 // and can save cookies.
1722 IN_PROC_BROWSER_TEST_F(SafeBrowsingDatabaseManagerCookieTest, 1723 IN_PROC_BROWSER_TEST_F(SafeBrowsingDatabaseManagerCookieTest,
1723 TestSBUpdateCookies) { 1724 TestSBUpdateCookies) {
1724 content::WindowedNotificationObserver observer( 1725 content::WindowedNotificationObserver observer(
1725 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, 1726 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE,
1726 content::Source<SafeBrowsingDatabaseManager>( 1727 content::Source<SafeBrowsingDatabaseManager>(
1727 sb_service_->database_manager().get())); 1728 sb_service_->database_manager().get()));
1728 BrowserThread::PostTask( 1729 BrowserThread::PostTask(
1729 BrowserThread::IO, FROM_HERE, 1730 BrowserThread::IO, FROM_HERE,
1730 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); 1731 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this));
1731 observer.Wait(); 1732 observer.Wait();
1732 } 1733 }
1733 1734
1734 } // namespace safe_browsing 1735 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698