github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/internal/relationship/binary/shared_library_index_test.go (about) 1 package binary 2 3 import ( 4 "path" 5 "testing" 6 7 "github.com/anchore/syft/syft/artifact" 8 "github.com/anchore/syft/syft/file" 9 "github.com/anchore/syft/syft/pkg" 10 ) 11 12 func Test_newShareLibIndex(t *testing.T) { 13 tests := []struct { 14 name string 15 resolver file.Resolver 16 coordinateIndex map[file.Coordinates]file.Executable 17 packages []pkg.Package 18 prexistingRelationships []artifact.Relationship 19 }{ 20 { 21 name: "constructor", 22 resolver: file.NewMockResolverForPaths(), 23 coordinateIndex: map[file.Coordinates]file.Executable{}, 24 packages: []pkg.Package{}, 25 prexistingRelationships: []artifact.Relationship{}, 26 }, 27 } 28 for _, tt := range tests { 29 t.Run(tt.name, func(t *testing.T) { 30 accessor := newAccessor(tt.packages, tt.coordinateIndex, tt.prexistingRelationships) 31 sharedLibraryIndex := newShareLibIndex(tt.resolver, accessor) 32 if sharedLibraryIndex == nil { 33 t.Errorf("newShareLibIndex() = %v, want non-nil", sharedLibraryIndex) 34 } 35 }) 36 } 37 } 38 39 func Test_sharedLibraryIndex_build(t *testing.T) { 40 glibcCoordinate := file.NewCoordinates("/usr/lib64/libc.so.6", "") 41 secondGlibcCoordinate := file.NewCoordinates("/usr/local/lib64/libc.so.6", "") 42 glibcExecutable := file.Executable{ 43 Format: "elf", 44 HasExports: true, 45 HasEntrypoint: true, 46 ImportedLibraries: []string{ 47 path.Base(glibcCoordinate.RealPath), 48 path.Base(secondGlibcCoordinate.RealPath), 49 }, 50 } 51 glibCPackage := pkg.Package{ 52 Name: "glibc", 53 Version: "2.28-236.el8_9.12", 54 Locations: file.NewLocationSet( 55 file.NewLocation(glibcCoordinate.RealPath), 56 file.NewLocation("some/other/path"), 57 ), 58 Type: pkg.RpmPkg, 59 Metadata: pkg.RpmDBEntry{ 60 Files: []pkg.RpmFileRecord{ 61 { 62 Path: glibcCoordinate.RealPath, 63 }, 64 { 65 Path: "some/other/path", 66 }, 67 }, 68 }, 69 } 70 71 tests := []struct { 72 name string 73 resolver file.Resolver 74 coordinateIndex map[file.Coordinates]file.Executable 75 packages []pkg.Package 76 prexistingRelationships []artifact.Relationship 77 }{ 78 { 79 name: "build with locations and packages", 80 resolver: file.NewMockResolverForPaths([]string{ 81 glibcCoordinate.RealPath, 82 secondGlibcCoordinate.RealPath, 83 }...), 84 coordinateIndex: map[file.Coordinates]file.Executable{ 85 glibcCoordinate: glibcExecutable, 86 secondGlibcCoordinate: glibcExecutable, 87 }, 88 packages: []pkg.Package{ 89 glibCPackage, 90 }, 91 prexistingRelationships: []artifact.Relationship{}, 92 }, 93 } 94 for _, tt := range tests { 95 t.Run(tt.name, func(t *testing.T) { 96 accessor := newAccessor(tt.packages, tt.coordinateIndex, tt.prexistingRelationships) 97 sharedLibraryIndex := newShareLibIndex(tt.resolver, accessor) 98 sharedLibraryIndex.build(tt.resolver, accessor) 99 pkgs := sharedLibraryIndex.owningLibraryPackage(path.Base(glibcCoordinate.RealPath)) 100 if pkgs.PackageCount() < 1 { 101 t.Errorf("owningLibraryPackage() = %v, want non-empty", pkgs) 102 } 103 }) 104 } 105 }