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

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

Issue 1943993006: Create test fixture for SafeBrowsingService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits 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 fake safebrowsing service, where we can inject known- 5 // This test creates a fake safebrowsing service, where we can inject known-
6 // threat urls. It then uses a real browser to go to these urls, and sends 6 // threat urls. It then uses a real browser to go to these urls, and sends
7 // "goback" or "proceed" commands and verifies they work. 7 // "goback" or "proceed" commands and verifies they work.
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/test/histogram_tester.h" 16 #include "base/test/histogram_tester.h"
17 #include "base/values.h" 17 #include "base/values.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 #include "chrome/browser/browser_process.h" 19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/interstitials/security_interstitial_page_test_utils.h" 20 #include "chrome/browser/interstitials/security_interstitial_page_test_utils.h"
21 #include "chrome/browser/net/url_request_mock_util.h" 21 #include "chrome/browser/net/url_request_mock_util.h"
22 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/safe_browsing/local_database_manager.h" 23 #include "chrome/browser/safe_browsing/local_database_manager.h"
24 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" 24 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
25 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 25 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
26 #include "chrome/browser/safe_browsing/threat_details.h" 26 #include "chrome/browser/safe_browsing/threat_details.h"
27 #include "chrome/browser/safe_browsing/ui_manager.h" 27 #include "chrome/browser/safe_browsing/ui_manager.h"
28 #include "chrome/browser/ui/browser.h" 28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/browser_tabstrip.h" 29 #include "chrome/browser/ui/browser_tabstrip.h"
30 #include "chrome/browser/ui/tabs/tab_strip_model.h" 30 #include "chrome/browser/ui/tabs/tab_strip_model.h"
31 #include "chrome/common/pref_names.h" 31 #include "chrome/common/pref_names.h"
32 #include "chrome/common/url_constants.h" 32 #include "chrome/common/url_constants.h"
33 #include "chrome/test/base/in_process_browser_test.h" 33 #include "chrome/test/base/in_process_browser_test.h"
34 #include "chrome/test/base/ui_test_utils.h" 34 #include "chrome/test/base/ui_test_utils.h"
35 #include "components/prefs/pref_service.h" 35 #include "components/prefs/pref_service.h"
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 } 129 }
130 130
131 private: 131 private:
132 ~FakeSafeBrowsingDatabaseManager() override {} 132 ~FakeSafeBrowsingDatabaseManager() override {}
133 133
134 base::hash_map<std::string, SBThreatType> badurls; 134 base::hash_map<std::string, SBThreatType> badurls;
135 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); 135 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager);
136 }; 136 };
137 137
138 // A SafeBrowingUIManager class that allows intercepting malware details. 138 // A SafeBrowingUIManager class that allows intercepting malware details.
139 class FakeSafeBrowsingUIManager : public SafeBrowsingUIManager { 139 class FakeSafeBrowsingUIManager : public TestSafeBrowsingUIManager {
140 public: 140 public:
141 FakeSafeBrowsingUIManager()
142 : TestSafeBrowsingUIManager(), threat_details_done_(false) {}
141 explicit FakeSafeBrowsingUIManager(SafeBrowsingService* service) 143 explicit FakeSafeBrowsingUIManager(SafeBrowsingService* service)
142 : SafeBrowsingUIManager(service), threat_details_done_(false) {} 144 : TestSafeBrowsingUIManager(service), threat_details_done_(false) {}
143 145
144 // Overrides SafeBrowsingUIManager 146 // Overrides SafeBrowsingUIManager
145 void SendSerializedThreatDetails(const std::string& serialized) override { 147 void SendSerializedThreatDetails(const std::string& serialized) override {
146 // Notify the UI thread that we got a report. 148 // Notify the UI thread that we got a report.
147 BrowserThread::PostTask( 149 BrowserThread::PostTask(
148 BrowserThread::UI, FROM_HERE, 150 BrowserThread::UI, FROM_HERE,
149 base::Bind(&FakeSafeBrowsingUIManager::OnThreatDetailsDone, this, 151 base::Bind(&FakeSafeBrowsingUIManager::OnThreatDetailsDone, this,
150 serialized)); 152 serialized));
151 } 153 }
152 154
(...skipping 27 matching lines...) Expand all
180 ~FakeSafeBrowsingUIManager() override {} 182 ~FakeSafeBrowsingUIManager() override {}
181 183
182 private: 184 private:
183 std::string report_; 185 std::string report_;
184 base::Closure threat_details_done_callback_; 186 base::Closure threat_details_done_callback_;
185 bool threat_details_done_; 187 bool threat_details_done_;
186 188
187 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingUIManager); 189 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingUIManager);
188 }; 190 };
189 191
190 class FakeSafeBrowsingService : public SafeBrowsingService {
191 public:
192 FakeSafeBrowsingService()
193 : fake_database_manager_(),
194 fake_ui_manager_() { }
195
196 // Returned pointer has the same lifespan as the database_manager_ refcounted
197 // object.
198 FakeSafeBrowsingDatabaseManager* fake_database_manager() {
199 return fake_database_manager_;
200 }
201 // Returned pointer has the same lifespan as the ui_manager_ refcounted
202 // object.
203 FakeSafeBrowsingUIManager* fake_ui_manager() {
204 return fake_ui_manager_;
205 }
206
207 protected:
208 ~FakeSafeBrowsingService() override {}
209
210 SafeBrowsingDatabaseManager* CreateDatabaseManager() override {
211 fake_database_manager_ = new FakeSafeBrowsingDatabaseManager();
212 return fake_database_manager_;
213 }
214
215 SafeBrowsingUIManager* CreateUIManager() override {
216 fake_ui_manager_ = new FakeSafeBrowsingUIManager(this);
217 return fake_ui_manager_;
218 }
219
220 SafeBrowsingProtocolManagerDelegate* GetProtocolManagerDelegate() override {
221 // Our SafeBrowsingDatabaseManager doesn't implement this delegate.
222 return NULL;
223 }
224
225 private:
226 FakeSafeBrowsingDatabaseManager* fake_database_manager_;
227 FakeSafeBrowsingUIManager* fake_ui_manager_;
228
229 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService);
230 };
231
232 // Factory that creates FakeSafeBrowsingService instances.
233 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory {
234 public:
235 TestSafeBrowsingServiceFactory() :
236 most_recent_service_(NULL) { }
237 ~TestSafeBrowsingServiceFactory() override {}
238
239 SafeBrowsingService* CreateSafeBrowsingService() override {
240 most_recent_service_ = new FakeSafeBrowsingService();
241 return most_recent_service_;
242 }
243
244 FakeSafeBrowsingService* most_recent_service() const {
245 return most_recent_service_;
246 }
247
248 private:
249 FakeSafeBrowsingService* most_recent_service_;
250 };
251
252 } // namespace 192 } // namespace
253 193
254 class TestThreatDetailsFactory : public ThreatDetailsFactory { 194 class TestThreatDetailsFactory : public ThreatDetailsFactory {
255 public: 195 public:
256 TestThreatDetailsFactory() : details_() {} 196 TestThreatDetailsFactory() : details_() {}
257 ~TestThreatDetailsFactory() override {} 197 ~TestThreatDetailsFactory() override {}
258 198
259 ThreatDetails* CreateThreatDetails( 199 ThreatDetails* CreateThreatDetails(
260 SafeBrowsingUIManager* delegate, 200 SafeBrowsingUIManager* delegate,
261 WebContents* web_contents, 201 WebContents* web_contents,
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 public: 275 public:
336 enum Visibility { 276 enum Visibility {
337 VISIBILITY_ERROR = -1, 277 VISIBILITY_ERROR = -1,
338 HIDDEN = 0, 278 HIDDEN = 0,
339 VISIBLE = 1 279 VISIBLE = 1
340 }; 280 };
341 281
342 SafeBrowsingBlockingPageBrowserTest() {} 282 SafeBrowsingBlockingPageBrowserTest() {}
343 283
344 void SetUp() override { 284 void SetUp() override {
285 // Test UI manager and test database manager should be set before
286 // InProcessBrowserTest::SetUp().
287 factory_.SetTestUIManager(new FakeSafeBrowsingUIManager());
288 factory_.SetTestDatabaseManager(new FakeSafeBrowsingDatabaseManager());
345 SafeBrowsingService::RegisterFactory(&factory_); 289 SafeBrowsingService::RegisterFactory(&factory_);
346 SafeBrowsingBlockingPage::RegisterFactory(&blocking_page_factory_); 290 SafeBrowsingBlockingPage::RegisterFactory(&blocking_page_factory_);
347 ThreatDetails::RegisterFactory(&details_factory_); 291 ThreatDetails::RegisterFactory(&details_factory_);
348 InProcessBrowserTest::SetUp(); 292 InProcessBrowserTest::SetUp();
349 } 293 }
350 294
351 void TearDown() override { 295 void TearDown() override {
352 InProcessBrowserTest::TearDown(); 296 InProcessBrowserTest::TearDown();
353 SafeBrowsingBlockingPage::RegisterFactory(NULL); 297 SafeBrowsingBlockingPage::RegisterFactory(NULL);
354 SafeBrowsingService::RegisterFactory(NULL); 298 SafeBrowsingService::RegisterFactory(NULL);
355 ThreatDetails::RegisterFactory(NULL); 299 ThreatDetails::RegisterFactory(NULL);
356 } 300 }
357 301
358 void SetUpCommandLine(base::CommandLine* command_line) override { 302 void SetUpCommandLine(base::CommandLine* command_line) override {
359 if (testing::get<1>(GetParam())) 303 if (testing::get<1>(GetParam()))
360 content::IsolateAllSitesForTesting(command_line); 304 content::IsolateAllSitesForTesting(command_line);
361 } 305 }
362 306
363 void SetUpOnMainThread() override { 307 void SetUpOnMainThread() override {
364 BrowserThread::PostTask( 308 BrowserThread::PostTask(
365 BrowserThread::IO, FROM_HERE, 309 BrowserThread::IO, FROM_HERE,
366 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); 310 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
367 } 311 }
368 312
369 void SetURLThreatType(const GURL& url, SBThreatType threat_type) { 313 void SetURLThreatType(const GURL& url, SBThreatType threat_type) {
370 FakeSafeBrowsingService* service = 314 TestSafeBrowsingService* service = factory_.test_safe_browsing_service();
371 static_cast<FakeSafeBrowsingService*>( 315 ASSERT_TRUE(service);
372 g_browser_process->safe_browsing_service());
373 316
374 ASSERT_TRUE(service); 317 static_cast<FakeSafeBrowsingDatabaseManager*>(
375 service->fake_database_manager()->SetURLThreatType(url, threat_type); 318 service->database_manager().get())
319 ->SetURLThreatType(url, threat_type);
376 } 320 }
377 321
378 // Adds a safebrowsing result of the current test threat to the fake 322 // Adds a safebrowsing result of the current test threat to the fake
379 // safebrowsing service, navigates to that page, and returns the url. 323 // safebrowsing service, navigates to that page, and returns the url.
380 GURL SetupWarningAndNavigate() { 324 GURL SetupWarningAndNavigate() {
381 GURL url = net::URLRequestMockHTTPJob::GetMockUrl(kEmptyPage); 325 GURL url = net::URLRequestMockHTTPJob::GetMockUrl(kEmptyPage);
382 SetURLThreatType(url, testing::get<0>(GetParam())); 326 SetURLThreatType(url, testing::get<0>(GetParam()));
383 327
384 ui_test_utils::NavigateToURL(browser(), url); 328 ui_test_utils::NavigateToURL(browser(), url);
385 EXPECT_TRUE(WaitForReady()); 329 EXPECT_TRUE(WaitForReady());
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 386
443 bool YesInterstitial() { 387 bool YesInterstitial() {
444 WebContents* contents = 388 WebContents* contents =
445 browser()->tab_strip_model()->GetActiveWebContents(); 389 browser()->tab_strip_model()->GetActiveWebContents();
446 InterstitialPage* interstitial_page = InterstitialPage::GetInterstitialPage( 390 InterstitialPage* interstitial_page = InterstitialPage::GetInterstitialPage(
447 contents); 391 contents);
448 return interstitial_page != NULL; 392 return interstitial_page != NULL;
449 } 393 }
450 394
451 void SetReportSentCallback(const base::Closure& callback) { 395 void SetReportSentCallback(const base::Closure& callback) {
452 factory_.most_recent_service() 396 static_cast<FakeSafeBrowsingUIManager*>(
453 ->fake_ui_manager() 397 factory_.test_safe_browsing_service()->ui_manager().get())
454 ->set_threat_details_done_callback(callback); 398 ->set_threat_details_done_callback(callback);
455 } 399 }
456 400
457 std::string GetReportSent() { 401 std::string GetReportSent() {
458 return factory_.most_recent_service()->fake_ui_manager()->GetReport(); 402 return static_cast<FakeSafeBrowsingUIManager*>(
403 factory_.test_safe_browsing_service()->ui_manager().get())
404 ->GetReport();
459 } 405 }
460 406
461 void MalwareRedirectCancelAndProceed(const std::string& open_function) { 407 void MalwareRedirectCancelAndProceed(const std::string& open_function) {
462 GURL load_url = net::URLRequestMockHTTPJob::GetMockUrl( 408 GURL load_url = net::URLRequestMockHTTPJob::GetMockUrl(
463 "safe_browsing/interstitial_cancel.html"); 409 "safe_browsing/interstitial_cancel.html");
464 GURL malware_url = net::URLRequestMockHTTPJob::GetMockUrl(kMalwarePage); 410 GURL malware_url = net::URLRequestMockHTTPJob::GetMockUrl(kMalwarePage);
465 SetURLThreatType(malware_url, testing::get<0>(GetParam())); 411 SetURLThreatType(malware_url, testing::get<0>(GetParam()));
466 412
467 // Load the test page. 413 // Load the test page.
468 ui_test_utils::NavigateToURL(browser(), load_url); 414 ui_test_utils::NavigateToURL(browser(), load_url);
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after
1093 1039
1094 INSTANTIATE_TEST_CASE_P( 1040 INSTANTIATE_TEST_CASE_P(
1095 SafeBrowsingBlockingPageIDNTestWithThreatType, 1041 SafeBrowsingBlockingPageIDNTestWithThreatType,
1096 SafeBrowsingBlockingPageIDNTest, 1042 SafeBrowsingBlockingPageIDNTest,
1097 testing::Combine(testing::Values(false, true), 1043 testing::Combine(testing::Values(false, true),
1098 testing::Values(SB_THREAT_TYPE_URL_MALWARE, 1044 testing::Values(SB_THREAT_TYPE_URL_MALWARE,
1099 SB_THREAT_TYPE_URL_PHISHING, 1045 SB_THREAT_TYPE_URL_PHISHING,
1100 SB_THREAT_TYPE_URL_UNWANTED))); 1046 SB_THREAT_TYPE_URL_UNWANTED)));
1101 1047
1102 } // namespace safe_browsing 1048 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698