Skip to content

Commit 9822704

Browse files
TMHSDigitalclaude
andcommitted
Test resume's integrity checks and source replay (#255)
TestE2EResumeRefusesBadState covers a changed input file and a state file that is missing, corrupt or from another version. TestE2EResumeReplaysSourceNames checks that a resumed scan reuses the saved passive-source names, in the current and the legacy -ct format, and makes no request to the source again. Fixes #255 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2e81940 commit 9822704

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

‎main_resume_test.go‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ import (
44
"encoding/json"
55
"fmt"
66
"maps"
7+
"net/http"
8+
"net/http/httptest"
79
"os"
810
"path/filepath"
911
"reflect"
1012
"strings"
13+
"sync/atomic"
1114
"testing"
1215

16+
"golang.org/x/net/dns/dnsmessage"
17+
1318
"github.com/TMHSDigital/subenum/internal/dns"
19+
"github.com/TMHSDigital/subenum/internal/dnstest"
1420
"github.com/TMHSDigital/subenum/internal/output"
1521
"github.com/TMHSDigital/subenum/internal/scan"
1622
"github.com/TMHSDigital/subenum/internal/wordlist"
@@ -231,6 +237,77 @@ func TestE2EResumeRefusesChangedSettings(t *testing.T) {
231237
}
232238
}
233239

240+
// TestE2EResumeRefusesBadState covers #255: a changed input file, and a
241+
// state file that is missing, corrupt or from another version, each refuse
242+
// to resume with a message saying which.
243+
func TestE2EResumeRefusesBadState(t *testing.T) {
244+
wl := writeFile(t, "wl.txt", "www\napi\n")
245+
args := []string{"-simulate", "-seed", "3", "-progress=false", "-w", wl, "example.com"}
246+
changed := writeState(t, resumeState{Args: args, Inputs: map[string]string{wl: "digest-of-the-old-file"}, Targets: []resumeTarget{{Domain: "example.com"}}})
247+
dir := t.TempDir()
248+
corrupt := filepath.Join(dir, "corrupt.json")
249+
future := filepath.Join(dir, "future.json")
250+
if err := os.WriteFile(corrupt, []byte(`{"version": 1, "args": [`), 0o600); err != nil {
251+
t.Fatal(err)
252+
}
253+
if err := os.WriteFile(future, []byte(`{"version": 999, "args": ["example.com"]}`), 0o600); err != nil {
254+
t.Fatal(err)
255+
}
256+
for _, tc := range []struct{ path, want string }{
257+
{changed, "changed since the interrupted run"},
258+
{corrupt, "-resume " + corrupt + ":"},
259+
{future, "unsupported state version 999"},
260+
{filepath.Join(dir, "missing.json"), "reading -resume state"},
261+
} {
262+
if code, out := runCLIMerged(t, "-resume", tc.path); code == exitOK || !strings.Contains(out, tc.want) {
263+
t.Errorf("-resume %s: exit %d, want a failure saying %q\n%s", filepath.Base(tc.path), code, tc.want, out)
264+
}
265+
}
266+
}
267+
268+
// TestE2EResumeReplaysSourceNames covers #255: a resumed scan reuses the
269+
// names each passive source returned before the interrupt, in the current
270+
// and the legacy (-ct only) state format, and asks no source again.
271+
func TestE2EResumeReplaysSourceNames(t *testing.T) {
272+
srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
273+
if q.Name == "legacy.example.com" && q.Type == dnsmessage.TypeA {
274+
return dnstest.Reply{A: []string{"192.0.2.10"}}
275+
}
276+
if q.Name == "legacy.example.com" {
277+
return dnstest.Reply{}
278+
}
279+
return dnstest.NXDomain
280+
})
281+
var hits atomic.Int32
282+
ct := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
283+
hits.Add(1)
284+
_, _ = w.Write([]byte(`[{"name_value": "other.example.com"}]`))
285+
}))
286+
defer ct.Close()
287+
ctBaseURL = ct.URL + "/"
288+
t.Cleanup(func() { ctBaseURL = "" })
289+
290+
wl := writeFile(t, "wl.txt", "www\n")
291+
for name, tc := range map[string]struct {
292+
flag string
293+
saved resumeTarget
294+
}{
295+
"sources": {"-sources=ct", resumeTarget{Domain: "example.com", Sources: map[string][]string{"ct": {"legacy"}}}},
296+
"legacy": {"-ct", resumeTarget{Domain: "example.com", CT: []string{"legacy"}, CTFetched: true}},
297+
} {
298+
args := []string{tc.flag, "-dns-server", srv.Addr, "-type", "A", "-progress=false", "-w", wl, "example.com"}
299+
path := writeState(t, resumeState{Args: args, Targets: []resumeTarget{tc.saved}})
300+
hits.Store(0)
301+
code, out := runCLI(t, "", "-resume", path)
302+
if code != 0 || !strings.Contains(out, "legacy.example.com") || strings.Contains(out, "other.example.com") {
303+
t.Errorf("%s: exit %d, results %q; want the saved name legacy only", name, code, out)
304+
}
305+
if hits.Load() != 0 {
306+
t.Errorf("%s: the resumed scan asked crt.sh again (%d requests)", name, hits.Load())
307+
}
308+
}
309+
}
310+
234311
// TestSaveResumeStateKeepsEarlierPosition covers #109: a resumed target
235312
// interrupted again before its scan reported progress, and a target the run
236313
// never reached, keep their saved resume points.

0 commit comments

Comments
 (0)