github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/golang/stdlib_package_test.go (about) 1 package golang 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 11 "github.com/anchore/syft/internal/cmptest" 12 "github.com/anchore/syft/syft/artifact" 13 "github.com/anchore/syft/syft/cpe" 14 "github.com/anchore/syft/syft/file" 15 "github.com/anchore/syft/syft/pkg" 16 ) 17 18 func Test_stdlibPackageAndRelationships(t *testing.T) { 19 ctx := context.Background() 20 tests := []struct { 21 name string 22 pkgs []pkg.Package 23 wantPkgs int 24 wantRels int 25 }{ 26 { 27 name: "no packages", 28 }, 29 { 30 name: "ignore non-go-binary packages", 31 pkgs: []pkg.Package{ 32 { 33 Name: "not-go", 34 Version: "1.0.0", 35 Metadata: pkg.GolangModuleEntry{}, 36 }, 37 }, 38 wantPkgs: 0, 39 wantRels: 0, 40 }, 41 { 42 name: "with go-binary packages -- missing location", 43 pkgs: []pkg.Package{ 44 { 45 Name: "github.com/something/go", 46 Version: "1.0.0", 47 Metadata: pkg.GolangBinaryBuildinfoEntry{ 48 GoCompiledVersion: "go1.22.2", 49 MainModule: "github.com/something/go", 50 }, 51 }, 52 }, 53 wantPkgs: 0, 54 wantRels: 0, 55 }, 56 { 57 name: "with go-binary packages", 58 pkgs: []pkg.Package{ 59 { 60 Name: "github.com/something/go", 61 Version: "1.0.0", 62 Locations: file.NewLocationSet(file.NewLocation("/bin/my-app")), 63 Metadata: pkg.GolangBinaryBuildinfoEntry{ 64 GoCompiledVersion: "go1.22.2", 65 MainModule: "github.com/something/go", 66 }, 67 }, 68 }, 69 wantPkgs: 1, 70 wantRels: 1, 71 }, 72 { 73 name: "go binary package with devel stdlib", 74 pkgs: []pkg.Package{ 75 { 76 Name: "github.com/something/go", 77 Version: "1.0.0", 78 Locations: file.NewLocationSet(file.NewLocation("/bin/my-app")), 79 Metadata: pkg.GolangBinaryBuildinfoEntry{ 80 GoCompiledVersion: "devel", 81 MainModule: "github.com/something/go", 82 }, 83 }, 84 }, 85 wantPkgs: 0, 86 wantRels: 0, 87 }, 88 } 89 for _, tt := range tests { 90 t.Run(tt.name, func(t *testing.T) { 91 gotPkgs, gotRels := stdlibPackageAndRelationships(ctx, tt.pkgs) 92 assert.Len(t, gotPkgs, tt.wantPkgs) 93 assert.Len(t, gotRels, tt.wantRels) 94 }) 95 } 96 } 97 98 func Test_stdlibPackageAndRelationships_values(t *testing.T) { 99 loc := file.NewLocation("/bin/my-app") 100 locSet := file.NewLocationSet(loc) 101 ctx := context.TODO() 102 p := pkg.Package{ 103 Name: "github.com/something/go", 104 Version: "1.0.0", 105 Locations: locSet, 106 Metadata: pkg.GolangBinaryBuildinfoEntry{ 107 GoCompiledVersion: "go1.22.2", 108 MainModule: "github.com/something/go", 109 }, 110 } 111 p.SetID() 112 113 expectedPkg := pkg.Package{ 114 Name: "stdlib", 115 Version: "go1.22.2", 116 PURL: packageURL("stdlib", "1.22.2"), 117 Language: pkg.Go, 118 Type: pkg.GoModulePkg, 119 Licenses: pkg.NewLicenseSet(pkg.NewLicenseWithContext(ctx, "BSD-3-Clause")), 120 CPEs: []cpe.CPE{ 121 { 122 Attributes: cpe.MustAttributes("cpe:2.3:a:golang:go:1.22.2:-:*:*:*:*:*:*"), 123 Source: "syft-generated", 124 }, 125 }, 126 Locations: locSet, 127 Metadata: pkg.GolangBinaryBuildinfoEntry{ 128 GoCompiledVersion: "go1.22.2", 129 }, 130 } 131 132 expectedPkg.SetID() 133 134 expectedRel := artifact.Relationship{ 135 From: expectedPkg, 136 To: p, 137 Type: artifact.DependencyOfRelationship, 138 } 139 140 gotPkgs, gotRels := stdlibPackageAndRelationships(ctx, []pkg.Package{p}) 141 require.Len(t, gotPkgs, 1) 142 143 gotPkg := gotPkgs[0] 144 if d := cmp.Diff(expectedPkg, gotPkg, cmptest.DefaultOptions()...); d != "" { 145 t.Errorf("unexpected package (-want +got): %s", d) 146 } 147 148 require.Len(t, gotRels, 1) 149 gotRel := gotRels[0] 150 151 if d := cmp.Diff(expectedRel, gotRel, cmptest.DefaultOptions()...); d != "" { 152 t.Errorf("unexpected relationship (-want +got): %s", d) 153 } 154 155 }