| 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" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/common/safe_browsing/csd.pb.h" | 12 #include "chrome/common/safe_browsing/csd.pb.h" |
| 13 #include "chrome/common/safe_browsing/safebrowsing_messages.h" | 13 #include "chrome/common/safe_browsing/safebrowsing_messages.h" |
| 14 #include "chrome/renderer/safe_browsing/features.h" | 14 #include "chrome/renderer/safe_browsing/features.h" |
| 15 #include "chrome/renderer/safe_browsing/phishing_classifier.h" | 15 #include "chrome/renderer/safe_browsing/phishing_classifier.h" |
| 16 #include "chrome/renderer/safe_browsing/scorer.h" | 16 #include "chrome/renderer/safe_browsing/scorer.h" |
| 17 #include "content/renderer/render_view.h" | 17 #include "content/public/renderer/render_view.h" |
| 18 #include "content/test/render_view_fake_resources_test.h" | 18 #include "content/test/render_view_fake_resources_test.h" |
| 19 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
| 25 | 25 |
| 26 using ::testing::_; | 26 using ::testing::_; |
| 27 using ::testing::DeleteArg; | 27 using ::testing::DeleteArg; |
| 28 using ::testing::InSequence; | 28 using ::testing::InSequence; |
| 29 using ::testing::Mock; | 29 using ::testing::Mock; |
| 30 using ::testing::Pointee; | 30 using ::testing::Pointee; |
| 31 using ::testing::StrictMock; | 31 using ::testing::StrictMock; |
| 32 | 32 |
| 33 namespace safe_browsing { | 33 namespace safe_browsing { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 class MockPhishingClassifier : public PhishingClassifier { | 36 class MockPhishingClassifier : public PhishingClassifier { |
| 37 public: | 37 public: |
| 38 explicit MockPhishingClassifier(RenderView* render_view) | 38 explicit MockPhishingClassifier(content::RenderView* render_view) |
| 39 : PhishingClassifier(render_view, NULL /* clock */) {} | 39 : PhishingClassifier(render_view, NULL /* clock */) {} |
| 40 | 40 |
| 41 virtual ~MockPhishingClassifier() {} | 41 virtual ~MockPhishingClassifier() {} |
| 42 | 42 |
| 43 MOCK_METHOD2(BeginClassification, void(const string16*, DoneCallback*)); | 43 MOCK_METHOD2(BeginClassification, void(const string16*, DoneCallback*)); |
| 44 MOCK_METHOD0(CancelPendingClassification, void()); | 44 MOCK_METHOD0(CancelPendingClassification, void()); |
| 45 | 45 |
| 46 private: | 46 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(MockPhishingClassifier); | 47 DISALLOW_COPY_AND_ASSIGN(MockPhishingClassifier); |
| 48 }; | 48 }; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 void OnStartPhishingDetection(PhishingClassifierDelegate* delegate, | 94 void OnStartPhishingDetection(PhishingClassifierDelegate* delegate, |
| 95 const GURL& url) { | 95 const GURL& url) { |
| 96 delegate->OnStartPhishingDetection(url); | 96 delegate->OnStartPhishingDetection(url); |
| 97 } | 97 } |
| 98 | 98 |
| 99 scoped_ptr<ClientPhishingRequest> verdict_; | 99 scoped_ptr<ClientPhishingRequest> verdict_; |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 TEST_F(PhishingClassifierDelegateTest, Navigation) { | 102 TEST_F(PhishingClassifierDelegateTest, Navigation) { |
| 103 MockPhishingClassifier* classifier = | 103 MockPhishingClassifier* classifier = |
| 104 new StrictMock<MockPhishingClassifier>(view_); | 104 new StrictMock<MockPhishingClassifier>(view()); |
| 105 PhishingClassifierDelegate* delegate = | 105 PhishingClassifierDelegate* delegate = |
| 106 PhishingClassifierDelegate::Create(view_, classifier); | 106 PhishingClassifierDelegate::Create(view(), classifier); |
| 107 MockScorer scorer; | 107 MockScorer scorer; |
| 108 delegate->SetPhishingScorer(&scorer); | 108 delegate->SetPhishingScorer(&scorer); |
| 109 ASSERT_TRUE(classifier->is_ready()); | 109 ASSERT_TRUE(classifier->is_ready()); |
| 110 | 110 |
| 111 // Test an initial load. We expect classification to happen normally. | 111 // Test an initial load. We expect classification to happen normally. |
| 112 EXPECT_CALL(*classifier, CancelPendingClassification()).Times(2); | 112 EXPECT_CALL(*classifier, CancelPendingClassification()).Times(2); |
| 113 responses_["http://host.com/"] = | 113 responses_["http://host.com/"] = |
| 114 "<html><body><iframe src=\"http://sub1.com/\"></iframe></body></html>"; | 114 "<html><body><iframe src=\"http://sub1.com/\"></iframe></body></html>"; |
| 115 LoadURL("http://host.com/"); | 115 LoadURL("http://host.com/"); |
| 116 Mock::VerifyAndClearExpectations(classifier); | 116 Mock::VerifyAndClearExpectations(classifier); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 delegate->PageCaptured(&page_text, false); | 221 delegate->PageCaptured(&page_text, false); |
| 222 Mock::VerifyAndClearExpectations(classifier); | 222 Mock::VerifyAndClearExpectations(classifier); |
| 223 | 223 |
| 224 // The delegate will cancel pending classification on destruction. | 224 // The delegate will cancel pending classification on destruction. |
| 225 EXPECT_CALL(*classifier, CancelPendingClassification()); | 225 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 226 } | 226 } |
| 227 | 227 |
| 228 TEST_F(PhishingClassifierDelegateTest, NoScorer) { | 228 TEST_F(PhishingClassifierDelegateTest, NoScorer) { |
| 229 // For this test, we'll create the delegate with no scorer available yet. | 229 // For this test, we'll create the delegate with no scorer available yet. |
| 230 MockPhishingClassifier* classifier = | 230 MockPhishingClassifier* classifier = |
| 231 new StrictMock<MockPhishingClassifier>(view_); | 231 new StrictMock<MockPhishingClassifier>(view()); |
| 232 PhishingClassifierDelegate* delegate = | 232 PhishingClassifierDelegate* delegate = |
| 233 PhishingClassifierDelegate::Create(view_, classifier); | 233 PhishingClassifierDelegate::Create(view(), classifier); |
| 234 ASSERT_FALSE(classifier->is_ready()); | 234 ASSERT_FALSE(classifier->is_ready()); |
| 235 | 235 |
| 236 // Queue up a pending classification, cancel it, then queue up another one. | 236 // Queue up a pending classification, cancel it, then queue up another one. |
| 237 LoadURL("http://host.com/"); | 237 LoadURL("http://host.com/"); |
| 238 string16 page_text = ASCIIToUTF16("dummy"); | 238 string16 page_text = ASCIIToUTF16("dummy"); |
| 239 OnStartPhishingDetection(delegate, GURL("http://host.com/")); | 239 OnStartPhishingDetection(delegate, GURL("http://host.com/")); |
| 240 delegate->PageCaptured(&page_text, false); | 240 delegate->PageCaptured(&page_text, false); |
| 241 | 241 |
| 242 LoadURL("http://host2.com/"); | 242 LoadURL("http://host2.com/"); |
| 243 page_text = ASCIIToUTF16("dummy2"); | 243 page_text = ASCIIToUTF16("dummy2"); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 260 Mock::VerifyAndClearExpectations(classifier); | 260 Mock::VerifyAndClearExpectations(classifier); |
| 261 | 261 |
| 262 // The delegate will cancel pending classification on destruction. | 262 // The delegate will cancel pending classification on destruction. |
| 263 EXPECT_CALL(*classifier, CancelPendingClassification()); | 263 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 264 } | 264 } |
| 265 | 265 |
| 266 TEST_F(PhishingClassifierDelegateTest, NoScorer_Ref) { | 266 TEST_F(PhishingClassifierDelegateTest, NoScorer_Ref) { |
| 267 // Similar to the last test, but navigates within the page before | 267 // Similar to the last test, but navigates within the page before |
| 268 // setting the scorer. | 268 // setting the scorer. |
| 269 MockPhishingClassifier* classifier = | 269 MockPhishingClassifier* classifier = |
| 270 new StrictMock<MockPhishingClassifier>(view_); | 270 new StrictMock<MockPhishingClassifier>(view()); |
| 271 PhishingClassifierDelegate* delegate = | 271 PhishingClassifierDelegate* delegate = |
| 272 PhishingClassifierDelegate::Create(view_, classifier); | 272 PhishingClassifierDelegate::Create(view(), classifier); |
| 273 ASSERT_FALSE(classifier->is_ready()); | 273 ASSERT_FALSE(classifier->is_ready()); |
| 274 | 274 |
| 275 // Queue up a pending classification, cancel it, then queue up another one. | 275 // Queue up a pending classification, cancel it, then queue up another one. |
| 276 LoadURL("http://host.com/"); | 276 LoadURL("http://host.com/"); |
| 277 string16 page_text = ASCIIToUTF16("dummy"); | 277 string16 page_text = ASCIIToUTF16("dummy"); |
| 278 OnStartPhishingDetection(delegate, GURL("http://host.com/")); | 278 OnStartPhishingDetection(delegate, GURL("http://host.com/")); |
| 279 delegate->PageCaptured(&page_text, false); | 279 delegate->PageCaptured(&page_text, false); |
| 280 | 280 |
| 281 LoadURL("http://host.com/#foo"); | 281 LoadURL("http://host.com/#foo"); |
| 282 OnStartPhishingDetection(delegate, GURL("http://host.com/#foo")); | 282 OnStartPhishingDetection(delegate, GURL("http://host.com/#foo")); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 293 Mock::VerifyAndClearExpectations(classifier); | 293 Mock::VerifyAndClearExpectations(classifier); |
| 294 | 294 |
| 295 // The delegate will cancel pending classification on destruction. | 295 // The delegate will cancel pending classification on destruction. |
| 296 EXPECT_CALL(*classifier, CancelPendingClassification()); | 296 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 297 } | 297 } |
| 298 | 298 |
| 299 TEST_F(PhishingClassifierDelegateTest, NoStartPhishingDetection) { | 299 TEST_F(PhishingClassifierDelegateTest, NoStartPhishingDetection) { |
| 300 // Tests the behavior when OnStartPhishingDetection has not yet been called | 300 // Tests the behavior when OnStartPhishingDetection has not yet been called |
| 301 // when the page load finishes. | 301 // when the page load finishes. |
| 302 MockPhishingClassifier* classifier = | 302 MockPhishingClassifier* classifier = |
| 303 new StrictMock<MockPhishingClassifier>(view_); | 303 new StrictMock<MockPhishingClassifier>(view()); |
| 304 PhishingClassifierDelegate* delegate = | 304 PhishingClassifierDelegate* delegate = |
| 305 PhishingClassifierDelegate::Create(view_, classifier); | 305 PhishingClassifierDelegate::Create(view(), classifier); |
| 306 MockScorer scorer; | 306 MockScorer scorer; |
| 307 delegate->SetPhishingScorer(&scorer); | 307 delegate->SetPhishingScorer(&scorer); |
| 308 ASSERT_TRUE(classifier->is_ready()); | 308 ASSERT_TRUE(classifier->is_ready()); |
| 309 | 309 |
| 310 EXPECT_CALL(*classifier, CancelPendingClassification()); | 310 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 311 responses_["http://host.com/"] = "<html><body>phish</body></html>"; | 311 responses_["http://host.com/"] = "<html><body>phish</body></html>"; |
| 312 LoadURL("http://host.com/"); | 312 LoadURL("http://host.com/"); |
| 313 Mock::VerifyAndClearExpectations(classifier); | 313 Mock::VerifyAndClearExpectations(classifier); |
| 314 string16 page_text = ASCIIToUTF16("phish"); | 314 string16 page_text = ASCIIToUTF16("phish"); |
| 315 EXPECT_CALL(*classifier, CancelPendingClassification()); | 315 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 Mock::VerifyAndClearExpectations(classifier); | 367 Mock::VerifyAndClearExpectations(classifier); |
| 368 } | 368 } |
| 369 | 369 |
| 370 // The delegate will cancel pending classification on destruction. | 370 // The delegate will cancel pending classification on destruction. |
| 371 EXPECT_CALL(*classifier, CancelPendingClassification()); | 371 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 372 } | 372 } |
| 373 | 373 |
| 374 TEST_F(PhishingClassifierDelegateTest, IgnorePreliminaryCapture) { | 374 TEST_F(PhishingClassifierDelegateTest, IgnorePreliminaryCapture) { |
| 375 // Tests that preliminary PageCaptured notifications are ignored. | 375 // Tests that preliminary PageCaptured notifications are ignored. |
| 376 MockPhishingClassifier* classifier = | 376 MockPhishingClassifier* classifier = |
| 377 new StrictMock<MockPhishingClassifier>(view_); | 377 new StrictMock<MockPhishingClassifier>(view()); |
| 378 PhishingClassifierDelegate* delegate = | 378 PhishingClassifierDelegate* delegate = |
| 379 PhishingClassifierDelegate::Create(view_, classifier); | 379 PhishingClassifierDelegate::Create(view(), classifier); |
| 380 MockScorer scorer; | 380 MockScorer scorer; |
| 381 delegate->SetPhishingScorer(&scorer); | 381 delegate->SetPhishingScorer(&scorer); |
| 382 ASSERT_TRUE(classifier->is_ready()); | 382 ASSERT_TRUE(classifier->is_ready()); |
| 383 | 383 |
| 384 EXPECT_CALL(*classifier, CancelPendingClassification()); | 384 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 385 responses_["http://host.com/"] = "<html><body>phish</body></html>"; | 385 responses_["http://host.com/"] = "<html><body>phish</body></html>"; |
| 386 LoadURL("http://host.com/"); | 386 LoadURL("http://host.com/"); |
| 387 Mock::VerifyAndClearExpectations(classifier); | 387 Mock::VerifyAndClearExpectations(classifier); |
| 388 OnStartPhishingDetection(delegate, GURL("http://host.com/")); | 388 OnStartPhishingDetection(delegate, GURL("http://host.com/")); |
| 389 string16 page_text = ASCIIToUTF16("phish"); | 389 string16 page_text = ASCIIToUTF16("phish"); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 401 } | 401 } |
| 402 | 402 |
| 403 // The delegate will cancel pending classification on destruction. | 403 // The delegate will cancel pending classification on destruction. |
| 404 EXPECT_CALL(*classifier, CancelPendingClassification()); | 404 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 405 } | 405 } |
| 406 | 406 |
| 407 TEST_F(PhishingClassifierDelegateTest, DuplicatePageCapture) { | 407 TEST_F(PhishingClassifierDelegateTest, DuplicatePageCapture) { |
| 408 // Tests that a second PageCaptured notification causes classification to | 408 // Tests that a second PageCaptured notification causes classification to |
| 409 // be cancelled. | 409 // be cancelled. |
| 410 MockPhishingClassifier* classifier = | 410 MockPhishingClassifier* classifier = |
| 411 new StrictMock<MockPhishingClassifier>(view_); | 411 new StrictMock<MockPhishingClassifier>(view()); |
| 412 PhishingClassifierDelegate* delegate = | 412 PhishingClassifierDelegate* delegate = |
| 413 PhishingClassifierDelegate::Create(view_, classifier); | 413 PhishingClassifierDelegate::Create(view(), classifier); |
| 414 MockScorer scorer; | 414 MockScorer scorer; |
| 415 delegate->SetPhishingScorer(&scorer); | 415 delegate->SetPhishingScorer(&scorer); |
| 416 ASSERT_TRUE(classifier->is_ready()); | 416 ASSERT_TRUE(classifier->is_ready()); |
| 417 | 417 |
| 418 EXPECT_CALL(*classifier, CancelPendingClassification()); | 418 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 419 responses_["http://host.com/"] = "<html><body>phish</body></html>"; | 419 responses_["http://host.com/"] = "<html><body>phish</body></html>"; |
| 420 LoadURL("http://host.com/"); | 420 LoadURL("http://host.com/"); |
| 421 Mock::VerifyAndClearExpectations(classifier); | 421 Mock::VerifyAndClearExpectations(classifier); |
| 422 OnStartPhishingDetection(delegate, GURL("http://host.com/")); | 422 OnStartPhishingDetection(delegate, GURL("http://host.com/")); |
| 423 string16 page_text = ASCIIToUTF16("phish"); | 423 string16 page_text = ASCIIToUTF16("phish"); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 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, PhishingDetectionDone) { | 442 TEST_F(PhishingClassifierDelegateTest, PhishingDetectionDone) { |
| 443 // Tests that a PhishingDetectionDone IPC is sent to the browser | 443 // Tests that a PhishingDetectionDone IPC is sent to the browser |
| 444 // whenever we finish classification. | 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"); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 473 verdict.set_is_phishing(false); // Send IPC even if site is not phishing. | 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 |