feat: implement dns record model and zone checks
- feat: implement dns record model and zone checks - feat: implement zone file import and export
This commit is contained in:
1 parent
5ac8f98f10
commit
7ba68aba15
18 files changed
+2590
-21
No files matched your search
+113
@@ -0,0 +1,113 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const modelSOAContent = `{"ns":"ns.example.net.","MBox":"hostmaster.example.org.","refresh":3600,"retry":600,"expire":604800,"minttl":300,"serial":2026111800}`
|
||||
|
||||
func modelBaseZone() []recordRow {
|
||||
return []recordRow{
|
||||
modelRawRow(1, "", "SOA", 3600, modelSOAContent),
|
||||
modelRawRow(2, "", "NS", 3600, `{"host":"ns.example.net."}`),
|
||||
}
|
||||
}
|
||||
|
||||
func modelProblem(problems []zoneProblem, severity, code string) bool {
|
||||
return slices.ContainsFunc(problems, func(problem zoneProblem) bool {
|
||||
return problem.Code == code && problem.Severity == severity && problem.Message != ""
|
||||
})
|
||||
}
|
||||
|
||||
func TestCheckZoneCodes(t *testing.T) {
|
||||
base := func() []recordRow {
|
||||
return append(modelBaseZone(), modelRawRow(3, "www", "A", 3600, `{"ip":"192.0.2.10"}`))
|
||||
}
|
||||
change := func(edit func(*recordRow)) []recordRow {
|
||||
rows := base()
|
||||
edit(&rows[2])
|
||||
return rows
|
||||
}
|
||||
with := func(extra ...recordRow) []recordRow { return append(base(), extra...) }
|
||||
for _, tc := range []struct {
|
||||
code, severity string
|
||||
broken []recordRow
|
||||
}{
|
||||
{"zone_not_fqdn", "error", change(func(r *recordRow) { r.Zone = "example.org" })},
|
||||
{"zone_case", "error", change(func(r *recordRow) { r.Zone = "Example.ORG." })},
|
||||
{"ttl_null", "error", change(func(r *recordRow) { r.TTL = sql.NullInt64{} })},
|
||||
{"ttl_negative", "error", change(func(r *recordRow) { r.TTL.Int64 = -1 })},
|
||||
{"content_null", "error", change(func(r *recordRow) { r.Content = sql.NullString{} })},
|
||||
{"content_json", "error", change(func(r *recordRow) { r.Content.String = "null" })},
|
||||
{"content_json", "error", change(func(r *recordRow) { r.Content.String = `[{"ip":"192.0.2.10"}]` })},
|
||||
{"type_unknown", "error", change(func(r *recordRow) { r.RecordType = "a" })},
|
||||
{"field_invalid", "error", change(func(r *recordRow) { r.Content.String = `{}` })},
|
||||
{"field_invalid", "error", change(func(r *recordRow) { r.Content.String = `{"ip":"bad"}` })},
|
||||
{"field_invalid", "error", change(func(r *recordRow) { r.TTL.Int64 = 2147483648 })},
|
||||
{"field_invalid", "error", change(func(r *recordRow) { r.Name = "www.example.org." })},
|
||||
{"field_invalid", "error", change(func(r *recordRow) { r.Name = "@" })},
|
||||
{"name_not_fqdn", "error", with(modelRawRow(4, "alias", "CNAME", 3600, `{"host":"target.example"}`))},
|
||||
{"mx_priority_key", "error", with(modelRawRow(4, "", "MX", 3600, `{"host":"mail.example.net.","priority":10}`))},
|
||||
{"soa_missing", "error", base()[1:]},
|
||||
{"soa_multiple", "error", with(modelRawRow(4, "", "SOA", 3600, modelSOAContent))},
|
||||
{"soa_not_apex", "error", append(base()[1:], modelRawRow(4, "child", "SOA", 3600, modelSOAContent))},
|
||||
{"cname_apex", "error", with(modelRawRow(4, "", "CNAME", 3600, `{"host":"target.example."}`))},
|
||||
{"cname_conflict", "error", with(modelRawRow(4, "WWW", "CNAME", 3600, `{"host":"target.example."}`))},
|
||||
{"pack_failed", "error", with(modelRawRow(4, "sub", "NS", 3600, `{"host":"ns.example.net"}`))},
|
||||
{"ns_apex_missing", "warning", base()[:1]},
|
||||
{"duplicate", "warning", with(modelRawRow(4, "WWW", "A", 3600, `{"other":false,"ip":"192.0.2.10"}`))},
|
||||
{"rrset_ttl_mismatch", "warning", with(modelRawRow(4, "www", "A", 0, `{"ip":"192.0.2.11"}`))},
|
||||
{"target_is_cname", "warning", with(modelRawRow(4, "", "MX", 3600, `{"host":"alias.example.org.","preference":5}`),
|
||||
modelRawRow(5, "alias", "CNAME", 3600, `{"host":"target.example."}`))},
|
||||
{"target_no_address", "warning", with(modelRawRow(4, "", "MX", 3600, `{"host":"mail.example.org.","preference":5}`))},
|
||||
{"below_zone_cut", "warning", with(modelRawRow(4, "sub", "NS", 3600, `{"host":"ns.example.net."}`),
|
||||
modelRawRow(5, "deep.a.sub", "A", 3600, `{"ip":"192.0.2.5"}`))},
|
||||
{"txt_split", "warning", with(modelRawRow(4, "text", "TXT", 3600, `{"text":"`+strings.Repeat("x", 256)+`"}`))},
|
||||
{"name_case", "warning", change(func(r *recordRow) { r.Name = "WWW" })},
|
||||
{"ttl_zero", "warning", change(func(r *recordRow) { r.TTL.Int64 = 0 })},
|
||||
{"soa_dynamic_serial", "warning", append(base()[1:], modelRawRow(1, "", "SOA", 3600,
|
||||
strings.Replace(modelSOAContent, `,"serial":2026111800`, "", 1)))},
|
||||
{"soa_dynamic_serial", "warning", append(base()[1:], modelRawRow(1, "", "SOA", 3600,
|
||||
strings.Replace(modelSOAContent, "2026111800", "0", 1)))},
|
||||
{"content_not_canonical", "warning", change(func(r *recordRow) { r.Content.String = `{ "ip":"192.0.2.10", "other":1 }` })},
|
||||
} {
|
||||
t.Run(tc.code, func(t *testing.T) {
|
||||
good := checkZone("example.org.", base(), modelNow)
|
||||
if len(good) != 0 {
|
||||
t.Fatalf("valid control has problems: %+v", good)
|
||||
}
|
||||
broken := checkZone("example.org.", tc.broken, modelNow)
|
||||
if !modelProblem(broken, tc.severity, tc.code) {
|
||||
t.Errorf("wanted %s %s, got %+v", tc.severity, tc.code, broken)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckMixedBrokenRows(t *testing.T) {
|
||||
rows := append(modelBaseZone(),
|
||||
recordRow{ID: 3, Zone: "bad..Zone", Name: "a.*", RecordType: "txt", Content: sql.NullString{String: "[]", Valid: true}},
|
||||
modelRawRow(4, "sub", "NS", 3600, `{"host":"ns.example.net"}`),
|
||||
modelRawRow(5, "a", "A", -1, `{"ip":null}`),
|
||||
modelRawRow(6, "a", "CNAME", 32, `{"host":987}`),
|
||||
modelRawRow(7, "b", "SRV", 32, `{"priority":0,"weight":0,"port":0,"target":"."}`),
|
||||
modelRawRow(8, "c", "A", 32, `{"ip":"::1"}`),
|
||||
modelRawRow(9, "*", "NS", 32, `{"host":"ns.example.net."}`),
|
||||
modelRawRow(10, "d", "A", 32, `{`),
|
||||
)
|
||||
problems := checkZone("example.org.", rows, modelNow)
|
||||
for _, code := range []string{"zone_case", "zone_not_fqdn", "type_unknown", "ttl_null", "ttl_negative", "content_json", "field_invalid", "pack_failed", "cname_conflict"} {
|
||||
if !modelProblem(problems, "error", code) {
|
||||
t.Errorf("missing %s in %+v", code, problems)
|
||||
}
|
||||
}
|
||||
if slices.ContainsFunc(problems, func(p zoneProblem) bool { return p.RecordID == 1 || p.RecordID == 2 }) {
|
||||
t.Fatalf("reported problems for good rows: %+v", problems)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user