Skip to content

Commit

Permalink
expand detailed tests to include other info like tool value
Browse files Browse the repository at this point in the history
Signed-off-by: Spencer Schrock <[email protected]>
  • Loading branch information
spencerschrock committed Feb 26, 2024
1 parent 2eaf3b0 commit 0fa5874
Showing 1 changed file with 71 additions and 16 deletions.
87 changes: 71 additions & 16 deletions probes/fuzzed/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,85 @@ func Test_Run(t *testing.T) {
}
}

func TestRun_Location(t *testing.T) {
// for tests that want to check more than just the outcome.
func TestRun_Detailed(t *testing.T) {
t.Parallel()
raw := &checker.RawResults{
FuzzingResults: checker.FuzzingData{
Fuzzers: []checker.Tool{
tests := []struct {
err error
raw *checker.RawResults
name string
expected []finding.Finding
}{
{
name: "fuzzer file locations are propagated",
raw: &checker.RawResults{
FuzzingResults: checker.FuzzingData{
Fuzzers: []checker.Tool{
{
Name: fuzzers.BuiltInGo,
Files: []checker.File{
{
Path: "foo.go",
},
},
},
},
},
},
expected: []finding.Finding{
{
Name: fuzzers.BuiltInGo,
Files: []checker.File{
Probe: Probe,
Message: "GoBuiltInFuzzer integration found",
Outcome: finding.OutcomePositive,
Values: map[string]string{
ToolKey: fuzzers.BuiltInGo,
},
Location: &finding.Location{
LineStart: asPtr(uint(0)),
Path: "foo.go",
},
},
},
},
{
name: "fuzzer name is included as tool Value",
raw: &checker.RawResults{
FuzzingResults: checker.FuzzingData{
Fuzzers: []checker.Tool{
{
Path: "foo.go",
Name: "some fuzzer",
},
},
},
},
expected: []finding.Finding{
{
Probe: Probe,
Message: "some fuzzer integration found",
Outcome: finding.OutcomePositive,
Values: map[string]string{
ToolKey: "some fuzzer",
},
},
},
},
}
findings, _, err := Run(raw)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(findings) != 1 {
t.Fatalf("expected one finding, got %v", findings)
}
if diff := cmp.Diff(findings[0].Location.Path, "foo.go"); diff != "" {
t.Errorf("incorrect path: %s", diff)

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
findings, _, err := Run(tt.raw)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if diff := cmp.Diff(findings, tt.expected); diff != "" {
t.Error(diff)
}
})
}
}

func asPtr[T any](x T) *T {
return &x
}

0 comments on commit 0fa5874

Please sign in to comment.