void TestCache(const Completion aCompletion, bool aExpectedHas, bool aExpectedConfirmed, bool aExpectedInCache, T* aCache = nullptr) { bool has, inCache, confirmed; uint32_t matchLength; if (aCache) { aCache->Has(aCompletion, &has, &matchLength, &confirmed); inCache = aCache->IsInCache(aCompletion.ToUint32()); } else { _PrefixArray array = { GeneratePrefix(_Fragment("cache.notexpired.com/"), 10), GeneratePrefix(_Fragment("cache.expired.com/"), 8), GeneratePrefix(_Fragment("gound.com/"), 5), GeneratePrefix(_Fragment("small.com/"), 4) }; UniquePtr<T> cache = SetupLookupCache<T>(array); // Create an expired entry and a non-expired entry SetupCacheEntry(cache.get(), _Fragment("cache.notexpired.com/")); SetupCacheEntry(cache.get(), _Fragment("cache.expired.com/"), true, true); cache->Has(aCompletion, &has, &matchLength, &confirmed); inCache = cache->IsInCache(aCompletion.ToUint32()); } EXPECT_EQ(has, aExpectedHas); EXPECT_EQ(confirmed, aExpectedConfirmed); EXPECT_EQ(inCache, aExpectedInCache); }
// This testcase check if an cache entry whose negative cache time is expired // and it doesn't have any postive cache entries in it, it should be removed // from cache after calling |Has|. TEST(UrlClassifierCaching, NegativeCacheExpireV2) { _PrefixArray array = { GeneratePrefix(NEG_CACHE_EXPIRED_URL, 8) }; UniquePtr<LookupCacheV2> cache = SetupLookupCache<LookupCacheV2>(array); nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID); MissPrefixArray misses; Prefix* prefix = misses.AppendElement(fallible); prefix->FromPlaintext(NEG_CACHE_EXPIRED_URL); AddCompleteArray dummy; cache->AddGethashResultToCache(dummy, misses, EXPIRED_TIME_SEC); // Ensure it is in cache in the first place. EXPECT_EQ(cache->IsInCache(prefix->ToUint32()), true); // It should be removed after calling Has API. TestCache<LookupCacheV2>(NEG_CACHE_EXPIRED_URL, true, false, false, cache.get()); }
TEST(UrlClassifierCaching, NegativeCacheExpireV4) { _PrefixArray array = { GeneratePrefix(NEG_CACHE_EXPIRED_URL, 8) }; UniquePtr<LookupCacheV4> cache = SetupLookupCache<LookupCacheV4>(array); FullHashResponseMap map; Prefix prefix; nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID); prefix.FromPlaintext(NEG_CACHE_EXPIRED_URL); CachedFullHashResponse* response = map.LookupOrAdd(prefix.ToUint32()); response->negativeCacheExpirySec = EXPIRED_TIME_SEC; cache->AddFullHashResponseToCache(map); // Ensure it is in cache in the first place. EXPECT_EQ(cache->IsInCache(prefix.ToUint32()), true); // It should be removed after calling Has API. TestCache<LookupCacheV4>(NEG_CACHE_EXPIRED_URL, true, false, false, cache.get()); }