OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/safe_browsing/ui_manager.h" | 5 #include "chrome/browser/safe_browsing/ui_manager.h" |
6 | 6 |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
8 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 9 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
9 #include "chrome/browser/safe_browsing/ui_manager.h" | 10 #include "chrome/browser/safe_browsing/ui_manager.h" |
10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
11 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
| 13 #include "components/safe_browsing_db/safe_browsing_prefs.cc" |
12 #include "components/safe_browsing_db/util.h" | 14 #include "components/safe_browsing_db/util.h" |
13 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
14 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
15 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/browser/web_contents_delegate.h" |
16 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
17 #include "content/public/test/web_contents_tester.h" | 20 #include "content/public/test/web_contents_tester.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "url/gurl.h" | 22 #include "url/gurl.h" |
20 | 23 |
21 using content::BrowserThread; | 24 using content::BrowserThread; |
22 | 25 |
23 static const char* kGoodURL = "https://www.good.com"; | 26 static const char* kGoodURL = "https://www.good.com"; |
24 static const char* kBadURL = "https://www.malware.com"; | 27 static const char* kBadURL = "https://www.malware.com"; |
25 static const char* kBadURLWithPath = "https://www.malware.com/index.html"; | 28 static const char* kBadURLWithPath = "https://www.malware.com/index.html"; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 content::WebContentsTester::For(web_contents())->StartNavigation(GURL(url)); | 109 content::WebContentsTester::For(web_contents())->StartNavigation(GURL(url)); |
107 return resource; | 110 return resource; |
108 } | 111 } |
109 | 112 |
110 void SimulateBlockingPageDone( | 113 void SimulateBlockingPageDone( |
111 const std::vector<SafeBrowsingUIManager::UnsafeResource>& resources, | 114 const std::vector<SafeBrowsingUIManager::UnsafeResource>& resources, |
112 bool proceed) { | 115 bool proceed) { |
113 ui_manager_->OnBlockingPageDone(resources, proceed); | 116 ui_manager_->OnBlockingPageDone(resources, proceed); |
114 } | 117 } |
115 | 118 |
| 119 protected: |
| 120 SafeBrowsingUIManager* ui_manager() { return ui_manager_.get(); } |
| 121 |
116 private: | 122 private: |
117 scoped_refptr<SafeBrowsingUIManager> ui_manager_; | 123 scoped_refptr<SafeBrowsingUIManager> ui_manager_; |
118 }; | 124 }; |
119 | 125 |
120 TEST_F(SafeBrowsingUIManagerTest, Whitelist) { | 126 TEST_F(SafeBrowsingUIManagerTest, Whitelist) { |
121 SafeBrowsingUIManager::UnsafeResource resource = | 127 SafeBrowsingUIManager::UnsafeResource resource = |
122 MakeUnsafeResourceAndStartNavigation(kBadURL); | 128 MakeUnsafeResourceAndStartNavigation(kBadURL); |
123 AddToWhitelist(resource); | 129 AddToWhitelist(resource); |
124 EXPECT_TRUE(IsWhitelisted(resource)); | 130 EXPECT_TRUE(IsWhitelisted(resource)); |
125 } | 131 } |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); | 270 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
265 std::vector<SafeBrowsingUIManager::UnsafeResource> resources; | 271 std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
266 resources.push_back(resource); | 272 resources.push_back(resource); |
267 SimulateBlockingPageDone(resources, false); | 273 SimulateBlockingPageDone(resources, false); |
268 EXPECT_FALSE(IsWhitelisted(resource)); | 274 EXPECT_FALSE(IsWhitelisted(resource)); |
269 waiter.WaitForCallback(); | 275 waiter.WaitForCallback(); |
270 EXPECT_TRUE(waiter.callback_called()); | 276 EXPECT_TRUE(waiter.callback_called()); |
271 EXPECT_FALSE(waiter.proceed()); | 277 EXPECT_FALSE(waiter.proceed()); |
272 } | 278 } |
273 | 279 |
| 280 namespace { |
| 281 |
| 282 // A WebContentsDelegate that records whether |
| 283 // VisibleSecurityStateChanged() was called. |
| 284 class SecurityStateWebContentsDelegate : public content::WebContentsDelegate { |
| 285 public: |
| 286 SecurityStateWebContentsDelegate() {} |
| 287 ~SecurityStateWebContentsDelegate() override {} |
| 288 |
| 289 bool visible_security_state_changed() const { |
| 290 return visible_security_state_changed_; |
| 291 } |
| 292 |
| 293 void ClearVisibleSecurityStateChanged() { |
| 294 visible_security_state_changed_ = false; |
| 295 } |
| 296 |
| 297 // WebContentsDelegate: |
| 298 void VisibleSecurityStateChanged(content::WebContents* source) override { |
| 299 visible_security_state_changed_ = true; |
| 300 } |
| 301 |
| 302 private: |
| 303 bool visible_security_state_changed_ = false; |
| 304 DISALLOW_COPY_AND_ASSIGN(SecurityStateWebContentsDelegate); |
| 305 }; |
| 306 |
| 307 // A test blocking page that does not create windows. |
| 308 class TestSafeBrowsingBlockingPage : public SafeBrowsingBlockingPage { |
| 309 public: |
| 310 TestSafeBrowsingBlockingPage(SafeBrowsingUIManager* manager, |
| 311 content::WebContents* web_contents, |
| 312 const GURL& main_frame_url, |
| 313 const UnsafeResourceList& unsafe_resources) |
| 314 : SafeBrowsingBlockingPage(manager, |
| 315 web_contents, |
| 316 main_frame_url, |
| 317 unsafe_resources) { |
| 318 // Don't delay details at all for the unittest. |
| 319 threat_details_proceed_delay_ms_ = 0; |
| 320 DontCreateViewForTesting(); |
| 321 } |
| 322 }; |
| 323 |
| 324 // A factory that creates TestSafeBrowsingBlockingPages. |
| 325 class TestSafeBrowsingBlockingPageFactory |
| 326 : public SafeBrowsingBlockingPageFactory { |
| 327 public: |
| 328 TestSafeBrowsingBlockingPageFactory() {} |
| 329 ~TestSafeBrowsingBlockingPageFactory() override {} |
| 330 |
| 331 SafeBrowsingBlockingPage* CreateSafeBrowsingPage( |
| 332 SafeBrowsingUIManager* delegate, |
| 333 content::WebContents* web_contents, |
| 334 const GURL& main_frame_url, |
| 335 const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources) |
| 336 override { |
| 337 return new TestSafeBrowsingBlockingPage(delegate, web_contents, |
| 338 main_frame_url, unsafe_resources); |
| 339 } |
| 340 }; |
| 341 |
| 342 } // namespace |
| 343 |
| 344 // Tests that the WebContentsDelegate is notified of a visible security |
| 345 // state change when a blocking page is shown for a subresource. |
| 346 TEST_F(SafeBrowsingUIManagerTest, |
| 347 VisibleSecurityStateChangedForUnsafeSubresource) { |
| 348 TestSafeBrowsingBlockingPageFactory factory; |
| 349 SafeBrowsingBlockingPage::RegisterFactory(&factory); |
| 350 SecurityStateWebContentsDelegate delegate; |
| 351 web_contents()->SetDelegate(&delegate); |
| 352 |
| 353 // Simulate a blocking page showing for an unsafe subresource. |
| 354 SafeBrowsingUIManager::UnsafeResource resource = |
| 355 MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 356 // Needed for showing the blocking page. |
| 357 resource.threat_source = safe_browsing::ThreatSource::REMOTE; |
| 358 NavigateAndCommit(GURL("http://example.test")); |
| 359 |
| 360 delegate.ClearVisibleSecurityStateChanged(); |
| 361 EXPECT_FALSE(delegate.visible_security_state_changed()); |
| 362 ui_manager()->DisplayBlockingPage(resource); |
| 363 EXPECT_TRUE(delegate.visible_security_state_changed()); |
| 364 |
| 365 // Simulate proceeding through the blocking page. |
| 366 SafeBrowsingCallbackWaiter waiter; |
| 367 resource.callback = |
| 368 base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 369 base::Unretained(&waiter)); |
| 370 resource.callback_thread = |
| 371 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
| 372 std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
| 373 resources.push_back(resource); |
| 374 |
| 375 delegate.ClearVisibleSecurityStateChanged(); |
| 376 EXPECT_FALSE(delegate.visible_security_state_changed()); |
| 377 SimulateBlockingPageDone(resources, true); |
| 378 EXPECT_TRUE(delegate.visible_security_state_changed()); |
| 379 |
| 380 waiter.WaitForCallback(); |
| 381 EXPECT_TRUE(waiter.callback_called()); |
| 382 EXPECT_TRUE(waiter.proceed()); |
| 383 EXPECT_TRUE(IsWhitelisted(resource)); |
| 384 } |
| 385 |
274 } // namespace safe_browsing | 386 } // namespace safe_browsing |
OLD | NEW |