-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzone_test.go
More file actions
658 lines (572 loc) · 17 KB
/
Copy pathzone_test.go
File metadata and controls
658 lines (572 loc) · 17 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
// SPDX-FileCopyrightText: 2026 Dinko Korunic
// SPDX-License-Identifier: MIT
package main
import (
"net"
"net/netip"
"os"
"strings"
"testing"
"time"
"github.com/miekg/dns"
)
func TestUnmapAddrFromSlice(t *testing.T) {
tests := []struct {
name string
slice []byte
expected netip.Addr
ok bool
}{
{
name: "IPv4 address",
slice: []byte{192, 0, 2, 1},
expected: netip.MustParseAddr("192.0.2.1"),
ok: true,
},
{
name: "IPv6 address",
slice: []byte{0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
expected: netip.MustParseAddr("2001:db8::1"),
ok: true,
},
{
name: "IPv4-mapped IPv6 address",
slice: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 0, 2, 1},
expected: netip.MustParseAddr("192.0.2.1"),
ok: true,
},
{
name: "Invalid slice length",
slice: []byte{1, 2, 3},
expected: netip.Addr{},
ok: false,
},
{
name: "Empty slice",
slice: []byte{},
expected: netip.Addr{},
ok: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := unmapAddrFromSlice(tt.slice)
if ok != tt.ok {
t.Errorf("unmapAddrFromSlice() ok = %v, want %v", ok, tt.ok)
}
if ok && got != tt.expected {
t.Errorf("unmapAddrFromSlice() got = %v, want %v", got, tt.expected)
}
})
}
}
func TestUnmapParseAddr(t *testing.T) {
tests := []struct {
name string
input string
expected netip.Addr
wantErr bool
}{
{
name: "IPv4 address",
input: "192.0.2.1",
expected: netip.MustParseAddr("192.0.2.1"),
wantErr: false,
},
{
name: "IPv6 address",
input: "2001:db8::1",
expected: netip.MustParseAddr("2001:db8::1"),
wantErr: false,
},
{
name: "IPv4-mapped IPv6 address",
input: "::ffff:192.0.2.1",
expected: netip.MustParseAddr("192.0.2.1"),
wantErr: false,
},
{
name: "Invalid address",
input: "invalid",
expected: netip.Addr{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := unmapParseAddr(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("unmapParseAddr() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && got != tt.expected {
t.Errorf("unmapParseAddr() got = %v, want %v", got, tt.expected)
}
})
}
}
func TestZoneParser(t *testing.T) {
// no $ORIGIN; domain supplied via parameter
zoneContent := `$TTL 3600
@ IN SOA ns1 admin 2021010101 3600 900 604800 300
@ IN NS ns1
ns1 IN A 192.0.2.1
host1 IN A 192.0.2.2
host2 IN AAAA 2001:db8::1
`
tmpFile, err := os.CreateTemp("", "test-zone-*.txt")
if err != nil {
t.Fatalf("CreateTemp: %v", err)
}
defer os.Remove(tmpFile.Name())
if _, err := tmpFile.WriteString(zoneContent); err != nil {
t.Fatalf("WriteString: %v", err)
}
tmpFile.Close()
records := zoneParser(tmpFile.Name(), "example.com.")
var aCount, aaaaCount, soaCount, nsCount int
for _, rr := range records {
switch rr.(type) {
case *dns.A:
aCount++
case *dns.AAAA:
aaaaCount++
case *dns.SOA:
soaCount++
case *dns.NS:
nsCount++
}
}
if aCount != 2 {
t.Errorf("zoneParser() A records = %d, want 2", aCount)
}
if aaaaCount != 1 {
t.Errorf("zoneParser() AAAA records = %d, want 1", aaaaCount)
}
if soaCount != 1 {
t.Errorf("zoneParser() SOA records = %d, want 1", soaCount)
}
if nsCount != 1 {
t.Errorf("zoneParser() NS records = %d, want 1", nsCount)
}
}
func TestZoneParserNonExistent(t *testing.T) {
records := zoneParser("/nonexistent/zone-file-that-does-not-exist.txt", "example.com.")
if len(records) != 0 {
t.Errorf("zoneParser() = %d records for non-existent file, want 0", len(records))
}
}
func TestProcessRecords(t *testing.T) {
origIgnoreStar := *ignoreStar
defer func() { *ignoreStar = origIgnoreStar }()
*ignoreStar = false
hosts := make(chan HostEntry, 20)
rrs := []dns.RR{
&dns.A{
Hdr: dns.RR_Header{Name: "host1.example.com.", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 3600},
A: net.IP{192, 0, 2, 1},
},
&dns.AAAA{
Hdr: dns.RR_Header{Name: "host2.example.com.", Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 3600},
AAAA: net.IP{0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
},
// SOA must be silently skipped (not A/AAAA/CNAME)
&dns.SOA{
Hdr: dns.RR_Header{Name: "example.com.", Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 3600},
},
}
processRecords("example.com", false, nil, hosts, rrs)
close(hosts)
var results []HostEntry
for h := range hosts {
results = append(results, h)
}
if len(results) != 2 {
t.Errorf("processRecords() = %d entries, want 2", len(results))
}
}
func TestProcessRecordsIgnoreStar(t *testing.T) {
origIgnoreStar := *ignoreStar
defer func() { *ignoreStar = origIgnoreStar }()
*ignoreStar = true
hosts := make(chan HostEntry, 20)
rrs := []dns.RR{
&dns.A{
Hdr: dns.RR_Header{Name: "*.example.com.", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 3600},
A: net.IP{192, 0, 2, 1},
},
&dns.A{
Hdr: dns.RR_Header{Name: "host1.example.com.", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 3600},
A: net.IP{192, 0, 2, 2},
},
&dns.AAAA{
Hdr: dns.RR_Header{Name: "*.v6.example.com.", Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 3600},
AAAA: net.IP{0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
},
}
processRecords("example.com", false, nil, hosts, rrs)
close(hosts)
var results []HostEntry
for h := range hosts {
results = append(results, h)
}
if len(results) != 1 {
t.Errorf("processRecords() = %d entries, want 1 (wildcards ignored)", len(results))
}
if len(results) == 1 && results[0].label != "host1.example.com" {
t.Errorf("processRecords() label = %q, want %q", results[0].label, "host1.example.com")
}
}
func TestProcessRecordsCIDR(t *testing.T) {
origIgnoreStar := *ignoreStar
defer func() { *ignoreStar = origIgnoreStar }()
*ignoreStar = false
ranger, doCIDR := rangerInit([]string{"192.0.2.0/24"})
hosts := make(chan HostEntry, 20)
rrs := []dns.RR{
&dns.A{
Hdr: dns.RR_Header{Name: "host1.example.com.", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 3600},
A: net.IP{192, 0, 2, 1}, // inside CIDR
},
&dns.A{
Hdr: dns.RR_Header{Name: "host2.example.com.", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 3600},
A: net.IP{10, 0, 0, 1}, // outside CIDR
},
&dns.AAAA{
Hdr: dns.RR_Header{Name: "host3.example.com.", Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 3600},
AAAA: net.IP{0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // outside CIDR
},
}
processRecords("example.com", doCIDR, ranger, hosts, rrs)
close(hosts)
var results []HostEntry
for h := range hosts {
results = append(results, h)
}
if len(results) != 1 {
t.Errorf("processRecords() = %d entries, want 1 (CIDR filtered)", len(results))
}
if len(results) == 1 && results[0].label != "host1.example.com" {
t.Errorf("processRecords() label = %q, want %q", results[0].label, "host1.example.com")
}
}
func TestProcessLocalZone(t *testing.T) {
// no $ORIGIN; domain supplied via "file=domain" argument
zoneContent := `$TTL 3600
@ IN SOA ns1 admin 2021010101 3600 900 604800 300
@ IN NS ns1
ns1 IN A 192.0.2.1
host1 IN A 192.0.2.2
host2 IN AAAA 2001:db8::1
`
tmpFile, err := os.CreateTemp("", "test-local-zone-*.txt")
if err != nil {
t.Fatalf("CreateTemp: %v", err)
}
defer os.Remove(tmpFile.Name())
if _, err := tmpFile.WriteString(zoneContent); err != nil {
t.Fatalf("WriteString: %v", err)
}
tmpFile.Close()
hosts := make(chan HostEntry, 20)
processLocalZone(tmpFile.Name()+"=example.com", false, nil, hosts)
close(hosts)
var results []HostEntry
for h := range hosts {
results = append(results, h)
}
// ns1, host1, host2; SOA/NS skipped
if len(results) < 3 {
t.Errorf("processLocalZone() = %d entries, want at least 3", len(results))
}
}
func TestProcessLocalZoneNoSeparator(t *testing.T) {
// no "=domain" + no $ORIGIN: yields zero records and a warning
tmpFile, err := os.CreateTemp("", "test-empty-zone-*.txt")
if err != nil {
t.Fatalf("CreateTemp: %v", err)
}
defer os.Remove(tmpFile.Name())
tmpFile.Close()
hosts := make(chan HostEntry, 5)
processLocalZone(tmpFile.Name(), false, nil, hosts)
close(hosts)
var results []HostEntry
for h := range hosts {
results = append(results, h)
}
if len(results) != 0 {
t.Errorf("processLocalZone() empty zone = %d entries, want 0", len(results))
}
}
func TestZoneParserOrigin(t *testing.T) {
// $ORIGIN inside file; empty domain parameter
zoneContent := `$ORIGIN example.com.
$TTL 3600
@ IN SOA ns1.example.com. admin.example.com. 2021010101 3600 900 604800 300
@ IN NS ns1.example.com.
ns1 IN A 192.0.2.1
host1 IN A 192.0.2.2
host2 IN AAAA 2001:db8::1
`
tmpFile, err := os.CreateTemp("", "test-zone-origin-*.txt")
if err != nil {
t.Fatalf("CreateTemp: %v", err)
}
defer os.Remove(tmpFile.Name())
if _, err := tmpFile.WriteString(zoneContent); err != nil {
t.Fatalf("WriteString: %v", err)
}
tmpFile.Close()
// empty domain — origin must come from in-file $ORIGIN
records := zoneParser(tmpFile.Name(), "")
var aCount, aaaaCount int
for _, rr := range records {
switch rr.(type) {
case *dns.A:
aCount++
case *dns.AAAA:
aaaaCount++
}
}
if aCount != 2 {
t.Errorf("zoneParser() with $ORIGIN: A records = %d, want 2", aCount)
}
if aaaaCount != 1 {
t.Errorf("zoneParser() with $ORIGIN: AAAA records = %d, want 1", aaaaCount)
}
}
func TestZoneParserMalformedLine(t *testing.T) {
// malformed RDATA: parser must not panic and must return prior valid records
zoneContent := `$TTL 3600
@ IN SOA ns1 admin 2021010101 3600 900 604800 300
@ IN NS ns1
ns1 IN A 192.0.2.1
badrecord IN A NOT_AN_IP_ADDRESS
host1 IN A 192.0.2.2
`
tmpFile, err := os.CreateTemp("", "test-zone-malformed-*.txt")
if err != nil {
t.Fatalf("CreateTemp: %v", err)
}
defer os.Remove(tmpFile.Name())
if _, err := tmpFile.WriteString(zoneContent); err != nil {
t.Fatalf("WriteString: %v", err)
}
tmpFile.Close()
records := zoneParser(tmpFile.Name(), "example.com.")
if len(records) == 0 {
t.Error("zoneParser() with malformed line: expected at least some valid records before the error")
}
}
func TestProcessRecordsEmpty(t *testing.T) {
hosts := make(chan HostEntry, 5)
processRecords("example.com", false, nil, hosts, []dns.RR{})
close(hosts)
var results []HostEntry
for h := range hosts {
results = append(results, h)
}
if len(results) != 0 {
t.Errorf("processRecords() empty records = %d entries, want 0", len(results))
}
}
func TestProcessRecordsCNAMEGreedy(t *testing.T) {
if testing.Short() {
t.Skip("skipping DNS-dependent CNAME test in short mode")
}
origGreedy := *greedyCNAME
origTimeout := *resolverTimeout
defer func() {
*greedyCNAME = origGreedy
*resolverTimeout = origTimeout
}()
*greedyCNAME = true
*resolverTimeout = 500 * time.Millisecond
hosts := make(chan HostEntry, 10)
rrs := []dns.RR{
&dns.CNAME{
Hdr: dns.RR_Header{Name: "alias.example.com.", Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: 3600},
Target: "canonical.example.com.",
},
}
// example.com lookups NXDOMAIN; verifies no panic on failure path
processRecords("example.com", false, nil, hosts, rrs)
close(hosts)
for range hosts {
}
}
func TestProcessRecordsCNAMENonGreedy(t *testing.T) {
if testing.Short() {
t.Skip("skipping DNS-dependent CNAME test in short mode")
}
origGreedy := *greedyCNAME
origTimeout := *resolverTimeout
defer func() {
*greedyCNAME = origGreedy
*resolverTimeout = origTimeout
}()
*greedyCNAME = false
*resolverTimeout = 500 * time.Millisecond
hosts := make(chan HostEntry, 10)
rrs := []dns.RR{
&dns.CNAME{
Hdr: dns.RR_Header{Name: "alias.example.com.", Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: 3600},
Target: "canonical.example.com.",
},
&dns.CNAME{
Hdr: dns.RR_Header{Name: "external.example.com.", Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: 3600},
Target: "target.other.com.",
},
}
// greedyCNAME=false routes through lookup(TypeCNAME); DNS fails, no entries
processRecords("example.com", false, nil, hosts, rrs)
close(hosts)
for range hosts {
}
}
func TestProcessLocalZoneMissingFileDomain(t *testing.T) {
hosts := make(chan HostEntry, 5)
// missing file with file=domain: no panic, 0 entries (silent when domain set)
processLocalZone("/nonexistent/axfr2hosts-test-missing.txt=example.com", false, nil, hosts)
close(hosts)
var results []HostEntry
for h := range hosts {
results = append(results, h)
}
if len(results) != 0 {
t.Errorf("processLocalZone() missing file = %d entries, want 0", len(results))
}
}
// TestCNAMEZoneSuffix validates the zone membership check used for CNAME filtering
// when greedyCNAME=false. net.LookupCNAME always returns FQDNs with a trailing dot
// (e.g. "target.example.com."), while the zone variable has its trailing dot stripped
// by parseFlags. The fix normalises the zone with dns.Fqdn() before the HasSuffix
// comparison so that in-zone CNAMEs are correctly identified.
func TestCNAMEZoneSuffix(t *testing.T) {
tests := []struct {
name string
canonical string // LookupCNAME output: always trailing dot
zone string // parseFlags strips trailing dot
inZone bool
}{
{
name: "in-zone CNAME",
canonical: "target.example.com.",
zone: "example.com",
inZone: true,
},
{
name: "out-of-zone CNAME",
canonical: "target.other.com.",
zone: "example.com",
inZone: false,
},
{
name: "zone apex CNAME",
canonical: "example.com.",
zone: "example.com",
inZone: true,
},
{
name: "subdomain in-zone CNAME",
canonical: "deep.sub.example.com.",
zone: "example.com",
inZone: true,
},
// bug #7: substring match would falsely accept "example.com.evil.org."
{
name: "contains-but-not-suffix CNAME (bug #7 oracle)",
canonical: "example.com.evil.org.",
zone: "example.com",
inZone: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// bare zone (no dns.Fqdn) misses the trailing-dot suffix
withoutFqdn := strings.HasSuffix(tt.canonical, tt.zone)
if tt.inZone && withoutFqdn {
t.Logf("note: bare zone suffix matched for %q — likely a zone with no dot-separated prefix", tt.canonical)
}
// dns.Fqdn aligns zone with LookupCNAME output
withFqdn := strings.HasSuffix(tt.canonical, dns.Fqdn(tt.zone))
if withFqdn != tt.inZone {
t.Errorf("HasSuffix(%q, dns.Fqdn(%q)) = %v, want %v",
tt.canonical, tt.zone, withFqdn, tt.inZone)
}
// bug #7 guard: HasSuffix must reject what Contains would accept
withContains := strings.Contains(tt.canonical, dns.Fqdn(tt.zone))
if !tt.inZone && withContains && withFqdn {
t.Errorf("HasSuffix unexpectedly accepted out-of-zone target %q", tt.canonical)
}
})
}
}
// TestProcessLocalZoneNoSpuriousWarning is the test oracle for bug #9.
// A zone file that legitimately has zero parseable records but was given an
// explicit domain (via "file=domain" syntax) must NOT trigger the
// "Try with file=domain" warning — that warning fires only when BOTH conditions
// are true: no records AND no domain supplied.
func TestProcessLocalZoneNoSpuriousWarning(t *testing.T) {
// empty file → zero parseable records
tmpFile, err := os.CreateTemp("", "test-empty-domain-*.txt")
if err != nil {
t.Fatalf("CreateTemp: %v", err)
}
defer os.Remove(tmpFile.Name())
tmpFile.Close()
hosts := make(chan HostEntry, 5)
// explicit domain must suppress the "Try with file=domain" warning
stderr := captureStderr(func() {
processLocalZone(tmpFile.Name()+"=example.com", false, nil, hosts)
})
close(hosts)
for range hosts {
}
// && (correct) suppresses warning when domain is set; || (buggy) would still fire
if strings.Contains(stderr, "Try") {
t.Errorf("processLocalZone with explicit domain printed spurious warning:\n%s", stderr)
}
}
// TestZoneParserNoNilEntries is the test oracle for bug #11.
// When the zone file contains a malformed line, zoneParser must return only
// valid (non-nil) RRs with no duplicates — it must not append a nil RR before
// checking the parse error.
func TestZoneParserNoNilEntries(t *testing.T) {
zoneContent := `$TTL 3600
@ IN SOA ns1 admin 2021010101 3600 900 604800 300
@ IN NS ns1
ns1 IN A 192.0.2.1
bad IN A NOT_AN_IP
host1 IN A 192.0.2.2
`
tmpFile, err := os.CreateTemp("", "test-zone-nil-*.txt")
if err != nil {
t.Fatalf("CreateTemp: %v", err)
}
defer os.Remove(tmpFile.Name())
if _, err := tmpFile.WriteString(zoneContent); err != nil {
t.Fatalf("WriteString: %v", err)
}
tmpFile.Close()
records := zoneParser(tmpFile.Name(), "example.com.")
// parser must never append nil RRs
for i, rr := range records {
if rr == nil {
t.Errorf("zoneParser() returned nil RR at index %d", i)
}
}
// double-append regression: no duplicate RRs
seen := make(map[string]int)
for i, rr := range records {
if rr == nil {
continue
}
s := rr.String()
if prev, ok := seen[s]; ok {
t.Errorf("zoneParser() duplicate RR at index %d (same as index %d): %q", i, prev, s)
}
seen[s] = i
}
}