346 lines
15 KiB
Go
346 lines
15 KiB
Go
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
package main
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
var modelNow = time.Date(2026, 11, 18, 15, 0, 0, 0, time.UTC)
|
|
|
|
func modelRawRow(id int64, name, recordType string, ttl int64, content string) recordRow {
|
|
return recordRow{id, "example.org.", name, sql.NullInt64{Int64: ttl, Valid: true}, sql.NullString{String: content, Valid: true}, recordType}
|
|
}
|
|
|
|
func modelValidatedRow(t *testing.T, id int64, name, typ string, ttl int64, content string) recordRow {
|
|
t.Helper()
|
|
row, fields := validateRecord("example.org.", name, typ, ttl, json.RawMessage(content), modelNow)
|
|
if len(fields) != 0 {
|
|
t.Fatalf("%s %s invalid: %v", name, typ, fields)
|
|
}
|
|
row.ID = id
|
|
return row
|
|
}
|
|
|
|
func TestRecordCanonicalEncodingAndPluginRR(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
typ, name, input, canonical, rdata string
|
|
}{
|
|
{"A", "WWW", `{"ignored":42,"ip":"192.0.2.10"}`, `{"ip":"192.0.2.10"}`, "192.0.2.10"},
|
|
{"AAAA", "www", `{"ip":"2001:0DB8:0000:0000::10"}`, `{"ip":"2001:db8::10"}`, "2001:db8::10"},
|
|
{"CNAME", "alias", `{"host":"@"}`, `{"host":"example.org."}`, "example.org."},
|
|
{"NS", "", `{"host":"Ns.Example.Net"}`, `{"host":"ns.example.net."}`, "ns.example.net."},
|
|
{"MX", "", `{"preference":65535,"host":"Mail.Example.Org","priority":23}`, `{"host":"mail.example.org.","preference":65535}`, "65535 mail.example.org."},
|
|
{"MX", "", `{"host":".","preference":0}`, `{"host":".","preference":0}`, "0 ."},
|
|
{"TXT", "", `{"text":"a \"quoted\" value"}`, `{"text":"a \"quoted\" value"}`, `"a \"quoted\" value"`},
|
|
{"SRV", "_https._tcp.dev", `{"port":443,"weight":0,"priority":65535,"target":"@"}`, `{"priority":65535,"weight":0,"port":443,"target":"example.org."}`, "65535 0 443 example.org."},
|
|
{"SRV", "_https._tcp", `{"port":0,"weight":65535,"priority":0,"target":"."}`, `{"priority":0,"weight":65535,"port":0,"target":"."}`, "0 65535 0 ."},
|
|
{"CAA", "", `{"value":"ca.example.net","tag":"issue","flag":0}`, `{"flag":0,"tag":"issue","value":"ca.example.net"}`, `0 issue "ca.example.net"`},
|
|
{"SOA", "@", `{"MBox":"First.Last@EXAMPLE.ORG","ns":"Ns.Example.Net","serial":4294967295,"minttl":0,"expire":2147483647,"retry":600,"refresh":3600}`,
|
|
`{"ns":"ns.example.net.","MBox":"first\\.last.example.org.","refresh":3600,"retry":600,"expire":2147483647,"minttl":0,"serial":4294967295}`,
|
|
`ns.example.net. first\.last.example.org. 4294967295 3600 600 2147483647 0`},
|
|
} {
|
|
t.Run(tc.typ+"/"+tc.name+"/"+tc.rdata, func(t *testing.T) {
|
|
row := modelValidatedRow(t, 42, tc.name, tc.typ, 0, tc.input)
|
|
if row.Content.String != tc.canonical {
|
|
t.Fatalf("content = %s; want %s", row.Content.String, tc.canonical)
|
|
}
|
|
rr, err := recordRR(row, modelNow)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want, err := dns.NewRR(fmt.Sprintf("%s 360 IN %s %s", recordFQDN(row), tc.typ, tc.rdata))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rr.String() != want.String() {
|
|
t.Fatalf("RR = %q; want %q", rr, want)
|
|
}
|
|
if err := packRecordRR(rr); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tc.typ != "TXT" && tc.typ != "SOA" && recordValue(row, modelNow) != tc.rdata {
|
|
t.Errorf("presentation = %q; want %q", recordValue(row, modelNow), tc.rdata)
|
|
}
|
|
data, err := decodeRecord(tc.typ, tc.input)
|
|
if err != nil || reflect.TypeOf(data) != recordRules[tc.typ].dataType {
|
|
t.Fatalf("typed decoder = %v, %v", data, err)
|
|
}
|
|
})
|
|
}
|
|
if _, err := encodeRecord(struct{}{}); err == nil {
|
|
t.Fatal("encoded unsupported data")
|
|
}
|
|
}
|
|
|
|
func TestRecordFieldValidation(t *testing.T) {
|
|
cases := []struct {
|
|
typ, name, content, field string
|
|
ttl int64
|
|
}{
|
|
{"A", "x", `{"ip":"192.0.2.1"}`, "", 2147483647},
|
|
{"A", "x", `{"ip":"192.0.2.1"}`, "ttl", 2147483648},
|
|
{"A", "x", `{"ip":"192.0.2.1"}`, "ttl", -1},
|
|
{"A", "other.org.", `{"ip":"192.0.2.1"}`, "name", 0},
|
|
{"A", "x", `{"ip":"::1"}`, "ip", 0},
|
|
{"A", "x", `{"ip":"::ffff:192.0.2.1"}`, "ip", 0},
|
|
{"A", "x", `{"ip":"192.000.2.1"}`, "ip", 0},
|
|
{"A", "x", `{}`, "ip", 0},
|
|
{"A", "x", `{"ip":null}`, "ip", 0},
|
|
{"A", "x", `[]`, "content", 0},
|
|
{"a", "x", `{"ip":"192.0.2.1"}`, "type", 0},
|
|
{"AAAA", "x", `{"ip":"::ffff:192.0.2.1"}`, "ip", 0},
|
|
{"AAAA", "x", `{"ip":"192.0.2.1"}`, "ip", 0},
|
|
{"AAAA", "x", `{"ip":"fe80::1%eth0"}`, "ip", 0},
|
|
{"CNAME", "@", `{"host":"example.net."}`, "name", 0},
|
|
{"CNAME", "x", `{"host":"."}`, "host", 0},
|
|
{"NS", "*.dev", `{"host":"example.net."}`, "name", 0},
|
|
{"NS", "*", `{"host":"example.net."}`, "name", 0},
|
|
{"MX", "x", `{"host":"mail.example.","preference":"10"}`, "preference", 0},
|
|
{"MX", "x", `{"host":"mail.example.","preference":-1}`, "preference", 0},
|
|
{"MX", "x", `{"host":"mail.example.","preference":65536}`, "preference", 0},
|
|
{"MX", "x", `{"host":"mail.example.","preference":0.5}`, "preference", 0},
|
|
{"MX", "x", `{"host":"mail.example.","priority":10}`, "preference", 0},
|
|
{"TXT", "x", `{"text":"\""}`, "", 0},
|
|
{"TXT", "x", `{"text":""}`, "text", 0},
|
|
{"TXT", "x", `{"text":"\\\""}`, "text", 0},
|
|
{"TXT", "x", `{"text":"é"}`, "text", 0},
|
|
{"TXT", "x", `{"text":"\u007f"}`, "text", 0},
|
|
{"TXT", "x", `{"text":"\n"}`, "text", 0},
|
|
{"TXT", "x", `{"text":"` + strings.Repeat("x", 4096) + `"}`, "", 0},
|
|
{"TXT", "x", `{"text":"` + strings.Repeat("x", 4097) + `"}`, "text", 0},
|
|
{"SRV", "service._tcp", `{"port":0,"weight":0,"priority":0,"target":"."}`, "name", 0},
|
|
{"SRV", "_service._", `{"port":0,"weight":0,"priority":0,"target":"."}`, "name", 0},
|
|
{"SRV", "_service._tcp", `{"port":65536,"weight":0,"priority":0,"target":"."}`, "port", 0},
|
|
{"SRV", "_service._tcp", `{"port":0,"weight":65536,"priority":0,"target":"."}`, "weight", 0},
|
|
{"SRV", "_service._tcp", `{"port":0,"weight":0,"priority":65536,"target":"."}`, "priority", 0},
|
|
{"CAA", "x", `{"flag":128,"tag":"a1234567890abcd","value":"` + strings.Repeat("x", 1024) + `"}`, "", 0},
|
|
{"CAA", "x", `{"flag":128,"tag":"issue","value":"` + strings.Repeat("x", 1025) + `"}`, "value", 0},
|
|
{"CAA", "x", `{"flag":256,"tag":"issue","value":"ca.example"}`, "flag", 0},
|
|
{"CAA", "x", `{"flag":127,"tag":"issue","value":"ca.example"}`, "flag", 0},
|
|
{"CAA", "x", `{"flag":0,"tag":"Issue","value":"ca.example"}`, "tag", 0},
|
|
{"CAA", "x", `{"flag":0,"tag":"a1234567890abcde","value":"ca.example"}`, "tag", 0},
|
|
{"CAA", "x", `{"flag":0,"tag":"issue","value":""}`, "value", 0},
|
|
{"SOA", "child", `{"ns":"ns.example.","MBox":"hostmaster.example.","refresh":0,"retry":0,"expire":0,"minttl":0}`, "name", 0},
|
|
{"SOA", "@", `{"ns":"ns.example.","MBox":"hostmaster.example.","refresh":2147483648,"retry":0,"expire":0,"minttl":0}`, "refresh", 0},
|
|
{"SOA", "@", `{"ns":"ns.example.","MBox":"hostmaster.example.","refresh":0,"retry":0,"expire":0,"minttl":0,"serial":4294967296}`, "serial", 0},
|
|
}
|
|
for i, tc := range cases {
|
|
_, fields := validateRecord("example.org.", tc.name, tc.typ, tc.ttl, json.RawMessage(tc.content), modelNow)
|
|
if tc.field == "" && len(fields) != 0 || tc.field != "" && fields[tc.field] == "" {
|
|
t.Errorf("case %d for %s wanted field %q, got %v", i, tc.typ, tc.field, fields)
|
|
}
|
|
}
|
|
_, fields := validateRecord("example.org.", "_x._y", "SRV", 0, json.RawMessage(`{"port":"bad","weight":-1,"target":null}`), modelNow)
|
|
if len(fields) != 4 {
|
|
t.Fatalf("combined field errors = %v", fields)
|
|
}
|
|
if _, err := decodeRecord("MX", `{"preference":"10","host":"mail.example."}`); err == nil {
|
|
t.Fatal("numeric string decoded")
|
|
}
|
|
if err := packRecordRR(nil); err == nil {
|
|
t.Fatal("packed a missing RR")
|
|
}
|
|
}
|
|
|
|
func TestPluginRRLegacyRows(t *testing.T) {
|
|
for _, size := range []int{1, 254, 255, 256, 510, 4096} {
|
|
text := strings.Repeat("x", size)
|
|
row := modelValidatedRow(t, 1, "txt", "TXT", 3600, fmt.Sprintf(`{"text":%q}`, text))
|
|
rr, err := recordRR(row, modelNow)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
parts := rr.(*dns.TXT).Txt
|
|
if len(parts) != size/255+1 || strings.Join(parts, "") != text || len(parts[len(parts)-1]) != size%255 {
|
|
t.Errorf("plugin split of %d bytes: %d strings, final length %d", size, len(parts), len(parts[len(parts)-1]))
|
|
}
|
|
}
|
|
cname := modelRawRow(1, "alias", "CNAME", 11, `{"host":"Target"}`)
|
|
if rr, err := recordRR(cname, modelNow); err != nil || rr.(*dns.CNAME).Target != "Target." {
|
|
t.Fatalf("raw CNAME = %v, %v", rr, err)
|
|
}
|
|
mx := modelRawRow(1, "", "MX", 0, `{"host":"mail.example.","priority":10}`)
|
|
if rr, err := recordRR(mx, modelNow); err != nil || rr.(*dns.MX).Preference != 0 || rr.Header().Ttl != 360 {
|
|
t.Fatalf("raw MX = %v, %v", rr, err)
|
|
}
|
|
soa := modelRawRow(1, "child", "SOA", 0, `{}`)
|
|
rr, err := recordRR(soa, modelNow)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value := rr.(*dns.SOA)
|
|
if value.Hdr.Name != "child.example.org." || value.Ns != "ns1.child" || value.Mbox != "hostmaster.child" ||
|
|
value.Refresh != 86400 || value.Retry != 7200 || value.Expire != 3600 || value.Minttl != 360 ||
|
|
value.Serial != uint32(modelNow.Unix()) || packRecordRR(rr) == nil {
|
|
t.Fatalf("unexpected SOA defaults: %v", value)
|
|
}
|
|
soa.Content.String = `{"ns":"ns.example.","MBox":"hostmaster.example.","refresh":0,"retry":0,"expire":0,"minttl":0}`
|
|
if rr, err := recordRR(soa, modelNow); err != nil || rr.Header().Name != soa.Zone || rr.(*dns.SOA).Serial != uint32(modelNow.Unix()) {
|
|
t.Fatalf("raw populated SOA = %v, %v", rr, err)
|
|
}
|
|
for _, typ := range []string{"A", "AAAA", "CNAME", "NS", "MX", "TXT", "SRV", "CAA"} {
|
|
row := modelRawRow(1, "child", typ, 0, `{}`)
|
|
if rr, err := recordRR(row, modelNow); rr != nil || err != nil || recordValue(row, modelNow) != "{}" {
|
|
t.Errorf("raw empty %s = %v, %v", typ, rr, err)
|
|
}
|
|
}
|
|
for _, row := range []recordRow{{}, modelRawRow(1, "", "A", 0, `{"ip":"bad"}`), modelRawRow(1, "", "A", 0, "null"),
|
|
modelRawRow(1, "", "A", 4294967296, `{"ip":"192.0.2.1"}`)} {
|
|
if _, err := recordRR(row, modelNow); err == nil {
|
|
t.Errorf("built invalid raw RR: %+v", row)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRecordETag(t *testing.T) {
|
|
row := modelRawRow(1, "x", "A", 0, "")
|
|
row.TTL.Valid = false
|
|
row.Content.Valid = false
|
|
want := fmt.Sprintf("\"%x\"", sha256.Sum256([]byte("1:112:example.org.1:xN1:AN")))
|
|
got := recordETag(row)
|
|
if got != want {
|
|
t.Fatalf("ETag = %s; want %s", got, want)
|
|
}
|
|
for _, mutate := range []func(*recordRow){
|
|
func(r *recordRow) { r.ID++ }, func(r *recordRow) { r.Zone = "example.net." },
|
|
func(r *recordRow) { r.Name = "y" }, func(r *recordRow) { r.RecordType = "TXT" },
|
|
func(r *recordRow) { r.TTL.Valid = true }, func(r *recordRow) { r.Content.Valid = true },
|
|
} {
|
|
changed := row
|
|
mutate(&changed)
|
|
if recordETag(changed) == got {
|
|
t.Fatalf("unchanged ETag for %+v", changed)
|
|
}
|
|
}
|
|
row.TTL.Int64, row.Content.String = 33, "ignored"
|
|
if recordETag(row) != got {
|
|
t.Fatal("NULL markers included unused backing values")
|
|
}
|
|
}
|
|
|
|
func TestNewZoneRows(t *testing.T) {
|
|
spec := newZoneSpec{"EXAMPLE.ORG", "ns1.example.net", "Root@Example.Org", []string{"NS2.EXAMPLE.NET"}, 3600, 600, 604800, 300, 3600}
|
|
rows, fields := newZoneRows(spec, modelNow)
|
|
if len(fields) != 0 || len(rows) != 3 || rows[0].RecordType != "SOA" || rows[1].RecordType != "NS" || rows[2].RecordType != "NS" {
|
|
t.Fatalf("new zone = %+v, %v", rows, fields)
|
|
}
|
|
if serial, present := storedSOASerial(rows[0]); !present || serial != 2026111800 || rows[2].Content.String != `{"host":"ns2.example.net."}` {
|
|
t.Fatalf("unexpected new zone content: %+v", rows)
|
|
}
|
|
for _, tc := range []struct {
|
|
change func(*newZoneSpec)
|
|
field string
|
|
}{
|
|
{func(s *newZoneSpec) { s.Zone = "." }, "zone"},
|
|
{func(s *newZoneSpec) { s.Mailbox = "x@y@z" }, "MBox"},
|
|
{func(s *newZoneSpec) { s.TTL = -1 }, "ttl"},
|
|
{func(s *newZoneSpec) { s.ExtraNS = []string{"."} }, "extra_ns.0"},
|
|
{func(s *newZoneSpec) { s.ExtraNS = []string{"NS1.example.net."} }, "extra_ns.0"},
|
|
} {
|
|
invalid := spec
|
|
tc.change(&invalid)
|
|
if rows, fields := newZoneRows(invalid, modelNow); len(rows) != 0 || fields[tc.field] == "" {
|
|
t.Errorf("wanted %s error, got %v", tc.field, fields)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRecordFieldBoundaries(t *testing.T) {
|
|
soa := func(key string, value int64) string {
|
|
timers := map[string]int64{"refresh": 3600, "retry": 600, "expire": 604800, "minttl": 300}
|
|
timers[key] = value
|
|
return fmt.Sprintf(`{"ns":"ns.example.net.","MBox":"hostmaster.example.org.","refresh":%d,"retry":%d,"expire":%d,"minttl":%d}`,
|
|
timers["refresh"], timers["retry"], timers["expire"], timers["minttl"])
|
|
}
|
|
caa := func(value string) string {
|
|
encoded, _ := json.Marshal(value)
|
|
return `{"flag":0,"tag":"issue","value":` + string(encoded) + `}`
|
|
}
|
|
type boundary struct {
|
|
typ, name, content, field string
|
|
}
|
|
cases := []boundary{
|
|
{"SRV", "_sip._tcp", `{"priority":65535,"weight":65535,"port":65535,"target":"."}`, ""},
|
|
{"SRV", "_sip._tcp", `{"priority":0,"weight":0,"port":65536,"target":"."}`, "port"},
|
|
{"SRV", "_sip._tcp", `{"priority":0,"weight":0,"port":-1,"target":"."}`, "port"},
|
|
{"CAA", "", caa(" "), ""},
|
|
{"CAA", "", caa("~"), ""},
|
|
{"CAA", "", caa(" !\"#$%&'()*+,-./0123456789:;<=>?@AZ[]^_`az{|}~"), ""},
|
|
{"CAA", "", caa("\x1f"), "value"},
|
|
{"CAA", "", caa("\x7f"), "value"},
|
|
{"CAA", "", caa("a\\b"), "value"},
|
|
{"CAA", "", caa("a\tb"), "value"},
|
|
{"CAA", "", caa("é"), "value"},
|
|
}
|
|
for _, key := range []string{"refresh", "retry", "expire", "minttl"} {
|
|
cases = append(cases, boundary{"SOA", "", soa(key, 2147483647), ""}, boundary{"SOA", "", soa(key, 2147483648), key})
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.typ+"/"+tc.content, func(t *testing.T) {
|
|
row, fields := validateRecord("example.org.", tc.name, tc.typ, 3600, json.RawMessage(tc.content), modelNow)
|
|
if tc.field == "" {
|
|
if len(fields) != 0 {
|
|
t.Fatalf("refused: %v", fields)
|
|
}
|
|
var want []modelFinding
|
|
if tc.typ == "SOA" {
|
|
want = []modelFinding{{0, "warning", "soa_dynamic_serial"}}
|
|
}
|
|
if got := modelFindings(checkRecord(row, modelNow)); !slices.Equal(got, want) {
|
|
t.Fatalf("check of an accepted record: %v, want %v", got, want)
|
|
}
|
|
if rr, err := recordRR(row, modelNow); err != nil || packRecordRR(rr) != nil {
|
|
t.Fatalf("accepted record does not pack: %v, %v", rr, err)
|
|
}
|
|
return
|
|
}
|
|
if len(fields) != 1 || fields[tc.field] == "" {
|
|
t.Fatalf("fields = %v, want only %q", fields, tc.field)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInspectRecordContentCache(t *testing.T) {
|
|
limit := decodedCacheLimit
|
|
decodedCacheLimit = 2
|
|
t.Cleanup(func() { decodedCacheLimit = limit })
|
|
decodedCache.Lock()
|
|
clear(decodedCache.entries)
|
|
decodedCache.Unlock()
|
|
cached := func() int {
|
|
decodedCache.Lock()
|
|
defer decodedCache.Unlock()
|
|
return len(decodedCache.entries)
|
|
}
|
|
first, err := inspectRecordContent("A", `{"ip":"192.0.2.1"}`)
|
|
if err != nil || first.data != (aData{IP: "192.0.2.1"}) || cached() != 1 {
|
|
t.Fatalf("first decode: %+v, %v, %d cached", first, err, cached())
|
|
}
|
|
again, err := inspectRecordContent("A", `{"ip":"192.0.2.1"}`)
|
|
if err != nil || !reflect.DeepEqual(again, first) || cached() != 1 {
|
|
t.Fatalf("cached decode: %+v, %v, %d cached", again, err, cached())
|
|
}
|
|
if _, err := inspectRecordContent("BOGUS", `{}`); err == nil || cached() != 2 {
|
|
t.Fatalf("unknown type: %v, %d cached", err, cached())
|
|
}
|
|
if _, err := inspectRecordContent("BOGUS", `{}`); err == nil || err.Error() != "unknown record type" {
|
|
t.Fatalf("cached error: %v", err)
|
|
}
|
|
if _, err := inspectRecordContent("A", `{"ip":"192.0.2.2"}`); err != nil || cached() != 1 {
|
|
t.Fatalf("eviction: %v, %d cached", err, cached())
|
|
}
|
|
}
|