summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/gtest/TestCaching.cpp
blob: 520eb35dcbb5c256853d8ad9b6a52dbbde8a57bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Common.h"
#include "LookupCacheV4.h"

#define EXPIRED_TIME_SEC (PR_Now() / PR_USEC_PER_SEC - 3600)
#define NOTEXPIRED_TIME_SEC (PR_Now() / PR_USEC_PER_SEC + 3600)

#define CACHED_URL "cache.com/"_ns
#define NEG_CACHE_EXPIRED_URL "cache.negExpired.com/"_ns
#define POS_CACHE_EXPIRED_URL "cache.posExpired.com/"_ns
#define BOTH_CACHE_EXPIRED_URL "cache.negAndposExpired.com/"_ns

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(mozilla::fallible);
  add->complete.FromPlaintext(aCompletion);

  Prefix* prefix = misses.AppendElement(mozilla::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.GetOrInsertNew(prefix.ToUint32());

  response->negativeCacheExpirySec =
      aNegExpired ? EXPIRED_TIME_SEC : NOTEXPIRED_TIME_SEC;
  response->fullHashes.InsertOrUpdate(
      CreatePrefixFromURL(aCompletion, COMPLETE_SIZE),
      aPosExpired ? EXPIRED_TIME_SEC : NOTEXPIRED_TIME_SEC);

  aLookupCache->AddFullHashResponseToCache(map);
}

template <typename T>
static 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 = {CreatePrefixFromURL("cache.notexpired.com/", 10),
                          CreatePrefixFromURL("cache.expired.com/", 8),
                          CreatePrefixFromURL("gound.com/", 5),
                          CreatePrefixFromURL("small.com/", 4)};

    RefPtr<T> cache = SetupLookupCache<T>(array);

    // Create an expired entry and a non-expired entry
    SetupCacheEntry(cache, "cache.notexpired.com/"_ns);
    SetupCacheEntry(cache, "cache.expired.com/"_ns, 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);
}

template <typename T>
static void TestCache(const nsCString& aURL, bool aExpectedHas,
                      bool aExpectedConfirmed, bool aExpectedInCache,
                      T* aCache = nullptr) {
  Completion lookupHash;
  lookupHash.FromPlaintext(aURL);

  TestCache<T>(lookupHash, aExpectedHas, aExpectedConfirmed, aExpectedInCache,
               aCache);
}

// This testcase check the returned result of |Has| API if fullhash cannot match
// any prefix in the local database.
TEST(UrlClassifierCaching, NotFound)
{
  TestCache<LookupCacheV2>("nomatch.com/"_ns, false, false, false);
  TestCache<LookupCacheV4>("nomatch.com/"_ns, false, false, false);
}

// This testcase check the returned result of |Has| API if fullhash find a match
// in the local database but not in the cache.
TEST(UrlClassifierCaching, NotInCache)
{
  TestCache<LookupCacheV2>("gound.com/"_ns, true, false, false);
  TestCache<LookupCacheV4>("gound.com/"_ns, true, false, false);
}

// This testcase check the returned result of |Has| API if fullhash matches
// a cache entry in positive cache.
TEST(UrlClassifierCaching, InPositiveCacheNotExpired)
{
  TestCache<LookupCacheV2>("cache.notexpired.com/"_ns, true, true, true);
  TestCache<LookupCacheV4>("cache.notexpired.com/"_ns, true, true, true);
}

// This testcase check the returned result of |Has| API if fullhash matches
// a cache entry in positive cache but that it is expired.
TEST(UrlClassifierCaching, InPositiveCacheExpired)
{
  TestCache<LookupCacheV2>("cache.expired.com/"_ns, true, false, true);
  TestCache<LookupCacheV4>("cache.expired.com/"_ns, true, false, true);
}

// This testcase check the returned result of |Has| API if fullhash matches
// a cache entry in negative cache.
TEST(UrlClassifierCaching, InNegativeCacheNotExpired)
{
  // Create a fullhash whose prefix matches the prefix in negative cache
  // but completion doesn't match any fullhash in positive cache.

  Completion prefix;
  prefix.FromPlaintext("cache.notexpired.com/"_ns);

  Completion fullhash;
  fullhash.FromPlaintext("firefox.com/"_ns);

  // Overwrite the 4-byte prefix of `fullhash` so that it conflicts with
  // `prefix`. Since "cache.notexpired.com" is added to database in TestCache as
  // a 10-byte prefix, we should copy more than 10 bytes to fullhash to ensure
  // it can match the prefix in database.
  memcpy(fullhash.buf, prefix.buf, 10);

  TestCache<LookupCacheV2>(fullhash, false, false, true);
  TestCache<LookupCacheV4>(fullhash, false, false, true);
}

// This testcase check the returned result of |Has| API if fullhash matches
// a cache entry in negative cache but that entry is expired.
TEST(UrlClassifierCaching, InNegativeCacheExpired)
{
  // Create a fullhash whose prefix is in the cache.

  Completion prefix;
  prefix.FromPlaintext("cache.expired.com/"_ns);

  Completion fullhash;
  fullhash.FromPlaintext("firefox.com/"_ns);

  memcpy(fullhash.buf, prefix.buf, 10);

  TestCache<LookupCacheV2>(fullhash, true, false, true);
  TestCache<LookupCacheV4>(fullhash, true, false, true);
}

// This testcase create 4 cache entries.
// 1. unexpired entry.
// 2. an entry whose negative cache time is expired but whose positive cache
//    is not expired.
// 3. an entry whose positive cache time is expired
// 4. an entry whose negative cache time and positive cache time are expired
// After calling |InvalidateExpiredCacheEntry| API, entries with expired
// negative time should be removed from cache(2 & 4)
template <typename T>
void TestInvalidateExpiredCacheEntry() {
  _PrefixArray array = {CreatePrefixFromURL(CACHED_URL, 10),
                        CreatePrefixFromURL(NEG_CACHE_EXPIRED_URL, 8),
                        CreatePrefixFromURL(POS_CACHE_EXPIRED_URL, 5),
                        CreatePrefixFromURL(BOTH_CACHE_EXPIRED_URL, 4)};
  RefPtr<T> cache = SetupLookupCache<T>(array);

  SetupCacheEntry(cache, CACHED_URL, false, false);
  SetupCacheEntry(cache, NEG_CACHE_EXPIRED_URL, true, false);
  SetupCacheEntry(cache, POS_CACHE_EXPIRED_URL, false, true);
  SetupCacheEntry(cache, 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());
}

TEST(UrlClassifierCaching, InvalidateExpiredCacheEntryV2)
{ TestInvalidateExpiredCacheEntry<LookupCacheV2>(); }

TEST(UrlClassifierCaching, InvalidateExpiredCacheEntryV4)
{ TestInvalidateExpiredCacheEntry<LookupCacheV4>(); }

// 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 = {CreatePrefixFromURL(NEG_CACHE_EXPIRED_URL, 8)};
  RefPtr<LookupCacheV2> cache = SetupLookupCache<LookupCacheV2>(array);

  nsCOMPtr<nsICryptoHash> cryptoHash =
      do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);

  MissPrefixArray misses;
  Prefix* prefix = misses.AppendElement(mozilla::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 = {CreatePrefixFromURL(NEG_CACHE_EXPIRED_URL, 8)};
  RefPtr<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.GetOrInsertNew(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());
}