-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdns_fuzz_test.go
More file actions
88 lines (70 loc) · 1.73 KB
/
Copy pathdns_fuzz_test.go
File metadata and controls
88 lines (70 loc) · 1.73 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
// CGo binding for Avahi
//
// Copyright (C) 2025 by Prashant Andoriya
// See LICENSE for license terms and conditions
//
// Fuzz tests for DNS record decoding functions
//
//go:build linux || freebsd
package avahi
import (
"net/netip"
"testing"
)
// FuzzDNSDecodeA fuzzes the DNSDecodeA function
func FuzzDNSDecodeA(f *testing.F) {
// Valid IPv4
f.Add([]byte{127, 0, 0, 1})
f.Add([]byte{8, 8, 8, 8})
// Invalid sizes
f.Add([]byte{})
f.Add([]byte{1})
f.Add([]byte{1, 2, 3})
f.Add([]byte{1, 2, 3, 4, 5})
// Random bytes
f.Add([]byte{255, 255, 255, 255})
f.Fuzz(func(t *testing.T, data []byte) {
addr := DNSDecodeA(data)
if addr.IsValid() && addr.Is6() {
t.Fatalf("DNSDecodeA returned IPv6 address: %v", addr)
}
})
}
// FuzzDNSDecodeAAAA fuzzes the DNSDecodeAAAA function
func FuzzDNSDecodeAAAA(f *testing.F) {
// Valid IPv6 (loopback)
f.Add(netip.IPv6Loopback().AsSlice())
// Invalid sizes
f.Add([]byte{})
f.Add([]byte{1, 2, 3})
f.Add(make([]byte, 15))
f.Add(make([]byte, 17))
f.Fuzz(func(t *testing.T, data []byte) {
addr := DNSDecodeAAAA(data)
if addr.IsValid() && !addr.Is6() {
t.Fatalf("DNSDecodeAAAA returned non-IPv6 address: %v", addr)
}
})
}
// FuzzDNSDecodeTXT fuzzes the DNSDecodeTXT function
func FuzzDNSDecodeTXT(f *testing.F) {
// Valid TXT records
f.Add([]byte{3, 'f', 'o', 'o'})
f.Add([]byte{3, 'f', 'o', 'o', 3, 'b', 'a', 'r'})
// Empty string
f.Add([]byte{0})
// Length exceeds data
f.Add([]byte{10, 'a'})
// Nested garbage
f.Add([]byte{255, 255, 255})
// Random bytes
f.Add([]byte{1, 0, 255, 10, 20, 30})
f.Fuzz(func(t *testing.T, data []byte) {
txt := DNSDecodeTXT(data)
for _, s := range txt {
if len(s) == 0 {
t.Fatalf("DNSDecodeTXT returned empty string")
}
}
})
}