Files
coredns-web-ui/rules_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

145 lines
8.1 KiB
Go

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
package main
import (
"database/sql"
"slices"
"strings"
"testing"
)
func TestValidateZoneChanges(t *testing.T) {
soa := modelRawRow(3, "", "SOA", 3600, modelSOAContent)
a := modelRawRow(4, "www", "A", 3600, `{"ip":"192.0.2.4"}`)
cname := modelRawRow(5, "www", "CNAME", 3600, `{"host":"target.example."}`)
for _, tc := range []struct {
name, code string
current []recordRow
changes zoneChanges
}{
{"missing SOA", "soa_missing", nil, zoneChanges{Creates: []recordRow{a}}},
{"second SOA", "soa_multiple", modelBaseZone(), zoneChanges{Creates: []recordRow{soa}}},
{"recreated SOA", "soa_multiple", modelBaseZone(), zoneChanges{Creates: []recordRow{soa}, Deletes: []int64{1}}},
{"deleted SOA", "soa_delete", modelBaseZone(), zoneChanges{Deletes: []int64{1}}},
{"renamed SOA", "soa_not_apex", modelBaseZone(), zoneChanges{SOAUpdate: &recordRow{ID: 1, Name: "sub", RecordType: "SOA"}}},
{"last NS", "ns_apex_missing", modelBaseZone(), zoneChanges{Deletes: []int64{2}}},
{"moved last NS", "ns_apex_missing", modelBaseZone(), zoneChanges{Updates: []recordRow{modelRawRow(2, "sub", "NS", 3600, `{"host":"ns2.example.net."}`)}}},
{"apex CNAME", "cname_apex", modelBaseZone(), zoneChanges{Creates: []recordRow{modelRawRow(5, "", "CNAME", 3600, cname.Content.String)}}},
{"second CNAME", "cname_conflict", append(modelBaseZone(), cname), zoneChanges{Creates: []recordRow{modelRawRow(6, "WWW", "CNAME", 3600, `{"host":"other.example."}`)}}},
{"CNAME with another type", "cname_conflict", append(modelBaseZone(), cname), zoneChanges{Creates: []recordRow{a}}},
{"duplicate", "duplicate", append(modelBaseZone(), a), zoneChanges{Creates: []recordRow{modelRawRow(7, "WWW", "A", 10, `{ "ip":"192.0.2.4","else":42 }`)}}},
{"replaced apex NS", "", modelBaseZone(), zoneChanges{Deletes: []int64{2}, Creates: []recordRow{modelRawRow(3, "", "NS", 3600, `{"host":"ns2.example.net."}`)}}},
{"atomic CNAME replacement", "", append(modelBaseZone(), cname), zoneChanges{Deletes: []int64{5}, Creates: []recordRow{a}}},
{"already absent apex NS", "", modelBaseZone()[:1], zoneChanges{Creates: []recordRow{a}}},
{"normal SOA update", "", modelBaseZone(), zoneChanges{SOAUpdate: &recordRow{ID: 1, RecordType: "SOA"}}},
} {
t.Run(tc.name, func(t *testing.T) {
problems, warnings := validateZoneChanges("example.org.", tc.current, tc.changes)
if tc.code == "" && len(problems) != 0 || tc.code != "" && !modelProblem(problems, "error", tc.code) {
t.Errorf("wanted %q, got %+v", tc.code, problems)
}
if modelProblem(warnings, "warning", "duplicate") {
t.Errorf("duplicate remained a warning: %+v", warnings)
}
})
}
}
func TestZoneRuleWarnings(t *testing.T) {
creates := []recordRow{
modelRawRow(3, "alias", "CNAME", 3600, `{"host":"www.example.net."}`),
modelRawRow(4, "sub", "NS", 3600, `{"host":"alias.example.org."}`),
modelRawRow(5, "_x._y.sub", "SRV", 3600, `{"priority":0,"weight":0,"port":0,"target":"www.example.org."}`),
modelRawRow(6, "www", "A", 3600, `{"ip":"192.0.2.6"}`),
modelRawRow(7, "www", "A", 7200, `{"ip":"192.0.2.7"}`),
modelRawRow(8, "text", "TXT", 3600, `{"text":"`+strings.Repeat("x", 256)+`"}`),
modelRawRow(9, "", "MX", 3600, `{"host":".","preference":0}`),
modelRawRow(10, "_a._b", "SRV", 3600, `{"priority":0,"weight":0,"port":0,"target":"."}`),
}
problems, warnings := validateZoneChanges("example.org.", modelBaseZone(), zoneChanges{Creates: creates})
if len(problems) != 0 {
t.Fatal(problems)
}
for _, code := range []string{"rrset_ttl_mismatch", "target_is_cname", "target_no_address", "below_zone_cut", "txt_split"} {
if !modelProblem(warnings, "warning", code) {
t.Errorf("missing %s in %+v", code, warnings)
}
}
for _, warning := range warnings {
if warning.RecordID == 5 && warning.Code != "below_zone_cut" || warning.RecordID == 9 || warning.RecordID == 10 {
t.Errorf("unexpected warning %+v", warning)
}
}
if problems, warnings := validateZoneChanges("example.org.", modelBaseZone(), zoneChanges{}); len(problems)+len(warnings) != 0 {
t.Fatalf("clean zone reported %+v %+v", problems, warnings)
}
}
func TestValidateZoneChangePairs(t *testing.T) {
a := func(id int64, name string, ttl int64, ip string) recordRow {
return modelRawRow(id, name, "A", ttl, `{"ip":"`+ip+`"}`)
}
txt := func(size int) recordRow {
return modelRawRow(5, "text", "TXT", 3600, `{"text":"`+strings.Repeat("x", size)+`"}`)
}
cut := modelRawRow(4, "sub", "NS", 3600, `{"host":"ns.example.net."}`)
for _, tc := range []struct {
name string
current []recordRow
triggered, control zoneChanges
problems, warnings []modelFinding
}{
{"same canonical identity", append(modelBaseZone(), a(3, "www", 3600, "192.0.2.1")),
zoneChanges{Creates: []recordRow{modelRawRow(5, "www", "A", 3600, `{"ip": "192.0.2.1"}`)}},
zoneChanges{Creates: []recordRow{a(5, "www", 3600, "192.0.2.2")}},
[]modelFinding{{3, "error", "duplicate"}, {5, "error", "duplicate"}}, nil},
{"different TTL in an RRset", append(modelBaseZone(), a(3, "www", 3600, "192.0.2.1")),
zoneChanges{Creates: []recordRow{a(5, "www", 60, "192.0.2.2")}},
zoneChanges{Creates: []recordRow{a(5, "www", 3600, "192.0.2.2")}},
nil, []modelFinding{{3, "warning", "rrset_ttl_mismatch"}, {5, "warning", "rrset_ttl_mismatch"}}},
{"below a zone cut", append(modelBaseZone(), cut),
zoneChanges{Creates: []recordRow{a(5, "host.sub", 3600, "192.0.2.1")}},
zoneChanges{Creates: []recordRow{a(5, "sub", 3600, "192.0.2.1")}},
nil, []modelFinding{{5, "warning", "below_zone_cut"}}},
{"TXT of 256 bytes", modelBaseZone(),
zoneChanges{Creates: []recordRow{txt(256)}}, zoneChanges{Creates: []recordRow{txt(255)}},
nil, []modelFinding{{5, "warning", "txt_split"}}},
{"in-zone CNAME target", append(modelBaseZone(), modelRawRow(3, "alias", "CNAME", 3600, `{"host":"www.example.net."}`)),
zoneChanges{Creates: []recordRow{modelRawRow(5, "", "MX", 3600, `{"host":"alias.example.org.","preference":10}`)}},
zoneChanges{Creates: []recordRow{modelRawRow(5, "", "MX", 3600, `{"host":"mail.example.net.","preference":10}`)}},
nil, []modelFinding{{5, "warning", "target_is_cname"}, {5, "warning", "target_no_address"}}},
{"SOA moved below the apex", modelBaseZone(),
zoneChanges{SOAUpdate: &recordRow{ID: 1, Name: "sub", RecordType: "SOA"}},
zoneChanges{SOAUpdate: &recordRow{ID: 1, RecordType: "SOA"}},
[]modelFinding{{1, "error", "soa_not_apex"}}, nil},
{"extra SOA deleted", append(modelBaseZone(), modelRawRow(3, "", "SOA", 3600, strings.Replace(modelSOAContent, "ns.example.net.", "ns2.example.net.", 1))),
zoneChanges{Creates: []recordRow{a(5, "www", 3600, "192.0.2.1")}},
zoneChanges{Deletes: []int64{3}},
[]modelFinding{{1, "error", "soa_multiple"}, {3, "error", "soa_multiple"}}, nil},
{"non-apex SOA moved to the apex", []recordRow{modelRawRow(1, "sub", "SOA", 3600, modelSOAContent), modelBaseZone()[1]},
zoneChanges{Creates: []recordRow{a(5, "www", 3600, "192.0.2.1")}},
zoneChanges{SOAUpdate: &recordRow{ID: 1, RecordType: "SOA", Content: sql.NullString{String: modelSOAContent, Valid: true}}},
[]modelFinding{{1, "error", "soa_not_apex"}}, nil},
{"last SOA deleted", []recordRow{modelRawRow(1, "sub", "SOA", 3600, modelSOAContent), modelBaseZone()[1]},
zoneChanges{Deletes: []int64{1}},
zoneChanges{SOAUpdate: &recordRow{ID: 1, RecordType: "SOA", Content: sql.NullString{String: modelSOAContent, Valid: true}}},
[]modelFinding{{0, "error", "soa_missing"}, {1, "error", "soa_delete"}}, nil},
} {
t.Run(tc.name, func(t *testing.T) {
problems, warnings := validateZoneChanges("example.org.", tc.current, tc.triggered)
if got := modelFindings(problems); !slices.Equal(got, tc.problems) {
t.Errorf("problems: got %v, want %v", got, tc.problems)
}
if got := modelFindings(warnings); !slices.Equal(got, tc.warnings) {
t.Errorf("warnings: got %v, want %v", got, tc.warnings)
}
problems, warnings = validateZoneChanges("example.org.", tc.current, tc.control)
if len(problems)+len(warnings) != 0 {
t.Errorf("control: problems %v, warnings %v", modelFindings(problems), modelFindings(warnings))
}
})
}
}