Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5084)

Unified Diff: chrome/browser/safe_browsing/client_side_detection_service_unittest.cc

Issue 6374017: Add caching to phishing client side detection. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Add UMA stat for not being able to serialize request. Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/safe_browsing/client_side_detection_service.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/safe_browsing/client_side_detection_service_unittest.cc
diff --git a/chrome/browser/safe_browsing/client_side_detection_service_unittest.cc b/chrome/browser/safe_browsing/client_side_detection_service_unittest.cc
index b418d14a6e4de6fc90c6b4f9204e0aba1c50eb19..75139169f1690f3c9d98aa2898c78ede3e249079 100644
--- a/chrome/browser/safe_browsing/client_side_detection_service_unittest.cc
+++ b/chrome/browser/safe_browsing/client_side_detection_service_unittest.cc
@@ -86,14 +86,66 @@ class ClientSideDetectionServiceTest : public testing::Test {
response_data, success);
}
- int GetNumReportsPerDay() {
- return csd_service_->GetNumReportsPerDay();
+ int GetNumReports() {
+ return csd_service_->GetNumReports();
}
std::queue<base::Time>& GetPhishingReportTimes() {
return csd_service_->phishing_report_times_;
}
+ void SetCache(const GURL& gurl, bool is_phishing, base::Time time) {
+ csd_service_->cache_[gurl] =
+ make_linked_ptr(new ClientSideDetectionService::CacheState(is_phishing,
+ time));
+ }
+
+ void TestCache() {
+ ClientSideDetectionService::PhishingCache& cache = csd_service_->cache_;
+ base::Time now = base::Time::Now();
+ base::Time time = now - ClientSideDetectionService::kNegativeCacheInterval +
+ base::TimeDelta::FromMinutes(5);
+ cache[GURL("http://first.url.com/")] =
+ make_linked_ptr(new ClientSideDetectionService::CacheState(false,
+ time));
+
+ time = now - ClientSideDetectionService::kNegativeCacheInterval -
+ base::TimeDelta::FromHours(1);
+ cache[GURL("http://second.url.com/")] =
+ make_linked_ptr(new ClientSideDetectionService::CacheState(false,
+ time));
+
+ time = now - ClientSideDetectionService::kPositiveCacheInterval -
+ base::TimeDelta::FromMinutes(5);
+ cache[GURL("http://third.url.com/")] =
+ make_linked_ptr(new ClientSideDetectionService::CacheState(true, time));
+
+ time = now - ClientSideDetectionService::kPositiveCacheInterval +
+ base::TimeDelta::FromMinutes(5);
+ cache[GURL("http://fourth.url.com/")] =
+ make_linked_ptr(new ClientSideDetectionService::CacheState(true, time));
+
+ csd_service_->UpdateCache();
+
+ // 3 elements should be in the cache, the first, third, and fourth.
+ EXPECT_EQ(3U, cache.size());
+ EXPECT_NE(cache.find(GURL("http://first.url.com/")), cache.end());
+ EXPECT_NE(cache.find(GURL("http://third.url.com/")), cache.end());
+ EXPECT_NE(cache.find(GURL("http://fourth.url.com/")), cache.end());
+
+ // While 3 elements remain, only the first and the fourth are actually
+ // valid.
+ bool is_phishing;
+ EXPECT_TRUE(csd_service_->GetCachedResult(GURL("http://first.url.com"),
+ &is_phishing));
+ EXPECT_FALSE(is_phishing);
+ EXPECT_FALSE(csd_service_->GetCachedResult(GURL("http://third.url.com"),
+ &is_phishing));
+ EXPECT_TRUE(csd_service_->GetCachedResult(GURL("http://fourth.url.com"),
+ &is_phishing));
+ EXPECT_TRUE(is_phishing);
+ }
+
protected:
scoped_ptr<ClientSideDetectionService> csd_service_;
scoped_ptr<FakeURLFetcherFactory> factory_;
@@ -188,16 +240,57 @@ TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) {
SetClientReportPhishingResponse(response.SerializeAsString(),
true /* success */);
EXPECT_TRUE(SendClientReportPhishingRequest(url, score));
+
+ // Caching causes this to still count as phishy.
response.set_phishy(false);
SetClientReportPhishingResponse(response.SerializeAsString(),
true /* success */);
- EXPECT_FALSE(SendClientReportPhishingRequest(url, score));
+ EXPECT_TRUE(SendClientReportPhishingRequest(url, score));
+
+ // This request will fail and should not be cached.
+ GURL second_url("http://b.com/");
+ response.set_phishy(false);
+ SetClientReportPhishingResponse(response.SerializeAsString(),
+ false /* success*/);
+ EXPECT_FALSE(SendClientReportPhishingRequest(second_url, score));
+
+ // Verify that the previous request was not cached.
+ response.set_phishy(true);
+ SetClientReportPhishingResponse(response.SerializeAsString(),
+ true /* success */);
+ EXPECT_TRUE(SendClientReportPhishingRequest(second_url, score));
+
+ // This request is blocked because it's not in the cache and we have more
+ // than 3 requests.
+ GURL third_url("http://c.com");
+ response.set_phishy(true);
+ SetClientReportPhishingResponse(response.SerializeAsString(),
+ true /* success */);
+ EXPECT_FALSE(SendClientReportPhishingRequest(third_url, score));
+
+ // Verify that caching still works even when new requests are blocked.
+ response.set_phishy(true);
+ SetClientReportPhishingResponse(response.SerializeAsString(),
+ true /* success */);
+ EXPECT_TRUE(SendClientReportPhishingRequest(url, score));
+
+ // Verify that we allow cache refreshing even when requests are blocked.
+ base::Time cache_time = base::Time::Now() - base::TimeDelta::FromHours(1);
+ SetCache(second_url, true, cache_time);
+
+ // Even though this element is in the cache, it's not currently valid so
+ // we make request and return that value instead.
+ response.set_phishy(false);
+ SetClientReportPhishingResponse(response.SerializeAsString(),
+ true /* success */);
+ EXPECT_FALSE(SendClientReportPhishingRequest(second_url, score));
base::Time after = base::Time::Now();
- // Check that we have recorded 3 requests, all within the correct time range.
+ // Check that we have recorded 5 requests, all within the correct time range.
+ // The blocked request and the cached requests should not be present.
std::queue<base::Time>& report_times = GetPhishingReportTimes();
- EXPECT_EQ(3U, report_times.size());
+ EXPECT_EQ(5U, report_times.size());
while (!report_times.empty()) {
base::Time time = report_times.back();
report_times.pop();
@@ -221,7 +314,17 @@ TEST_F(ClientSideDetectionServiceTest, GetNumReportTest) {
report_times.push(now);
report_times.push(now);
- EXPECT_EQ(2, GetNumReportsPerDay());
+ EXPECT_EQ(2, GetNumReports());
+}
+
+TEST_F(ClientSideDetectionServiceTest, CacheTest) {
+ SetModelFetchResponse("bogus model", true /* success */);
+ ScopedTempDir tmp_dir;
+ ASSERT_TRUE(tmp_dir.CreateUniqueTempDir());
+ csd_service_.reset(ClientSideDetectionService::Create(
+ tmp_dir.path().AppendASCII("model"), NULL));
+
+ TestCache();
}
} // namespace safe_browsing
« no previous file with comments | « chrome/browser/safe_browsing/client_side_detection_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698