OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <map> | 5 #include <map> |
6 #include <queue> | 6 #include <queue> |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/stringprintf.h" | |
14 #include "base/task.h" | 13 #include "base/task.h" |
15 #include "base/time.h" | 14 #include "base/time.h" |
16 #include "chrome/browser/safe_browsing/browser_features.h" | |
17 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 15 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
18 #include "chrome/common/safe_browsing/client_model.pb.h" | 16 #include "chrome/common/safe_browsing/client_model.pb.h" |
19 #include "chrome/common/safe_browsing/csd.pb.h" | 17 #include "chrome/common/safe_browsing/csd.pb.h" |
20 #include "chrome/renderer/safe_browsing/features.h" | |
21 #include "chrome/test/base/testing_browser_process_test.h" | 18 #include "chrome/test/base/testing_browser_process_test.h" |
22 #include "content/browser/browser_thread.h" | 19 #include "content/browser/browser_thread.h" |
23 #include "content/common/url_fetcher.h" | 20 #include "content/common/url_fetcher.h" |
24 #include "content/test/test_url_fetcher_factory.h" | 21 #include "content/test/test_url_fetcher_factory.h" |
25 #include "crypto/sha2.h" | 22 #include "crypto/sha2.h" |
26 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
27 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
30 | 27 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 EXPECT_TRUE(csd_service_->GetValidCachedResult( | 156 EXPECT_TRUE(csd_service_->GetValidCachedResult( |
160 GURL("http://first.url.com"), &is_phishing)); | 157 GURL("http://first.url.com"), &is_phishing)); |
161 EXPECT_FALSE(is_phishing); | 158 EXPECT_FALSE(is_phishing); |
162 EXPECT_FALSE(csd_service_->GetValidCachedResult( | 159 EXPECT_FALSE(csd_service_->GetValidCachedResult( |
163 GURL("http://third.url.com"), &is_phishing)); | 160 GURL("http://third.url.com"), &is_phishing)); |
164 EXPECT_TRUE(csd_service_->GetValidCachedResult( | 161 EXPECT_TRUE(csd_service_->GetValidCachedResult( |
165 GURL("http://fourth.url.com"), &is_phishing)); | 162 GURL("http://fourth.url.com"), &is_phishing)); |
166 EXPECT_TRUE(is_phishing); | 163 EXPECT_TRUE(is_phishing); |
167 } | 164 } |
168 | 165 |
169 void AddFeature(const std::string& name, double value, | |
170 ClientPhishingRequest* request) { | |
171 ClientPhishingRequest_Feature* feature = request->add_feature_map(); | |
172 feature->set_name(name); | |
173 feature->set_value(value); | |
174 } | |
175 | |
176 void AddNonModelFeature(const std::string& name, double value, | |
177 ClientPhishingRequest* request) { | |
178 ClientPhishingRequest_Feature* feature = | |
179 request->add_non_model_feature_map(); | |
180 feature->set_name(name); | |
181 feature->set_value(value); | |
182 } | |
183 | |
184 protected: | 166 protected: |
185 scoped_ptr<ClientSideDetectionService> csd_service_; | 167 scoped_ptr<ClientSideDetectionService> csd_service_; |
186 scoped_ptr<FakeURLFetcherFactory> factory_; | 168 scoped_ptr<FakeURLFetcherFactory> factory_; |
187 MessageLoop msg_loop_; | 169 MessageLoop msg_loop_; |
188 | 170 |
189 private: | 171 private: |
190 void SendRequestDone(GURL phishing_url, bool is_phishing) { | 172 void SendRequestDone(GURL phishing_url, bool is_phishing) { |
191 ASSERT_EQ(phishing_url, phishing_url_); | 173 ASSERT_EQ(phishing_url, phishing_url_); |
192 is_phishing_ = is_phishing; | 174 is_phishing_ = is_phishing; |
193 msg_loop_.Quit(); | 175 msg_loop_.Quit(); |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 EXPECT_CALL(*service, ScheduleFetchModel(_)) | 639 EXPECT_CALL(*service, ScheduleFetchModel(_)) |
658 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); | 640 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); |
659 EXPECT_CALL(*service, EndFetchModel( | 641 EXPECT_CALL(*service, EndFetchModel( |
660 ClientSideDetectionService::MODEL_NOT_CHANGED)) | 642 ClientSideDetectionService::MODEL_NOT_CHANGED)) |
661 .WillOnce(Invoke(service, &MockClientSideDetectionService::Disable)); | 643 .WillOnce(Invoke(service, &MockClientSideDetectionService::Disable)); |
662 csd_service_->SetEnabled(true); | 644 csd_service_->SetEnabled(true); |
663 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); | 645 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); |
664 Mock::VerifyAndClearExpectations(service); | 646 Mock::VerifyAndClearExpectations(service); |
665 } | 647 } |
666 | 648 |
667 TEST_F(ClientSideDetectionServiceTest, SanitizeRequestForPingback) { | |
668 ClientPhishingRequest request; | |
669 request.set_url("http://www.us.host.com/blah"); | |
670 request.set_suffix_prefix_hash("hash"); | |
671 request.set_client_score(0.8f); | |
672 request.set_is_phishing(true); | |
673 AddFeature(std::string(features::kUrlTldToken) + "com", 1.0, &request); | |
674 AddFeature(std::string(features::kUrlDomainToken) + "host", 1.0, &request); | |
675 AddFeature(std::string(features::kUrlOtherHostToken) + "us", 1.0, &request); | |
676 AddFeature(std::string(features::kUrlOtherHostToken) + "www", 1.0, &request); | |
677 AddFeature(features::kUrlNumOtherHostTokensGTOne, 1.0, &request); | |
678 AddFeature(std::string(features::kUrlPathToken) + "blah", 1.0, &request); | |
679 AddFeature(features::kPageHasForms, 1.0, &request); | |
680 AddFeature(std::string(features::kPageTerm) + "term", 1.0, &request); | |
681 AddFeature(features::kPageImgOtherDomainFreq, 0.5, &request); | |
682 request.set_model_version(3); | |
683 AddNonModelFeature(features::kUrlHistoryVisitCount, 5.0, &request); | |
684 AddNonModelFeature(StringPrintf("%s=http://referrer.com/", | |
685 features::kReferrer), | |
686 1.0, &request); | |
687 AddNonModelFeature(StringPrintf("%s%s=http://redirreferrer.com/", | |
688 features::kRedirectPrefix, | |
689 features::kReferrer), | |
690 1.0, &request); | |
691 AddNonModelFeature(StringPrintf("%s%s=http://hostreferrer.com/", | |
692 features::kHostPrefix, features::kReferrer), | |
693 1.0, &request); | |
694 AddNonModelFeature(StringPrintf("%s%s%s=http://hostredirreferrer.com/", | |
695 features::kHostPrefix, | |
696 features::kRedirectPrefix, | |
697 features::kReferrer), | |
698 1.0, &request); | |
699 AddNonModelFeature(std::string(features::kBadIpFetch) + "1.2.3.4", | |
700 1.0, &request); | |
701 AddNonModelFeature(std::string(features::kSafeBrowsingMaliciousUrl) + | |
702 "http://malicious.com/", 1.0, &request); | |
703 AddNonModelFeature(std::string(features::kSafeBrowsingOriginalUrl) + | |
704 "http://original.com/", 1.0, &request); | |
705 | |
706 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | |
707 | |
708 ClientPhishingRequest sanitized_request; | |
709 csd_service_->SanitizeRequestForPingback(request, &sanitized_request); | |
710 | |
711 // For easier debugging, we'll check the output protobuf fields individually. | |
712 ClientPhishingRequest expected; | |
713 expected.set_suffix_prefix_hash(request.suffix_prefix_hash()); | |
714 expected.set_client_score(request.client_score()); | |
715 expected.set_is_phishing(request.is_phishing()); | |
716 AddFeature(features::kUrlNumOtherHostTokensGTOne, 1.0, &expected); | |
717 AddFeature(features::kPageHasForms, 1.0, &expected); | |
718 AddFeature(features::kPageImgOtherDomainFreq, 0.5, &expected); | |
719 expected.set_model_version(3); | |
720 AddNonModelFeature(features::kUrlHistoryVisitCount, 5.0, &expected); | |
721 | |
722 EXPECT_FALSE(sanitized_request.has_url()); | |
723 EXPECT_EQ(expected.suffix_prefix_hash(), | |
724 sanitized_request.suffix_prefix_hash()); | |
725 EXPECT_FLOAT_EQ(expected.client_score(), sanitized_request.client_score()); | |
726 EXPECT_EQ(expected.is_phishing(), sanitized_request.is_phishing()); | |
727 | |
728 ASSERT_EQ(expected.feature_map_size(), sanitized_request.feature_map_size()); | |
729 for (int i = 0; i < expected.feature_map_size(); ++i) { | |
730 EXPECT_EQ(expected.feature_map(i).name(), | |
731 sanitized_request.feature_map(i).name()) << "Feature " << i; | |
732 EXPECT_DOUBLE_EQ(expected.feature_map(i).value(), | |
733 sanitized_request.feature_map(i).value()) | |
734 << "Feature " << i; | |
735 } | |
736 EXPECT_EQ(expected.model_version(), sanitized_request.model_version()); | |
737 ASSERT_EQ(expected.non_model_feature_map_size(), | |
738 sanitized_request.non_model_feature_map_size()); | |
739 for (int i = 0; i < expected.non_model_feature_map_size(); ++i) { | |
740 EXPECT_EQ(expected.non_model_feature_map(i).name(), | |
741 sanitized_request.non_model_feature_map(i).name()) | |
742 << "Non-model feature " << i; | |
743 EXPECT_DOUBLE_EQ(expected.non_model_feature_map(i).value(), | |
744 sanitized_request.non_model_feature_map(i).value()) | |
745 << "Non-model feature " << i; | |
746 } | |
747 | |
748 // Also check the serialized forms in case there's a field that we forget | |
749 // to add above. | |
750 EXPECT_EQ(expected.SerializeAsString(), | |
751 sanitized_request.SerializeAsString()); | |
752 } | |
753 | |
754 } // namespace safe_browsing | 649 } // namespace safe_browsing |
OLD | NEW |