- 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
133 lines
4.7 KiB
Go
133 lines
4.7 KiB
Go
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
package main
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSOASerial(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
current, wanted uint32
|
|
now time.Time
|
|
}{
|
|
{0, 0, modelNow},
|
|
{2026111703, 2026111800, modelNow},
|
|
{2026111800, 2026111801, modelNow},
|
|
{2026111898, 2026111899, modelNow},
|
|
{2026111899, 2026111900, modelNow},
|
|
{2026111900, 2026111901, modelNow},
|
|
{2026113099, 2026120100, modelNow},
|
|
{2026123199, 2027010100, modelNow},
|
|
{2028022899, 2028022900, modelNow},
|
|
{2028022999, 2028030100, modelNow},
|
|
{3000999999, 3001000000, modelNow},
|
|
{4294967294, 4294967295, modelNow},
|
|
{4294967295, 2026111800, modelNow},
|
|
} {
|
|
if got := nextSOASerial(tc.current, tc.now); got != tc.wanted {
|
|
t.Errorf("nextSOASerial(%d) = %d; want %d", tc.current, got, tc.wanted)
|
|
}
|
|
}
|
|
if got := nextSOASerial(^uint32(0), modelNow.AddDate(-2026, -10, -17)); got == 0 {
|
|
t.Fatal("wrapped serial became zero")
|
|
}
|
|
if got := newSOASerial(time.Date(2026, 11, 18, 0, 1, 0, 0, time.FixedZone("UTC+1", 3600))); got != 2026111700 {
|
|
t.Fatalf("UTC initial serial = %d", got)
|
|
}
|
|
}
|
|
|
|
func TestUpdateSOASerial(t *testing.T) {
|
|
current := modelBaseZone()
|
|
changes := zoneChanges{
|
|
Creates: []recordRow{modelRawRow(0, "www", "A", 17, `{"ip":"192.0.2.1"}`)},
|
|
Updates: []recordRow{modelRawRow(2, "", "NS", 17, `{"host":"ns.example.net."}`)},
|
|
Deletes: []int64{42, 43},
|
|
}
|
|
mustBump := func(rows []recordRow, changes zoneChanges, expected uint32) recordRow {
|
|
t.Helper()
|
|
got, err := updateSOASerial(rows, changes, modelNow)
|
|
if err != nil || got == nil {
|
|
t.Fatalf("missing SOA update: %v %v", got, err)
|
|
}
|
|
if serial, present := storedSOASerial(*got); serial != expected || !present {
|
|
t.Fatalf("updated SOA serial = %d (%t); want %d", serial, present, expected)
|
|
}
|
|
return *got
|
|
}
|
|
mustBump(current, changes, 2026111801)
|
|
edited := current[0]
|
|
edited.TTL.Int64 = 19
|
|
edited.Content.String = strings.Replace(modelSOAContent, "2026111800", "1", 1)
|
|
changes.SOAUpdate = &edited
|
|
if got := mustBump(current, changes, 2026111801); got.TTL.Int64 != 19 {
|
|
t.Fatalf("lost edited SOA TTL: %+v", got)
|
|
}
|
|
changes.SOAUpdate = nil
|
|
changes.Updates = append(changes.Updates, edited)
|
|
if got := mustBump(current, changes, 2026111801); got.TTL.Int64 != 19 {
|
|
t.Fatalf("lost SOA in ordinary updates: %+v", got)
|
|
}
|
|
for _, tc := range []struct {
|
|
replacement string
|
|
dynamicKey bool
|
|
}{
|
|
{"", false}, {`,"serial":0`, true},
|
|
} {
|
|
dynamic := modelBaseZone()
|
|
dynamic[0].Content.String = strings.Replace(modelSOAContent, `,"serial":2026111800`, tc.replacement, 1)
|
|
prepared, err := prepareSOAUpdate(dynamic[0], edited)
|
|
serial, hasKey := storedSOASerial(prepared)
|
|
if err != nil || serial != 0 || hasKey != tc.dynamicKey || prepared.ID != dynamic[0].ID {
|
|
t.Fatalf("dynamic SOA not preserved: %+v, %v", prepared, err)
|
|
}
|
|
changes.SOAUpdate = &prepared
|
|
if got, err := updateSOASerial(dynamic, changes, modelNow); err != nil || got != nil {
|
|
t.Fatalf("dynamic write bumped serial: %+v, %v", got, err)
|
|
}
|
|
mustBump(dynamic, zoneChanges{ManageSerial: true}, 2026111800)
|
|
}
|
|
for _, tc := range []struct {
|
|
rows []recordRow
|
|
changes zoneChanges
|
|
}{
|
|
{current, zoneChanges{}}, {current[1:], changes},
|
|
} {
|
|
if row, err := updateSOASerial(tc.rows, tc.changes, modelNow); err != nil || row != nil {
|
|
t.Fatalf("unexpected SOA: %+v, %v", row, err)
|
|
}
|
|
}
|
|
duplicate := current[0]
|
|
duplicate.ID = 99
|
|
ambiguous := append(slices.Clone(current), duplicate)
|
|
if row, err := updateSOASerial(ambiguous, zoneChanges{Creates: changes.Creates}, modelNow); err != nil || row != nil {
|
|
t.Fatalf("bumped ambiguous SOA: %+v, %v", row, err)
|
|
}
|
|
if got := mustBump(ambiguous, zoneChanges{Deletes: []int64{1}}, 2026111801); got.ID != 99 {
|
|
t.Fatalf("bumped the deleted SOA: %+v", got)
|
|
}
|
|
moved := current[0]
|
|
moved.Name = "below"
|
|
if got := mustBump([]recordRow{moved, current[1]}, zoneChanges{Updates: []recordRow{current[0]}}, 2026111801); got.Name != "" {
|
|
t.Fatalf("lost the moved SOA: %+v", got)
|
|
}
|
|
converted := modelRawRow(7, "", "A", 1, `{"ip":"192.0.2.1"}`)
|
|
if row, err := updateSOASerial([]recordRow{converted}, zoneChanges{Updates: []recordRow{modelRawRow(7, "", "SOA", 1, modelSOAContent)}}, modelNow); err != nil || row != nil {
|
|
t.Fatalf("bumped a row that was not an SOA: %+v, %v", row, err)
|
|
}
|
|
if _, err := withSOASerial(recordRow{}, 42, true); err == nil {
|
|
t.Fatal("bumped absent SOA")
|
|
}
|
|
bad := modelRawRow(1, "", "SOA", 1, "bad")
|
|
if _, err := withSOASerial(bad, 42, true); err == nil {
|
|
t.Fatal("bumped malformed SOA")
|
|
}
|
|
if _, err := updateSOASerial([]recordRow{bad}, zoneChanges{ManageSerial: true}, modelNow); err == nil {
|
|
t.Fatal("activated a managed serial on malformed SOA content")
|
|
}
|
|
}
|