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>
This commit is contained in:
co-authored by
Claude Fable 5
parent
af15c9c2d4
commit
d71c7fbef2
+188
-131
@@ -3,201 +3,258 @@ package traverse
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func TestNewInfoCache(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
if c.parent != nil {
|
||||
t.Error("root cache should have nil parent")
|
||||
}
|
||||
if c.NSCount() != 0 {
|
||||
t.Errorf("NSCount = %d, want 0", c.NSCount())
|
||||
}
|
||||
if c.GlueCount() != 0 {
|
||||
t.Errorf("GlueCount = %d, want 0", c.GlueCount())
|
||||
func nsRR(zone, target string) dns.RR {
|
||||
return &dns.NS{
|
||||
Hdr: dns.RR_Header{Name: dns.Fqdn(zone), Rrtype: dns.TypeNS, Class: dns.ClassINET},
|
||||
Ns: dns.Fqdn(target),
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheStoreAndLookupNS(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
|
||||
c.StoreNS("com.", []string{"a.gtld-servers.net.", "b.gtld-servers.net."})
|
||||
if c.NSCount() != 1 {
|
||||
t.Errorf("NSCount = %d, want 1", c.NSCount())
|
||||
}
|
||||
|
||||
names := c.LookupNS("com.")
|
||||
if len(names) != 2 {
|
||||
t.Fatalf("expected 2 nameservers, got %d", len(names))
|
||||
}
|
||||
if names[0] != "a.gtld-servers.net." {
|
||||
t.Errorf("nameserver[0] = %q, want %q", names[0], "a.gtld-servers.net.")
|
||||
func aRR(name, ip string) dns.RR {
|
||||
return &dns.A{
|
||||
Hdr: dns.RR_Header{Name: dns.Fqdn(name), Rrtype: dns.TypeA, Class: dns.ClassINET},
|
||||
A: net.ParseIP(ip).To4(),
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheNSDedup(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
c.StoreNS("com.", []string{"a.gtld-servers.net.", "a.gtld-servers.net."})
|
||||
names := c.LookupNS("com.")
|
||||
if len(names) != 1 {
|
||||
t.Errorf("expected 1 deduped NS, got %d", len(names))
|
||||
func aaaaRR(name, ip string) dns.RR {
|
||||
return &dns.AAAA{
|
||||
Hdr: dns.RR_Header{Name: dns.Fqdn(name), Rrtype: dns.TypeAAAA, Class: dns.ClassINET},
|
||||
AAAA: net.ParseIP(ip),
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheNSCaseInsensitive(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
c.StoreNS("COM.", []string{"A.GTLD-SERVERS.NET."})
|
||||
names := c.LookupNS("com.")
|
||||
if len(names) != 1 {
|
||||
t.Fatalf("expected 1 NS, got %d", len(names))
|
||||
func TestCanonicalName(t *testing.T) {
|
||||
tests := []struct{ in, want string }{
|
||||
{"example.com", "example.com"},
|
||||
{"Example.COM.", "example.com"},
|
||||
{".", ""},
|
||||
{"", ""},
|
||||
{"WWW.Example.Com", "www.example.com"},
|
||||
}
|
||||
if names[0] != "a.gtld-servers.net." {
|
||||
t.Errorf("NS = %q, want %q", names[0], "a.gtld-servers.net.")
|
||||
for _, tt := range tests {
|
||||
if got := canonicalName(tt.in); got != tt.want {
|
||||
t.Errorf("canonicalName(%q) = %q, want %q", tt.in, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheNSLookupMiss(t *testing.T) {
|
||||
func TestInfoCacheAddAndGet(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
names := c.LookupNS("org.")
|
||||
if names != nil {
|
||||
t.Errorf("expected nil for miss, got %v", names)
|
||||
c.Add([]dns.RR{nsRR("com", "a.gtld-servers.net"), nsRR("com", "b.gtld-servers.net")})
|
||||
|
||||
rrs := c.Get("com", dns.ClassINET, dns.TypeNS)
|
||||
if len(rrs) != 2 {
|
||||
t.Fatalf("expected 2 NS records, got %d", len(rrs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheNSStoreEmpty(t *testing.T) {
|
||||
func TestInfoCacheAddReplacesSameKey(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
c.StoreNS("com.", nil)
|
||||
if c.NSCount() != 0 {
|
||||
t.Errorf("expected 0 after empty store, got %d", c.NSCount())
|
||||
c.Add([]dns.RR{nsRR("com", "old1.example.net"), nsRR("com", "old2.example.net")})
|
||||
c.Add([]dns.RR{nsRR("com", "new.example.net")})
|
||||
|
||||
rrs := c.Get("com", dns.ClassINET, dns.TypeNS)
|
||||
if len(rrs) != 1 {
|
||||
t.Fatalf("add should replace same name:class:type entry, got %d records", len(rrs))
|
||||
}
|
||||
if rrs[0].(*dns.NS).Ns != "new.example.net." {
|
||||
t.Errorf("NS = %q, want new.example.net.", rrs[0].(*dns.NS).Ns)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheChainedNS(t *testing.T) {
|
||||
func TestInfoCacheAddKeepsDistinctKeys(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
c.Add([]dns.RR{nsRR("com", "a.gtld-servers.net"), aRR("a.gtld-servers.net", "192.5.6.30")})
|
||||
c.Add([]dns.RR{nsRR("org", "a0.org-servers.net")})
|
||||
|
||||
if got := c.Get("com", dns.ClassINET, dns.TypeNS); len(got) != 1 {
|
||||
t.Errorf("com NS lost after unrelated add: %v", got)
|
||||
}
|
||||
if got := c.Get("a.gtld-servers.net", dns.ClassINET, dns.TypeA); len(got) != 1 {
|
||||
t.Errorf("glue lost after unrelated add: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheGetCaseInsensitive(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
c.Add([]dns.RR{nsRR("COM.", "A.GTLD-SERVERS.NET.")})
|
||||
if got := c.Get("com", dns.ClassINET, dns.TypeNS); len(got) != 1 {
|
||||
t.Fatalf("expected case-insensitive hit, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheGetMiss(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
if got := c.Get("org", dns.ClassINET, dns.TypeNS); got != nil {
|
||||
t.Errorf("expected nil for miss, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheGetRecursesToParent(t *testing.T) {
|
||||
parent := NewInfoCache(nil)
|
||||
parent.StoreNS("com.", []string{"a.gtld-servers.net."})
|
||||
|
||||
parent.Add([]dns.RR{nsRR("com", "a.gtld-servers.net")})
|
||||
child := parent.Child()
|
||||
if child.parent != parent {
|
||||
t.Error("child parent should be the parent cache")
|
||||
t.Fatal("Child() should link to parent")
|
||||
}
|
||||
|
||||
names := child.LookupNS("com.")
|
||||
if len(names) != 1 {
|
||||
t.Fatalf("expected 1 NS from parent, got %d", len(names))
|
||||
}
|
||||
if child.NSCount() != 0 {
|
||||
t.Errorf("child NSCount = %d, want 0", child.NSCount())
|
||||
if got := child.Get("com", dns.ClassINET, dns.TypeNS); len(got) != 1 {
|
||||
t.Fatalf("expected parent hit through child, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheChildOverridesParent(t *testing.T) {
|
||||
func TestInfoCacheChildShadowsParent(t *testing.T) {
|
||||
parent := NewInfoCache(nil)
|
||||
parent.StoreNS("com.", []string{"a.gtld-servers.net."})
|
||||
|
||||
parent.Add([]dns.RR{nsRR("com", "parent.example.net")})
|
||||
child := parent.Child()
|
||||
child.StoreNS("com.", []string{"b.gtld-servers.net."})
|
||||
child.Add([]dns.RR{nsRR("com", "child.example.net")})
|
||||
|
||||
names := child.LookupNS("com.")
|
||||
if len(names) != 1 {
|
||||
t.Fatalf("expected 1 NS, got %d", len(names))
|
||||
got := child.Get("com", dns.ClassINET, dns.TypeNS)
|
||||
if len(got) != 1 || got[0].(*dns.NS).Ns != "child.example.net." {
|
||||
t.Errorf("child entry should shadow parent, got %v", got)
|
||||
}
|
||||
if names[0] != "b.gtld-servers.net." {
|
||||
t.Errorf("expected child's NS to override, got %q", names[0])
|
||||
// The parent must be untouched.
|
||||
got = parent.Get("com", dns.ClassINET, dns.TypeNS)
|
||||
if len(got) != 1 || got[0].(*dns.NS).Ns != "parent.example.net." {
|
||||
t.Errorf("parent entry modified, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheStoreAndLookupGlue(t *testing.T) {
|
||||
func TestGetStartServersWalksLabels(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
addrs := []net.IP{net.ParseIP("1.2.3.4"), net.ParseIP("5.6.7.8")}
|
||||
c.StoreGlue("ns1.example.com.", addrs)
|
||||
c.Add([]dns.RR{
|
||||
nsRR("com", "a.gtld-servers.net"),
|
||||
nsRR("com", "b.gtld-servers.net"),
|
||||
aRR("a.gtld-servers.net", "192.5.6.30"),
|
||||
})
|
||||
|
||||
result := c.LookupGlue("ns1.example.com.")
|
||||
if len(result) != 2 {
|
||||
t.Fatalf("expected 2 glue addresses, got %d", len(result))
|
||||
starters, bw, err := c.GetStartServers("www.deep.example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("GetStartServers: %v", err)
|
||||
}
|
||||
if bw != "com" {
|
||||
t.Errorf("newbailiwick = %q, want com", bw)
|
||||
}
|
||||
if len(starters) != 2 {
|
||||
t.Fatalf("expected 2 starters, got %d", len(starters))
|
||||
}
|
||||
if starters[0].Name != "a.gtld-servers.net" {
|
||||
t.Errorf("starter[0] = %q", starters[0].Name)
|
||||
}
|
||||
if len(starters[0].IPs) != 1 || starters[0].IPs[0] != "192.5.6.30" {
|
||||
t.Errorf("starter[0] IPs = %v, want [192.5.6.30]", starters[0].IPs)
|
||||
}
|
||||
if starters[1].IPs != nil {
|
||||
t.Errorf("glueless starter should have nil IPs, got %v", starters[1].IPs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheGlueDedup(t *testing.T) {
|
||||
func TestGetStartServersPrefersDeepestZone(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
ip := net.ParseIP("1.2.3.4")
|
||||
c.StoreGlue("ns1.example.com.", []net.IP{ip, ip})
|
||||
result := c.LookupGlue("ns1.example.com.")
|
||||
if len(result) != 1 {
|
||||
t.Errorf("expected 1 deduped glue, got %d", len(result))
|
||||
c.AddHints("", []StartServer{{Name: "a.root-servers.net", IPs: []string{"198.41.0.4"}}})
|
||||
c.Add([]dns.RR{nsRR("example.com", "ns1.example.com"), aRR("ns1.example.com", "1.2.3.4")})
|
||||
|
||||
starters, bw, err := c.GetStartServers("www.example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("GetStartServers: %v", err)
|
||||
}
|
||||
if bw != "example.com" {
|
||||
t.Errorf("newbailiwick = %q, want example.com", bw)
|
||||
}
|
||||
if len(starters) != 1 || starters[0].Name != "ns1.example.com" {
|
||||
t.Errorf("starters = %v", starters)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheChainedGlue(t *testing.T) {
|
||||
func TestGetStartServersRootHints(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
c.AddHints("", []StartServer{
|
||||
{Name: "a.root-servers.net", IPs: []string{"198.41.0.4", "2001:503:ba3e::2:30"}},
|
||||
{Name: "b.root-servers.net", IPs: []string{"170.247.170.2"}},
|
||||
})
|
||||
|
||||
starters, bw, err := c.GetStartServers("anything.example.org")
|
||||
if err != nil {
|
||||
t.Fatalf("GetStartServers: %v", err)
|
||||
}
|
||||
if bw != "" {
|
||||
t.Errorf("root bailiwick should be \"\", got %q", bw)
|
||||
}
|
||||
if len(starters) != 2 {
|
||||
t.Fatalf("expected 2 root starters, got %d", len(starters))
|
||||
}
|
||||
// Only the IPv4 address surfaces (IPv4-only transport); the AAAA is
|
||||
// cached but not returned as a start address.
|
||||
if len(starters[0].IPs) != 1 || starters[0].IPs[0] != "198.41.0.4" {
|
||||
t.Errorf("starter[0].IPs = %v, want [198.41.0.4]", starters[0].IPs)
|
||||
}
|
||||
if got := c.Get("a.root-servers.net", dns.ClassINET, dns.TypeAAAA); len(got) != 1 {
|
||||
t.Errorf("AAAA hint should be cached, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetStartServersNoRootHints(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
if _, _, err := c.GetStartServers("example.com"); err == nil {
|
||||
t.Fatal("expected error with no NS cached anywhere")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetStartServersExactDomainMatch(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
c.Add([]dns.RR{nsRR("example.com", "ns1.example.net")})
|
||||
_, bw, err := c.GetStartServers("example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("GetStartServers: %v", err)
|
||||
}
|
||||
if bw != "example.com" {
|
||||
t.Errorf("newbailiwick = %q, want example.com", bw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetStartServersUsesBranchCache(t *testing.T) {
|
||||
parent := NewInfoCache(nil)
|
||||
parent.StoreGlue("ns1.example.com.", []net.IP{net.ParseIP("1.2.3.4")})
|
||||
|
||||
parent.AddHints("", []StartServer{{Name: "a.root-servers.net", IPs: []string{"198.41.0.4"}}})
|
||||
child := parent.Child()
|
||||
result := child.LookupGlue("ns1.example.com.")
|
||||
if len(result) != 1 {
|
||||
t.Fatalf("expected 1 glue from parent, got %d", len(result))
|
||||
}
|
||||
if child.GlueCount() != 0 {
|
||||
t.Errorf("child GlueCount = %d, want 0", child.GlueCount())
|
||||
}
|
||||
}
|
||||
child.Add([]dns.RR{nsRR("com", "a.gtld-servers.net")})
|
||||
|
||||
func TestInfoCacheGlueLookupMiss(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
result := c.LookupGlue("nonexistent.example.com.")
|
||||
if result != nil {
|
||||
t.Errorf("expected nil for miss, got %v", result)
|
||||
_, bw, err := child.GetStartServers("www.example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("GetStartServers: %v", err)
|
||||
}
|
||||
if bw != "com" {
|
||||
t.Errorf("newbailiwick = %q, want com (child cache hit)", bw)
|
||||
}
|
||||
|
||||
// A sibling branch must not see the child's records.
|
||||
sibling := parent.Child()
|
||||
_, bw, err = sibling.GetStartServers("www.example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("GetStartServers: %v", err)
|
||||
}
|
||||
if bw != "" {
|
||||
t.Errorf("sibling newbailiwick = %q, want \"\" (root only)", bw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfoCacheConcurrentAccess(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
name := strings.ToLower(dns.Fqdn(fmt.Sprintf("ns%d.example.com.", i)))
|
||||
c.StoreNS("example.com.", []string{name})
|
||||
c.StoreGlue(name, []net.IP{net.ParseIP(fmt.Sprintf("1.2.3.%d", i%256))})
|
||||
_ = c.LookupNS("example.com.")
|
||||
_ = c.LookupGlue(name)
|
||||
name := fmt.Sprintf("ns%d.example.com", i)
|
||||
c.Add([]dns.RR{nsRR("example.com", name), aRR(name, fmt.Sprintf("1.2.3.%d", i%256))})
|
||||
_, _, _ = c.GetStartServers("www.example.com")
|
||||
_ = c.Get(name, dns.ClassINET, dns.TypeA)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestInfoCacheNilParent(t *testing.T) {
|
||||
c := NewInfoCache(nil)
|
||||
if c.LookupNS("com.") != nil {
|
||||
t.Error("root cache should return nil for miss")
|
||||
}
|
||||
if c.LookupGlue("ns.example.com.") != nil {
|
||||
t.Error("root cache should return nil for glue miss")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalize(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
want string
|
||||
}{
|
||||
{"example.com", "example.com."},
|
||||
{"Example.COM.", "example.com."},
|
||||
{"EXAMPLE.COM", "example.com."},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
got := normalize(tt.input)
|
||||
if got != tt.want {
|
||||
t.Errorf("normalize(%q) = %q, want %q", tt.input, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user