From 0fa5874aa59dfb3f9db0a741e457e4b74e64c24c Mon Sep 17 00:00:00 2001 From: Spencer Schrock Date: Mon, 26 Feb 2024 13:41:27 -0800 Subject: [PATCH] expand detailed tests to include other info like tool value Signed-off-by: Spencer Schrock --- probes/fuzzed/impl_test.go | 87 +++++++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 16 deletions(-) diff --git a/probes/fuzzed/impl_test.go b/probes/fuzzed/impl_test.go index e91bcbed7cb..082cd013850 100644 --- a/probes/fuzzed/impl_test.go +++ b/probes/fuzzed/impl_test.go @@ -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 +}