8
8
"testing"
9
9
10
10
"github.com/slsa-framework/slsa-verifier/pkg"
11
+ "golang.org/x/mod/semver"
11
12
12
13
"github.com/google/go-cmp/cmp"
13
14
"github.com/google/go-cmp/cmp/cmpopts"
@@ -28,6 +29,8 @@ var generatorVersions = map[string][]string{
28
29
"v1.1.1" : {"go" },
29
30
"v1.2.0" : {"generic" }}
30
31
32
+ const TEST_DIR = "./testdata"
33
+
31
34
func Test_runVerify (t * testing.T ) {
32
35
t .Parallel ()
33
36
tests := []struct {
@@ -43,6 +46,10 @@ func Test_runVerify(t *testing.T) {
43
46
// or testdata from malicious untrusted builders.
44
47
// When true, this does not iterate over all builder versions.
45
48
noversion bool
49
+ // minversion is a special case to test a newly added feature into a builder
50
+ minversion string
51
+ // specifying builders will restrict builders to only the specified ones.
52
+ builders []string
46
53
}{
47
54
{
48
55
name : "valid main branch default" ,
@@ -300,6 +307,21 @@ func Test_runVerify(t *testing.T) {
300
307
pversiontag : pString ("v15.1" ),
301
308
err : pkg .ErrorMismatchVersionedTag ,
302
309
},
310
+ // Multiple subjects in version v1.2.0+
311
+ {
312
+ name : "multiple subject first match" ,
313
+ artifact : "binary-linux-amd64-multi-subject-first" ,
314
+ source : "github.com/slsa-framework/example-package" ,
315
+ minversion : "v1.2.0" ,
316
+ builders : []string {"generic" },
317
+ },
318
+ {
319
+ name : "multiple subject second match" ,
320
+ artifact : "binary-linux-amd64-multi-subject-second" ,
321
+ source : "github.com/slsa-framework/example-package" ,
322
+ minversion : "v1.2.0" ,
323
+ builders : []string {"generic" },
324
+ },
303
325
// Special case of the e2e test repository building builder from head.
304
326
{
305
327
name : "e2e test repository verified with builder at head" ,
@@ -343,35 +365,39 @@ func Test_runVerify(t *testing.T) {
343
365
tt := tt // Re-initializing variable so it is not changed while executing the closure below
344
366
t .Run (tt .name , func (t * testing.T ) {
345
367
// t.Parallel()
346
- getBuildersAndVersions := func () []string {
368
+ getBuildersAndVersions := func (minversion string , ttBuilders [] string ) []string {
347
369
res := []string {}
348
- builders := []string {}
349
- testdataDir , err := ioutil .ReadDir ("./testdata" )
350
- if err != nil {
351
- t .Fatal (err )
352
- }
353
- for _ , f := range testdataDir {
354
- if f .IsDir () {
355
- // These are the builder subfolders
356
- builders = append (builders , f .Name ())
370
+ builders := tt .builders
371
+ if len (builders ) == 0 {
372
+ testdataDir , err := ioutil .ReadDir (TEST_DIR )
373
+ if err != nil {
374
+ t .Error (err )
375
+ }
376
+ for _ , f := range testdataDir {
377
+ if f .IsDir () {
378
+ // These are the builder subfolders
379
+ builders = append (builders , f .Name ())
380
+ }
357
381
}
358
382
}
359
383
for _ , builder := range builders {
360
- builderDir , err := ioutil .ReadDir (fmt . Sprintf ( "./testdata/%s" , builder ))
384
+ builderDir , err := ioutil .ReadDir (filepath . Join ( TEST_DIR , builder ))
361
385
if err != nil {
362
- t .Fatal (err )
386
+ t .Error (err )
363
387
}
364
388
for _ , f := range builderDir {
365
- if f .IsDir () {
389
+ // Builder subfolders are semantic version strings.
390
+ // Compare if a min version is given.
391
+ if f .IsDir () && semver .Compare (minversion , f .Name ()) <= 0 {
366
392
// These are the supported versions of the builder
367
- res = append (res , fmt . Sprintf ( "%s/%s" , builder , f .Name ()))
393
+ res = append (res , filepath . Join ( builder , f .Name ()))
368
394
}
369
395
}
370
396
}
371
397
return res
372
398
}
373
399
374
- checkVersions := getBuildersAndVersions ()
400
+ checkVersions := getBuildersAndVersions (tt . minversion , tt . builders )
375
401
if tt .noversion {
376
402
checkVersions = []string {"" }
377
403
}
@@ -382,7 +408,7 @@ func Test_runVerify(t *testing.T) {
382
408
branch = "main"
383
409
}
384
410
385
- artifactPath = filepath .Clean (fmt . Sprintf ( "./testdata/%v/%s" , v , tt .artifact ))
411
+ artifactPath = filepath .Clean (filepath . Join ( TEST_DIR , v , tt .artifact ))
386
412
provenancePath = fmt .Sprintf ("%s.intoto.jsonl" , artifactPath )
387
413
388
414
_ , err := runVerify (artifactPath ,
0 commit comments