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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/dns/host_cache.h"
#include <string>
#include "base/bind.h"
#include "base/callback.h"
#include "base/format_macros.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
const int kMaxCacheEntries = 10;
// Builds a key for |hostname|, defaulting the address family to unspecified.
HostCache::Key Key(const std::string& hostname) {
return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED, 0);
}
bool FoobarIndexIsOdd(const std::string& foobarx_com) {
return (foobarx_com[6] - '0') % 2 == 1;
}
} // namespace
TEST(HostCacheTest, Basic) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
HostCache cache(kMaxCacheEntries);
// Start at t=0.
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
EXPECT_EQ(0U, cache.size());
// Add an entry for "foobar.com" at t=0.
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key1, now)->error() == entry.error());
EXPECT_EQ(1U, cache.size());
// Advance to t=5.
now += base::TimeDelta::FromSeconds(5);
// Add an entry for "foobar2.com" at t=5.
EXPECT_FALSE(cache.Lookup(key2, now));
cache.Set(key2, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(2U, cache.size());
// Advance to t=9
now += base::TimeDelta::FromSeconds(4);
// Verify that the entries we added are still retrievable, and usable.
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now));
// Advance to t=10; key is now expired.
now += base::TimeDelta::FromSeconds(1);
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
// Update key1, so it is no longer expired.
cache.Set(key1, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(2U, cache.size());
// Both entries should still be retrievable and usable.
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
// Advance to t=20; both entries are now expired.
now += base::TimeDelta::FromSeconds(10);
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
}
// Try caching entries for a failed resolve attempt -- since we set the TTL of
// such entries to 0 it won't store, but it will kick out the previous result.
TEST(HostCacheTest, NoCacheZeroTTL) {
const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10);
const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0);
HostCache cache(kMaxCacheEntries);
// Set t=0.
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kFailureEntryTTL);
EXPECT_EQ(1U, cache.size());
// We disallow use of negative entries.
EXPECT_FALSE(cache.Lookup(key1, now));
// Now overwrite with a valid entry, and then overwrite with negative entry
// again -- the valid entry should be kicked out.
cache.Set(key1, entry, now, kSuccessEntryTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kFailureEntryTTL);
EXPECT_FALSE(cache.Lookup(key1, now));
}
// Try caching entries for a failed resolves for 10 seconds.
TEST(HostCacheTest, CacheNegativeEntry) {
const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(10);
HostCache cache(kMaxCacheEntries);
// Start at t=0.
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
EXPECT_EQ(0U, cache.size());
// Add an entry for "foobar.com" at t=0.
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kFailureEntryTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(1U, cache.size());
// Advance to t=5.
now += base::TimeDelta::FromSeconds(5);
// Add an entry for "foobar2.com" at t=5.
EXPECT_FALSE(cache.Lookup(key2, now));
cache.Set(key2, entry, now, kFailureEntryTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(2U, cache.size());
// Advance to t=9
now += base::TimeDelta::FromSeconds(4);
// Verify that the entries we added are still retrievable, and usable.
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
// Advance to t=10; key1 is now expired.
now += base::TimeDelta::FromSeconds(1);
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
// Update key1, so it is no longer expired.
cache.Set(key1, entry, now, kFailureEntryTTL);
// Re-uses existing entry storage.
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(2U, cache.size());
// Both entries should still be retrievable and usable.
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
// Advance to t=20; both entries are now expired.
now += base::TimeDelta::FromSeconds(10);
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
}
// Tests that the same hostname can be duplicated in the cache, so long as
// the address family differs.
TEST(HostCacheTest, AddressFamilyIsPartOfKey) {
const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10);
HostCache cache(kMaxCacheEntries);
// t=0.
base::TimeTicks now;
HostCache::Key key1("foobar.com", ADDRESS_FAMILY_UNSPECIFIED, 0);
HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, 0);
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
EXPECT_EQ(0U, cache.size());
// Add an entry for ("foobar.com", UNSPECIFIED) at t=0.
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kSuccessEntryTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(1U, cache.size());
// Add an entry for ("foobar.com", IPV4_ONLY) at t=0.
EXPECT_FALSE(cache.Lookup(key2, now));
cache.Set(key2, entry, now, kSuccessEntryTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(2U, cache.size());
// Even though the hostnames were the same, we should have two unique
// entries (because the address families differ).
EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now));
}
// Tests that the same hostname can be duplicated in the cache, so long as
// the HostResolverFlags differ.
TEST(HostCacheTest, HostResolverFlagsArePartOfKey) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
HostCache cache(kMaxCacheEntries);
// t=0.
base::TimeTicks now;
HostCache::Key key1("foobar.com", ADDRESS_FAMILY_IPV4, 0);
HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4,
HOST_RESOLVER_CANONNAME);
HostCache::Key key3("foobar.com", ADDRESS_FAMILY_IPV4,
HOST_RESOLVER_LOOPBACK_ONLY);
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
EXPECT_EQ(0U, cache.size());
// Add an entry for ("foobar.com", IPV4, NONE) at t=0.
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(1U, cache.size());
// Add an entry for ("foobar.com", IPV4, CANONNAME) at t=0.
EXPECT_FALSE(cache.Lookup(key2, now));
cache.Set(key2, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(2U, cache.size());
// Add an entry for ("foobar.com", IPV4, LOOPBACK_ONLY) at t=0.
EXPECT_FALSE(cache.Lookup(key3, now));
cache.Set(key3, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key3, now));
EXPECT_EQ(3U, cache.size());
// Even though the hostnames were the same, we should have two unique
// entries (because the HostResolverFlags differ).
EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now));
EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key3, now));
EXPECT_NE(cache.Lookup(key2, now), cache.Lookup(key3, now));
}
TEST(HostCacheTest, NoCache) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
// Disable caching.
HostCache cache(0);
EXPECT_TRUE(cache.caching_is_disabled());
// Set t=0.
base::TimeTicks now;
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
// Lookup and Set should have no effect.
EXPECT_FALSE(cache.Lookup(Key("foobar.com"),now));
cache.Set(Key("foobar.com"), entry, now, kTTL);
EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now));
EXPECT_EQ(0U, cache.size());
}
TEST(HostCacheTest, Clear) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
HostCache cache(kMaxCacheEntries);
// Set t=0.
base::TimeTicks now;
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
EXPECT_EQ(0u, cache.size());
// Add three entries.
cache.Set(Key("foobar1.com"), entry, now, kTTL);
cache.Set(Key("foobar2.com"), entry, now, kTTL);
cache.Set(Key("foobar3.com"), entry, now, kTTL);
EXPECT_EQ(3u, cache.size());
cache.clear();
EXPECT_EQ(0u, cache.size());
}
TEST(HostCacheTest, ClearForHosts) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
HostCache cache(kMaxCacheEntries);
// Set t=0.
base::TimeTicks now;
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
EXPECT_EQ(0u, cache.size());
// Add several entries.
cache.Set(Key("foobar1.com"), entry, now, kTTL);
cache.Set(Key("foobar2.com"), entry, now, kTTL);
cache.Set(Key("foobar3.com"), entry, now, kTTL);
cache.Set(Key("foobar4.com"), entry, now, kTTL);
cache.Set(Key("foobar5.com"), entry, now, kTTL);
EXPECT_EQ(5u, cache.size());
// Clear the hosts matching a certain predicate, such as the number being odd.
cache.ClearForHosts(base::Bind(&FoobarIndexIsOdd));
EXPECT_EQ(2u, cache.size());
EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now));
EXPECT_TRUE(cache.Lookup(Key("foobar4.com"), now));
// Passing null callback will delete all hosts.
cache.ClearForHosts(base::Callback<bool(const std::string&)>());
EXPECT_EQ(0u, cache.size());
}
// Try to add too many entries to cache; it should evict the one with the oldest
// expiration time.
TEST(HostCacheTest, Evict) {
HostCache cache(2);
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Key key3 = Key("foobar3.com");
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
EXPECT_EQ(0u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
// |key1| expires in 10 seconds, but |key2| in just 5.
cache.Set(key1, entry, now, base::TimeDelta::FromSeconds(10));
cache.Set(key2, entry, now, base::TimeDelta::FromSeconds(5));
EXPECT_EQ(2u, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
// |key2| should be chosen for eviction, since it expires sooner.
cache.Set(key3, entry, now, base::TimeDelta::FromSeconds(10));
EXPECT_EQ(2u, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_TRUE(cache.Lookup(key3, now));
}
void TestEvictionCallback(int* evict_count,
HostCache::Key* key_out,
const HostCache::Key& key,
const HostCache::Entry& entry) {
++*evict_count;
*key_out = key;
}
// Try to add too many entries to cache; it should evict the one with the oldest
// expiration time.
TEST(HostCacheTest, EvictWithCallback) {
HostCache cache(2);
int evict_count = 0;
HostCache::Key evicted_key = Key("nothingevicted.com");
cache.set_eviction_callback(
base::Bind(&TestEvictionCallback, &evict_count, &evicted_key));
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Key key3 = Key("foobar3.com");
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
EXPECT_EQ(0u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
// |key1| expires in 10 seconds, but |key2| in just 5.
cache.Set(key1, entry, now, base::TimeDelta::FromSeconds(10));
cache.Set(key2, entry, now, base::TimeDelta::FromSeconds(5));
EXPECT_EQ(2u, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
EXPECT_EQ(0, evict_count);
// |key2| should be chosen for eviction, since it expires sooner.
cache.Set(key3, entry, now, base::TimeDelta::FromSeconds(10));
EXPECT_EQ(2u, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_TRUE(cache.Lookup(key3, now));
EXPECT_EQ(1, evict_count);
EXPECT_EQ(key2.hostname, evicted_key.hostname);
}
// Try to retrieve stale entries from the cache. They should be returned by
// |LookupStale()| but not |Lookup()|, with correct |EntryStaleness| data.
TEST(HostCacheTest, Stale) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
HostCache cache(kMaxCacheEntries);
// Start at t=0.
base::TimeTicks now;
HostCache::EntryStaleness stale;
HostCache::Key key = Key("foobar.com");
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
EXPECT_EQ(0U, cache.size());
// Add an entry for "foobar.com" at t=0.
EXPECT_FALSE(cache.Lookup(key, now));
EXPECT_FALSE(cache.LookupStale(key, now, &stale));
cache.Set(key, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key, now));
EXPECT_TRUE(cache.LookupStale(key, now, &stale));
EXPECT_FALSE(stale.is_stale());
EXPECT_EQ(0, stale.stale_hits);
EXPECT_EQ(1U, cache.size());
// Advance to t=5.
now += base::TimeDelta::FromSeconds(5);
EXPECT_TRUE(cache.Lookup(key, now));
EXPECT_TRUE(cache.LookupStale(key, now, &stale));
EXPECT_FALSE(stale.is_stale());
EXPECT_EQ(0, stale.stale_hits);
// Advance to t=15.
now += base::TimeDelta::FromSeconds(10);
EXPECT_FALSE(cache.Lookup(key, now));
EXPECT_TRUE(cache.LookupStale(key, now, &stale));
EXPECT_TRUE(stale.is_stale());
EXPECT_EQ(base::TimeDelta::FromSeconds(5), stale.expired_by);
EXPECT_EQ(0, stale.network_changes);
EXPECT_EQ(1, stale.stale_hits);
// Advance to t=20.
now += base::TimeDelta::FromSeconds(5);
EXPECT_FALSE(cache.Lookup(key, now));
EXPECT_TRUE(cache.LookupStale(key, now, &stale));
EXPECT_TRUE(stale.is_stale());
EXPECT_EQ(base::TimeDelta::FromSeconds(10), stale.expired_by);
EXPECT_EQ(0, stale.network_changes);
EXPECT_EQ(2, stale.stale_hits);
// Simulate network change.
cache.OnNetworkChange();
EXPECT_FALSE(cache.Lookup(key, now));
EXPECT_TRUE(cache.LookupStale(key, now, &stale));
EXPECT_TRUE(stale.is_stale());
EXPECT_EQ(base::TimeDelta::FromSeconds(10), stale.expired_by);
EXPECT_EQ(1, stale.network_changes);
EXPECT_EQ(3, stale.stale_hits);
}
TEST(HostCacheTest, EvictStale) {
HostCache cache(2);
base::TimeTicks now;
HostCache::EntryStaleness stale;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Key key3 = Key("foobar3.com");
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
EXPECT_EQ(0u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
// |key1| expires in 10 seconds.
cache.Set(key1, entry, now, base::TimeDelta::FromSeconds(10));
EXPECT_EQ(1u, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
// Simulate network change, expiring the cache.
cache.OnNetworkChange();
EXPECT_EQ(1u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.LookupStale(key1, now, &stale));
EXPECT_EQ(1, stale.network_changes);
// Advance to t=1.
now += base::TimeDelta::FromSeconds(1);
// |key2| expires before |key1| would originally have expired.
cache.Set(key2, entry, now, base::TimeDelta::FromSeconds(5));
EXPECT_EQ(2u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.LookupStale(key1, now, &stale));
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
// |key1| should be chosen for eviction, since it is stale.
cache.Set(key3, entry, now, base::TimeDelta::FromSeconds(1));
EXPECT_EQ(2u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.LookupStale(key1, now, &stale));
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_TRUE(cache.Lookup(key3, now));
// Advance to t=6.
now += base::TimeDelta::FromSeconds(5);
// Insert |key1| again. |key3| should be evicted.
cache.Set(key1, entry, now, base::TimeDelta::FromSeconds(10));
EXPECT_EQ(2u, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_TRUE(cache.LookupStale(key2, now, &stale));
EXPECT_FALSE(cache.Lookup(key3, now));
EXPECT_FALSE(cache.LookupStale(key3, now, &stale));
}
// Tests the less than and equal operators for HostCache::Key work.
TEST(HostCacheTest, KeyComparators) {
struct {
// Inputs.
HostCache::Key key1;
HostCache::Key key2;
// Expectation.
// -1 means key1 is less than key2
// 0 means key1 equals key2
// 1 means key1 is greater than key2
int expected_comparison;
} tests[] = {
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
0
},
{
HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0),
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0),
-1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0),
-1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0),
HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0),
1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
HostCache::Key("host2", ADDRESS_FAMILY_IPV4, 0),
-1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED,
HOST_RESOLVER_CANONNAME),
-1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED,
HOST_RESOLVER_CANONNAME),
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED,
HOST_RESOLVER_CANONNAME),
HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED,
HOST_RESOLVER_CANONNAME),
-1
},
};
for (size_t i = 0; i < arraysize(tests); ++i) {
SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]", i));
const HostCache::Key& key1 = tests[i].key1;
const HostCache::Key& key2 = tests[i].key2;
switch (tests[i].expected_comparison) {
case -1:
EXPECT_TRUE(key1 < key2);
EXPECT_FALSE(key2 < key1);
break;
case 0:
EXPECT_FALSE(key1 < key2);
EXPECT_FALSE(key2 < key1);
break;
case 1:
EXPECT_FALSE(key1 < key2);
EXPECT_TRUE(key2 < key1);
break;
default:
FAIL() << "Invalid expectation. Can be only -1, 0, 1";
}
}
}
} // namespace net
|