From 8ed292809d774496f03ee88852b29886b4f202b1 Mon Sep 17 00:00:00 2001 From: nova-loop Date: Mon, 30 Nov 2026 10:02:59 +0800 Subject: [PATCH] Test the remaining #486 edges: report inputs and zone sources as Names subenum report: -results as one JSON array (with the summary as its last element) gives the same report as the JSONL file, a '{bad' -results file fails with 'reading -results file', and a '{}' -stats file fails with 'not a subenum -stats report'. pkg/subenum: ZoneTransfer(opts).Names and NSECWalk(opts).Names called directly return the zone's names against the local test server; a refused transfer gives no names and no error, and bad options are errors. Refs #486 --- internal/cli/runreport_test.go | 39 ++++++++++++++++++++++++++++++++++ pkg/subenum/zone_test.go | 33 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/internal/cli/runreport_test.go b/internal/cli/runreport_test.go index b98a1c3..d16ba08 100644 --- a/internal/cli/runreport_test.go +++ b/internal/cli/runreport_test.go @@ -56,6 +56,15 @@ func TestReportGolden(t *testing.T) { func TestReportErrors(t *testing.T) { results := filepath.Join("internal", "cli", "testdata", "report", "clean", "results.jsonl") stats := filepath.Join("internal", "cli", "testdata", "report", "clean", "run.json") + dir := t.TempDir() + badResults := filepath.Join(dir, "bad.jsonl") + emptyStats := filepath.Join(dir, "empty.json") + if err := os.WriteFile(badResults, []byte("{bad\n"), 0o600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(emptyStats, []byte("{}\n"), 0o600); err != nil { + t.Fatal(err) + } for _, tc := range []struct { args []string code int @@ -67,6 +76,8 @@ func TestReportErrors(t *testing.T) { {[]string{"report", "-stats", filepath.Join(t.TempDir(), "missing.json")}, exitFailure, "reading -stats file"}, {[]string{"report", "-stats", results}, exitFailure, "reading -stats file"}, {[]string{"report", "-stats", stats, "-changes", stats + ".missing"}, exitFailure, "reading -changes file"}, + {[]string{"report", "-stats", stats, "-results", badResults}, exitFailure, "reading -results file"}, + {[]string{"report", "-stats", emptyStats}, exitFailure, "not a subenum -stats report"}, } { var stdout, stderr bytes.Buffer code := runWith(runArgs{args: tc.args, stdin: strings.NewReader(""), stdout: &stdout, stderr: &stderr}) @@ -75,3 +86,31 @@ func TestReportErrors(t *testing.T) { } } } + +// TestReportResultsJSONArray covers #486: a -results file holding one JSON +// array (with the summary as its last element) gives the same report as the +// JSONL file with the same lines. +func TestReportResultsJSONArray(t *testing.T) { + dir := filepath.Join("internal", "cli", "testdata", "report", "degraded") + jsonl, err := os.ReadFile(filepath.Join(dir, "results.jsonl")) + if err != nil { + t.Fatal(err) + } + lines := strings.Split(strings.TrimSpace(strings.ReplaceAll(string(jsonl), "\r\n", "\n")), "\n") + array := filepath.Join(t.TempDir(), "results.json") + if err := os.WriteFile(array, []byte("[\n"+strings.Join(lines, ",\n")+"\n]\n"), 0o600); err != nil { + t.Fatal(err) + } + var stdout, stderr bytes.Buffer + args := []string{"report", "-stats", filepath.Join(dir, "run.json"), "-results", array, "-limit", "2"} + if code := runWith(runArgs{args: args, stdin: strings.NewReader(""), stdout: &stdout, stderr: &stderr}); code != exitOK { + t.Fatalf("exit %d: %s", code, stderr.String()) + } + want, err := os.ReadFile(filepath.Join(dir, "want.md")) + if err != nil { + t.Fatal(err) + } + if got := stdout.String(); got != strings.ReplaceAll(string(want), "\r\n", "\n") { + t.Errorf("report from a JSON array differs from the JSONL report:\n%s", got) + } +} diff --git a/pkg/subenum/zone_test.go b/pkg/subenum/zone_test.go index 3f97cb4..ed73653 100644 --- a/pkg/subenum/zone_test.go +++ b/pkg/subenum/zone_test.go @@ -232,3 +232,36 @@ func TestNSECWalkSource(t *testing.T) { t.Errorf("AXFR queries = %d, want 0", n) } } + +// TestZoneSourceNames covers #486: ZoneTransfer and NSECWalk used directly +// as sources, outside Scan, return the zone's names; a refused transfer +// gives no names and no error, and a bad resolver is an error. +func TestZoneSourceNames(t *testing.T) { + ctx := context.Background() + srv := zoneServer(t, true, "") + names, err := subenum.ZoneTransfer(subenum.ZoneOptions{Resolver: srv.Addr}).Names(ctx, "example.test") + if want := []string{"admin-legacy", "www"}; err != nil || !slices.Equal(names, want) { + t.Errorf("ZoneTransfer Names = %q, %v; want %q", names, err, want) + } + if _, err := subenum.ZoneTransfer(subenum.ZoneOptions{Resolver: "nonsense:"}).Names(ctx, "example.test"); err == nil { + t.Error("ZoneTransfer Names with a bad resolver: no error") + } + + refused := zoneServer(t, false, "") + names, err = subenum.ZoneTransfer(subenum.ZoneOptions{Resolver: refused.Addr}).Names(ctx, "example.test") + if err != nil || len(names) != 0 { + t.Errorf("refused ZoneTransfer Names = %q, %v; want no names and no error", names, err) + } + + walk := walkZone(t) + names, err = subenum.NSECWalk(subenum.ZoneOptions{Resolver: walk.Addr}).Names(ctx, "example.test") + if want := []string{"admin-legacy", "ns1", "secret"}; err != nil || !slices.Equal(names, want) { + t.Errorf("NSECWalk Names = %q, %v; want %q", names, err, want) + } + if n := walk.QueriesFor("example.test", dnsmessage.TypeAXFR); n != 0 { + t.Errorf("NSECWalk Names sent %d AXFR queries, want 0", n) + } + if _, err := subenum.NSECWalk(subenum.ZoneOptions{Resolver: walk.Addr, Rate: -1}).Names(ctx, "example.test"); err == nil { + t.Error("NSECWalk Names with a negative Rate: no error") + } +}