Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(502)

Side by Side Diff: chrome/browser/safe_browsing/client_side_detection_host_unittest.cc

Issue 42553002: Mostly integrate new malware IP blacklist with the csd client. When (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove inline accessor Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 } 79 }
80 80
81 // It's kind of insane that InvokeArgument doesn't work with callbacks, but it 81 // It's kind of insane that InvokeArgument doesn't work with callbacks, but it
82 // doesn't seem like it. 82 // doesn't seem like it.
83 ACTION_TEMPLATE(InvokeCallbackArgument, 83 ACTION_TEMPLATE(InvokeCallbackArgument,
84 HAS_1_TEMPLATE_PARAMS(int, k), 84 HAS_1_TEMPLATE_PARAMS(int, k),
85 AND_2_VALUE_PARAMS(p0, p1)) { 85 AND_2_VALUE_PARAMS(p0, p1)) {
86 ::std::tr1::get<k>(args).Run(p0, p1); 86 ::std::tr1::get<k>(args).Run(p0, p1);
87 } 87 }
88 88
89 ACTION_P(InvokeMalwareCallback, verdict) {
90 scoped_ptr<ClientMalwareRequest> request(::std::tr1::get<1>(args));
91 request->CopyFrom(*verdict);
92 ::std::tr1::get<2>(args).Run(true, request.Pass());
93 }
94
89 void EmptyUrlCheckCallback(bool processed) { 95 void EmptyUrlCheckCallback(bool processed) {
90 } 96 }
91 97
92 class MockClientSideDetectionService : public ClientSideDetectionService { 98 class MockClientSideDetectionService : public ClientSideDetectionService {
93 public: 99 public:
94 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {} 100 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {}
95 virtual ~MockClientSideDetectionService() {}; 101 virtual ~MockClientSideDetectionService() {};
96 102
97 MOCK_METHOD2(SendClientReportPhishingRequest, 103 MOCK_METHOD2(SendClientReportPhishingRequest,
98 void(ClientPhishingRequest*, 104 void(ClientPhishingRequest*,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 private: 138 private:
133 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingUIManager); 139 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingUIManager);
134 }; 140 };
135 141
136 class MockSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager { 142 class MockSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager {
137 public: 143 public:
138 explicit MockSafeBrowsingDatabaseManager(SafeBrowsingService* service) 144 explicit MockSafeBrowsingDatabaseManager(SafeBrowsingService* service)
139 : SafeBrowsingDatabaseManager(service) { } 145 : SafeBrowsingDatabaseManager(service) { }
140 146
141 MOCK_METHOD1(MatchCsdWhitelistUrl, bool(const GURL&)); 147 MOCK_METHOD1(MatchCsdWhitelistUrl, bool(const GURL&));
148 MOCK_METHOD1(MatchMalwareIP, bool(const std::string& ip_address));
142 149
143 protected: 150 protected:
144 virtual ~MockSafeBrowsingDatabaseManager() {} 151 virtual ~MockSafeBrowsingDatabaseManager() {}
145 152
146 private: 153 private:
147 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); 154 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager);
148 }; 155 };
149 156
150 class MockTestingProfile : public TestingProfile { 157 class MockTestingProfile : public TestingProfile {
151 public: 158 public:
152 MockTestingProfile() {} 159 MockTestingProfile() {}
153 virtual ~MockTestingProfile() {} 160 virtual ~MockTestingProfile() {}
154 161
155 MOCK_CONST_METHOD0(IsOffTheRecord, bool()); 162 MOCK_CONST_METHOD0(IsOffTheRecord, bool());
156 }; 163 };
157 164
158 class MockBrowserFeatureExtractor : public BrowserFeatureExtractor { 165 class MockBrowserFeatureExtractor : public BrowserFeatureExtractor {
159 public: 166 public:
160 explicit MockBrowserFeatureExtractor( 167 explicit MockBrowserFeatureExtractor(
161 WebContents* tab, 168 WebContents* tab,
162 ClientSideDetectionService* service) 169 ClientSideDetectionHost* host)
163 : BrowserFeatureExtractor(tab, service) {} 170 : BrowserFeatureExtractor(tab, host) {}
164 virtual ~MockBrowserFeatureExtractor() {} 171 virtual ~MockBrowserFeatureExtractor() {}
165 172
166 MOCK_METHOD3(ExtractFeatures, 173 MOCK_METHOD3(ExtractFeatures,
167 void(const BrowseInfo* info, 174 void(const BrowseInfo*,
168 ClientPhishingRequest*, 175 ClientPhishingRequest*,
169 const BrowserFeatureExtractor::DoneCallback&)); 176 const BrowserFeatureExtractor::DoneCallback&));
170 177
171 MOCK_METHOD2(ExtractMalwareFeatures, 178 MOCK_METHOD3(ExtractMalwareFeatures,
172 void(const BrowseInfo* info, 179 void(BrowseInfo*,
173 ClientMalwareRequest*)); 180 ClientMalwareRequest*,
181 const BrowserFeatureExtractor::MalwareDoneCallback&));
174 }; 182 };
175 183
176 } // namespace 184 } // namespace
177 185
178 class ClientSideDetectionHostTest : public ChromeRenderViewHostTestHarness { 186 class ClientSideDetectionHostTest : public ChromeRenderViewHostTestHarness {
179 public: 187 public:
180 typedef SafeBrowsingUIManager::UnsafeResource UnsafeResource; 188 typedef SafeBrowsingUIManager::UnsafeResource UnsafeResource;
181 189
182 virtual void SetUp() { 190 virtual void SetUp() {
183 ChromeRenderViewHostTestHarness::SetUp(); 191 ChromeRenderViewHostTestHarness::SetUp();
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 // Crashes on Blink canary bots: http://crbug.com/299149 333 // Crashes on Blink canary bots: http://crbug.com/299149
326 TEST_F(ClientSideDetectionHostTest, 334 TEST_F(ClientSideDetectionHostTest,
327 DISABLED_OnPhishingDetectionDoneInvalidVerdict) { 335 DISABLED_OnPhishingDetectionDoneInvalidVerdict) {
328 #else 336 #else
329 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneInvalidVerdict) { 337 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneInvalidVerdict) {
330 #endif 338 #endif
331 // Case 0: renderer sends an invalid verdict string that we're unable to 339 // Case 0: renderer sends an invalid verdict string that we're unable to
332 // parse. 340 // parse.
333 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( 341 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor(
334 web_contents(), 342 web_contents(),
335 csd_service_.get()); 343 csd_host_.get());
336 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 344 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
337 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0); 345 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0);
338 OnPhishingDetectionDone("Invalid Protocol Buffer"); 346 OnPhishingDetectionDone("Invalid Protocol Buffer");
339 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); 347 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor));
340 } 348 }
341 349
342 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneNotPhishing) { 350 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneNotPhishing) {
343 // Case 1: client thinks the page is phishing. The server does not agree. 351 // Case 1: client thinks the page is phishing. The server does not agree.
344 // No interstitial is shown. 352 // No interstitial is shown.
345 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( 353 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor(
346 web_contents(), 354 web_contents(),
347 csd_service_.get()); 355 csd_host_.get());
348 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 356 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
349 357
350 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; 358 ClientSideDetectionService::ClientReportPhishingRequestCallback cb;
351 ClientPhishingRequest verdict; 359 ClientPhishingRequest verdict;
352 verdict.set_url("http://phishingurl.com/"); 360 verdict.set_url("http://phishingurl.com/");
353 verdict.set_client_score(1.0f); 361 verdict.set_client_score(1.0f);
354 verdict.set_is_phishing(true); 362 verdict.set_is_phishing(true);
355 363
356 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) 364 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _))
357 .WillOnce(DoAll(DeleteArg<1>(), 365 .WillOnce(DoAll(DeleteArg<1>(),
358 InvokeCallbackArgument<2>(true, &verdict))); 366 InvokeCallbackArgument<2>(true, &verdict)));
359 EXPECT_CALL(*csd_service_, 367 EXPECT_CALL(*csd_service_,
360 SendClientReportPhishingRequest( 368 SendClientReportPhishingRequest(
361 Pointee(PartiallyEqualVerdict(verdict)), _)) 369 Pointee(PartiallyEqualVerdict(verdict)), _))
362 .WillOnce(SaveArg<1>(&cb)); 370 .WillOnce(SaveArg<1>(&cb));
363 OnPhishingDetectionDone(verdict.SerializeAsString()); 371 OnPhishingDetectionDone(verdict.SerializeAsString());
364 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 372 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
365 ASSERT_FALSE(cb.is_null()); 373 ASSERT_FALSE(cb.is_null());
366 374
367 // Make sure DoDisplayBlockingPage is not going to be called. 375 // Make sure DoDisplayBlockingPage is not going to be called.
368 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)).Times(0); 376 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)).Times(0);
369 cb.Run(GURL(verdict.url()), false); 377 cb.Run(GURL(verdict.url()), false);
370 base::RunLoop().RunUntilIdle(); 378 base::RunLoop().RunUntilIdle();
371 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); 379 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get()));
372 } 380 }
373 381
374 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { 382 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) {
375 // Case 2: client thinks the page is phishing and so does the server but 383 // Case 2: client thinks the page is phishing and so does the server but
376 // showing the interstitial is disabled => no interstitial is shown. 384 // showing the interstitial is disabled => no interstitial is shown.
377 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( 385 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor(
378 web_contents(), 386 web_contents(),
379 csd_service_.get()); 387 csd_host_.get());
380 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 388 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
381 389
382 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; 390 ClientSideDetectionService::ClientReportPhishingRequestCallback cb;
383 ClientPhishingRequest verdict; 391 ClientPhishingRequest verdict;
384 verdict.set_url("http://phishingurl.com/"); 392 verdict.set_url("http://phishingurl.com/");
385 verdict.set_client_score(1.0f); 393 verdict.set_client_score(1.0f);
386 verdict.set_is_phishing(true); 394 verdict.set_is_phishing(true);
387 395
388 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) 396 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _))
389 .WillOnce(DoAll(DeleteArg<1>(), 397 .WillOnce(DoAll(DeleteArg<1>(),
390 InvokeCallbackArgument<2>(true, &verdict))); 398 InvokeCallbackArgument<2>(true, &verdict)));
391 EXPECT_CALL(*csd_service_, 399 EXPECT_CALL(*csd_service_,
392 SendClientReportPhishingRequest( 400 SendClientReportPhishingRequest(
393 Pointee(PartiallyEqualVerdict(verdict)), _)) 401 Pointee(PartiallyEqualVerdict(verdict)), _))
394 .WillOnce(SaveArg<1>(&cb)); 402 .WillOnce(SaveArg<1>(&cb));
395 OnPhishingDetectionDone(verdict.SerializeAsString()); 403 OnPhishingDetectionDone(verdict.SerializeAsString());
396 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 404 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
397 ASSERT_FALSE(cb.is_null()); 405 ASSERT_FALSE(cb.is_null());
398 406
399 // Make sure DoDisplayBlockingPage is not going to be called. 407 // Make sure DoDisplayBlockingPage is not going to be called.
400 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)).Times(0); 408 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)).Times(0);
401 cb.Run(GURL(verdict.url()), false); 409 cb.Run(GURL(verdict.url()), false);
402 base::RunLoop().RunUntilIdle(); 410 base::RunLoop().RunUntilIdle();
403 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); 411 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get()));
404 } 412 }
405 413
406 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { 414 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) {
407 // Case 3: client thinks the page is phishing and so does the server. 415 // Case 3: client thinks the page is phishing and so does the server.
408 // We show an interstitial. 416 // We show an interstitial.
409 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( 417 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor(
410 web_contents(), 418 web_contents(),
411 csd_service_.get()); 419 csd_host_.get());
412 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 420 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
413 421
414 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; 422 ClientSideDetectionService::ClientReportPhishingRequestCallback cb;
415 GURL phishing_url("http://phishingurl.com/"); 423 GURL phishing_url("http://phishingurl.com/");
416 ClientPhishingRequest verdict; 424 ClientPhishingRequest verdict;
417 verdict.set_url(phishing_url.spec()); 425 verdict.set_url(phishing_url.spec());
418 verdict.set_client_score(1.0f); 426 verdict.set_client_score(1.0f);
419 verdict.set_is_phishing(true); 427 verdict.set_is_phishing(true);
420 428
421 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) 429 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _))
422 .WillOnce(DoAll(DeleteArg<1>(), 430 .WillOnce(DoAll(DeleteArg<1>(),
423 InvokeCallbackArgument<2>(true, &verdict))); 431 InvokeCallbackArgument<2>(true, &verdict)));
424 EXPECT_CALL(*csd_service_, 432 EXPECT_CALL(*csd_service_,
425 SendClientReportPhishingRequest( 433 SendClientReportPhishingRequest(
426 Pointee(PartiallyEqualVerdict(verdict)), _)) 434 Pointee(PartiallyEqualVerdict(verdict)), _))
427 .WillOnce(SaveArg<1>(&cb)); 435 .WillOnce(SaveArg<1>(&cb));
428 OnPhishingDetectionDone(verdict.SerializeAsString()); 436 OnPhishingDetectionDone(verdict.SerializeAsString());
437 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
429 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 438 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
430 ASSERT_FALSE(cb.is_null()); 439 ASSERT_FALSE(cb.is_null());
431 440
432 UnsafeResource resource; 441 UnsafeResource resource;
433 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) 442 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_))
434 .WillOnce(SaveArg<0>(&resource)); 443 .WillOnce(SaveArg<0>(&resource));
435 cb.Run(phishing_url, true); 444 cb.Run(phishing_url, true);
436 445
437 base::RunLoop().RunUntilIdle(); 446 base::RunLoop().RunUntilIdle();
438 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); 447 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get()));
(...skipping 15 matching lines...) Expand all
454 } 463 }
455 464
456 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneMultiplePings) { 465 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneMultiplePings) {
457 // Case 4 & 5: client thinks a page is phishing then navigates to 466 // Case 4 & 5: client thinks a page is phishing then navigates to
458 // another page which is also considered phishing by the client 467 // another page which is also considered phishing by the client
459 // before the server responds with a verdict. After a while the 468 // before the server responds with a verdict. After a while the
460 // server responds for both requests with a phishing verdict. Only 469 // server responds for both requests with a phishing verdict. Only
461 // a single interstitial is shown for the second URL. 470 // a single interstitial is shown for the second URL.
462 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( 471 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor(
463 web_contents(), 472 web_contents(),
464 csd_service_.get()); 473 csd_host_.get());
465 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 474 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
466 475
467 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; 476 ClientSideDetectionService::ClientReportPhishingRequestCallback cb;
468 GURL phishing_url("http://phishingurl.com/"); 477 GURL phishing_url("http://phishingurl.com/");
469 ClientPhishingRequest verdict; 478 ClientPhishingRequest verdict;
470 verdict.set_url(phishing_url.spec()); 479 verdict.set_url(phishing_url.spec());
471 verdict.set_client_score(1.0f); 480 verdict.set_client_score(1.0f);
472 verdict.set_is_phishing(true); 481 verdict.set_is_phishing(true);
473 482
474 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) 483 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _))
475 .WillOnce(DoAll(DeleteArg<1>(), 484 .WillOnce(DoAll(DeleteArg<1>(),
476 InvokeCallbackArgument<2>(true, &verdict))); 485 InvokeCallbackArgument<2>(true, &verdict)));
477 EXPECT_CALL(*csd_service_, 486 EXPECT_CALL(*csd_service_,
478 SendClientReportPhishingRequest( 487 SendClientReportPhishingRequest(
479 Pointee(PartiallyEqualVerdict(verdict)), _)) 488 Pointee(PartiallyEqualVerdict(verdict)), _))
480 .WillOnce(SaveArg<1>(&cb)); 489 .WillOnce(SaveArg<1>(&cb));
481 OnPhishingDetectionDone(verdict.SerializeAsString()); 490 OnPhishingDetectionDone(verdict.SerializeAsString());
491 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
482 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 492 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
483 ASSERT_FALSE(cb.is_null()); 493 ASSERT_FALSE(cb.is_null());
484 494
485 // Set this back to a normal browser feature extractor since we're using 495 // Set this back to a normal browser feature extractor since we're using
486 // NavigateAndCommit() and it's easier to use the real thing than setting up 496 // NavigateAndCommit() and it's easier to use the real thing than setting up
487 // mock expectations. 497 // mock expectations.
488 SetFeatureExtractor(new BrowserFeatureExtractor(web_contents(), 498 SetFeatureExtractor(new BrowserFeatureExtractor(web_contents(),
489 csd_service_.get())); 499 csd_host_.get()));
490 GURL other_phishing_url("http://other_phishing_url.com/bla"); 500 GURL other_phishing_url("http://other_phishing_url.com/bla");
491 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, 501 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse,
492 &kFalse, &kFalse, &kFalse); 502 &kFalse, &kFalse, &kFalse);
493 // We navigate away. The callback cb should be revoked. 503 // We navigate away. The callback cb should be revoked.
494 NavigateAndCommit(other_phishing_url); 504 NavigateAndCommit(other_phishing_url);
495 // Wait for the pre-classification checks to finish for other_phishing_url. 505 // Wait for the pre-classification checks to finish for other_phishing_url.
496 WaitAndCheckPreClassificationChecks(); 506 WaitAndCheckPreClassificationChecks();
497 507
498 ClientSideDetectionService::ClientReportPhishingRequestCallback cb_other; 508 ClientSideDetectionService::ClientReportPhishingRequestCallback cb_other;
499 verdict.set_url(other_phishing_url.spec()); 509 verdict.set_url(other_phishing_url.spec());
500 verdict.set_client_score(0.8f); 510 verdict.set_client_score(0.8f);
501 EXPECT_CALL(*csd_service_, 511 EXPECT_CALL(*csd_service_,
502 SendClientReportPhishingRequest( 512 SendClientReportPhishingRequest(
503 Pointee(PartiallyEqualVerdict(verdict)), _)) 513 Pointee(PartiallyEqualVerdict(verdict)), _))
504 .WillOnce(DoAll(DeleteArg<0>(), 514 .WillOnce(DoAll(DeleteArg<0>(),
505 SaveArg<1>(&cb_other), 515 SaveArg<1>(&cb_other),
506 QuitUIMessageLoop())); 516 QuitUIMessageLoop()));
507 std::vector<GURL> redirect_chain; 517 std::vector<GURL> redirect_chain;
508 redirect_chain.push_back(other_phishing_url); 518 redirect_chain.push_back(other_phishing_url);
509 SetRedirectChain(redirect_chain); 519 SetRedirectChain(redirect_chain);
510 OnPhishingDetectionDone(verdict.SerializeAsString()); 520 OnPhishingDetectionDone(verdict.SerializeAsString());
511 base::MessageLoop::current()->Run(); 521 base::MessageLoop::current()->Run();
522 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
512 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 523 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
513 ASSERT_FALSE(cb_other.is_null()); 524 ASSERT_FALSE(cb_other.is_null());
514 525
515 // We expect that the interstitial is shown for the second phishing URL and 526 // We expect that the interstitial is shown for the second phishing URL and
516 // not for the first phishing URL. 527 // not for the first phishing URL.
517 UnsafeResource resource; 528 UnsafeResource resource;
518 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) 529 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_))
519 .WillOnce(SaveArg<0>(&resource)); 530 .WillOnce(SaveArg<0>(&resource));
520 531
521 cb.Run(phishing_url, true); // Should have no effect. 532 cb.Run(phishing_url, true); // Should have no effect.
(...skipping 16 matching lines...) Expand all
538 FROM_HERE, 549 FROM_HERE,
539 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, 550 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete,
540 ui_manager_, resource.callback)); 551 ui_manager_, resource.callback));
541 } 552 }
542 553
543 TEST_F(ClientSideDetectionHostTest, 554 TEST_F(ClientSideDetectionHostTest,
544 OnPhishingDetectionDoneVerdictNotPhishing) { 555 OnPhishingDetectionDoneVerdictNotPhishing) {
545 // Case 6: renderer sends a verdict string that isn't phishing. 556 // Case 6: renderer sends a verdict string that isn't phishing.
546 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( 557 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor(
547 web_contents(), 558 web_contents(),
548 csd_service_.get()); 559 csd_host_.get());
549 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 560 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
550 561
551 ClientPhishingRequest verdict; 562 ClientPhishingRequest verdict;
552 verdict.set_url("http://not-phishing.com/"); 563 verdict.set_url("http://not-phishing.com/");
553 verdict.set_client_score(0.1f); 564 verdict.set_client_score(0.1f);
554 verdict.set_is_phishing(false); 565 verdict.set_is_phishing(false);
555 566
556 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0); 567 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0);
557 OnPhishingDetectionDone(verdict.SerializeAsString()); 568 OnPhishingDetectionDone(verdict.SerializeAsString());
558 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); 569 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor));
(...skipping 18 matching lines...) Expand all
577 588
578 EXPECT_CALL(*csd_service_, 589 EXPECT_CALL(*csd_service_,
579 SendClientReportPhishingRequest( 590 SendClientReportPhishingRequest(
580 Pointee(PartiallyEqualVerdict(verdict)), CallbackIsNull())) 591 Pointee(PartiallyEqualVerdict(verdict)), CallbackIsNull()))
581 .WillOnce(DoAll(DeleteArg<0>(), QuitUIMessageLoop())); 592 .WillOnce(DoAll(DeleteArg<0>(), QuitUIMessageLoop()));
582 std::vector<GURL> redirect_chain; 593 std::vector<GURL> redirect_chain;
583 redirect_chain.push_back(url); 594 redirect_chain.push_back(url);
584 SetRedirectChain(redirect_chain); 595 SetRedirectChain(redirect_chain);
585 OnPhishingDetectionDone(verdict.SerializeAsString()); 596 OnPhishingDetectionDone(verdict.SerializeAsString());
586 base::MessageLoop::current()->Run(); 597 base::MessageLoop::current()->Run();
587 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 598 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
588 } 599 }
589 600
590 TEST_F(ClientSideDetectionHostTest, UpdateIPUrlMap) { 601 TEST_F(ClientSideDetectionHostTest, UpdateIPUrlMap) {
591 BrowseInfo* browse_info = GetBrowseInfo(); 602 BrowseInfo* browse_info = GetBrowseInfo();
592 603
593 // Empty IP or host are skipped 604 // Empty IP or host are skipped
594 UpdateIPUrlMap("250.10.10.10", std::string()); 605 UpdateIPUrlMap("250.10.10.10", std::string());
595 ASSERT_EQ(0U, browse_info->ips.size()); 606 ASSERT_EQ(0U, browse_info->ips.size());
596 UpdateIPUrlMap(std::string(), "http://google.com/a"); 607 UpdateIPUrlMap(std::string(), "http://google.com/a");
597 ASSERT_EQ(0U, browse_info->ips.size()); 608 ASSERT_EQ(0U, browse_info->ips.size());
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 expected_urls.insert("more.com/"); 650 expected_urls.insert("more.com/");
640 EXPECT_EQ(expected_urls, browse_info->ips["100.100.100.256"]); 651 EXPECT_EQ(expected_urls, browse_info->ips["100.100.100.256"]);
641 } 652 }
642 653
643 TEST_F(ClientSideDetectionHostTest, 654 TEST_F(ClientSideDetectionHostTest,
644 OnPhishingDetectionDoneVerdictNotPhishingNotMalwareIP) { 655 OnPhishingDetectionDoneVerdictNotPhishingNotMalwareIP) {
645 // Case 7: renderer sends a verdict string that isn't phishing and not matches 656 // Case 7: renderer sends a verdict string that isn't phishing and not matches
646 // malware bad IP list 657 // malware bad IP list
647 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( 658 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor(
648 web_contents(), 659 web_contents(),
649 csd_service_.get()); 660 csd_host_.get());
650 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 661 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
651 662
652 ClientPhishingRequest verdict; 663 ClientPhishingRequest verdict;
653 verdict.set_url("http://not-phishing.com/"); 664 verdict.set_url("http://not-phishing.com/");
654 verdict.set_client_score(0.1f); 665 verdict.set_client_score(0.1f);
655 verdict.set_is_phishing(false); 666 verdict.set_is_phishing(false);
656 667
657 ClientMalwareRequest malware_verdict; 668 ClientMalwareRequest malware_verdict;
658 malware_verdict.set_url("http://not-phishing.com/"); 669 malware_verdict.set_url("http://not-phishing.com/");
659 670
660 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _)) 671 // That is a special case. If there were no IP matches or if feature
661 .WillOnce(SetArgumentPointee<1>(malware_verdict)); 672 // extraction failed the callback will delete the malware_verdict.
673 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
674 .WillOnce(InvokeMalwareCallback(&malware_verdict));
662 EXPECT_CALL(*csd_service_, 675 EXPECT_CALL(*csd_service_,
663 SendClientReportMalwareRequest(_, _)).Times(0); 676 SendClientReportMalwareRequest(_, _)).Times(0);
664 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0); 677 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0);
665 678
666 OnPhishingDetectionDone(verdict.SerializeAsString()); 679 OnPhishingDetectionDone(verdict.SerializeAsString());
667 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); 680 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor));
668 } 681 }
669 682
670 TEST_F(ClientSideDetectionHostTest, 683 TEST_F(ClientSideDetectionHostTest,
671 OnPhishingDetectionDoneVerdictNotPhishingButMalwareIP) { 684 OnPhishingDetectionDoneVerdictNotPhishingButMalwareIP) {
672 // Case 8: renderer sends a verdict string that isn't phishing but matches 685 // Case 8: renderer sends a verdict string that isn't phishing but matches
673 // malware bad IP list 686 // malware bad IP list
674 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( 687 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor(
675 web_contents(), 688 web_contents(),
676 csd_service_.get()); 689 csd_host_.get());
677 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 690 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
678 691
679 ClientPhishingRequest verdict; 692 ClientPhishingRequest verdict;
680 verdict.set_url("http://not-phishing.com/"); 693 verdict.set_url("http://not-phishing.com/");
681 verdict.set_client_score(0.1f); 694 verdict.set_client_score(0.1f);
682 verdict.set_is_phishing(false); 695 verdict.set_is_phishing(false);
683 696
684 ClientMalwareRequest malware_verdict; 697 ClientMalwareRequest malware_verdict;
685 malware_verdict.set_url("http://not-phishing.com/"); 698 malware_verdict.set_url("http://not-phishing.com/");
686 ClientMalwareRequest::Feature* feature = malware_verdict.add_feature_map(); 699 ClientMalwareRequest::Feature* feature = malware_verdict.add_feature_map();
687 feature->set_name("malwareip1.2.3.4"); 700 feature->set_name("malwareip1.2.3.4");
688 feature->set_value(1.0); 701 feature->set_value(1.0);
689 feature->add_metainfo("badip.com"); 702 feature->add_metainfo("badip.com");
690 703
691 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _)) 704 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
692 .WillOnce(SetArgumentPointee<1>(malware_verdict)); 705 .WillOnce(InvokeMalwareCallback(&malware_verdict));
693 EXPECT_CALL(*csd_service_, 706 EXPECT_CALL(*csd_service_,
694 SendClientReportMalwareRequest( 707 SendClientReportMalwareRequest(
695 Pointee(PartiallyEqualMalwareVerdict(malware_verdict)), _)) 708 Pointee(PartiallyEqualMalwareVerdict(malware_verdict)), _))
696 .WillOnce(DeleteArg<0>()); 709 .WillOnce(DeleteArg<0>());
697 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0); 710 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0);
698 711
699 OnPhishingDetectionDone(verdict.SerializeAsString()); 712 OnPhishingDetectionDone(verdict.SerializeAsString());
700 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); 713 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor));
701 } 714 }
702 715
703 TEST_F(ClientSideDetectionHostTest, 716 TEST_F(ClientSideDetectionHostTest,
704 OnPhishingDetectionDoneVerdictPhishingAndMalwareIP) { 717 OnPhishingDetectionDoneVerdictPhishingAndMalwareIP) {
705 // Case 9: renderer sends a verdict string that is phishing and matches 718 // Case 9: renderer sends a verdict string that is phishing and matches
706 // malware bad IP list 719 // malware bad IP list
707 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( 720 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor(
708 web_contents(), 721 web_contents(),
709 csd_service_.get()); 722 csd_host_.get());
710 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 723 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
711 724
712 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; 725 ClientSideDetectionService::ClientReportPhishingRequestCallback cb;
713 ClientPhishingRequest verdict; 726 ClientPhishingRequest verdict;
714 verdict.set_url("http://not-phishing.com/"); 727 verdict.set_url("http://not-phishing.com/");
715 verdict.set_client_score(0.1f); 728 verdict.set_client_score(0.1f);
716 verdict.set_is_phishing(true); 729 verdict.set_is_phishing(true);
717 730
718 ClientMalwareRequest malware_verdict; 731 ClientMalwareRequest malware_verdict;
719 malware_verdict.set_url("http://not-phishing.com/"); 732 malware_verdict.set_url("http://not-phishing.com/");
720 ClientMalwareRequest::Feature* feature = malware_verdict.add_feature_map(); 733 ClientMalwareRequest::Feature* feature = malware_verdict.add_feature_map();
721 feature->set_name("malwareip1.2.3.4"); 734 feature->set_name("malwareip1.2.3.4");
722 feature->set_value(1.0); 735 feature->set_value(1.0);
723 feature->add_metainfo("badip.com"); 736 feature->add_metainfo("badip.com");
724 737
725 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _)) 738 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
726 .WillOnce(SetArgumentPointee<1>(malware_verdict)); 739 .WillOnce(InvokeMalwareCallback(&malware_verdict));
727 EXPECT_CALL(*csd_service_, 740 EXPECT_CALL(*csd_service_,
728 SendClientReportMalwareRequest( 741 SendClientReportMalwareRequest(
729 Pointee(PartiallyEqualMalwareVerdict(malware_verdict)), _)) 742 Pointee(PartiallyEqualMalwareVerdict(malware_verdict)), _))
730 .WillOnce(DeleteArg<0>()); 743 .WillOnce(DeleteArg<0>());
731 744
732 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) 745 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _))
733 .WillOnce(DoAll(DeleteArg<1>(), 746 .WillOnce(DoAll(DeleteArg<1>(),
734 InvokeCallbackArgument<2>(true, &verdict))); 747 InvokeCallbackArgument<2>(true, &verdict)));
735 748
736 EXPECT_CALL(*csd_service_, 749 EXPECT_CALL(*csd_service_,
737 SendClientReportPhishingRequest( 750 SendClientReportPhishingRequest(
738 Pointee(PartiallyEqualVerdict(verdict)), _)) 751 Pointee(PartiallyEqualVerdict(verdict)), _))
739 .WillOnce(SaveArg<1>(&cb)); 752 .WillOnce(SaveArg<1>(&cb));
740 OnPhishingDetectionDone(verdict.SerializeAsString()); 753 OnPhishingDetectionDone(verdict.SerializeAsString());
741 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); 754 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor));
755 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
742 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 756 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
743 ASSERT_FALSE(cb.is_null()); 757 ASSERT_FALSE(cb.is_null());
744 } 758 }
745 759
746 TEST_F(ClientSideDetectionHostTest, 760 TEST_F(ClientSideDetectionHostTest,
747 OnPhishingDetectionDoneShowMalwareInterstitial) { 761 OnPhishingDetectionDoneShowMalwareInterstitial) {
748 // Case 10: client thinks the page match malware IP and so does the server. 762 // Case 10: client thinks the page match malware IP and so does the server.
749 // We show an sub-resource malware interstitial. 763 // We show an sub-resource malware interstitial.
750 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( 764 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor(
751 web_contents(), 765 web_contents(),
752 csd_service_.get()); 766 csd_host_.get());
753 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 767 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
754 768
755 ClientPhishingRequest verdict; 769 ClientPhishingRequest verdict;
756 verdict.set_url("http://not-phishing.com/"); 770 verdict.set_url("http://not-phishing.com/");
757 verdict.set_client_score(0.1f); 771 verdict.set_client_score(0.1f);
758 verdict.set_is_phishing(false); 772 verdict.set_is_phishing(false);
759 773
760 ClientSideDetectionService::ClientReportMalwareRequestCallback cb; 774 ClientSideDetectionService::ClientReportMalwareRequestCallback cb;
761 GURL malware_landing_url("http://malware.com/"); 775 GURL malware_landing_url("http://malware.com/");
762 GURL malware_ip_url("http://badip.com"); 776 GURL malware_ip_url("http://badip.com");
763 ClientMalwareRequest malware_verdict; 777 ClientMalwareRequest malware_verdict;
764 malware_verdict.set_url("http://malware.com/"); 778 malware_verdict.set_url("http://malware.com/");
765 ClientMalwareRequest::Feature* feature = malware_verdict.add_feature_map(); 779 ClientMalwareRequest::Feature* feature = malware_verdict.add_feature_map();
766 feature->set_name("malwareip1.2.3.4"); 780 feature->set_name("malwareip1.2.3.4");
767 feature->set_value(1.0); 781 feature->set_value(1.0);
768 feature->add_metainfo("http://badip.com"); 782 feature->add_metainfo("http://badip.com");
769 783
770 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _)) 784 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
771 .WillOnce(SetArgumentPointee<1>(malware_verdict)); 785 .WillOnce(InvokeMalwareCallback(&malware_verdict));
772 EXPECT_CALL(*csd_service_, 786 EXPECT_CALL(*csd_service_,
773 SendClientReportMalwareRequest( 787 SendClientReportMalwareRequest(
774 Pointee(PartiallyEqualMalwareVerdict(malware_verdict)), _)) 788 Pointee(PartiallyEqualMalwareVerdict(malware_verdict)), _))
775 .WillOnce(DoAll(DeleteArg<0>(), 789 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb)));
776 SaveArg<1>(&cb)));
777 OnPhishingDetectionDone(verdict.SerializeAsString()); 790 OnPhishingDetectionDone(verdict.SerializeAsString());
791 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
778 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 792 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
779 ASSERT_FALSE(cb.is_null()); 793 ASSERT_FALSE(cb.is_null());
780 794
781 UnsafeResource resource; 795 UnsafeResource resource;
782 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) 796 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_))
783 .WillOnce(SaveArg<0>(&resource)); 797 .WillOnce(SaveArg<0>(&resource));
784 cb.Run(malware_landing_url, malware_ip_url, true); 798 cb.Run(malware_landing_url, malware_ip_url, true);
785 799
786 base::RunLoop().RunUntilIdle(); 800 base::RunLoop().RunUntilIdle();
787 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); 801 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get()));
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
963 977
964 UnsafeResource resource; 978 UnsafeResource resource;
965 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) 979 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_))
966 .WillOnce(SaveArg<0>(&resource)); 980 .WillOnce(SaveArg<0>(&resource));
967 981
968 NavigateAndCommit(url); 982 NavigateAndCommit(url);
969 // Wait for CheckCsdWhitelist and CheckCache() to be called. 983 // Wait for CheckCsdWhitelist and CheckCache() to be called.
970 base::RunLoop().RunUntilIdle(); 984 base::RunLoop().RunUntilIdle();
971 // Now we check that all expected functions were indeed called on the two 985 // Now we check that all expected functions were indeed called on the two
972 // service objects. 986 // service objects.
973 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 987 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
974 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); 988 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get()));
975 EXPECT_EQ(url, resource.url); 989 EXPECT_EQ(url, resource.url);
976 EXPECT_EQ(url, resource.original_url); 990 EXPECT_EQ(url, resource.original_url);
977 resource.callback.Reset(); 991 resource.callback.Reset();
978 msg = process()->sink().GetFirstMessageMatching( 992 msg = process()->sink().GetFirstMessageMatching(
979 SafeBrowsingMsg_StartPhishingDetection::ID); 993 SafeBrowsingMsg_StartPhishingDetection::ID);
980 ASSERT_FALSE(msg); 994 ASSERT_FALSE(msg);
981 } 995 }
982
983 } // namespace safe_browsing 996 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698