OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 // | |
5 | |
6 #include "base/base64.h" | |
7 #include "base/logging.h" | |
8 #include "base/run_loop.h" | |
9 #include "base/strings/stringprintf.h" | |
10 #include "base/time/time.h" | |
11 #include "base/values.h" | |
12 #include "chrome/browser/safe_browsing/ping_manager.h" | |
13 #include "google_apis/google_api_keys.h" | |
14 #include "net/base/escape.h" | |
15 #include "net/log/net_log.h" | |
16 #include "net/log/net_log_source_type.h" | |
17 #include "net/log/test_net_log.h" | |
18 #include "net/log/test_net_log_entry.h" | |
19 #include "net/url_request/report_sender.h" | |
20 #include "net/url_request/test_url_fetcher_factory.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 using base::Time; | |
24 using base::TimeDelta; | |
25 using safe_browsing::HitReport; | |
26 using safe_browsing::ThreatSource; | |
27 | |
28 static const char kUrlPrefix[] = "https://prefix.com/foo"; | |
29 static const char kClient[] = "unittest"; | |
30 static const char kAppVer[] = "1.0"; | |
31 | |
32 namespace safe_browsing { | |
33 | |
34 class SafeBrowsingPingManagerTest : public testing::Test { | |
35 public: | |
36 SafeBrowsingPingManagerTest() | |
37 : net_log_(new net::TestNetLog()) { | |
38 net_log_with_source_ = net::NetLogWithSource::Make( | |
39 net_log_.get(), net::NetLogSourceType::SAFE_BROWSING); | |
40 } | |
41 | |
42 protected: | |
43 void SetUp() override { | |
44 std::string key = google_apis::GetAPIKey(); | |
45 if (!key.empty()) { | |
46 key_param_ = base::StringPrintf( | |
47 "&key=%s", | |
48 net::EscapeQueryParamValue(key, true).c_str()); | |
49 } | |
50 | |
51 SafeBrowsingProtocolConfig config; | |
52 config.client_name = kClient; | |
53 config.url_prefix = kUrlPrefix; | |
54 ping_manager_.reset(new SafeBrowsingPingManager(NULL, config)); | |
55 ping_manager_->version_ = kAppVer; | |
56 ping_manager_->net_log_ = net_log_with_source_; | |
57 } | |
58 | |
59 SafeBrowsingPingManager* ping_manager() { | |
60 return ping_manager_.get(); | |
61 } | |
62 | |
63 std::string key_param_; | |
64 std::unique_ptr<net::TestNetLog> net_log_; | |
65 net::NetLogWithSource net_log_with_source_; | |
66 net::TestURLFetcherFactory fetcher_factory_; | |
67 std::unique_ptr<SafeBrowsingPingManager> ping_manager_; | |
68 }; | |
69 | |
70 TEST_F(SafeBrowsingPingManagerTest, TestSafeBrowsingHitUrl) { | |
71 HitReport base_hp; | |
72 base_hp.malicious_url = GURL("http://malicious.url.com"); | |
73 base_hp.page_url = GURL("http://page.url.com"); | |
74 base_hp.referrer_url = GURL("http://referrer.url.com"); | |
75 | |
76 { | |
77 HitReport hp(base_hp); | |
78 hp.threat_type = SB_THREAT_TYPE_URL_MALWARE; | |
79 hp.threat_source = ThreatSource::LOCAL_PVER3; | |
80 hp.is_subresource = true; | |
81 hp.extended_reporting_level = SBER_LEVEL_LEGACY; | |
82 hp.is_metrics_reporting_active = true; | |
83 | |
84 EXPECT_EQ( | |
85 "https://prefix.com/foo/report?client=unittest&appver=1.0&" | |
86 "pver=3.0" + | |
87 key_param_ + | |
88 "&ext=1&evts=malblhit&evtd=http%3A%2F%2Fmalicious.url.com%2F&" | |
89 "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer." | |
90 "url.com%2F&evtb=1&src=l3&m=1", | |
91 ping_manager()->SafeBrowsingHitUrl(hp).spec()); | |
92 } | |
93 | |
94 { | |
95 HitReport hp(base_hp); | |
96 hp.threat_type = SB_THREAT_TYPE_URL_PHISHING; | |
97 hp.threat_source = ThreatSource::DATA_SAVER; | |
98 hp.is_subresource = false; | |
99 hp.extended_reporting_level = SBER_LEVEL_LEGACY; | |
100 hp.is_metrics_reporting_active = true; | |
101 EXPECT_EQ( | |
102 "https://prefix.com/foo/report?client=unittest&appver=1.0&" | |
103 "pver=3.0" + | |
104 key_param_ + | |
105 "&ext=1&evts=phishblhit&" | |
106 "evtd=http%3A%2F%2Fmalicious.url.com%2F&" | |
107 "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer." | |
108 "url.com%2F&evtb=0&src=ds&m=1", | |
109 ping_manager()->SafeBrowsingHitUrl(hp).spec()); | |
110 } | |
111 | |
112 { | |
113 HitReport hp(base_hp); | |
114 hp.threat_type = SB_THREAT_TYPE_URL_PHISHING; | |
115 hp.threat_source = ThreatSource::DATA_SAVER; | |
116 hp.is_subresource = false; | |
117 hp.extended_reporting_level = SBER_LEVEL_SCOUT; | |
118 hp.is_metrics_reporting_active = true; | |
119 EXPECT_EQ( | |
120 "https://prefix.com/foo/report?client=unittest&appver=1.0&" | |
121 "pver=3.0" + | |
122 key_param_ + | |
123 "&ext=2&evts=phishblhit&" | |
124 "evtd=http%3A%2F%2Fmalicious.url.com%2F&" | |
125 "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer." | |
126 "url.com%2F&evtb=0&src=ds&m=1", | |
127 ping_manager()->SafeBrowsingHitUrl(hp).spec()); | |
128 } | |
129 | |
130 { | |
131 HitReport hp(base_hp); | |
132 hp.threat_type = SB_THREAT_TYPE_BINARY_MALWARE_URL; | |
133 hp.threat_source = ThreatSource::REMOTE; | |
134 hp.extended_reporting_level = SBER_LEVEL_OFF; | |
135 hp.is_metrics_reporting_active = true; | |
136 hp.is_subresource = false; | |
137 EXPECT_EQ( | |
138 "https://prefix.com/foo/report?client=unittest&appver=1.0&" | |
139 "pver=3.0" + | |
140 key_param_ + | |
141 "&ext=0&evts=binurlhit&" | |
142 "evtd=http%3A%2F%2Fmalicious.url.com%2F&" | |
143 "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer." | |
144 "url.com%2F&evtb=0&src=rem&m=1", | |
145 ping_manager()->SafeBrowsingHitUrl(hp).spec()); | |
146 } | |
147 | |
148 { | |
149 HitReport hp(base_hp); | |
150 hp.threat_type = SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL; | |
151 hp.threat_source = ThreatSource::LOCAL_PVER4; | |
152 hp.extended_reporting_level = SBER_LEVEL_OFF; | |
153 hp.is_metrics_reporting_active = false; | |
154 hp.is_subresource = false; | |
155 EXPECT_EQ( | |
156 "https://prefix.com/foo/report?client=unittest&appver=1.0&" | |
157 "pver=3.0" + | |
158 key_param_ + | |
159 "&ext=0&evts=phishcsdhit&" | |
160 "evtd=http%3A%2F%2Fmalicious.url.com%2F&" | |
161 "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer." | |
162 "url.com%2F&evtb=0&src=l4&m=0", | |
163 ping_manager()->SafeBrowsingHitUrl(hp).spec()); | |
164 } | |
165 | |
166 { | |
167 HitReport hp(base_hp); | |
168 hp.threat_type = SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL; | |
169 hp.threat_source = ThreatSource::LOCAL_PVER4; | |
170 hp.extended_reporting_level = SBER_LEVEL_OFF; | |
171 hp.is_metrics_reporting_active = false; | |
172 hp.is_subresource = true; | |
173 EXPECT_EQ( | |
174 "https://prefix.com/foo/report?client=unittest&appver=1.0&" | |
175 "pver=3.0" + | |
176 key_param_ + | |
177 "&ext=0&evts=malcsdhit&" | |
178 "evtd=http%3A%2F%2Fmalicious.url.com%2F&" | |
179 "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer." | |
180 "url.com%2F&evtb=1&src=l4&m=0", | |
181 ping_manager()->SafeBrowsingHitUrl(hp).spec()); | |
182 } | |
183 | |
184 // Same as above, but add population_id | |
185 { | |
186 HitReport hp(base_hp); | |
187 hp.threat_type = SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL; | |
188 hp.threat_source = ThreatSource::LOCAL_PVER4; | |
189 hp.extended_reporting_level = SBER_LEVEL_OFF; | |
190 hp.is_metrics_reporting_active = false; | |
191 hp.is_subresource = true; | |
192 hp.population_id = "foo bar"; | |
193 EXPECT_EQ( | |
194 "https://prefix.com/foo/report?client=unittest&appver=1.0&" | |
195 "pver=3.0" + | |
196 key_param_ + | |
197 "&ext=0&evts=malcsdhit&" | |
198 "evtd=http%3A%2F%2Fmalicious.url.com%2F&" | |
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", | |
201 ping_manager()->SafeBrowsingHitUrl(hp).spec()); | |
202 } | |
203 } | |
204 | |
205 TEST_F(SafeBrowsingPingManagerTest, TestThreatDetailsUrl) { | |
206 EXPECT_EQ("https://prefix.com/foo/clientreport/malware?" | |
207 "client=unittest&appver=1.0&pver=1.0" + key_param_, | |
208 ping_manager()->ThreatDetailsUrl().spec()); | |
209 } | |
210 | |
211 TEST_F(SafeBrowsingPingManagerTest, TestReportThreatDetails) { | |
212 const std::string kThreatDetailsReportString = "Threat Details Report String"; | |
213 std::string encoded_threat_report = ""; | |
214 base::Base64Encode(kThreatDetailsReportString, &encoded_threat_report); | |
215 std::string expected_threat_details_url = ping_manager()->ThreatDetailsUrl() | |
216 .spec(); | |
217 const int kRequestErrorCode = -123; | |
218 | |
219 // Start the report. | |
220 ping_manager()->ReportThreatDetails(kThreatDetailsReportString); | |
221 | |
222 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | |
223 DCHECK(fetcher); | |
224 // Set some error response data on the fetcher to make things interesting. | |
225 fetcher->set_status( | |
226 net::URLRequestStatus(net::URLRequestStatus::FAILED, kRequestErrorCode)); | |
227 // Tell the test fetcher to invoke the fetch callback. | |
228 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
229 | |
230 // We expect two net log entries: one when the ping starts, one when it ends. | |
231 net::TestNetLogEntry::List entries; | |
232 net_log_->GetEntries(&entries); | |
233 ASSERT_EQ(2u, entries.size()); | |
234 | |
235 // Check for expected log entries for the begin phase. | |
236 const net::TestNetLogEntry& start_entry = entries[0]; | |
237 ASSERT_EQ(3u, start_entry.params->size()); | |
238 | |
239 std::string string_value; | |
240 EXPECT_TRUE(start_entry.GetStringValue("url", &string_value)); | |
241 EXPECT_EQ(expected_threat_details_url, string_value); | |
242 | |
243 EXPECT_TRUE(start_entry.GetStringValue("payload", &string_value)); | |
244 EXPECT_EQ(encoded_threat_report, string_value); | |
245 | |
246 // We don't really care what the source_dependency value is, just making sure | |
247 // it's there. | |
248 EXPECT_TRUE(start_entry.params->HasKey("source_dependency")); | |
249 | |
250 // Check for expected log entries for the end phase. | |
251 const net::TestNetLogEntry& end_entry = entries[1]; | |
252 ASSERT_EQ(3u, end_entry.params->size()); | |
253 | |
254 int int_value; | |
255 EXPECT_TRUE(end_entry.GetIntegerValue("status", &int_value)); | |
256 EXPECT_EQ(net::URLRequestStatus::FAILED, int_value); | |
257 | |
258 EXPECT_TRUE(end_entry.GetIntegerValue("error", &int_value)); | |
259 EXPECT_EQ(kRequestErrorCode, int_value); | |
260 | |
261 // We don't really care what the source_dependency value is, just making sure | |
262 // it's there. | |
263 EXPECT_TRUE(end_entry.params->HasKey("source_dependency")); | |
264 } | |
265 | |
266 TEST_F(SafeBrowsingPingManagerTest, TestReportSafeBrowsingHit) { | |
267 const std::string kHitReportPostData = "Hit Report POST Data"; | |
268 std::string encoded_post_data = ""; | |
269 base::Base64Encode(kHitReportPostData, &encoded_post_data); | |
270 | |
271 HitReport hp; | |
272 hp.malicious_url = GURL("http://malicious.url.com"); | |
273 hp.page_url = GURL("http://page.url.com"); | |
274 hp.referrer_url = GURL("http://referrer.url.com"); | |
275 hp.threat_type = SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL; | |
276 hp.threat_source = ThreatSource::LOCAL_PVER4; | |
277 hp.extended_reporting_level = SBER_LEVEL_OFF; | |
278 hp.is_metrics_reporting_active = false; | |
279 hp.is_subresource = true; | |
280 hp.population_id = "foo bar"; | |
281 hp.post_data = kHitReportPostData; | |
282 std::string expected_hit_report_url = ping_manager()->SafeBrowsingHitUrl(hp) | |
283 .spec(); | |
284 const int kRequestErrorCode = -321; | |
285 | |
286 // Start the report. | |
287 ping_manager()->ReportSafeBrowsingHit(hp); | |
288 | |
289 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | |
290 DCHECK(fetcher); | |
291 // Set some error response data on the fetcher to make things interesting. | |
292 fetcher->set_status( | |
293 net::URLRequestStatus(net::URLRequestStatus::FAILED, kRequestErrorCode)); | |
294 // Tell the test fetcher to invoke the fetch callback. | |
295 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
296 | |
297 // We expect two net log entries: one when the ping starts, one when it ends. | |
298 net::TestNetLogEntry::List entries; | |
299 net_log_->GetEntries(&entries); | |
300 ASSERT_EQ(2u, entries.size()); | |
301 | |
302 // Check for expected log entries for the begin phase. | |
303 const net::TestNetLogEntry& start_entry = entries[0]; | |
304 ASSERT_EQ(3u, start_entry.params->size()); | |
305 | |
306 std::string string_value; | |
307 EXPECT_TRUE(start_entry.GetStringValue("url", &string_value)); | |
308 EXPECT_EQ(expected_hit_report_url, string_value); | |
309 | |
310 EXPECT_TRUE(start_entry.GetStringValue("payload", &string_value)); | |
311 EXPECT_EQ(encoded_post_data, string_value); | |
312 | |
313 // We don't really care what the source_dependency value is, just making sure | |
314 // it's there. | |
315 EXPECT_TRUE(start_entry.params->HasKey("source_dependency")); | |
316 | |
317 // Check for expected log entries for the end phase. | |
318 const net::TestNetLogEntry& end_entry = entries[1]; | |
319 ASSERT_EQ(3u, end_entry.params->size()); | |
320 | |
321 int int_value; | |
322 EXPECT_TRUE(end_entry.GetIntegerValue("status", &int_value)); | |
323 EXPECT_EQ(net::URLRequestStatus::FAILED, int_value); | |
324 | |
325 EXPECT_TRUE(end_entry.GetIntegerValue("error", &int_value)); | |
326 EXPECT_EQ(kRequestErrorCode, int_value); | |
327 | |
328 // We don't really care what the source_dependency value is, just making sure | |
329 // it's there. | |
330 EXPECT_TRUE(end_entry.params->HasKey("source_dependency")); | |
331 } | |
332 | |
333 } // namespace safe_browsing | |
OLD | NEW |