| 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/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // Test that the callback is NULL when the verdict is not phishing. | 84 // Test that the callback is NULL when the verdict is not phishing. |
| 85 MATCHER(CallbackIsNull, "") { | 85 MATCHER(CallbackIsNull, "") { |
| 86 return arg.is_null(); | 86 return arg.is_null(); |
| 87 } | 87 } |
| 88 | 88 |
| 89 ACTION(QuitUIMessageLoop) { | 89 ACTION(QuitUIMessageLoop) { |
| 90 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 90 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 91 base::MessageLoopForUI::current()->Quit(); | 91 base::MessageLoopForUI::current()->Quit(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // It's kind of insane that InvokeArgument doesn't work with callbacks, but it | 94 ACTION_P(InvokeDoneCallback, verdict) { |
| 95 // doesn't seem like it. | 95 scoped_ptr<ClientPhishingRequest> request(::std::tr1::get<1>(args)); |
| 96 ACTION_TEMPLATE(InvokeCallbackArgument, | 96 request->CopyFrom(*verdict); |
| 97 HAS_1_TEMPLATE_PARAMS(int, k), | 97 ::std::tr1::get<2>(args).Run(true, request.Pass()); |
| 98 AND_2_VALUE_PARAMS(p0, p1)) { | |
| 99 ::std::tr1::get<k>(args).Run(p0, p1); | |
| 100 } | 98 } |
| 101 | 99 |
| 102 ACTION_P(InvokeMalwareCallback, verdict) { | 100 ACTION_P(InvokeMalwareCallback, verdict) { |
| 103 scoped_ptr<ClientMalwareRequest> request(::std::tr1::get<1>(args)); | 101 scoped_ptr<ClientMalwareRequest> request(::std::tr1::get<1>(args)); |
| 104 request->CopyFrom(*verdict); | 102 request->CopyFrom(*verdict); |
| 105 ::std::tr1::get<2>(args).Run(true, request.Pass()); | 103 ::std::tr1::get<2>(args).Run(true, request.Pass()); |
| 106 } | 104 } |
| 107 | 105 |
| 108 void EmptyUrlCheckCallback(bool processed) { | 106 void EmptyUrlCheckCallback(bool processed) { |
| 109 } | 107 } |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 csd_host_.get()); | 473 csd_host_.get()); |
| 476 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 474 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| 477 | 475 |
| 478 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; | 476 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
| 479 ClientPhishingRequest verdict; | 477 ClientPhishingRequest verdict; |
| 480 verdict.set_url("http://phishingurl.com/"); | 478 verdict.set_url("http://phishingurl.com/"); |
| 481 verdict.set_client_score(1.0f); | 479 verdict.set_client_score(1.0f); |
| 482 verdict.set_is_phishing(true); | 480 verdict.set_is_phishing(true); |
| 483 | 481 |
| 484 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 482 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
| 485 .WillOnce(DoAll(DeleteArg<1>(), | 483 .WillOnce(InvokeDoneCallback(&verdict)); |
| 486 InvokeCallbackArgument<2>(true, &verdict))); | |
| 487 EXPECT_CALL(*csd_service_, | 484 EXPECT_CALL(*csd_service_, |
| 488 SendClientReportPhishingRequest( | 485 SendClientReportPhishingRequest( |
| 489 Pointee(PartiallyEqualVerdict(verdict)), _)) | 486 Pointee(PartiallyEqualVerdict(verdict)), _)) |
| 490 .WillOnce(SaveArg<1>(&cb)); | 487 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); |
| 491 OnPhishingDetectionDone(verdict.SerializeAsString()); | 488 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 492 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); | 489 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); |
| 493 ASSERT_FALSE(cb.is_null()); | 490 ASSERT_FALSE(cb.is_null()); |
| 494 | 491 |
| 495 // Make sure DisplayBlockingPage is not going to be called. | 492 // Make sure DisplayBlockingPage is not going to be called. |
| 496 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)).Times(0); | 493 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)).Times(0); |
| 497 cb.Run(GURL(verdict.url()), false); | 494 cb.Run(GURL(verdict.url()), false); |
| 498 base::RunLoop().RunUntilIdle(); | 495 base::RunLoop().RunUntilIdle(); |
| 499 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 496 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
| 500 } | 497 } |
| 501 | 498 |
| 502 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { | 499 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { |
| 503 // Case 2: client thinks the page is phishing and so does the server but | 500 // Case 2: client thinks the page is phishing and so does the server but |
| 504 // showing the interstitial is disabled => no interstitial is shown. | 501 // showing the interstitial is disabled => no interstitial is shown. |
| 505 MockBrowserFeatureExtractor* mock_extractor = | 502 MockBrowserFeatureExtractor* mock_extractor = |
| 506 new StrictMock<MockBrowserFeatureExtractor>( | 503 new StrictMock<MockBrowserFeatureExtractor>( |
| 507 web_contents(), | 504 web_contents(), |
| 508 csd_host_.get()); | 505 csd_host_.get()); |
| 509 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 506 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| 510 | 507 |
| 511 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; | 508 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
| 512 ClientPhishingRequest verdict; | 509 ClientPhishingRequest verdict; |
| 513 verdict.set_url("http://phishingurl.com/"); | 510 verdict.set_url("http://phishingurl.com/"); |
| 514 verdict.set_client_score(1.0f); | 511 verdict.set_client_score(1.0f); |
| 515 verdict.set_is_phishing(true); | 512 verdict.set_is_phishing(true); |
| 516 | 513 |
| 517 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 514 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
| 518 .WillOnce(DoAll(DeleteArg<1>(), | 515 .WillOnce(InvokeDoneCallback(&verdict)); |
| 519 InvokeCallbackArgument<2>(true, &verdict))); | |
| 520 EXPECT_CALL(*csd_service_, | 516 EXPECT_CALL(*csd_service_, |
| 521 SendClientReportPhishingRequest( | 517 SendClientReportPhishingRequest( |
| 522 Pointee(PartiallyEqualVerdict(verdict)), _)) | 518 Pointee(PartiallyEqualVerdict(verdict)), _)) |
| 523 .WillOnce(SaveArg<1>(&cb)); | 519 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); |
| 524 OnPhishingDetectionDone(verdict.SerializeAsString()); | 520 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 525 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); | 521 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); |
| 526 ASSERT_FALSE(cb.is_null()); | 522 ASSERT_FALSE(cb.is_null()); |
| 527 | 523 |
| 528 // Make sure DisplayBlockingPage is not going to be called. | 524 // Make sure DisplayBlockingPage is not going to be called. |
| 529 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)).Times(0); | 525 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)).Times(0); |
| 530 cb.Run(GURL(verdict.url()), false); | 526 cb.Run(GURL(verdict.url()), false); |
| 531 base::RunLoop().RunUntilIdle(); | 527 base::RunLoop().RunUntilIdle(); |
| 532 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 528 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
| 533 } | 529 } |
| 534 | 530 |
| 535 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { | 531 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { |
| 536 // Case 3: client thinks the page is phishing and so does the server. | 532 // Case 3: client thinks the page is phishing and so does the server. |
| 537 // We show an interstitial. | 533 // We show an interstitial. |
| 538 MockBrowserFeatureExtractor* mock_extractor = | 534 MockBrowserFeatureExtractor* mock_extractor = |
| 539 new StrictMock<MockBrowserFeatureExtractor>( | 535 new StrictMock<MockBrowserFeatureExtractor>( |
| 540 web_contents(), | 536 web_contents(), |
| 541 csd_host_.get()); | 537 csd_host_.get()); |
| 542 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 538 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| 543 | 539 |
| 544 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; | 540 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
| 545 GURL phishing_url("http://phishingurl.com/"); | 541 GURL phishing_url("http://phishingurl.com/"); |
| 546 ClientPhishingRequest verdict; | 542 ClientPhishingRequest verdict; |
| 547 verdict.set_url(phishing_url.spec()); | 543 verdict.set_url(phishing_url.spec()); |
| 548 verdict.set_client_score(1.0f); | 544 verdict.set_client_score(1.0f); |
| 549 verdict.set_is_phishing(true); | 545 verdict.set_is_phishing(true); |
| 550 | 546 |
| 551 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 547 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
| 552 .WillOnce(DoAll(DeleteArg<1>(), | 548 .WillOnce(InvokeDoneCallback(&verdict)); |
| 553 InvokeCallbackArgument<2>(true, &verdict))); | |
| 554 EXPECT_CALL(*csd_service_, | 549 EXPECT_CALL(*csd_service_, |
| 555 SendClientReportPhishingRequest( | 550 SendClientReportPhishingRequest( |
| 556 Pointee(PartiallyEqualVerdict(verdict)), _)) | 551 Pointee(PartiallyEqualVerdict(verdict)), _)) |
| 557 .WillOnce(SaveArg<1>(&cb)); | 552 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); |
| 558 OnPhishingDetectionDone(verdict.SerializeAsString()); | 553 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 559 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); | 554 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); |
| 560 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 555 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 561 ASSERT_FALSE(cb.is_null()); | 556 ASSERT_FALSE(cb.is_null()); |
| 562 | 557 |
| 563 UnsafeResource resource; | 558 UnsafeResource resource; |
| 564 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)) | 559 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)) |
| 565 .WillOnce(SaveArg<0>(&resource)); | 560 .WillOnce(SaveArg<0>(&resource)); |
| 566 cb.Run(phishing_url, true); | 561 cb.Run(phishing_url, true); |
| 567 | 562 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 597 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 592 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| 598 | 593 |
| 599 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; | 594 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
| 600 GURL phishing_url("http://phishingurl.com/"); | 595 GURL phishing_url("http://phishingurl.com/"); |
| 601 ClientPhishingRequest verdict; | 596 ClientPhishingRequest verdict; |
| 602 verdict.set_url(phishing_url.spec()); | 597 verdict.set_url(phishing_url.spec()); |
| 603 verdict.set_client_score(1.0f); | 598 verdict.set_client_score(1.0f); |
| 604 verdict.set_is_phishing(true); | 599 verdict.set_is_phishing(true); |
| 605 | 600 |
| 606 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 601 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
| 607 .WillOnce(DoAll(DeleteArg<1>(), | 602 .WillOnce(InvokeDoneCallback(&verdict)); |
| 608 InvokeCallbackArgument<2>(true, &verdict))); | |
| 609 EXPECT_CALL(*csd_service_, | 603 EXPECT_CALL(*csd_service_, |
| 610 SendClientReportPhishingRequest( | 604 SendClientReportPhishingRequest( |
| 611 Pointee(PartiallyEqualVerdict(verdict)), _)) | 605 Pointee(PartiallyEqualVerdict(verdict)), _)) |
| 612 .WillOnce(SaveArg<1>(&cb)); | 606 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); |
| 613 OnPhishingDetectionDone(verdict.SerializeAsString()); | 607 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 614 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); | 608 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); |
| 615 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 609 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 616 ASSERT_FALSE(cb.is_null()); | 610 ASSERT_FALSE(cb.is_null()); |
| 617 | 611 |
| 618 // Set this back to a normal browser feature extractor since we're using | 612 // Set this back to a normal browser feature extractor since we're using |
| 619 // NavigateAndCommit() and it's easier to use the real thing than setting up | 613 // NavigateAndCommit() and it's easier to use the real thing than setting up |
| 620 // mock expectations. | 614 // mock expectations. |
| 621 SetFeatureExtractor(new BrowserFeatureExtractor(web_contents(), | 615 SetFeatureExtractor(new BrowserFeatureExtractor(web_contents(), |
| 622 csd_host_.get())); | 616 csd_host_.get())); |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1143 EXPECT_EQ(url, resource.url); | 1137 EXPECT_EQ(url, resource.url); |
| 1144 EXPECT_EQ(url, resource.original_url); | 1138 EXPECT_EQ(url, resource.original_url); |
| 1145 | 1139 |
| 1146 ExpectStartPhishingDetection(NULL); | 1140 ExpectStartPhishingDetection(NULL); |
| 1147 | 1141 |
| 1148 // Showing a phishing warning will invalidate all the weak pointers which | 1142 // Showing a phishing warning will invalidate all the weak pointers which |
| 1149 // means we will not extract malware features. | 1143 // means we will not extract malware features. |
| 1150 ExpectShouldClassifyForMalwareResult(false); | 1144 ExpectShouldClassifyForMalwareResult(false); |
| 1151 } | 1145 } |
| 1152 } // namespace safe_browsing | 1146 } // namespace safe_browsing |
| OLD | NEW |