Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions internal/tui/follow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package tui

import (
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
"testing"

tea "charm.land/bubbletea/v2"

"github.com/TMHSDigital/subenum/internal/candidates"
"github.com/TMHSDigital/subenum/internal/dnstest"
"github.com/TMHSDigital/subenum/internal/output"
"github.com/TMHSDigital/subenum/internal/scan"
)

// TestFollowRecordsSources covers #494: a name a found name's records
// named is saved with source records and counted as records_found, with
// Follow Records alone and with Permute on.
func TestFollowRecordsSources(t *testing.T) {
redirectConfig(t)
srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
switch q.Name {
case "example.com":
return dnstest.Reply{}
case "app.example.com":
return dnstest.Reply{CNAME: "lb-7.int.example.com", A: []string{"192.0.2.7"}}
case "lb-7.int.example.com":
return dnstest.Reply{A: []string{"192.0.2.7"}}
}
return dnstest.NXDomain
})
for _, permute := range []bool{false, true} {
dir := t.TempDir()
wl, out := filepath.Join(dir, "wl.txt"), filepath.Join(dir, "out.jsonl")
if err := os.WriteFile(wl, []byte("app\n"), 0o644); err != nil {
t.Fatal(err)
}
m := New(Options{Version: "test"})
sized, _ := m.Update(tea.WindowSizeMsg{Width: 120, Height: 40})
m = sized.(Model)
for f, v := range map[int]string{
fieldDomain: "example.com", fieldWordlist: wl, fieldDNSServer: srv.Addr,
fieldTypes: "A,CNAME", fieldOutput: out, fieldFormat: "jsonl",
} {
m.form.inputs[f].SetValue(v)
}
m.form.toggles[fieldFollowRecords], m.form.toggles[fieldPermute] = true, permute
next, load := m.Update(keyCtrlR)
m = next.(Model)
if m.form.err != "" || load == nil {
t.Fatalf("permute %v: ctrl+r: error %q, load started %v", permute, m.form.err, load != nil)
}
next, listen := m.Update(load())
m = drive(t, next.(Model), listen)
data, err := os.ReadFile(out)
if err != nil {
t.Fatal(err)
}
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
sources := map[string][]string{}
for _, line := range lines[:len(lines)-1] {
var r output.Result
if err := json.Unmarshal([]byte(line), &r); err != nil {
t.Fatalf("result line %q: %v", line, err)
}
sources[r.Subdomain] = r.Sources
}
var summary output.Summary
if err := json.Unmarshal([]byte(lines[len(lines)-1]), &summary); err != nil || len(summary.Targets) != 1 {
t.Fatalf("no report (err %v): %s", err, lines[len(lines)-1])
}
if got := sources["lb-7.int.example.com"]; !slices.Equal(got, candidates.FromRecords) {
t.Errorf("permute %v: sources of lb-7.int = %v, want [records] (results: %v)", permute, got, sources)
}
if got := sources["app.example.com"]; !slices.Equal(got, candidates.FromWordlist) {
t.Errorf("permute %v: sources of app = %v, want [wordlist]", permute, got)
}
if got := summary.Targets[0].RecordsFound; got != 1 {
t.Errorf("permute %v: records_found = %d, want 1", permute, got)
}
}
}

// TestRecordFollowedInLaterPass covers #494 for a followed result found
// after the main pass: it keeps source records, not the pass's own.
func TestRecordFollowedInLaterPass(t *testing.T) {
for _, stage := range []scanStage{stagePermute, stagePTR} {
m := Model{scanView: newScanViewModel(120, 40, false), foundBy: map[string]int{}}
m.scanView.stage = stage
m.record(scan.Result{Subdomain: "lb-7.int.example.com"}, true)
if m.foundBy[candidates.Records] != 1 || len(m.foundBy) != 1 {
t.Errorf("stage %v: foundBy %v, want only records", stage, m.foundBy)
}
}
}
2 changes: 1 addition & 1 deletion internal/tui/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,5 +757,5 @@ func (v formValues) zoneLookup() bool { return v.axfr || v.nsecWalk }
// attributed reports whether results say what proposed them: something
// other than the wordlist can, as on the command line (#274, #418).
func (v formValues) attributed() bool {
return len(v.sources) > 0 || v.permute || v.zoneLookup() || v.srv || v.ptr
return len(v.sources) > 0 || v.permute || v.zoneLookup() || v.srv || v.ptr || v.FollowRecords
}
11 changes: 9 additions & 2 deletions internal/tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ func (m Model) updateScan(msg tea.Msg) (tea.Model, tea.Cmd) {
m.finishOutput(ev.Event)
return m, svCmd
case ev.Kind == scan.EventResult:
m.record(ev.Result)
m.record(ev.Result, ev.Followed)
}
// A kind this view does not know yet must not stall the scan: the
// listener is re-armed for it like any other event (#165).
Expand All @@ -567,9 +567,15 @@ func (m Model) updateScan(msg tea.Msg) (tea.Model, tea.Cmd) {
// record keeps a result for the permutation pass's seeds and writes it to
// the output file, saying what proposed it when the scan has sources or a
// permutation pass (#274).
func (m *Model) record(r scan.Result) {
func (m *Model) record(r scan.Result, followed bool) {
res := output.Result{Result: r}
switch stage := m.scanView.stage; {
case followed:
res.Sources = candidates.FromRecords // a found name's record named it (#432, #494)
res.Permutation = stage == stagePermute
if stage != stagePTR && stage != stagePermute && m.vals.permute {
m.names = append(m.names, r.Subdomain)
}
case stage == stagePTR:
res.Sources = candidates.FromPTR
case stage == stagePermute:
Expand Down Expand Up @@ -940,6 +946,7 @@ func (m *Model) summary(final scan.Event) output.Summary {
m.scanView.perm.summarize(&t, m.scanView.aborted, m.scanView.errText)
m.scanView.ptrp.summarize(&t, m.scanView.aborted, m.scanView.errText)
t.PTRFound = m.foundBy[candidates.PTR]
t.RecordsFound = m.foundBy[candidates.Records]
// As the CLI's, with the resolver pool's accounting (#367).
return report.Build(report.Run{
Version: m.opts.Version,
Expand Down