Skip to content

Commit 3669c52

Browse files
TMHSDigitalclaude
andcommitted
test: end-to-end tests for the CLI run path
Drive run() in simulation mode to cover exit codes (help, bad flags, extra positionals, empty wordlist or domain list, unwritable -o), JSON output as a single document including across -dL targets, stdin wordlists with plain piped output, and -seed reproducibility. main package coverage goes from 34% to 60%. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1a83076 commit 3669c52

1 file changed

Lines changed: 153 additions & 0 deletions

File tree

‎main_e2e_test.go‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
"os"
7+
"path/filepath"
8+
"sort"
9+
"strings"
10+
"testing"
11+
)
12+
13+
// runCLI invokes run() as the binary would, with args after the program name,
14+
// optional stdin, and stdout captured. Stderr is discarded. Simulation mode
15+
// keeps these tests free of network I/O.
16+
func runCLI(t *testing.T, stdin string, args ...string) (int, string) {
17+
t.Helper()
18+
oldArgs, oldStdout, oldStderr, oldStdin := os.Args, os.Stdout, os.Stderr, os.Stdin
19+
t.Cleanup(func() { os.Args, os.Stdout, os.Stderr, os.Stdin = oldArgs, oldStdout, oldStderr, oldStdin })
20+
21+
os.Args = append([]string{ProgramName}, args...)
22+
devnull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
23+
if err != nil {
24+
t.Fatal(err)
25+
}
26+
defer func() { _ = devnull.Close() }()
27+
os.Stderr = devnull
28+
29+
if stdin != "" {
30+
in, w, err := os.Pipe()
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
go func() {
35+
_, _ = io.WriteString(w, stdin)
36+
_ = w.Close()
37+
}()
38+
os.Stdin = in
39+
}
40+
41+
r, w, err := os.Pipe()
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
os.Stdout = w
46+
outCh := make(chan string)
47+
go func() {
48+
b, _ := io.ReadAll(r)
49+
outCh <- string(b)
50+
}()
51+
code := run()
52+
_ = w.Close()
53+
return code, <-outCh
54+
}
55+
56+
func writeFile(t *testing.T, name, content string) string {
57+
t.Helper()
58+
p := filepath.Join(t.TempDir(), name)
59+
if err := os.WriteFile(p, []byte(content), 0o600); err != nil {
60+
t.Fatal(err)
61+
}
62+
return p
63+
}
64+
65+
const e2eWords = "www\nmail\napi\ndev\nshop\nblog\n"
66+
67+
func TestE2ESimulateJSON(t *testing.T) {
68+
wl := writeFile(t, "wl.txt", e2eWords)
69+
code, out := runCLI(t, "", "-simulate", "-hit-rate", "100", "-progress=false", "-format", "json", "-w", wl, "example.com")
70+
if code != 0 {
71+
t.Fatalf("exit %d", code)
72+
}
73+
var res []struct {
74+
Subdomain string `json:"subdomain"`
75+
}
76+
if err := json.Unmarshal([]byte(out), &res); err != nil {
77+
t.Fatalf("stdout is not one JSON document: %v\n%s", err, out)
78+
}
79+
if len(res) != 6 {
80+
t.Errorf("got %d results, want 6", len(res))
81+
}
82+
}
83+
84+
func TestE2EDomainListSingleJSONArray(t *testing.T) {
85+
wl := writeFile(t, "wl.txt", e2eWords)
86+
dl := writeFile(t, "domains.txt", "# targets\na.example\nb.example\n")
87+
code, out := runCLI(t, "", "-simulate", "-hit-rate", "100", "-progress=false", "-format", "json", "-w", wl, "-dL", dl)
88+
if code != 0 {
89+
t.Fatalf("exit %d", code)
90+
}
91+
var res []struct {
92+
Subdomain string `json:"subdomain"`
93+
}
94+
if err := json.Unmarshal([]byte(out), &res); err != nil {
95+
t.Fatalf("-dL output is not a single JSON array: %v", err)
96+
}
97+
apexes := map[string]int{}
98+
for _, r := range res {
99+
apexes[r.Subdomain[strings.Index(r.Subdomain, ".")+1:]]++
100+
}
101+
if apexes["a.example"] != 6 || apexes["b.example"] != 6 {
102+
t.Errorf("results per apex = %v, want 6 each", apexes)
103+
}
104+
}
105+
106+
func TestE2EStdinWordlistPlainOutput(t *testing.T) {
107+
code, out := runCLI(t, "WWW\nmail\n# comment\n", "-simulate", "-hit-rate", "100", "-progress=false", "-w", "-", "example.com")
108+
if code != 0 {
109+
t.Fatalf("exit %d", code)
110+
}
111+
lines := strings.Fields(out)
112+
sort.Strings(lines)
113+
if strings.Join(lines, ",") != "mail.example.com,www.example.com" {
114+
t.Errorf("piped stdout = %q, want bare names", out)
115+
}
116+
}
117+
118+
func TestE2ESeedReproducible(t *testing.T) {
119+
wl := writeFile(t, "wl.txt", e2eWords+"vpn\ncdn\nadmin\nportal\n")
120+
args := []string{"-simulate", "-seed", "99", "-hit-rate", "50", "-t", "1", "-progress=false", "-show-records", "-w", wl, "example.com"}
121+
_, first := runCLI(t, "", args...)
122+
_, second := runCLI(t, "", args...)
123+
if first == "" || first != second {
124+
t.Errorf("same -seed gave different output:\n%s\n---\n%s", first, second)
125+
}
126+
}
127+
128+
func TestE2EExitCodes(t *testing.T) {
129+
wl := writeFile(t, "wl.txt", e2eWords)
130+
empty := writeFile(t, "empty.txt", "# only comments\n")
131+
cases := []struct {
132+
name string
133+
args []string
134+
want int
135+
}{
136+
{"help", []string{"-h"}, 0},
137+
{"unknown flag", []string{"-nope"}, 2},
138+
{"missing domain", []string{"-w", wl}, 1},
139+
{"two domains", []string{"-simulate", "-w", wl, "a.example", "b.example"}, 1},
140+
{"invalid flag value after domain", []string{"-simulate", "-w", wl, "example.com", "-t", "0"}, 1},
141+
{"wordlist without valid entries", []string{"-simulate", "-w", empty, "example.com"}, 1},
142+
{"domain list without valid domains", []string{"-simulate", "-w", wl, "-dL", empty}, 1},
143+
{"unwritable output file", []string{"-simulate", "-progress=false", "-w", wl, "-o", t.TempDir(), "example.com"}, 1},
144+
{"ok", []string{"-simulate", "-progress=false", "-w", wl, "example.com"}, 0},
145+
}
146+
for _, tc := range cases {
147+
t.Run(tc.name, func(t *testing.T) {
148+
if code, _ := runCLI(t, "", tc.args...); code != tc.want {
149+
t.Errorf("exit %d, want %d", code, tc.want)
150+
}
151+
})
152+
}
153+
}

0 commit comments

Comments
 (0)