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

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

Issue 2854263003: Add quiet safe browsing interstitial for WebView (Closed)
Patch Set: Fix unit test Created 3 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 #include <list> 5 #include <list>
6 6
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "chrome/browser/interstitials/chrome_controller_client.h" 8 #include "chrome/browser/interstitials/chrome_controller_client.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" 10 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
11 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" 11 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
12 #include "chrome/browser/safe_browsing/ui_manager.h" 12 #include "chrome/browser/safe_browsing/ui_manager.h"
13 #include "chrome/common/pref_names.h" 13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
15 #include "chrome/test/base/testing_browser_process.h" 15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
17 #include "components/grit/components_resources.h"
17 #include "components/prefs/pref_service.h" 18 #include "components/prefs/pref_service.h"
18 #include "components/safe_browsing/browser/threat_details.h" 19 #include "components/safe_browsing/browser/threat_details.h"
19 #include "components/safe_browsing/common/safe_browsing_prefs.h" 20 #include "components/safe_browsing/common/safe_browsing_prefs.h"
21 #include "components/security_interstitials/core/safe_browsing_quiet_error_ui.h"
22 #include "components/strings/grit/components_strings.h"
20 #include "content/public/browser/interstitial_page.h" 23 #include "content/public/browser/interstitial_page.h"
21 #include "content/public/browser/navigation_entry.h" 24 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/browser/render_process_host.h" 25 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
24 #include "content/public/test/web_contents_tester.h" 27 #include "content/public/test/web_contents_tester.h"
25 #include "testing/gmock/include/gmock/gmock.h" 28 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/base/webui/jstemplate_builder.h"
33 #include "ui/base/webui/web_ui_util.h"
27 34
28 using content::InterstitialPage; 35 using content::InterstitialPage;
29 using content::NavigationEntry; 36 using content::NavigationEntry;
30 using content::WebContents; 37 using content::WebContents;
31 using content::WebContentsTester; 38 using content::WebContentsTester;
32 using security_interstitials::BaseSafeBrowsingErrorUI; 39 using security_interstitials::BaseSafeBrowsingErrorUI;
33 40
34 static const char* kGoogleURL = "http://www.google.com/"; 41 static const char* kGoogleURL = "http://www.google.com/";
35 static const char* kGoodURL = "http://www.goodguys.com/"; 42 static const char* kGoodURL = "http://www.goodguys.com/";
36 static const char* kBadURL = "http://www.badguys.com/"; 43 static const char* kBadURL = "http://www.badguys.com/";
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 }; 101 };
95 102
96 class MockTestingProfile : public TestingProfile { 103 class MockTestingProfile : public TestingProfile {
97 public: 104 public:
98 MockTestingProfile() {} 105 MockTestingProfile() {}
99 virtual ~MockTestingProfile() {} 106 virtual ~MockTestingProfile() {}
100 107
101 MOCK_CONST_METHOD0(IsOffTheRecord, bool()); 108 MOCK_CONST_METHOD0(IsOffTheRecord, bool());
102 }; 109 };
103 110
111 class TestSafeBrowsingBlockingPageQuiet : public SafeBrowsingBlockingPage {
112 public:
113 TestSafeBrowsingBlockingPageQuiet(
114 BaseUIManager* manager,
115 WebContents* web_contents,
116 const GURL& main_frame_url,
117 const UnsafeResourceList& unsafe_resources,
118 const BaseSafeBrowsingErrorUI::SBErrorDisplayOptions& display_options)
119 : SafeBrowsingBlockingPage(manager,
120 web_contents,
121 main_frame_url,
122 unsafe_resources,
123 display_options),
124 sb_error_ui_(unsafe_resources[0].url,
125 main_frame_url,
126 GetInterstitialReason(unsafe_resources),
127 display_options,
128 manager->app_locale(),
129 base::Time::NowFromSystemTime(),
130 controller(),
131 false) {
132 // Don't delay details at all for the unittest.
133 SetThreatDetailsProceedDelayForTesting(0);
134 DontCreateViewForTesting();
135 }
136
137 // Manually specify that the WebView extends beyond viewing bounds.
138 void SetGiantWebView() { sb_error_ui_.SetGiantWebViewForTesting(true); }
139
140 base::DictionaryValue GetUIStrings() {
141 base::DictionaryValue load_time_data;
142 sb_error_ui_.PopulateStringsForHtml(&load_time_data);
143 webui::SetLoadTimeDataDefaults(controller()->GetApplicationLocale(),
144 &load_time_data);
145 return load_time_data;
146 }
147
148 security_interstitials::SafeBrowsingQuietErrorUI sb_error_ui_;
149 };
150
151 // TODO(edwardjung): Refactor into TestSafeBrowsingBlockingPageFactory.
152 class TestSafeBrowsingBlockingQuietPageFactory
153 : public SafeBrowsingBlockingPageFactory {
154 public:
155 TestSafeBrowsingBlockingQuietPageFactory() {}
156 ~TestSafeBrowsingBlockingQuietPageFactory() override {}
157
158 SafeBrowsingBlockingPage* CreateSafeBrowsingPage(
159 BaseUIManager* manager,
160 WebContents* web_contents,
161 const GURL& main_frame_url,
162 const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources)
163 override {
164 PrefService* prefs =
165 Profile::FromBrowserContext(web_contents->GetBrowserContext())
166 ->GetPrefs();
167 bool is_extended_reporting_opt_in_allowed =
168 prefs->GetBoolean(prefs::kSafeBrowsingExtendedReportingOptInAllowed);
169 bool is_proceed_anyway_disabled =
170 prefs->GetBoolean(prefs::kSafeBrowsingProceedAnywayDisabled);
171 BaseSafeBrowsingErrorUI::SBErrorDisplayOptions display_options(
172 BaseBlockingPage::IsMainPageLoadBlocked(unsafe_resources),
173 is_extended_reporting_opt_in_allowed,
174 web_contents->GetBrowserContext()->IsOffTheRecord(),
175 IsExtendedReportingEnabled(*prefs), IsScout(*prefs),
176 is_proceed_anyway_disabled,
177 BaseBlockingPage::IsMainPageLoadBlocked(unsafe_resources));
178 return new TestSafeBrowsingBlockingPageQuiet(
179 manager, web_contents, main_frame_url, unsafe_resources,
180 display_options);
181 }
182 };
183
104 } // namespace 184 } // namespace
105 185
106 class SafeBrowsingBlockingPageTest : public ChromeRenderViewHostTestHarness { 186 class SafeBrowsingBlockingPageTest : public ChromeRenderViewHostTestHarness {
107 public: 187 public:
108 // The decision the user made. 188 // The decision the user made.
109 enum UserResponse { 189 enum UserResponse {
110 PENDING, 190 PENDING,
111 OK, 191 OK,
112 CANCEL 192 CANCEL
113 }; 193 };
(...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 868
789 // The interstitial should be gone. 869 // The interstitial should be gone.
790 EXPECT_EQ(CANCEL, user_response()); 870 EXPECT_EQ(CANCEL, user_response());
791 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); 871 EXPECT_FALSE(GetSafeBrowsingBlockingPage());
792 872
793 // No report should have been sent. 873 // No report should have been sent.
794 EXPECT_EQ(0u, ui_manager_->GetThreatDetails()->size()); 874 EXPECT_EQ(0u, ui_manager_->GetThreatDetails()->size());
795 ui_manager_->GetThreatDetails()->clear(); 875 ui_manager_->GetThreatDetails()->clear();
796 } 876 }
797 877
878 class SafeBrowsingBlockingQuietPageTest
879 : public ChromeRenderViewHostTestHarness {
880 public:
881 // The decision the user made.
882 enum UserResponse { PENDING, OK, CANCEL };
883
884 SafeBrowsingBlockingQuietPageTest() {
885 // The safe browsing UI manager does not need a service for this test.
886 ui_manager_ = new TestSafeBrowsingUIManager(NULL);
887 }
888
889 void SetUp() override {
890 ChromeRenderViewHostTestHarness::SetUp();
891
892 SafeBrowsingBlockingPage::RegisterFactory(&factory_);
893 SafeBrowsingUIManager::CreateWhitelistForTesting(web_contents());
894
895 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory;
896 sb_service_factory.SetTestUIManager(ui_manager_.get());
897 auto* safe_browsing_service =
898 sb_service_factory.CreateSafeBrowsingService();
899 // A profile was created already but SafeBrowsingService wasn't around to
900 // get notified of it, so include that notification now.
901 safe_browsing_service->AddPrefService(
902 Profile::FromBrowserContext(web_contents()->GetBrowserContext())
903 ->GetPrefs());
904 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(
905 safe_browsing_service);
906 g_browser_process->safe_browsing_service()->Initialize();
907 }
908
909 void TearDown() override {
910 // Release the UI manager before the BrowserThreads are destroyed.
911 ui_manager_ = NULL;
912 TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown();
913 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr);
914 SafeBrowsingBlockingPage::RegisterFactory(NULL);
915 // Clean up singleton reference (crbug.com/110594).
916 ThreatDetails::RegisterFactory(NULL);
917 ChromeRenderViewHostTestHarness::TearDown();
918 }
919
920 void OnBlockingPageComplete(bool proceed) {
921 if (proceed)
922 user_response_ = OK;
923 else
924 user_response_ = CANCEL;
925 }
926
927 void ShowInterstitial(bool is_subresource,
928 const char* url,
929 SBThreatType type) {
930 security_interstitials::UnsafeResource resource;
931 InitResource(&resource, is_subresource, GURL(url), type);
932 SafeBrowsingBlockingPage::ShowBlockingPage(ui_manager_.get(), resource);
933 }
934
935 // Returns the SafeBrowsingBlockingPage currently showing or NULL if none is
936 // showing.
937 TestSafeBrowsingBlockingPageQuiet* GetSafeBrowsingBlockingPage() {
938 InterstitialPage* interstitial =
939 InterstitialPage::GetInterstitialPage(web_contents());
940 if (!interstitial)
941 return NULL;
942 return static_cast<TestSafeBrowsingBlockingPageQuiet*>(
943 interstitial->GetDelegateForTesting());
944 }
945
946 scoped_refptr<TestSafeBrowsingUIManager> ui_manager_;
947
948 // Owned by TestSafeBrowsingBlockingQuietPage.
949 MockTestingProfile* mock_profile_;
950
951 private:
952 void InitResource(security_interstitials::UnsafeResource* resource,
953 bool is_subresource,
954 const GURL& url,
955 SBThreatType type) {
956 resource->callback =
957 base::Bind(&SafeBrowsingBlockingQuietPageTest::OnBlockingPageComplete,
958 base::Unretained(this));
959 resource->callback_thread = content::BrowserThread::GetTaskRunnerForThread(
960 content::BrowserThread::IO);
961 resource->url = url;
962 resource->is_subresource = is_subresource;
963 resource->threat_type = type;
964 resource->web_contents_getter =
965 security_interstitials::UnsafeResource::GetWebContentsGetter(
966 web_contents()->GetRenderProcessHost()->GetID(),
967 web_contents()->GetMainFrame()->GetRoutingID());
968 resource->threat_source = safe_browsing::ThreatSource::LOCAL_PVER3;
969 }
970
971 UserResponse user_response_;
972 TestSafeBrowsingBlockingQuietPageFactory factory_;
973 };
974
975 // Tests showing a quiet blocking page for a malware page.
976 TEST_F(SafeBrowsingBlockingQuietPageTest, MalwarePage) {
977 // Start a load.
978 controller().LoadURL(GURL(kBadURL), content::Referrer(),
979 ui::PAGE_TRANSITION_TYPED, std::string());
980
981 // Simulate the load causing a safe browsing interstitial to be shown.
982 ShowInterstitial(false, kBadURL, SB_THREAT_TYPE_URL_MALWARE);
983 TestSafeBrowsingBlockingPageQuiet* sb_interstitial =
984 GetSafeBrowsingBlockingPage();
985 ASSERT_TRUE(sb_interstitial);
986
987 base::DictionaryValue load_time_data = sb_interstitial->GetUIStrings();
988 base::string16 str;
989 load_time_data.GetString("heading", &str);
990 EXPECT_EQ(str, l10n_util::GetStringUTF16(IDS_MALWARE_WEBVIEW_HEADING));
991 bool is_giant;
992 load_time_data.GetBoolean("is_giant", &is_giant);
993 EXPECT_FALSE(is_giant);
994 }
995
996 // Tests showing a quiet blocking page for a phishing page.
997 TEST_F(SafeBrowsingBlockingQuietPageTest, PhishingPage) {
998 // Start a load.
999 controller().LoadURL(GURL(kBadURL), content::Referrer(),
1000 ui::PAGE_TRANSITION_TYPED, std::string());
1001
1002 // Simulate the load causing a safe browsing interstitial to be shown.
1003 ShowInterstitial(false, kBadURL, SB_THREAT_TYPE_URL_PHISHING);
1004 TestSafeBrowsingBlockingPageQuiet* sb_interstitial =
1005 GetSafeBrowsingBlockingPage();
1006 ASSERT_TRUE(sb_interstitial);
1007
1008 base::DictionaryValue load_time_data = sb_interstitial->GetUIStrings();
1009 base::string16 str;
1010 load_time_data.GetString("heading", &str);
1011 EXPECT_EQ(str, l10n_util::GetStringUTF16(IDS_PHISHING_WEBVIEW_HEADING));
1012 bool is_giant;
1013 load_time_data.GetBoolean("is_giant", &is_giant);
1014 EXPECT_FALSE(is_giant);
1015 }
1016
1017 // Tests showing a quiet blocking page in a giant webview.
1018 TEST_F(SafeBrowsingBlockingQuietPageTest, GiantWebView) {
1019 // Start a load.
1020 controller().LoadURL(GURL(kBadURL), content::Referrer(),
1021 ui::PAGE_TRANSITION_TYPED, std::string());
1022
1023 // Simulate the load causing a safe browsing interstitial to be shown.
1024 ShowInterstitial(false, kBadURL, SB_THREAT_TYPE_URL_MALWARE);
1025 TestSafeBrowsingBlockingPageQuiet* sb_interstitial =
1026 GetSafeBrowsingBlockingPage();
1027 EXPECT_TRUE(sb_interstitial);
1028
1029 sb_interstitial->SetGiantWebView();
1030 base::DictionaryValue load_time_data = sb_interstitial->GetUIStrings();
1031 bool is_giant;
1032 load_time_data.GetBoolean("is_giant", &is_giant);
1033 EXPECT_TRUE(is_giant);
1034 }
1035
798 } // namespace safe_browsing 1036 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/safe_browsing_blocking_page.h ('k') | chrome/browser/safe_browsing/safe_browsing_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698