OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include <map> | 5 #include <map> |
6 #include <queue> | 6 #include <queue> |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 response_data, success); | 79 response_data, success); |
80 } | 80 } |
81 | 81 |
82 void SetClientReportPhishingResponse(std::string response_data, | 82 void SetClientReportPhishingResponse(std::string response_data, |
83 bool success) { | 83 bool success) { |
84 factory_->SetFakeResponse( | 84 factory_->SetFakeResponse( |
85 ClientSideDetectionService::kClientReportPhishingUrl, | 85 ClientSideDetectionService::kClientReportPhishingUrl, |
86 response_data, success); | 86 response_data, success); |
87 } | 87 } |
88 | 88 |
89 int GetNumReportsPerDay() { | 89 int GetNumReports() { |
90 return csd_service_->GetNumReportsPerDay(); | 90 return csd_service_->GetNumReports(); |
91 } | 91 } |
92 | 92 |
93 std::queue<base::Time>& GetPhishingReportTimes() { | 93 std::queue<base::Time>& GetPhishingReportTimes() { |
94 return csd_service_->phishing_report_times_; | 94 return csd_service_->phishing_report_times_; |
95 } | 95 } |
96 | 96 |
| 97 void SetCache(const GURL& gurl, bool is_phishing, base::Time time) { |
| 98 csd_service_->cache_[gurl] = |
| 99 make_linked_ptr(new ClientSideDetectionService::CacheState(is_phishing, |
| 100 time)); |
| 101 } |
| 102 |
| 103 void TestCache() { |
| 104 ClientSideDetectionService::PhishingCache& cache = csd_service_->cache_; |
| 105 base::Time now = base::Time::Now(); |
| 106 base::Time time = now - ClientSideDetectionService::kNegativeCacheInterval + |
| 107 base::TimeDelta::FromMinutes(5); |
| 108 cache[GURL("http://first.url.com/")] = |
| 109 make_linked_ptr(new ClientSideDetectionService::CacheState(false, |
| 110 time)); |
| 111 |
| 112 time = now - ClientSideDetectionService::kNegativeCacheInterval - |
| 113 base::TimeDelta::FromHours(1); |
| 114 cache[GURL("http://second.url.com/")] = |
| 115 make_linked_ptr(new ClientSideDetectionService::CacheState(false, |
| 116 time)); |
| 117 |
| 118 time = now - ClientSideDetectionService::kPositiveCacheInterval - |
| 119 base::TimeDelta::FromMinutes(5); |
| 120 cache[GURL("http://third.url.com/")] = |
| 121 make_linked_ptr(new ClientSideDetectionService::CacheState(true, time)); |
| 122 |
| 123 time = now - ClientSideDetectionService::kPositiveCacheInterval + |
| 124 base::TimeDelta::FromMinutes(5); |
| 125 cache[GURL("http://fourth.url.com/")] = |
| 126 make_linked_ptr(new ClientSideDetectionService::CacheState(true, time)); |
| 127 |
| 128 csd_service_->UpdateCache(); |
| 129 |
| 130 // 3 elements should be in the cache, the first, third, and fourth. |
| 131 EXPECT_EQ(3U, cache.size()); |
| 132 EXPECT_NE(cache.find(GURL("http://first.url.com/")), cache.end()); |
| 133 EXPECT_NE(cache.find(GURL("http://third.url.com/")), cache.end()); |
| 134 EXPECT_NE(cache.find(GURL("http://fourth.url.com/")), cache.end()); |
| 135 |
| 136 // While 3 elements remain, only the first and the fourth are actually |
| 137 // valid. |
| 138 bool is_phishing; |
| 139 EXPECT_TRUE(csd_service_->GetCachedResult(GURL("http://first.url.com"), |
| 140 &is_phishing)); |
| 141 EXPECT_FALSE(is_phishing); |
| 142 EXPECT_FALSE(csd_service_->GetCachedResult(GURL("http://third.url.com"), |
| 143 &is_phishing)); |
| 144 EXPECT_TRUE(csd_service_->GetCachedResult(GURL("http://fourth.url.com"), |
| 145 &is_phishing)); |
| 146 EXPECT_TRUE(is_phishing); |
| 147 } |
| 148 |
97 protected: | 149 protected: |
98 scoped_ptr<ClientSideDetectionService> csd_service_; | 150 scoped_ptr<ClientSideDetectionService> csd_service_; |
99 scoped_ptr<FakeURLFetcherFactory> factory_; | 151 scoped_ptr<FakeURLFetcherFactory> factory_; |
100 MessageLoop msg_loop_; | 152 MessageLoop msg_loop_; |
101 | 153 |
102 private: | 154 private: |
103 void GetModelFileDone(base::PlatformFile model_file) { | 155 void GetModelFileDone(base::PlatformFile model_file) { |
104 model_file_ = model_file; | 156 model_file_ = model_file; |
105 msg_loop_.Quit(); | 157 msg_loop_.Quit(); |
106 } | 158 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 // Invalid response body from the server. | 233 // Invalid response body from the server. |
182 SetClientReportPhishingResponse("invalid proto response", true /* success */); | 234 SetClientReportPhishingResponse("invalid proto response", true /* success */); |
183 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); | 235 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); |
184 | 236 |
185 // Normal behavior. | 237 // Normal behavior. |
186 ClientPhishingResponse response; | 238 ClientPhishingResponse response; |
187 response.set_phishy(true); | 239 response.set_phishy(true); |
188 SetClientReportPhishingResponse(response.SerializeAsString(), | 240 SetClientReportPhishingResponse(response.SerializeAsString(), |
189 true /* success */); | 241 true /* success */); |
190 EXPECT_TRUE(SendClientReportPhishingRequest(url, score)); | 242 EXPECT_TRUE(SendClientReportPhishingRequest(url, score)); |
| 243 |
| 244 // Caching causes this to still count as phishy. |
191 response.set_phishy(false); | 245 response.set_phishy(false); |
192 SetClientReportPhishingResponse(response.SerializeAsString(), | 246 SetClientReportPhishingResponse(response.SerializeAsString(), |
193 true /* success */); | 247 true /* success */); |
194 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); | 248 EXPECT_TRUE(SendClientReportPhishingRequest(url, score)); |
| 249 |
| 250 // This request will fail and should not be cached. |
| 251 GURL second_url("http://b.com/"); |
| 252 response.set_phishy(false); |
| 253 SetClientReportPhishingResponse(response.SerializeAsString(), |
| 254 false /* success*/); |
| 255 EXPECT_FALSE(SendClientReportPhishingRequest(second_url, score)); |
| 256 |
| 257 // Verify that the previous request was not cached. |
| 258 response.set_phishy(true); |
| 259 SetClientReportPhishingResponse(response.SerializeAsString(), |
| 260 true /* success */); |
| 261 EXPECT_TRUE(SendClientReportPhishingRequest(second_url, score)); |
| 262 |
| 263 // This request is blocked because it's not in the cache and we have more |
| 264 // than 3 requests. |
| 265 GURL third_url("http://c.com"); |
| 266 response.set_phishy(true); |
| 267 SetClientReportPhishingResponse(response.SerializeAsString(), |
| 268 true /* success */); |
| 269 EXPECT_FALSE(SendClientReportPhishingRequest(third_url, score)); |
| 270 |
| 271 // Verify that caching still works even when new requests are blocked. |
| 272 response.set_phishy(true); |
| 273 SetClientReportPhishingResponse(response.SerializeAsString(), |
| 274 true /* success */); |
| 275 EXPECT_TRUE(SendClientReportPhishingRequest(url, score)); |
| 276 |
| 277 // Verify that we allow cache refreshing even when requests are blocked. |
| 278 base::Time cache_time = base::Time::Now() - base::TimeDelta::FromHours(1); |
| 279 SetCache(second_url, true, cache_time); |
| 280 |
| 281 // Even though this element is in the cache, it's not currently valid so |
| 282 // we make request and return that value instead. |
| 283 response.set_phishy(false); |
| 284 SetClientReportPhishingResponse(response.SerializeAsString(), |
| 285 true /* success */); |
| 286 EXPECT_FALSE(SendClientReportPhishingRequest(second_url, score)); |
195 | 287 |
196 base::Time after = base::Time::Now(); | 288 base::Time after = base::Time::Now(); |
197 | 289 |
198 // Check that we have recorded 3 requests, all within the correct time range. | 290 // Check that we have recorded 5 requests, all within the correct time range. |
| 291 // The blocked request and the cached requests should not be present. |
199 std::queue<base::Time>& report_times = GetPhishingReportTimes(); | 292 std::queue<base::Time>& report_times = GetPhishingReportTimes(); |
200 EXPECT_EQ(3U, report_times.size()); | 293 EXPECT_EQ(5U, report_times.size()); |
201 while (!report_times.empty()) { | 294 while (!report_times.empty()) { |
202 base::Time time = report_times.back(); | 295 base::Time time = report_times.back(); |
203 report_times.pop(); | 296 report_times.pop(); |
204 EXPECT_LE(before, time); | 297 EXPECT_LE(before, time); |
205 EXPECT_GE(after, time); | 298 EXPECT_GE(after, time); |
206 } | 299 } |
207 } | 300 } |
208 | 301 |
209 TEST_F(ClientSideDetectionServiceTest, GetNumReportTest) { | 302 TEST_F(ClientSideDetectionServiceTest, GetNumReportTest) { |
210 SetModelFetchResponse("bogus model", true /* success */); | 303 SetModelFetchResponse("bogus model", true /* success */); |
211 ScopedTempDir tmp_dir; | 304 ScopedTempDir tmp_dir; |
212 ASSERT_TRUE(tmp_dir.CreateUniqueTempDir()); | 305 ASSERT_TRUE(tmp_dir.CreateUniqueTempDir()); |
213 csd_service_.reset(ClientSideDetectionService::Create( | 306 csd_service_.reset(ClientSideDetectionService::Create( |
214 tmp_dir.path().AppendASCII("model"), NULL)); | 307 tmp_dir.path().AppendASCII("model"), NULL)); |
215 | 308 |
216 std::queue<base::Time>& report_times = GetPhishingReportTimes(); | 309 std::queue<base::Time>& report_times = GetPhishingReportTimes(); |
217 base::Time now = base::Time::Now(); | 310 base::Time now = base::Time::Now(); |
218 base::TimeDelta twenty_five_hours = base::TimeDelta::FromHours(25); | 311 base::TimeDelta twenty_five_hours = base::TimeDelta::FromHours(25); |
219 report_times.push(now - twenty_five_hours); | 312 report_times.push(now - twenty_five_hours); |
220 report_times.push(now - twenty_five_hours); | 313 report_times.push(now - twenty_five_hours); |
221 report_times.push(now); | 314 report_times.push(now); |
222 report_times.push(now); | 315 report_times.push(now); |
223 | 316 |
224 EXPECT_EQ(2, GetNumReportsPerDay()); | 317 EXPECT_EQ(2, GetNumReports()); |
| 318 } |
| 319 |
| 320 TEST_F(ClientSideDetectionServiceTest, CacheTest) { |
| 321 SetModelFetchResponse("bogus model", true /* success */); |
| 322 ScopedTempDir tmp_dir; |
| 323 ASSERT_TRUE(tmp_dir.CreateUniqueTempDir()); |
| 324 csd_service_.reset(ClientSideDetectionService::Create( |
| 325 tmp_dir.path().AppendASCII("model"), NULL)); |
| 326 |
| 327 TestCache(); |
225 } | 328 } |
226 | 329 |
227 } // namespace safe_browsing | 330 } // namespace safe_browsing |
OLD | NEW |