| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Note: this test uses RenderViewFakeResourcesTest in order to set up a | 5 // Note: this test uses RenderViewFakeResourcesTest in order to set up a |
| 6 // real RenderThread to hold the phishing Scorer object. | 6 // real RenderThread to hold the phishing Scorer object. |
| 7 | 7 |
| 8 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" | 8 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 private: | 57 private: |
| 58 DISALLOW_COPY_AND_ASSIGN(MockScorer); | 58 DISALLOW_COPY_AND_ASSIGN(MockScorer); |
| 59 }; | 59 }; |
| 60 } // namespace | 60 } // namespace |
| 61 | 61 |
| 62 class PhishingClassifierDelegateTest : public RenderViewFakeResourcesTest { | 62 class PhishingClassifierDelegateTest : public RenderViewFakeResourcesTest { |
| 63 protected: | 63 protected: |
| 64 bool OnMessageReceived(const IPC::Message& message) { | 64 bool OnMessageReceived(const IPC::Message& message) { |
| 65 bool handled = true; | 65 bool handled = true; |
| 66 IPC_BEGIN_MESSAGE_MAP(PhishingClassifierDelegateTest, message) | 66 IPC_BEGIN_MESSAGE_MAP(PhishingClassifierDelegateTest, message) |
| 67 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_DetectedPhishingSite, | 67 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_PhishingDetectionDone, |
| 68 OnDetectedPhishingSite) | 68 OnPhishingDetectionDone) |
| 69 IPC_MESSAGE_UNHANDLED( | 69 IPC_MESSAGE_UNHANDLED( |
| 70 handled = RenderViewFakeResourcesTest::OnMessageReceived(message)) | 70 handled = RenderViewFakeResourcesTest::OnMessageReceived(message)) |
| 71 IPC_END_MESSAGE_MAP() | 71 IPC_END_MESSAGE_MAP() |
| 72 return handled; | 72 return handled; |
| 73 } | 73 } |
| 74 | 74 |
| 75 void OnDetectedPhishingSite(const std::string& verdict_str) { | 75 void OnPhishingDetectionDone(const std::string& verdict_str) { |
| 76 scoped_ptr<ClientPhishingRequest> verdict(new ClientPhishingRequest); | 76 scoped_ptr<ClientPhishingRequest> verdict(new ClientPhishingRequest); |
| 77 if (verdict->ParseFromString(verdict_str) && | 77 if (verdict->ParseFromString(verdict_str) && |
| 78 verdict->IsInitialized()) { | 78 verdict->IsInitialized()) { |
| 79 verdict_.swap(verdict); | 79 verdict_.swap(verdict); |
| 80 } | 80 } |
| 81 message_loop_.Quit(); | 81 message_loop_.Quit(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 // Runs the ClassificationDone callback, then waits for the | 84 // Runs the ClassificationDone callback, then waits for the |
| 85 // DetectedPhishingSite IPC to arrive. | 85 // PhishingDetectionDone IPC to arrive. |
| 86 void RunClassificationDone(PhishingClassifierDelegate* delegate, | 86 void RunClassificationDone(PhishingClassifierDelegate* delegate, |
| 87 const ClientPhishingRequest& verdict) { | 87 const ClientPhishingRequest& verdict) { |
| 88 // Clear out any previous state. | 88 // Clear out any previous state. |
| 89 verdict_.reset(); | 89 verdict_.reset(); |
| 90 delegate->ClassificationDone(verdict); | 90 delegate->ClassificationDone(verdict); |
| 91 message_loop_.Run(); | 91 message_loop_.Run(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void OnStartPhishingDetection(PhishingClassifierDelegate* delegate, | 94 void OnStartPhishingDetection(PhishingClassifierDelegate* delegate, |
| 95 const GURL& url) { | 95 const GURL& url) { |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 | 432 |
| 433 page_text = ASCIIToUTF16("phish"); | 433 page_text = ASCIIToUTF16("phish"); |
| 434 EXPECT_CALL(*classifier, CancelPendingClassification()); | 434 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 435 delegate->PageCaptured(&page_text, false); | 435 delegate->PageCaptured(&page_text, false); |
| 436 Mock::VerifyAndClearExpectations(classifier); | 436 Mock::VerifyAndClearExpectations(classifier); |
| 437 | 437 |
| 438 // The delegate will cancel pending classification on destruction. | 438 // The delegate will cancel pending classification on destruction. |
| 439 EXPECT_CALL(*classifier, CancelPendingClassification()); | 439 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 440 } | 440 } |
| 441 | 441 |
| 442 TEST_F(PhishingClassifierDelegateTest, DetectedPhishingSite) { | 442 TEST_F(PhishingClassifierDelegateTest, PhishingDetectionDone) { |
| 443 // Tests that a DetectedPhishingSite IPC is sent to the browser | 443 // Tests that a PhishingDetectionDone IPC is sent to the browser |
| 444 // if a site comes back as phishy. | 444 // whenever we finish classification. |
| 445 MockPhishingClassifier* classifier = | 445 MockPhishingClassifier* classifier = |
| 446 new StrictMock<MockPhishingClassifier>(view_); | 446 new StrictMock<MockPhishingClassifier>(view_); |
| 447 PhishingClassifierDelegate* delegate = | 447 PhishingClassifierDelegate* delegate = |
| 448 PhishingClassifierDelegate::Create(view_, classifier); | 448 PhishingClassifierDelegate::Create(view_, classifier); |
| 449 MockScorer scorer; | 449 MockScorer scorer; |
| 450 delegate->SetPhishingScorer(&scorer); | 450 delegate->SetPhishingScorer(&scorer); |
| 451 ASSERT_TRUE(classifier->is_ready()); | 451 ASSERT_TRUE(classifier->is_ready()); |
| 452 | 452 |
| 453 // Start by loading a page to populate the delegate's state. | 453 // Start by loading a page to populate the delegate's state. |
| 454 responses_["http://host.com/"] = "<html><body>phish</body></html>"; | 454 responses_["http://host.com/"] = "<html><body>phish</body></html>"; |
| 455 EXPECT_CALL(*classifier, CancelPendingClassification()); | 455 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 456 LoadURL("http://host.com/#a"); | 456 LoadURL("http://host.com/#a"); |
| 457 Mock::VerifyAndClearExpectations(classifier); | 457 Mock::VerifyAndClearExpectations(classifier); |
| 458 string16 page_text = ASCIIToUTF16("phish"); | 458 string16 page_text = ASCIIToUTF16("phish"); |
| 459 OnStartPhishingDetection(delegate, GURL("http://host.com/#a")); | 459 OnStartPhishingDetection(delegate, GURL("http://host.com/#a")); |
| 460 { | 460 { |
| 461 InSequence s; | 461 InSequence s; |
| 462 EXPECT_CALL(*classifier, CancelPendingClassification()); | 462 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 463 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). | 463 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). |
| 464 WillOnce(DeleteArg<1>()); | 464 WillOnce(DeleteArg<1>()); |
| 465 delegate->PageCaptured(&page_text, false); | 465 delegate->PageCaptured(&page_text, false); |
| 466 Mock::VerifyAndClearExpectations(classifier); | 466 Mock::VerifyAndClearExpectations(classifier); |
| 467 } | 467 } |
| 468 | 468 |
| 469 // Now run the callback to simulate the classifier finishing. | 469 // Now run the callback to simulate the classifier finishing. |
| 470 ClientPhishingRequest verdict; | 470 ClientPhishingRequest verdict; |
| 471 verdict.set_url("http://host.com/#a"); | 471 verdict.set_url("http://host.com/#a"); |
| 472 verdict.set_client_score(0.8f); | 472 verdict.set_client_score(0.8f); |
| 473 verdict.set_is_phishing(true); | 473 verdict.set_is_phishing(false); // Send IPC even if site is not phishing. |
| 474 RunClassificationDone(delegate, verdict); | 474 RunClassificationDone(delegate, verdict); |
| 475 ASSERT_TRUE(verdict_.get()); | 475 ASSERT_TRUE(verdict_.get()); |
| 476 EXPECT_EQ(verdict.SerializeAsString(), verdict_->SerializeAsString()); | 476 EXPECT_EQ(verdict.SerializeAsString(), verdict_->SerializeAsString()); |
| 477 | 477 |
| 478 // The delegate will cancel pending classification on destruction. | 478 // The delegate will cancel pending classification on destruction. |
| 479 EXPECT_CALL(*classifier, CancelPendingClassification()); | 479 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 480 } | 480 } |
| 481 | 481 |
| 482 } // namespace safe_browsing | 482 } // namespace safe_browsing |
| OLD | NEW |