fix: close record repair and verification gaps

This commit is contained in:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 9251c85fd8
commit adff599e08
27 files changed
+787 -153

No files matched your search

+85
View File
@@ -4,6 +4,7 @@
package main
import (
"cmp"
"database/sql"
"slices"
"strings"
@@ -47,6 +48,90 @@ func TestValidateZoneChanges(t *testing.T) {
}
}
func TestMixedZoneSOARepair(t *testing.T) {
const zone = "example.org."
extraSOA := modelRawRow(3, "", "SOA", 3600, modelSOAContent)
subSOA := modelRawRow(4, "sub", "SOA", 3600, modelSOAContent)
apexCNAME := modelRawRow(5, "", "CNAME", 3600, `{"host":"target.example."}`)
alias := modelRawRow(6, "www", "CNAME", 3600, `{"host":"target.example."}`)
address := modelRawRow(7, "www", "A", 3600, `{"ip":"192.0.2.4"}`)
current := append(modelBaseZone(), extraSOA, subSOA, apexCNAME, alias, address)
for _, changes := range []zoneChanges{
{Deletes: []int64{address.ID}},
{Deletes: []int64{alias.ID, address.ID}},
{Deletes: []int64{extraSOA.ID, address.ID}},
} {
problems, _ := validateZoneChanges(zone, current, changes)
if len(problems) == 0 {
t.Fatalf("allowed unrelated deletions during SOA repair: %v", changes.Deletes)
}
}
moved := subSOA
moved.Name = ""
problems, _ := validateZoneChanges(zone, current, zoneChanges{SOAUpdate: &moved})
if !slices.Contains(modelFindings(problems), modelFinding{subSOA.ID, "error", "cname_conflict"}) {
t.Fatalf("missed new conflict caused by the SOA move: %+v", problems)
}
for _, changes := range []zoneChanges{
{Deletes: []int64{subSOA.ID}},
{Deletes: []int64{extraSOA.ID}},
{Deletes: []int64{apexCNAME.ID, alias.ID}},
} {
problems, _ := validateZoneChanges(zone, current, changes)
if len(problems) != 0 {
t.Fatalf("rejected repair %v: %+v", changes.Deletes, problems)
}
current = changes.finalRows(current)
}
if want := append(modelBaseZone(), address); !slices.Equal(current, want) {
t.Fatalf("repair changed other records: got %+v, want %+v", current, want)
}
}
func TestDeleteApexCNAMEBeforeSOARepair(t *testing.T) {
const zone = "example.org."
base := modelBaseZone()
apexCNAME := modelRawRow(3, "", "CNAME", 3600, `{"host":"target.example."}`)
healthy := modelRawRow(4, "host", "A", 3600, `{"ip":"192.0.2.4"}`)
misplacedSOA := base[0]
misplacedSOA.Name = "sub"
for _, tc := range []struct {
name string
current []recordRow
restore zoneChanges
}{
{"missing SOA", []recordRow{base[1], apexCNAME, healthy}, zoneChanges{Creates: base[:1]}},
{"non-apex SOA", []recordRow{misplacedSOA, base[1], apexCNAME, healthy}, zoneChanges{SOAUpdate: &base[0]}},
} {
t.Run(tc.name, func(t *testing.T) {
for _, changes := range []zoneChanges{
{Deletes: []int64{apexCNAME.ID, healthy.ID}},
{Creates: []recordRow{modelRawRow(5, "other", "A", 3600, `{"ip":"192.0.2.5"}`)}},
} {
if problems, _ := validateZoneChanges(zone, tc.current, changes); len(problems) == 0 {
t.Fatalf("allowed unrelated change during SOA repair: %+v", changes)
}
}
deleteCNAME := zoneChanges{Deletes: []int64{apexCNAME.ID}}
if problems, _ := validateZoneChanges(zone, tc.current, deleteCNAME); len(problems) != 0 {
t.Fatalf("failed to clear the apex conflict: %+v", problems)
}
current := deleteCNAME.finalRows(tc.current)
if problems, _ := validateZoneChanges(zone, current, tc.restore); len(problems) != 0 {
t.Fatalf("failed to restore SOA: %+v", problems)
}
want := append(slices.Clone(base), healthy)
got := tc.restore.finalRows(current)
slices.SortFunc(got, func(a, b recordRow) int { return cmp.Compare(a.ID, b.ID) })
if !slices.Equal(got, want) {
t.Fatalf("repair changed healthy records: got %+v, want %+v", got, want)
}
})
}
}
func TestZoneRuleWarnings(t *testing.T) {
creates := []recordRow{
modelRawRow(3, "alias", "CNAME", 3600, `{"host":"www.example.net."}`),