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