Skip to content

Commit 529ca52

Browse files
TMHSDigitalclaude
andcommitted
Test the delegation check's name server discovery (#468)
Every delegation test used the lab's ZoneServer shortcut, so the path real scans take (look up the zone's name servers, resolve them to endpoints, ask each in turn) never ran; dropping the endpoint append left the package green. Add an unexported zonePort test seam to Config (port 53 when unset) and tests that find two name servers through a local resolver, count the lookups, move past an endpoint that fails to one that answers, and notice once when no name server resolves. readDelegation's wrong-ID and QR=0 rejections get a test too. Fixes #468 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7d44bd3 commit 529ca52

4 files changed

Lines changed: 115 additions & 1 deletion

File tree

‎internal/dns/referral_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,18 @@ func TestAskDelegationPacedAndCounted(t *testing.T) {
9191
t.Errorf("sent %d, counted %d, rate slots %d; want 2 each (UDP, then TCP)", sent, counted.Load(), reserved)
9292
}
9393
}
94+
95+
// TestReadDelegationRejectsForeignReply covers #468: a reply with another
96+
// query's ID, or one that is not a response, gives no hosts.
97+
func TestReadDelegationRejectsForeignReply(t *testing.T) {
98+
const id, name = 0x4242, "old.example.com"
99+
for desc, msg := range map[string][]byte{
100+
"wrong ID": coveringReply(t, id+1, true, dnsmessage.RCodeSuccess, name, "", ""),
101+
"not a response": coveringReply(t, id, false, dnsmessage.RCodeSuccess, name, "", ""),
102+
} {
103+
hosts, err := readDelegation(msg, id, name)
104+
if err == nil || !strings.Contains(err.Error(), "unexpected message") || hosts != nil {
105+
t.Errorf("%s: hosts %v, err %v; want an unexpected-message error and no hosts", desc, hosts, err)
106+
}
107+
}
108+
}

‎internal/scan/delegation.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scan
22

