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

Side by Side Diff: components/safe_browsing/base_ping_manager_unittest.cc

Issue 2650973005: Componentize ping_manager (Closed)
Patch Set: rebase Created 3 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
« no previous file with comments | « components/safe_browsing/base_ping_manager.cc ('k') | components/safe_browsing_db/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "components/safe_browsing/base_ping_manager.h"
6 #include "base/base64.h" 7 #include "base/base64.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "base/run_loop.h" 9 #include "base/run_loop.h"
9 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
10 #include "base/time/time.h" 11 #include "base/time/time.h"
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "chrome/browser/safe_browsing/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() : net_log_(new net::TestNetLog()) {
37 : net_log_(new net::TestNetLog()) {
38 net_log_with_source_ = net::NetLogWithSource::Make( 37 net_log_with_source_ = net::NetLogWithSource::Make(
39 net_log_.get(), net::NetLogSourceType::SAFE_BROWSING); 38 net_log_.get(), net::NetLogSourceType::SAFE_BROWSING);
40 } 39 }
41 40
42 protected: 41 protected:
43 void SetUp() override { 42 void SetUp() override {
44 std::string key = google_apis::GetAPIKey(); 43 std::string key = google_apis::GetAPIKey();
45 if (!key.empty()) { 44 if (!key.empty()) {
46 key_param_ = base::StringPrintf( 45 key_param_ = base::StringPrintf(
47 "&key=%s", 46 "&key=%s", net::EscapeQueryParamValue(key, true).c_str());
48 net::EscapeQueryParamValue(key, true).c_str());
49 } 47 }
50 48
51 SafeBrowsingProtocolConfig config; 49 SafeBrowsingProtocolConfig config;
52 config.client_name = kClient; 50 config.client_name = kClient;
53 config.url_prefix = kUrlPrefix; 51 config.url_prefix = kUrlPrefix;
54 ping_manager_.reset(new SafeBrowsingPingManager(NULL, config)); 52 ping_manager_.reset(new BasePingManager(NULL, config));
55 ping_manager_->version_ = kAppVer; 53 ping_manager_->version_ = kAppVer;
56 ping_manager_->net_log_ = net_log_with_source_; 54 ping_manager_->net_log_ = net_log_with_source_;
57 } 55 }
58 56
59 SafeBrowsingPingManager* ping_manager() { 57 BasePingManager* ping_manager() { return ping_manager_.get(); }
60 return ping_manager_.get();
61 }
62 58
63 std::string key_param_; 59 std::string key_param_;
64 std::unique_ptr<net::TestNetLog> net_log_; 60 std::unique_ptr<net::TestNetLog> net_log_;
65 net::NetLogWithSource net_log_with_source_; 61 net::NetLogWithSource net_log_with_source_;
66 net::TestURLFetcherFactory fetcher_factory_; 62 net::TestURLFetcherFactory fetcher_factory_;
67 std::unique_ptr<SafeBrowsingPingManager> ping_manager_; 63 std::unique_ptr<BasePingManager> ping_manager_;
68 }; 64 };
69 65
70 TEST_F(SafeBrowsingPingManagerTest, TestSafeBrowsingHitUrl) { 66 TEST_F(BasePingManagerTest, TestSafeBrowsingHitUrl) {
71 HitReport base_hp; 67 HitReport base_hp;
72 base_hp.malicious_url = GURL("http://malicious.url.com"); 68 base_hp.malicious_url = GURL("http://malicious.url.com");
73 base_hp.page_url = GURL("http://page.url.com"); 69 base_hp.page_url = GURL("http://page.url.com");
74 base_hp.referrer_url = GURL("http://referrer.url.com"); 70 base_hp.referrer_url = GURL("http://referrer.url.com");
75 71
76 { 72 {
77 HitReport hp(base_hp); 73 HitReport hp(base_hp);
78 hp.threat_type = SB_THREAT_TYPE_URL_MALWARE; 74 hp.threat_type = SB_THREAT_TYPE_URL_MALWARE;
79 hp.threat_source = ThreatSource::LOCAL_PVER3; 75 hp.threat_source = ThreatSource::LOCAL_PVER3;
80 hp.is_subresource = true; 76 hp.is_subresource = true;
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 "pver=3.0" + 191 "pver=3.0" +
196 key_param_ + 192 key_param_ +
197 "&ext=0&evts=malcsdhit&" 193 "&ext=0&evts=malcsdhit&"
198 "evtd=http%3A%2F%2Fmalicious.url.com%2F&" 194 "evtd=http%3A%2F%2Fmalicious.url.com%2F&"
199 "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer." 195 "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", 196 "url.com%2F&evtb=1&src=l4&m=0&up=foo+bar",
201 ping_manager()->SafeBrowsingHitUrl(hp).spec()); 197 ping_manager()->SafeBrowsingHitUrl(hp).spec());
202 } 198 }
203 } 199 }
204 200
205 TEST_F(SafeBrowsingPingManagerTest, TestThreatDetailsUrl) { 201 TEST_F(BasePingManagerTest, TestThreatDetailsUrl) {
206 EXPECT_EQ("https://prefix.com/foo/clientreport/malware?" 202 EXPECT_EQ(
207 "client=unittest&appver=1.0&pver=1.0" + key_param_, 203 "https://prefix.com/foo/clientreport/malware?"
208 ping_manager()->ThreatDetailsUrl().spec()); 204 "client=unittest&appver=1.0&pver=1.0" +
205 key_param_,
206 ping_manager()->ThreatDetailsUrl().spec());
209 } 207 }
210 208
211 TEST_F(SafeBrowsingPingManagerTest, TestReportThreatDetails) { 209 TEST_F(BasePingManagerTest, TestReportThreatDetails) {
212 const std::string kThreatDetailsReportString = "Threat Details Report String"; 210 const std::string kThreatDetailsReportString = "Threat Details Report String";
213 std::string encoded_threat_report = ""; 211 std::string encoded_threat_report = "";
214 base::Base64Encode(kThreatDetailsReportString, &encoded_threat_report); 212 base::Base64Encode(kThreatDetailsReportString, &encoded_threat_report);
215 std::string expected_threat_details_url = ping_manager()->ThreatDetailsUrl() 213 std::string expected_threat_details_url =
216 .spec(); 214 ping_manager()->ThreatDetailsUrl().spec();
217 const int kRequestErrorCode = -123; 215 const int kRequestErrorCode = -123;
218 216
219 // Start the report. 217 // Start the report.
220 ping_manager()->ReportThreatDetails(kThreatDetailsReportString); 218 ping_manager()->ReportThreatDetails(kThreatDetailsReportString);
221 219
222 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 220 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
223 DCHECK(fetcher); 221 DCHECK(fetcher);
224 // Set some error response data on the fetcher to make things interesting. 222 // Set some error response data on the fetcher to make things interesting.
225 fetcher->set_status( 223 fetcher->set_status(
226 net::URLRequestStatus(net::URLRequestStatus::FAILED, kRequestErrorCode)); 224 net::URLRequestStatus(net::URLRequestStatus::FAILED, kRequestErrorCode));
(...skipping 29 matching lines...) Expand all
256 EXPECT_EQ(net::URLRequestStatus::FAILED, int_value); 254 EXPECT_EQ(net::URLRequestStatus::FAILED, int_value);
257 255
258 EXPECT_TRUE(end_entry.GetIntegerValue("error", &int_value)); 256 EXPECT_TRUE(end_entry.GetIntegerValue("error", &int_value));
259 EXPECT_EQ(kRequestErrorCode, int_value); 257 EXPECT_EQ(kRequestErrorCode, int_value);
260 258
261 // We don't really care what the source_dependency value is, just making sure 259 // We don't really care what the source_dependency value is, just making sure
262 // it's there. 260 // it's there.
263 EXPECT_TRUE(end_entry.params->HasKey("source_dependency")); 261 EXPECT_TRUE(end_entry.params->HasKey("source_dependency"));
264 } 262 }
265 263
266 TEST_F(SafeBrowsingPingManagerTest, TestReportSafeBrowsingHit) { 264 TEST_F(BasePingManagerTest, TestReportSafeBrowsingHit) {
267 const std::string kHitReportPostData = "Hit Report POST Data"; 265 const std::string kHitReportPostData = "Hit Report POST Data";
268 std::string encoded_post_data = ""; 266 std::string encoded_post_data = "";
269 base::Base64Encode(kHitReportPostData, &encoded_post_data); 267 base::Base64Encode(kHitReportPostData, &encoded_post_data);
270 268
271 HitReport hp; 269 HitReport hp;
272 hp.malicious_url = GURL("http://malicious.url.com"); 270 hp.malicious_url = GURL("http://malicious.url.com");
273 hp.page_url = GURL("http://page.url.com"); 271 hp.page_url = GURL("http://page.url.com");
274 hp.referrer_url = GURL("http://referrer.url.com"); 272 hp.referrer_url = GURL("http://referrer.url.com");
275 hp.threat_type = SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL; 273 hp.threat_type = SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL;
276 hp.threat_source = ThreatSource::LOCAL_PVER4; 274 hp.threat_source = ThreatSource::LOCAL_PVER4;
277 hp.extended_reporting_level = SBER_LEVEL_OFF; 275 hp.extended_reporting_level = SBER_LEVEL_OFF;
278 hp.is_metrics_reporting_active = false; 276 hp.is_metrics_reporting_active = false;
279 hp.is_subresource = true; 277 hp.is_subresource = true;
280 hp.population_id = "foo bar"; 278 hp.population_id = "foo bar";
281 hp.post_data = kHitReportPostData; 279 hp.post_data = kHitReportPostData;
282 std::string expected_hit_report_url = ping_manager()->SafeBrowsingHitUrl(hp) 280 std::string expected_hit_report_url =
283 .spec(); 281 ping_manager()->SafeBrowsingHitUrl(hp).spec();
284 const int kRequestErrorCode = -321; 282 const int kRequestErrorCode = -321;
285 283
286 // Start the report. 284 // Start the report.
287 ping_manager()->ReportSafeBrowsingHit(hp); 285 ping_manager()->ReportSafeBrowsingHit(hp);
288 286
289 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 287 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
290 DCHECK(fetcher); 288 DCHECK(fetcher);
291 // Set some error response data on the fetcher to make things interesting. 289 // Set some error response data on the fetcher to make things interesting.
292 fetcher->set_status( 290 fetcher->set_status(
293 net::URLRequestStatus(net::URLRequestStatus::FAILED, kRequestErrorCode)); 291 net::URLRequestStatus(net::URLRequestStatus::FAILED, kRequestErrorCode));
(...skipping 30 matching lines...) Expand all
324 322
325 EXPECT_TRUE(end_entry.GetIntegerValue("error", &int_value)); 323 EXPECT_TRUE(end_entry.GetIntegerValue("error", &int_value));
326 EXPECT_EQ(kRequestErrorCode, int_value); 324 EXPECT_EQ(kRequestErrorCode, int_value);
327 325
328 // We don't really care what the source_dependency value is, just making sure 326 // We don't really care what the source_dependency value is, just making sure
329 // it's there. 327 // it's there.
330 EXPECT_TRUE(end_entry.params->HasKey("source_dependency")); 328 EXPECT_TRUE(end_entry.params->HasKey("source_dependency"));
331 } 329 }
332 330
333 } // namespace safe_browsing 331 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing/base_ping_manager.cc ('k') | components/safe_browsing_db/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698