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 "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" | 10 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 csd_service_.reset(new StrictMock<MockClientSideDetectionService>()); | 208 csd_service_.reset(new StrictMock<MockClientSideDetectionService>()); |
209 // Only used for initializing mock objects. | 209 // Only used for initializing mock objects. |
210 SafeBrowsingService* sb_service = | 210 SafeBrowsingService* sb_service = |
211 SafeBrowsingService::CreateSafeBrowsingService(); | 211 SafeBrowsingService::CreateSafeBrowsingService(); |
212 database_manager_ = | 212 database_manager_ = |
213 new StrictMock<MockSafeBrowsingDatabaseManager>(sb_service); | 213 new StrictMock<MockSafeBrowsingDatabaseManager>(sb_service); |
214 ui_manager_ = new StrictMock<MockSafeBrowsingUIManager>(sb_service); | 214 ui_manager_ = new StrictMock<MockSafeBrowsingUIManager>(sb_service); |
215 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( | 215 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( |
216 web_contents())); | 216 web_contents())); |
217 csd_host_->set_client_side_detection_service(csd_service_.get()); | 217 csd_host_->set_client_side_detection_service(csd_service_.get()); |
218 csd_host_->set_safe_browsing_managers(ui_manager_, database_manager_); | 218 csd_host_->set_safe_browsing_managers(ui_manager_.get(), |
| 219 database_manager_.get()); |
219 // We need to create this here since we don't call | 220 // We need to create this here since we don't call |
220 // DidNavigateMainFramePostCommit in this test. | 221 // DidNavigateMainFramePostCommit in this test. |
221 csd_host_->browse_info_.reset(new BrowseInfo); | 222 csd_host_->browse_info_.reset(new BrowseInfo); |
222 | 223 |
223 // By default this is set to false. Turn it on as if we are in canary or | 224 // By default this is set to false. Turn it on as if we are in canary or |
224 // dev channel | 225 // dev channel |
225 csd_host_->malware_report_enabled_ = true; | 226 csd_host_->malware_report_enabled_ = true; |
226 } | 227 } |
227 | 228 |
228 static void RunAllPendingOnIO(base::WaitableEvent* event) { | 229 static void RunAllPendingOnIO(base::WaitableEvent* event) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 const bool* over_report_limit) { | 283 const bool* over_report_limit) { |
283 if (is_private) { | 284 if (is_private) { |
284 EXPECT_CALL(*csd_service_, IsPrivateIPAddress(_)) | 285 EXPECT_CALL(*csd_service_, IsPrivateIPAddress(_)) |
285 .WillOnce(Return(*is_private)); | 286 .WillOnce(Return(*is_private)); |
286 } | 287 } |
287 if (is_incognito) { | 288 if (is_incognito) { |
288 EXPECT_CALL(*mock_profile_, IsOffTheRecord()) | 289 EXPECT_CALL(*mock_profile_, IsOffTheRecord()) |
289 .WillRepeatedly(Return(*is_incognito)); | 290 .WillRepeatedly(Return(*is_incognito)); |
290 } | 291 } |
291 if (match_csd_whitelist) { | 292 if (match_csd_whitelist) { |
292 EXPECT_CALL(*database_manager_, MatchCsdWhitelistUrl(url)) | 293 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(url)) |
293 .WillOnce(Return(*match_csd_whitelist)); | 294 .WillOnce(Return(*match_csd_whitelist)); |
294 } | 295 } |
295 if (get_valid_cached_result) { | 296 if (get_valid_cached_result) { |
296 EXPECT_CALL(*csd_service_, GetValidCachedResult(url, NotNull())) | 297 EXPECT_CALL(*csd_service_, GetValidCachedResult(url, NotNull())) |
297 .WillOnce(DoAll(SetArgumentPointee<1>(true), | 298 .WillOnce(DoAll(SetArgumentPointee<1>(true), |
298 Return(*get_valid_cached_result))); | 299 Return(*get_valid_cached_result))); |
299 } | 300 } |
300 if (is_in_cache) { | 301 if (is_in_cache) { |
301 EXPECT_CALL(*csd_service_, IsInCache(url)).WillOnce(Return(*is_in_cache)); | 302 EXPECT_CALL(*csd_service_, IsInCache(url)).WillOnce(Return(*is_in_cache)); |
302 } | 303 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 InvokeCallbackArgument<2>(true, &verdict))); | 399 InvokeCallbackArgument<2>(true, &verdict))); |
399 EXPECT_CALL(*csd_service_, | 400 EXPECT_CALL(*csd_service_, |
400 SendClientReportPhishingRequest( | 401 SendClientReportPhishingRequest( |
401 Pointee(PartiallyEqualVerdict(verdict)), _)) | 402 Pointee(PartiallyEqualVerdict(verdict)), _)) |
402 .WillOnce(SaveArg<1>(&cb)); | 403 .WillOnce(SaveArg<1>(&cb)); |
403 OnPhishingDetectionDone(verdict.SerializeAsString()); | 404 OnPhishingDetectionDone(verdict.SerializeAsString()); |
404 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 405 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
405 ASSERT_FALSE(cb.is_null()); | 406 ASSERT_FALSE(cb.is_null()); |
406 | 407 |
407 // Make sure DoDisplayBlockingPage is not going to be called. | 408 // Make sure DoDisplayBlockingPage is not going to be called. |
408 EXPECT_CALL(*ui_manager_, DoDisplayBlockingPage(_)).Times(0); | 409 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)).Times(0); |
409 cb.Run(GURL(verdict.url()), false); | 410 cb.Run(GURL(verdict.url()), false); |
410 base::MessageLoop::current()->RunUntilIdle(); | 411 base::MessageLoop::current()->RunUntilIdle(); |
411 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 412 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
412 } | 413 } |
413 | 414 |
414 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { | 415 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { |
415 // Case 2: client thinks the page is phishing and so does the server but | 416 // Case 2: client thinks the page is phishing and so does the server but |
416 // showing the interstitial is disabled => no interstitial is shown. | 417 // showing the interstitial is disabled => no interstitial is shown. |
417 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 418 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
418 web_contents(), | 419 web_contents(), |
(...skipping 11 matching lines...) Expand all Loading... |
430 InvokeCallbackArgument<2>(true, &verdict))); | 431 InvokeCallbackArgument<2>(true, &verdict))); |
431 EXPECT_CALL(*csd_service_, | 432 EXPECT_CALL(*csd_service_, |
432 SendClientReportPhishingRequest( | 433 SendClientReportPhishingRequest( |
433 Pointee(PartiallyEqualVerdict(verdict)), _)) | 434 Pointee(PartiallyEqualVerdict(verdict)), _)) |
434 .WillOnce(SaveArg<1>(&cb)); | 435 .WillOnce(SaveArg<1>(&cb)); |
435 OnPhishingDetectionDone(verdict.SerializeAsString()); | 436 OnPhishingDetectionDone(verdict.SerializeAsString()); |
436 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 437 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
437 ASSERT_FALSE(cb.is_null()); | 438 ASSERT_FALSE(cb.is_null()); |
438 | 439 |
439 // Make sure DoDisplayBlockingPage is not going to be called. | 440 // Make sure DoDisplayBlockingPage is not going to be called. |
440 EXPECT_CALL(*ui_manager_, DoDisplayBlockingPage(_)).Times(0); | 441 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)).Times(0); |
441 cb.Run(GURL(verdict.url()), false); | 442 cb.Run(GURL(verdict.url()), false); |
442 base::MessageLoop::current()->RunUntilIdle(); | 443 base::MessageLoop::current()->RunUntilIdle(); |
443 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 444 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
444 } | 445 } |
445 | 446 |
446 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { | 447 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { |
447 // Case 3: client thinks the page is phishing and so does the server. | 448 // Case 3: client thinks the page is phishing and so does the server. |
448 // We show an interstitial. | 449 // We show an interstitial. |
449 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 450 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
450 web_contents(), | 451 web_contents(), |
(...skipping 12 matching lines...) Expand all Loading... |
463 InvokeCallbackArgument<2>(true, &verdict))); | 464 InvokeCallbackArgument<2>(true, &verdict))); |
464 EXPECT_CALL(*csd_service_, | 465 EXPECT_CALL(*csd_service_, |
465 SendClientReportPhishingRequest( | 466 SendClientReportPhishingRequest( |
466 Pointee(PartiallyEqualVerdict(verdict)), _)) | 467 Pointee(PartiallyEqualVerdict(verdict)), _)) |
467 .WillOnce(SaveArg<1>(&cb)); | 468 .WillOnce(SaveArg<1>(&cb)); |
468 OnPhishingDetectionDone(verdict.SerializeAsString()); | 469 OnPhishingDetectionDone(verdict.SerializeAsString()); |
469 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 470 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
470 ASSERT_FALSE(cb.is_null()); | 471 ASSERT_FALSE(cb.is_null()); |
471 | 472 |
472 UnsafeResource resource; | 473 UnsafeResource resource; |
473 EXPECT_CALL(*ui_manager_, DoDisplayBlockingPage(_)) | 474 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) |
474 .WillOnce(SaveArg<0>(&resource)); | 475 .WillOnce(SaveArg<0>(&resource)); |
475 cb.Run(phishing_url, true); | 476 cb.Run(phishing_url, true); |
476 | 477 |
477 base::MessageLoop::current()->RunUntilIdle(); | 478 base::MessageLoop::current()->RunUntilIdle(); |
478 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 479 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
479 EXPECT_EQ(phishing_url, resource.url); | 480 EXPECT_EQ(phishing_url, resource.url); |
480 EXPECT_EQ(phishing_url, resource.original_url); | 481 EXPECT_EQ(phishing_url, resource.original_url); |
481 EXPECT_FALSE(resource.is_subresource); | 482 EXPECT_FALSE(resource.is_subresource); |
482 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); | 483 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); |
483 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), | 484 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 redirect_chain.push_back(other_phishing_url); | 553 redirect_chain.push_back(other_phishing_url); |
553 SetRedirectChain(redirect_chain); | 554 SetRedirectChain(redirect_chain); |
554 OnPhishingDetectionDone(verdict.SerializeAsString()); | 555 OnPhishingDetectionDone(verdict.SerializeAsString()); |
555 base::MessageLoop::current()->Run(); | 556 base::MessageLoop::current()->Run(); |
556 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 557 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
557 ASSERT_FALSE(cb_other.is_null()); | 558 ASSERT_FALSE(cb_other.is_null()); |
558 | 559 |
559 // We expect that the interstitial is shown for the second phishing URL and | 560 // We expect that the interstitial is shown for the second phishing URL and |
560 // not for the first phishing URL. | 561 // not for the first phishing URL. |
561 UnsafeResource resource; | 562 UnsafeResource resource; |
562 EXPECT_CALL(*ui_manager_, DoDisplayBlockingPage(_)) | 563 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) |
563 .WillOnce(SaveArg<0>(&resource)); | 564 .WillOnce(SaveArg<0>(&resource)); |
564 | 565 |
565 cb.Run(phishing_url, true); // Should have no effect. | 566 cb.Run(phishing_url, true); // Should have no effect. |
566 cb_other.Run(other_phishing_url, true); // Should show interstitial. | 567 cb_other.Run(other_phishing_url, true); // Should show interstitial. |
567 | 568 |
568 base::MessageLoop::current()->RunUntilIdle(); | 569 base::MessageLoop::current()->RunUntilIdle(); |
569 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 570 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
570 EXPECT_EQ(other_phishing_url, resource.url); | 571 EXPECT_EQ(other_phishing_url, resource.url); |
571 EXPECT_EQ(other_phishing_url, resource.original_url); | 572 EXPECT_EQ(other_phishing_url, resource.original_url); |
572 EXPECT_FALSE(resource.is_subresource); | 573 EXPECT_FALSE(resource.is_subresource); |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
944 SafeBrowsingMsg_StartPhishingDetection::ID); | 945 SafeBrowsingMsg_StartPhishingDetection::ID); |
945 ASSERT_FALSE(msg); | 946 ASSERT_FALSE(msg); |
946 | 947 |
947 // If result is cached, we will try and display the blocking page directly | 948 // If result is cached, we will try and display the blocking page directly |
948 // with no start classification message. | 949 // with no start classification message. |
949 url = GURL("http://host8.com/"); | 950 url = GURL("http://host8.com/"); |
950 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kTrue, NULL, | 951 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kTrue, NULL, |
951 NULL); | 952 NULL); |
952 | 953 |
953 UnsafeResource resource; | 954 UnsafeResource resource; |
954 EXPECT_CALL(*ui_manager_, DoDisplayBlockingPage(_)) | 955 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) |
955 .WillOnce(SaveArg<0>(&resource)); | 956 .WillOnce(SaveArg<0>(&resource)); |
956 | 957 |
957 NavigateAndCommit(url); | 958 NavigateAndCommit(url); |
958 // Wait for CheckCsdWhitelist to be called on the IO thread. | 959 // Wait for CheckCsdWhitelist to be called on the IO thread. |
959 FlushIOMessageLoop(); | 960 FlushIOMessageLoop(); |
960 // Wait for CheckCache() to be called on the UI thread. | 961 // Wait for CheckCache() to be called on the UI thread. |
961 base::MessageLoop::current()->RunUntilIdle(); | 962 base::MessageLoop::current()->RunUntilIdle(); |
962 // Now we check that all expected functions were indeed called on the two | 963 // Now we check that all expected functions were indeed called on the two |
963 // service objects. | 964 // service objects. |
964 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 965 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
965 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 966 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
966 EXPECT_EQ(url, resource.url); | 967 EXPECT_EQ(url, resource.url); |
967 EXPECT_EQ(url, resource.original_url); | 968 EXPECT_EQ(url, resource.original_url); |
968 resource.callback.Reset(); | 969 resource.callback.Reset(); |
969 msg = process()->sink().GetFirstMessageMatching( | 970 msg = process()->sink().GetFirstMessageMatching( |
970 SafeBrowsingMsg_StartPhishingDetection::ID); | 971 SafeBrowsingMsg_StartPhishingDetection::ID); |
971 ASSERT_FALSE(msg); | 972 ASSERT_FALSE(msg); |
972 } | 973 } |
973 | 974 |
974 } // namespace safe_browsing | 975 } // namespace safe_browsing |
OLD | NEW |