OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <map> | 5 #include <map> |
6 #include <queue> | 6 #include <queue> |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 | 570 |
571 mask = std::string(16, '\xFF'); | 571 mask = std::string(16, '\xFF'); |
572 EXPECT_TRUE(bad_subnets.count(mask)); | 572 EXPECT_TRUE(bad_subnets.count(mask)); |
573 EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::kSHA256Length, '.'))); | 573 EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::kSHA256Length, '.'))); |
574 | 574 |
575 mask = std::string(12, '\xFF') + "\xF0" + std::string(3, '\x00'); | 575 mask = std::string(12, '\xFF') + "\xF0" + std::string(3, '\x00'); |
576 EXPECT_TRUE(bad_subnets.count(mask)); | 576 EXPECT_TRUE(bad_subnets.count(mask)); |
577 EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::kSHA256Length, '.'))); | 577 EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::kSHA256Length, '.'))); |
578 } | 578 } |
579 | 579 |
580 TEST_F(ClientSideDetectionServiceTest, IsBadIpAddress) { | |
581 ClientSideModel model; | |
582 // IPv6 exact match for: 2620:0:1000:3103:21a:a0ff:fe10:786e. | |
583 ClientSideModel::IPSubnet* subnet = model.add_bad_subnet(); | |
584 subnet->set_prefix(crypto::SHA256HashString(std::string( | |
585 "\x26\x20\x00\x00\x10\x00\x31\x03\x02\x1a\xa0\xff\xfe\x10\x78\x6e", 16))); | |
586 subnet->set_size(128); | |
587 | |
588 // IPv6 prefix match for: fe80::21a:a0ff:fe10:786e/64. | |
589 subnet = model.add_bad_subnet(); | |
590 subnet->set_prefix(crypto::SHA256HashString(std::string( | |
591 "\xfe\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16))); | |
592 subnet->set_size(64); | |
593 | |
594 // IPv4 exact match for ::ffff:192.0.2.128. | |
595 subnet = model.add_bad_subnet(); | |
596 subnet->set_prefix(crypto::SHA256HashString(std::string( | |
597 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xc0\x00\x02\x80", 16))); | |
598 subnet->set_size(128); | |
599 | |
600 // IPv4 prefix match (/8) for ::ffff:192.1.1.0. | |
601 subnet = model.add_bad_subnet(); | |
602 subnet->set_prefix(crypto::SHA256HashString(std::string( | |
603 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xc0\x01\x01\x00", 16))); | |
604 subnet->set_size(120); | |
605 | |
606 // IPv4 prefix match (/9) for ::ffff:192.1.122.0. | |
607 subnet = model.add_bad_subnet(); | |
608 subnet->set_prefix(crypto::SHA256HashString(std::string( | |
609 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xc0\x01\x7a\x00", 16))); | |
610 subnet->set_size(119); | |
611 | |
612 // IPv4 prefix match (/15) for ::ffff:192.1.128.0. | |
613 subnet = model.add_bad_subnet(); | |
614 subnet->set_prefix(crypto::SHA256HashString(std::string( | |
615 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xc0\x01\x80\x00", 16))); | |
616 subnet->set_size(113); | |
617 | |
618 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | |
619 ClientSideDetectionService::SetBadSubnets( | |
620 model, &(csd_service_->bad_subnets_)); | |
621 EXPECT_FALSE(csd_service_->IsBadIpAddress("blabla")); | |
622 EXPECT_FALSE(csd_service_->IsBadIpAddress(std::string())); | |
623 | |
624 EXPECT_TRUE(csd_service_->IsBadIpAddress( | |
625 "2620:0:1000:3103:21a:a0ff:fe10:786e")); | |
626 EXPECT_FALSE(csd_service_->IsBadIpAddress( | |
627 "2620:0:1000:3103:21a:a0ff:fe10:786f")); | |
628 | |
629 EXPECT_TRUE(csd_service_->IsBadIpAddress("fe80::21a:a0ff:fe10:786e")); | |
630 EXPECT_TRUE(csd_service_->IsBadIpAddress("fe80::31a:a0ff:fe10:786e")); | |
631 EXPECT_TRUE(csd_service_->IsBadIpAddress("fe80::21a:a0ff:fe10:786f")); | |
632 EXPECT_FALSE(csd_service_->IsBadIpAddress("fe81::21a:a0ff:fe10:786e")); | |
633 | |
634 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.0.2.128")); | |
635 EXPECT_TRUE(csd_service_->IsBadIpAddress("::ffff:192.0.2.128")); | |
636 EXPECT_FALSE(csd_service_->IsBadIpAddress("192.0.2.129")); | |
637 | |
638 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.1.0")); | |
639 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.1.255")); | |
640 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.1.10")); | |
641 EXPECT_TRUE(csd_service_->IsBadIpAddress("::ffff:192.1.1.2")); | |
642 | |
643 EXPECT_FALSE(csd_service_->IsBadIpAddress("192.1.121.255")); | |
644 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.122.0")); | |
645 EXPECT_TRUE(csd_service_->IsBadIpAddress("::ffff:192.1.122.1")); | |
646 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.122.255")); | |
647 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.123.0")); | |
648 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.123.255")); | |
649 EXPECT_FALSE(csd_service_->IsBadIpAddress("192.1.124.0")); | |
650 | |
651 EXPECT_FALSE(csd_service_->IsBadIpAddress("192.1.127.255")); | |
652 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.128.0")); | |
653 EXPECT_TRUE(csd_service_->IsBadIpAddress("::ffff:192.1.128.1")); | |
654 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.128.255")); | |
655 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.255.0")); | |
656 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.255.255")); | |
657 EXPECT_FALSE(csd_service_->IsBadIpAddress("192.2.0.0")); | |
658 } | |
659 | |
660 TEST_F(ClientSideDetectionServiceTest, ModelHasValidHashIds) { | 580 TEST_F(ClientSideDetectionServiceTest, ModelHasValidHashIds) { |
661 ClientSideModel model; | 581 ClientSideModel model; |
662 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); | 582 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); |
663 model.add_hashes("bla"); | 583 model.add_hashes("bla"); |
664 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); | 584 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); |
665 model.add_page_term(0); | 585 model.add_page_term(0); |
666 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); | 586 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); |
667 | 587 |
668 model.add_page_term(-1); | 588 model.add_page_term(-1); |
669 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); | 589 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 EXPECT_CALL(*service, ScheduleFetchModel(_)) | 674 EXPECT_CALL(*service, ScheduleFetchModel(_)) |
755 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); | 675 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); |
756 EXPECT_CALL(*service, EndFetchModel( | 676 EXPECT_CALL(*service, EndFetchModel( |
757 ClientSideDetectionService::MODEL_NOT_CHANGED)) | 677 ClientSideDetectionService::MODEL_NOT_CHANGED)) |
758 .WillOnce(Invoke(service, &MockClientSideDetectionService::Disable)); | 678 .WillOnce(Invoke(service, &MockClientSideDetectionService::Disable)); |
759 csd_service_->SetEnabledAndRefreshState(true); | 679 csd_service_->SetEnabledAndRefreshState(true); |
760 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); | 680 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); |
761 Mock::VerifyAndClearExpectations(service); | 681 Mock::VerifyAndClearExpectations(service); |
762 } | 682 } |
763 } // namespace safe_browsing | 683 } // namespace safe_browsing |
OLD | NEW |