github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/cmd/syft/internal/test/integration/package_binary_elf_relationships_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/anchore/syft/syft/artifact"
     9  	"github.com/anchore/syft/syft/source"
    10  )
    11  
    12  func TestBinaryElfRelationships(t *testing.T) {
    13  	// node --> ["dependency of" nodes]
    14  	expectedGraph := map[string][]string{
    15  		"glibc": {
    16  			"libhello_world.so",
    17  			"syfttestfixture",
    18  		},
    19  		"libstdc++": {
    20  			"syfttestfixture",
    21  		},
    22  		"libhello_world.so": {
    23  			"syfttestfixture",
    24  		},
    25  	}
    26  
    27  	// run the test...
    28  	sbom, _ := catalogFixtureImage(t, "elf-test-fixtures", source.SquashedScope)
    29  
    30  	// get a mapping of package names to their IDs
    31  	nameToId := map[string]artifact.ID{}
    32  
    33  	recordPkgId := func(name string) {
    34  		pkgs := sbom.Artifacts.Packages.PackagesByName(name)
    35  		require.NotEmpty(t, pkgs, "expected package %q to be present in the SBOM", name)
    36  		for _, p := range pkgs {
    37  			nameToId[p.Name] = p.ID()
    38  		}
    39  	}
    40  	for name, depNames := range expectedGraph {
    41  		recordPkgId(name)
    42  		for _, depName := range depNames {
    43  			recordPkgId(depName)
    44  		}
    45  	}
    46  
    47  	for name, expectedDepNames := range expectedGraph {
    48  		pkgId := nameToId[name]
    49  		p := sbom.Artifacts.Packages.Package(pkgId)
    50  		require.NotNil(t, p, "expected package %q to be present in the SBOM", name)
    51  
    52  		rels := sbom.RelationshipsForPackage(*p, artifact.DependencyOfRelationship)
    53  		require.NotEmpty(t, rels, "expected package %q to have relationships", name)
    54  
    55  		toIds := map[artifact.ID]struct{}{}
    56  		for _, rel := range rels {
    57  			toIds[rel.To.ID()] = struct{}{}
    58  		}
    59  
    60  		for _, depName := range expectedDepNames {
    61  			depId := nameToId[depName]
    62  			_, exists := toIds[depId]
    63  			require.True(t, exists, "expected package %q to have a relationship to %q", name, depName)
    64  		}
    65  	}
    66  
    67  }