- feat: implement dns record model and zone checks - feat: implement zone file import and export
100 lines
3.8 KiB
Go
100 lines
3.8 KiB
Go
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
package main
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNormalizeZone(t *testing.T) {
|
|
label := strings.Repeat("a", 63)
|
|
limit := strings.Repeat(label+".", 3) + strings.Repeat("b", 61)
|
|
for _, input := range []string{"Example_ORG", "Example_ORG.", "xn--example.test", limit} {
|
|
got, err := normalizeZone(input)
|
|
if want := strings.ToLower(strings.TrimSuffix(input, ".")) + "."; err != nil || got != want {
|
|
t.Errorf("normalizeZone(%q) = %q, %v; want %q", input, got, err, want)
|
|
}
|
|
}
|
|
for _, input := range []string{"", ".", "a..b", "a..", "é.test", "a/b", "a ", "*", label + "a", limit + "b"} {
|
|
if _, err := normalizeZone(input); err == nil {
|
|
t.Errorf("accepted zone %q", input)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNormalizeOwner(t *testing.T) {
|
|
for _, tc := range []struct{ input, want string }{
|
|
{"", ""}, {"@", ""}, {"EXAMPLE.ORG.", ""}, {"WWW.Example.ORG.", "www"},
|
|
{"WWW.Example.ORG", "www.example.org"}, {"*", "*"}, {"*.Dev", "*.dev"},
|
|
{"*.DEV.example.org.", "*.dev"}, {"_Service._TCP", "_service._tcp"},
|
|
} {
|
|
got, fqdn, err := normalizeOwner("Example.Org", tc.input)
|
|
wantFQDN := "example.org."
|
|
if tc.want != "" {
|
|
wantFQDN = tc.want + "." + wantFQDN
|
|
}
|
|
if err != nil || got != tc.want || fqdn != wantFQDN {
|
|
t.Errorf("normalizeOwner(%q) = %q, %q, %v; want %q, %q", tc.input, got, fqdn, err, tc.want, wantFQDN)
|
|
}
|
|
}
|
|
label := strings.Repeat("a", 63)
|
|
limit := strings.Repeat(label+".", 3) + strings.Repeat("b", 49)
|
|
if _, fqdn, err := normalizeOwner("example.org.", limit); err != nil || len(fqdn) != 254 {
|
|
t.Fatalf("owner length = %d, %v", len(fqdn), err)
|
|
}
|
|
for _, input := range []string{"evil.example.", "notexample.org.", ".", "foo.*", "*bar", "@.example.org.", "foo..", "foo/aa", label + "a", limit + "b"} {
|
|
if _, _, err := normalizeOwner("example.org.", input); err == nil {
|
|
t.Errorf("accepted owner %q", input)
|
|
}
|
|
}
|
|
if _, _, err := normalizeOwner(".", "@"); err == nil {
|
|
t.Fatal("accepted invalid origin")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeHostAndMailbox(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
input, want string
|
|
root bool
|
|
}{
|
|
{"@", "example.org.", false}, {"Mail.Example.ORG", "mail.example.org.", false},
|
|
{"MAIL.", "mail.", false}, {".", ".", true}, {".", "", false}, {"", "", true}, {"*.example", "", true},
|
|
} {
|
|
got, err := normalizeHost("EXAMPLE.ORG", tc.input, tc.root)
|
|
if got != tc.want || (err != nil) != (tc.want == "") {
|
|
t.Errorf("normalizeHost(%q) = %q, %v", tc.input, got, err)
|
|
}
|
|
}
|
|
for _, tc := range []struct{ input, want string }{
|
|
{"Hostmaster@Example.Org", "hostmaster.example.org."},
|
|
{`First\.Last@Example.Org.`, `first\.last.example.org.`},
|
|
{"First.Last@Example.Org", `first\.last.example.org.`},
|
|
{`First\.Last.Example.Org`, `first\.last.example.org.`},
|
|
{"hostmaster.example.org", "hostmaster.example.org."},
|
|
{"Hostmaster", "hostmaster."},
|
|
{"Hostmaster.", "hostmaster."},
|
|
{"DNS+Ops@Example.Org", "dns+ops.example.org."},
|
|
{"O'Neil@example.org", `o\'neil.example.org.`},
|
|
{`O\'Neil.example.org.`, `o\'neil.example.org.`},
|
|
{"@example.org", ""}, {"hostmaster@", ""}, {"host@x@y", ""}, {"host name@example.org", ""},
|
|
{`host\x@example.org`, ""}, {strings.Repeat("a", 64) + "@example.org.", ""},
|
|
} {
|
|
got, err := normalizeMailbox(tc.input)
|
|
if got != tc.want || (err != nil) != (tc.want == "") {
|
|
t.Errorf("normalizeMailbox(%q) = %q, %v", tc.input, got, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCompareDNSNames(t *testing.T) {
|
|
names := []string{"a.z", "z.a", "B", "", "A", "a.a", "*.a", "z"}
|
|
slices.SortFunc(names, compareDNSNames)
|
|
want := []string{"", "A", "*.a", "a.a", "z.a", "B", "z", "a.z"}
|
|
if !slices.Equal(names, want) || compareDNSNames("WWW.Example.", "www.example") != 0 || compareDNSNames(".", "") != 0 {
|
|
t.Fatalf("canonical order = %v; want %v", names, want)
|
|
}
|
|
}
|