static void SetupCacheEntry(LookupCacheV2* aLookupCache, const nsCString& aCompletion, bool aNegExpired = false, bool aPosExpired = false) { AddCompleteArray completes; AddCompleteArray emptyCompletes; MissPrefixArray misses; MissPrefixArray emptyMisses; AddComplete* add = completes.AppendElement(fallible); add->complete.FromPlaintext(aCompletion); Prefix* prefix = misses.AppendElement(fallible); prefix->FromPlaintext(aCompletion); // Setup positive cache first otherwise negative cache expiry will be // overwritten. int64_t posExpirySec = aPosExpired ? EXPIRED_TIME_SEC : NOTEXPIRED_TIME_SEC; aLookupCache->AddGethashResultToCache(completes, emptyMisses, posExpirySec); int64_t negExpirySec = aNegExpired ? EXPIRED_TIME_SEC : NOTEXPIRED_TIME_SEC; aLookupCache->AddGethashResultToCache(emptyCompletes, misses, negExpirySec); }
static void SetupCacheEntry(LookupCacheV4* aLookupCache, const nsCString& aCompletion, bool aNegExpired = false, bool aPosExpired = false) { FullHashResponseMap map; Prefix prefix; prefix.FromPlaintext(aCompletion); CachedFullHashResponse* response = map.LookupOrAdd(prefix.ToUint32()); response->negativeCacheExpirySec = aNegExpired ? EXPIRED_TIME_SEC : NOTEXPIRED_TIME_SEC; response->fullHashes.Put(GeneratePrefix(aCompletion, COMPLETE_SIZE), aPosExpired ? EXPIRED_TIME_SEC : NOTEXPIRED_TIME_SEC); aLookupCache->AddFullHashResponseToCache(map); }
// 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()); }