| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/client_side_detection_host.h" | 5 #include "chrome/browser/safe_browsing/client_side_detection_host.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <tuple> | 8 #include <tuple> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 void TestUnsafeResourceCopied(const UnsafeResource& resource) { | 343 void TestUnsafeResourceCopied(const UnsafeResource& resource) { |
| 344 ASSERT_TRUE(csd_host_->unsafe_resource_.get()); | 344 ASSERT_TRUE(csd_host_->unsafe_resource_.get()); |
| 345 // Test that the resource from OnSafeBrowsingHit notification was copied | 345 // Test that the resource from OnSafeBrowsingHit notification was copied |
| 346 // into the CSDH. | 346 // into the CSDH. |
| 347 EXPECT_EQ(resource.url, csd_host_->unsafe_resource_->url); | 347 EXPECT_EQ(resource.url, csd_host_->unsafe_resource_->url); |
| 348 EXPECT_EQ(resource.original_url, csd_host_->unsafe_resource_->original_url); | 348 EXPECT_EQ(resource.original_url, csd_host_->unsafe_resource_->original_url); |
| 349 EXPECT_EQ(resource.is_subresource, | 349 EXPECT_EQ(resource.is_subresource, |
| 350 csd_host_->unsafe_resource_->is_subresource); | 350 csd_host_->unsafe_resource_->is_subresource); |
| 351 EXPECT_EQ(resource.threat_type, csd_host_->unsafe_resource_->threat_type); | 351 EXPECT_EQ(resource.threat_type, csd_host_->unsafe_resource_->threat_type); |
| 352 EXPECT_TRUE(csd_host_->unsafe_resource_->callback.is_null()); | 352 EXPECT_TRUE(csd_host_->unsafe_resource_->callback.is_null()); |
| 353 EXPECT_EQ(resource.render_process_host_id, | 353 EXPECT_EQ(resource.web_contents_getter.Run(), |
| 354 csd_host_->unsafe_resource_->render_process_host_id); | 354 csd_host_->unsafe_resource_->web_contents_getter.Run()); |
| 355 EXPECT_EQ(resource.render_frame_id, | |
| 356 csd_host_->unsafe_resource_->render_frame_id); | |
| 357 } | 355 } |
| 358 | 356 |
| 359 void SetUnsafeSubResourceForCurrent(bool expect_unsafe_resource) { | 357 void SetUnsafeSubResourceForCurrent(bool expect_unsafe_resource) { |
| 360 UnsafeResource resource; | 358 UnsafeResource resource; |
| 361 resource.url = GURL("http://www.malware.com/"); | 359 resource.url = GURL("http://www.malware.com/"); |
| 362 resource.original_url = web_contents()->GetURL(); | 360 resource.original_url = web_contents()->GetURL(); |
| 363 resource.is_subresource = true; | 361 resource.is_subresource = true; |
| 364 resource.threat_type = SB_THREAT_TYPE_URL_MALWARE; | 362 resource.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 365 resource.callback = base::Bind(&EmptyUrlCheckCallback); | 363 resource.callback = base::Bind(&EmptyUrlCheckCallback); |
| 366 resource.callback_thread = | 364 resource.callback_thread = |
| 367 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); | 365 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
| 368 resource.render_process_host_id = web_contents()->GetRenderProcessHost()-> | 366 resource.web_contents_getter = |
| 369 GetID(); | 367 SafeBrowsingUIManager::UnsafeResource::GetWebContentsGetter( |
| 370 resource.render_frame_id = web_contents()->GetMainFrame()->GetRoutingID(); | 368 web_contents()->GetRenderProcessHost()->GetID(), |
| 369 web_contents()->GetMainFrame()->GetRoutingID()); |
| 371 csd_host_->OnSafeBrowsingHit(resource); | 370 csd_host_->OnSafeBrowsingHit(resource); |
| 372 resource.callback.Reset(); | 371 resource.callback.Reset(); |
| 373 ASSERT_EQ(expect_unsafe_resource, csd_host_->DidShowSBInterstitial()); | 372 ASSERT_EQ(expect_unsafe_resource, csd_host_->DidShowSBInterstitial()); |
| 374 if (expect_unsafe_resource) | 373 if (expect_unsafe_resource) |
| 375 TestUnsafeResourceCopied(resource); | 374 TestUnsafeResourceCopied(resource); |
| 376 } | 375 } |
| 377 | 376 |
| 378 void NavigateWithSBHitAndCommit(const GURL& url) { | 377 void NavigateWithSBHitAndCommit(const GURL& url) { |
| 379 // Create a pending navigation. | 378 // Create a pending navigation. |
| 380 controller().LoadURL( | 379 controller().LoadURL( |
| 381 url, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string()); | 380 url, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string()); |
| 382 | 381 |
| 383 ASSERT_TRUE(pending_main_rfh()); | 382 ASSERT_TRUE(pending_main_rfh()); |
| 384 if (web_contents()->GetMainFrame()->GetProcess()->GetID() == | 383 if (web_contents()->GetMainFrame()->GetProcess()->GetID() == |
| 385 pending_main_rfh()->GetProcess()->GetID()) { | 384 pending_main_rfh()->GetProcess()->GetID()) { |
| 386 EXPECT_NE(web_contents()->GetMainFrame()->GetRoutingID(), | 385 EXPECT_NE(web_contents()->GetMainFrame()->GetRoutingID(), |
| 387 pending_main_rfh()->GetRoutingID()); | 386 pending_main_rfh()->GetRoutingID()); |
| 388 } | 387 } |
| 389 | 388 |
| 390 // Simulate a safebrowsing hit before navigation completes. | 389 // Simulate a safebrowsing hit before navigation completes. |
| 391 UnsafeResource resource; | 390 UnsafeResource resource; |
| 392 resource.url = url; | 391 resource.url = url; |
| 393 resource.original_url = url; | 392 resource.original_url = url; |
| 394 resource.is_subresource = false; | 393 resource.is_subresource = false; |
| 395 resource.threat_type = SB_THREAT_TYPE_URL_MALWARE; | 394 resource.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 396 resource.callback = base::Bind(&EmptyUrlCheckCallback); | 395 resource.callback = base::Bind(&EmptyUrlCheckCallback); |
| 397 resource.callback_thread = | 396 resource.callback_thread = |
| 398 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); | 397 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
| 399 resource.render_process_host_id = pending_rvh()->GetProcess()->GetID(); | 398 resource.web_contents_getter = |
| 400 resource.render_frame_id = pending_main_rfh()->GetRoutingID(); | 399 SafeBrowsingUIManager::UnsafeResource::GetWebContentsGetter( |
| 400 pending_rvh()->GetProcess()->GetID(), |
| 401 pending_main_rfh()->GetRoutingID()); |
| 401 csd_host_->OnSafeBrowsingHit(resource); | 402 csd_host_->OnSafeBrowsingHit(resource); |
| 402 resource.callback.Reset(); | 403 resource.callback.Reset(); |
| 403 | 404 |
| 404 // LoadURL created a navigation entry, now simulate the RenderView sending | 405 // LoadURL created a navigation entry, now simulate the RenderView sending |
| 405 // a notification that it actually navigated. | 406 // a notification that it actually navigated. |
| 406 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); | 407 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 407 | 408 |
| 408 ASSERT_TRUE(csd_host_->DidShowSBInterstitial()); | 409 ASSERT_TRUE(csd_host_->DidShowSBInterstitial()); |
| 409 TestUnsafeResourceCopied(resource); | 410 TestUnsafeResourceCopied(resource); |
| 410 } | 411 } |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 .WillOnce(SaveArg<0>(&resource)); | 555 .WillOnce(SaveArg<0>(&resource)); |
| 555 cb.Run(phishing_url, true); | 556 cb.Run(phishing_url, true); |
| 556 | 557 |
| 557 base::RunLoop().RunUntilIdle(); | 558 base::RunLoop().RunUntilIdle(); |
| 558 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 559 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
| 559 EXPECT_EQ(phishing_url, resource.url); | 560 EXPECT_EQ(phishing_url, resource.url); |
| 560 EXPECT_EQ(phishing_url, resource.original_url); | 561 EXPECT_EQ(phishing_url, resource.original_url); |
| 561 EXPECT_FALSE(resource.is_subresource); | 562 EXPECT_FALSE(resource.is_subresource); |
| 562 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); | 563 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); |
| 563 EXPECT_EQ(ThreatSource::CLIENT_SIDE_DETECTION, resource.threat_source); | 564 EXPECT_EQ(ThreatSource::CLIENT_SIDE_DETECTION, resource.threat_source); |
| 564 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), | 565 EXPECT_EQ(web_contents(), resource.web_contents_getter.Run()); |
| 565 resource.render_process_host_id); | |
| 566 EXPECT_EQ(web_contents()->GetMainFrame()->GetRoutingID(), | |
| 567 resource.render_frame_id); | |
| 568 | 566 |
| 569 // Make sure the client object will be deleted. | 567 // Make sure the client object will be deleted. |
| 570 BrowserThread::PostTask( | 568 BrowserThread::PostTask( |
| 571 BrowserThread::IO, FROM_HERE, | 569 BrowserThread::IO, FROM_HERE, |
| 572 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, | 570 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, |
| 573 ui_manager_, resource.callback)); | 571 ui_manager_, resource.callback)); |
| 574 } | 572 } |
| 575 | 573 |
| 576 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneMultiplePings) { | 574 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneMultiplePings) { |
| 577 // Case 4 & 5: client thinks a page is phishing then navigates to | 575 // Case 4 & 5: client thinks a page is phishing then navigates to |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 643 cb.Run(phishing_url, true); // Should have no effect. | 641 cb.Run(phishing_url, true); // Should have no effect. |
| 644 cb_other.Run(other_phishing_url, true); // Should show interstitial. | 642 cb_other.Run(other_phishing_url, true); // Should show interstitial. |
| 645 | 643 |
| 646 base::RunLoop().RunUntilIdle(); | 644 base::RunLoop().RunUntilIdle(); |
| 647 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 645 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
| 648 EXPECT_EQ(other_phishing_url, resource.url); | 646 EXPECT_EQ(other_phishing_url, resource.url); |
| 649 EXPECT_EQ(other_phishing_url, resource.original_url); | 647 EXPECT_EQ(other_phishing_url, resource.original_url); |
| 650 EXPECT_FALSE(resource.is_subresource); | 648 EXPECT_FALSE(resource.is_subresource); |
| 651 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); | 649 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); |
| 652 EXPECT_EQ(ThreatSource::CLIENT_SIDE_DETECTION, resource.threat_source); | 650 EXPECT_EQ(ThreatSource::CLIENT_SIDE_DETECTION, resource.threat_source); |
| 653 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), | 651 EXPECT_EQ(web_contents(), resource.web_contents_getter.Run()); |
| 654 resource.render_process_host_id); | |
| 655 EXPECT_EQ(web_contents()->GetMainFrame()->GetRoutingID(), | |
| 656 resource.render_frame_id); | |
| 657 | 652 |
| 658 // Make sure the client object will be deleted. | 653 // Make sure the client object will be deleted. |
| 659 BrowserThread::PostTask( | 654 BrowserThread::PostTask( |
| 660 BrowserThread::IO, FROM_HERE, | 655 BrowserThread::IO, FROM_HERE, |
| 661 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, | 656 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, |
| 662 ui_manager_, resource.callback)); | 657 ui_manager_, resource.callback)); |
| 663 } | 658 } |
| 664 | 659 |
| 665 TEST_F(ClientSideDetectionHostTest, | 660 TEST_F(ClientSideDetectionHostTest, |
| 666 OnPhishingDetectionDoneVerdictNotPhishing) { | 661 OnPhishingDetectionDoneVerdictNotPhishing) { |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 859 .WillOnce(SaveArg<0>(&resource)); | 854 .WillOnce(SaveArg<0>(&resource)); |
| 860 cb.Run(malware_landing_url, malware_ip_url, true); | 855 cb.Run(malware_landing_url, malware_ip_url, true); |
| 861 | 856 |
| 862 base::RunLoop().RunUntilIdle(); | 857 base::RunLoop().RunUntilIdle(); |
| 863 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 858 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
| 864 EXPECT_EQ(malware_ip_url, resource.url); | 859 EXPECT_EQ(malware_ip_url, resource.url); |
| 865 EXPECT_EQ(malware_landing_url, resource.original_url); | 860 EXPECT_EQ(malware_landing_url, resource.original_url); |
| 866 EXPECT_TRUE(resource.is_subresource); | 861 EXPECT_TRUE(resource.is_subresource); |
| 867 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL, resource.threat_type); | 862 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL, resource.threat_type); |
| 868 EXPECT_EQ(ThreatSource::CLIENT_SIDE_DETECTION, resource.threat_source); | 863 EXPECT_EQ(ThreatSource::CLIENT_SIDE_DETECTION, resource.threat_source); |
| 869 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), | 864 EXPECT_EQ(web_contents(), resource.web_contents_getter.Run()); |
| 870 resource.render_process_host_id); | |
| 871 EXPECT_EQ(web_contents()->GetMainFrame()->GetRoutingID(), | |
| 872 resource.render_frame_id); | |
| 873 | 865 |
| 874 // Make sure the client object will be deleted. | 866 // Make sure the client object will be deleted. |
| 875 BrowserThread::PostTask( | 867 BrowserThread::PostTask( |
| 876 BrowserThread::IO, FROM_HERE, | 868 BrowserThread::IO, FROM_HERE, |
| 877 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, | 869 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, |
| 878 ui_manager_, resource.callback)); | 870 ui_manager_, resource.callback)); |
| 879 } | 871 } |
| 880 | 872 |
| 881 TEST_F(ClientSideDetectionHostTest, UpdateIPUrlMap) { | 873 TEST_F(ClientSideDetectionHostTest, UpdateIPUrlMap) { |
| 882 BrowseInfo* browse_info = GetBrowseInfo(); | 874 BrowseInfo* browse_info = GetBrowseInfo(); |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1199 EXPECT_EQ(url, resource.url); | 1191 EXPECT_EQ(url, resource.url); |
| 1200 EXPECT_EQ(url, resource.original_url); | 1192 EXPECT_EQ(url, resource.original_url); |
| 1201 | 1193 |
| 1202 ExpectStartPhishingDetection(NULL); | 1194 ExpectStartPhishingDetection(NULL); |
| 1203 | 1195 |
| 1204 // Showing a phishing warning will invalidate all the weak pointers which | 1196 // Showing a phishing warning will invalidate all the weak pointers which |
| 1205 // means we will not extract malware features. | 1197 // means we will not extract malware features. |
| 1206 ExpectShouldClassifyForMalwareResult(false); | 1198 ExpectShouldClassifyForMalwareResult(false); |
| 1207 } | 1199 } |
| 1208 } // namespace safe_browsing | 1200 } // namespace safe_browsing |
| OLD | NEW |