Skip to content
Open
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
39 changes: 39 additions & 0 deletions internal/cli/runreport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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})
Expand All @@ -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)
}
}
33 changes: 33 additions & 0 deletions pkg/subenum/zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Loading