- feat: implement dns record model and zone checks - feat: implement zone file import and export
77 lines
4.1 KiB
Go
77 lines
4.1 KiB
Go
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
package main
|
|
|
|
import (
|
|
"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)
|
|
}
|
|
}
|