11package secret_test
22
33import (
4+ "cmp"
45 "os"
56 "testing"
7+ "time"
68
79 "github.com/stretchr/testify/assert"
810 "github.com/stretchr/testify/require"
@@ -12,6 +14,17 @@ import (
1214 "github.com/aquasecurity/trivy/pkg/fanal/types"
1315)
1416
17+ // fakeFileInfo lets Required tests use synthetic scan-relative paths without
18+ // needing the path to exist on disk; Required only reads Size().
19+ type fakeFileInfo struct { size int64 }
20+
21+ func (f fakeFileInfo ) Name () string { return "" }
22+ func (f fakeFileInfo ) Size () int64 { return f .size }
23+ func (f fakeFileInfo ) Mode () os.FileMode { return 0 }
24+ func (f fakeFileInfo ) ModTime () time.Time { return time.Time {} }
25+ func (f fakeFileInfo ) IsDir () bool { return false }
26+ func (f fakeFileInfo ) Sys () any { return nil }
27+
1528func TestSecretAnalyzer (t * testing.T ) {
1629 wantFinding1 := types.SecretFinding {
1730 RuleID : "rule1" ,
@@ -208,35 +221,85 @@ func TestSecretAnalyzer(t *testing.T) {
208221}
209222
210223func TestSecretRequire (t * testing.T ) {
224+ const defaultConfig = "testdata/skip-tests-config.yaml"
225+
211226 tests := []struct {
212- name string
213- filePath string
214- want bool
227+ name string
228+ configPath string
229+ filePath string
230+ size int64
231+ want bool
215232 }{
216233 {
217- name : "pass regular file" ,
218- filePath : "testdata/secret.txt" ,
219- want : true ,
234+ name : "pass regular file" ,
235+ configPath : defaultConfig ,
236+ filePath : "testdata/secret.txt" ,
237+ want : true ,
220238 },
221239 {
222- name : "skip small file" ,
223- filePath : "testdata/emptyfile" ,
224- want : false ,
240+ name : "skip small file" ,
241+ configPath : defaultConfig ,
242+ filePath : "testdata/emptyfile" ,
243+ size : 5 ,
244+ want : false ,
225245 },
226246 {
227- name : "skip folder" ,
228- filePath : "testdata/node_modules/secret.txt" ,
229- want : false ,
247+ name : "skip folder" ,
248+ configPath : defaultConfig ,
249+ filePath : "testdata/node_modules/secret.txt" ,
250+ want : false ,
230251 },
231252 {
232- name : "skip file" ,
233- filePath : "testdata/package-lock.json" ,
234- want : false ,
253+ name : "skip file" ,
254+ configPath : defaultConfig ,
255+ filePath : "testdata/package-lock.json" ,
256+ want : false ,
235257 },
236258 {
237- name : "skip extension" ,
238- filePath : "testdata/secret.doc" ,
239- want : false ,
259+ name : "skip extension" ,
260+ configPath : defaultConfig ,
261+ filePath : "testdata/secret.doc" ,
262+ want : false ,
263+ },
264+ {
265+ name : "skip config file when configPath is a relative path matching filePath" ,
266+ configPath : "testdata/skip-tests-config.yaml" ,
267+ filePath : "testdata/skip-tests-config.yaml" ,
268+ want : false ,
269+ },
270+ {
271+ name : "skip config file when configPath is a bare filename matching filePath" ,
272+ configPath : "skip-tests-config.yaml" ,
273+ filePath : "skip-tests-config.yaml" ,
274+ want : false ,
275+ },
276+ {
277+ name : "skip config file when configPath has a scan-root prefix and filePath is the tail" ,
278+ configPath : "testdata/fixtures/repo/secrets/trivy-secret.yaml" ,
279+ filePath : "trivy-secret.yaml" ,
280+ want : false ,
281+ },
282+ {
283+ name : "do not skip file whose basename matches but path boundary does not" ,
284+ configPath : "trivy-secret.yaml" ,
285+ filePath : "my-trivy-secret.yaml" ,
286+ want : true ,
287+ },
288+ {
289+ // Known limitation of the path-suffix match: an unrelated file at the scan
290+ // root whose name equals the configPath's tail is also skipped. This locks
291+ // in the trade-off documented in Required so a refactor cannot silently
292+ // change it.
293+ name : "over-skip: configPath suffix matches unrelated file at scan root" ,
294+ configPath : "configs/myconfig.yaml" ,
295+ filePath : "myconfig.yaml" ,
296+ want : false ,
297+ },
298+ {
299+ name : "do not skip file when configPath is empty" ,
300+ configPath : "" ,
301+ filePath : "src/myfile.yaml" ,
302+ want : true ,
240303 },
241304 }
242305
@@ -245,15 +308,13 @@ func TestSecretRequire(t *testing.T) {
245308 a := secret.SecretAnalyzer {}
246309 err := a .Init (analyzer.AnalyzerOptions {
247310 SecretScannerOption : analyzer.SecretScannerOption {
248- ConfigPath : "testdata/skip-tests-config.yaml" ,
311+ ConfigPath : tt . configPath ,
249312 },
250313 })
251314 require .NoError (t , err )
252315
253- fi , err := os .Stat (tt .filePath )
254- require .NoError (t , err )
255-
256- got := a .Required (tt .filePath , fi )
316+ size := cmp .Or (tt .size , 1024 )
317+ got := a .Required (tt .filePath , fakeFileInfo {size : size })
257318 assert .Equal (t , tt .want , got )
258319 })
259320 }
0 commit comments