Skip to content

Commit 496df99

Browse files
TMHSDigitalclaude
andcommitted
Move package main's test seams onto runArgs (#266)
signalReady, testSignals, ctBaseURL, waybackBaseURL and axfrPort were package variables that tests overwrote, hidden global state that also kept those tests from running in parallel. They are now per-run fields on runArgs (signals, signalReady, sourceURLs, axfrPort), zero in production; passiveNames and zoneTransfer take the URLs and port as parameters. Tests pass them through new runCLIWith/runCLIMergedWith helpers, and the globals are gone. Not done here: adding t.Parallel to the root tests. Runs still share process-wide state (signal.Notify, and the SIGTERM test signals the whole process), which needs its own change. Refs #266 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d1da60f commit 496df99

9 files changed

Lines changed: 82 additions & 62 deletions

‎main.go‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,6 @@ const exitCodesHelp = `Exit codes:
7070
143 terminated (SIGTERM); partial results are kept
7171
`
7272

73-
// signalReady, when set by a test, is closed once run's signal handler is
74-
// installed.
75-
var signalReady chan struct{}
76-
77-
// testSignals, when set by a test, receives signals in place of a fresh
78-
// channel, so a test can interrupt a run in-process on Windows too.
79-
var testSignals chan os.Signal
80-
8173
// Version is the release identifier. Makefile, CI and the Dockerfile set it
8274
// with -ldflags "-X main.Version=$(git describe --tags --dirty)". When it is
8375
// empty (go install, plain go build), the module version from the build info
@@ -607,6 +599,27 @@ type runArgs struct {
607599
stdin io.Reader
608600
stdout io.Writer
609601
stderr io.Writer
602+
603+
// Test seams, zero in production (#266). Being per run rather than
604+
// package variables, they let tests that set them run in parallel.
605+
606+
// signals, when set, receives signals in place of a fresh channel, so a
607+
// test can interrupt a run in-process on Windows too.
608+
signals chan os.Signal
609+
// signalReady, when set, is closed once the signal handler is installed.
610+
signalReady chan struct{}
611+
// sourceURLs replace passive sources' endpoints, by source name.
612+
sourceURLs map[string]string
613+
// axfrPort is the port zone transfers are requested on; "" is 53.
614+
axfrPort string
615+
}
616+
617+
// transferPort is the port -axfr asks name servers on.
618+
func (a runArgs) transferPort() string {
619+
if a.axfrPort == "" {
620+
return "53"
621+
}
622+
return a.axfrPort
610623
}
611624

