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