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

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

Issue 1147353007: safe_browsing_blocking_page_test.cc: Use mock URLRequest jobs instead of SpawnedTestServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sb-test-server2
Patch Set: Created 5 years, 6 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 "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/browser/browser_process.h" 15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/interstitials/security_interstitial_page_test_utils.h" 16 #include "chrome/browser/interstitials/security_interstitial_page_test_utils.h"
17 #include "chrome/browser/net/url_request_mock_util.h"
17 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/safe_browsing/database_manager.h" 19 #include "chrome/browser/safe_browsing/database_manager.h"
19 #include "chrome/browser/safe_browsing/local_database_manager.h" 20 #include "chrome/browser/safe_browsing/local_database_manager.h"
20 #include "chrome/browser/safe_browsing/malware_details.h" 21 #include "chrome/browser/safe_browsing/malware_details.h"
21 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" 22 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
22 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 23 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
23 #include "chrome/browser/safe_browsing/safe_browsing_util.h" 24 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
24 #include "chrome/browser/safe_browsing/test_database_manager.h" 25 #include "chrome/browser/safe_browsing/test_database_manager.h"
25 #include "chrome/browser/safe_browsing/ui_manager.h" 26 #include "chrome/browser/safe_browsing/ui_manager.h"
26 #include "chrome/browser/ui/browser.h" 27 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_tabstrip.h" 28 #include "chrome/browser/ui/browser_tabstrip.h"
28 #include "chrome/browser/ui/tabs/tab_strip_model.h" 29 #include "chrome/browser/ui/tabs/tab_strip_model.h"
29 #include "chrome/common/chrome_switches.h" 30 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/pref_names.h" 31 #include "chrome/common/pref_names.h"
31 #include "chrome/common/url_constants.h" 32 #include "chrome/common/url_constants.h"
32 #include "chrome/test/base/in_process_browser_test.h" 33 #include "chrome/test/base/in_process_browser_test.h"
33 #include "chrome/test/base/test_switches.h" 34 #include "chrome/test/base/test_switches.h"
34 #include "chrome/test/base/ui_test_utils.h" 35 #include "chrome/test/base/ui_test_utils.h"
35 #include "content/public/browser/interstitial_page.h" 36 #include "content/public/browser/interstitial_page.h"
36 #include "content/public/browser/navigation_controller.h" 37 #include "content/public/browser/navigation_controller.h"
37 #include "content/public/browser/notification_types.h" 38 #include "content/public/browser/notification_types.h"
38 #include "content/public/browser/render_frame_host.h" 39 #include "content/public/browser/render_frame_host.h"
39 #include "content/public/browser/render_process_host.h" 40 #include "content/public/browser/render_process_host.h"
40 #include "content/public/browser/render_view_host.h" 41 #include "content/public/browser/render_view_host.h"
41 #include "content/public/browser/web_contents.h" 42 #include "content/public/browser/web_contents.h"
42 #include "content/public/test/browser_test_utils.h" 43 #include "content/public/test/browser_test_utils.h"
43 #include "content/public/test/test_browser_thread.h" 44 #include "content/public/test/test_browser_thread.h"
44 #include "content/public/test/test_utils.h" 45 #include "content/public/test/test_utils.h"
46 #include "net/test/url_request/url_request_mock_http_job.h"
45 47
46 using chrome_browser_interstitials::SecurityInterstitialIDNTest; 48 using chrome_browser_interstitials::SecurityInterstitialIDNTest;
47 using content::BrowserThread; 49 using content::BrowserThread;
48 using content::InterstitialPage; 50 using content::InterstitialPage;
49 using content::NavigationController; 51 using content::NavigationController;
50 using content::WebContents; 52 using content::WebContents;
51 53
52 namespace { 54 namespace {
53 55
54 const char kEmptyPage[] = "files/empty.html"; 56 const char kEmptyPage[] = "empty.html";
55 const char kMalwarePage[] = "files/safe_browsing/malware.html"; 57 const char kMalwarePage[] = "safe_browsing/malware.html";
56 const char kMalwareIframe[] = "files/safe_browsing/malware_iframe.html"; 58 const char kMalwareIframe[] = "safe_browsing/malware_iframe.html";
57 59
58 // A SafeBrowsingDatabaseManager class that allows us to inject the malicious 60 // A SafeBrowsingDatabaseManager class that allows us to inject the malicious
59 // URLs. 61 // URLs.
60 class FakeSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { 62 class FakeSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager {
61 public: 63 public:
62 FakeSafeBrowsingDatabaseManager() {} 64 FakeSafeBrowsingDatabaseManager() {}
63 65
64 // Called on the IO thread to check if the given url is safe or not. If we 66 // Called on the IO thread to check if the given url is safe or not. If we
65 // can synchronously determine that the url is safe, CheckUrl returns true. 67 // can synchronously determine that the url is safe, CheckUrl returns true.
66 // Otherwise it returns false, and "client" is called asynchronously with the 68 // Otherwise it returns false, and "client" is called asynchronously with the
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 switches::kForceFieldTrials, "UwSInterstitialStatus/On/"); 376 switches::kForceFieldTrials, "UwSInterstitialStatus/On/");
375 } 377 }
376 378
377 void TearDown() override { 379 void TearDown() override {
378 InProcessBrowserTest::TearDown(); 380 InProcessBrowserTest::TearDown();
379 SafeBrowsingBlockingPage::RegisterFactory(NULL); 381 SafeBrowsingBlockingPage::RegisterFactory(NULL);
380 SafeBrowsingService::RegisterFactory(NULL); 382 SafeBrowsingService::RegisterFactory(NULL);
381 MalwareDetails::RegisterFactory(NULL); 383 MalwareDetails::RegisterFactory(NULL);
382 } 384 }
383 385
384 void SetUpInProcessBrowserTestFixture() override { 386 void SetUpOnMainThread() override {
385 ASSERT_TRUE(test_server()->Start()); 387 BrowserThread::PostTask(
388 BrowserThread::IO, FROM_HERE,
389 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
386 } 390 }
387 391
388 void SetURLThreatType(const GURL& url, SBThreatType threat_type) { 392 void SetURLThreatType(const GURL& url, SBThreatType threat_type) {
389 FakeSafeBrowsingService* service = 393 FakeSafeBrowsingService* service =
390 static_cast<FakeSafeBrowsingService*>( 394 static_cast<FakeSafeBrowsingService*>(
391 g_browser_process->safe_browsing_service()); 395 g_browser_process->safe_browsing_service());
392 396
393 ASSERT_TRUE(service); 397 ASSERT_TRUE(service);
394 service->fake_database_manager()->SetURLThreatType(url, threat_type); 398 service->fake_database_manager()->SetURLThreatType(url, threat_type);
395 } 399 }
396 400
397 // Adds a safebrowsing result of the current test threat to the fake 401 // Adds a safebrowsing result of the current test threat to the fake
398 // safebrowsing service, navigates to that page, and returns the url. 402 // safebrowsing service, navigates to that page, and returns the url.
399 GURL SetupWarningAndNavigate() { 403 GURL SetupWarningAndNavigate() {
400 GURL url = test_server()->GetURL(kEmptyPage); 404 GURL url = net::URLRequestMockHTTPJob::GetMockUrl(kEmptyPage);
401 SetURLThreatType(url, GetParam()); 405 SetURLThreatType(url, GetParam());
402 406
403 ui_test_utils::NavigateToURL(browser(), url); 407 ui_test_utils::NavigateToURL(browser(), url);
404 EXPECT_TRUE(WaitForReady()); 408 EXPECT_TRUE(WaitForReady());
405 return url; 409 return url;
406 } 410 }
407 411
408 // Adds a safebrowsing threat result to the fake safebrowsing service, 412 // Adds a safebrowsing threat result to the fake safebrowsing service,
409 // navigates to a page with an iframe containing the threat site, and returns 413 // navigates to a page with an iframe containing the threat site, and returns
410 // the url of the parent page. 414 // the url of the parent page.
411 GURL SetupThreatIframeWarningAndNavigate() { 415 GURL SetupThreatIframeWarningAndNavigate() {
412 GURL url = test_server()->GetURL(kMalwarePage); 416 GURL url = net::URLRequestMockHTTPJob::GetMockUrl(kMalwarePage);
413 GURL iframe_url = test_server()->GetURL(kMalwareIframe); 417 GURL iframe_url = net::URLRequestMockHTTPJob::GetMockUrl(kMalwareIframe);
414 SetURLThreatType(iframe_url, GetParam()); 418 SetURLThreatType(iframe_url, GetParam());
415 419
416 ui_test_utils::NavigateToURL(browser(), url); 420 ui_test_utils::NavigateToURL(browser(), url);
417 EXPECT_TRUE(WaitForReady()); 421 EXPECT_TRUE(WaitForReady());
418 return url; 422 return url;
419 } 423 }
420 424
421 void SendCommand( 425 void SendCommand(
422 SecurityInterstitialPage::SecurityInterstitialCommands command) { 426 SecurityInterstitialPage::SecurityInterstitialCommands command) {
423 WebContents* contents = 427 WebContents* contents =
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 factory_.most_recent_service() 490 factory_.most_recent_service()
487 ->fake_ui_manager() 491 ->fake_ui_manager()
488 ->set_malware_details_done_callback(callback); 492 ->set_malware_details_done_callback(callback);
489 } 493 }
490 494
491 std::string GetReportSent() { 495 std::string GetReportSent() {
492 return factory_.most_recent_service()->fake_ui_manager()->GetReport(); 496 return factory_.most_recent_service()->fake_ui_manager()->GetReport();
493 } 497 }
494 498
495 void MalwareRedirectCancelAndProceed(const std::string& open_function) { 499 void MalwareRedirectCancelAndProceed(const std::string& open_function) {
496 GURL load_url = test_server()->GetURL( 500 GURL load_url = net::URLRequestMockHTTPJob::GetMockUrl(
497 "files/safe_browsing/interstitial_cancel.html"); 501 "safe_browsing/interstitial_cancel.html");
498 GURL malware_url("http://localhost/files/safe_browsing/malware.html"); 502 GURL malware_url =
503 net::URLRequestMockHTTPJob::GetMockUrl("safe_browsing/malware.html");
mmenke 2015/06/05 15:20:28 kMalwarePage?
mattm 2015/06/05 19:37:45 Done.
499 SetURLThreatType(malware_url, GetParam()); 504 SetURLThreatType(malware_url, GetParam());
500 505
501 // Load the test page. 506 // Load the test page.
502 ui_test_utils::NavigateToURL(browser(), load_url); 507 ui_test_utils::NavigateToURL(browser(), load_url);
503 // Trigger the safe browsing interstitial page via a redirect in 508 // Trigger the safe browsing interstitial page via a redirect in
504 // "openWin()". 509 // "openWin()".
505 ui_test_utils::NavigateToURLWithDisposition( 510 ui_test_utils::NavigateToURLWithDisposition(
506 browser(), 511 browser(),
507 GURL("javascript:" + open_function + "()"), 512 GURL("javascript:" + open_function + "()"),
508 CURRENT_TAB, 513 CURRENT_TAB,
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
784 #if defined(OS_WIN) && defined(USE_ASH) 789 #if defined(OS_WIN) && defined(USE_ASH)
785 // Disable this test in Metro+Ash for now (http://crbug.com/262796). 790 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
786 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 791 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
787 switches::kAshBrowserTests)) 792 switches::kAshBrowserTests))
788 return; 793 return;
789 #endif 794 #endif
790 795
791 browser()->profile()->GetPrefs()->SetBoolean( 796 browser()->profile()->GetPrefs()->SetBoolean(
792 prefs::kSafeBrowsingExtendedReportingEnabled, true); 797 prefs::kSafeBrowsingExtendedReportingEnabled, true);
793 798
794 net::SpawnedTestServer https_server( 799 TestReportingDisabledAndDontProceed(
795 net::SpawnedTestServer::TYPE_HTTPS, net::SpawnedTestServer::kLocalhost, 800 net::URLRequestMockHTTPJob::GetMockHttpsUrl(kEmptyPage));
796 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
797 ASSERT_TRUE(https_server.Start());
798 GURL url = https_server.GetURL(kEmptyPage);
799
800 TestReportingDisabledAndDontProceed(url);
801 } 801 }
802 802
803 // Verifies that the reporting checkbox is hidden when opt-in is 803 // Verifies that the reporting checkbox is hidden when opt-in is
804 // disabled by policy. 804 // disabled by policy.
805 IN_PROC_BROWSER_TEST_P(SafeBrowsingBlockingPageBrowserTest, 805 IN_PROC_BROWSER_TEST_P(SafeBrowsingBlockingPageBrowserTest,
806 ReportingDisabledByPolicy) { 806 ReportingDisabledByPolicy) {
807 #if defined(OS_WIN) && defined(USE_ASH) 807 #if defined(OS_WIN) && defined(USE_ASH)
808 // Disable this test in Metro+Ash for now (http://crbug.com/262796). 808 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
809 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 809 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
810 switches::kAshBrowserTests)) 810 switches::kAshBrowserTests))
811 return; 811 return;
812 #endif 812 #endif
813 813
814 browser()->profile()->GetPrefs()->SetBoolean( 814 browser()->profile()->GetPrefs()->SetBoolean(
815 prefs::kSafeBrowsingExtendedReportingEnabled, true); 815 prefs::kSafeBrowsingExtendedReportingEnabled, true);
816 browser()->profile()->GetPrefs()->SetBoolean( 816 browser()->profile()->GetPrefs()->SetBoolean(
817 prefs::kSafeBrowsingExtendedReportingOptInAllowed, false); 817 prefs::kSafeBrowsingExtendedReportingOptInAllowed, false);
818 818
819 TestReportingDisabledAndDontProceed(test_server()->GetURL(kEmptyPage)); 819 TestReportingDisabledAndDontProceed(
820 net::URLRequestMockHTTPJob::GetMockUrl(kEmptyPage));
820 } 821 }
821 822
822 IN_PROC_BROWSER_TEST_P(SafeBrowsingBlockingPageBrowserTest, LearnMore) { 823 IN_PROC_BROWSER_TEST_P(SafeBrowsingBlockingPageBrowserTest, LearnMore) {
823 SetupWarningAndNavigate(); 824 SetupWarningAndNavigate();
824 EXPECT_TRUE(ClickAndWaitForDetach("help-link")); 825 EXPECT_TRUE(ClickAndWaitForDetach("help-link"));
825 AssertNoInterstitial(false); // Assert the interstitial is gone 826 AssertNoInterstitial(false); // Assert the interstitial is gone
826 827
827 // We are in the help page. 828 // We are in the help page.
828 EXPECT_EQ( 829 EXPECT_EQ(
829 GetParam() == SB_THREAT_TYPE_URL_PHISHING 830 GetParam() == SB_THREAT_TYPE_URL_PHISHING
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
866 IN_PROC_BROWSER_TEST_P(SafeBrowsingBlockingPageIDNTest, 867 IN_PROC_BROWSER_TEST_P(SafeBrowsingBlockingPageIDNTest,
867 SafeBrowsingBlockingPageDecodesIDN) { 868 SafeBrowsingBlockingPageDecodesIDN) {
868 EXPECT_TRUE(VerifyIDNDecoded()); 869 EXPECT_TRUE(VerifyIDNDecoded());
869 } 870 }
870 871
871 INSTANTIATE_TEST_CASE_P(SafeBrowsingBlockingPageIDNTestWithThreatType, 872 INSTANTIATE_TEST_CASE_P(SafeBrowsingBlockingPageIDNTestWithThreatType,
872 SafeBrowsingBlockingPageIDNTest, 873 SafeBrowsingBlockingPageIDNTest,
873 testing::Values(SB_THREAT_TYPE_URL_MALWARE, 874 testing::Values(SB_THREAT_TYPE_URL_MALWARE,
874 SB_THREAT_TYPE_URL_PHISHING, 875 SB_THREAT_TYPE_URL_PHISHING,
875 SB_THREAT_TYPE_URL_UNWANTED)); 876 SB_THREAT_TYPE_URL_UNWANTED));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698