| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2017 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 | 5 |
| 6 #include "base/base64.h" | 6 #include "base/base64.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.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/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/safe_browsing/ping_manager.h" | 12 #include "components/safe_browsing/base_ping_manager.h" |
| 13 #include "google_apis/google_api_keys.h" | 13 #include "google_apis/google_api_keys.h" |
| 14 #include "net/base/escape.h" | 14 #include "net/base/escape.h" |
| 15 #include "net/log/net_log.h" | 15 #include "net/log/net_log.h" |
| 16 #include "net/log/net_log_source_type.h" | 16 #include "net/log/net_log_source_type.h" |
| 17 #include "net/log/test_net_log.h" | 17 #include "net/log/test_net_log.h" |
| 18 #include "net/log/test_net_log_entry.h" | 18 #include "net/log/test_net_log_entry.h" |
| 19 #include "net/url_request/report_sender.h" | 19 #include "net/url_request/report_sender.h" |
| 20 #include "net/url_request/test_url_fetcher_factory.h" | 20 #include "net/url_request/test_url_fetcher_factory.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 using base::Time; | 23 using base::Time; |
| 24 using base::TimeDelta; | 24 using base::TimeDelta; |
| 25 using safe_browsing::HitReport; | 25 using safe_browsing::HitReport; |
| 26 using safe_browsing::ThreatSource; | 26 using safe_browsing::ThreatSource; |
| 27 | 27 |
| 28 static const char kUrlPrefix[] = "https://prefix.com/foo"; | 28 static const char kUrlPrefix[] = "https://prefix.com/foo"; |
| 29 static const char kClient[] = "unittest"; | 29 static const char kClient[] = "unittest"; |
| 30 static const char kAppVer[] = "1.0"; | 30 static const char kAppVer[] = "1.0"; |
| 31 | 31 |
| 32 namespace safe_browsing { | 32 namespace safe_browsing { |
| 33 | 33 |
| 34 class SafeBrowsingPingManagerTest : public testing::Test { | 34 class BasePingManagerTest : public testing::Test { |
| 35 public: | 35 public: |
| 36 SafeBrowsingPingManagerTest() | 36 BasePingManagerTest() |
| 37 : net_log_(new net::TestNetLog()) { | 37 : net_log_(new net::TestNetLog()) { |
| 38 net_log_with_source_ = net::NetLogWithSource::Make( | 38 net_log_with_source_ = net::NetLogWithSource::Make( |
| 39 net_log_.get(), net::NetLogSourceType::SAFE_BROWSING); | 39 net_log_.get(), net::NetLogSourceType::SAFE_BROWSING); |
| 40 } | 40 } |
| 41 | 41 |
| 42 protected: | 42 protected: |
| 43 void SetUp() override { | 43 void SetUp() override { |
| 44 std::string key = google_apis::GetAPIKey(); | 44 std::string key = google_apis::GetAPIKey(); |
| 45 if (!key.empty()) { | 45 if (!key.empty()) { |
| 46 key_param_ = base::StringPrintf( | 46 key_param_ = base::StringPrintf( |
| 47 "&key=%s", | 47 "&key=%s", |
| 48 net::EscapeQueryParamValue(key, true).c_str()); | 48 net::EscapeQueryParamValue(key, true).c_str()); |
| 49 } | 49 } |
| 50 | 50 |
| 51 SafeBrowsingProtocolConfig config; | 51 SafeBrowsingProtocolConfig config; |
| 52 config.client_name = kClient; | 52 config.client_name = kClient; |
| 53 config.url_prefix = kUrlPrefix; | 53 config.url_prefix = kUrlPrefix; |
| 54 ping_manager_.reset(new SafeBrowsingPingManager(NULL, config)); | 54 ping_manager_.reset(new BasePingManager(NULL, config)); |
| 55 ping_manager_->version_ = kAppVer; | 55 ping_manager_->version_ = kAppVer; |
| 56 ping_manager_->net_log_ = net_log_with_source_; | 56 ping_manager_->net_log_ = net_log_with_source_; |
| 57 } | 57 } |
| 58 | 58 |
| 59 SafeBrowsingPingManager* ping_manager() { | 59 BasePingManager* ping_manager() { |
| 60 return ping_manager_.get(); | 60 return ping_manager_.get(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 std::string key_param_; | 63 std::string key_param_; |
| 64 std::unique_ptr<net::TestNetLog> net_log_; | 64 std::unique_ptr<net::TestNetLog> net_log_; |
| 65 net::NetLogWithSource net_log_with_source_; | 65 net::NetLogWithSource net_log_with_source_; |
| 66 net::TestURLFetcherFactory fetcher_factory_; | 66 net::TestURLFetcherFactory fetcher_factory_; |
| 67 std::unique_ptr<SafeBrowsingPingManager> ping_manager_; | 67 std::unique_ptr<BasePingManager> ping_manager_; |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 TEST_F(SafeBrowsingPingManagerTest, TestSafeBrowsingHitUrl) { | 70 TEST_F(BasePingManagerTest, TestSafeBrowsingHitUrl) { |
| 71 HitReport base_hp; | 71 HitReport base_hp; |
| 72 base_hp.malicious_url = GURL("http://malicious.url.com"); | 72 base_hp.malicious_url = GURL("http://malicious.url.com"); |
| 73 base_hp.page_url = GURL("http://page.url.com"); | 73 base_hp.page_url = GURL("http://page.url.com"); |
| 74 base_hp.referrer_url = GURL("http://referrer.url.com"); | 74 base_hp.referrer_url = GURL("http://referrer.url.com"); |
| 75 | 75 |
| 76 { | 76 { |
| 77 HitReport hp(base_hp); | 77 HitReport hp(base_hp); |
| 78 hp.threat_type = SB_THREAT_TYPE_URL_MALWARE; | 78 hp.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 79 hp.threat_source = ThreatSource::LOCAL_PVER3; | 79 hp.threat_source = ThreatSource::LOCAL_PVER3; |
| 80 hp.is_subresource = true; | 80 hp.is_subresource = true; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 "pver=3.0" + | 195 "pver=3.0" + |
| 196 key_param_ + | 196 key_param_ + |
| 197 "&ext=0&evts=malcsdhit&" | 197 "&ext=0&evts=malcsdhit&" |
| 198 "evtd=http%3A%2F%2Fmalicious.url.com%2F&" | 198 "evtd=http%3A%2F%2Fmalicious.url.com%2F&" |
| 199 "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer." | 199 "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer." |
| 200 "url.com%2F&evtb=1&src=l4&m=0&up=foo+bar", | 200 "url.com%2F&evtb=1&src=l4&m=0&up=foo+bar", |
| 201 ping_manager()->SafeBrowsingHitUrl(hp).spec()); | 201 ping_manager()->SafeBrowsingHitUrl(hp).spec()); |
| 202 } | 202 } |
| 203 } | 203 } |
| 204 | 204 |
| 205 TEST_F(SafeBrowsingPingManagerTest, TestThreatDetailsUrl) { | 205 TEST_F(BasePingManagerTest, TestThreatDetailsUrl) { |
| 206 EXPECT_EQ("https://prefix.com/foo/clientreport/malware?" | 206 EXPECT_EQ("https://prefix.com/foo/clientreport/malware?" |
| 207 "client=unittest&appver=1.0&pver=1.0" + key_param_, | 207 "client=unittest&appver=1.0&pver=1.0" + key_param_, |
| 208 ping_manager()->ThreatDetailsUrl().spec()); | 208 ping_manager()->ThreatDetailsUrl().spec()); |
| 209 } | 209 } |
| 210 | 210 |
| 211 TEST_F(SafeBrowsingPingManagerTest, TestReportThreatDetails) { | 211 TEST_F(BasePingManagerTest, TestReportThreatDetails) { |
| 212 const std::string kThreatDetailsReportString = "Threat Details Report String"; | 212 const std::string kThreatDetailsReportString = "Threat Details Report String"; |
| 213 std::string encoded_threat_report = ""; | 213 std::string encoded_threat_report = ""; |
| 214 base::Base64Encode(kThreatDetailsReportString, &encoded_threat_report); | 214 base::Base64Encode(kThreatDetailsReportString, &encoded_threat_report); |
| 215 std::string expected_threat_details_url = ping_manager()->ThreatDetailsUrl() | 215 std::string expected_threat_details_url = ping_manager()->ThreatDetailsUrl() |
| 216 .spec(); | 216 .spec(); |
| 217 const int kRequestErrorCode = -123; | 217 const int kRequestErrorCode = -123; |
| 218 | 218 |
| 219 // Start the report. | 219 // Start the report. |
| 220 ping_manager()->ReportThreatDetails(kThreatDetailsReportString); | 220 ping_manager()->ReportThreatDetails(kThreatDetailsReportString); |
| 221 | 221 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 EXPECT_EQ(net::URLRequestStatus::FAILED, int_value); | 256 EXPECT_EQ(net::URLRequestStatus::FAILED, int_value); |
| 257 | 257 |
| 258 EXPECT_TRUE(end_entry.GetIntegerValue("error", &int_value)); | 258 EXPECT_TRUE(end_entry.GetIntegerValue("error", &int_value)); |
| 259 EXPECT_EQ(kRequestErrorCode, int_value); | 259 EXPECT_EQ(kRequestErrorCode, int_value); |
| 260 | 260 |
| 261 // We don't really care what the source_dependency value is, just making sure | 261 // We don't really care what the source_dependency value is, just making sure |
| 262 // it's there. | 262 // it's there. |
| 263 EXPECT_TRUE(end_entry.params->HasKey("source_dependency")); | 263 EXPECT_TRUE(end_entry.params->HasKey("source_dependency")); |
| 264 } | 264 } |
| 265 | 265 |
| 266 TEST_F(SafeBrowsingPingManagerTest, TestReportSafeBrowsingHit) { | 266 TEST_F(BasePingManagerTest, TestReportSafeBrowsingHit) { |
| 267 const std::string kHitReportPostData = "Hit Report POST Data"; | 267 const std::string kHitReportPostData = "Hit Report POST Data"; |
| 268 std::string encoded_post_data = ""; | 268 std::string encoded_post_data = ""; |
| 269 base::Base64Encode(kHitReportPostData, &encoded_post_data); | 269 base::Base64Encode(kHitReportPostData, &encoded_post_data); |
| 270 | 270 |
| 271 HitReport hp; | 271 HitReport hp; |
| 272 hp.malicious_url = GURL("http://malicious.url.com"); | 272 hp.malicious_url = GURL("http://malicious.url.com"); |
| 273 hp.page_url = GURL("http://page.url.com"); | 273 hp.page_url = GURL("http://page.url.com"); |
| 274 hp.referrer_url = GURL("http://referrer.url.com"); | 274 hp.referrer_url = GURL("http://referrer.url.com"); |
| 275 hp.threat_type = SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL; | 275 hp.threat_type = SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL; |
| 276 hp.threat_source = ThreatSource::LOCAL_PVER4; | 276 hp.threat_source = ThreatSource::LOCAL_PVER4; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 | 324 |
| 325 EXPECT_TRUE(end_entry.GetIntegerValue("error", &int_value)); | 325 EXPECT_TRUE(end_entry.GetIntegerValue("error", &int_value)); |
| 326 EXPECT_EQ(kRequestErrorCode, int_value); | 326 EXPECT_EQ(kRequestErrorCode, int_value); |
| 327 | 327 |
| 328 // We don't really care what the source_dependency value is, just making sure | 328 // We don't really care what the source_dependency value is, just making sure |
| 329 // it's there. | 329 // it's there. |
| 330 EXPECT_TRUE(end_entry.params->HasKey("source_dependency")); | 330 EXPECT_TRUE(end_entry.params->HasKey("source_dependency")); |
| 331 } | 331 } |
| 332 | 332 |
| 333 } // namespace safe_browsing | 333 } // namespace safe_browsing |
| OLD | NEW |