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" | |
9 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 8 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
10 #include "chrome/browser/safe_browsing/ui_manager.h" | 9 #include "chrome/browser/safe_browsing/ui_manager.h" |
11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
12 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
13 #include "components/safe_browsing_db/safe_browsing_prefs.h" | |
14 #include "components/safe_browsing_db/util.h" | 12 #include "components/safe_browsing_db/util.h" |
15 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" |
16 #include "content/public/browser/render_view_host.h" | 14 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
18 #include "content/public/browser/web_contents_delegate.h" | |
19 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
20 #include "content/public/test/web_contents_tester.h" | 17 #include "content/public/test/web_contents_tester.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "url/gurl.h" | 19 #include "url/gurl.h" |
23 | 20 |
24 using content::BrowserThread; | 21 using content::BrowserThread; |
25 | 22 |
26 static const char* kGoodURL = "https://www.good.com"; | 23 static const char* kGoodURL = "https://www.good.com"; |
27 static const char* kBadURL = "https://www.malware.com"; | 24 static const char* kBadURL = "https://www.malware.com"; |
28 static const char* kBadURLWithPath = "https://www.malware.com/index.html"; | 25 static const char* kBadURLWithPath = "https://www.malware.com/index.html"; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 content::WebContentsTester::For(web_contents())->StartNavigation(GURL(url)); | 106 content::WebContentsTester::For(web_contents())->StartNavigation(GURL(url)); |
110 return resource; | 107 return resource; |
111 } | 108 } |
112 | 109 |
113 void SimulateBlockingPageDone( | 110 void SimulateBlockingPageDone( |
114 const std::vector<SafeBrowsingUIManager::UnsafeResource>& resources, | 111 const std::vector<SafeBrowsingUIManager::UnsafeResource>& resources, |
115 bool proceed) { | 112 bool proceed) { |
116 ui_manager_->OnBlockingPageDone(resources, proceed); | 113 ui_manager_->OnBlockingPageDone(resources, proceed); |
117 } | 114 } |
118 | 115 |
119 protected: | |
120 SafeBrowsingUIManager* ui_manager() { return ui_manager_.get(); } | |
121 | |
122 private: | 116 private: |
123 scoped_refptr<SafeBrowsingUIManager> ui_manager_; | 117 scoped_refptr<SafeBrowsingUIManager> ui_manager_; |
124 }; | 118 }; |
125 | 119 |
126 TEST_F(SafeBrowsingUIManagerTest, Whitelist) { | 120 TEST_F(SafeBrowsingUIManagerTest, Whitelist) { |
127 SafeBrowsingUIManager::UnsafeResource resource = | 121 SafeBrowsingUIManager::UnsafeResource resource = |
128 MakeUnsafeResourceAndStartNavigation(kBadURL); | 122 MakeUnsafeResourceAndStartNavigation(kBadURL); |
129 AddToWhitelist(resource); | 123 AddToWhitelist(resource); |
130 EXPECT_TRUE(IsWhitelisted(resource)); | 124 EXPECT_TRUE(IsWhitelisted(resource)); |
131 } | 125 } |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); | 264 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
271 std::vector<SafeBrowsingUIManager::UnsafeResource> resources; | 265 std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
272 resources.push_back(resource); | 266 resources.push_back(resource); |
273 SimulateBlockingPageDone(resources, false); | 267 SimulateBlockingPageDone(resources, false); |
274 EXPECT_FALSE(IsWhitelisted(resource)); | 268 EXPECT_FALSE(IsWhitelisted(resource)); |
275 waiter.WaitForCallback(); | 269 waiter.WaitForCallback(); |
276 EXPECT_TRUE(waiter.callback_called()); | 270 EXPECT_TRUE(waiter.callback_called()); |
277 EXPECT_FALSE(waiter.proceed()); | 271 EXPECT_FALSE(waiter.proceed()); |
278 } | 272 } |
279 | 273 |
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 | |
386 } // namespace safe_browsing | 274 } // namespace safe_browsing |
OLD | NEW |