-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathcache_test.go
More file actions
837 lines (624 loc) · 25.7 KB
/
cache_test.go
File metadata and controls
837 lines (624 loc) · 25.7 KB
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
// SPDX-FileCopyrightText: The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package mdns
import (
"fmt"
"net/netip"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/dns/dnsmessage"
)
// ---------------------------------------------------------------------------
// Test clock — deterministic time for cache tests
// ---------------------------------------------------------------------------
type testClock struct {
current time.Time
}
func (tc *testClock) now() time.Time {
return tc.current
}
func (tc *testClock) advance(dur time.Duration) {
tc.current = tc.current.Add(dur)
}
func newTestClock() *testClock {
return &testClock{current: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)}
}
// ---------------------------------------------------------------------------
// Test helpers — build records without error handling boilerplate
// ---------------------------------------------------------------------------
func mustBuildA(t *testing.T, name, addr string, ttl uint32) dnsmessage.Resource {
t.Helper()
res, err := buildAResource(name, netip.MustParseAddr(addr), ttl)
require.NoError(t, err)
return res
}
func mustBuildAAAA(t *testing.T, name, addr string, ttl uint32) dnsmessage.Resource {
t.Helper()
res, err := buildAAAAResource(name, netip.MustParseAddr(addr), ttl)
require.NoError(t, err)
return res
}
func mustBuildPTR(t *testing.T, name, target string, ttl uint32) dnsmessage.Resource {
t.Helper()
res, err := buildPTRResource(name, target, ttl)
require.NoError(t, err)
return res
}
func mustBuildSRV(t *testing.T, name, target string, port uint16, ttl uint32) dnsmessage.Resource {
t.Helper()
res, err := buildSRVResource(name, target, port, 0, 0, ttl)
require.NoError(t, err)
return res
}
func mustBuildTXT(t *testing.T, name string, txts []string, ttl uint32) dnsmessage.Resource {
t.Helper()
res, err := buildTXTResource(name, txts, ttl)
require.NoError(t, err)
return res
}
// ---------------------------------------------------------------------------
// Basic insert/lookup — one test per record type
// ---------------------------------------------------------------------------
func TestCacheInsertLookupA(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
rec := mustBuildA(t, "host.local.", "192.168.1.1", 120)
ca.insert(rec, clock.now())
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
assert.Equal(t, dnsmessage.TypeA, results[0].Header.Type)
body, ok := results[0].Body.(*dnsmessage.AResource)
require.True(t, ok)
assert.Equal(t, [4]byte{192, 168, 1, 1}, body.A)
}
func TestCacheInsertLookupAAAA(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
rec := mustBuildAAAA(t, "v6host.local.", "fd00::1", 120)
ca.insert(rec, clock.now())
results := ca.lookup("v6host.local.", dnsmessage.TypeAAAA, dnsmessage.ClassINET)
require.Len(t, results, 1)
body, ok := results[0].Body.(*dnsmessage.AAAAResource)
require.True(t, ok)
assert.Equal(t, netip.MustParseAddr("fd00::1").As16(), body.AAAA)
}
func TestCacheInsertLookupPTR(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
rec := mustBuildPTR(t, "_http._tcp.local.", "My Web._http._tcp.local.", 4500)
ca.insert(rec, clock.now())
results := ca.lookup("_http._tcp.local.", dnsmessage.TypePTR, dnsmessage.ClassINET)
require.Len(t, results, 1)
target, err := parsePTRTarget(results[0].Body)
require.NoError(t, err)
assert.Equal(t, "My Web._http._tcp.local.", target)
}
func TestCacheInsertLookupSRV(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
rec := mustBuildSRV(t, "svc._http._tcp.local.", "myhost.local.", 8080, 120)
ca.insert(rec, clock.now())
results := ca.lookup("svc._http._tcp.local.", dnsmessage.TypeSRV, dnsmessage.ClassINET)
require.Len(t, results, 1)
target, port, _, _, err := parseSRVData(results[0].Body) //nolint:dogsled
require.NoError(t, err)
assert.Equal(t, "myhost.local.", target)
assert.Equal(t, uint16(8080), port)
}
func TestCacheInsertLookupTXT(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
rec := mustBuildTXT(t, "svc._http._tcp.local.", []string{"txtvers=1", "path=/"}, 4500)
ca.insert(rec, clock.now())
results := ca.lookup("svc._http._tcp.local.", dnsmessage.TypeTXT, dnsmessage.ClassINET)
require.Len(t, results, 1)
txts, err := parseTXTData(results[0].Body)
require.NoError(t, err)
assert.Equal(t, []string{"txtvers=1", "path=/"}, txts)
}
// ---------------------------------------------------------------------------
// TTL recalculation
// ---------------------------------------------------------------------------
func TestCacheLookupRemainingTTL(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
rec := mustBuildA(t, "host.local.", "192.168.1.1", 120)
ca.insert(rec, clock.now())
clock.advance(30 * time.Second)
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
assert.Equal(t, uint32(90), results[0].Header.TTL)
}
// ---------------------------------------------------------------------------
// Case-insensitive lookup
// ---------------------------------------------------------------------------
func TestCacheLookupCaseInsensitive(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
rec := mustBuildA(t, "Test.Local.", "10.0.0.1", 120)
ca.insert(rec, clock.now())
results := ca.lookup("test.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
body, ok := results[0].Body.(*dnsmessage.AResource)
require.True(t, ok)
assert.Equal(t, [4]byte{10, 0, 0, 1}, body.A)
// Also try uppercase lookup.
results = ca.lookup("TEST.LOCAL.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
body, ok = results[0].Body.(*dnsmessage.AResource)
require.True(t, ok)
assert.Equal(t, [4]byte{10, 0, 0, 1}, body.A)
}
// ---------------------------------------------------------------------------
// Multiple records per key
// ---------------------------------------------------------------------------
func TestCacheMultipleRecordsSameName(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 120), clock.now())
ca.insert(mustBuildA(t, "host.local.", "192.168.1.2", 120), clock.now())
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 2)
var ips [][4]byte
for _, res := range results {
body, ok := res.Body.(*dnsmessage.AResource)
require.True(t, ok)
ips = append(ips, body.A)
}
assert.ElementsMatch(t, [][4]byte{{192, 168, 1, 1}, {192, 168, 1, 2}}, ips)
}
func TestCacheMultiplePTRTargets(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildPTR(t, "_http._tcp.local.", "Web1._http._tcp.local.", 4500), clock.now())
ca.insert(mustBuildPTR(t, "_http._tcp.local.", "Web2._http._tcp.local.", 4500), clock.now())
results := ca.lookup("_http._tcp.local.", dnsmessage.TypePTR, dnsmessage.ClassINET)
require.Len(t, results, 2)
var targets []string
for _, res := range results {
target, err := parsePTRTarget(res.Body)
require.NoError(t, err)
targets = append(targets, target)
}
assert.ElementsMatch(t, []string{"Web1._http._tcp.local.", "Web2._http._tcp.local."}, targets)
}
// ---------------------------------------------------------------------------
// TTL expiration
// ---------------------------------------------------------------------------
func TestCacheTTLExpiration(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 120), clock.now())
clock.advance(120 * time.Second)
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
assert.Empty(t, results)
}
func TestCacheTTLValidBeforeExpiry(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 120), clock.now())
clock.advance(119 * time.Second)
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
assert.Equal(t, uint32(1), results[0].Header.TTL)
}
// ---------------------------------------------------------------------------
// Sweep
// ---------------------------------------------------------------------------
func TestCacheSweepRemovesExpired(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "gone.local.", "10.0.0.1", 60), clock.now())
ca.insert(mustBuildA(t, "alive.local.", "10.0.0.2", 300), clock.now())
clock.advance(120 * time.Second)
ca.sweep()
assert.Empty(t, ca.lookup("gone.local.", dnsmessage.TypeA, dnsmessage.ClassINET))
alive := ca.lookup("alive.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, alive, 1)
body, ok := alive[0].Body.(*dnsmessage.AResource)
require.True(t, ok)
assert.Equal(t, [4]byte{10, 0, 0, 2}, body.A)
}
func TestCacheSweepEmpty(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
// Should not panic or error.
ca.sweep()
assert.Equal(t, 0, ca.len())
}
// ---------------------------------------------------------------------------
// TTL update — same rdata, new TTL
// ---------------------------------------------------------------------------
func TestCacheTTLUpdate(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 60), clock.now())
clock.advance(10 * time.Second)
// Re-insert same rdata with new TTL.
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 120), clock.now())
// Should still be one entry, not two.
assert.Equal(t, 1, ca.len())
// Remaining TTL should be based on the new insert: 120s from t=10s.
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
assert.Equal(t, uint32(120), results[0].Header.TTL)
}
// ---------------------------------------------------------------------------
// Goodbye packets — RFC 6762 §10.1
// ---------------------------------------------------------------------------
func TestCacheGoodbyeExistingRecord(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 120), clock.now())
// Send goodbye (TTL=0).
goodbye := mustBuildA(t, "host.local.", "192.168.1.1", 0)
ca.insert(goodbye, clock.now())
// Still visible (expires in 1s).
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
body, ok := results[0].Body.(*dnsmessage.AResource)
require.True(t, ok)
assert.Equal(t, [4]byte{192, 168, 1, 1}, body.A)
}
func TestCacheGoodbyeExpiration(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 120), clock.now())
goodbye := mustBuildA(t, "host.local.", "192.168.1.1", 0)
ca.insert(goodbye, clock.now())
clock.advance(2 * time.Second)
// After 2s the goodbye entry should be expired.
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
assert.Empty(t, results)
}
func TestCacheGoodbyeUnknownRecord(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
// Goodbye for a record not in cache — should create a 1s entry.
goodbye := mustBuildA(t, "host.local.", "192.168.1.1", 0)
ca.insert(goodbye, clock.now())
assert.Equal(t, 1, ca.len())
clock.advance(2 * time.Second)
assert.Equal(t, 0, ca.len())
}
func TestCacheGoodbyeSelectiveByRdata(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 120), clock.now())
ca.insert(mustBuildA(t, "host.local.", "192.168.1.2", 120), clock.now())
// Goodbye only the first address.
goodbye := mustBuildA(t, "host.local.", "192.168.1.1", 0)
ca.insert(goodbye, clock.now())
clock.advance(2 * time.Second)
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
body, ok := results[0].Body.(*dnsmessage.AResource)
require.True(t, ok)
assert.Equal(t, [4]byte{192, 168, 1, 2}, body.A)
}
func TestCacheGoodbyeThenReinsert(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 120), clock.now())
// Goodbye shortens TTL to 1s.
goodbye := mustBuildA(t, "host.local.", "192.168.1.1", 0)
ca.insert(goodbye, clock.now())
// Fresh announcement before the goodbye expires revives the record.
clock.advance(500 * time.Millisecond)
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 300), clock.now())
// Well past the original goodbye expiry, record should still be alive.
clock.advance(5 * time.Second)
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
assert.Equal(t, uint32(295), results[0].Header.TTL)
}
// ---------------------------------------------------------------------------
// Cache-flush bit — RFC 6762 §10.2
// ---------------------------------------------------------------------------
func TestCacheFlushBitStripped(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
rec := mustBuildA(t, "host.local.", "192.168.1.1", 120)
rec.Header.Class |= rrClassCacheFlush
ca.insert(rec, clock.now())
// Lookup without the flush bit should find the record.
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
body, ok := results[0].Body.(*dnsmessage.AResource)
require.True(t, ok)
assert.Equal(t, [4]byte{192, 168, 1, 1}, body.A)
}
func TestCacheFlushOldEntries(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
// Insert old record at t=0.
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 300), clock.now())
clock.advance(2 * time.Second)
// Insert new record with cache-flush bit at t=2s.
flusher := mustBuildA(t, "host.local.", "192.168.1.2", 120)
flusher.Header.Class |= rrClassCacheFlush
ca.insert(flusher, clock.now())
// Old entry is marked to expire in 1s (at t=3s). Advance past that.
clock.advance(2 * time.Second)
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
// Only the new record should survive.
body, ok := results[0].Body.(*dnsmessage.AResource)
require.True(t, ok)
assert.Equal(t, [4]byte{192, 168, 1, 2}, body.A)
}
func TestCacheFlushPreservesRecent(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
// Insert record A at t=0.5s (recent).
clock.advance(500 * time.Millisecond)
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 300), clock.now())
// Insert record B with cache-flush bit at t=0.8s.
clock.advance(300 * time.Millisecond)
flusher := mustBuildA(t, "host.local.", "192.168.1.2", 120)
flusher.Header.Class |= rrClassCacheFlush
ca.insert(flusher, clock.now())
// Record A was created 300ms ago (< 1s), so it is preserved.
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 2)
var ips [][4]byte
for _, res := range results {
body, ok := res.Body.(*dnsmessage.AResource)
require.True(t, ok)
ips = append(ips, body.A)
}
assert.ElementsMatch(t, [][4]byte{{192, 168, 1, 1}, {192, 168, 1, 2}}, ips)
}
func TestCacheFlushPreservesRefreshed(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
// Insert record A at t=0.
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 300), clock.now())
// Advance well past the 1s cache-flush window before refreshing.
clock.advance(5 * time.Second)
// Refresh record A at t=5s (TTL update resets createdAt).
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 200), clock.now())
// Record B arrives with cache-flush at t=5.5s.
// Deadline = 5.5s - 1s = 4.5s. With createdAt reset to 5s, record A
// survives (5s > 4.5s). Without the reset, createdAt=0 < 4.5s and
// record A would be flushed (expiresAt set to 6.5s).
clock.advance(500 * time.Millisecond)
flusher := mustBuildA(t, "host.local.", "192.168.1.2", 120)
flusher.Header.Class |= rrClassCacheFlush
ca.insert(flusher, clock.now())
// Advance past the 1s flush expiry so incorrectly flushed records
// are gone, not just marked.
clock.advance(2 * time.Second)
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 2)
var ips [][4]byte
for _, res := range results {
body, ok := res.Body.(*dnsmessage.AResource)
require.True(t, ok)
ips = append(ips, body.A)
}
assert.ElementsMatch(t, [][4]byte{{192, 168, 1, 1}, {192, 168, 1, 2}}, ips)
}
func TestCacheFlushTypeIsolation(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
// Insert A and AAAA for the same name.
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 300), clock.now())
ca.insert(mustBuildAAAA(t, "host.local.", "fd00::1", 300), clock.now())
clock.advance(2 * time.Second)
// Cache-flush on a new A record should not touch the AAAA.
flusher := mustBuildA(t, "host.local.", "192.168.1.2", 120)
flusher.Header.Class |= rrClassCacheFlush
ca.insert(flusher, clock.now())
clock.advance(2 * time.Second)
// Old A record flushed, new A record present.
aResults := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, aResults, 1)
body, ok := aResults[0].Body.(*dnsmessage.AResource)
require.True(t, ok)
assert.Equal(t, [4]byte{192, 168, 1, 2}, body.A)
// AAAA record untouched.
aaaaResults := ca.lookup("host.local.", dnsmessage.TypeAAAA, dnsmessage.ClassINET)
require.Len(t, aaaaResults, 1)
body6, ok := aaaaResults[0].Body.(*dnsmessage.AAAAResource)
require.True(t, ok)
assert.Equal(t, netip.MustParseAddr("fd00::1").As16(), body6.AAAA)
}
// ---------------------------------------------------------------------------
// Topology change — RFC 6762 §10.3
// ---------------------------------------------------------------------------
func TestCacheFlushAll(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "a.local.", "10.0.0.1", 120), clock.now())
ca.insert(mustBuildA(t, "b.local.", "10.0.0.2", 120), clock.now())
assert.Equal(t, 2, ca.len())
ca.flushAll()
assert.Equal(t, 0, ca.len())
}
func TestCacheReduceTTLs(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "long.local.", "10.0.0.1", 300), clock.now())
ca.insert(mustBuildA(t, "short.local.", "10.0.0.2", 3), clock.now())
ca.reduceTTLs(5 * time.Second)
// Long TTL should be capped to ~5s.
results := ca.lookup("long.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
assert.Equal(t, uint32(5), results[0].Header.TTL)
// Short TTL (3s) is already under 5s, should be unchanged.
results = ca.lookup("short.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
assert.Equal(t, uint32(3), results[0].Header.TTL)
}
// ---------------------------------------------------------------------------
// Edge cases
// ---------------------------------------------------------------------------
func TestCacheLookupEmpty(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
results := ca.lookup("missing.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
assert.Empty(t, results)
// Different class also returns empty.
results = ca.lookup("missing.local.", dnsmessage.TypeA, dnsmessage.ClassCHAOS)
assert.Empty(t, results)
}
func TestCacheLookupWrongType(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
ca.insert(mustBuildA(t, "host.local.", "192.168.1.1", 120), clock.now())
// Same name, wrong type.
results := ca.lookup("host.local.", dnsmessage.TypeAAAA, dnsmessage.ClassINET)
assert.Empty(t, results)
}
func TestCacheLen(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
assert.Equal(t, 0, ca.len())
ca.insert(mustBuildA(t, "host.local.", "10.0.0.1", 60), clock.now())
ca.insert(mustBuildA(t, "host.local.", "10.0.0.2", 120), clock.now())
assert.Equal(t, 2, ca.len())
// Expire the first record.
clock.advance(60 * time.Second)
assert.Equal(t, 1, ca.len())
}
// ---------------------------------------------------------------------------
// Concurrency — run with -race
// ---------------------------------------------------------------------------
func TestCacheConcurrency(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
rec := mustBuildA(t, "host.local.", "192.168.1.1", 120)
key := cacheKey{
name: "host.local.",
rrType: dnsmessage.TypeA,
rrClass: dnsmessage.ClassINET,
}
var wg sync.WaitGroup
for range 100 {
wg.Add(4)
go func() {
defer wg.Done()
ca.insert(rec, clock.now())
}()
go func() {
defer wg.Done()
ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
}()
go func() {
defer wg.Done()
ca.sweep()
}()
go func() {
defer wg.Done()
ca.takeRefreshCandidates([]cacheKey{key})
}()
}
wg.Wait()
}
// ---------------------------------------------------------------------------
// resourceDataEqual — per-type coverage
// ---------------------------------------------------------------------------
func TestResourceDataEqualA(t *testing.T) {
recA := mustBuildA(t, "host.local.", "192.168.1.1", 120)
recB := mustBuildA(t, "host.local.", "192.168.1.1", 60)
recC := mustBuildA(t, "host.local.", "192.168.1.2", 120)
assert.True(t, resourceDataEqual(recA, recB), "same A rdata, different TTL")
assert.False(t, resourceDataEqual(recA, recC), "different A address")
}
func TestResourceDataEqualAAAA(t *testing.T) {
recA := mustBuildAAAA(t, "host.local.", "fd00::1", 120)
recB := mustBuildAAAA(t, "host.local.", "fd00::1", 60)
recC := mustBuildAAAA(t, "host.local.", "fd00::2", 120)
assert.True(t, resourceDataEqual(recA, recB), "same AAAA rdata")
assert.False(t, resourceDataEqual(recA, recC), "different AAAA address")
}
func TestResourceDataEqualPTR(t *testing.T) {
recA := mustBuildPTR(t, "_http._tcp.local.", "Web._http._tcp.local.", 4500)
recB := mustBuildPTR(t, "_http._tcp.local.", "Web._http._tcp.local.", 120)
recC := mustBuildPTR(t, "_ipp._tcp.local.", "Other._ipp._tcp.local.", 4500)
assert.True(t, resourceDataEqual(recA, recB), "same PTR target")
assert.False(t, resourceDataEqual(recA, recC), "different PTR target")
}
func TestResourceDataEqualSRV(t *testing.T) {
recA := mustBuildSRV(t, "svc._tcp.local.", "host.local.", 80, 120)
recB := mustBuildSRV(t, "svc._tcp.local.", "host.local.", 80, 60)
recC := mustBuildSRV(t, "svc._tcp.local.", "host.local.", 8080, 120)
assert.True(t, resourceDataEqual(recA, recB), "same SRV rdata")
assert.False(t, resourceDataEqual(recA, recC), "different SRV port")
}
func TestResourceDataEqualTXT(t *testing.T) {
recA := mustBuildTXT(t, "svc._tcp.local.", []string{"a=1", "b=2"}, 120)
recB := mustBuildTXT(t, "svc._tcp.local.", []string{"a=1", "b=2"}, 60)
recC := mustBuildTXT(t, "svc._tcp.local.", []string{"a=1", "c=3"}, 120)
recD := mustBuildTXT(t, "svc._tcp.local.", []string{"a=1"}, 120)
assert.True(t, resourceDataEqual(recA, recB), "same TXT strings")
assert.False(t, resourceDataEqual(recA, recC), "different TXT strings")
assert.False(t, resourceDataEqual(recA, recD), "different TXT length")
}
func TestResourceDataEqualDifferentTypes(t *testing.T) {
recA := mustBuildA(t, "host.local.", "192.168.1.1", 120)
recAAAA := mustBuildAAAA(t, "host.local.", "fd00::1", 120)
assert.False(t, resourceDataEqual(recA, recAAAA))
}
func TestResourceDataEqualNilBody(t *testing.T) {
recA := mustBuildA(t, "host.local.", "192.168.1.1", 120)
recNil := dnsmessage.Resource{
Header: recA.Header,
Body: nil,
}
assert.False(t, resourceDataEqual(recA, recNil))
}
func TestResourceDataEqualDifferentNames(t *testing.T) {
recA := mustBuildA(t, "a.local.", "192.168.1.1", 120)
recB := mustBuildA(t, "b.local.", "192.168.1.1", 120)
assert.False(t, resourceDataEqual(recA, recB))
}
// ---------------------------------------------------------------------------
// TTL clamping and capacity
// ---------------------------------------------------------------------------
func TestCacheTTLClampedToMax(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
// A 24h TTL is clamped to maxRecordTTL (75 minutes).
ca.insert(mustBuildA(t, "host.local.", "10.0.0.1", 86400), clock.now())
results := ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
assert.LessOrEqual(t, time.Duration(results[0].Header.TTL)*time.Second, maxRecordTTL)
// Expired at the clamp boundary despite the huge advertised TTL.
clock.advance(maxRecordTTL + time.Second)
assert.Empty(t, ca.lookup("host.local.", dnsmessage.TypeA, dnsmessage.ClassINET))
}
func TestCacheCapEvictsSoonestExpiring(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
// Short-lived record that should be evicted when the cache fills up.
ca.insert(mustBuildA(t, "victim.local.", "10.0.0.1", 5), clock.now())
for i := range maxCacheEntries {
name := fmt.Sprintf("host%d.local.", i)
ca.insert(mustBuildA(t, name, "10.0.0.2", 100), clock.now())
}
assert.Equal(t, maxCacheEntries, ca.len())
assert.Empty(t, ca.lookup("victim.local.", dnsmessage.TypeA, dnsmessage.ClassINET))
}
func TestCacheCapEvictionKeepsSiblingEntries(t *testing.T) {
clock := newTestClock()
ca := newCache(clock.now)
// Two records under the same key (same name/type, different rdata);
// the shorter-lived one is the eviction victim.
ca.insert(mustBuildA(t, "shared.local.", "10.0.0.1", 5), clock.now())
ca.insert(mustBuildA(t, "shared.local.", "10.0.0.2", 100), clock.now())
for i := range maxCacheEntries - 1 {
name := fmt.Sprintf("host%d.local.", i)
ca.insert(mustBuildA(t, name, "10.0.0.3", 100), clock.now())
}
// The sibling under the shared key must survive the eviction.
results := ca.lookup("shared.local.", dnsmessage.TypeA, dnsmessage.ClassINET)
require.Len(t, results, 1)
body, ok := results[0].Body.(*dnsmessage.AResource)
require.True(t, ok)
assert.Equal(t, [4]byte{10, 0, 0, 2}, body.A)
}