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

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: 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/test_switches.h" 34 #include "chrome/test/base/test_switches.h"
35 #include "chrome/test/base/ui_test_utils.h" 35 #include "chrome/test/base/ui_test_utils.h"
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 } 130 }
131 131
132 private: 132 private:
133 ~FakeSafeBrowsingDatabaseManager() override {} 133 ~FakeSafeBrowsingDatabaseManager() override {}
134 134
135 base::hash_map<std::string, SBThreatType> badurls; 135 base::hash_map<std::string, SBThreatType> badurls;
136 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); 136 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager);
137 }; 137 };
138 138
139 // A SafeBrowingUIManager class that allows intercepting malware details. 139 // A SafeBrowingUIManager class that allows intercepting malware details.
140 class FakeSafeBrowsingUIManager : public SafeBrowsingUIManager { 140 class FakeSafeBrowsingUIManager : public TestSafeBrowsingUIManager {
141 public: 141 public:
142 FakeSafeBrowsingUIManager()
143 : TestSafeBrowsingUIManager(), threat_details_done_(false) {}
142 explicit FakeSafeBrowsingUIManager(SafeBrowsingService* service) 144 explicit FakeSafeBrowsingUIManager(SafeBrowsingService* service)
143 : SafeBrowsingUIManager(service), threat_details_done_(false) {} 145 : TestSafeBrowsingUIManager(service), threat_details_done_(false) {}
144 146
145 // Overrides SafeBrowsingUIManager 147 // Overrides SafeBrowsingUIManager
146 void SendSerializedThreatDetails(const std::string& serialized) override { 148 void SendSerializedThreatDetails(const std::string& serialized) override {
147 // Notify the UI thread that we got a report. 149 // Notify the UI thread that we got a report.
148 BrowserThread::PostTask( 150 BrowserThread::PostTask(
149 BrowserThread::UI, FROM_HERE, 151 BrowserThread::UI, FROM_HERE,
150 base::Bind(&FakeSafeBrowsingUIManager::OnThreatDetailsDone, this, 152 base::Bind(&FakeSafeBrowsingUIManager::OnThreatDetailsDone, this,
151 serialized)); 153 serialized));
152 } 154 }
153 155
(...skipping 27 matching lines...) Expand all
181 ~FakeSafeBrowsingUIManager() override {} 183 ~FakeSafeBrowsingUIManager() override {}
182 184
183 private: 185 private:
184 std::string report_; 186 std::string report_;
185 base::Closure threat_details_done_callback_; 187 base::Closure threat_details_done_callback_;
186 bool threat_details_done_; 188 bool threat_details_done_;
187 189
188 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingUIManager); 190 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingUIManager);
189 }; 191 };
190 192
191 class FakeSafeBrowsingService : public SafeBrowsingService {
192 public:
193 FakeSafeBrowsingService()
194 : fake_database_manager_(),
195 fake_ui_manager_() { }
196
197 // Returned pointer has the same lifespan as the database_manager_ refcounted
198 // object.
199 FakeSafeBrowsingDatabaseManager* fake_database_manager() {
200 return fake_database_manager_;
201 }
202 // Returned pointer has the same lifespan as the ui_manager_ refcounted
203 // object.
204 FakeSafeBrowsingUIManager* fake_ui_manager() {
205 return fake_ui_manager_;
206 }
207
208 protected:
209 ~FakeSafeBrowsingService() override {}
210
211 SafeBrowsingDatabaseManager* CreateDatabaseManager() override {
212 fake_database_manager_ = new FakeSafeBrowsingDatabaseManager();
213 return fake_database_manager_;
214 }
215
216 SafeBrowsingUIManager* CreateUIManager() override {
217 fake_ui_manager_ = new FakeSafeBrowsingUIManager(this);
218 return fake_ui_manager_;
219 }
220
221 SafeBrowsingProtocolManagerDelegate* GetProtocolManagerDelegate() override {
222 // Our SafeBrowsingDatabaseManager doesn't implement this delegate.
223 return NULL;
224 }
225
226 private:
227 FakeSafeBrowsingDatabaseManager* fake_database_manager_;
228 FakeSafeBrowsingUIManager* fake_ui_manager_;
229
230 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService);
231 };
232
233 // Factory that creates FakeSafeBrowsingService instances.
234 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory {
235 public:
236 TestSafeBrowsingServiceFactory() :
237 most_recent_service_(NULL) { }
238 ~TestSafeBrowsingServiceFactory() override {}
239
240 SafeBrowsingService* CreateSafeBrowsingService() override {
241 most_recent_service_ = new FakeSafeBrowsingService();
242 return most_recent_service_;
243 }
244
245 FakeSafeBrowsingService* most_recent_service() const {
246 return most_recent_service_;
247 }
248
249 private:
250 FakeSafeBrowsingService* most_recent_service_;
251 };
252
253 } // namespace 193 } // namespace
254 194
255 class TestThreatDetailsFactory : public ThreatDetailsFactory { 195 class TestThreatDetailsFactory : public ThreatDetailsFactory {
256 public: 196 public:
257 TestThreatDetailsFactory() : details_() {} 197 TestThreatDetailsFactory() : details_() {}
258 ~TestThreatDetailsFactory() override {} 198 ~TestThreatDetailsFactory() override {}
259 199
260 ThreatDetails* CreateThreatDetails( 200 ThreatDetails* CreateThreatDetails(
261 SafeBrowsingUIManager* delegate, 201 SafeBrowsingUIManager* delegate,
262 WebContents* web_contents, 202 WebContents* web_contents,
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 public: 276 public:
337 enum Visibility { 277 enum Visibility {
338 VISIBILITY_ERROR = -1, 278 VISIBILITY_ERROR = -1,
339 HIDDEN = 0, 279 HIDDEN = 0,
340 VISIBLE = 1 280 VISIBLE = 1
341 }; 281 };
342 282
343 SafeBrowsingBlockingPageBrowserTest() {} 283 SafeBrowsingBlockingPageBrowserTest() {}
344 284
345 void SetUp() override { 285 void SetUp() override {
286 // Test UI manager and test database manager should be set before
287 // InProcessBrowserTest::SetUp().
288 factory_.SetTestUIManager(new FakeSafeBrowsingUIManager());
289 factory_.SetTestDatabaseManager(new FakeSafeBrowsingDatabaseManager());
346 SafeBrowsingService::RegisterFactory(&factory_); 290 SafeBrowsingService::RegisterFactory(&factory_);
347 SafeBrowsingBlockingPage::RegisterFactory(&blocking_page_factory_); 291 SafeBrowsingBlockingPage::RegisterFactory(&blocking_page_factory_);
348 ThreatDetails::RegisterFactory(&details_factory_); 292 ThreatDetails::RegisterFactory(&details_factory_);
349 InProcessBrowserTest::SetUp(); 293 InProcessBrowserTest::SetUp();
350 } 294 }
351 295
352 void TearDown() override { 296 void TearDown() override {
353 InProcessBrowserTest::TearDown(); 297 InProcessBrowserTest::TearDown();
354 SafeBrowsingBlockingPage::RegisterFactory(NULL); 298 SafeBrowsingBlockingPage::RegisterFactory(NULL);
355 SafeBrowsingService::RegisterFactory(NULL); 299 SafeBrowsingService::RegisterFactory(NULL);
356 ThreatDetails::RegisterFactory(NULL); 300 ThreatDetails::RegisterFactory(NULL);
357 } 301 }
358 302
359 void SetUpCommandLine(base::CommandLine* command_line) override { 303 void SetUpCommandLine(base::CommandLine* command_line) override {
360 if (testing::get<1>(GetParam())) 304 if (testing::get<1>(GetParam()))
361 content::IsolateAllSitesForTesting(command_line); 305 content::IsolateAllSitesForTesting(command_line);
362 } 306 }
363 307
364 void SetUpOnMainThread() override { 308 void SetUpOnMainThread() override {
365 BrowserThread::PostTask( 309 BrowserThread::PostTask(
366 BrowserThread::IO, FROM_HERE, 310 BrowserThread::IO, FROM_HERE,
367 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); 311 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
368 } 312 }
369 313
370 void SetURLThreatType(const GURL& url, SBThreatType threat_type) { 314 void SetURLThreatType(const GURL& url, SBThreatType threat_type) {
371 FakeSafeBrowsingService* service = 315 TestSafeBrowsingService* service = factory_.test_safe_browsing_service();
372 static_cast<FakeSafeBrowsingService*>( 316 ASSERT_TRUE(service);
373 g_browser_process->safe_browsing_service());
374 317
375 ASSERT_TRUE(service); 318 static_cast<FakeSafeBrowsingDatabaseManager*>(
376 service->fake_database_manager()->SetURLThreatType(url, threat_type); 319 service->database_manager().get())
320 ->SetURLThreatType(url, threat_type);
377 } 321 }
378 322
379 // Adds a safebrowsing result of the current test threat to the fake 323 // Adds a safebrowsing result of the current test threat to the fake
380 // safebrowsing service, navigates to that page, and returns the url. 324 // safebrowsing service, navigates to that page, and returns the url.
381 GURL SetupWarningAndNavigate() { 325 GURL SetupWarningAndNavigate() {
382 GURL url = net::URLRequestMockHTTPJob::GetMockUrl(kEmptyPage); 326 GURL url = net::URLRequestMockHTTPJob::GetMockUrl(kEmptyPage);
383 SetURLThreatType(url, testing::get<0>(GetParam())); 327 SetURLThreatType(url, testing::get<0>(GetParam()));
384 328
385 ui_test_utils::NavigateToURL(browser(), url); 329 ui_test_utils::NavigateToURL(browser(), url);
386 EXPECT_TRUE(WaitForReady()); 330 EXPECT_TRUE(WaitForReady());
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 387
444 bool YesInterstitial() { 388 bool YesInterstitial() {
445 WebContents* contents = 389 WebContents* contents =
446 browser()->tab_strip_model()->GetActiveWebContents(); 390 browser()->tab_strip_model()->GetActiveWebContents();
447 InterstitialPage* interstitial_page = InterstitialPage::GetInterstitialPage( 391 InterstitialPage* interstitial_page = InterstitialPage::GetInterstitialPage(
448 contents); 392 contents);
449 return interstitial_page != NULL; 393 return interstitial_page != NULL;
450 } 394 }
451 395
452 void SetReportSentCallback(const base::Closure& callback) { 396 void SetReportSentCallback(const base::Closure& callback) {
453 factory_.most_recent_service() 397 static_cast<FakeSafeBrowsingUIManager*>(
454 ->fake_ui_manager() 398 factory_.test_safe_browsing_service()
399 ->ui_manager().get())
455 ->set_threat_details_done_callback(callback); 400 ->set_threat_details_done_callback(callback);
456 } 401 }
457 402
458 std::string GetReportSent() { 403 std::string GetReportSent() {
459 return factory_.most_recent_service()->fake_ui_manager()->GetReport(); 404 return static_cast<FakeSafeBrowsingUIManager*>(
405 factory_.test_safe_browsing_service()->ui_manager().get())
406 ->GetReport();
460 } 407 }
461 408
462 void MalwareRedirectCancelAndProceed(const std::string& open_function) { 409 void MalwareRedirectCancelAndProceed(const std::string& open_function) {
463 GURL load_url = net::URLRequestMockHTTPJob::GetMockUrl( 410 GURL load_url = net::URLRequestMockHTTPJob::GetMockUrl(
464 "safe_browsing/interstitial_cancel.html"); 411 "safe_browsing/interstitial_cancel.html");
465 GURL malware_url = net::URLRequestMockHTTPJob::GetMockUrl(kMalwarePage); 412 GURL malware_url = net::URLRequestMockHTTPJob::GetMockUrl(kMalwarePage);
466 SetURLThreatType(malware_url, testing::get<0>(GetParam())); 413 SetURLThreatType(malware_url, testing::get<0>(GetParam()));
467 414
468 // Load the test page. 415 // Load the test page.
469 ui_test_utils::NavigateToURL(browser(), load_url); 416 ui_test_utils::NavigateToURL(browser(), load_url);
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after
1142 1089
1143 INSTANTIATE_TEST_CASE_P( 1090 INSTANTIATE_TEST_CASE_P(
1144 SafeBrowsingBlockingPageIDNTestWithThreatType, 1091 SafeBrowsingBlockingPageIDNTestWithThreatType,
1145 SafeBrowsingBlockingPageIDNTest, 1092 SafeBrowsingBlockingPageIDNTest,
1146 testing::Combine(testing::Values(false, true), 1093 testing::Combine(testing::Values(false, true),
1147 testing::Values(SB_THREAT_TYPE_URL_MALWARE, 1094 testing::Values(SB_THREAT_TYPE_URL_MALWARE,
1148 SB_THREAT_TYPE_URL_PHISHING, 1095 SB_THREAT_TYPE_URL_PHISHING,
1149 SB_THREAT_TYPE_URL_UNWANTED))); 1096 SB_THREAT_TYPE_URL_UNWANTED)));
1150 1097
1151 } // namespace safe_browsing 1098 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698