33
import (
4+
"cmp"
45
"context"
56
"fmt"
67
"net"
@@ -63,9 +64,10 @@ func (p *parentZone) servers(ctx context.Context) ([]string, string) {
6364
p.err = errMsg
6465
return
6566
}
67+
port := cmp.Or(cfg.zonePort, "53")
6668
for _, ns := range servers {
6769
for _, addr := range ns.Addrs {
68-
if ep := net.JoinHostPort(addr, "53"); !slices.Contains(p.endpoints, ep) {
70+
if ep := net.JoinHostPort(addr, port); !slices.Contains(p.endpoints, ep) {
6971
p.endpoints = append(p.endpoints, ep)
7072
}
7173
}

‎internal/scan/delegation_test.go‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ package scan
22

33
import (
44
"context"
5+
"net"
6+
"slices"
57
"strings"
68
"testing"
79
"time"
810

11+
"golang.org/x/net/dns/dnsmessage"
12+
913
"github.com/TMHSDigital/subenum/internal/dns"
1014
"github.com/TMHSDigital/subenum/internal/dnstest"
1115
)
@@ -149,3 +153,92 @@ func TestDelegationCheckWithoutNameServers(t *testing.T) {
149153
t.Errorf("parent checks %d, queries %d; want 0 and 1 (the NS lookup)", st.ParentChecks, st.ParentQueries)
150154
}
151155
}
156+
157+
// zoneLookupConfig is a delegation-check config that finds the zone's name
158+
// servers the way a real scan does: through resolver, with no ZoneServer,
159+
// and with their addresses reached on the zone server's port (#468).
160+
func zoneLookupConfig(resolver, zone *dnstest.Server) Config {
161+
_, port, _ := net.SplitHostPort(zone.Addr)
162+
return Config{
163+
Domain: "example.com", Types: []string{"A", "NS"}, Timeout: time.Second,
164+
DNSServer: resolver.Addr, Resolver: dns.NewResolver(time.Second, resolver.Addr),
165+
zonePort: port, scope: newScope(nil),
166+
}
167+
}
168+
169+
// TestDelegationFindsZoneNameServers covers #468: without the lab shortcut,
170+
// the check looks up the zone's name servers through the resolver, keeps
171+
// every address of each, asks them in turn, moves past one that fails, and
172+
// counts the lookups and queries it sent.
173+
func TestDelegationFindsZoneNameServers(t *testing.T) {
174+
zone := delegationZone(t, false)
175+
resolver := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
176+
switch {
177+
case q.Name == "example.com" && q.Type == dnsmessage.TypeNS:
178+
return dnstest.Reply{NS: []string{"ns1.example.com", "ns2.example.com"}}
179+
case q.Name == "ns1.example.com" && q.Type == dnsmessage.TypeA:
180+
return dnstest.Reply{A: []string{"127.0.0.1"}}
181+
case q.Name == "ns2.example.com" && q.Type == dnsmessage.TypeA:
182+
return dnstest.Reply{A: []string{"127.0.0.2"}} // nothing answers there
183+
case q.Name == "example.com" || strings.HasPrefix(q.Name, "ns"):
184+
return dnstest.Reply{}
185+
}
186+
return dnstest.NXDomain
187+
})
188+
p := &parentZone{cfg: zoneLookupConfig(resolver, zone)}
189+
_, port, _ := net.SplitHostPort(zone.Addr)
190+
endpoints, errMsg := p.servers(context.Background())
191+
good, bad := net.JoinHostPort("127.0.0.1", port), net.JoinHostPort("127.0.0.2", port)
192+
if errMsg != "" || len(endpoints) != 2 || !slices.Contains(endpoints, good) || !slices.Contains(endpoints, bad) {
193+
t.Fatalf("endpoints %v (err %q), want %s and %s", endpoints, errMsg, good, bad)
194+
}
195+
lookups := p.queries.Load()
196+
if lookups != 5 { // NS, then A and AAAA for each host
197+
t.Errorf("name server lookups counted %d queries, want 5", lookups)
198+
}
199+
200+
// The one that fails is asked first, then the next one answers.
201+
p.endpoints = []string{bad, good}
202+
var notices []string
203+
records := p.delegation(context.Background(), "old.example.com", func(m string) { notices = append(notices, m) })
204+
if want := (dns.Record{Type: "NS", Value: "ns1.gone.example.net"}); len(records) != 1 || records[0] != want {
205+
t.Errorf("records %v, want %v from the second name server", records, want)
206+
}
207+
if len(notices) != 0 || p.checks.Load() != 1 {
208+
t.Errorf("notices %q, checks %d; want none and 1", notices, p.checks.Load())
209+
}
210+
if n := zone.QueriesFor("old.example.com", dnsmessage.TypeNS); n != 1 {
211+
t.Errorf("the answering name server saw %d delegation queries, want 1", n)
212+
}
213+
if p.queries.Load() <= lookups {
214+
t.Errorf("queries %d after the check, want the delegation queries counted on top of %d", p.queries.Load(), lookups)
215+
}
216+
}
217+
218+
// TestDelegationUnresolvedNameServers covers #468: when none of the zone's
219+
// name servers resolves, no check is made and the reason is noticed once.
220+
func TestDelegationUnresolvedNameServers(t *testing.T) {
221+
zone := delegationZone(t, false)
222+
resolver := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
223+
if q.Name == "example.com" && q.Type == dnsmessage.TypeNS {
224+
return dnstest.Reply{NS: []string{"ns1.gone.example.net"}}
225+
}
226+
if q.Name == "example.com" {
227+
return dnstest.Reply{}
228+
}
229+
return dnstest.NXDomain
230+
})
231+
p := &parentZone{cfg: zoneLookupConfig(resolver, zone)}
232+
var notices []string
233+
for _, name := range []string{"old.example.com", "old2.example.com"} {
234+
if r := p.delegation(context.Background(), name, func(m string) { notices = append(notices, m) }); r != nil {
235+
t.Errorf("%s: records %v, want none", name, r)
236+
}
237+
}
238+
if len(notices) != 1 || !strings.Contains(notices[0], "none of the name servers of example.com could be resolved") {
239+
t.Errorf("notices %q, want one naming the unresolved name servers", notices)
240+
}
241+
if p.checks.Load() != 0 || zone.Queries() != 0 {
242+
t.Errorf("checks %d, zone queries %d; want 0 and 0", p.checks.Load(), zone.Queries())
243+
}
244+
}

‎internal/scan/runner.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ type Config struct {
115115
// resolveHook, if set, replaces the simulation and live lookups (see resolve).
116116
// Tests inject classified outcomes through this field without network I/O.
117117
resolveHook func(ctx context.Context, domain string) ([]dns.Record, dns.Outcome)
118+
// zonePort, if set, replaces port 53 on the zone's name server addresses
119+
// the delegation check finds, so tests can run that path against local
120+
// servers (#468).
121+
zonePort string
118122

119123
scope *scope // built from Exclude by Run
120124
takeover *dns.TakeoverCache // one CNAME-target lookup per target per scan (#121)

0 commit comments

Comments
 (0)