package checker import ( "net" "testing" ) func TestReverseNameToIP(t *testing.T) { cases := []struct { name string in string want string // empty = expect nil }{ {"ipv4 ok", "4.3.2.1.in-addr.arpa.", "1.2.3.4"}, {"ipv4 no trailing dot", "4.3.2.1.in-addr.arpa", "1.2.3.4"}, {"ipv4 uppercase suffix", "4.3.2.1.IN-ADDR.ARPA.", "1.2.3.4"}, {"ipv4 too few labels", "3.2.1.in-addr.arpa.", ""}, {"ipv4 too many labels", "5.4.3.2.1.in-addr.arpa.", ""}, {"ipv4 non-numeric label", "a.3.2.1.in-addr.arpa.", ""}, {"ipv4 octet out of range", "256.3.2.1.in-addr.arpa.", ""}, { "ipv6 ok", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.", "2001:db8::1", }, {"ipv6 too few nibbles", "0.0.8.b.d.0.ip6.arpa.", ""}, { "ipv6 multi-char label", "00.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.", "", }, {"not arpa", "example.com.", ""}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { got := reverseNameToIP(tc.in) if tc.want == "" { if got != nil { t.Fatalf("expected nil, got %v", got) } return } if got == nil { t.Fatalf("expected %s, got nil", tc.want) } want := net.ParseIP(tc.want) if !got.Equal(want) { t.Fatalf("expected %s, got %s", want, got) } }) } } func TestIsReverseArpa(t *testing.T) { cases := map[string]bool{ "4.3.2.1.in-addr.arpa.": true, "4.3.2.1.in-addr.arpa": true, "1.0.0.0.ip6.arpa.": true, "IN-ADDR.ARPA.": false, // bare apex, no leading label "example.com.": false, "": false, } for in, want := range cases { if got := isReverseArpa(in); got != want { t.Errorf("isReverseArpa(%q) = %v, want %v", in, got, want) } } } func TestLowerFQDN(t *testing.T) { cases := map[string]string{ "Example.COM": "example.com.", "example.com.": "example.com.", "": ".", } for in, want := range cases { if got := lowerFQDN(in); got != want { t.Errorf("lowerFQDN(%q) = %q, want %q", in, got, want) } } } func TestHostPort(t *testing.T) { cases := []struct { host, port, want string }{ {"1.2.3.4", "53", "1.2.3.4:53"}, {"ns1.example.com.", "53", "ns1.example.com:53"}, {"2001:db8::1", "53", "[2001:db8::1]:53"}, } for _, tc := range cases { if got := hostPort(tc.host, tc.port); got != tc.want { t.Errorf("hostPort(%q,%q) = %q, want %q", tc.host, tc.port, got, tc.want) } } } func TestRcodeText(t *testing.T) { if got := rcodeText(0); got != "NOERROR" { t.Errorf("rcodeText(0) = %q, want NOERROR", got) } if got := rcodeText(3); got != "NXDOMAIN" { t.Errorf("rcodeText(3) = %q, want NXDOMAIN", got) } if got := rcodeText(999); got != "RCODE(999)" { t.Errorf("rcodeText(999) = %q, want RCODE(999)", got) } }