Skip to content

Commit 8b908bb

Browse files
TMHSDigitalclaude
andcommitted
Check library ranges once and name TUI fields by label (#414)
Config.options re-checked Rate, MaxQueries and HitRate before calling scan.Options.Validate, which checks the same ranges with different wording. Drop the duplicates and keep only the negative checks that must run before 0 is replaced by a default; a table test checks that Validate's errors name the Config field. The resolver check stays in the library, since its message names the Resolver field, which the shared settings table has no entry for. The TUI's num helper took a hard-coded label for most numeric fields; it now takes the label from formFields, so parse errors always use the on-screen name. A test covers every numeric field. Fixes #414 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 58eba27 commit 8b908bb

4 files changed

Lines changed: 51 additions & 17 deletions

File tree

‎internal/tui/form.go‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,9 @@ func (m *formModel) validate() (formValues, string) {
553553

554554
// The form only parses its text fields here; ranges and the resolver are
555555
// checked by scan.Options.Validate, the same validator the CLI uses (#80).
556-
// Blank values use the same defaults as remembered().
557-
num := func(f int, name string) int {
556+
// Blank values use the same defaults as remembered(). A parse error names
557+
// the field by its on-screen label (#414).
558+
num := func(f int) int {
558559
s := strings.TrimSpace(text(f))
559560
if err != nil {
560561
return 0
@@ -564,27 +565,27 @@ func (m *formModel) validate() (formValues, string) {
564565
}
565566
n, perr := strconv.Atoi(s)
566567
if perr != nil {
567-
err = fmt.Errorf("%s must be a whole number, got %q", name, text(f))
568+
err = fmt.Errorf("%s must be a whole number, got %q", formFields[f].name(), text(f))
568569
}
569570
keep(f, strconv.Itoa(n))
570571
return n
571572
}
572-
concurrency := num(fieldConcurrency, "Concurrency")
573-
timeout := num(fieldTimeout, "Timeout")
574-
attempts := num(fieldAttempts, "Attempts")
573+
concurrency := num(fieldConcurrency)
574+
timeout := num(fieldTimeout)
575+
attempts := num(fieldAttempts)
575576
// Hit rate only matters in simulation mode, and depth only when
576577
// recursive is on; otherwise their fields must not block the scan.
577578
hitRate := scan.DefaultHitRate
578579
if m.shown(fieldHitRate) {
579-
hitRate = num(fieldHitRate, "Hit rate")
580+
hitRate = num(fieldHitRate)
580581
}
581582
depth := 1
582583
if m.shown(fieldDepth) {
583-
depth = num(fieldDepth, "Depth")
584+
depth = num(fieldDepth)
584585
}
585-
rate := num(fieldRate, "Rate") // blank means unlimited
586-
maxQueries := num(fieldMaxQueries, "Max queries") // blank means unlimited
587-
ratePerResolver := num(fieldRatePerResolver, formFields[fieldRatePerResolver].name())
586+
rate := num(fieldRate) // blank means unlimited
587+
maxQueries := num(fieldMaxQueries) // blank means unlimited
588+
ratePerResolver := num(fieldRatePerResolver)
588589
var maxDuration time.Duration // blank means unlimited, as -max-duration 0
589590
if s := strings.TrimSpace(text(fieldMaxDuration)); err == nil && s != "" {
590591
var perr error

‎internal/tui/form_test.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ func TestValidateHitRateOnlyWhenSimulate(t *testing.T) {
7474
}
7575
}
7676

77+
// TestValidateNumberErrorsUseFieldLabels covers #414: a value that is not a
78+
// whole number is reported under the field's on-screen label.
79+
func TestValidateNumberErrorsUseFieldLabels(t *testing.T) {
80+
for _, field := range []int{fieldConcurrency, fieldTimeout, fieldAttempts, fieldHitRate,
81+
fieldDepth, fieldRate, fieldMaxQueries, fieldRatePerResolver} {
82+
m := newFormModel(savedConfig{})
83+
m.inputs[fieldDomain].SetValue("example.com")
84+
m.toggles[fieldSimulate] = true // shows Hit Rate
85+
m.toggles[fieldRecursive] = true // shows Depth
86+
m.inputs[field].SetValue("lots")
87+
_, errStr := m.validate()
88+
if want := formFields[field].name() + " must be a whole number"; !strings.HasPrefix(errStr, want) {
89+
t.Errorf("field %d: error %q, want it to start %q", field, errStr, want)
90+
}
91+
}
92+
}
93+
7794
func TestValidateBlankNumericFieldsUseDefaults(t *testing.T) {
7895
for _, field := range []int{fieldConcurrency, fieldTimeout, fieldAttempts, fieldHitRate,
7996
fieldDepth, fieldRate, fieldMaxQueries, fieldRatePerResolver} {

‎pkg/subenum/subenum.go‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -616,21 +616,19 @@ func (c Config) options() (scan.Options, error) {
616616
return scan.Options{}, fmt.Errorf("subenum: Domain: %w", err)
617617
}
618618

619+
// Only the checks that differ from scan.Options.Validate live here: 0
620+
// means "use the default" for these fields, so a negative value must be
621+
// caught before the default replaces it. Every other range is checked
622+
// once, by Validate below (#414).
619623
switch {
620624
case c.Concurrency < 0:
621625
return scan.Options{}, fmt.Errorf("subenum: Concurrency must not be negative, got %d", c.Concurrency)
622626
case c.Timeout < 0:
623627
return scan.Options{}, fmt.Errorf("subenum: Timeout must not be negative, got %s", c.Timeout)
624628
case c.Attempts < 0:
625629
return scan.Options{}, fmt.Errorf("subenum: Attempts must not be negative, got %d", c.Attempts)
626-
case c.Rate < 0:
627-
return scan.Options{}, fmt.Errorf("subenum: Rate must be 0 (unlimited) or positive, got %d", c.Rate)
628-
case c.MaxQueries < 0:
629-
return scan.Options{}, fmt.Errorf("subenum: MaxQueries must be 0 (unlimited) or positive, got %d", c.MaxQueries)
630630
case c.Depth < 0:
631631
return scan.Options{}, fmt.Errorf("subenum: Depth must not be negative, got %d", c.Depth)
632-
case c.Simulate && c.HitRate != 0 && (c.HitRate < 1 || c.HitRate > 100):
633-
return scan.Options{}, fmt.Errorf("subenum: HitRate must be 1-100, got %d", c.HitRate)
634632
}
635633

636634
resolver := orDefaultString(c.Resolver, DefaultResolver)

‎pkg/subenum/subenum_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,24 @@ func TestValidateMatchesRun(t *testing.T) {
302302
}
303303
}
304304

305+
// TestValidateNamesConfigFields covers #414: the range checks Config shares
306+
// with the CLI and TUI (scan.Options.Validate) report the Config field.
307+
func TestValidateNamesConfigFields(t *testing.T) {
308+
for field, cfg := range map[string]subenum.Config{
309+
"Rate": {Domain: "example.com", Simulate: true, Rate: -1},
310+
"MaxQueries": {Domain: "example.com", Simulate: true, MaxQueries: -1},
311+
"HitRate": {Domain: "example.com", Simulate: true, HitRate: 101},
312+
"MaxDuration": {Domain: "example.com", Simulate: true, MaxDuration: -time.Second},
313+
"RatePerResolver": {Domain: "example.com", RatePerResolver: -1},
314+
"Resolver": {Domain: "example.com", Resolver: "not a resolver"},
315+
} {
316+
err := cfg.Validate()
317+
if err == nil || !strings.Contains(err.Error(), field) || !strings.HasPrefix(err.Error(), "subenum: ") {
318+
t.Errorf("%s: Validate = %v, want a subenum error naming %s", field, err, field)
319+
}
320+
}
321+
}
322+
305323
func TestDefaultWordsAreUsable(t *testing.T) {
306324
words := subenum.DefaultWords()
307325
if len(words) < 1000 {

0 commit comments

Comments
 (0)