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