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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 } | 87 } |
88 | 88 |
89 int GetNumReportsPerDay() { | 89 int GetNumReportsPerDay() { |
90 return csd_service_->GetNumReportsPerDay(); | 90 return csd_service_->GetNumReportsPerDay(); |
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 TestUpdateCache() { | |
98 ClientSideDetectionService::PhishingCache& cache = csd_service_->cache_; | |
99 base::Time now = base::Time::Now(); | |
100 base::Time time = base::Time::Now() - | |
101 ClientSideDetectionService::kNegativeCacheInterval + | |
102 base::TimeDelta::FromMinutes(2); | |
Brian Ryner
2011/02/08 20:16:48
Did you mean to use the "now" variable here instea
gcasto (DO NOT USE)
2011/02/09 00:10:52
Done.
| |
103 cache[GURL("http://first.url.com/")] = | |
104 make_linked_ptr(new ClientSideDetectionService::CacheState(false, | |
105 time)); | |
106 | |
107 time = now - ClientSideDetectionService::kNegativeCacheInterval - | |
108 base::TimeDelta::FromHours(1); | |
109 cache[GURL("http://second.url.com/")] = | |
110 make_linked_ptr(new ClientSideDetectionService::CacheState(false, | |
111 time)); | |
112 | |
113 time = now - ClientSideDetectionService::kPositiveCacheInterval - | |
114 base::TimeDelta::FromMinutes(5); | |
115 cache[GURL("http://third.url.com/")] = | |
116 make_linked_ptr(new ClientSideDetectionService::CacheState(true, time)); | |
117 | |
118 time = now - ClientSideDetectionService::kPositiveCacheInterval + | |
119 base::TimeDelta::FromMinutes(5); | |
120 cache[GURL("http://fourth.url.com/")] = | |
121 make_linked_ptr(new ClientSideDetectionService::CacheState(true, time)); | |
122 | |
123 LOG(INFO) << "Size:" << cache.size(); | |
Brian Ryner
2011/02/08 20:16:48
Did you mean to leave this in?
gcasto (DO NOT USE)
2011/02/09 00:10:52
Removed.
| |
124 csd_service_->UpdateCache(); | |
125 | |
126 // Only 2 elements should be in the cache, the first and the fourth. | |
127 EXPECT_EQ(2U, cache.size()); | |
128 EXPECT_NE(cache.find(GURL("http://first.url.com/")), cache.end()); | |
129 EXPECT_NE(cache.find(GURL("http://fourth.url.com/")), cache.end()); | |
130 } | |
131 | |
97 protected: | 132 protected: |
98 scoped_ptr<ClientSideDetectionService> csd_service_; | 133 scoped_ptr<ClientSideDetectionService> csd_service_; |
99 scoped_ptr<FakeURLFetcherFactory> factory_; | 134 scoped_ptr<FakeURLFetcherFactory> factory_; |
100 MessageLoop msg_loop_; | 135 MessageLoop msg_loop_; |
101 | 136 |
102 private: | 137 private: |
103 void GetModelFileDone(base::PlatformFile model_file) { | 138 void GetModelFileDone(base::PlatformFile model_file) { |
104 model_file_ = model_file; | 139 model_file_ = model_file; |
105 msg_loop_.Quit(); | 140 msg_loop_.Quit(); |
106 } | 141 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 // Invalid response body from the server. | 216 // Invalid response body from the server. |
182 SetClientReportPhishingResponse("invalid proto response", true /* success */); | 217 SetClientReportPhishingResponse("invalid proto response", true /* success */); |
183 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); | 218 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); |
184 | 219 |
185 // Normal behavior. | 220 // Normal behavior. |
186 ClientPhishingResponse response; | 221 ClientPhishingResponse response; |
187 response.set_phishy(true); | 222 response.set_phishy(true); |
188 SetClientReportPhishingResponse(response.SerializeAsString(), | 223 SetClientReportPhishingResponse(response.SerializeAsString(), |
189 true /* success */); | 224 true /* success */); |
190 EXPECT_TRUE(SendClientReportPhishingRequest(url, score)); | 225 EXPECT_TRUE(SendClientReportPhishingRequest(url, score)); |
226 | |
227 // Caching causes this to still count as phishy. | |
191 response.set_phishy(false); | 228 response.set_phishy(false); |
192 SetClientReportPhishingResponse(response.SerializeAsString(), | 229 SetClientReportPhishingResponse(response.SerializeAsString(), |
193 true /* success */); | 230 true /* success */); |
194 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); | 231 EXPECT_TRUE(SendClientReportPhishingRequest(url, score)); |
232 | |
233 // This request will fail and should not be cached. | |
234 GURL second_url("http://b.com/"); | |
235 response.set_phishy(false); | |
236 SetClientReportPhishingResponse(response.SerializeAsString(), | |
237 false /* success*/); | |
238 EXPECT_FALSE(SendClientReportPhishingRequest(second_url, score)); | |
239 | |
240 // Verify that the previous request was not cached. | |
241 response.set_phishy(true); | |
242 SetClientReportPhishingResponse(response.SerializeAsString(), | |
243 true /* success */); | |
244 EXPECT_TRUE(SendClientReportPhishingRequest(second_url, score)); | |
245 | |
246 // This request is blocked because it's not in the cache and we have more | |
247 // than 3 requests. | |
248 GURL third_url("http://c.com"); | |
249 response.set_phishy(true); | |
250 SetClientReportPhishingResponse(response.SerializeAsString(), | |
251 true /* success */); | |
252 EXPECT_FALSE(SendClientReportPhishingRequest(third_url, score)); | |
253 | |
254 // Verify that caching still works even when new requests are blocked. | |
255 response.set_phishy(true); | |
256 SetClientReportPhishingResponse(response.SerializeAsString(), | |
257 true /* success */); | |
258 EXPECT_TRUE(SendClientReportPhishingRequest(url, score)); | |
195 | 259 |
196 base::Time after = base::Time::Now(); | 260 base::Time after = base::Time::Now(); |
197 | 261 |
198 // Check that we have recorded 3 requests, all within the correct time range. | 262 // Check that we have recorded 4 requests, all within the correct time range. |
263 // The blocked request and the cached requests should not be present. | |
199 std::queue<base::Time>& report_times = GetPhishingReportTimes(); | 264 std::queue<base::Time>& report_times = GetPhishingReportTimes(); |
200 EXPECT_EQ(3U, report_times.size()); | 265 EXPECT_EQ(4U, report_times.size()); |
201 while (!report_times.empty()) { | 266 while (!report_times.empty()) { |
202 base::Time time = report_times.back(); | 267 base::Time time = report_times.back(); |
203 report_times.pop(); | 268 report_times.pop(); |
204 EXPECT_LE(before, time); | 269 EXPECT_LE(before, time); |
205 EXPECT_GE(after, time); | 270 EXPECT_GE(after, time); |
206 } | 271 } |
207 } | 272 } |
208 | 273 |
209 TEST_F(ClientSideDetectionServiceTest, GetNumReportTest) { | 274 TEST_F(ClientSideDetectionServiceTest, GetNumReportTest) { |
210 SetModelFetchResponse("bogus model", true /* success */); | 275 SetModelFetchResponse("bogus model", true /* success */); |
211 ScopedTempDir tmp_dir; | 276 ScopedTempDir tmp_dir; |
212 ASSERT_TRUE(tmp_dir.CreateUniqueTempDir()); | 277 ASSERT_TRUE(tmp_dir.CreateUniqueTempDir()); |
213 csd_service_.reset(ClientSideDetectionService::Create( | 278 csd_service_.reset(ClientSideDetectionService::Create( |
214 tmp_dir.path().AppendASCII("model"), NULL)); | 279 tmp_dir.path().AppendASCII("model"), NULL)); |
215 | 280 |
216 std::queue<base::Time>& report_times = GetPhishingReportTimes(); | 281 std::queue<base::Time>& report_times = GetPhishingReportTimes(); |
217 base::Time now = base::Time::Now(); | 282 base::Time now = base::Time::Now(); |
218 base::TimeDelta twenty_five_hours = base::TimeDelta::FromHours(25); | 283 base::TimeDelta twenty_five_hours = base::TimeDelta::FromHours(25); |
219 report_times.push(now - twenty_five_hours); | 284 report_times.push(now - twenty_five_hours); |
220 report_times.push(now - twenty_five_hours); | 285 report_times.push(now - twenty_five_hours); |
221 report_times.push(now); | 286 report_times.push(now); |
222 report_times.push(now); | 287 report_times.push(now); |
223 | 288 |
224 EXPECT_EQ(2, GetNumReportsPerDay()); | 289 EXPECT_EQ(2, GetNumReportsPerDay()); |
225 } | 290 } |
226 | 291 |
292 TEST_F(ClientSideDetectionServiceTest, UpdateCacheTest) { | |
293 SetModelFetchResponse("bogus model", true /* success */); | |
294 ScopedTempDir tmp_dir; | |
295 ASSERT_TRUE(tmp_dir.CreateUniqueTempDir()); | |
296 csd_service_.reset(ClientSideDetectionService::Create( | |
297 tmp_dir.path().AppendASCII("model"), NULL)); | |
298 | |
299 TestUpdateCache(); | |
300 } | |
301 | |
227 } // namespace safe_browsing | 302 } // namespace safe_browsing |
OLD | NEW |