Skip to content

Commit 931fbb1

Browse files
TMHSDigitalclaude
andcommitted
test: shared scriptable DNS server (internal/dnstest) and new scenarios
Replace the two hand-encoded test responders (internal/dns and internal/scan) with internal/dnstest, built on golang.org/x/net/dns/dnsmessage. A Handler answers each query per name and type with A/AAAA/CNAME records, any RCODE, NODATA with an SOA, a delay, a drop or a truncated UDP answer; the server counts queries in total and per name and type, runs serially on request (a throttled resolver), and can listen on IPv6. The existing table and action helpers are thin adapters over it. New tests: NODATA is a definitive negative and is not retried, a resolver at an IPv6 address, exact wire queries per record type (pinning queriesPerType), and TCP fallback after truncation. The other scenarios #67 listed were covered by earlier fixes (#47-#50, #57, #49). Fixes #67 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c39dc0e commit 931fbb1

10 files changed

Lines changed: 521 additions & 411 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Run-quality report: `-stats <file>` writes a schema-versioned JSON summary of a run (outcomes per target, DNS queries actually sent, achieved query rate, wildcard fingerprint, candidates skipped by `-max-queries`) with a `complete` / `degraded` / `unreliable` verdict and its reason. `-format jsonl` ends with the same object as its last line. The README documents the schema and how to gate CI on the verdict (#70).
1717

1818
### Changed
19+
- Tests run against a shared, scriptable DNS server, `internal/dnstest`, built on `golang.org/x/net/dns/dnsmessage`. It replaces the two hand-encoded responders, answers per name and type with records, any RCODE, NODATA with SOA, delays, drops or truncation, and counts queries per name and type. New tests cover NODATA, a resolver at an IPv6 address, exact wire queries per record type, and TCP fallback after truncation (#67).
1920
- The minimum Go version is now 1.26 (`go 1.26.0` in go.mod), required by current `golang.org/x/sys` and `golang.org/x/text`; both were bumped and `govulncheck -show verbose` now reports no vulnerable modules. Dependabot also updates indirect modules, and CI lints with errorlint, nilerr, contextcheck, noctx, copyloopvar, usestdlibvars and revive, with errcheck exemptions narrowed to stdout prints (#83).
2021

2122
### Fixed

‎docs/CONTRIBUTING.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ go test -v -race ./...
151151
```
152152

153153
New features should include tests. New flags must be covered by at least one test case.
154-
Do not hit public resolvers. Use `-simulate` or the in-process responder in
154+
Do not hit public resolvers. Use `-simulate` or the in-process DNS server in
155+
`internal/dnstest`, which answers from a per-query handler (records, RCODE,
156+
NODATA, delay, drop, truncation) and counts every query per name and type.
157+
`internal/dns` tests can also use the table-driven `startTestDNS` wrapper in
155158
`internal/dns/testdns_test.go`. The optional live smoke test is gated on
156159
`SUBENUM_NETWORK_TESTS=1`.

‎docs/DEVELOPER_GUIDE.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ subenum/
9696
│ │ ├── resolver_test.go # DNS resolution, classification and wildcard detection tests
9797
│ │ ├── ratelimit.go # RateLimiter, WithLimiter (per-wire-query pacing)
9898
│ │ ├── ratelimit_test.go # Rate limiter tests
99-
│ │ ├── testdns_test.go # In-process UDP/TCP DNS responder for hermetic tests
99+
│ │ ├── testdns_test.go # Table-driven wrapper over internal/dnstest
100100
│ │ ├── simulate.go # SimulateResolve (seeded synthetic DNS)
101101
│ │ └── simulate_test.go # Simulation logic tests
102102
│ ├── output/
@@ -165,7 +165,7 @@ go test ./internal/dns -run TestLiveResolverSmoke
165165

166166
### Writing Tests
167167

168-
When adding new features or modifying existing ones, please ensure you add appropriate tests. Tests must not depend on the network: DNS tests in `internal/dns` use `startTestDNS` (in `testdns_test.go`), an in-process UDP/TCP server that answers from a table of `testReply` entries; names missing from the table get NXDOMAIN. Its `Resolver` method returns a `*net.Resolver` pointed at it. Here's a basic structure (save it as a `_test.go` file in `internal/dns`):
168+
When adding new features or modifying existing ones, please ensure you add appropriate tests. Tests must not depend on the network: DNS tests in `internal/dns` use `startTestDNS` (in `testdns_test.go`), a table-driven wrapper over `internal/dnstest`, the shared in-process UDP/TCP DNS server; names missing from the table get NXDOMAIN and missing record types get NODATA. Use `dnstest.Start` directly when a test needs per-query scripting or exact query counts (`QueriesFor`). Its `Resolver` method returns a `*net.Resolver` pointed at it. Here's a basic structure (save it as a `_test.go` file in `internal/dns`):
169169

170170
```go
171171
package dns

‎docs/ROADMAP.md‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ Work in progress or next up.
1616

1717
- **Patch release for static Linux binaries** ([#46](https://github.com/TMHSDigital/subenum/issues/46)).
1818
The build fix is on `main`; a v0.8.1 release replaces the dynamically linked asset.
19-
- **Test infrastructure** ([#67](https://github.com/TMHSDigital/subenum/issues/67)):
20-
replace the hand-rolled test DNS server and close coverage gaps.
2119
- **Repository hardening** ([#60](https://github.com/TMHSDigital/subenum/issues/60)):
2220
protected `main`, Dependabot alerts, Actions pinned by SHA.
2321

@@ -66,9 +64,10 @@ surprises:
6664

6765
Constraints to keep in mind when changing the code.
6866

69-
- **The test DNS server is hand-rolled.** It serves A, AAAA, one-hop CNAME
70-
chains, SERVFAIL, REFUSED, dropped queries and delayed A answers. Other qtypes
71-
and EDNS are not modelled; extend it (or finish #67) before testing them.
67+
- **The test DNS server (`internal/dnstest`) models A, AAAA and CNAME
68+
answers,** any RCODE, NODATA with SOA, delays, drops and truncation. Other
69+
record types and EDNS are not modelled; extend its `Reply` before testing
70+
them.
7271
- **DNS goes through Go's stdlib resolver.** Its internal retries depend on the
7372
host's resolver configuration. `-rate` charges every query it dials, but the
7473
number of queries per lookup is not fully under subenum's control.

‎go.mod‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/charmbracelet/bubbles v1.0.0
77
github.com/charmbracelet/bubbletea v1.3.10
88
github.com/charmbracelet/lipgloss v1.1.0
9+
golang.org/x/net v0.59.0
910
)
1011

1112
require (

‎go.sum‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
4242
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
4343
github.com/xo/terminfo v1.2.0 h1:d0ZTOCpuGE0lwSAOs0zcJjwz3jWQyqcRt9XbGJpCOl4=
4444
github.com/xo/terminfo v1.2.0/go.mod h1:lGzkSo8Fe7IRh/w+Gqz7n5mDog4FVXCj3gy/DYTBqio=
45+
golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues=
46+
golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
4547
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4648
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
4749
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=

‎internal/dns/scenarios_test.go‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package dns
2+
3+
import (
4+
"context"
5+
"sync/atomic"
6+
"testing"
7+
"time"
8+
9+
"golang.org/x/net/dns/dnsmessage"
10+
11+
"github.com/TMHSDigital/subenum/internal/dnstest"
12+
)
13+
14+
// TestNoDataIsDefinitiveNegative (#67): NOERROR with no records and an SOA
15+
// (NODATA) is a definitive negative, never retried, and not a failure.
16+
func TestNoDataIsDefinitiveNegative(t *testing.T) {
17+
srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
18+
if q.Name == "v4only.example.com" && q.Type == dnsmessage.TypeA {
19+
return dnstest.Reply{A: []string{"192.0.2.1"}}
20+
}
21+
if q.Name == "v4only.example.com" {
22+
return dnstest.Reply{} // NODATA
23+
}
24+
return dnstest.NXDomain
25+
})
26+
r := NewResolver(time.Second, srv.Addr)
27+
recs, outcome := ResolveDomainWithRetry(context.Background(), r, "v4only.example.com", time.Second, nil, 3, []string{"AAAA"})
28+
if len(recs) != 0 || outcome != OutcomeNXDomain {
29+
t.Fatalf("AAAA on an A-only name: records %v, outcome %v; want none, NXDOMAIN", recs, outcome)
30+
}
31+
if n := srv.QueriesFor("v4only.example.com", dnsmessage.TypeAAAA); n != 1 {
32+
t.Fatalf("AAAA queries = %d, want 1 (NODATA must not be retried)", n)
33+
}
34+
// With A requested too, the name resolves despite the AAAA NODATA.
35+
recs, outcome = ResolveDomainWithRetry(context.Background(), r, "v4only.example.com", time.Second, nil, 1, DefaultTypes)
36+
if outcome != OutcomeFound || len(recs) != 1 || recs[0].Value != "192.0.2.1" {
37+
t.Fatalf("A+AAAA: records %v, outcome %v", recs, outcome)
38+
}
39+
}
40+
41+
// TestResolverAtIPv6Address (#67): -dns-server can be an IPv6 address.
42+
func TestResolverAtIPv6Address(t *testing.T) {
43+
srv := dnstest.StartWith(t, dnstest.Options{IPv6: true}, func(q dnstest.Query) dnstest.Reply {
44+
if q.Name == "www.example.com" && q.Type == dnsmessage.TypeA {
45+
return dnstest.Reply{A: []string{"192.0.2.7"}}
46+
}
47+
return dnstest.Reply{}
48+
})
49+
if srv.Addr[0] != '[' {
50+
t.Fatalf("server address %q is not an IPv6 host:port", srv.Addr)
51+
}
52+
recs, outcome := ResolveDomainWithRetry(context.Background(), NewResolver(time.Second, srv.Addr), "www.example.com", time.Second, nil, 1, []string{"A"})
53+
if outcome != OutcomeFound || len(recs) != 1 {
54+
t.Fatalf("lookup via %s: records %v, outcome %v", srv.Addr, recs, outcome)
55+
}
56+
}
57+
58+
// TestWireQueriesPerType (#67) pins the exact queries each record type puts on
59+
// the wire, which -rate's up-front reservation (queriesPerType) relies on.
60+
func TestWireQueriesPerType(t *testing.T) {
61+
srv := dnstest.Start(t, func(dnstest.Query) dnstest.Reply {
62+
return dnstest.Reply{A: []string{"192.0.2.1"}, AAAA: []string{"2001:db8::1"}}
63+
})
64+
r := NewResolver(time.Second, srv.Addr)
65+
for _, typ := range []string{"A", "AAAA", "CNAME"} {
66+
name := "q-" + typ + ".example.com"
67+
if _, _, err := ResolveTypes(context.Background(), r, name, time.Second, []string{typ}); err != nil {
68+
t.Fatalf("%s: %v", typ, err)
69+
}
70+
got := map[string]int64{
71+
"A": srv.QueriesFor(name, dnsmessage.TypeA),
72+
"AAAA": srv.QueriesFor(name, dnsmessage.TypeAAAA),
73+
"CNAME": srv.QueriesFor(name, dnsmessage.TypeCNAME),
74+
}
75+
total := got["A"] + got["AAAA"] + got["CNAME"]
76+
if int32(total) != queriesPerType[typ] {
77+
t.Errorf("-type %s sent %v (%d queries); queriesPerType reserves %d", typ, got, total, queriesPerType[typ])
78+
}
79+
}
80+
}
81+
82+
// TestTruncationFallsBackToTCP: a truncated UDP answer is retried over TCP,
83+
// and each transport's query is counted.
84+
func TestTruncationFallsBackToTCP(t *testing.T) {
85+
var sawTCP atomic.Bool
86+
srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
87+
if q.TCP {
88+
sawTCP.Store(true)
89+
}
90+
return dnstest.Reply{A: []string{"192.0.2.9"}, Truncate: true}
91+
})
92+
recs, _, err := ResolveTypes(context.Background(), NewResolver(time.Second, srv.Addr), "big.example.com", time.Second, []string{"A"})
93+
if err != nil || len(recs) != 1 || !sawTCP.Load() {
94+
t.Fatalf("records %v, err %v, TCP retry %v", recs, err, sawTCP.Load())
95+
}
96+
if n := srv.QueriesFor("big.example.com", dnsmessage.TypeA); n != 2 {
97+
t.Fatalf("A queries = %d, want 2 (UDP, then TCP)", n)
98+
}
99+
}

0 commit comments

Comments
 (0)