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

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

Issue 173133004: Separate pre-classification checks for client-side malware and phishing (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix the service unit-test. Created 6 years, 9 months 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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingUIManager); 152 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingUIManager);
153 }; 153 };
154 154
155 class MockSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager { 155 class MockSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager {
156 public: 156 public:
157 explicit MockSafeBrowsingDatabaseManager(SafeBrowsingService* service) 157 explicit MockSafeBrowsingDatabaseManager(SafeBrowsingService* service)
158 : SafeBrowsingDatabaseManager(service) { } 158 : SafeBrowsingDatabaseManager(service) { }
159 159
160 MOCK_METHOD1(MatchCsdWhitelistUrl, bool(const GURL&)); 160 MOCK_METHOD1(MatchCsdWhitelistUrl, bool(const GURL&));
161 MOCK_METHOD1(MatchMalwareIP, bool(const std::string& ip_address)); 161 MOCK_METHOD1(MatchMalwareIP, bool(const std::string& ip_address));
162 MOCK_METHOD0(IsMalwareKillSwitchOn, bool());
162 163
163 protected: 164 protected:
164 virtual ~MockSafeBrowsingDatabaseManager() {} 165 virtual ~MockSafeBrowsingDatabaseManager() {}
165 166
166 private: 167 private:
167 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); 168 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager);
168 }; 169 };
169 170
170 class MockTestingProfile : public TestingProfile { 171 class MockTestingProfile : public TestingProfile {
171 public: 172 public:
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 new StrictMock<MockSafeBrowsingDatabaseManager>(sb_service); 213 new StrictMock<MockSafeBrowsingDatabaseManager>(sb_service);
213 ui_manager_ = new StrictMock<MockSafeBrowsingUIManager>(sb_service); 214 ui_manager_ = new StrictMock<MockSafeBrowsingUIManager>(sb_service);
214 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( 215 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create(
215 web_contents())); 216 web_contents()));
216 csd_host_->set_client_side_detection_service(csd_service_.get()); 217 csd_host_->set_client_side_detection_service(csd_service_.get());
217 csd_host_->set_safe_browsing_managers(ui_manager_.get(), 218 csd_host_->set_safe_browsing_managers(ui_manager_.get(),
218 database_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 // By default this is set to false. Turn it on as if we are in canary or
224 // dev channel
225 csd_host_->malware_report_enabled_ = true;
226 } 223 }
227 224
228 virtual void TearDown() { 225 virtual void TearDown() {
229 // Delete the host object on the UI thread and release the 226 // Delete the host object on the UI thread and release the
230 // SafeBrowsingService. 227 // SafeBrowsingService.
231 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, 228 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE,
232 csd_host_.release()); 229 csd_host_.release());
233 database_manager_ = NULL; 230 database_manager_ = NULL;
234 ui_manager_ = NULL; 231 ui_manager_ = NULL;
235 base::RunLoop().RunUntilIdle(); 232 base::RunLoop().RunUntilIdle();
236 ChromeRenderViewHostTestHarness::TearDown(); 233 ChromeRenderViewHostTestHarness::TearDown();
237 } 234 }
238 235
239 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE { 236 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE {
240 // Set custom profile object so that we can mock calls to IsOffTheRecord. 237 // Set custom profile object so that we can mock calls to IsOffTheRecord.
241 // This needs to happen before we call the parent SetUp() function. We use 238 // This needs to happen before we call the parent SetUp() function. We use
242 // a nice mock because other parts of the code are calling IsOffTheRecord. 239 // a nice mock because other parts of the code are calling IsOffTheRecord.
243 mock_profile_ = new NiceMock<MockTestingProfile>(); 240 mock_profile_ = new NiceMock<MockTestingProfile>();
244 return mock_profile_; 241 return mock_profile_;
245 } 242 }
246 243
247 void OnPhishingDetectionDone(const std::string& verdict_str) { 244 void OnPhishingDetectionDone(const std::string& verdict_str) {
248 csd_host_->OnPhishingDetectionDone(verdict_str); 245 csd_host_->OnPhishingDetectionDone(verdict_str);
249 } 246 }
250 247
248 void DocumentOnLoadCompletedInMainFrame(int32 page_id) {
249 csd_host_->DocumentOnLoadCompletedInMainFrame(page_id);
250 }
251
251 void UpdateIPUrlMap(const std::string& ip, const std::string& host) { 252 void UpdateIPUrlMap(const std::string& ip, const std::string& host) {
252 csd_host_->UpdateIPUrlMap(ip, host, "", "", ResourceType::OBJECT); 253 csd_host_->UpdateIPUrlMap(ip, host, "", "", ResourceType::OBJECT);
253 } 254 }
254 255
255 BrowseInfo* GetBrowseInfo() { 256 BrowseInfo* GetBrowseInfo() {
256 return csd_host_->browse_info_.get(); 257 return csd_host_->browse_info_.get();
257 } 258 }
258 259
259 void ExpectPreClassificationChecks(const GURL& url, 260 void ExpectPreClassificationChecks(const GURL& url,
260 const bool* is_private, 261 const bool* is_private,
261 const bool* is_incognito, 262 const bool* is_incognito,
262 const bool* match_csd_whitelist, 263 const bool* match_csd_whitelist,
263 const bool* get_valid_cached_result, 264 const bool* get_valid_cached_result,
264 const bool* is_in_cache, 265 const bool* is_in_cache,
265 const bool* over_report_limit) { 266 const bool* over_report_limit) {
266 if (is_private) { 267 if (is_private) {
267 EXPECT_CALL(*csd_service_, IsPrivateIPAddress(_)) 268 EXPECT_CALL(*csd_service_, IsPrivateIPAddress(_))
268 .WillOnce(Return(*is_private)); 269 .WillOnce(Return(*is_private));
269 } 270 }
270 if (is_incognito) { 271 if (is_incognito) {
271 EXPECT_CALL(*mock_profile_, IsOffTheRecord()) 272 EXPECT_CALL(*mock_profile_, IsOffTheRecord())
272 .WillRepeatedly(Return(*is_incognito)); 273 .WillRepeatedly(Return(*is_incognito));
273 } 274 }
274 if (match_csd_whitelist) { 275 if (match_csd_whitelist) {
mattm 2014/03/18 02:19:06 hm, does it make sense to make these dependent on
noé 2014/03/20 17:01:45 Separated them.
275 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(url)) 276 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(url))
276 .WillOnce(Return(*match_csd_whitelist)); 277 .WillOnce(Return(*match_csd_whitelist));
278 EXPECT_CALL(*database_manager_.get(), IsMalwareKillSwitchOn())
279 .WillRepeatedly(Return(*match_csd_whitelist));
mattm 2014/03/18 02:19:06 reason this is Repeatedly instead of Once?
noé 2014/03/20 17:01:45 That's because some tests call the Expect method m
277 } 280 }
278 if (get_valid_cached_result) { 281 if (get_valid_cached_result) {
279 EXPECT_CALL(*csd_service_, GetValidCachedResult(url, NotNull())) 282 EXPECT_CALL(*csd_service_, GetValidCachedResult(url, NotNull()))
280 .WillOnce(DoAll(SetArgumentPointee<1>(true), 283 .WillOnce(DoAll(SetArgumentPointee<1>(true),
281 Return(*get_valid_cached_result))); 284 Return(*get_valid_cached_result)));
282 } 285 }
283 if (is_in_cache) { 286 if (is_in_cache) {
284 EXPECT_CALL(*csd_service_, IsInCache(url)).WillOnce(Return(*is_in_cache)); 287 EXPECT_CALL(*csd_service_, IsInCache(url)).WillOnce(Return(*is_in_cache));
285 } 288 }
286 if (over_report_limit) { 289 if (over_report_limit) {
(...skipping 16 matching lines...) Expand all
303 } 306 }
304 307
305 void SetRedirectChain(const std::vector<GURL>& redirect_chain) { 308 void SetRedirectChain(const std::vector<GURL>& redirect_chain) {
306 csd_host_->browse_info_->url_redirects = redirect_chain; 309 csd_host_->browse_info_->url_redirects = redirect_chain;
307 } 310 }
308 311
309 void SetReferrer(const GURL& referrer) { 312 void SetReferrer(const GURL& referrer) {
310 csd_host_->browse_info_->referrer = referrer; 313 csd_host_->browse_info_->referrer = referrer;
311 } 314 }
312 315
316 void ExpectShouldClassifyForMalwareResult(const bool* should_classify) {
317 if (should_classify == NULL) {
318 EXPECT_EQ(NULL, csd_host_->should_classify_for_malware_.get());
319 } else {
320 EXPECT_TRUE(csd_host_->should_classify_for_malware_.get());
321 if (csd_host_->should_classify_for_malware_.get()) {
322 EXPECT_EQ(*should_classify, *(csd_host_->should_classify_for_malware_));
323 }
324 }
325 }
326
313 void TestUnsafeResourceCopied(const UnsafeResource& resource) { 327 void TestUnsafeResourceCopied(const UnsafeResource& resource) {
314 ASSERT_TRUE(csd_host_->unsafe_resource_.get()); 328 ASSERT_TRUE(csd_host_->unsafe_resource_.get());
315 // Test that the resource from OnSafeBrowsingHit notification was copied 329 // Test that the resource from OnSafeBrowsingHit notification was copied
316 // into the CSDH. 330 // into the CSDH.
317 EXPECT_EQ(resource.url, csd_host_->unsafe_resource_->url); 331 EXPECT_EQ(resource.url, csd_host_->unsafe_resource_->url);
318 EXPECT_EQ(resource.original_url, csd_host_->unsafe_resource_->original_url); 332 EXPECT_EQ(resource.original_url, csd_host_->unsafe_resource_->original_url);
319 EXPECT_EQ(resource.is_subresource, 333 EXPECT_EQ(resource.is_subresource,
320 csd_host_->unsafe_resource_->is_subresource); 334 csd_host_->unsafe_resource_->is_subresource);
321 EXPECT_EQ(resource.threat_type, csd_host_->unsafe_resource_->threat_type); 335 EXPECT_EQ(resource.threat_type, csd_host_->unsafe_resource_->threat_type);
322 EXPECT_TRUE(csd_host_->unsafe_resource_->callback.is_null()); 336 EXPECT_TRUE(csd_host_->unsafe_resource_->callback.is_null());
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 web_contents(), 457 web_contents(),
444 csd_host_.get()); 458 csd_host_.get());
445 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 459 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
446 460
447 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; 461 ClientSideDetectionService::ClientReportPhishingRequestCallback cb;
448 ClientPhishingRequest verdict; 462 ClientPhishingRequest verdict;
449 verdict.set_url("http://phishingurl.com/"); 463 verdict.set_url("http://phishingurl.com/");
450 verdict.set_client_score(1.0f); 464 verdict.set_client_score(1.0f);
451 verdict.set_is_phishing(true); 465 verdict.set_is_phishing(true);
452 466
453 ClientMalwareRequest malware_verdict;
454 malware_verdict.set_url(verdict.url());
455 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) 467 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _))
456 .WillOnce(DoAll(DeleteArg<1>(), 468 .WillOnce(DoAll(DeleteArg<1>(),
457 InvokeCallbackArgument<2>(true, &verdict))); 469 InvokeCallbackArgument<2>(true, &verdict)));
458 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
459 .WillOnce(InvokeMalwareCallback(&malware_verdict));
460 EXPECT_CALL(*csd_service_, 470 EXPECT_CALL(*csd_service_,
461 SendClientReportPhishingRequest( 471 SendClientReportPhishingRequest(
462 Pointee(PartiallyEqualVerdict(verdict)), _)) 472 Pointee(PartiallyEqualVerdict(verdict)), _))
463 .WillOnce(SaveArg<1>(&cb)); 473 .WillOnce(SaveArg<1>(&cb));
464 OnPhishingDetectionDone(verdict.SerializeAsString()); 474 OnPhishingDetectionDone(verdict.SerializeAsString());
465 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); 475 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
466 ASSERT_FALSE(cb.is_null()); 476 ASSERT_FALSE(cb.is_null());
467 477
468 // Make sure DisplayBlockingPage is not going to be called. 478 // Make sure DisplayBlockingPage is not going to be called.
469 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)).Times(0); 479 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)).Times(0);
(...skipping 17 matching lines...) Expand all
487 verdict.set_client_score(1.0f); 497 verdict.set_client_score(1.0f);
488 verdict.set_is_phishing(true); 498 verdict.set_is_phishing(true);
489 499
490 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) 500 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _))
491 .WillOnce(DoAll(DeleteArg<1>(), 501 .WillOnce(DoAll(DeleteArg<1>(),
492 InvokeCallbackArgument<2>(true, &verdict))); 502 InvokeCallbackArgument<2>(true, &verdict)));
493 EXPECT_CALL(*csd_service_, 503 EXPECT_CALL(*csd_service_,
494 SendClientReportPhishingRequest( 504 SendClientReportPhishingRequest(
495 Pointee(PartiallyEqualVerdict(verdict)), _)) 505 Pointee(PartiallyEqualVerdict(verdict)), _))
496 .WillOnce(SaveArg<1>(&cb)); 506 .WillOnce(SaveArg<1>(&cb));
497
498 ClientMalwareRequest malware_verdict;
499 malware_verdict.set_url(verdict.url());
500 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
501 .WillOnce(InvokeMalwareCallback(&malware_verdict));
502 EXPECT_CALL(*csd_service_,
503 SendClientReportMalwareRequest(_, _)).Times(0);
504
505 OnPhishingDetectionDone(verdict.SerializeAsString()); 507 OnPhishingDetectionDone(verdict.SerializeAsString());
506 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); 508 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
507 ASSERT_FALSE(cb.is_null()); 509 ASSERT_FALSE(cb.is_null());
508 510
509 // Make sure DisplayBlockingPage is not going to be called. 511 // Make sure DisplayBlockingPage is not going to be called.
510 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)).Times(0); 512 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)).Times(0);
511 cb.Run(GURL(verdict.url()), false); 513 cb.Run(GURL(verdict.url()), false);
512 base::RunLoop().RunUntilIdle(); 514 base::RunLoop().RunUntilIdle();
513 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); 515 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get()));
514 } 516 }
515 517
516 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { 518 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) {
517 // Case 3: client thinks the page is phishing and so does the server. 519 // Case 3: client thinks the page is phishing and so does the server.
518 // We show an interstitial. 520 // We show an interstitial.
519 MockBrowserFeatureExtractor* mock_extractor = 521 MockBrowserFeatureExtractor* mock_extractor =
520 new StrictMock<MockBrowserFeatureExtractor>( 522 new StrictMock<MockBrowserFeatureExtractor>(
521 web_contents(), 523 web_contents(),
522 csd_host_.get()); 524 csd_host_.get());
523 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 525 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
524 526
525 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; 527 ClientSideDetectionService::ClientReportPhishingRequestCallback cb;
526 GURL phishing_url("http://phishingurl.com/"); 528 GURL phishing_url("http://phishingurl.com/");
527 ClientPhishingRequest verdict; 529 ClientPhishingRequest verdict;
528 verdict.set_url(phishing_url.spec()); 530 verdict.set_url(phishing_url.spec());
529 verdict.set_client_score(1.0f); 531 verdict.set_client_score(1.0f);
530 verdict.set_is_phishing(true); 532 verdict.set_is_phishing(true);
531 533
532 ClientMalwareRequest malware_verdict;
533 malware_verdict.set_url(verdict.url());
534
535 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) 534 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _))
536 .WillOnce(DoAll(DeleteArg<1>(), 535 .WillOnce(DoAll(DeleteArg<1>(),
537 InvokeCallbackArgument<2>(true, &verdict))); 536 InvokeCallbackArgument<2>(true, &verdict)));
538 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
539 .WillOnce(InvokeMalwareCallback(&malware_verdict));
540 EXPECT_CALL(*csd_service_, 537 EXPECT_CALL(*csd_service_,
541 SendClientReportPhishingRequest( 538 SendClientReportPhishingRequest(
542 Pointee(PartiallyEqualVerdict(verdict)), _)) 539 Pointee(PartiallyEqualVerdict(verdict)), _))
543 .WillOnce(SaveArg<1>(&cb)); 540 .WillOnce(SaveArg<1>(&cb));
544 OnPhishingDetectionDone(verdict.SerializeAsString()); 541 OnPhishingDetectionDone(verdict.SerializeAsString());
545 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); 542 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
546 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 543 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
547 ASSERT_FALSE(cb.is_null()); 544 ASSERT_FALSE(cb.is_null());
548 545
549 UnsafeResource resource; 546 UnsafeResource resource;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 csd_host_.get()); 579 csd_host_.get());
583 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 580 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
584 581
585 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; 582 ClientSideDetectionService::ClientReportPhishingRequestCallback cb;
586 GURL phishing_url("http://phishingurl.com/"); 583 GURL phishing_url("http://phishingurl.com/");
587 ClientPhishingRequest verdict; 584 ClientPhishingRequest verdict;
588 verdict.set_url(phishing_url.spec()); 585 verdict.set_url(phishing_url.spec());
589 verdict.set_client_score(1.0f); 586 verdict.set_client_score(1.0f);
590 verdict.set_is_phishing(true); 587 verdict.set_is_phishing(true);
591 588
592 ClientMalwareRequest malware_verdict;
593 malware_verdict.set_url(verdict.url());
594
595 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) 589 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _))
596 .WillOnce(DoAll(DeleteArg<1>(), 590 .WillOnce(DoAll(DeleteArg<1>(),
597 InvokeCallbackArgument<2>(true, &verdict))); 591 InvokeCallbackArgument<2>(true, &verdict)));
598 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
599 .WillOnce(InvokeMalwareCallback(&malware_verdict));
600 EXPECT_CALL(*csd_service_, 592 EXPECT_CALL(*csd_service_,
601 SendClientReportPhishingRequest( 593 SendClientReportPhishingRequest(
602 Pointee(PartiallyEqualVerdict(verdict)), _)) 594 Pointee(PartiallyEqualVerdict(verdict)), _))
603 .WillOnce(SaveArg<1>(&cb)); 595 .WillOnce(SaveArg<1>(&cb));
604 OnPhishingDetectionDone(verdict.SerializeAsString()); 596 OnPhishingDetectionDone(verdict.SerializeAsString());
605 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); 597 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
606 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 598 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
607 ASSERT_FALSE(cb.is_null()); 599 ASSERT_FALSE(cb.is_null());
608 600
609 // Set this back to a normal browser feature extractor since we're using 601 // Set this back to a normal browser feature extractor since we're using
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 new StrictMock<MockBrowserFeatureExtractor>( 664 new StrictMock<MockBrowserFeatureExtractor>(
673 web_contents(), 665 web_contents(),
674 csd_host_.get()); 666 csd_host_.get());
675 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 667 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
676 668
677 ClientPhishingRequest verdict; 669 ClientPhishingRequest verdict;
678 verdict.set_url("http://not-phishing.com/"); 670 verdict.set_url("http://not-phishing.com/");
679 verdict.set_client_score(0.1f); 671 verdict.set_client_score(0.1f);
680 verdict.set_is_phishing(false); 672 verdict.set_is_phishing(false);
681 673
682 ClientMalwareRequest malware_verdict;
683 malware_verdict.set_url(verdict.url());
684
685 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0); 674 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0);
686 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
687 .WillOnce(InvokeMalwareCallback(&malware_verdict));
688 OnPhishingDetectionDone(verdict.SerializeAsString()); 675 OnPhishingDetectionDone(verdict.SerializeAsString());
689 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); 676 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor));
690 } 677 }
691 678
692 TEST_F(ClientSideDetectionHostTest, 679 TEST_F(ClientSideDetectionHostTest,
693 OnPhishingDetectionDoneVerdictNotPhishingButSBMatchSubResource) { 680 OnPhishingDetectionDoneVerdictNotPhishingButSBMatchSubResource) {
694 // Case 7: renderer sends a verdict string that isn't phishing but the URL 681 // Case 7: renderer sends a verdict string that isn't phishing but the URL
695 // of a subresource was on the regular phishing or malware lists. 682 // of a subresource was on the regular phishing or malware lists.
696 GURL url("http://not-phishing.com/"); 683 GURL url("http://not-phishing.com/");
697 ClientPhishingRequest verdict; 684 ClientPhishingRequest verdict;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 OnPhishingDetectionDone(verdict.SerializeAsString()); 741 OnPhishingDetectionDone(verdict.SerializeAsString());
755 base::MessageLoop::current()->Run(); 742 base::MessageLoop::current()->Run();
756 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); 743 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
757 744
758 ExpectPreClassificationChecks(start_url, &kFalse, &kFalse, &kFalse, &kFalse, 745 ExpectPreClassificationChecks(start_url, &kFalse, &kFalse, &kFalse, &kFalse,
759 &kFalse, &kFalse); 746 &kFalse, &kFalse);
760 NavigateWithoutSBHitAndCommit(start_url); 747 NavigateWithoutSBHitAndCommit(start_url);
761 WaitAndCheckPreClassificationChecks(); 748 WaitAndCheckPreClassificationChecks();
762 } 749 }
763 750
764 TEST_F(ClientSideDetectionHostTest, UpdateIPUrlMap) {
765 BrowseInfo* browse_info = GetBrowseInfo();
766
767 // Empty IP or host are skipped
768 UpdateIPUrlMap("250.10.10.10", std::string());
769 ASSERT_EQ(0U, browse_info->ips.size());
770 UpdateIPUrlMap(std::string(), "http://google.com/a");
771 ASSERT_EQ(0U, browse_info->ips.size());
772 UpdateIPUrlMap(std::string(), std::string());
773 ASSERT_EQ(0U, browse_info->ips.size());
774
775 std::vector<IPUrlInfo> expected_urls;
776 for (int i = 0; i < 20; i++) {
777 std::string url = base::StringPrintf("http://%d.com/", i);
778 expected_urls.push_back(IPUrlInfo(url, "", "", ResourceType::OBJECT));
779 UpdateIPUrlMap("250.10.10.10", url);
780 }
781 ASSERT_EQ(1U, browse_info->ips.size());
782 ASSERT_EQ(20U, browse_info->ips["250.10.10.10"].size());
783 CheckIPUrlEqual(expected_urls,
784 browse_info->ips["250.10.10.10"]);
785
786 // Add more urls for this ip, it exceeds max limit and won't be added
787 UpdateIPUrlMap("250.10.10.10", "http://21.com/");
788 ASSERT_EQ(1U, browse_info->ips.size());
789 ASSERT_EQ(20U, browse_info->ips["250.10.10.10"].size());
790 CheckIPUrlEqual(expected_urls,
791 browse_info->ips["250.10.10.10"]);
792
793 // Add 199 more IPs
794 for (int i = 0; i < 199; i++) {
795 std::string ip = base::StringPrintf("%d.%d.%d.256", i, i, i);
796 expected_urls.clear();
797 expected_urls.push_back(IPUrlInfo("test.com/", "", "",
798 ResourceType::OBJECT));
799 UpdateIPUrlMap(ip, "test.com/");
800 ASSERT_EQ(1U, browse_info->ips[ip].size());
801 CheckIPUrlEqual(expected_urls,
802 browse_info->ips[ip]);
803 }
804 ASSERT_EQ(200U, browse_info->ips.size());
805
806 // Exceeding max ip limit 200, these won't be added
807 UpdateIPUrlMap("250.250.250.250", "goo.com/");
808 UpdateIPUrlMap("250.250.250.250", "bar.com/");
809 UpdateIPUrlMap("250.250.0.250", "foo.com/");
810 ASSERT_EQ(200U, browse_info->ips.size());
811
812 // Add url to existing IPs succeed
813 UpdateIPUrlMap("100.100.100.256", "more.com/");
814 ASSERT_EQ(2U, browse_info->ips["100.100.100.256"].size());
815 expected_urls.clear();
816 expected_urls.push_back(IPUrlInfo("test.com/", "", "", ResourceType::OBJECT));
817 expected_urls.push_back(IPUrlInfo("more.com/", "", "", ResourceType::OBJECT));
818 CheckIPUrlEqual(expected_urls,
819 browse_info->ips["100.100.100.256"]);
820 }
821
822 TEST_F(ClientSideDetectionHostTest, 751 TEST_F(ClientSideDetectionHostTest,
823 OnPhishingDetectionDoneVerdictNotPhishingNotMalwareIP) { 752 DocumentOnLoadCompletedInMainFrameMalwareIP) {
mattm 2014/03/18 02:19:06 what is this case testing / how is it different th
noé 2014/03/20 17:01:45 Good point. The other test is more complete. Rem
824 // Case 7: renderer sends a verdict string that isn't phishing and not matches 753 // Renderer is done loading the main frame and there is a bad IP match.
825 // malware bad IP list
826 MockBrowserFeatureExtractor* mock_extractor = 754 MockBrowserFeatureExtractor* mock_extractor =
827 new StrictMock<MockBrowserFeatureExtractor>( 755 new StrictMock<MockBrowserFeatureExtractor>(
828 web_contents(), 756 web_contents(),
829 csd_host_.get()); 757 csd_host_.get());
830 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 758 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
831 759
832 ClientPhishingRequest verdict;
833 verdict.set_url("http://not-phishing.com/");
834 verdict.set_client_score(0.1f);
835 verdict.set_is_phishing(false);
836
837 ClientMalwareRequest malware_verdict; 760 ClientMalwareRequest malware_verdict;
838 malware_verdict.set_url(verdict.url()); 761 malware_verdict.set_url("http://not-phishing.com/");
839
840 // That is a special case. If there were no IP matches or if feature
841 // extraction failed the callback will delete the malware_verdict.
842 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
843 .WillOnce(InvokeMalwareCallback(&malware_verdict));
844 EXPECT_CALL(*csd_service_,
845 SendClientReportMalwareRequest(_, _)).Times(0);
846 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0);
847
848 OnPhishingDetectionDone(verdict.SerializeAsString());
849 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor));
850 }
851
852 TEST_F(ClientSideDetectionHostTest,
853 OnPhishingDetectionDoneVerdictNotPhishingButMalwareIP) {
854 // Case 8: renderer sends a verdict string that isn't phishing but matches
855 // malware bad IP list
856 MockBrowserFeatureExtractor* mock_extractor =
857 new StrictMock<MockBrowserFeatureExtractor>(
858 web_contents(),
859 csd_host_.get());
860 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
861
862 ClientPhishingRequest verdict;
863 verdict.set_url("http://not-phishing.com/");
864 verdict.set_client_score(0.1f);
865 verdict.set_is_phishing(false);
866
867 ClientMalwareRequest malware_verdict;
868 malware_verdict.set_url(verdict.url());
869 malware_verdict.set_referrer_url("http://referrer.com/"); 762 malware_verdict.set_referrer_url("http://referrer.com/");
870 ClientMalwareRequest::UrlInfo* badipurl = 763 ClientMalwareRequest::UrlInfo* badipurl =
871 malware_verdict.add_bad_ip_url_info(); 764 malware_verdict.add_bad_ip_url_info();
872 badipurl->set_ip("1.2.3.4"); 765 badipurl->set_ip("1.2.3.4");
873 badipurl->set_url("badip.com"); 766 badipurl->set_url("badip.com");
874 767
875 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _)) 768 SetReferrer(GURL("http://referrer.com/"));
876 .WillOnce(InvokeMalwareCallback(&malware_verdict));
877 EXPECT_CALL(*csd_service_,
878 SendClientReportMalwareRequest(
879 Pointee(PartiallyEqualMalwareVerdict(malware_verdict)), _))
880 .WillOnce(DeleteArg<0>());
881 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0);
882 769
883 SetReferrer(GURL("http://referrer.com/")); 770 ExpectPreClassificationChecks(GURL(malware_verdict.url()), &kFalse, &kFalse,
884 OnPhishingDetectionDone(verdict.SerializeAsString()); 771 &kFalse, &kFalse, &kFalse, &kFalse);
885 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); 772 NavigateAndCommit(GURL(malware_verdict.url()));
886 } 773 WaitAndCheckPreClassificationChecks();
887
888 TEST_F(ClientSideDetectionHostTest,
889 OnPhishingDetectionDoneVerdictPhishingAndMalwareIP) {
890 // Case 9: renderer sends a verdict string that is phishing and matches
891 // malware bad IP list
892 MockBrowserFeatureExtractor* mock_extractor =
893 new StrictMock<MockBrowserFeatureExtractor>(
894 web_contents(),
895 csd_host_.get());
896 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
897
898 ClientSideDetectionService::ClientReportPhishingRequestCallback cb;
899 ClientPhishingRequest verdict;
900 verdict.set_url("http://not-phishing.com/");
901 verdict.set_client_score(0.1f);
902 verdict.set_is_phishing(true);
903
904 ClientMalwareRequest malware_verdict;
905 malware_verdict.set_url(verdict.url());
906 ClientMalwareRequest::UrlInfo* badipurl =
907 malware_verdict.add_bad_ip_url_info();
908 badipurl->set_ip("1.2.3.4");
909 badipurl->set_url("badip.com");
910 774
911 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _)) 775 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
912 .WillOnce(InvokeMalwareCallback(&malware_verdict)); 776 .WillOnce(InvokeMalwareCallback(&malware_verdict));
913 EXPECT_CALL(*csd_service_, 777 EXPECT_CALL(*csd_service_,
914 SendClientReportMalwareRequest( 778 SendClientReportMalwareRequest(
915 Pointee(PartiallyEqualMalwareVerdict(malware_verdict)), _)) 779 Pointee(PartiallyEqualMalwareVerdict(malware_verdict)), _))
916 .WillOnce(DeleteArg<0>()); 780 .WillOnce(DeleteArg<0>());
917 781
918 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) 782 DocumentOnLoadCompletedInMainFrame(GetBrowseInfo()->page_id);
919 .WillOnce(DoAll(DeleteArg<1>(),
920 InvokeCallbackArgument<2>(true, &verdict)));
921
922 EXPECT_CALL(*csd_service_,
923 SendClientReportPhishingRequest(
924 Pointee(PartiallyEqualVerdict(verdict)), _))
925 .WillOnce(SaveArg<1>(&cb));
926
927 // Referrer url using https won't be set and sent out.
928 SetReferrer(GURL("https://referrer.com/"));
929 OnPhishingDetectionDone(verdict.SerializeAsString());
930 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); 783 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor));
931 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
932 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
933 ASSERT_FALSE(cb.is_null());
934 } 784 }
935 785
936 TEST_F(ClientSideDetectionHostTest, 786 TEST_F(ClientSideDetectionHostTest,
937 OnPhishingDetectionDoneShowMalwareInterstitial) { 787 DocumentOnLoadCompletedInMainFrameShowMalwareInterstitial) {
938 // Case 10: client thinks the page match malware IP and so does the server. 788 // Case 9: client thinks the page match malware IP and so does the server.
939 // We show an sub-resource malware interstitial. 789 // We show an sub-resource malware interstitial.
940 MockBrowserFeatureExtractor* mock_extractor = 790 MockBrowserFeatureExtractor* mock_extractor =
941 new StrictMock<MockBrowserFeatureExtractor>( 791 new StrictMock<MockBrowserFeatureExtractor>(
942 web_contents(), 792 web_contents(),
943 csd_host_.get()); 793 csd_host_.get());
944 SetFeatureExtractor(mock_extractor); // The host class takes ownership. 794 SetFeatureExtractor(mock_extractor); // The host class takes ownership.
945 795
946 ClientPhishingRequest verdict;
947 verdict.set_url("http://not-phishing.com/");
948 verdict.set_client_score(0.1f);
949 verdict.set_is_phishing(false);
950
951 ClientSideDetectionService::ClientReportMalwareRequestCallback cb;
952 GURL malware_landing_url("http://malware.com/"); 796 GURL malware_landing_url("http://malware.com/");
953 GURL malware_ip_url("http://badip.com"); 797 GURL malware_ip_url("http://badip.com");
954 ClientMalwareRequest malware_verdict; 798 ClientMalwareRequest malware_verdict;
955 malware_verdict.set_url("http://malware.com/"); 799 malware_verdict.set_url("http://malware.com/");
956 ClientMalwareRequest::UrlInfo* badipurl = 800 ClientMalwareRequest::UrlInfo* badipurl =
957 malware_verdict.add_bad_ip_url_info(); 801 malware_verdict.add_bad_ip_url_info();
958 badipurl->set_ip("1.2.3.4"); 802 badipurl->set_ip("1.2.3.4");
959 badipurl->set_url("http://badip.com"); 803 badipurl->set_url("http://badip.com");
960 804
805 ExpectPreClassificationChecks(GURL(malware_verdict.url()), &kFalse, &kFalse,
806 &kFalse, &kFalse, &kFalse, &kFalse);
807 NavigateAndCommit(GURL(malware_verdict.url()));
808 WaitAndCheckPreClassificationChecks();
809
810 ClientSideDetectionService::ClientReportMalwareRequestCallback cb;
961 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _)) 811 EXPECT_CALL(*mock_extractor, ExtractMalwareFeatures(_, _, _))
962 .WillOnce(InvokeMalwareCallback(&malware_verdict)); 812 .WillOnce(InvokeMalwareCallback(&malware_verdict));
963 EXPECT_CALL(*csd_service_, 813 EXPECT_CALL(*csd_service_,
964 SendClientReportMalwareRequest( 814 SendClientReportMalwareRequest(
965 Pointee(PartiallyEqualMalwareVerdict(malware_verdict)), _)) 815 Pointee(PartiallyEqualMalwareVerdict(malware_verdict)), _))
966 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); 816 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb)));
967 OnPhishingDetectionDone(verdict.SerializeAsString()); 817 DocumentOnLoadCompletedInMainFrame(GetBrowseInfo()->page_id);
968 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); 818 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
969 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 819 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
970 ASSERT_FALSE(cb.is_null()); 820 ASSERT_FALSE(cb.is_null());
971 821
972 UnsafeResource resource; 822 UnsafeResource resource;
973 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)) 823 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_))
974 .WillOnce(SaveArg<0>(&resource)); 824 .WillOnce(SaveArg<0>(&resource));
975 cb.Run(malware_landing_url, malware_ip_url, true); 825 cb.Run(malware_landing_url, malware_ip_url, true);
976 826
977 base::RunLoop().RunUntilIdle(); 827 base::RunLoop().RunUntilIdle();
978 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); 828 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get()));
979 EXPECT_EQ(malware_ip_url, resource.url); 829 EXPECT_EQ(malware_ip_url, resource.url);
980 EXPECT_EQ(malware_landing_url, resource.original_url); 830 EXPECT_EQ(malware_landing_url, resource.original_url);
981 EXPECT_TRUE(resource.is_subresource); 831 EXPECT_TRUE(resource.is_subresource);
982 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL, resource.threat_type); 832 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL, resource.threat_type);
983 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), 833 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(),
984 resource.render_process_host_id); 834 resource.render_process_host_id);
985 EXPECT_EQ(web_contents()->GetRenderViewHost()->GetRoutingID(), 835 EXPECT_EQ(web_contents()->GetRenderViewHost()->GetRoutingID(),
986 resource.render_view_id); 836 resource.render_view_id);
987 837
988 // Make sure the client object will be deleted. 838 // Make sure the client object will be deleted.
989 BrowserThread::PostTask( 839 BrowserThread::PostTask(
990 BrowserThread::IO, 840 BrowserThread::IO,
991 FROM_HERE, 841 FROM_HERE,
992 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, 842 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete,
993 ui_manager_, resource.callback)); 843 ui_manager_, resource.callback));
994 } 844 }
995 845
846 TEST_F(ClientSideDetectionHostTest, UpdateIPUrlMap) {
847 BrowseInfo* browse_info = GetBrowseInfo();
848
849 // Empty IP or host are skipped
850 UpdateIPUrlMap("250.10.10.10", std::string());
851 ASSERT_EQ(0U, browse_info->ips.size());
852 UpdateIPUrlMap(std::string(), "http://google.com/a");
853 ASSERT_EQ(0U, browse_info->ips.size());
854 UpdateIPUrlMap(std::string(), std::string());
855 ASSERT_EQ(0U, browse_info->ips.size());
856
857 std::vector<IPUrlInfo> expected_urls;
858 for (int i = 0; i < 20; i++) {
859 std::string url = base::StringPrintf("http://%d.com/", i);
860 expected_urls.push_back(IPUrlInfo(url, "", "", ResourceType::OBJECT));
861 UpdateIPUrlMap("250.10.10.10", url);
862 }
863 ASSERT_EQ(1U, browse_info->ips.size());
864 ASSERT_EQ(20U, browse_info->ips["250.10.10.10"].size());
865 CheckIPUrlEqual(expected_urls,
866 browse_info->ips["250.10.10.10"]);
867
868 // Add more urls for this ip, it exceeds max limit and won't be added
869 UpdateIPUrlMap("250.10.10.10", "http://21.com/");
870 ASSERT_EQ(1U, browse_info->ips.size());
871 ASSERT_EQ(20U, browse_info->ips["250.10.10.10"].size());
872 CheckIPUrlEqual(expected_urls,
873 browse_info->ips["250.10.10.10"]);
874
875 // Add 199 more IPs
876 for (int i = 0; i < 199; i++) {
877 std::string ip = base::StringPrintf("%d.%d.%d.256", i, i, i);
878 expected_urls.clear();
879 expected_urls.push_back(IPUrlInfo("test.com/", "", "",
880 ResourceType::OBJECT));
881 UpdateIPUrlMap(ip, "test.com/");
882 ASSERT_EQ(1U, browse_info->ips[ip].size());
883 CheckIPUrlEqual(expected_urls,
884 browse_info->ips[ip]);
885 }
886 ASSERT_EQ(200U, browse_info->ips.size());
887
888 // Exceeding max ip limit 200, these won't be added
889 UpdateIPUrlMap("250.250.250.250", "goo.com/");
890 UpdateIPUrlMap("250.250.250.250", "bar.com/");
891 UpdateIPUrlMap("250.250.0.250", "foo.com/");
892 ASSERT_EQ(200U, browse_info->ips.size());
893
894 // Add url to existing IPs succeed
895 UpdateIPUrlMap("100.100.100.256", "more.com/");
896 ASSERT_EQ(2U, browse_info->ips["100.100.100.256"].size());
897 expected_urls.clear();
898 expected_urls.push_back(IPUrlInfo("test.com/", "", "", ResourceType::OBJECT));
899 expected_urls.push_back(IPUrlInfo("more.com/", "", "", ResourceType::OBJECT));
900 CheckIPUrlEqual(expected_urls,
901 browse_info->ips["100.100.100.256"]);
902 }
903
996 TEST_F(ClientSideDetectionHostTest, NavigationCancelsShouldClassifyUrl) { 904 TEST_F(ClientSideDetectionHostTest, NavigationCancelsShouldClassifyUrl) {
997 // Test that canceling pending should classify requests works as expected. 905 // Test that canceling pending should classify requests works as expected.
998 906
999 GURL first_url("http://first.phishy.url.com"); 907 GURL first_url("http://first.phishy.url.com");
1000 GURL second_url("http://second.url.com/"); 908 GURL second_url("http://second.url.com/");
1001 // The first few checks are done synchronously so check that they have been 909 // The first few checks are done synchronously so check that they have been
1002 // done for the first URL, while the second URL has all the checks done. We 910 // done for the first URL, while the second URL has all the checks done. We
1003 // need to manually set up the IsPrivateIPAddress mock since if the same mock 911 // need to manually set up the IsPrivateIPAddress mock since if the same mock
1004 // expectation is specified twice, gmock will only use the last instance of 912 // expectation is specified twice, gmock will only use the last instance of
1005 // it, meaning the first will never be matched. 913 // it, meaning the first will never be matched.
(...skipping 21 matching lines...) Expand all
1027 WaitAndCheckPreClassificationChecks(); 935 WaitAndCheckPreClassificationChecks();
1028 936
1029 const IPC::Message* msg = process()->sink().GetFirstMessageMatching( 937 const IPC::Message* msg = process()->sink().GetFirstMessageMatching(
1030 SafeBrowsingMsg_StartPhishingDetection::ID); 938 SafeBrowsingMsg_StartPhishingDetection::ID);
1031 ASSERT_TRUE(msg); 939 ASSERT_TRUE(msg);
1032 Tuple1<GURL> actual_url; 940 Tuple1<GURL> actual_url;
1033 SafeBrowsingMsg_StartPhishingDetection::Read(msg, &actual_url); 941 SafeBrowsingMsg_StartPhishingDetection::Read(msg, &actual_url);
1034 EXPECT_EQ(url, actual_url.a); 942 EXPECT_EQ(url, actual_url.a);
1035 EXPECT_EQ(rvh()->GetRoutingID(), msg->routing_id()); 943 EXPECT_EQ(rvh()->GetRoutingID(), msg->routing_id());
1036 process()->sink().ClearMessages(); 944 process()->sink().ClearMessages();
945 ExpectShouldClassifyForMalwareResult(&kTrue);
1037 946
1038 // Now try an in-page navigation. This should not trigger an IPC. 947 // Now try an in-page navigation. This should not trigger an IPC.
1039 EXPECT_CALL(*csd_service_, IsPrivateIPAddress(_)).Times(0); 948 EXPECT_CALL(*csd_service_, IsPrivateIPAddress(_)).Times(0);
1040 url = GURL("http://host.com/#foo"); 949 url = GURL("http://host.com/#foo");
1041 ExpectPreClassificationChecks(url, NULL, NULL, NULL, NULL, NULL, NULL); 950 ExpectPreClassificationChecks(url, NULL, NULL, NULL, NULL, NULL, NULL);
1042 NavigateAndCommit(url); 951 NavigateAndCommit(url);
1043 WaitAndCheckPreClassificationChecks(); 952 WaitAndCheckPreClassificationChecks();
1044 953
1045 msg = process()->sink().GetFirstMessageMatching( 954 msg = process()->sink().GetFirstMessageMatching(
1046 SafeBrowsingMsg_StartPhishingDetection::ID); 955 SafeBrowsingMsg_StartPhishingDetection::ID);
1047 ASSERT_FALSE(msg); 956 ASSERT_FALSE(msg);
1048 957
1049 // Check that XHTML is supported, in addition to the default HTML type. 958 // Check that XHTML is supported, in addition to the default HTML type.
1050 // Note: for this test to work correctly, the new URL must be on the 959 // Note: for this test to work correctly, the new URL must be on the
1051 // same domain as the previous URL, otherwise it will create a new 960 // same domain as the previous URL, otherwise it will create a new
1052 // RenderViewHost that won't have the mime type set. 961 // RenderViewHost that won't have the mime type set.
1053 url = GURL("http://host.com/xhtml"); 962 url = GURL("http://host.com/xhtml");
1054 rvh_tester()->SetContentsMimeType("application/xhtml+xml"); 963 rvh_tester()->SetContentsMimeType("application/xhtml+xml");
1055 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse, 964 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse,
1056 &kFalse, &kFalse); 965 &kFalse, &kFalse);
1057 NavigateAndCommit(url); 966 NavigateAndCommit(url);
1058 WaitAndCheckPreClassificationChecks(); 967 WaitAndCheckPreClassificationChecks();
1059 msg = process()->sink().GetFirstMessageMatching( 968 msg = process()->sink().GetFirstMessageMatching(
1060 SafeBrowsingMsg_StartPhishingDetection::ID); 969 SafeBrowsingMsg_StartPhishingDetection::ID);
1061 ASSERT_TRUE(msg); 970 ASSERT_TRUE(msg);
1062 SafeBrowsingMsg_StartPhishingDetection::Read(msg, &actual_url); 971 SafeBrowsingMsg_StartPhishingDetection::Read(msg, &actual_url);
1063 EXPECT_EQ(url, actual_url.a); 972 EXPECT_EQ(url, actual_url.a);
1064 EXPECT_EQ(rvh()->GetRoutingID(), msg->routing_id()); 973 EXPECT_EQ(rvh()->GetRoutingID(), msg->routing_id());
1065 process()->sink().ClearMessages(); 974 process()->sink().ClearMessages();
975 ExpectShouldClassifyForMalwareResult(&kTrue);
1066 976
1067 // Navigate to a new host, which should cause another IPC. 977 // Navigate to a new host, which should cause another IPC.
1068 url = GURL("http://host2.com/"); 978 url = GURL("http://host2.com/");
1069 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse, 979 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse,
1070 &kFalse, &kFalse); 980 &kFalse, &kFalse);
1071 NavigateAndCommit(url); 981 NavigateAndCommit(url);
1072 WaitAndCheckPreClassificationChecks(); 982 WaitAndCheckPreClassificationChecks();
1073 msg = process()->sink().GetFirstMessageMatching( 983 msg = process()->sink().GetFirstMessageMatching(
1074 SafeBrowsingMsg_StartPhishingDetection::ID); 984 SafeBrowsingMsg_StartPhishingDetection::ID);
1075 ASSERT_TRUE(msg); 985 ASSERT_TRUE(msg);
1076 SafeBrowsingMsg_StartPhishingDetection::Read(msg, &actual_url); 986 SafeBrowsingMsg_StartPhishingDetection::Read(msg, &actual_url);
1077 EXPECT_EQ(url, actual_url.a); 987 EXPECT_EQ(url, actual_url.a);
1078 EXPECT_EQ(rvh()->GetRoutingID(), msg->routing_id()); 988 EXPECT_EQ(rvh()->GetRoutingID(), msg->routing_id());
1079 process()->sink().ClearMessages(); 989 process()->sink().ClearMessages();
990 ExpectShouldClassifyForMalwareResult(&kTrue);
1080 991
1081 // If the mime type is not one that we support, no IPC should be triggered. 992 // If the mime type is not one that we support, no IPC should be triggered
993 // but all pre-classification checks should run because we might classify
994 // other mime types for malware.
1082 // Note: for this test to work correctly, the new URL must be on the 995 // Note: for this test to work correctly, the new URL must be on the
1083 // same domain as the previous URL, otherwise it will create a new 996 // same domain as the previous URL, otherwise it will create a new
1084 // RenderViewHost that won't have the mime type set. 997 // RenderViewHost that won't have the mime type set.
1085 url = GURL("http://host2.com/image.jpg"); 998 url = GURL("http://host2.com/image.jpg");
1086 rvh_tester()->SetContentsMimeType("image/jpeg"); 999 rvh_tester()->SetContentsMimeType("image/jpeg");
1087 ExpectPreClassificationChecks(url, NULL, NULL, NULL, NULL, NULL, NULL); 1000 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse,
1001 &kFalse,&kFalse);
1088 NavigateAndCommit(url); 1002 NavigateAndCommit(url);
1089 WaitAndCheckPreClassificationChecks(); 1003 WaitAndCheckPreClassificationChecks();
1090 msg = process()->sink().GetFirstMessageMatching( 1004 msg = process()->sink().GetFirstMessageMatching(
1091 SafeBrowsingMsg_StartPhishingDetection::ID); 1005 SafeBrowsingMsg_StartPhishingDetection::ID);
1092 ASSERT_FALSE(msg); 1006 ASSERT_FALSE(msg);
1007 ExpectShouldClassifyForMalwareResult(&kTrue);
1093 1008
1094 // If IsPrivateIPAddress returns true, no IPC should be triggered. 1009 // If IsPrivateIPAddress returns true, no IPC should be triggered.
1095 url = GURL("http://host3.com/"); 1010 url = GURL("http://host3.com/");
1096 ExpectPreClassificationChecks(url, &kTrue, NULL, NULL, NULL, NULL, NULL); 1011 ExpectPreClassificationChecks(url, &kTrue, &kFalse, NULL, NULL, NULL, NULL);
1097 NavigateAndCommit(url); 1012 NavigateAndCommit(url);
1098 WaitAndCheckPreClassificationChecks(); 1013 WaitAndCheckPreClassificationChecks();
1099 msg = process()->sink().GetFirstMessageMatching( 1014 msg = process()->sink().GetFirstMessageMatching(
1100 SafeBrowsingMsg_StartPhishingDetection::ID); 1015 SafeBrowsingMsg_StartPhishingDetection::ID);
1101 ASSERT_FALSE(msg); 1016 ASSERT_FALSE(msg);
1017 ExpectShouldClassifyForMalwareResult(&kFalse);
1102 1018
1103 // If the tab is incognito there should be no IPC. Also, we shouldn't 1019 // If the tab is incognito there should be no IPC. Also, we shouldn't
1104 // even check the csd-whitelist. 1020 // even check the csd-whitelist.
1105 url = GURL("http://host4.com/"); 1021 url = GURL("http://host4.com/");
1106 ExpectPreClassificationChecks(url, &kFalse, &kTrue, NULL, NULL, NULL, NULL); 1022 ExpectPreClassificationChecks(url, &kFalse, &kTrue, NULL, NULL, NULL, NULL);
1107 NavigateAndCommit(url); 1023 NavigateAndCommit(url);
1108 WaitAndCheckPreClassificationChecks(); 1024 WaitAndCheckPreClassificationChecks();
1109 msg = process()->sink().GetFirstMessageMatching( 1025 msg = process()->sink().GetFirstMessageMatching(
1110 SafeBrowsingMsg_StartPhishingDetection::ID); 1026 SafeBrowsingMsg_StartPhishingDetection::ID);
1111 ASSERT_FALSE(msg); 1027 ASSERT_FALSE(msg);
1028 ExpectShouldClassifyForMalwareResult(&kFalse);
1112 1029
1113 // If the URL is on the csd whitelist, no IPC should be triggered. 1030 // If the URL is on the csd whitelist, no IPC should be triggered.
1114 url = GURL("http://host5.com/"); 1031 url = GURL("http://host5.com/");
1115 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kTrue, NULL, NULL, 1032 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kTrue, NULL, NULL,
1116 NULL); 1033 NULL);
1117 NavigateAndCommit(url); 1034 NavigateAndCommit(url);
1118 WaitAndCheckPreClassificationChecks(); 1035 WaitAndCheckPreClassificationChecks();
1119 msg = process()->sink().GetFirstMessageMatching( 1036 msg = process()->sink().GetFirstMessageMatching(
1120 SafeBrowsingMsg_StartPhishingDetection::ID); 1037 SafeBrowsingMsg_StartPhishingDetection::ID);
1121 ASSERT_FALSE(msg); 1038 ASSERT_FALSE(msg);
1039 ExpectShouldClassifyForMalwareResult(&kFalse);
1122 1040
1123 // If item is in the cache but it isn't valid, we will classify regardless 1041 // If item is in the cache but it isn't valid, we will classify regardless
1124 // of whether we are over the reporting limit. 1042 // of whether we are over the reporting limit.
1125 url = GURL("http://host6.com/"); 1043 url = GURL("http://host6.com/");
1126 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse, &kTrue, 1044 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse, &kTrue,
1127 NULL); 1045 NULL);
1128 NavigateAndCommit(url); 1046 NavigateAndCommit(url);
1129 WaitAndCheckPreClassificationChecks(); 1047 WaitAndCheckPreClassificationChecks();
1130 msg = process()->sink().GetFirstMessageMatching( 1048 msg = process()->sink().GetFirstMessageMatching(
1131 SafeBrowsingMsg_StartPhishingDetection::ID); 1049 SafeBrowsingMsg_StartPhishingDetection::ID);
1132 ASSERT_TRUE(msg); 1050 ASSERT_TRUE(msg);
1133 SafeBrowsingMsg_StartPhishingDetection::Read(msg, &actual_url); 1051 SafeBrowsingMsg_StartPhishingDetection::Read(msg, &actual_url);
1134 EXPECT_EQ(url, actual_url.a); 1052 EXPECT_EQ(url, actual_url.a);
1135 EXPECT_EQ(rvh()->GetRoutingID(), msg->routing_id()); 1053 EXPECT_EQ(rvh()->GetRoutingID(), msg->routing_id());
1136 process()->sink().ClearMessages(); 1054 process()->sink().ClearMessages();
1055 ExpectShouldClassifyForMalwareResult(&kTrue);
1137 1056
1138 // If the url isn't in the cache and we are over the reporting limit, we 1057 // If the url isn't in the cache and we are over the reporting limit, we
1139 // don't do classification. 1058 // don't do classification.
1140 url = GURL("http://host7.com/"); 1059 url = GURL("http://host7.com/");
1141 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse, 1060 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse,
1142 &kFalse, &kTrue); 1061 &kFalse, &kTrue);
1143 NavigateAndCommit(url); 1062 NavigateAndCommit(url);
1144 WaitAndCheckPreClassificationChecks(); 1063 WaitAndCheckPreClassificationChecks();
1145 msg = process()->sink().GetFirstMessageMatching( 1064 msg = process()->sink().GetFirstMessageMatching(
1146 SafeBrowsingMsg_StartPhishingDetection::ID); 1065 SafeBrowsingMsg_StartPhishingDetection::ID);
1147 ASSERT_FALSE(msg); 1066 ASSERT_FALSE(msg);
1067 ExpectShouldClassifyForMalwareResult(&kTrue);
1148 1068
1149 // If result is cached, we will try and display the blocking page directly 1069 // If result is cached, we will try and display the blocking page directly
1150 // with no start classification message. 1070 // with no start classification message.
1151 url = GURL("http://host8.com/"); 1071 url = GURL("http://host8.com/");
1152 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kTrue, NULL, 1072 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kTrue, &kFalse,
1153 NULL); 1073 &kFalse);
1154 1074
1155 UnsafeResource resource; 1075 UnsafeResource resource;
1156 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_)) 1076 EXPECT_CALL(*ui_manager_.get(), DisplayBlockingPage(_))
1157 .WillOnce(SaveArg<0>(&resource)); 1077 .WillOnce(SaveArg<0>(&resource));
1158 1078
1159 NavigateAndCommit(url); 1079 NavigateAndCommit(url);
1160 // Wait for CheckCsdWhitelist and CheckCache() to be called. 1080 // Wait for CheckCsdWhitelist and CheckCache() to be called.
1161 base::RunLoop().RunUntilIdle(); 1081 base::RunLoop().RunUntilIdle();
1162 // Now we check that all expected functions were indeed called on the two 1082 // Now we check that all expected functions were indeed called on the two
1163 // service objects. 1083 // service objects.
1164 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get())); 1084 EXPECT_TRUE(Mock::VerifyAndClear(csd_host_.get()));
1165 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); 1085 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get()));
1166 EXPECT_EQ(url, resource.url); 1086 EXPECT_EQ(url, resource.url);
1167 EXPECT_EQ(url, resource.original_url); 1087 EXPECT_EQ(url, resource.original_url);
1168 resource.callback.Reset(); 1088 resource.callback.Reset();
1169 msg = process()->sink().GetFirstMessageMatching( 1089 msg = process()->sink().GetFirstMessageMatching(
1170 SafeBrowsingMsg_StartPhishingDetection::ID); 1090 SafeBrowsingMsg_StartPhishingDetection::ID);
1171 ASSERT_FALSE(msg); 1091 ASSERT_FALSE(msg);
1172 } 1092 }
1173 } // namespace safe_browsing 1093 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698