Files
coredns-web-ui/check_test.go
T
bryanpedini a7ba01fc77 refactor(api): use audit operation constants and the shared zone not found error in zone handlers
- refactor(api): use audit operation constants and the shared zone not found error in zone handlers
- fix(api): return the stored content of rows with check errors
- fix(api): return warnings from record deletes, zone creation and import apply
- fix(api): allow repairing rows stored with a non canonical zone and invalid SOA sets
- fix(check): accept JSON column formatting and an absent SOA serial as canonical content
- fix(ui): match audit diff items by identity instead of array index
- fix(ui): count the SOA change once, report import warnings and check the request size
- fix(ui): edit the selected SOA row, keep the server order, show error rows raw and report write warnings
- test(check): cover triggering and clean pairs with exact codes and record ids
- test(records): cover SRV port, SOA timer and CAA value boundaries
- test(cli): check the exact check output and the exit code of warning-only zones
- refactor(import): order current rows with cmp.Compare
- test(check): cover malformed stored content and a mailbox stored as an address
- test(api): cover zone write failure paths, ambiguous SOA updates and unchanged record detection
2026-11-19 15:46:12 +01:00

250 lines
14 KiB
Go

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
package main
import (
"cmp"
"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)
}
}
func TestCheckCanonicalContentStorage(t *testing.T) {
soaJSONColumn := `{"ns": "ns.example.net.", "MBox": "hostmaster.example.org.", "retry": 600, "expire": 604800, "minttl": 300, "serial": 2026111800, "refresh": 3600}`
for _, tc := range []struct {
name, recordType, content string
codes []string
}{
{"text canonical", "A", `{"ip":"192.0.2.1"}`, nil},
{"json column spacing", "A", `{"ip": "192.0.2.1"}`, nil},
{"text whitespace", "A", "{ \"ip\" :\n\"192.0.2.1\" }", nil},
{"extra key", "A", `{"ip": "192.0.2.1", "x": 1}`, []string{"content_not_canonical"}},
{"duplicate key", "A", `{"ip":"192.0.2.1","ip":"192.0.2.1"}`, []string{"content_not_canonical"}},
{"json column MX", "MX", `{"host": "mx.example.org.", "preference": 10}`, []string{"target_no_address"}},
{"reordered MX", "MX", `{"preference":10,"host":"mx.example.org."}`, []string{"content_not_canonical", "target_no_address"}},
{"json column SRV", "SRV", `{"port": 443, "target": ".", "weight": 5, "priority": 10}`, nil},
{"text canonical SOA", "SOA", modelSOAContent, nil},
{"json column SOA", "SOA", soaJSONColumn, nil},
{"text SOA in key length order", "SOA", strings.ReplaceAll(soaJSONColumn, ": ", ":"), nil},
{"partly reordered SOA", "SOA", `{"MBox":"hostmaster.example.org.","ns":"ns.example.net.","refresh":3600,"retry":600,"expire":604800,"minttl":300,"serial":2026111800}`, []string{"content_not_canonical"}},
{"lowercase mbox key", "SOA", strings.Replace(modelSOAContent, `"MBox"`, `"mbox"`, 1), []string{"content_not_canonical"}},
{"dynamic SOA without serial", "SOA", strings.Replace(modelSOAContent, `,"serial":2026111800`, "", 1), []string{"soa_dynamic_serial"}},
{"dynamic SOA with serial zero", "SOA", strings.Replace(modelSOAContent, `2026111800`, "0", 1), []string{"soa_dynamic_serial"}},
{"json column dynamic SOA", "SOA", strings.Replace(soaJSONColumn, `"serial": 2026111800, `, "", 1), []string{"soa_dynamic_serial"}},
{"text escaped character", "TXT", `{"text":"a<b"}`, nil},
{"json column raw character", "TXT", `{"text": "a<b"}`, nil},
{"nested extra key", "A", `{"ip":"192.0.2.1","x":{"y":1}}`, []string{"content_not_canonical"}},
{"number spelled differently", "MX", `{"host":"mx.example.org.","preference":10.0}`, []string{"field_invalid"}},
} {
t.Run(tc.name, func(t *testing.T) {
name := ""
if tc.recordType == "SRV" {
name = "_sip._tcp"
}
row := modelRawRow(9, name, tc.recordType, 3600, tc.content)
rows := append(modelBaseZone(), row)
if tc.recordType == "SOA" {
rows = []recordRow{row, modelBaseZone()[1]}
}
var codes []string
for _, p := range checkZone("example.org.", rows, modelNow) {
if p.RecordID == 9 {
codes = append(codes, p.Code)
}
}
if !slices.Equal(codes, tc.codes) {
t.Fatalf("codes = %v, want %v", codes, tc.codes)
}
})
}
for _, content := range []string{`[]`, `{"ip":{"nested":1}}`, `{"ip":"x"} {}`, `{"ip"`, `{1:2}`, `{"ip":"x"`} {
if _, ok := contentMembers(content); ok {
t.Fatalf("contentMembers accepted %q", content)
}
}
}
type modelFinding struct {
id int64
severity string
code string
}
func modelFindings(problems []zoneProblem) []modelFinding {
var findings []modelFinding
for _, p := range problems {
findings = append(findings, modelFinding{p.RecordID, p.Severity, p.Code})
}
slices.SortFunc(findings, func(a, b modelFinding) int {
return cmp.Or(cmp.Compare(a.id, b.id), strings.Compare(a.severity, b.severity), strings.Compare(a.code, b.code))
})
return findings
}
func TestCheckZonePairs(t *testing.T) {
soa := func(content string) []recordRow {
return []recordRow{modelRawRow(1, "", "SOA", 3600, content), modelRawRow(2, "", "NS", 3600, `{"host":"ns.example.net."}`)}
}
with := func(extra ...recordRow) []recordRow { return append(modelBaseZone(), extra...) }
txt := func(size int) string { return `{"text":"` + strings.Repeat("x", size) + `"}` }
for _, tc := range []struct {
name string
triggered []recordRow
want []modelFinding
control []recordRow
}{
{"MX priority key", with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.net.","priority":10}`)),
[]modelFinding{{3, "error", "field_invalid"}, {3, "error", "mx_priority_key"}},
with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.net.","preference":10}`))},
{"relative NS host", with(modelRawRow(3, "", "NS", 3600, `{"host":"ns2.example.net"}`)),
[]modelFinding{{3, "error", "name_not_fqdn"}, {3, "error", "pack_failed"}, {3, "warning", "content_not_canonical"}},
with(modelRawRow(3, "", "NS", 3600, `{"host":"ns2.example.net."}`))},
{"relative MX host", with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.net","preference":10}`)),
[]modelFinding{{3, "error", "name_not_fqdn"}, {3, "error", "pack_failed"}, {3, "warning", "content_not_canonical"}},
with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.net.","preference":10}`))},
{"relative SRV target", with(modelRawRow(3, "_sip._tcp", "SRV", 3600, `{"priority":1,"weight":1,"port":5060,"target":"sip.example.net"}`)),
[]modelFinding{{3, "error", "name_not_fqdn"}, {3, "error", "pack_failed"}, {3, "warning", "content_not_canonical"}},
with(modelRawRow(3, "_sip._tcp", "SRV", 3600, `{"priority":1,"weight":1,"port":5060,"target":"sip.example.net."}`))},
{"relative SOA ns", soa(strings.Replace(modelSOAContent, "ns.example.net.", "ns.example.net", 1)),
[]modelFinding{{1, "error", "name_not_fqdn"}, {1, "error", "pack_failed"}, {1, "warning", "content_not_canonical"}},
soa(modelSOAContent)},
{"relative SOA MBox", soa(strings.Replace(modelSOAContent, "hostmaster.example.org.", "hostmaster.example.org", 1)),
[]modelFinding{{1, "error", "name_not_fqdn"}, {1, "error", "pack_failed"}, {1, "warning", "content_not_canonical"}},
soa(modelSOAContent)},
{"SOA mailbox stored as an address", soa(strings.Replace(modelSOAContent, "hostmaster.example.org.", "hostmaster@example.org.", 1)),
[]modelFinding{{1, "error", "field_invalid"}},
soa(modelSOAContent)},
{"TXT of 256 bytes", with(modelRawRow(3, "text", "TXT", 3600, txt(256))),
[]modelFinding{{3, "warning", "txt_split"}},
with(modelRawRow(3, "text", "TXT", 3600, txt(255)))},
{"different TTL in an RRset", with(modelRawRow(3, "www", "A", 3600, `{"ip":"192.0.2.1"}`), modelRawRow(4, "www", "A", 7200, `{"ip":"192.0.2.2"}`)),
[]modelFinding{{3, "warning", "rrset_ttl_mismatch"}, {4, "warning", "rrset_ttl_mismatch"}},
with(modelRawRow(3, "www", "A", 3600, `{"ip":"192.0.2.1"}`), modelRawRow(4, "www", "A", 3600, `{"ip":"192.0.2.2"}`))},
{"same canonical identity", with(modelRawRow(3, "www", "A", 3600, `{"ip":"192.0.2.1"}`), modelRawRow(4, "www", "A", 3600, `{"ip": "192.0.2.1"}`)),
[]modelFinding{{3, "warning", "duplicate"}, {4, "warning", "duplicate"}},
with(modelRawRow(3, "www", "A", 3600, `{"ip":"192.0.2.1"}`), modelRawRow(4, "www", "A", 3600, `{"ip": "192.0.2.10"}`))},
{"below a zone cut", with(modelRawRow(3, "sub", "NS", 3600, `{"host":"ns.example.net."}`), modelRawRow(4, "www.sub", "A", 3600, `{"ip":"192.0.2.1"}`)),
[]modelFinding{{4, "warning", "below_zone_cut"}},
with(modelRawRow(3, "sub", "NS", 3600, `{"host":"ns.example.net."}`), modelRawRow(4, "sub", "A", 3600, `{"ip":"192.0.2.1"}`))},
{"dynamic SOA serial", soa(strings.Replace(modelSOAContent, `,"serial":2026111800`, "", 1)),
[]modelFinding{{1, "warning", "soa_dynamic_serial"}},
soa(modelSOAContent)},
{"in-zone target without address", with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.org.","preference":10}`)),
[]modelFinding{{3, "warning", "target_no_address"}},
with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.org.","preference":10}`), modelRawRow(4, "mail", "AAAA", 3600, `{"ip":"2001:db8::1"}`))},
} {
t.Run(tc.name, func(t *testing.T) {
if got := modelFindings(checkZone("example.org.", tc.triggered, modelNow)); !slices.Equal(got, tc.want) {
t.Errorf("triggered: got %v, want %v", got, tc.want)
}
if got := modelFindings(checkZone("example.org.", tc.control, modelNow)); len(got) != 0 {
t.Errorf("control: got %v", got)
}
})
}
}