Files
coredns-web-ui/rules_test.go
T

250 lines
12 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"
)
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 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."}`),
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))
}
})
}
}
func TestRepairMalformedSOAWithConflictingCNAME(t *testing.T) {
base := modelBaseZone()
broken := base[0]
broken.Content.String = strings.Replace(broken.Content.String, `"refresh":3600`, `"refresh":"invalid"`, 1)
if validSOAContent(broken) {
t.Fatal("test SOA must have invalid content")
}
cname := modelRawRow(3, "", "CNAME", 3600, `{"host":"target.example."}`)
current := []recordRow{broken, base[1], cname}
repair := zoneChanges{SOAUpdate: &base[0]}
if problems, _ := validateZoneChanges(base[0].Zone, current, repair); len(problems) != 0 {
t.Fatalf("SOA repair rejected: %+v", problems)
}
current = repair.finalRows(current)
remove := zoneChanges{Deletes: []int64{cname.ID}}
if problems, _ := validateZoneChanges(base[0].Zone, current, remove); len(problems) != 0 {
t.Fatalf("CNAME removal rejected after SOA repair: %+v", problems)
}
}