blob: 83e6c2c387cacb252095f4e5960b98af97fceaed [file] [log] [blame]
feltbc2eda2d2015-06-23 02:06:031// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dcheng7bacc0e2016-04-11 20:10:545#include "chrome/browser/safe_browsing/ui_manager.h"
6
clamy4edbf0e2015-12-02 13:35:417#include "base/run_loop.h"
dalecurtis6c58ed02016-10-28 23:02:378#include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
Luke Zielinski12ef88552017-06-23 15:36:279#include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
scottmg22e4f25a2016-08-15 21:09:0310#include "chrome/browser/safe_browsing/ui_manager.h"
feltfb118572015-08-18 05:22:0111#include "chrome/test/base/chrome_render_view_host_test_harness.h"
Luke Zielinski12ef88552017-06-23 15:36:2712#include "chrome/test/base/testing_browser_process.h"
feltfb118572015-08-18 05:22:0113#include "chrome/test/base/testing_profile.h"
timvolodine89cf11712017-05-15 18:05:0714#include "components/safe_browsing/common/safe_browsing_prefs.h"
vitaliii58e1e962017-09-20 08:27:2115#include "components/safe_browsing_db/util.h"
edwardjungd7395fb02017-05-12 23:13:2916#include "components/security_interstitials/core/base_safe_browsing_error_ui.h"
estark1ca09ca2016-11-01 04:04:1217#include "content/public/browser/navigation_entry.h"
feltfb118572015-08-18 05:22:0118#include "content/public/browser/render_process_host.h"
19#include "content/public/browser/render_view_host.h"
20#include "content/public/browser/web_contents.h"
dalecurtis6c58ed02016-10-28 23:02:3721#include "content/public/browser/web_contents_delegate.h"
clamy511cf022017-08-23 14:11:0622#include "content/public/test/navigation_simulator.h"
feltbc2eda2d2015-06-23 02:06:0323#include "content/public/test/test_browser_thread_bundle.h"
feltfb118572015-08-18 05:22:0124#include "content/public/test/web_contents_tester.h"
feltbc2eda2d2015-06-23 02:06:0325#include "testing/gtest/include/gtest/gtest.h"
26#include "url/gurl.h"
27
clamy4edbf0e2015-12-02 13:35:4128using content::BrowserThread;
29
feltfb118572015-08-18 05:22:0130static const char* kGoodURL = "https://www.good.com";
31static const char* kBadURL = "https://www.malware.com";
32static const char* kBadURLWithPath = "https://www.malware.com/index.html";
mattmbfc4060d2015-12-18 23:11:3833static const char* kAnotherBadURL = "https://www.badware.com";
34static const char* kLandingURL = "https://www.landing.com";
feltfb118572015-08-18 05:22:0135
vakh9a474d832015-11-13 01:43:0936namespace safe_browsing {
37
clamy4edbf0e2015-12-02 13:35:4138class SafeBrowsingCallbackWaiter {
39 public:
40 SafeBrowsingCallbackWaiter() {}
41
42 bool callback_called() const { return callback_called_; }
43 bool proceed() const { return proceed_; }
44
45 void OnBlockingPageDone(bool proceed) {
46 DCHECK_CURRENTLY_ON(BrowserThread::UI);
47 callback_called_ = true;
48 proceed_ = proceed;
49 loop_.Quit();
50 }
51
52 void OnBlockingPageDoneOnIO(bool proceed) {
53 DCHECK_CURRENTLY_ON(BrowserThread::IO);
54 BrowserThread::PostTask(
55 BrowserThread::UI, FROM_HERE,
tzikb5f84b82017-04-20 00:55:2856 base::BindOnce(&SafeBrowsingCallbackWaiter::OnBlockingPageDone,
57 base::Unretained(this), proceed));
clamy4edbf0e2015-12-02 13:35:4158 }
59
60 void WaitForCallback() {
61 DCHECK_CURRENTLY_ON(BrowserThread::UI);
62 loop_.Run();
63 }
64
65 private:
66 bool callback_called_ = false;
67 bool proceed_ = false;
68 base::RunLoop loop_;
69};
70
feltfb118572015-08-18 05:22:0171class SafeBrowsingUIManagerTest : public ChromeRenderViewHostTestHarness {
feltbc2eda2d2015-06-23 02:06:0372 public:
Gabriel Charettecc8362b2017-09-20 21:59:4073 SafeBrowsingUIManagerTest()
74 : ChromeRenderViewHostTestHarness(
75 content::TestBrowserThreadBundle::REAL_IO_THREAD),
76 ui_manager_(new SafeBrowsingUIManager(NULL)) {}
feltfb118572015-08-18 05:22:0177
juncai1ee189bd2017-06-09 04:25:4378 ~SafeBrowsingUIManagerTest() override {}
feltbc2eda2d2015-06-23 02:06:0379
clamy4edbf0e2015-12-02 13:35:4180 void SetUp() override {
clamy4edbf0e2015-12-02 13:35:4181 ChromeRenderViewHostTestHarness::SetUp();
estark1ca09ca2016-11-01 04:04:1282 SafeBrowsingUIManager::CreateWhitelistForTesting(web_contents());
Luke Zielinski12ef88552017-06-23 15:36:2783
84 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory;
85 auto* safe_browsing_service =
86 sb_service_factory.CreateSafeBrowsingService();
87 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(
88 safe_browsing_service);
89 g_browser_process->safe_browsing_service()->Initialize();
90 // A profile was created already but SafeBrowsingService wasn't around to
91 // get notified of it, so include that notification now.
92 safe_browsing_service->AddPrefService(
93 Profile::FromBrowserContext(web_contents()->GetBrowserContext())
94 ->GetPrefs());
clamy4edbf0e2015-12-02 13:35:4195 }
feltfb118572015-08-18 05:22:0196
Luke Zielinski12ef88552017-06-23 15:36:2797 void TearDown() override {
98 TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown();
99 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr);
100 ChromeRenderViewHostTestHarness::TearDown();
101 }
feltfb118572015-08-18 05:22:01102
jialiul792a6662016-12-03 01:44:10103 bool IsWhitelisted(security_interstitials::UnsafeResource resource) {
feltbc2eda2d2015-06-23 02:06:03104 return ui_manager_->IsWhitelisted(resource);
105 }
106
jialiul792a6662016-12-03 01:44:10107 void AddToWhitelist(security_interstitials::UnsafeResource resource) {
estark1ca09ca2016-11-01 04:04:12108 ui_manager_->AddToWhitelistUrlSet(
109 SafeBrowsingUIManager::GetMainFrameWhitelistUrlForResourceForTesting(
110 resource),
estark7ffa8c62016-11-11 23:21:55111 web_contents(), false, resource.threat_type);
feltfb118572015-08-18 05:22:01112 }
113
jialiul792a6662016-12-03 01:44:10114 security_interstitials::UnsafeResource MakeUnsafeResource(
mattmbfc4060d2015-12-18 23:11:38115 const char* url,
116 bool is_subresource) {
jialiul792a6662016-12-03 01:44:10117 security_interstitials::UnsafeResource resource;
feltfb118572015-08-18 05:22:01118 resource.url = GURL(url);
mattmbfc4060d2015-12-18 23:11:38119 resource.is_subresource = is_subresource;
scottmg22e4f25a2016-08-15 21:09:03120 resource.web_contents_getter =
jialiul792a6662016-12-03 01:44:10121 security_interstitials::UnsafeResource::GetWebContentsGetter(
scottmg22e4f25a2016-08-15 21:09:03122 web_contents()->GetRenderProcessHost()->GetID(),
123 web_contents()->GetMainFrame()->GetRoutingID());
feltfb118572015-08-18 05:22:01124 resource.threat_type = SB_THREAT_TYPE_URL_MALWARE;
125 return resource;
126 }
127
jialiul792a6662016-12-03 01:44:10128 security_interstitials::UnsafeResource MakeUnsafeResourceAndStartNavigation(
feltfb118572015-08-18 05:22:01129 const char* url) {
jialiul792a6662016-12-03 01:44:10130 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38131 MakeUnsafeResource(url, false /* is_subresource */);
feltfb118572015-08-18 05:22:01132
mattmbfc4060d2015-12-18 23:11:38133 // The WC doesn't have a URL without a navigation. A main-frame malware
134 // unsafe resource must be a pending navigation.
clamy511cf022017-08-23 14:11:06135 auto navigation = content::NavigationSimulator::CreateBrowserInitiated(
136 GURL(url), web_contents());
137 navigation->Start();
feltfb118572015-08-18 05:22:01138 return resource;
feltbc2eda2d2015-06-23 02:06:03139 }
140
clamy4edbf0e2015-12-02 13:35:41141 void SimulateBlockingPageDone(
jialiul792a6662016-12-03 01:44:10142 const std::vector<security_interstitials::UnsafeResource>& resources,
clamy4edbf0e2015-12-02 13:35:41143 bool proceed) {
estark1ca09ca2016-11-01 04:04:12144 GURL main_frame_url;
145 content::NavigationEntry* entry =
146 web_contents()->GetController().GetVisibleEntry();
147 if (entry)
148 main_frame_url = entry->GetURL();
149
150 ui_manager_->OnBlockingPageDone(resources, proceed, web_contents(),
151 main_frame_url);
clamy4edbf0e2015-12-02 13:35:41152 }
153
dalecurtis6c58ed02016-10-28 23:02:37154 protected:
155 SafeBrowsingUIManager* ui_manager() { return ui_manager_.get(); }
156
feltbc2eda2d2015-06-23 02:06:03157 private:
158 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
feltbc2eda2d2015-06-23 02:06:03159};
160
Marc Treib3d26e922017-08-14 16:58:26161// Leaks memory. https://crbug.com/755118
162#if defined(LEAK_SANITIZER)
163#define MAYBE_Whitelist DISABLED_Whitelist
164#else
165#define MAYBE_Whitelist Whitelist
166#endif
167TEST_F(SafeBrowsingUIManagerTest, MAYBE_Whitelist) {
jialiul792a6662016-12-03 01:44:10168 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38169 MakeUnsafeResourceAndStartNavigation(kBadURL);
feltbc2eda2d2015-06-23 02:06:03170 AddToWhitelist(resource);
171 EXPECT_TRUE(IsWhitelisted(resource));
172}
173
Marc Treib3d26e922017-08-14 16:58:26174// Leaks memory. https://crbug.com/755118
175#if defined(LEAK_SANITIZER)
176#define MAYBE_WhitelistIgnoresSitesNotAdded \
177 DISABLED_WhitelistIgnoresSitesNotAdded
178#else
179#define MAYBE_WhitelistIgnoresSitesNotAdded WhitelistIgnoresSitesNotAdded
180#endif
181TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresSitesNotAdded) {
jialiul792a6662016-12-03 01:44:10182 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38183 MakeUnsafeResourceAndStartNavigation(kGoodURL);
feltbc2eda2d2015-06-23 02:06:03184 EXPECT_FALSE(IsWhitelisted(resource));
feltfb118572015-08-18 05:22:01185}
186
Marc Treib3d26e922017-08-14 16:58:26187// Leaks memory. https://crbug.com/755118
188#if defined(LEAK_SANITIZER)
189#define MAYBE_WhitelistRemembersThreatType DISABLED_WhitelistRemembersThreatType
190#else
191#define MAYBE_WhitelistRemembersThreatType WhitelistRemembersThreatType
192#endif
193TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistRemembersThreatType) {
jialiul792a6662016-12-03 01:44:10194 security_interstitials::UnsafeResource resource =
estark7ffa8c62016-11-11 23:21:55195 MakeUnsafeResourceAndStartNavigation(kBadURL);
196 AddToWhitelist(resource);
197 EXPECT_TRUE(IsWhitelisted(resource));
198 SBThreatType threat_type;
199 content::NavigationEntry* entry =
200 web_contents()->GetController().GetVisibleEntry();
201 ASSERT_TRUE(entry);
202 EXPECT_TRUE(ui_manager()->IsUrlWhitelistedOrPendingForWebContents(
203 resource.url, resource.is_subresource, entry,
204 resource.web_contents_getter.Run(), true, &threat_type));
205 EXPECT_EQ(resource.threat_type, threat_type);
206}
207
Marc Treib3d26e922017-08-14 16:58:26208// Leaks memory. https://crbug.com/755118
209#if defined(LEAK_SANITIZER)
210#define MAYBE_WhitelistIgnoresPath DISABLED_WhitelistIgnoresPath
211#else
212#define MAYBE_WhitelistIgnoresPath WhitelistIgnoresPath
213#endif
214TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresPath) {
jialiul792a6662016-12-03 01:44:10215 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38216 MakeUnsafeResourceAndStartNavigation(kBadURL);
feltbc2eda2d2015-06-23 02:06:03217 AddToWhitelist(resource);
218 EXPECT_TRUE(IsWhitelisted(resource));
feltfb118572015-08-18 05:22:01219
mattmbfc4060d2015-12-18 23:11:38220 content::WebContentsTester::For(web_contents())->CommitPendingNavigation();
221
jialiul792a6662016-12-03 01:44:10222 security_interstitials::UnsafeResource resource_path =
mattmbfc4060d2015-12-18 23:11:38223 MakeUnsafeResourceAndStartNavigation(kBadURLWithPath);
feltfb118572015-08-18 05:22:01224 EXPECT_TRUE(IsWhitelisted(resource_path));
feltbc2eda2d2015-06-23 02:06:03225}
226
Marc Treib3d26e922017-08-14 16:58:26227// Leaks memory. https://crbug.com/755118
228#if defined(LEAK_SANITIZER)
229#define MAYBE_WhitelistIgnoresThreatType DISABLED_WhitelistIgnoresThreatType
230#else
231#define MAYBE_WhitelistIgnoresThreatType WhitelistIgnoresThreatType
232#endif
233TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresThreatType) {
jialiul792a6662016-12-03 01:44:10234 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38235 MakeUnsafeResourceAndStartNavigation(kBadURL);
feltfb118572015-08-18 05:22:01236 AddToWhitelist(resource);
237 EXPECT_TRUE(IsWhitelisted(resource));
feltbc2eda2d2015-06-23 02:06:03238
jialiul792a6662016-12-03 01:44:10239 security_interstitials::UnsafeResource resource_phishing =
mattmbfc4060d2015-12-18 23:11:38240 MakeUnsafeResource(kBadURL, false /* is_subresource */);
feltfb118572015-08-18 05:22:01241 resource_phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING;
242 EXPECT_TRUE(IsWhitelisted(resource_phishing));
feltbc2eda2d2015-06-23 02:06:03243}
244
Marc Treib3d26e922017-08-14 16:58:26245// Leaks memory. https://crbug.com/755118
246#if defined(LEAK_SANITIZER)
247#define MAYBE_WhitelistWithUnrelatedPendingLoad \
248 DISABLED_WhitelistWithUnrelatedPendingLoad
249#else
250#define MAYBE_WhitelistWithUnrelatedPendingLoad \
251 WhitelistWithUnrelatedPendingLoad
252#endif
253TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistWithUnrelatedPendingLoad) {
mattmbfc4060d2015-12-18 23:11:38254 // Commit load of landing page.
255 NavigateAndCommit(GURL(kLandingURL));
clamy511cf022017-08-23 14:11:06256 auto unrelated_navigation =
257 content::NavigationSimulator::CreateBrowserInitiated(GURL(kGoodURL),
258 web_contents());
mattmbfc4060d2015-12-18 23:11:38259 {
260 // Simulate subresource malware hit on the landing page.
jialiul792a6662016-12-03 01:44:10261 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38262 MakeUnsafeResource(kBadURL, true /* is_subresource */);
263
264 // Start pending load to unrelated site.
clamy511cf022017-08-23 14:11:06265 unrelated_navigation->Start();
mattmbfc4060d2015-12-18 23:11:38266
267 // Whitelist the resource on the landing page.
268 AddToWhitelist(resource);
269 EXPECT_TRUE(IsWhitelisted(resource));
270 }
271
272 // Commit the pending load of unrelated site.
clamy511cf022017-08-23 14:11:06273 unrelated_navigation->Commit();
mattmbfc4060d2015-12-18 23:11:38274 {
275 // The unrelated site is not on the whitelist, even if the same subresource
276 // was on it.
jialiul792a6662016-12-03 01:44:10277 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38278 MakeUnsafeResource(kBadURL, true /* is_subresource */);
279 EXPECT_FALSE(IsWhitelisted(resource));
280 }
281
282 // Navigate back to the original landing url.
283 NavigateAndCommit(GURL(kLandingURL));
284 {
jialiul792a6662016-12-03 01:44:10285 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38286 MakeUnsafeResource(kBadURL, true /* is_subresource */);
287 // Original resource url is whitelisted.
288 EXPECT_TRUE(IsWhitelisted(resource));
289 }
290 {
291 // A different malware subresource on the same page is also whitelisted.
292 // (The whitelist is by the page url, not the resource url.)
jialiul792a6662016-12-03 01:44:10293 security_interstitials::UnsafeResource resource2 =
mattmbfc4060d2015-12-18 23:11:38294 MakeUnsafeResource(kAnotherBadURL, true /* is_subresource */);
295 EXPECT_TRUE(IsWhitelisted(resource2));
296 }
297}
298
Evgenii Stepanov199c0132017-08-18 21:19:41299// Leaks memory. https://crbug.com/755118
300#if defined(LEAK_SANITIZER)
301#define MAYBE_UICallbackProceed DISABLED_UICallbackProceed
302#else
303#define MAYBE_UICallbackProceed UICallbackProceed
304#endif
305TEST_F(SafeBrowsingUIManagerTest, MAYBE_UICallbackProceed) {
jialiul792a6662016-12-03 01:44:10306 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38307 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41308 SafeBrowsingCallbackWaiter waiter;
309 resource.callback =
310 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone,
311 base::Unretained(&waiter));
312 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12313 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI);
jialiul792a6662016-12-03 01:44:10314 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41315 resources.push_back(resource);
316 SimulateBlockingPageDone(resources, true);
317 EXPECT_TRUE(IsWhitelisted(resource));
318 waiter.WaitForCallback();
319 EXPECT_TRUE(waiter.callback_called());
320 EXPECT_TRUE(waiter.proceed());
321}
322
Evgenii Stepanov199c0132017-08-18 21:19:41323// Leaks memory. https://crbug.com/755118
324#if defined(LEAK_SANITIZER)
325#define MAYBE_UICallbackDontProceed DISABLED_UICallbackDontProceed
326#else
327#define MAYBE_UICallbackDontProceed UICallbackDontProceed
328#endif
329TEST_F(SafeBrowsingUIManagerTest, MAYBE_UICallbackDontProceed) {
jialiul792a6662016-12-03 01:44:10330 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38331 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41332 SafeBrowsingCallbackWaiter waiter;
333 resource.callback =
334 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone,
335 base::Unretained(&waiter));
336 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12337 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI);
jialiul792a6662016-12-03 01:44:10338 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41339 resources.push_back(resource);
340 SimulateBlockingPageDone(resources, false);
341 EXPECT_FALSE(IsWhitelisted(resource));
342 waiter.WaitForCallback();
343 EXPECT_TRUE(waiter.callback_called());
344 EXPECT_FALSE(waiter.proceed());
345}
346
Evgenii Stepanov199c0132017-08-18 21:19:41347// Leaks memory. https://crbug.com/755118
348#if defined(LEAK_SANITIZER)
349#define MAYBE_IOCallbackProceed DISABLED_IOCallbackProceed
350#else
351#define MAYBE_IOCallbackProceed IOCallbackProceed
352#endif
353TEST_F(SafeBrowsingUIManagerTest, MAYBE_IOCallbackProceed) {
jialiul792a6662016-12-03 01:44:10354 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38355 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41356 SafeBrowsingCallbackWaiter waiter;
357 resource.callback =
358 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO,
359 base::Unretained(&waiter));
360 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12361 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
jialiul792a6662016-12-03 01:44:10362 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41363 resources.push_back(resource);
364 SimulateBlockingPageDone(resources, true);
365 EXPECT_TRUE(IsWhitelisted(resource));
366 waiter.WaitForCallback();
367 EXPECT_TRUE(waiter.callback_called());
368 EXPECT_TRUE(waiter.proceed());
369}
370
Evgenii Stepanov199c0132017-08-18 21:19:41371// Leaks memory. https://crbug.com/755118
372#if defined(LEAK_SANITIZER)
373#define MAYBE_IOCallbackDontProceed DISABLED_IOCallbackDontProceed
374#else
375#define MAYBE_IOCallbackDontProceed IOCallbackDontProceed
376#endif
377TEST_F(SafeBrowsingUIManagerTest, MAYBE_IOCallbackDontProceed) {
jialiul792a6662016-12-03 01:44:10378 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38379 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41380 SafeBrowsingCallbackWaiter waiter;
381 resource.callback =
382 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO,
383 base::Unretained(&waiter));
384 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12385 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
jialiul792a6662016-12-03 01:44:10386 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41387 resources.push_back(resource);
388 SimulateBlockingPageDone(resources, false);
389 EXPECT_FALSE(IsWhitelisted(resource));
390 waiter.WaitForCallback();
391 EXPECT_TRUE(waiter.callback_called());
392 EXPECT_FALSE(waiter.proceed());
393}
394
dalecurtis6c58ed02016-10-28 23:02:37395namespace {
396
397// A WebContentsDelegate that records whether
398// VisibleSecurityStateChanged() was called.
399class SecurityStateWebContentsDelegate : public content::WebContentsDelegate {
400 public:
401 SecurityStateWebContentsDelegate() {}
402 ~SecurityStateWebContentsDelegate() override {}
403
404 bool visible_security_state_changed() const {
405 return visible_security_state_changed_;
406 }
407
408 void ClearVisibleSecurityStateChanged() {
409 visible_security_state_changed_ = false;
410 }
411
412 // WebContentsDelegate:
413 void VisibleSecurityStateChanged(content::WebContents* source) override {
414 visible_security_state_changed_ = true;
415 }
416
417 private:
418 bool visible_security_state_changed_ = false;
419 DISALLOW_COPY_AND_ASSIGN(SecurityStateWebContentsDelegate);
420};
421
422// A test blocking page that does not create windows.
423class TestSafeBrowsingBlockingPage : public SafeBrowsingBlockingPage {
424 public:
jialiul3d6032e2017-01-12 00:41:31425 TestSafeBrowsingBlockingPage(BaseUIManager* manager,
dalecurtis6c58ed02016-10-28 23:02:37426 content::WebContents* web_contents,
427 const GURL& main_frame_url,
428 const UnsafeResourceList& unsafe_resources)
jialiul3d6032e2017-01-12 00:41:31429 : SafeBrowsingBlockingPage(
430 manager,
431 web_contents,
432 main_frame_url,
433 unsafe_resources,
edwardjungd7395fb02017-05-12 23:13:29434 BaseSafeBrowsingErrorUI::SBErrorDisplayOptions(
ntfschra5448fa2017-02-02 01:01:31435 BaseBlockingPage::IsMainPageLoadBlocked(unsafe_resources),
Nate Fischer2820c5be2017-06-27 23:04:46436 false, // is_extended_reporting_opt_in_allowed
437 false, // is_off_the_record
438 false, // is_extended_reporting_enabled
439 false, // is_scout_reporting_enabled
440 false, // is_proceed_anyway_disabled
441 true, // should_open_links_in_new_tab
Edward Jungc06541d2017-09-12 09:36:19442 true, // always_show_back_to_safety
ntfschre952a3e2017-05-19 19:15:30443 "cpn_safe_browsing")) { // help_center_article_link
dalecurtis6c58ed02016-10-28 23:02:37444 // Don't delay details at all for the unittest.
ntfschrfef42f92017-02-24 02:15:47445 SetThreatDetailsProceedDelayForTesting(0);
dalecurtis6c58ed02016-10-28 23:02:37446 DontCreateViewForTesting();
447 }
448};
449
450// A factory that creates TestSafeBrowsingBlockingPages.
451class TestSafeBrowsingBlockingPageFactory
452 : public SafeBrowsingBlockingPageFactory {
453 public:
454 TestSafeBrowsingBlockingPageFactory() {}
455 ~TestSafeBrowsingBlockingPageFactory() override {}
456
457 SafeBrowsingBlockingPage* CreateSafeBrowsingPage(
jialiul3d6032e2017-01-12 00:41:31458 BaseUIManager* delegate,
dalecurtis6c58ed02016-10-28 23:02:37459 content::WebContents* web_contents,
460 const GURL& main_frame_url,
461 const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources)
462 override {
463 return new TestSafeBrowsingBlockingPage(delegate, web_contents,
464 main_frame_url, unsafe_resources);
465 }
466};
467
468} // namespace
469
470// Tests that the WebContentsDelegate is notified of a visible security
471// state change when a blocking page is shown for a subresource.
Evgenii Stepanov199c0132017-08-18 21:19:41472// Leaks memory. https://crbug.com/755118
473#if defined(LEAK_SANITIZER)
474#define MAYBE_VisibleSecurityStateChangedForUnsafeSubresource \
475 DISABLED_VisibleSecurityStateChangedForUnsafeSubresource
476#else
477#define MAYBE_VisibleSecurityStateChangedForUnsafeSubresource \
478 VisibleSecurityStateChangedForUnsafeSubresource
479#endif
dalecurtis6c58ed02016-10-28 23:02:37480TEST_F(SafeBrowsingUIManagerTest,
Evgenii Stepanov199c0132017-08-18 21:19:41481 MAYBE_VisibleSecurityStateChangedForUnsafeSubresource) {
dalecurtis6c58ed02016-10-28 23:02:37482 TestSafeBrowsingBlockingPageFactory factory;
483 SafeBrowsingBlockingPage::RegisterFactory(&factory);
484 SecurityStateWebContentsDelegate delegate;
485 web_contents()->SetDelegate(&delegate);
486
487 // Simulate a blocking page showing for an unsafe subresource.
jialiul792a6662016-12-03 01:44:10488 security_interstitials::UnsafeResource resource =
dalecurtis6c58ed02016-10-28 23:02:37489 MakeUnsafeResource(kBadURL, true /* is_subresource */);
490 // Needed for showing the blocking page.
491 resource.threat_source = safe_browsing::ThreatSource::REMOTE;
492 NavigateAndCommit(GURL("http://example.test"));
493
494 delegate.ClearVisibleSecurityStateChanged();
495 EXPECT_FALSE(delegate.visible_security_state_changed());
496 ui_manager()->DisplayBlockingPage(resource);
497 EXPECT_TRUE(delegate.visible_security_state_changed());
498
499 // Simulate proceeding through the blocking page.
500 SafeBrowsingCallbackWaiter waiter;
501 resource.callback =
502 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO,
503 base::Unretained(&waiter));
504 resource.callback_thread =
505 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
jialiul792a6662016-12-03 01:44:10506 std::vector<security_interstitials::UnsafeResource> resources;
dalecurtis6c58ed02016-10-28 23:02:37507 resources.push_back(resource);
508
509 delegate.ClearVisibleSecurityStateChanged();
510 EXPECT_FALSE(delegate.visible_security_state_changed());
511 SimulateBlockingPageDone(resources, true);
512 EXPECT_TRUE(delegate.visible_security_state_changed());
513
514 waiter.WaitForCallback();
515 EXPECT_TRUE(waiter.callback_called());
516 EXPECT_TRUE(waiter.proceed());
517 EXPECT_TRUE(IsWhitelisted(resource));
518}
519
vakh9a474d832015-11-13 01:43:09520} // namespace safe_browsing