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

Unified Diff: components/safe_browsing_db/database_manager_unittest.cc

Issue 2009133005: SafeBrowsing: Implement cache eviction for API full hash results. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@osb-cache
Patch Set: Resolve merge conflicts Created 4 years, 7 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
Index: components/safe_browsing_db/database_manager_unittest.cc
diff --git a/components/safe_browsing_db/database_manager_unittest.cc b/components/safe_browsing_db/database_manager_unittest.cc
index e47165df9d73378562f243c7621ef7f055834677..2eab970b486230f20190365b510b0d779f54ab31 100644
--- a/components/safe_browsing_db/database_manager_unittest.cc
+++ b/components/safe_browsing_db/database_manager_unittest.cc
@@ -450,4 +450,89 @@ TEST_F(SafeBrowsingDatabaseManagerTest, CachedResultsMerged) {
EXPECT_EQ("GEOLOCATION", permissions3[2]);
}
+TEST_F(SafeBrowsingDatabaseManagerTest, CachedResultsAreEvicted) {
+ base::Time epoch = base::Time::UnixEpoch();
+ SBFullHashResult full_hash_result;
+ full_hash_result.hash = SBFullHashForString("example.com/");
+ full_hash_result.cache_expire_after = epoch;
+
+ // Fill the cache with some expired entries.
+ // Both negative cache and full hash expired.
+ db_manager_->v4_full_hash_cache_[full_hash_result.hash.prefix] =
+ SBCachedFullHashResult(epoch);
+ db_manager_->v4_full_hash_cache_[full_hash_result.hash.prefix].
+ full_hashes.push_back(full_hash_result);
+
+ TestClient client;
+ const GURL url("https://www.example.com/more");
+
+ EXPECT_EQ(1ul, db_manager_->v4_full_hash_cache_.size());
+ EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
+ base::RunLoop().RunUntilIdle();
+
+ // Cache should be empty.
+ EXPECT_TRUE(client.callback_invoked());
+ EXPECT_TRUE(db_manager_->v4_full_hash_cache_.empty());
+
+ // Negative cache still valid and full hash expired.
+ db_manager_->v4_full_hash_cache_[full_hash_result.hash.prefix] =
+ SBCachedFullHashResult(base::Time::Max());
+ db_manager_->v4_full_hash_cache_[full_hash_result.hash.prefix].
+ full_hashes.push_back(full_hash_result);
+
+ EXPECT_EQ(1ul, db_manager_->v4_full_hash_cache_.size());
+ EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
+ base::RunLoop().RunUntilIdle();
+
+ // Cache entry should still be there.
+ EXPECT_EQ(1ul, db_manager_->v4_full_hash_cache_.size());
+ auto entry = db_manager_->v4_full_hash_cache_.
+ find(full_hash_result.hash.prefix);
+ EXPECT_NE(db_manager_->v4_full_hash_cache_.end(), entry);
+ EXPECT_EQ(base::Time::Max(), entry->second.expire_after);
+ EXPECT_EQ(1ul, entry->second.full_hashes.size());
+ EXPECT_TRUE(SBFullHashEqual(full_hash_result.hash,
+ entry->second.full_hashes[0].hash));
+ EXPECT_EQ(full_hash_result.cache_expire_after,
+ entry->second.full_hashes[0].cache_expire_after);
+
+ // Negative cache still valid and full hash still valid.
+ db_manager_->v4_full_hash_cache_[full_hash_result.hash.prefix].full_hashes[0].
+ cache_expire_after = base::Time::Max();
+
+ EXPECT_EQ(1ul, db_manager_->v4_full_hash_cache_.size());
+ EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
+ base::RunLoop().RunUntilIdle();
+
+ // Cache entry should still be there.
+ EXPECT_EQ(1ul, db_manager_->v4_full_hash_cache_.size());
+ entry = db_manager_->v4_full_hash_cache_.find(full_hash_result.hash.prefix);
+ EXPECT_NE(db_manager_->v4_full_hash_cache_.end(), entry);
+ EXPECT_EQ(base::Time::Max(), entry->second.expire_after);
+ EXPECT_EQ(1ul, entry->second.full_hashes.size());
+ EXPECT_TRUE(SBFullHashEqual(full_hash_result.hash,
+ entry->second.full_hashes[0].hash));
+ EXPECT_EQ(base::Time::Max(),
+ entry->second.full_hashes[0].cache_expire_after);
+
+ // Negative cache expired and full hash still valid.
+ db_manager_->v4_full_hash_cache_[full_hash_result.hash.prefix].expire_after =
+ epoch;
+
+ EXPECT_EQ(1ul, db_manager_->v4_full_hash_cache_.size());
+ EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
+ base::RunLoop().RunUntilIdle();
+
+ // Cache entry should still be there.
+ EXPECT_EQ(1ul, db_manager_->v4_full_hash_cache_.size());
+ entry = db_manager_->v4_full_hash_cache_.find(full_hash_result.hash.prefix);
+ EXPECT_NE(db_manager_->v4_full_hash_cache_.end(), entry);
+ EXPECT_EQ(epoch, entry->second.expire_after);
+ EXPECT_EQ(1ul, entry->second.full_hashes.size());
+ EXPECT_TRUE(SBFullHashEqual(full_hash_result.hash,
+ entry->second.full_hashes[0].hash));
+ EXPECT_EQ(base::Time::Max(),
+ entry->second.full_hashes[0].cache_expire_after);
+}
+
} // namespace safe_browsing

Powered by Google App Engine
This is Rietveld 408576698