blob: 96aa5265aa91119a86b6ac90a35b1910bfba35b3 [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"
vakhfa183fa2016-03-29 17:33:3715#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"
feltbc2eda2d2015-06-23 02:06:0322#include "content/public/test/test_browser_thread_bundle.h"
feltfb118572015-08-18 05:22:0123#include "content/public/test/web_contents_tester.h"
feltbc2eda2d2015-06-23 02:06:0324#include "testing/gtest/include/gtest/gtest.h"
25#include "url/gurl.h"
26
clamy4edbf0e2015-12-02 13:35:4127using content::BrowserThread;
28
feltfb118572015-08-18 05:22:0129static const char* kGoodURL = "https://www.good.com";
30static const char* kBadURL = "https://www.malware.com";
31static const char* kBadURLWithPath = "https://www.malware.com/index.html";
mattmbfc4060d2015-12-18 23:11:3832static const char* kAnotherBadURL = "https://www.badware.com";
33static const char* kLandingURL = "https://www.landing.com";
feltfb118572015-08-18 05:22:0134
vakh9a474d832015-11-13 01:43:0935namespace safe_browsing {
36
clamy4edbf0e2015-12-02 13:35:4137class SafeBrowsingCallbackWaiter {
38 public:
39 SafeBrowsingCallbackWaiter() {}
40
41 bool callback_called() const { return callback_called_; }
42 bool proceed() const { return proceed_; }
43
44 void OnBlockingPageDone(bool proceed) {
45 DCHECK_CURRENTLY_ON(BrowserThread::UI);
46 callback_called_ = true;
47 proceed_ = proceed;
48 loop_.Quit();
49 }
50
51 void OnBlockingPageDoneOnIO(bool proceed) {
52 DCHECK_CURRENTLY_ON(BrowserThread::IO);
53 BrowserThread::PostTask(
54 BrowserThread::UI, FROM_HERE,
tzikb5f84b82017-04-20 00:55:2855 base::BindOnce(&SafeBrowsingCallbackWaiter::OnBlockingPageDone,
56 base::Unretained(this), proceed));
clamy4edbf0e2015-12-02 13:35:4157 }
58
59 void WaitForCallback() {
60 DCHECK_CURRENTLY_ON(BrowserThread::UI);
61 loop_.Run();
62 }
63
64 private:
65 bool callback_called_ = false;
66 bool proceed_ = false;
67 base::RunLoop loop_;
68};
69
feltfb118572015-08-18 05:22:0170class SafeBrowsingUIManagerTest : public ChromeRenderViewHostTestHarness {
feltbc2eda2d2015-06-23 02:06:0371 public:
72 SafeBrowsingUIManagerTest() : ui_manager_(new SafeBrowsingUIManager(NULL)) {}
feltfb118572015-08-18 05:22:0173
juncai1ee189bd2017-06-09 04:25:4374 ~SafeBrowsingUIManagerTest() override {}
feltbc2eda2d2015-06-23 02:06:0375
clamy4edbf0e2015-12-02 13:35:4176 void SetUp() override {
77 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD);
78 ChromeRenderViewHostTestHarness::SetUp();
estark1ca09ca2016-11-01 04:04:1279 SafeBrowsingUIManager::CreateWhitelistForTesting(web_contents());
Luke Zielinski12ef88552017-06-23 15:36:2780
81 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory;
82 auto* safe_browsing_service =
83 sb_service_factory.CreateSafeBrowsingService();
84 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(
85 safe_browsing_service);
86 g_browser_process->safe_browsing_service()->Initialize();
87 // A profile was created already but SafeBrowsingService wasn't around to
88 // get notified of it, so include that notification now.
89 safe_browsing_service->AddPrefService(
90 Profile::FromBrowserContext(web_contents()->GetBrowserContext())
91 ->GetPrefs());
clamy4edbf0e2015-12-02 13:35:4192 }
feltfb118572015-08-18 05:22:0193
Luke Zielinski12ef88552017-06-23 15:36:2794 void TearDown() override {
95 TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown();
96 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr);
97 ChromeRenderViewHostTestHarness::TearDown();
98 }
feltfb118572015-08-18 05:22:0199
jialiul792a6662016-12-03 01:44:10100 bool IsWhitelisted(security_interstitials::UnsafeResource resource) {
feltbc2eda2d2015-06-23 02:06:03101 return ui_manager_->IsWhitelisted(resource);
102 }
103
jialiul792a6662016-12-03 01:44:10104 void AddToWhitelist(security_interstitials::UnsafeResource resource) {
estark1ca09ca2016-11-01 04:04:12105 ui_manager_->AddToWhitelistUrlSet(
106 SafeBrowsingUIManager::GetMainFrameWhitelistUrlForResourceForTesting(
107 resource),
estark7ffa8c62016-11-11 23:21:55108 web_contents(), false, resource.threat_type);
feltfb118572015-08-18 05:22:01109 }
110
jialiul792a6662016-12-03 01:44:10111 security_interstitials::UnsafeResource MakeUnsafeResource(
mattmbfc4060d2015-12-18 23:11:38112 const char* url,
113 bool is_subresource) {
jialiul792a6662016-12-03 01:44:10114 security_interstitials::UnsafeResource resource;
feltfb118572015-08-18 05:22:01115 resource.url = GURL(url);
mattmbfc4060d2015-12-18 23:11:38116 resource.is_subresource = is_subresource;
scottmg22e4f25a2016-08-15 21:09:03117 resource.web_contents_getter =
jialiul792a6662016-12-03 01:44:10118 security_interstitials::UnsafeResource::GetWebContentsGetter(
scottmg22e4f25a2016-08-15 21:09:03119 web_contents()->GetRenderProcessHost()->GetID(),
120 web_contents()->GetMainFrame()->GetRoutingID());
feltfb118572015-08-18 05:22:01121 resource.threat_type = SB_THREAT_TYPE_URL_MALWARE;
122 return resource;
123 }
124
jialiul792a6662016-12-03 01:44:10125 security_interstitials::UnsafeResource MakeUnsafeResourceAndStartNavigation(
feltfb118572015-08-18 05:22:01126 const char* url) {
jialiul792a6662016-12-03 01:44:10127 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38128 MakeUnsafeResource(url, false /* is_subresource */);
feltfb118572015-08-18 05:22:01129
mattmbfc4060d2015-12-18 23:11:38130 // The WC doesn't have a URL without a navigation. A main-frame malware
131 // unsafe resource must be a pending navigation.
132 content::WebContentsTester::For(web_contents())->StartNavigation(GURL(url));
feltfb118572015-08-18 05:22:01133 return resource;
feltbc2eda2d2015-06-23 02:06:03134 }
135
clamy4edbf0e2015-12-02 13:35:41136 void SimulateBlockingPageDone(
jialiul792a6662016-12-03 01:44:10137 const std::vector<security_interstitials::UnsafeResource>& resources,
clamy4edbf0e2015-12-02 13:35:41138 bool proceed) {
estark1ca09ca2016-11-01 04:04:12139 GURL main_frame_url;
140 content::NavigationEntry* entry =
141 web_contents()->GetController().GetVisibleEntry();
142 if (entry)
143 main_frame_url = entry->GetURL();
144
145 ui_manager_->OnBlockingPageDone(resources, proceed, web_contents(),
146 main_frame_url);
clamy4edbf0e2015-12-02 13:35:41147 }
148
dalecurtis6c58ed02016-10-28 23:02:37149 protected:
150 SafeBrowsingUIManager* ui_manager() { return ui_manager_.get(); }
151
feltbc2eda2d2015-06-23 02:06:03152 private:
153 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
feltbc2eda2d2015-06-23 02:06:03154};
155
156TEST_F(SafeBrowsingUIManagerTest, Whitelist) {
jialiul792a6662016-12-03 01:44:10157 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38158 MakeUnsafeResourceAndStartNavigation(kBadURL);
feltbc2eda2d2015-06-23 02:06:03159 AddToWhitelist(resource);
160 EXPECT_TRUE(IsWhitelisted(resource));
161}
162
feltfb118572015-08-18 05:22:01163TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresSitesNotAdded) {
jialiul792a6662016-12-03 01:44:10164 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38165 MakeUnsafeResourceAndStartNavigation(kGoodURL);
feltbc2eda2d2015-06-23 02:06:03166 EXPECT_FALSE(IsWhitelisted(resource));
feltfb118572015-08-18 05:22:01167}
168
estark7ffa8c62016-11-11 23:21:55169TEST_F(SafeBrowsingUIManagerTest, WhitelistRemembersThreatType) {
jialiul792a6662016-12-03 01:44:10170 security_interstitials::UnsafeResource resource =
estark7ffa8c62016-11-11 23:21:55171 MakeUnsafeResourceAndStartNavigation(kBadURL);
172 AddToWhitelist(resource);
173 EXPECT_TRUE(IsWhitelisted(resource));
174 SBThreatType threat_type;
175 content::NavigationEntry* entry =
176 web_contents()->GetController().GetVisibleEntry();
177 ASSERT_TRUE(entry);
178 EXPECT_TRUE(ui_manager()->IsUrlWhitelistedOrPendingForWebContents(
179 resource.url, resource.is_subresource, entry,
180 resource.web_contents_getter.Run(), true, &threat_type));
181 EXPECT_EQ(resource.threat_type, threat_type);
182}
183
feltfb118572015-08-18 05:22:01184TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresPath) {
jialiul792a6662016-12-03 01:44:10185 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38186 MakeUnsafeResourceAndStartNavigation(kBadURL);
feltbc2eda2d2015-06-23 02:06:03187 AddToWhitelist(resource);
188 EXPECT_TRUE(IsWhitelisted(resource));
feltfb118572015-08-18 05:22:01189
mattmbfc4060d2015-12-18 23:11:38190 content::WebContentsTester::For(web_contents())->CommitPendingNavigation();
191
jialiul792a6662016-12-03 01:44:10192 security_interstitials::UnsafeResource resource_path =
mattmbfc4060d2015-12-18 23:11:38193 MakeUnsafeResourceAndStartNavigation(kBadURLWithPath);
feltfb118572015-08-18 05:22:01194 EXPECT_TRUE(IsWhitelisted(resource_path));
feltbc2eda2d2015-06-23 02:06:03195}
196
feltfb118572015-08-18 05:22:01197TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresThreatType) {
jialiul792a6662016-12-03 01:44:10198 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38199 MakeUnsafeResourceAndStartNavigation(kBadURL);
feltfb118572015-08-18 05:22:01200 AddToWhitelist(resource);
201 EXPECT_TRUE(IsWhitelisted(resource));
feltbc2eda2d2015-06-23 02:06:03202
jialiul792a6662016-12-03 01:44:10203 security_interstitials::UnsafeResource resource_phishing =
mattmbfc4060d2015-12-18 23:11:38204 MakeUnsafeResource(kBadURL, false /* is_subresource */);
feltfb118572015-08-18 05:22:01205 resource_phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING;
206 EXPECT_TRUE(IsWhitelisted(resource_phishing));
feltbc2eda2d2015-06-23 02:06:03207}
208
mattmbfc4060d2015-12-18 23:11:38209TEST_F(SafeBrowsingUIManagerTest, WhitelistWithUnrelatedPendingLoad) {
210 // Commit load of landing page.
211 NavigateAndCommit(GURL(kLandingURL));
212 {
213 // Simulate subresource malware hit on the landing page.
jialiul792a6662016-12-03 01:44:10214 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38215 MakeUnsafeResource(kBadURL, true /* is_subresource */);
216
217 // Start pending load to unrelated site.
218 content::WebContentsTester::For(web_contents())
219 ->StartNavigation(GURL(kGoodURL));
220
221 // Whitelist the resource on the landing page.
222 AddToWhitelist(resource);
223 EXPECT_TRUE(IsWhitelisted(resource));
224 }
225
226 // Commit the pending load of unrelated site.
227 content::WebContentsTester::For(web_contents())->CommitPendingNavigation();
228 {
229 // The unrelated site is not on the whitelist, even if the same subresource
230 // was on it.
jialiul792a6662016-12-03 01:44:10231 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38232 MakeUnsafeResource(kBadURL, true /* is_subresource */);
233 EXPECT_FALSE(IsWhitelisted(resource));
234 }
235
236 // Navigate back to the original landing url.
237 NavigateAndCommit(GURL(kLandingURL));
238 {
jialiul792a6662016-12-03 01:44:10239 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38240 MakeUnsafeResource(kBadURL, true /* is_subresource */);
241 // Original resource url is whitelisted.
242 EXPECT_TRUE(IsWhitelisted(resource));
243 }
244 {
245 // A different malware subresource on the same page is also whitelisted.
246 // (The whitelist is by the page url, not the resource url.)
jialiul792a6662016-12-03 01:44:10247 security_interstitials::UnsafeResource resource2 =
mattmbfc4060d2015-12-18 23:11:38248 MakeUnsafeResource(kAnotherBadURL, true /* is_subresource */);
249 EXPECT_TRUE(IsWhitelisted(resource2));
250 }
251}
252
clamy4edbf0e2015-12-02 13:35:41253TEST_F(SafeBrowsingUIManagerTest, UICallbackProceed) {
jialiul792a6662016-12-03 01:44:10254 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38255 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41256 SafeBrowsingCallbackWaiter waiter;
257 resource.callback =
258 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone,
259 base::Unretained(&waiter));
260 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12261 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI);
jialiul792a6662016-12-03 01:44:10262 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41263 resources.push_back(resource);
264 SimulateBlockingPageDone(resources, true);
265 EXPECT_TRUE(IsWhitelisted(resource));
266 waiter.WaitForCallback();
267 EXPECT_TRUE(waiter.callback_called());
268 EXPECT_TRUE(waiter.proceed());
269}
270
271TEST_F(SafeBrowsingUIManagerTest, UICallbackDontProceed) {
jialiul792a6662016-12-03 01:44:10272 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38273 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41274 SafeBrowsingCallbackWaiter waiter;
275 resource.callback =
276 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone,
277 base::Unretained(&waiter));
278 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12279 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI);
jialiul792a6662016-12-03 01:44:10280 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41281 resources.push_back(resource);
282 SimulateBlockingPageDone(resources, false);
283 EXPECT_FALSE(IsWhitelisted(resource));
284 waiter.WaitForCallback();
285 EXPECT_TRUE(waiter.callback_called());
286 EXPECT_FALSE(waiter.proceed());
287}
288
289TEST_F(SafeBrowsingUIManagerTest, IOCallbackProceed) {
jialiul792a6662016-12-03 01:44:10290 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38291 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41292 SafeBrowsingCallbackWaiter waiter;
293 resource.callback =
294 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO,
295 base::Unretained(&waiter));
296 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12297 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
jialiul792a6662016-12-03 01:44:10298 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41299 resources.push_back(resource);
300 SimulateBlockingPageDone(resources, true);
301 EXPECT_TRUE(IsWhitelisted(resource));
302 waiter.WaitForCallback();
303 EXPECT_TRUE(waiter.callback_called());
304 EXPECT_TRUE(waiter.proceed());
305}
306
307TEST_F(SafeBrowsingUIManagerTest, IOCallbackDontProceed) {
jialiul792a6662016-12-03 01:44:10308 security_interstitials::UnsafeResource resource =
mattmbfc4060d2015-12-18 23:11:38309 MakeUnsafeResourceAndStartNavigation(kBadURL);
clamy4edbf0e2015-12-02 13:35:41310 SafeBrowsingCallbackWaiter waiter;
311 resource.callback =
312 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO,
313 base::Unretained(&waiter));
314 resource.callback_thread =
thestig529ad8a2016-07-08 20:30:12315 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
jialiul792a6662016-12-03 01:44:10316 std::vector<security_interstitials::UnsafeResource> resources;
clamy4edbf0e2015-12-02 13:35:41317 resources.push_back(resource);
318 SimulateBlockingPageDone(resources, false);
319 EXPECT_FALSE(IsWhitelisted(resource));
320 waiter.WaitForCallback();
321 EXPECT_TRUE(waiter.callback_called());
322 EXPECT_FALSE(waiter.proceed());
323}
324
dalecurtis6c58ed02016-10-28 23:02:37325namespace {
326
327// A WebContentsDelegate that records whether
328// VisibleSecurityStateChanged() was called.
329class SecurityStateWebContentsDelegate : public content::WebContentsDelegate {
330 public:
331 SecurityStateWebContentsDelegate() {}
332 ~SecurityStateWebContentsDelegate() override {}
333
334 bool visible_security_state_changed() const {
335 return visible_security_state_changed_;
336 }
337
338 void ClearVisibleSecurityStateChanged() {
339 visible_security_state_changed_ = false;
340 }
341
342 // WebContentsDelegate:
343 void VisibleSecurityStateChanged(content::WebContents* source) override {
344 visible_security_state_changed_ = true;
345 }
346
347 private:
348 bool visible_security_state_changed_ = false;
349 DISALLOW_COPY_AND_ASSIGN(SecurityStateWebContentsDelegate);
350};
351
352// A test blocking page that does not create windows.
353class TestSafeBrowsingBlockingPage : public SafeBrowsingBlockingPage {
354 public:
jialiul3d6032e2017-01-12 00:41:31355 TestSafeBrowsingBlockingPage(BaseUIManager* manager,
dalecurtis6c58ed02016-10-28 23:02:37356 content::WebContents* web_contents,
357 const GURL& main_frame_url,
358 const UnsafeResourceList& unsafe_resources)
jialiul3d6032e2017-01-12 00:41:31359 : SafeBrowsingBlockingPage(
360 manager,
361 web_contents,
362 main_frame_url,
363 unsafe_resources,
edwardjungd7395fb02017-05-12 23:13:29364 BaseSafeBrowsingErrorUI::SBErrorDisplayOptions(
ntfschra5448fa2017-02-02 01:01:31365 BaseBlockingPage::IsMainPageLoadBlocked(unsafe_resources),
ntfschre952a3e2017-05-19 19:15:30366 false, // is_extended_reporting_opt_in_allowed
367 false, // is_off_the_record
368 false, // is_extended_reporting_enabled
369 false, // is_scout_reporting_enabled
370 false, // is_proceed_anyway_disabled
ntfschre952a3e2017-05-19 19:15:30371 "cpn_safe_browsing")) { // help_center_article_link
dalecurtis6c58ed02016-10-28 23:02:37372 // Don't delay details at all for the unittest.
ntfschrfef42f92017-02-24 02:15:47373 SetThreatDetailsProceedDelayForTesting(0);
dalecurtis6c58ed02016-10-28 23:02:37374 DontCreateViewForTesting();
375 }
376};
377
378// A factory that creates TestSafeBrowsingBlockingPages.
379class TestSafeBrowsingBlockingPageFactory
380 : public SafeBrowsingBlockingPageFactory {
381 public:
382 TestSafeBrowsingBlockingPageFactory() {}
383 ~TestSafeBrowsingBlockingPageFactory() override {}
384
385 SafeBrowsingBlockingPage* CreateSafeBrowsingPage(
jialiul3d6032e2017-01-12 00:41:31386 BaseUIManager* delegate,
dalecurtis6c58ed02016-10-28 23:02:37387 content::WebContents* web_contents,
388 const GURL& main_frame_url,
389 const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources)
390 override {
391 return new TestSafeBrowsingBlockingPage(delegate, web_contents,
392 main_frame_url, unsafe_resources);
393 }
394};
395
396} // namespace
397
398// Tests that the WebContentsDelegate is notified of a visible security
399// state change when a blocking page is shown for a subresource.
400TEST_F(SafeBrowsingUIManagerTest,
401 VisibleSecurityStateChangedForUnsafeSubresource) {
402 TestSafeBrowsingBlockingPageFactory factory;
403 SafeBrowsingBlockingPage::RegisterFactory(&factory);
404 SecurityStateWebContentsDelegate delegate;
405 web_contents()->SetDelegate(&delegate);
406
407 // Simulate a blocking page showing for an unsafe subresource.
jialiul792a6662016-12-03 01:44:10408 security_interstitials::UnsafeResource resource =
dalecurtis6c58ed02016-10-28 23:02:37409 MakeUnsafeResource(kBadURL, true /* is_subresource */);
410 // Needed for showing the blocking page.
411 resource.threat_source = safe_browsing::ThreatSource::REMOTE;
412 NavigateAndCommit(GURL("http://example.test"));
413
414 delegate.ClearVisibleSecurityStateChanged();
415 EXPECT_FALSE(delegate.visible_security_state_changed());
416 ui_manager()->DisplayBlockingPage(resource);
417 EXPECT_TRUE(delegate.visible_security_state_changed());
418
419 // Simulate proceeding through the blocking page.
420 SafeBrowsingCallbackWaiter waiter;
421 resource.callback =
422 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO,
423 base::Unretained(&waiter));
424 resource.callback_thread =
425 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
jialiul792a6662016-12-03 01:44:10426 std::vector<security_interstitials::UnsafeResource> resources;
dalecurtis6c58ed02016-10-28 23:02:37427 resources.push_back(resource);
428
429 delegate.ClearVisibleSecurityStateChanged();
430 EXPECT_FALSE(delegate.visible_security_state_changed());
431 SimulateBlockingPageDone(resources, true);
432 EXPECT_TRUE(delegate.visible_security_state_changed());
433
434 waiter.WaitForCallback();
435 EXPECT_TRUE(waiter.callback_called());
436 EXPECT_TRUE(waiter.proceed());
437 EXPECT_TRUE(IsWhitelisted(resource));
438}
439
vakh9a474d832015-11-13 01:43:09440} // namespace safe_browsing