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

Unified Diff: components/safe_browsing_db/database_manager_unittest.cc

Issue 2233103002: Move full hash caching logic to v4_get_hash_protocol_manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move another test from database_manager to get_hash_manager Created 4 years, 3 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 92cdacf066fa2197443e6282d77692657f5649d2..0d6296083e096234b00548e5bf8c544dec209dc5 100644
--- a/components/safe_browsing_db/database_manager_unittest.cc
+++ b/components/safe_browsing_db/database_manager_unittest.cc
@@ -156,24 +156,6 @@ TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlWrongScheme) {
EXPECT_TRUE(db_manager_->CheckApiBlacklistUrl(url, &client));
}
-TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlPrefixes) {
- TestClient client;
- const GURL url("https://www.example.com/more");
- // Generated from the sorted output of UrlToFullHashes in util.h.
- std::vector<SBPrefix> expected_prefixes =
- {1237562338, 2871045197, 3553205461, 3766933875};
-
- TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>(
- db_manager_->v4_get_hash_protocol_manager_);
- EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
- base::RunLoop().RunUntilIdle();
- std::vector<SBPrefix> prefixes = pm->GetRequestPrefixes();
- EXPECT_EQ(expected_prefixes.size(), prefixes.size());
- for (unsigned int i = 0; i < prefixes.size(); ++i) {
- EXPECT_EQ(expected_prefixes[i], prefixes[i]);
- }
-}
-
TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResults) {
TestClient client;
const GURL url("https://www.example.com/more");
@@ -259,282 +241,4 @@ TEST_F(SafeBrowsingDatabaseManagerTest, CancelApiCheck) {
EXPECT_FALSE(client.callback_invoked());
}
-TEST_F(SafeBrowsingDatabaseManagerTest, ResultsAreCached) {
- TestClient client;
- const GURL url("https://www.example.com/more");
- TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>(
- db_manager_->v4_get_hash_protocol_manager_);
- base::Time now = base::Time::UnixEpoch();
- SBFullHashResult full_hash_result;
- full_hash_result.hash = SBFullHashForString("example.com/");
- full_hash_result.metadata.api_permissions.insert("GEOLOCATION");
- full_hash_result.cache_expire_after = now + base::TimeDelta::FromMinutes(3);
- pm->AddGetFullHashResponse(full_hash_result);
- pm->SetNegativeCacheDurationMins(now, 5);
-
- EXPECT_TRUE(db_manager_->v4_full_hash_cache_.empty());
- EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
- base::RunLoop().RunUntilIdle();
-
- EXPECT_TRUE(client.callback_invoked());
- const std::set<std::string>& permissions = client.GetBlockedPermissions();
- EXPECT_EQ(1ul, permissions.size());
- EXPECT_EQ(1ul, permissions.count("GEOLOCATION"));
-
- // Check the cache.
- const SafeBrowsingDatabaseManager::PrefixToFullHashResultsMap& cache =
- db_manager_->v4_full_hash_cache_[SB_THREAT_TYPE_API_ABUSE];
- // Generated from the sorted output of UrlToFullHashes in util.h.
- std::vector<SBPrefix> expected_prefixes =
- {1237562338, 2871045197, 3553205461, 3766933875};
- EXPECT_EQ(expected_prefixes.size(),
- db_manager_->v4_full_hash_cache_[SB_THREAT_TYPE_API_ABUSE].size());
-
- auto entry = cache.find(expected_prefixes[0]);
- EXPECT_NE(cache.end(), entry);
- EXPECT_EQ(now + base::TimeDelta::FromMinutes(5), entry->second.expire_after);
- EXPECT_EQ(0ul, entry->second.full_hashes.size());
-
- entry = cache.find(expected_prefixes[1]);
- EXPECT_NE(cache.end(), entry);
- EXPECT_EQ(now + base::TimeDelta::FromMinutes(5), entry->second.expire_after);
- EXPECT_EQ(0ul, entry->second.full_hashes.size());
-
- entry = cache.find(expected_prefixes[2]);
- EXPECT_NE(cache.end(), entry);
- EXPECT_EQ(now + base::TimeDelta::FromMinutes(5), entry->second.expire_after);
- EXPECT_EQ(0ul, entry->second.full_hashes.size());
-
- entry = cache.find(expected_prefixes[3]);
- EXPECT_NE(cache.end(), entry);
- EXPECT_EQ(now + base::TimeDelta::FromMinutes(5), 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(1ul, entry->second.full_hashes[0].metadata.api_permissions.size());
- EXPECT_EQ(1ul, entry->second.full_hashes[0].metadata.api_permissions.
- count("GEOLOCATION"));
- EXPECT_EQ(full_hash_result.cache_expire_after,
- entry->second.full_hashes[0].cache_expire_after);
-}
-
-// An uninitialized value for negative cache expire does not cache results.
-TEST_F(SafeBrowsingDatabaseManagerTest, ResultsAreNotCachedOnNull) {
- TestClient client;
- const GURL url("https://www.example.com/more");
- TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>(
- db_manager_->v4_get_hash_protocol_manager_);
- base::Time now = base::Time::UnixEpoch();
- SBFullHashResult full_hash_result;
- full_hash_result.hash = SBFullHashForString("example.com/");
- full_hash_result.cache_expire_after = now + base::TimeDelta::FromMinutes(3);
- pm->AddGetFullHashResponse(full_hash_result);
-
- EXPECT_TRUE(db_manager_->v4_full_hash_cache_.empty());
- EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
- base::RunLoop().RunUntilIdle();
-
- EXPECT_TRUE(client.callback_invoked());
- EXPECT_TRUE(
- db_manager_->v4_full_hash_cache_[SB_THREAT_TYPE_API_ABUSE].empty());
-}
-
-// Checks that results are looked up correctly in the cache.
-TEST_F(SafeBrowsingDatabaseManagerTest, GetCachedResults) {
- base::Time now = base::Time::UnixEpoch();
- std::vector<SBFullHash> full_hashes;
- SBFullHash full_hash = SBFullHashForString("example.com/");
- full_hashes.push_back(full_hash);
- std::vector<SBFullHashResult> cached_results;
- std::vector<SBPrefix> prefixes;
- db_manager_->GetFullHashCachedResults(SB_THREAT_TYPE_API_ABUSE,
- full_hashes, now, &prefixes, &cached_results);
-
- // The cache is empty.
- EXPECT_TRUE(cached_results.empty());
- EXPECT_EQ(1ul, prefixes.size());
- EXPECT_EQ(full_hash.prefix, prefixes[0]);
-
- // Prefix has a cache entry but full hash is not there.
- SBCachedFullHashResult& entry = db_manager_->
- v4_full_hash_cache_[SB_THREAT_TYPE_API_ABUSE][full_hash.prefix] =
- SBCachedFullHashResult(now + base::TimeDelta::FromMinutes(5));
- db_manager_->GetFullHashCachedResults(SB_THREAT_TYPE_API_ABUSE,
- full_hashes, now, &prefixes, &cached_results);
-
- EXPECT_TRUE(prefixes.empty());
- EXPECT_TRUE(cached_results.empty());
-
- // Expired negative cache entry.
- entry.expire_after = now - base::TimeDelta::FromMinutes(5);
- db_manager_->GetFullHashCachedResults(SB_THREAT_TYPE_API_ABUSE,
- full_hashes, now, &prefixes, &cached_results);
-
- EXPECT_TRUE(cached_results.empty());
- EXPECT_EQ(1ul, prefixes.size());
- EXPECT_EQ(full_hash.prefix, prefixes[0]);
-
- // Now put the full hash in the cache.
- SBFullHashResult full_hash_result;
- full_hash_result.hash = full_hash;
- full_hash_result.cache_expire_after = now + base::TimeDelta::FromMinutes(3);
- entry.full_hashes.push_back(full_hash_result);
- db_manager_->GetFullHashCachedResults(SB_THREAT_TYPE_API_ABUSE,
- full_hashes, now, &prefixes, &cached_results);
-
- EXPECT_TRUE(prefixes.empty());
- EXPECT_EQ(1ul, cached_results.size());
- EXPECT_TRUE(SBFullHashEqual(full_hash, cached_results[0].hash));
-
- // Expired full hash in cache.
- entry.full_hashes.clear();
- full_hash_result.cache_expire_after = now - base::TimeDelta::FromMinutes(3);
- entry.full_hashes.push_back(full_hash_result);
- db_manager_->GetFullHashCachedResults(SB_THREAT_TYPE_API_ABUSE,
- full_hashes, now, &prefixes, &cached_results);
-
- EXPECT_TRUE(cached_results.empty());
- EXPECT_EQ(1ul, prefixes.size());
- EXPECT_EQ(full_hash.prefix, prefixes[0]);
-}
-
-// Checks that the cached results and request results are merged.
-TEST_F(SafeBrowsingDatabaseManagerTest, CachedResultsMerged) {
- TestClient client;
- const GURL url("https://www.example.com/more");
- TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>(
- db_manager_->v4_get_hash_protocol_manager_);
- // Set now to max time so the cache expire times are in the future.
- SBFullHashResult full_hash_result;
- full_hash_result.hash = SBFullHashForString("example.com/");
- full_hash_result.metadata.api_permissions.insert("GEOLOCATION");
- full_hash_result.cache_expire_after = base::Time::Max();
- pm->AddGetFullHashResponse(full_hash_result);
- pm->SetNegativeCacheDurationMins(base::Time::Max(), 0);
-
- EXPECT_TRUE(db_manager_->v4_full_hash_cache_.empty());
- EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
- base::RunLoop().RunUntilIdle();
-
- EXPECT_TRUE(client.callback_invoked());
- const std::set<std::string>& permissions = client.GetBlockedPermissions();
- EXPECT_EQ(1ul, permissions.size());
- EXPECT_EQ(1ul, permissions.count("GEOLOCATION"));
-
- // The results should be cached, so remove them from the protocol manager
- // response.
- TestClient client2;
- pm->ClearFullHashResponse();
- pm->SetNegativeCacheDurationMins(base::Time(), 0);
- EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client2));
- base::RunLoop().RunUntilIdle();
-
- EXPECT_TRUE(client2.callback_invoked());
- const std::set<std::string>& permissions2 =
- client2.GetBlockedPermissions();
- EXPECT_EQ(1ul, permissions2.size());
- EXPECT_EQ(1ul, permissions2.count("GEOLOCATION"));
-
- // Add a different result to the protocol manager response and ensure it is
- // merged with the cached result in the metadata.
- TestClient client3;
- const GURL url2("https://m.example.com/more");
- full_hash_result.hash = SBFullHashForString("m.example.com/");
- full_hash_result.metadata.api_permissions.insert("NOTIFICATIONS");
- pm->AddGetFullHashResponse(full_hash_result);
- pm->SetNegativeCacheDurationMins(base::Time::Max(), 0);
- EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url2, &client3));
- base::RunLoop().RunUntilIdle();
-
- EXPECT_TRUE(client3.callback_invoked());
- const std::set<std::string>& permissions3 =
- client3.GetBlockedPermissions();
- EXPECT_EQ(2ul, permissions3.size());
- EXPECT_EQ(1ul, permissions3.count("GEOLOCATION"));
- EXPECT_EQ(1ul, permissions3.count("NOTIFICATIONS"));
-}
-
-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;
-
- SafeBrowsingDatabaseManager::PrefixToFullHashResultsMap& cache =
- db_manager_->v4_full_hash_cache_[SB_THREAT_TYPE_API_ABUSE];
-
- // Fill the cache with some expired entries.
- // Both negative cache and full hash expired.
- cache[full_hash_result.hash.prefix] = SBCachedFullHashResult(epoch);
- 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, cache.size());
- EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
- base::RunLoop().RunUntilIdle();
-
- // Cache should be empty.
- EXPECT_TRUE(client.callback_invoked());
- EXPECT_TRUE(cache.empty());
-
- // Negative cache still valid and full hash expired.
- cache[full_hash_result.hash.prefix] =
- SBCachedFullHashResult(base::Time::Max());
- cache[full_hash_result.hash.prefix].full_hashes.push_back(full_hash_result);
-
- EXPECT_EQ(1ul, cache.size());
- EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
- base::RunLoop().RunUntilIdle();
-
- // Cache entry should still be there.
- EXPECT_EQ(1ul, cache.size());
- auto entry = cache.find(full_hash_result.hash.prefix);
- EXPECT_NE(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.
- cache[full_hash_result.hash.prefix].full_hashes[0].
- cache_expire_after = base::Time::Max();
-
- EXPECT_EQ(1ul, cache.size());
- EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
- base::RunLoop().RunUntilIdle();
-
- // Cache entry should still be there.
- EXPECT_EQ(1ul, cache.size());
- entry = cache.find(full_hash_result.hash.prefix);
- EXPECT_NE(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.
- cache[full_hash_result.hash.prefix].expire_after = epoch;
-
- EXPECT_EQ(1ul, cache.size());
- EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
- base::RunLoop().RunUntilIdle();
-
- // Cache entry should still be there.
- EXPECT_EQ(1ul, cache.size());
- entry = cache.find(full_hash_result.hash.prefix);
- EXPECT_NE(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