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

Side by Side Diff: chrome/browser/safe_browsing/unverified_download_policy_unittest.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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 #include "chrome/browser/safe_browsing/unverified_download_policy.h" 5 #include "chrome/browser/safe_browsing/unverified_download_policy.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/callback_helpers.h" 9 #include "base/callback_helpers.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 13 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
14 #include "chrome/common/chrome_switches.h" 14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/testing_browser_process.h" 16 #include "chrome/test/base/testing_browser_process.h"
17 #include "chrome/test/base/testing_profile_manager.h" 17 #include "chrome/test/base/testing_profile_manager.h"
18 #include "components/prefs/testing_pref_service.h" 18 #include "components/prefs/testing_pref_service.h"
19 #include "components/safe_browsing_db/test_database_manager.h" 19 #include "components/safe_browsing_db/test_database_manager.h"
20 #include "content/public/test/test_browser_thread_bundle.h" 20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "url/gurl.h" 22 #include "url/gurl.h"
23 23
24 namespace safe_browsing { 24 namespace safe_browsing {
25 25
26 namespace { 26 namespace {
27 27
28 class FakeDatabaseManager : public TestSafeBrowsingDatabaseManager { 28 class FakeDatabaseManager : public TestSafeBrowsingDatabaseManager {
29 public: 29 public:
30 bool IsSupported() const override { return true; } 30 bool IsSupported() const override { return true; }
31 31
32 bool MatchDownloadWhitelistUrl(const GURL& url) override { 32 bool MatchDownloadWhitelistUrl(const GURL& url) override {
33 return url.SchemeIsHTTPOrHTTPS() && url.host() == "supported.example.com"; 33 return url.SchemeIsHTTPOrHTTPS() && url.host() == "supported.example.com";
34 } 34 }
35 35
36 protected: 36 protected:
37 ~FakeDatabaseManager() override {} 37 ~FakeDatabaseManager() override {}
38 }; 38 };
39 39
40 class TestSafeBrowsingService : public SafeBrowsingService {
41 public:
42 SafeBrowsingDatabaseManager* CreateDatabaseManager() override {
43 return new FakeDatabaseManager();
44 }
45
46 protected:
47 ~TestSafeBrowsingService() override {}
48
49 SafeBrowsingProtocolManagerDelegate* GetProtocolManagerDelegate() override {
50 // Our FakeDatabaseManager doesn't implement this delegate.
51 return NULL;
52 }
53 };
54
55 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory {
56 public:
57 SafeBrowsingService* CreateSafeBrowsingService() override {
58 return new TestSafeBrowsingService();
59 }
60 };
61
62 class CompletionCallback { 40 class CompletionCallback {
63 public: 41 public:
64 CompletionCallback() {} 42 CompletionCallback() {}
65 43
66 UnverifiedDownloadCheckCompletionCallback GetCallback() { 44 UnverifiedDownloadCheckCompletionCallback GetCallback() {
67 completed_ = false; 45 completed_ = false;
68 completion_closure_.Reset(); 46 completion_closure_.Reset();
69 return base::Bind(&CompletionCallback::OnUnverifiedDownloadPolicyAvailable, 47 return base::Bind(&CompletionCallback::OnUnverifiedDownloadPolicyAvailable,
70 base::Unretained(this)); 48 base::Unretained(this));
71 } 49 }
(...skipping 16 matching lines...) Expand all
88 } 66 }
89 67
90 bool completed_ = false; 68 bool completed_ = false;
91 base::Closure completion_closure_; 69 base::Closure completion_closure_;
92 UnverifiedDownloadPolicy result_ = UnverifiedDownloadPolicy::DISALLOWED; 70 UnverifiedDownloadPolicy result_ = UnverifiedDownloadPolicy::DISALLOWED;
93 }; 71 };
94 72
95 class UnverifiedDownloadPolicyTest : public ::testing::Test { 73 class UnverifiedDownloadPolicyTest : public ::testing::Test {
96 public: 74 public:
97 void SetUp() override { 75 void SetUp() override {
98 SafeBrowsingService::RegisterFactory(&test_safe_browsing_service_factory_); 76 SafeBrowsingService::RegisterFactory(
99 testing_safe_browsing_service_ = 77 &testing_safe_browsing_service_factory_);
100 SafeBrowsingService::CreateSafeBrowsingService();
101 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); 78 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal();
102 browser_process->SetSafeBrowsingService( 79 browser_process->SetSafeBrowsingService(
103 testing_safe_browsing_service_.get()); 80 testing_safe_browsing_service_factory_.CreateSafeBrowsingService());
104 browser_process->safe_browsing_service()->Initialize(); 81 browser_process->safe_browsing_service()->Initialize();
82 // Set up FakeDatabaseManager.
83 testing_safe_browsing_service_factory_.test_safe_browsing_service()
Nathan Parker 2016/05/07 00:05:32 Shouldn't this set the FakeDatabaseManager on the
Jialiu Lin 2016/05/12 21:53:58 Ah, this is not a InProcessBrowserTest but rather
84 ->SetDatabaseManager(new FakeDatabaseManager());
105 base::RunLoop().RunUntilIdle(); 85 base::RunLoop().RunUntilIdle();
106 86
107 testing_profile_manager_.reset( 87 testing_profile_manager_.reset(
108 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); 88 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
109 ASSERT_TRUE(testing_profile_manager_->SetUp()); 89 ASSERT_TRUE(testing_profile_manager_->SetUp());
110 90
111 testing_profile_ = testing_profile_manager_->CreateTestingProfile("foo"); 91 testing_profile_ = testing_profile_manager_->CreateTestingProfile("foo");
112 testing_profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled, true); 92 testing_profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled, true);
113 } 93 }
114 94
115 void TearDown() override { 95 void TearDown() override {
116 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); 96 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal();
117 browser_process->safe_browsing_service()->ShutDown(); 97 browser_process->safe_browsing_service()->ShutDown();
118 browser_process->SetSafeBrowsingService(nullptr); 98 browser_process->SetSafeBrowsingService(nullptr);
Jialiu Lin 2016/05/12 21:53:58 No need this line, since TestSafeBrowsingFactory d
119 testing_safe_browsing_service_ = nullptr;
120 SafeBrowsingService::RegisterFactory(nullptr); 99 SafeBrowsingService::RegisterFactory(nullptr);
121 base::RunLoop().RunUntilIdle(); 100 base::RunLoop().RunUntilIdle();
122 } 101 }
123 102
124 protected: 103 protected:
125 content::TestBrowserThreadBundle thread_bundle_; 104 content::TestBrowserThreadBundle thread_bundle_;
126 TestSafeBrowsingServiceFactory test_safe_browsing_service_factory_; 105 TestSafeBrowsingServiceFactory testing_safe_browsing_service_factory_;
127 std::unique_ptr<TestingProfileManager> testing_profile_manager_; 106 std::unique_ptr<TestingProfileManager> testing_profile_manager_;
128 scoped_refptr<SafeBrowsingService> testing_safe_browsing_service_;
129 TestingProfile* testing_profile_ = nullptr; 107 TestingProfile* testing_profile_ = nullptr;
130 }; 108 };
131 109
132 } // namespace 110 } // namespace
133 111
134 // Verify that SafeBrowsing whitelists can override field trials. 112 // Verify that SafeBrowsing whitelists can override field trials.
135 TEST_F(UnverifiedDownloadPolicyTest, Whitelist) { 113 TEST_F(UnverifiedDownloadPolicyTest, Whitelist) {
136 base::CommandLine::ForCurrentProcess()->AppendSwitch( 114 base::CommandLine::ForCurrentProcess()->AppendSwitch(
137 switches::kDisallowUncheckedDangerousDownloads); 115 switches::kDisallowUncheckedDangerousDownloads);
138 116
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 197
220 CompletionCallback completion_callback; 198 CompletionCallback completion_callback;
221 CheckUnverifiedDownloadPolicy(GURL("http://supported.example.com/foo/bar"), 199 CheckUnverifiedDownloadPolicy(GURL("http://supported.example.com/foo/bar"),
222 test_file_path, test_extensions, 200 test_file_path, test_extensions,
223 completion_callback.GetCallback()); 201 completion_callback.GetCallback());
224 EXPECT_EQ(UnverifiedDownloadPolicy::ALLOWED, 202 EXPECT_EQ(UnverifiedDownloadPolicy::ALLOWED,
225 completion_callback.WaitForResult()); 203 completion_callback.WaitForResult());
226 } 204 }
227 205
228 } // namespace safe_browsing 206 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698