nsresult Classifier::ReadNoiseEntries(const Prefix& aPrefix, const nsACString& aTableName, PRInt32 aCount, PrefixArray* aNoiseEntries) { LookupCache *cache = GetLookupCache(aTableName); if (!cache) { return NS_ERROR_FAILURE; } nsTArray<PRUint32> prefixes; nsresult rv = cache->GetPrefixes(&prefixes); NS_ENSURE_SUCCESS(rv, rv); PRInt32 idx = prefixes.BinaryIndexOf(aPrefix.ToUint32()); if (idx == nsTArray<PRUint32>::NoIndex) { NS_WARNING("Could not find prefix in PrefixSet during noise lookup"); return NS_ERROR_FAILURE; } idx -= idx % aCount; for (PRInt32 i = 0; (i < aCount) && ((idx+i) < prefixes.Length()); i++) { Prefix newPref; newPref.FromUint32(prefixes[idx+i]); aNoiseEntries->AppendElement(newPref); } return NS_OK; }
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()); }
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()); }