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

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