|
| 1 | +package ospkg_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + ospkgDetector "github.com/aquasecurity/trivy/pkg/detector/ospkg" |
| 11 | + ftypes "github.com/aquasecurity/trivy/pkg/fanal/types" |
| 12 | + "github.com/aquasecurity/trivy/pkg/scan/ospkg" |
| 13 | + "github.com/aquasecurity/trivy/pkg/types" |
| 14 | +) |
| 15 | + |
| 16 | +type mockDriver struct { |
| 17 | + called bool |
| 18 | + vulns []types.DetectedVulnerability |
| 19 | +} |
| 20 | + |
| 21 | +func (m *mockDriver) Detect(_ context.Context, _ string, _ *ftypes.Repository, _ []ftypes.Package) ([]types.DetectedVulnerability, error) { |
| 22 | + m.called = true |
| 23 | + return m.vulns, nil |
| 24 | +} |
| 25 | + |
| 26 | +func (m *mockDriver) IsSupportedVersion(_ context.Context, _ ftypes.OSType, _ string) bool { |
| 27 | + return true |
| 28 | +} |
| 29 | + |
| 30 | +// TestScanner_Scan_ForwardsOptions verifies that options passed to NewScanner |
| 31 | +// are forwarded to ospkgDetector.NewDetector during Scan. |
| 32 | +func TestScanner_Scan_ForwardsOptions(t *testing.T) { |
| 33 | + target := types.ScanTarget{ |
| 34 | + OS: ftypes.OS{ |
| 35 | + Family: ftypes.CentOS, |
| 36 | + Name: "7", |
| 37 | + }, |
| 38 | + Packages: []ftypes.Package{ |
| 39 | + {Name: "vim"}, |
| 40 | + }, |
| 41 | + } |
| 42 | + opts := types.ScanOptions{ |
| 43 | + Scanners: types.Scanners{types.VulnerabilityScanner}, |
| 44 | + } |
| 45 | + |
| 46 | + want := []types.DetectedVulnerability{ |
| 47 | + {VulnerabilityID: "CVE-2024-0001"}, |
| 48 | + } |
| 49 | + |
| 50 | + mockDrv := &mockDriver{vulns: want} |
| 51 | + s := ospkg.NewScanner(ospkgDetector.WithDriver(target.OS.Family, mockDrv)) |
| 52 | + |
| 53 | + result, _, err := s.Scan(t.Context(), target, opts) |
| 54 | + require.NoError(t, err) |
| 55 | + |
| 56 | + assert.True(t, mockDrv.called, "expected the driver registered via the option to be used") |
| 57 | + assert.Equal(t, want, result.Vulnerabilities) |
| 58 | +} |
0 commit comments