| 1 |
package dnsutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"codeberg.org/miekg/dns" |
| 7 |
"codeberg.org/miekg/dns/dnstest" |
| 8 |
) |
| 9 |
|
| 10 |
func TestTrim(t *testing.T) { |
| 11 |
testcases := []struct { |
| 12 |
qname string |
| 13 |
zone string |
| 14 |
expected string |
| 15 |
}{ |
| 16 |
{"a.example.org.", "example.org.", "a"}, |
| 17 |
{"a.b.example.org.", "example.org.", "a.b"}, |
| 18 |
{"b.", ".", "b"}, |
| 19 |
{"example.org.", "example.org.", "example.org."}, |
| 20 |
{"org.", "example.org.", "org."}, |
| 21 |
} |
| 22 |
|
| 23 |
for i, tc := range testcases { |
| 24 |
got := Trim(Fqdn(tc.qname), Fqdn(tc.zone)) |
| 25 |
if got != tc.expected { |
| 26 |
t.Errorf("test %d, expected %s, got %s", i, tc.expected, got) |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestIsFqdn(t *testing.T) { |
| 32 |
testcases := []struct { |
| 33 |
in string |
| 34 |
expected bool |
| 35 |
}{ |
| 36 |
{"miek.nl", false}, |
| 37 |
{"miek.nl.", true}, |
| 38 |
{"miek.nl\\.", true}, |
| 39 |
{"miek.nl\\\\.", true}, |
| 40 |
{"miek.n\\..", true}, |
| 41 |
} |
| 42 |
for i, tc := range testcases { |
| 43 |
got := IsFqdn(tc.in) |
| 44 |
if got != tc.expected { |
| 45 |
t.Errorf("test %d, %s, expected %t, got %t", i, tc.in, tc.expected, got) |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func TestRandomize(t *testing.T) { |
| 51 |
testcases := []string{ |
| 52 |
"www.example.org.", |
| 53 |
"www.EXAM__PLE.org.", |
| 54 |
"www.Example1223.org.", |
| 55 |
"www.examPle.^$^$a.%orG.", |
| 56 |
} |
| 57 |
for _, tc := range testcases { |
| 58 |
rand := Randomize(tc) |
| 59 |
if rand == tc { |
| 60 |
t.Fatalf("expected randomized string to not be equal to input: %s", tc) |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestRemoteIP(t *testing.T) { |
| 66 |
testcases := []struct { |
| 67 |
name string |
| 68 |
w dns.ResponseWriter |
| 69 |
exp string |
| 70 |
}{ |
| 71 |
{"ipv4", dnstest.NewTestRecorder(), "198.51.100.1"}, |
| 72 |
{"ipv6", dnstest.NewTestRecorder6(), "2001:db8::1"}, |
| 73 |
} |
| 74 |
for _, tc := range testcases { |
| 75 |
t.Run(tc.name, func(t *testing.T) { |
| 76 |
got := RemoteIP(tc.w) |
| 77 |
if got != tc.exp { |
| 78 |
t.Fatalf("expected %s, got %s", tc.exp, got) |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
} |