github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/pkg/cataloger/golang/cataloger_test.go (about) 1 package golang 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/anchore/syft/syft/cpe" 9 "github.com/lineaje-labs/syft/syft/pkg/cataloger/internal/pkgtest" 10 ) 11 12 func Test_Mod_Cataloger_Globs(t *testing.T) { 13 tests := []struct { 14 name string 15 fixture string 16 expected []string 17 }{ 18 { 19 name: "obtain go.mod files", 20 fixture: "test-fixtures/glob-paths", 21 expected: []string{ 22 "src/go.mod", 23 }, 24 }, 25 } 26 27 for _, test := range tests { 28 t.Run(test.name, func(t *testing.T) { 29 pkgtest.NewCatalogTester(). 30 FromDirectory(t, test.fixture). 31 ExpectsResolverContentQueries(test.expected). 32 IgnoreUnfulfilledPathResponses("src/go.sum"). 33 TestCataloger(t, NewGoModuleFileCataloger(CatalogerConfig{})) 34 }) 35 } 36 } 37 38 func Test_Binary_Cataloger_Globs(t *testing.T) { 39 tests := []struct { 40 name string 41 fixture string 42 expected []string 43 }{ 44 { 45 name: "obtain binary files", 46 fixture: "test-fixtures/glob-paths", 47 expected: []string{ 48 "partial-binary", 49 }, 50 }, 51 } 52 53 for _, test := range tests { 54 t.Run(test.name, func(t *testing.T) { 55 pkgtest.NewCatalogTester(). 56 FromDirectory(t, test.fixture). 57 ExpectsResolverContentQueries(test.expected). 58 TestCataloger(t, NewGoModuleBinaryCataloger(CatalogerConfig{})) 59 }) 60 } 61 } 62 63 func Test_Binary_Cataloger_Stdlib_Cpe(t *testing.T) { 64 tests := []struct { 65 name string 66 candidate string 67 want string 68 }{ 69 { 70 name: "generateStdlibCpe generates a cpe with a - for a major version", 71 candidate: "go1.21.0", 72 want: "cpe:2.3:a:golang:go:1.21.0:-:*:*:*:*:*:*", 73 }, 74 { 75 name: "generateStdlibCpe generates a cpe with an rc candidate for a major rc version", 76 candidate: "go1.21rc2", 77 want: "cpe:2.3:a:golang:go:1.21:rc2:*:*:*:*:*:*", 78 }, 79 } 80 81 for _, tc := range tests { 82 t.Run(tc.name, func(t *testing.T) { 83 got, err := generateStdlibCpe(tc.candidate) 84 assert.NoError(t, err, "expected no err; got %v", err) 85 assert.Equal(t, cpe.String(got), tc.want) 86 }) 87 } 88 }