612625
func run() int {

‎main_axfr.go‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ import (
1212
"github.com/TMHSDigital/subenum/internal/output"
1313
)
1414

15-
// axfrPort is the port zone transfers are requested on; tests point it at a
16-
// local server.
17-
var axfrPort = "53"
18-
1915
// Zone transfer bounds (#85): a target with many name servers, or name
2016
// servers with many addresses, cannot turn -axfr into a flood.
2117
const (
@@ -39,8 +35,8 @@ type targetAXFR struct {
3935
// not exist (#234). Plain NSEC means anyone can list the zone by walking it,
4036
// which is reported like an allowed transfer. A failed probe is "" so the
4137
// next address is tried.
42-
func probeDenial(ctx context.Context, f cliFlags, host, addr, target string, timeout time.Duration, out *output.Writer) dns.Denial {
43-
d, err := dns.ProbeDenial(ctx, net.JoinHostPort(addr, axfrPort), target, timeout)
38+
func probeDenial(ctx context.Context, f cliFlags, host, addr, port, target string, timeout time.Duration, out *output.Writer) dns.Denial {
39+
d, err := dns.ProbeDenial(ctx, net.JoinHostPort(addr, port), target, timeout)
4440
if err != nil {
4541
if f.verbose {
4642
out.Info("-axfr: DNSSEC denial probe to %s (%s): %v", host, addr, err)
@@ -60,7 +56,7 @@ func probeDenial(ctx context.Context, f cliFlags, host, addr, target string, tim
6056
// gives every name in the zone in one query; most refuse, which is the
6157
// correct configuration and not reported as an error. A resumed run reuses
6258
// the names the interrupted run got, so its candidate list matches.
63-
func zoneTransfer(ctx context.Context, f cliFlags, target string, saved *resumeTarget, out *output.Writer) targetAXFR {
59+
func zoneTransfer(ctx context.Context, f cliFlags, port, target string, saved *resumeTarget, out *output.Writer) targetAXFR {
6460
if saved != nil && saved.AXFRDone {
6561
return targetAXFR{names: saved.AXFR, allowed: saved.AXFRServers, denial: dns.Denial(saved.Denial), done: true}
6662
}
@@ -108,11 +104,11 @@ func zoneTransfer(ctx context.Context, f cliFlags, target string, saved *resumeT
108104
}
109105
if res.denial == "" {
110106
res.queries++
111-
res.denial = probeDenial(ctx, f, host, addr, target, timeout, out)
107+
res.denial = probeDenial(ctx, f, host, addr, port, target, timeout, out)
112108
}
113109
res.tried++
114110
res.queries++
115-
got, err := dns.Transfer(ctx, net.JoinHostPort(addr, axfrPort), target, transferTimeout)
111+
got, err := dns.Transfer(ctx, net.JoinHostPort(addr, port), target, transferTimeout)
116112
if err != nil {
117113
if f.verbose || !errors.Is(err, dns.ErrTransferRefused) {
118114
out.Info("-axfr: %s (%s): %v", host, addr, err)

‎main_axfr_test.go‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,18 @@ func axfrZone(t *testing.T, allow bool, denial string) *dnstest.Server {
4343
}
4444
return dnstest.Reply{RCode: dnsmessage.RCodeNameError, Denial: denial}
4545
})
46+
return srv
47+
}
48+
49+
// runAXFR runs the CLI against srv with zone transfers asked on its port,
50+
// stdout and stderr merged.
51+
func runAXFR(t *testing.T, srv *dnstest.Server, args ...string) (int, string) {
52+
t.Helper()
4653
_, port, err := net.SplitHostPort(srv.Addr)
4754
if err != nil {
4855
t.Fatal(err)
4956
}
50-
old := axfrPort
51-
axfrPort = port
52-
t.Cleanup(func() { axfrPort = old })
53-
return srv
57+
return runCLIMergedWith(t, runArgs{axfrPort: port, args: args})
5458
}
5559

5660
// TestE2EZoneTransfer covers #85: a name server that allows AXFR gives
@@ -60,7 +64,7 @@ func TestE2EZoneTransfer(t *testing.T) {
6064
srv := axfrZone(t, true, "")
6165
wl := writeFile(t, "wl.txt", "www\nmail\n")
6266
stats := filepath.Join(t.TempDir(), "run.json")
63-
code, out := runCLIMerged(t, "-axfr", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.test")
67+
code, out := runAXFR(t, srv, "-axfr", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.test")
6468
if code != 0 || !strings.Contains(out, "admin-legacy.example.test") {
6569
t.Fatalf("exit %d: the transferred name was not found\n%s", code, out)
6670
}
@@ -93,7 +97,7 @@ func TestE2EZoneTransferRefused(t *testing.T) {
9397
srv := axfrZone(t, false, "nsec3")
9498
wl := writeFile(t, "wl.txt", "www\nmail\n")
9599
stats := filepath.Join(t.TempDir(), "run.json")
96-
code, out := runCLIMerged(t, "-axfr", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.test")
100+
code, out := runAXFR(t, srv, "-axfr", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.test")
97101
if code != 0 || !strings.Contains(out, "www.example.test") || strings.Contains(out, "admin-legacy") {
98102
t.Fatalf("exit %d; want the wordlist scan only\n%s", code, out)
99103
}
@@ -117,7 +121,7 @@ func TestE2EZoneWalkable(t *testing.T) {
117121
srv := axfrZone(t, false, "nsec")
118122
wl := writeFile(t, "wl.txt", "www\n")
119123
stats := filepath.Join(t.TempDir(), "run.json")
120-
code, out := runCLIMerged(t, "-axfr", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.test")
124+
code, out := runAXFR(t, srv, "-axfr", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.test")
121125
if code != 0 || !strings.Contains(out, "Zone walkable") {
122126
t.Fatalf("exit %d; want a walkable-zone finding\n%s", code, out)
123127
}

‎main_ct.go‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import (
99
"github.com/TMHSDigital/subenum/internal/passive"
1010
)
1111

12-
// ctBaseURL and waybackBaseURL, when set by a test, replace crt.sh and the
13-
// Wayback Machine.
14-
var ctBaseURL, waybackBaseURL string
15-
1612
// targetSource is one passive source's lookup for a target (#130, #193).
1713
type targetSource struct {
1814
source string // -sources name: ct, wayback
@@ -28,13 +24,14 @@ type targetSource struct {
2824
// ones a resumed run saved, so its candidate list matches the interrupted
2925
// run's, or a fresh lookup. The results, and the warning for each failed
3026
// lookup (a warning, not a failed scan), keep the order of sources.
31-
func passiveNames(ctx context.Context, sources []string, target string, saved *resumeTarget, out *output.Writer) []targetSource {
27+
// baseURLs, set by tests, replace the sources' endpoints by name.
28+
func passiveNames(ctx context.Context, sources []string, target string, saved *resumeTarget, out *output.Writer, baseURLs map[string]string) []targetSource {
3229
if len(sources) == 0 {
3330
return nil
3431
}
3532
opts := passive.Options{
3633
UserAgent: ProgramName + "/" + resolveVersion() + " (+https://github.com/TMHSDigital/subenum)",
37-
BaseURLs: map[string]string{"ct": ctBaseURL, "wayback": waybackBaseURL},
34+
BaseURLs: baseURLs,
3835
}
3936
res := make([]targetSource, len(sources))
4037
var wg sync.WaitGroup

‎main_ct_test.go‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ func TestE2ECertificateTransparency(t *testing.T) {
3737
w.WriteHeader(http.StatusServiceUnavailable)
3838
}))
3939
defer ctDown.Close()
40-
t.Cleanup(func() { ctBaseURL = "" })
4140

4241
wl := writeFile(t, "wl.txt", "www\nmail\n")
4342
stats := filepath.Join(t.TempDir(), "run.json")
4443
scan := func(base string) (int, string, output.Summary) {
45-
ctBaseURL = base + "/"
46-
code, out := runCLI(t, "", "-ct", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.com")
44+
code, out := runCLIWith(t, runArgs{sourceURLs: map[string]string{"ct": base + "/"},
45+
args: []string{"-ct", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.com"}})
4746
var s output.Summary
4847
if data, err := os.ReadFile(stats); err == nil {
4948
_ = json.Unmarshal(data, &s)
@@ -94,7 +93,6 @@ func TestE2EPassiveSources(t *testing.T) {
9493
w.WriteHeader(http.StatusServiceUnavailable)
9594
}))
9695
defer down.Close()
97-
t.Cleanup(func() { ctBaseURL, waybackBaseURL = "", "" })
9896

9997
wl := writeFile(t, "wl.txt", "www\nmail\n")
10098
stats := filepath.Join(t.TempDir(), "run.json")
@@ -110,8 +108,8 @@ func TestE2EPassiveSources(t *testing.T) {
110108
return s.Targets[0]
111109
}
112110

113-
ctBaseURL, waybackBaseURL = ctUp.URL+"/", wayback.URL
114-
code, out := runCLI(t, "", "-ct", "-sources", "wayback", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.com")
111+
code, out := runCLIWith(t, runArgs{sourceURLs: map[string]string{"ct": ctUp.URL + "/", "wayback": wayback.URL},
112+
args: []string{"-ct", "-sources", "wayback", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.com"}})
115113
if code != 0 || !strings.Contains(out, "legacy-billing.example.com") || !strings.Contains(out, "old-shop.example.com") {
116114
t.Fatalf("exit %d, results %q: want the CT and the Wayback name", code, out)
117115
}
@@ -123,8 +121,8 @@ func TestE2EPassiveSources(t *testing.T) {
123121
t.Errorf("ct_names = %d, want 1 (the ct entry, for compatibility)", tg.CTNames)
124122
}
125123

126-
waybackBaseURL = down.URL
127-
code, out = runCLI(t, "", "-sources", "wayback,ct", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.com")
124+
code, out = runCLIWith(t, runArgs{sourceURLs: map[string]string{"ct": ctUp.URL + "/", "wayback": down.URL},
125+
args: []string{"-sources", "wayback,ct", "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-stats", stats, "-w", wl, "example.com"}})
128126
if code != 0 || !strings.Contains(out, "legacy-billing.example.com") || strings.Contains(out, "old-shop") {
129127
t.Errorf("wayback down: exit %d, results %q; want the CT name only", code, out)
130128
}
@@ -182,11 +180,10 @@ func TestE2ESimulateSkipsCT(t *testing.T) {
182180
_, _ = w.Write([]byte(`[]`))
183181
}))
184182
defer ct.Close()
185-
ctBaseURL = ct.URL + "/"
186-
t.Cleanup(func() { ctBaseURL = "" })
187183

188184
wl := writeFile(t, "wl.txt", "www\nmail\n")
189-
code, out := runCLIMerged(t, "-ct", "-simulate", "-seed", "1", "-progress=false", "-w", wl, "example.com")
185+
code, out := runCLIMergedWith(t, runArgs{sourceURLs: map[string]string{"ct": ct.URL + "/"},
186+
args: []string{"-ct", "-simulate", "-seed", "1", "-progress=false", "-w", wl, "example.com"}})
190187
if code != 0 || !strings.Contains(out, "-ct is ignored in simulation mode") {
191188
t.Errorf("exit %d, want 0 and a note that -ct is ignored\n%s", code, out)
192189
}

‎main_e2e_test.go‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,30 @@ func (w *lineTrigger) Write(p []byte) (int, error) {
7474
return n, err
7575
}
7676

77-
// runCLIUntilLines runs the CLI like runCLI and calls fire once n lines of
78-
// its stdout have been written.
79-
func runCLIUntilLines(t *testing.T, n int, fire func(), args ...string) (int, string) {
77+
// runCLIUntilLines runs the CLI like runCLIWith and calls fire once n lines
78+
// of its stdout have been written.
79+
func runCLIUntilLines(t *testing.T, n int, fire func(), a runArgs) (int, string) {
8080
t.Helper()
8181
stdout := &lineTrigger{n: n, fire: fire}
82-
code := runWith(runArgs{args: args, stdin: strings.NewReader(""), stdout: stdout, stderr: io.Discard})
83-
return code, stdout.String()
82+
a.stdin, a.stdout, a.stderr = strings.NewReader(""), stdout, io.Discard
83+
return runWith(a), stdout.String()
84+
}
85+
86+
// runCLIWith is runCLI for a run that also sets test seams on runArgs
87+
// (#266): stdin is empty, and stdout is returned.
88+
func runCLIWith(t *testing.T, a runArgs) (int, string) {
89+
t.Helper()
90+
var stdout bytes.Buffer
91+
a.stdin, a.stdout, a.stderr = strings.NewReader(""), &stdout, io.Discard
92+
return runWith(a), stdout.String()
93+
}
94+
95+
// runCLIMergedWith is runCLIMerged with test seams set on runArgs.
96+
func runCLIMergedWith(t *testing.T, a runArgs) (int, string) {
97+
t.Helper()
98+
var merged lockedBuffer
99+
a.stdin, a.stdout, a.stderr = strings.NewReader(""), &merged, &merged
100+
return runWith(a), merged.String()
84101
}
85102

86103
func writeFile(t *testing.T, name, content string) string {
@@ -876,11 +893,8 @@ func TestE2EResume(t *testing.T) {
876893
// Interrupted run: -rate 20 makes 40 names take ~2s; interrupt once
877894
// three results are out, whatever the runner's speed (#260).
878895
signals := make(chan os.Signal, 1)
879-
signalReady, testSignals = nil, signals
880-
t.Cleanup(func() { signalReady, testSignals = nil, nil })
881-
code, first := runCLIUntilLines(t, 3, func() { signals <- os.Interrupt },
882-
append(append([]string{}, base...), "-rate", "20", "-state", statePath, "-o", outPath, "example.com")...)
883-
signalReady, testSignals = nil, nil
896+
code, first := runCLIUntilLines(t, 3, func() { signals <- os.Interrupt }, runArgs{signals: signals,
897+
args: append(append([]string{}, base...), "-rate", "20", "-state", statePath, "-o", outPath, "example.com")})
884898
if code != 130 {
885899
t.Fatalf("interrupted run: exit %d, want 130", code)
886900
}

‎main_resume_test.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,7 @@ func TestE2EResumeReplaysSourceNames(t *testing.T) {
284284
_, _ = w.Write([]byte(`[{"name_value": "other.example.com"}]`))
285285
}))
286286
defer ct.Close()
287-
ctBaseURL = ct.URL + "/"
288-
t.Cleanup(func() { ctBaseURL = "" })
287+
urls := map[string]string{"ct": ct.URL + "/"}
289288

290289
wl := writeFile(t, "wl.txt", "www\n")
291290
for name, tc := range map[string]struct {
@@ -298,7 +297,7 @@ func TestE2EResumeReplaysSourceNames(t *testing.T) {
298297
args := []string{tc.flag, "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-w", wl, "example.com"}
299298
path := writeState(t, resumeState{Args: args, Targets: []resumeTarget{tc.saved}})
300299
hits.Store(0)
301-
code, out := runCLI(t, "", "-resume", path)
300+
code, out := runCLIWith(t, runArgs{sourceURLs: urls, args: []string{"-resume", path}})
302301
if code != 0 || !strings.Contains(out, "legacy.example.com") || strings.Contains(out, "other.example.com") {
303302
t.Errorf("%s: exit %d, results %q; want the saved name legacy only", name, code, out)
304303
}

‎main_session.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,12 @@ func (s *session) setProgress() {
505505
// exit code for it. The returned stop ends the signal handling.
506506
func (s *session) handleSignals(ctx context.Context, cancel context.CancelFunc) (stop func()) {
507507
sigCh := make(chan os.Signal, 1)
508-
if testSignals != nil {
509-
sigCh = testSignals // tests deliver an interrupt on any OS
508+
if s.a.signals != nil {
509+
sigCh = s.a.signals // tests deliver an interrupt on any OS
510510
}
511511
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
512-
if signalReady != nil {
513-
close(signalReady)
512+
if s.a.signalReady != nil {
513+
close(s.a.signalReady)
514514
}
515515
out := s.out
516516
go func() {
@@ -633,7 +633,7 @@ func (s *session) scanOneTarget(ctx context.Context, i int, target string) (next
633633
// like a wordlist entry (#130, #193). One set of the entries serves
634634
// every source and the zone transfer (#248).
635635
cands := &candidates{target: target, entries: entries, seeds: s.seeds}
636-
sourceInfo := passiveNames(ctx, s.in.sources, target, saved, out)
636+
sourceInfo := passiveNames(ctx, s.in.sources, target, saved, out, s.a.sourceURLs)
637637
for _, src := range sourceInfo {
638638
added := cands.add(src.names)
639639
if src.fetched {
@@ -644,7 +644,7 @@ func (s *session) scanOneTarget(ctx context.Context, i int, target string) (next
644644
if s.f.axfr {
645645
// Transferred names are candidates like -ct's: each is still looked
646646
// up, so only names that resolve are reported (#85).
647-
axfrInfo = zoneTransfer(ctx, s.f, target, saved, out)
647+
axfrInfo = zoneTransfer(ctx, s.f, s.a.transferPort(), target, saved, out)
648648
added := cands.add(axfrInfo.names)
649649
if len(axfrInfo.allowed) > 0 {
650650
out.Info("Zone transfer: %d names under %s, %d of them new candidates", len(axfrInfo.names), target, added)

‎main_signal_unix_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func TestE2ESIGTERMExits143(t *testing.T) {
2020

2121
// 200 names at -rate 10 would take 20s; the signal, sent once the first
2222
// result is out rather than after a fixed sleep (#260), ends it early.
23-
code, _ := runCLIUntilLines(t, 1, func() { _ = syscall.Kill(syscall.Getpid(), syscall.SIGTERM) },
24-
"-simulate", "-hit-rate", "100", "-rate", "10", "-progress=false", "-w", wl, "example.com")
23+
code, _ := runCLIUntilLines(t, 1, func() { _ = syscall.Kill(syscall.Getpid(), syscall.SIGTERM) }, runArgs{
24+
args: []string{"-simulate", "-hit-rate", "100", "-rate", "10", "-progress=false", "-w", wl, "example.com"}})
2525
if code != 143 {
2626
t.Fatalf("exit %d after SIGTERM, want 143", code)
2727
}

0 commit comments

Comments
 (0)