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" |
13 #include "base/task.h" | 14 #include "base/task.h" |
14 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "chrome/browser/safe_browsing/browser_features.h" |
15 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 17 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
16 #include "chrome/common/safe_browsing/client_model.pb.h" | 18 #include "chrome/common/safe_browsing/client_model.pb.h" |
17 #include "chrome/common/safe_browsing/csd.pb.h" | 19 #include "chrome/common/safe_browsing/csd.pb.h" |
| 20 #include "chrome/renderer/safe_browsing/features.h" |
18 #include "chrome/test/base/testing_browser_process_test.h" | 21 #include "chrome/test/base/testing_browser_process_test.h" |
19 #include "content/browser/browser_thread.h" | 22 #include "content/browser/browser_thread.h" |
20 #include "content/common/url_fetcher.h" | 23 #include "content/common/url_fetcher.h" |
21 #include "content/test/test_url_fetcher_factory.h" | 24 #include "content/test/test_url_fetcher_factory.h" |
22 #include "crypto/sha2.h" | 25 #include "crypto/sha2.h" |
23 #include "googleurl/src/gurl.h" | 26 #include "googleurl/src/gurl.h" |
24 #include "net/url_request/url_request_status.h" | 27 #include "net/url_request/url_request_status.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 28 #include "testing/gmock/include/gmock/gmock.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
27 | 30 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 EXPECT_TRUE(csd_service_->GetValidCachedResult( | 144 EXPECT_TRUE(csd_service_->GetValidCachedResult( |
142 GURL("http://first.url.com"), &is_phishing)); | 145 GURL("http://first.url.com"), &is_phishing)); |
143 EXPECT_FALSE(is_phishing); | 146 EXPECT_FALSE(is_phishing); |
144 EXPECT_FALSE(csd_service_->GetValidCachedResult( | 147 EXPECT_FALSE(csd_service_->GetValidCachedResult( |
145 GURL("http://third.url.com"), &is_phishing)); | 148 GURL("http://third.url.com"), &is_phishing)); |
146 EXPECT_TRUE(csd_service_->GetValidCachedResult( | 149 EXPECT_TRUE(csd_service_->GetValidCachedResult( |
147 GURL("http://fourth.url.com"), &is_phishing)); | 150 GURL("http://fourth.url.com"), &is_phishing)); |
148 EXPECT_TRUE(is_phishing); | 151 EXPECT_TRUE(is_phishing); |
149 } | 152 } |
150 | 153 |
| 154 void AddFeature(const std::string& name, double value, |
| 155 ClientPhishingRequest* request) { |
| 156 ClientPhishingRequest_Feature* feature = request->add_feature_map(); |
| 157 feature->set_name(name); |
| 158 feature->set_value(value); |
| 159 } |
| 160 |
| 161 void AddNonModelFeature(const std::string& name, double value, |
| 162 ClientPhishingRequest* request) { |
| 163 ClientPhishingRequest_Feature* feature = |
| 164 request->add_non_model_feature_map(); |
| 165 feature->set_name(name); |
| 166 feature->set_value(value); |
| 167 } |
| 168 |
151 protected: | 169 protected: |
152 scoped_ptr<ClientSideDetectionService> csd_service_; | 170 scoped_ptr<ClientSideDetectionService> csd_service_; |
153 scoped_ptr<FakeURLFetcherFactory> factory_; | 171 scoped_ptr<FakeURLFetcherFactory> factory_; |
154 MessageLoop msg_loop_; | 172 MessageLoop msg_loop_; |
155 | 173 |
156 private: | 174 private: |
157 void SendRequestDone(GURL phishing_url, bool is_phishing) { | 175 void SendRequestDone(GURL phishing_url, bool is_phishing) { |
158 ASSERT_EQ(phishing_url, phishing_url_); | 176 ASSERT_EQ(phishing_url, phishing_url_); |
159 is_phishing_ = is_phishing; | 177 is_phishing_ = is_phishing; |
160 msg_loop_.Quit(); | 178 msg_loop_.Quit(); |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 rule->add_feature(-1); | 570 rule->add_feature(-1); |
553 rule->set_weight(0.2f); | 571 rule->set_weight(0.2f); |
554 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); | 572 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); |
555 | 573 |
556 rule->set_feature(2, 2); | 574 rule->set_feature(2, 2); |
557 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); | 575 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); |
558 | 576 |
559 rule->set_feature(2, 1); | 577 rule->set_feature(2, 1); |
560 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); | 578 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); |
561 } | 579 } |
| 580 |
| 581 TEST_F(ClientSideDetectionServiceTest, SanitizeRequestForPingback) { |
| 582 ClientPhishingRequest request; |
| 583 request.set_url("http://www.us.host.com/blah"); |
| 584 request.set_suffix_prefix_hash("hash"); |
| 585 request.set_client_score(0.8f); |
| 586 request.set_is_phishing(true); |
| 587 AddFeature(std::string(features::kUrlTldToken) + "com", 1.0, &request); |
| 588 AddFeature(std::string(features::kUrlDomainToken) + "host", 1.0, &request); |
| 589 AddFeature(std::string(features::kUrlOtherHostToken) + "us", 1.0, &request); |
| 590 AddFeature(std::string(features::kUrlOtherHostToken) + "www", 1.0, &request); |
| 591 AddFeature(features::kUrlNumOtherHostTokensGTOne, 1.0, &request); |
| 592 AddFeature(std::string(features::kUrlPathToken) + "blah", 1.0, &request); |
| 593 AddFeature(features::kPageHasForms, 1.0, &request); |
| 594 AddFeature(std::string(features::kPageTerm) + "term", 1.0, &request); |
| 595 AddFeature(features::kPageImgOtherDomainFreq, 0.5, &request); |
| 596 request.set_model_version(3); |
| 597 AddNonModelFeature(features::kUrlHistoryVisitCount, 5.0, &request); |
| 598 AddNonModelFeature(StringPrintf("%s=http://referrer.com/", |
| 599 features::kReferrer), |
| 600 1.0, &request); |
| 601 AddNonModelFeature(StringPrintf("%s%s=http://redirreferrer.com/", |
| 602 features::kRedirectPrefix, |
| 603 features::kReferrer), |
| 604 1.0, &request); |
| 605 AddNonModelFeature(StringPrintf("%s%s=http://hostreferrer.com/", |
| 606 features::kHostPrefix, features::kReferrer), |
| 607 1.0, &request); |
| 608 AddNonModelFeature(StringPrintf("%s%s%s=http://hostredirreferrer.com/", |
| 609 features::kHostPrefix, |
| 610 features::kRedirectPrefix, |
| 611 features::kReferrer), |
| 612 1.0, &request); |
| 613 AddNonModelFeature(std::string(features::kBadIpFetch) + "1.2.3.4", |
| 614 1.0, &request); |
| 615 AddNonModelFeature(std::string(features::kSafeBrowsingMaliciousUrl) + |
| 616 "http://malicious.com/", 1.0, &request); |
| 617 AddNonModelFeature(std::string(features::kSafeBrowsingOriginalUrl) + |
| 618 "http://original.com/", 1.0, &request); |
| 619 |
| 620 ClientPhishingRequest sanitized_request; |
| 621 ClientSideDetectionService::SanitizeRequestForPingback(request, |
| 622 &sanitized_request); |
| 623 |
| 624 // For easier debugging, we'll check the output protobuf fields individually. |
| 625 ClientPhishingRequest expected; |
| 626 expected.set_suffix_prefix_hash(request.suffix_prefix_hash()); |
| 627 expected.set_client_score(request.client_score()); |
| 628 expected.set_is_phishing(request.is_phishing()); |
| 629 AddFeature(features::kUrlNumOtherHostTokensGTOne, 1.0, &expected); |
| 630 AddFeature(features::kPageHasForms, 1.0, &expected); |
| 631 AddFeature(features::kPageImgOtherDomainFreq, 0.5, &expected); |
| 632 expected.set_model_version(3); |
| 633 AddNonModelFeature(features::kUrlHistoryVisitCount, 5.0, &expected); |
| 634 |
| 635 EXPECT_FALSE(sanitized_request.has_url()); |
| 636 EXPECT_EQ(expected.suffix_prefix_hash(), |
| 637 sanitized_request.suffix_prefix_hash()); |
| 638 EXPECT_FLOAT_EQ(expected.client_score(), sanitized_request.client_score()); |
| 639 EXPECT_EQ(expected.is_phishing(), sanitized_request.is_phishing()); |
| 640 |
| 641 ASSERT_EQ(expected.feature_map_size(), sanitized_request.feature_map_size()); |
| 642 for (int i = 0; i < expected.feature_map_size(); ++i) { |
| 643 EXPECT_EQ(expected.feature_map(i).name(), |
| 644 sanitized_request.feature_map(i).name()) << "Feature " << i; |
| 645 EXPECT_DOUBLE_EQ(expected.feature_map(i).value(), |
| 646 sanitized_request.feature_map(i).value()) |
| 647 << "Feature " << i; |
| 648 } |
| 649 EXPECT_EQ(expected.model_version(), sanitized_request.model_version()); |
| 650 ASSERT_EQ(expected.non_model_feature_map_size(), |
| 651 sanitized_request.non_model_feature_map_size()); |
| 652 for (int i = 0; i < expected.non_model_feature_map_size(); ++i) { |
| 653 EXPECT_EQ(expected.non_model_feature_map(i).name(), |
| 654 sanitized_request.non_model_feature_map(i).name()) |
| 655 << "Non-model feature " << i; |
| 656 EXPECT_DOUBLE_EQ(expected.non_model_feature_map(i).value(), |
| 657 sanitized_request.non_model_feature_map(i).value()) |
| 658 << "Non-model feature " << i; |
| 659 } |
| 660 |
| 661 // Also check the serialized forms in case there's a field that we forget |
| 662 // to add above. |
| 663 EXPECT_EQ(expected.SerializeAsString(), |
| 664 sanitized_request.SerializeAsString()); |
| 665 } |
562 } // namespace safe_browsing | 666 } // namespace safe_browsing |
OLD | NEW |