void TestHasPrefix(const _Fragment& aFragment, bool aExpectedHas, bool aExpectedComplete) { _PrefixArray array = { GeneratePrefix(_Fragment("bravo.com/"), 32), GeneratePrefix(_Fragment("browsing.com/"), 8), GeneratePrefix(_Fragment("gound.com/"), 5), GeneratePrefix(_Fragment("small.com/"), 4) }; RunTestInNewThread([&] () -> void { UniquePtr<LookupCache> cache = SetupLookupCache<LookupCacheV4>(array); Completion lookupHash; lookupHash.FromPlaintext(aFragment); bool has, confirmed; uint32_t matchLength; // Freshness is not used in V4 so we just put dummy values here. TableFreshnessMap dummy; nsresult rv = cache->Has(lookupHash, &has, &matchLength, &confirmed); EXPECT_EQ(rv, NS_OK); EXPECT_EQ(has, aExpectedHas); EXPECT_EQ(matchLength == COMPLETE_SIZE, aExpectedComplete); EXPECT_EQ(confirmed, false); cache->ClearAll(); }); }
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); }
void TestHasPrefix(const _Fragment& aFragment, bool aExpectedHas, bool aExpectedComplete) { _PrefixArray array = { GeneratePrefix(_Fragment("bravo.com/"), 32), GeneratePrefix(_Fragment("browsing.com/"), 8), GeneratePrefix(_Fragment("gound.com/"), 5), GeneratePrefix(_Fragment("small.com/"), 4) }; RunTestInNewThread([&] () -> void { UniquePtr<LookupCache> cache = SetupLookupCacheV4(array); Completion lookupHash; nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID); lookupHash.FromPlaintext(aFragment, cryptoHash); bool has, complete; nsresult rv = cache->Has(lookupHash, &has, &complete); EXPECT_EQ(rv, NS_OK); EXPECT_EQ(has, aExpectedHas); EXPECT_EQ(complete, aExpectedComplete); cache->ClearAll(); }); }
void TestInvalidateExpiredCacheEntry() { _PrefixArray array = { GeneratePrefix(CACHED_URL, 10), GeneratePrefix(NEG_CACHE_EXPIRED_URL, 8), GeneratePrefix(POS_CACHE_EXPIRED_URL, 5), GeneratePrefix(BOTH_CACHE_EXPIRED_URL, 4) }; UniquePtr<T> cache = SetupLookupCache<T>(array); SetupCacheEntry(cache.get(), CACHED_URL, false, false); SetupCacheEntry(cache.get(), NEG_CACHE_EXPIRED_URL, true, false); SetupCacheEntry(cache.get(), POS_CACHE_EXPIRED_URL, false, true); SetupCacheEntry(cache.get(), BOTH_CACHE_EXPIRED_URL, true, true); // Before invalidate TestCache<T>(CACHED_URL, true, true, true, cache.get()); TestCache<T>(NEG_CACHE_EXPIRED_URL, true, true, true, cache.get()); TestCache<T>(POS_CACHE_EXPIRED_URL, true, false, true, cache.get()); TestCache<T>(BOTH_CACHE_EXPIRED_URL, true, false, true, cache.get()); // Call InvalidateExpiredCacheEntry to remove cache entries whose negative cache // time is expired cache->InvalidateExpiredCacheEntries(); // After invalidate, NEG_CACHE_EXPIRED_URL & BOTH_CACHE_EXPIRED_URL should // not be found in cache. TestCache<T>(NEG_CACHE_EXPIRED_URL, true, false, false, cache.get()); TestCache<T>(BOTH_CACHE_EXPIRED_URL, true, false, false, cache.get()); // Other entries should remain the same result. TestCache<T>(CACHED_URL, true, true, true, cache.get()); TestCache<T>(POS_CACHE_EXPIRED_URL, true, false, true, 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()); }
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 AndroidOutputFunction(Severity severity, const char* functionName, unsigned int line, const char* msg) { std::string prefix = GeneratePrefix(message.prettyFunctionName, message.line); int sev = static_cast<int>(severity) + ANDROID_LOG_INFO; __android_log_write(sev, prefix.c_str(), msg); }