Files
Gary HansenandClaude Fable 5 d71c7fbef2 feat: rework engine and CLI for dnstraverse parity
Port the traversal engine to the Ruby dnstraverse model so behaviour and
output match dns.squish.net:

- dns: single RD=0 query path (RD=1 only for upstream root discovery),
  per-run packet cache, EDNS0 512-fallback with warnings, UDP->TCP on
  truncation; fix --retries 0 and --root-server IP-literal handling;
  drop all hardcoded 127.0.0.1:53 resolvers
- traverse: hierarchical per-branch InfoCache, 7-step response
  classification with the full 10-status vocabulary, bailiwick
  partitioning, strictly-deeper lame-referral rule, refid grammar with
  .0 resolve subtrees and childset digits, per-IP branching at 1/n
  weight, cache-based glue resolution with noglue/loop dead ends, CNAME
  restarts from the deepest cached zone, fast-mode memoization,
  probability aggregation with Ruby-identical stats keys (sums to 1.0)
- output: byte-for-byte reference text format pinned by a golden test,
  reference CLI defaults, working --quiet/--show-X=false, TTY-aware
  colour, deduplicated deterministic JSON
- web: adapt API/SPA to the new engine, SSE events carry refid/status,
  fix subscribe/snapshot duplicate-event race and a statusCls TDZ bug,
  align SPA type list with the backend
- delete the old engine and dead code (net -4,350 lines)

Verified against live runs of the reference Ruby engine across five
domains (answers, NXDOMAIN, null MX, CNAME restart, glueless resolve)
with no divergences beyond the documented typo fixes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 21:42:06 +10:00

72 lines
2.0 KiB
Go

package traverse
import (
"testing"
"github.com/miekg/dns"
)
func TestEventStageStrings(t *testing.T) {
tests := map[EventStage]string{
StageNew: "new",
StageStart: "start",
StageNewReferralSet: "new_referral_set",
StageNewFast: "new_fast",
StageResolve: "resolve",
StageAnswer: "answer",
StageAnswerFast: "answer_fast",
EventStage(99): "unknown",
}
for stage, want := range tests {
if got := stage.String(); got != want {
t.Errorf("EventStage(%d).String() = %q, want %q", stage, got, want)
}
}
}
func TestHooksEmitNilSafe(t *testing.T) {
var h *TraverserHooks
h.emit(StageNew, newTestReferral("ns1.example.com", nil), "") // must not panic
(&TraverserHooks{}).emit(StageNew, newTestReferral("ns1.example.com", nil), "")
(&TraverserHooks{OnEvent: func(TraversalEvent) { t.Fatal("emitted for nil referral") }}).emit(StageNew, nil, "")
}
func TestHooksEventSequenceSimpleAnswer(t *testing.T) {
m := newMockExchange()
m.on("198.41.0.4", "example.com", dns.TypeA, answerMsg(aRR("example.com", "9.9.9.9")))
var got []string
cfg := testConfig(false)
cfg.Hooks = &TraverserHooks{OnEvent: func(ev TraversalEvent) {
got = append(got, ev.Stage.String()+":"+ev.RefID)
}}
runTraversal(t, cfg, m, "example.com")
want := []string{"new:", "start:", "new:1", "start:1", "answer:1", "answer:"}
if len(got) != len(want) {
t.Fatalf("events = %v, want %v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("events = %v, want %v", got, want)
}
}
}
func TestHooksEventCarriesStatus(t *testing.T) {
m := newMockExchange()
m.on("198.41.0.4", "example.com", dns.TypeA, answerMsg(aRR("example.com", "9.9.9.9")))
var answerStatus Status
cfg := testConfig(false)
cfg.Hooks = &TraverserHooks{OnEvent: func(ev TraversalEvent) {
if ev.Stage == StageAnswer && ev.RefID == "1" {
answerStatus = ev.Status
}
}}
runTraversal(t, cfg, m, "example.com")
if answerStatus != StatusAnswered {
t.Errorf("answer event status = %q, want answered", answerStatus)
}
}