github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/format/internal/cyclonedxutil/helpers/cpe_test.go (about) 1 package helpers 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/anchore/syft/syft/cpe" 9 "github.com/anchore/syft/syft/pkg" 10 ) 11 12 func Test_encodeCPE(t *testing.T) { 13 testCPE := cpe.Must("cpe:2.3:a:name:name:3.2:*:*:*:*:*:*:*", "test-source") 14 testCPE2 := cpe.Must("cpe:2.3:a:name:name2:3.2:*:*:*:*:*:*:*", "test-source-2") 15 tests := []struct { 16 name string 17 input pkg.Package 18 expected string 19 }{ 20 { 21 // note: since this is an optional field, no value is preferred over NONE or NOASSERTION 22 name: "no metadata", 23 input: pkg.Package{ 24 CPEs: []cpe.CPE{}, 25 }, 26 expected: "", 27 }, 28 { 29 name: "single Attributes", 30 input: pkg.Package{ 31 CPEs: []cpe.CPE{ 32 testCPE, 33 }, 34 }, 35 expected: "cpe:2.3:a:name:name:3.2:*:*:*:*:*:*:*", 36 }, 37 { 38 name: "multiple CPEs", 39 input: pkg.Package{ 40 CPEs: []cpe.CPE{ 41 testCPE2, 42 testCPE, 43 }, 44 }, 45 expected: "cpe:2.3:a:name:name2:3.2:*:*:*:*:*:*:*", 46 }, 47 { 48 // note: since this is an optional field, no value is preferred over NONE or NOASSERTION 49 name: "empty", 50 input: pkg.Package{}, 51 expected: "", 52 }, 53 } 54 for _, test := range tests { 55 t.Run(test.name, func(t *testing.T) { 56 assert.Equal(t, test.expected, encodeSingleCPE(test.input)) 57 }) 58 } 59 }