blob: 03e6d27116d31f1c9fa7ccf3d03bb78d8ff459a8 [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:
73 SafeBrowsingUIManagerTest() : ui_manager_(new SafeBrowsingUIManager(NULL)) {}
feltfb118572015-08-18 05:22:0174
juncai1ee189bd2017-06-09 04:25:4375 ~SafeBrowsingUIManagerTest() override {}
feltbc2eda2d2015-06-23 02:06:0376
clamy4edbf0e2015-12-02 13:35:4177 void SetUp() override {
78 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD);
79 ChromeRenderViewHostTestHarness::SetUp();
estark1ca09ca2016-11-01 04:04:1280 SafeBrowsingUIManager::CreateWhitelistForTesting(web_contents());
Luke Zielinski12ef88552017-06-23 15:36:2781
82 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory;
83 auto* safe_browsing_service =
84 sb_service_factory.CreateSafeBrowsingService();
85 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(
86 safe_browsing_service);
87 g_browser_process->safe_browsing_service()->Initialize();
88 // A profile was created already but SafeBrowsingService wasn't around to
89 // get notified of it, so include that notification now.
90 safe_browsing_service->AddPrefService(
91 Profile::FromBrowserContext(web_contents()->GetBrowserContext())
92 ->GetPrefs());
clamy4edbf0e2015-12-02 13:35:4193 }
feltfb118572015-08-18 05:22:0194
Luke Zielinski12ef88552017-06-23 15:36:2795 void TearDown() override {
96 TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown();
97 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr);
98 ChromeRenderViewHostTestHarness::TearDown();
99 }
feltfb118572015-08-18 05:22:01100
jialiul792a6662016-12-03 01:44:10101 bool IsWhitelisted(security_interstitials::UnsafeResource resource) {
feltbc2eda2d2015-06-23 02:06:03102 return ui_manager_->IsWhitelisted(resource);
103 }
104
jialiul792a6662016-12-03 01:44:10105 void AddToWhitelist(security_interstitials::UnsafeResource resource) {
estark1ca09ca2016-11-01 04:04:12106 ui_manager_->AddToWhitelistUrlSet(
107 SafeBrowsingUIManager::GetMainFrameWhitelistUrlForResourceForTesting(
108 resource),
estark7ffa8c62016-11-11 23:21:55109 web_contents(), false, resource.threat_type);
feltfb118572015-08-18 05:22:01110 }
111
jialiul792a6662016-12-03 01:44:10112 security_interstitials::UnsafeResource MakeUnsafeResource(
mattmbfc4060d2015-12-18 23:11:38113 const char* url,
114 bool is_subresource) {
jialiul792a6662016-12-03 01:44:10115 security_interstitials::UnsafeResource resource;
feltfb118572015-08-18 05:22:01116 resource.url = GURL(url);
mattmbfc4060d2015-12-18 23:11:38117 resource.is_subresource = is_subresource;
scottmg22e4f25a2016-08-15 21:09:03118 resource.web_contents_getter =
jialiul792a6662016-12-03 01:44:10119 security_interstitials::UnsafeResource::GetWebContentsGetter(
scottmg22e4f25a2016-08-15 21:09:03120 web_contents()->GetRenderProcessHost()->GetID(),
121 web_contents()->GetMainFrame()->GetRoutingID());
feltfb118572015-08-18 05:22:01122 resource.threat_type = SB_THREAT_TYPE_URL_MALWARE;
123 return resource;
124 }
125
jialiul792a6662016-12-03 01:44:10126 security_interstitials::UnsafeResource MakeUnsafeResourceAndStartNavigation(
feltfb118572015-08-18 05:22:01127 const char* url) {
jialiul792a6662016-12-03 01:44:10128 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38129 MakeUnsafeResource(url, false /* is_subresource */);
feltfb118572015-08-18 05:22:01130
mattmbfc4060d2015-12-18 23:11:38131 // The WC doesn't have a URL without a navigation. A main-frame malware
132 // unsafe resource must be a pending navigation.
clamy511cf022017-08-23 14:11:06133 auto navigation = content::NavigationSimulator::CreateBrowserInitiated(
134 GURL(url), web_contents());
135 navigation->Start();
feltfb118572015-08-18 05:22:01136 return resource;
feltbc2eda2d2015-06-23 02:06:03137 }
138
clamy4edbf0e2015-12-02 13:35:41139 void SimulateBlockingPageDone(
jialiul792a6662016-12-03 01:44:10140 const std::vector<security_interstitials::UnsafeResource>& resources,
clamy4edbf0e2015-12-02 13:35:41141 bool proceed) {
estark1ca09ca2016-11-01 04:04:12142 GURL main_frame_url;
143 content::NavigationEntry* entry =
144 web_contents()->GetController().GetVisibleEntry();
145 if (entry)
146 main_frame_url = entry->GetURL();
147
148 ui_manager_->OnBlockingPageDone(resources, proceed, web_contents(),
149 main_frame_url);
clamy4edbf0e2015-12-02 13:35:41150 }
151
dalecurtis6c58ed02016-10-28 23:02:37152 protected:
153 SafeBrowsingUIManager* ui_manager() { return ui_manager_.get(); }
154
feltbc2eda2d2015-06-23 02:06:03155 private:
156 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
feltbc2eda2d2015-06-23 02:06:03157};
158
Marc Treib3d26e922017-08-14 16:58:26159// Leaks memory. https://crbug.com/755118
160#if defined(LEAK_SANITIZER)
161#define MAYBE_Whitelist DISABLED_Whitelist
162#else
163#define MAYBE_Whitelist Whitelist
164#endif
165TEST_F(SafeBrowsingUIManagerTest, MAYBE_Whitelist) {
jialiul792a6662016-12-03 01:44:10166 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38167 MakeUnsafeResourceAndStartNavigation(kBadURL);
feltbc2eda2d2015-06-23 02:06:03168 AddToWhitelist(resource);
169 EXPECT_TRUE(IsWhitelisted(resource));
170}
171
Marc Treib3d26e922017-08-14 16:58:26172// Leaks memory. https://crbug.com/755118
173#if defined(LEAK_SANITIZER)
174#define MAYBE_WhitelistIgnoresSitesNotAdded \
175 DISABLED_WhitelistIgnoresSitesNotAdded
176#else
177#define MAYBE_WhitelistIgnoresSitesNotAdded WhitelistIgnoresSitesNotAdded
178#endif
179TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresSitesNotAdded) {
jialiul792a6662016-12-03 01:44:10180 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38181 MakeUnsafeResourceAndStartNavigation(kGoodURL);
feltbc2eda2d2015-06-23 02:06:03182 EXPECT_FALSE(IsWhitelisted(resource));
feltfb118572015-08-18 05:22:01183}
184
Marc Treib3d26e922017-08-14 16:58:26185// Leaks memory. https://crbug.com/755118
186#if defined(LEAK_SANITIZER)
187#define MAYBE_WhitelistRemembersThreatType DISABLED_WhitelistRemembersThreatType
188#else
189#define MAYBE_WhitelistRemembersThreatType WhitelistRemembersThreatType
190#endif
191TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistRemembersThreatType) {
jialiul792a6662016-12-03 01:44:10192 security_interstitials::UnsafeResource resource =
estark7ffa8c62016-11-11 23:21:55193 MakeUnsafeResourceAndStartNavigation(kBadURL);
194 AddToWhitelist(resource);
195 EXPECT_TRUE(IsWhitelisted(resource));
196 SBThreatType threat_type;
197 content::NavigationEntry* entry =
198 web_contents()->GetController().GetVisibleEntry();
199 ASSERT_TRUE(entry);
200 EXPECT_TRUE(ui_manager()->IsUrlWhitelistedOrPendingForWebContents(
201 resource.url, resource.is_subresource, entry,
202 resource.web_contents_getter.Run(), true, &threat_type));
203 EXPECT_EQ(resource.threat_type, threat_type);
204}
205
Marc Treib3d26e922017-08-14 16:58:26206// Leaks memory. https://crbug.com/755118
207#if defined(LEAK_SANITIZER)
208#define MAYBE_WhitelistIgnoresPath DISABLED_WhitelistIgnoresPath
209#else
210#define MAYBE_WhitelistIgnoresPath WhitelistIgnoresPath
211#endif
212TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresPath) {
jialiul792a6662016-12-03 01:44:10213 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38214 MakeUnsafeResourceAndStartNavigation(kBadURL);
feltbc2eda2d2015-06-23 02:06:03215 AddToWhitelist(resource);
216 EXPECT_TRUE(IsWhitelisted(resource));
feltfb118572015-08-18 05:22:01217
mattmbfc4060d2015-12-18 23:11:38218 content::WebContentsTester::For(web_contents())->CommitPendingNavigation();
219
jialiul792a6662016-12-03 01:44:10220 security_interstitials::UnsafeResource resource_path =
mattmbfc4060d2015-12-18 23:11:38221 MakeUnsafeResourceAndStartNavigation(kBadURLWithPath);
feltfb118572015-08-18 05:22:01222 EXPECT_TRUE(IsWhitelisted(resource_path));
feltbc2eda2d2015-06-23 02:06:03223}
224
Marc Treib3d26e922017-08-14 16:58:26225// Leaks memory. https://crbug.com/755118
226#if defined(LEAK_SANITIZER)
227#define MAYBE_WhitelistIgnoresThreatType DISABLED_WhitelistIgnoresThreatType
228#else
229#define MAYBE_WhitelistIgnoresThreatType WhitelistIgnoresThreatType
230#endif
231TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresThreatType) {
jialiul792a6662016-12-03 01:44:10232 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38233 MakeUnsafeResourceAndStartNavigation(kBadURL);
feltfb118572015-08-18 05:22:01234 AddToWhitelist(resource);
235 EXPECT_TRUE(IsWhitelisted(resource));
feltbc2eda2d2015-06-23 02:06:03236
jialiul792a6662016-12-03 01:44:10237 security_interstitials::UnsafeResource resource_phishing =
mattmbfc4060d2015-12-18 23:11:38238 MakeUnsafeResource(kBadURL, false /* is_subresource */);
feltfb118572015-08-18 05:22:01239 resource_phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING;
240 EXPECT_TRUE(IsWhitelisted(resource_phishing));
feltbc2eda2d2015-06-23 02:06:03241}
242
Marc Treib3d26e922017-08-14 16:58:26243// Leaks memory. https://crbug.com/755118
244#if defined(LEAK_SANITIZER)
245#define MAYBE_WhitelistWithUnrelatedPendingLoad \
246 DISABLED_WhitelistWithUnrelatedPendingLoad
247#else
248#define MAYBE_WhitelistWithUnrelatedPendingLoad \
249 WhitelistWithUnrelatedPendingLoad
250#endif
251TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistWithUnrelatedPendingLoad) {
mattmbfc4060d2015-12-18 23:11:38252 // Commit load of landing page.
253 NavigateAndCommit(GURL(kLandingURL));
clamy511cf022017-08-23 14:11:06254 auto unrelated_navigation =
255 content::NavigationSimulator::CreateBrowserInitiated(GURL(kGoodURL),
256 web_contents());
mattmbfc4060d2015-12-18 23:11:38257 {
258 // Simulate subresource malware hit on the landing page.
jialiul792a6662016-12-03 01:44:10259 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38260 MakeUnsafeResource(kBadURL, true /* is_subresource */);
261
262 // Start pending load to unrelated site.
clamy511cf022017-08-23 14:11:06263 unrelated_navigation->Start();
mattmbfc4060d2015-12-18 23:11:38264
265 // Whitelist the resource on the landing page.
266 AddToWhitelist(resource);
267 EXPECT_TRUE(IsWhitelisted(resource));
268 }
269
270 // Commit the pending load of unrelated site.
clamy511cf022017-08-23 14:11:06271 unrelated_navigation->Commit();
mattmbfc4060d2015-12-18 23:11:38272 {
273 // The unrelated site is not on the whitelist, even if the same subresource
274 // was on it.
jialiul792a6662016-12-03 01:44:10275 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38276 MakeUnsafeResource(kBadURL, true /* is_subresource */);
277 EXPECT_FALSE(IsWhitelisted(resource));
278 }
279
280 // Navigate back to the original landing url.
281 NavigateAndCommit(GURL(kLandingURL));
282 {
jialiul792a6662016-12-03 01:44:10283 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38284 MakeUnsafeResource(kBadURL, true /* is_subresource */);
285 // Original resource url is whitelisted.
286 EXPECT_TRUE(IsWhitelisted(resource));
287 }
288 {
289 // A different malware subresource on the same page is also whitelisted.
290 // (The whitelist is by the page url, not the resource url.)
jialiul792a6662016-12-03 01:44:10291 security_interstitials::UnsafeResource resource2 =
mattmbfc4060d2015-12-18 23:11:38292 MakeUnsafeResource(kAnotherBadURL, true /* is_subresource */);
293 EXPECT_TRUE(IsWhitelisted(resource2));
294 }
295}
296
Evgenii Stepanov199c0132017-08-18 21:19:41297// Leaks memory. https://crbug.com/755118
298#if defined(LEAK_SANITIZER)
299#define MAYBE_UICallbackProceed DISABLED_UICallbackProceed
300#else
301#define MAYBE_UICallbackProceed UICallbackProceed
302#endif
303TEST_F(SafeBrowsingUIManagerTest, MAYBE_UICallbackProceed) {
jialiul792a6662016-12-03 01:44:10304 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38305 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41306 SafeBrowsingCallbackWaiter waiter;
307 resource.callback =
308 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone,
309 base::Unretained(&waiter));
310 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12311 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI);
jialiul792a6662016-12-03 01:44:10312 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41313 resources.push_back(resource);
314 SimulateBlockingPageDone(resources, true);
315 EXPECT_TRUE(IsWhitelisted(resource));
316 waiter.WaitForCallback();
317 EXPECT_TRUE(waiter.callback_called());
318 EXPECT_TRUE(waiter.proceed());
319}
320
Evgenii Stepanov199c0132017-08-18 21:19:41321// Leaks memory. https://crbug.com/755118
322#if defined(LEAK_SANITIZER)
323#define MAYBE_UICallbackDontProceed DISABLED_UICallbackDontProceed
324#else
325#define MAYBE_UICallbackDontProceed UICallbackDontProceed
326#endif
327TEST_F(SafeBrowsingUIManagerTest, MAYBE_UICallbackDontProceed) {
jialiul792a6662016-12-03 01:44:10328 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38329 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41330 SafeBrowsingCallbackWaiter waiter;
331 resource.callback =
332 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone,
333 base::Unretained(&waiter));
334 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12335 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI);
jialiul792a6662016-12-03 01:44:10336 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41337 resources.push_back(resource);
338 SimulateBlockingPageDone(resources, false);
339 EXPECT_FALSE(IsWhitelisted(resource));
340 waiter.WaitForCallback();
341 EXPECT_TRUE(waiter.callback_called());
342 EXPECT_FALSE(waiter.proceed());
343}
344
Evgenii Stepanov199c0132017-08-18 21:19:41345// Leaks memory. https://crbug.com/755118
346#if defined(LEAK_SANITIZER)
347#define MAYBE_IOCallbackProceed DISABLED_IOCallbackProceed
348#else
349#define MAYBE_IOCallbackProceed IOCallbackProceed
350#endif
351TEST_F(SafeBrowsingUIManagerTest, MAYBE_IOCallbackProceed) {
jialiul792a6662016-12-03 01:44:10352 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38353 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41354 SafeBrowsingCallbackWaiter waiter;
355 resource.callback =
356 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO,
357 base::Unretained(&waiter));
358 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12359 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
jialiul792a6662016-12-03 01:44:10360 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41361 resources.push_back(resource);
362 SimulateBlockingPageDone(resources, true);
363 EXPECT_TRUE(IsWhitelisted(resource));
364 waiter.WaitForCallback();
365 EXPECT_TRUE(waiter.callback_called());
366 EXPECT_TRUE(waiter.proceed());
367}
368
Evgenii Stepanov199c0132017-08-18 21:19:41369// Leaks memory. https://crbug.com/755118
370#if defined(LEAK_SANITIZER)
371#define MAYBE_IOCallbackDontProceed DISABLED_IOCallbackDontProceed
372#else
373#define MAYBE_IOCallbackDontProceed IOCallbackDontProceed
374#endif
375TEST_F(SafeBrowsingUIManagerTest, MAYBE_IOCallbackDontProceed) {
jialiul792a6662016-12-03 01:44:10376 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38377 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41378 SafeBrowsingCallbackWaiter waiter;
379 resource.callback =
380 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO,
381 base::Unretained(&waiter));
382 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12383 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
jialiul792a6662016-12-03 01:44:10384 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41385 resources.push_back(resource);
386 SimulateBlockingPageDone(resources, false);
387 EXPECT_FALSE(IsWhitelisted(resource));
388 waiter.WaitForCallback();
389 EXPECT_TRUE(waiter.callback_called());
390 EXPECT_FALSE(waiter.proceed());
391}
392
dalecurtis6c58ed02016-10-28 23:02:37393namespace {
394
395// A WebContentsDelegate that records whether
396// VisibleSecurityStateChanged() was called.
397class SecurityStateWebContentsDelegate : public content::WebContentsDelegate {
398 public:
399 SecurityStateWebContentsDelegate() {}
400 ~SecurityStateWebContentsDelegate() override {}
401
402 bool visible_security_state_changed() const {
403 return visible_security_state_changed_;
404 }
405
406 void ClearVisibleSecurityStateChanged() {
407 visible_security_state_changed_ = false;
408 }
409
410 // WebContentsDelegate:
411 void VisibleSecurityStateChanged(content::WebContents* source) override {
412 visible_security_state_changed_ = true;
413 }
414
415 private:
416 bool visible_security_state_changed_ = false;
417 DISALLOW_COPY_AND_ASSIGN(SecurityStateWebContentsDelegate);
418};
419
420// A test blocking page that does not create windows.
421class TestSafeBrowsingBlockingPage : public SafeBrowsingBlockingPage {
422 public:
jialiul3d6032e2017-01-12 00:41:31423 TestSafeBrowsingBlockingPage(BaseUIManager* manager,
dalecurtis6c58ed02016-10-28 23:02:37424 content::WebContents* web_contents,
425 const GURL& main_frame_url,
426 const UnsafeResourceList& unsafe_resources)
jialiul3d6032e2017-01-12 00:41:31427 : SafeBrowsingBlockingPage(
428 manager,
429 web_contents,
430 main_frame_url,
431 unsafe_resources,
edwardjungd7395fb02017-05-12 23:13:29432 BaseSafeBrowsingErrorUI::SBErrorDisplayOptions(
ntfschra5448fa2017-02-02 01:01:31433 BaseBlockingPage::IsMainPageLoadBlocked(unsafe_resources),
Nate Fischer2820c5be2017-06-27 23:04:46434 false, // is_extended_reporting_opt_in_allowed
435 false, // is_off_the_record
436 false, // is_extended_reporting_enabled
437 false, // is_scout_reporting_enabled
438 false, // is_proceed_anyway_disabled
439 true, // should_open_links_in_new_tab
Edward Jungc06541d2017-09-12 09:36:19440 true, // always_show_back_to_safety
ntfschre952a3e2017-05-19 19:15:30441 "cpn_safe_browsing")) { // help_center_article_link
dalecurtis6c58ed02016-10-28 23:02:37442 // Don't delay details at all for the unittest.
ntfschrfef42f92017-02-24 02:15:47443 SetThreatDetailsProceedDelayForTesting(0);
dalecurtis6c58ed02016-10-28 23:02:37444 DontCreateViewForTesting();
445 }
446};
447
448// A factory that creates TestSafeBrowsingBlockingPages.
449class TestSafeBrowsingBlockingPageFactory
450 : public SafeBrowsingBlockingPageFactory {
451 public:
452 TestSafeBrowsingBlockingPageFactory() {}
453 ~TestSafeBrowsingBlockingPageFactory() override {}
454
455 SafeBrowsingBlockingPage* CreateSafeBrowsingPage(
jialiul3d6032e2017-01-12 00:41:31456 BaseUIManager* delegate,
dalecurtis6c58ed02016-10-28 23:02:37457 content::WebContents* web_contents,
458 const GURL& main_frame_url,
459 const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources)
460 override {
461 return new TestSafeBrowsingBlockingPage(delegate, web_contents,
462 main_frame_url, unsafe_resources);
463 }
464};
465
466} // namespace
467
468// Tests that the WebContentsDelegate is notified of a visible security
469// state change when a blocking page is shown for a subresource.
Evgenii Stepanov199c0132017-08-18 21:19:41470// Leaks memory. https://crbug.com/755118
471#if defined(LEAK_SANITIZER)
472#define MAYBE_VisibleSecurityStateChangedForUnsafeSubresource \
473 DISABLED_VisibleSecurityStateChangedForUnsafeSubresource
474#else
475#define MAYBE_VisibleSecurityStateChangedForUnsafeSubresource \
476 VisibleSecurityStateChangedForUnsafeSubresource
477#endif
dalecurtis6c58ed02016-10-28 23:02:37478TEST_F(SafeBrowsingUIManagerTest,
Evgenii Stepanov199c0132017-08-18 21:19:41479 MAYBE_VisibleSecurityStateChangedForUnsafeSubresource) {
dalecurtis6c58ed02016-10-28 23:02:37480 TestSafeBrowsingBlockingPageFactory factory;
481 SafeBrowsingBlockingPage::RegisterFactory(&factory);
482 SecurityStateWebContentsDelegate delegate;
483 web_contents()->SetDelegate(&delegate);
484
485 // Simulate a blocking page showing for an unsafe subresource.
jialiul792a6662016-12-03 01:44:10486 security_interstitials::UnsafeResource resource =
dalecurtis6c58ed02016-10-28 23:02:37487 MakeUnsafeResource(kBadURL, true /* is_subresource */);
488 // Needed for showing the blocking page.
489 resource.threat_source = safe_browsing::ThreatSource::REMOTE;
490 NavigateAndCommit(GURL("http://example.test"));
491
492 delegate.ClearVisibleSecurityStateChanged();
493 EXPECT_FALSE(delegate.visible_security_state_changed());
494 ui_manager()->DisplayBlockingPage(resource);
495 EXPECT_TRUE(delegate.visible_security_state_changed());
496
497 // Simulate proceeding through the blocking page.
498 SafeBrowsingCallbackWaiter waiter;
499 resource.callback =
500 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO,
501 base::Unretained(&waiter));
502 resource.callback_thread =
503 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
jialiul792a6662016-12-03 01:44:10504 std::vector<security_interstitials::UnsafeResource> resources;
dalecurtis6c58ed02016-10-28 23:02:37505 resources.push_back(resource);
506
507 delegate.ClearVisibleSecurityStateChanged();
508 EXPECT_FALSE(delegate.visible_security_state_changed());
509 SimulateBlockingPageDone(resources, true);
510 EXPECT_TRUE(delegate.visible_security_state_changed());
511
512 waiter.WaitForCallback();
513 EXPECT_TRUE(waiter.callback_called());
514 EXPECT_TRUE(waiter.proceed());
515 EXPECT_TRUE(IsWhitelisted(resource));
516}
517
vakh9a474d832015-11-13 01:43:09518} // namespace safe_browsing