-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] adding tests for ELF package relationships
Signed-off-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
8 changed files
with
251 additions
and
18 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
cmd/syft/internal/test/integration/package_binary_elf_relationships_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package integration | ||
|
||
import ( | ||
"github.com/anchore/syft/syft/artifact" | ||
"github.com/anchore/syft/syft/source" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestBinaryElfRelationships(t *testing.T) { | ||
// node --> ["dependency of" nodes] | ||
expectedGraph := map[string][]string{ | ||
"glibc": { | ||
"libhello_world.so", | ||
"syfttestfixture", | ||
}, | ||
"libstdc++": { | ||
"syfttestfixture", | ||
}, | ||
"libhello_world.so": { | ||
"syfttestfixture", | ||
}, | ||
// TODO: we need to capture devel package | ||
} | ||
|
||
// run the test... | ||
sbom, _ := catalogFixtureImage(t, "elf-test-fixtures", source.SquashedScope) | ||
|
||
// get a mapping of package names to their IDs | ||
nameToId := map[string]artifact.ID{} | ||
|
||
recordPkgId := func(name string) { | ||
pkgs := sbom.Artifacts.Packages.PackagesByName(name) | ||
require.NotEmpty(t, pkgs, "expected package %q to be present in the SBOM", name) | ||
for _, p := range pkgs { | ||
nameToId[p.Name] = p.ID() | ||
} | ||
} | ||
for name, depNames := range expectedGraph { | ||
recordPkgId(name) | ||
for _, depName := range depNames { | ||
recordPkgId(depName) | ||
} | ||
} | ||
|
||
for name, expectedDepNames := range expectedGraph { | ||
pkgId := nameToId[name] | ||
p := sbom.Artifacts.Packages.Package(pkgId) | ||
require.NotNil(t, p, "expected package %q to be present in the SBOM", name) | ||
|
||
rels := sbom.RelationshipsForPackage(*p, artifact.DependencyOfRelationship) | ||
require.NotEmpty(t, rels, "expected package %q to have relationships", name) | ||
|
||
toIds := map[artifact.ID]struct{}{} | ||
for _, rel := range rels { | ||
toIds[rel.To.ID()] = struct{}{} | ||
} | ||
|
||
for _, depName := range expectedDepNames { | ||
depId := nameToId[depName] | ||
_, exists := toIds[depId] | ||
require.True(t, exists, "expected package %q to have a relationship to %q", name, depName) | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
cmd/syft/internal/test/integration/test-fixtures/elf-test-fixtures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../../../syft/pkg/cataloger/binary/test-fixtures/elf-test-fixtures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package binary | ||
|
||
import ( | ||
"reflect" | ||
"github.com/google/go-cmp/cmp" | ||
"testing" | ||
|
||
"github.com/anchore/syft/internal/sbomsync" | ||
|
@@ -11,7 +11,7 @@ import ( | |
) | ||
|
||
func TestNewDependencyRelationships(t *testing.T) { | ||
s := &sbom.SBOM{} | ||
|
||
tests := []struct { | ||
name string | ||
resolver file.Resolver | ||
|
@@ -21,15 +21,155 @@ func TestNewDependencyRelationships(t *testing.T) { | |
{ | ||
name: "blank sbom and accessor returns empty relationships", | ||
resolver: nil, | ||
accessor: sbomsync.NewBuilder(s).(sbomsync.Accessor), | ||
want: make([]artifact.Relationship, 0), | ||
accessor: func() sbomsync.Accessor { | ||
return sbomsync.NewBuilder(&sbom.SBOM{}).(sbomsync.Accessor) | ||
}(), | ||
want: make([]artifact.Relationship, 0), | ||
}, | ||
//{ | ||
// name: "binary elf cataloger test fixture", | ||
// resolver: nil, | ||
// accessor: func() sbomsync.Accessor { | ||
// s := sbom.SBOM{ | ||
// Artifacts: sbom.Artifacts{ | ||
// Packages: pkg.NewCollection(), | ||
// }, | ||
// } | ||
// builder := sbomsync.NewBuilder(&s) | ||
// | ||
// // add ELF packages | ||
// builder.AddPackages( | ||
// []pkg.Package{ | ||
// { | ||
// Name: "glibc", | ||
// Version: "2.28-236.el8_9.12", | ||
// Type: pkg.RpmPkg, | ||
// Metadata: pkg.RpmDBEntry{ | ||
// Files: []pkg.RpmFileRecord{ | ||
// // TODO...? | ||
// }, | ||
// }, | ||
// }, | ||
// { | ||
// Name: "libstdc++", | ||
// Version: "8.5.0-20.el8", | ||
// Type: pkg.RpmPkg, | ||
// Metadata: pkg.RpmDBEntry{ | ||
// Files: []pkg.RpmFileRecord{ | ||
// // TODO...? | ||
// }, | ||
// }, | ||
// }, | ||
// { | ||
// Name: "libhello_world.so", | ||
// Version: "0.01", | ||
// PURL: "pkg:generic/syftsys/[email protected]", | ||
// FoundBy: "", | ||
// Locations: file.NewLocationSet( | ||
// file.NewVirtualLocation("/usr/local/bin/elftests/elfbinwithnestedlib/bin/lib/libhello_world.so", "/usr/local/bin/elftests/elfbinwithnestedlib/bin/lib/libhello_world.so"), | ||
// file.NewVirtualLocation("/usr/local/bin/elftests/elfbinwithsisterlib/lib/libhello_world.so", "/usr/local/bin/elftests/elfbinwithsisterlib/lib/libhello_world.so"), | ||
// file.NewVirtualLocation("/usr/local/bin/elftests/elfbinwithsisterlib/lib/libhello_world2.so", "/usr/local/bin/elftests/elfbinwithsisterlib/lib/libhello_world2.so"), | ||
// ), | ||
// Language: "", | ||
// Type: pkg.BinaryPkg, | ||
// Metadata: pkg.ELFBinaryPackageNoteJSONPayload{ | ||
// Type: "testfixture", | ||
// Vendor: "syft", | ||
// System: "syftsys", | ||
// SourceRepo: "https://github.com/someone/somewhere.git", | ||
// Commit: "5534c38d0ffef9a3f83154f0b7a7fb6ab0ab6dbb", | ||
// }, | ||
// }, | ||
// { | ||
// Name: "syfttestfixture", | ||
// Version: "0.01", | ||
// PURL: "pkg:generic/syftsys/[email protected]", | ||
// FoundBy: "", | ||
// Locations: file.NewLocationSet( | ||
// file.NewLocation("/usr/local/bin/elftests/elfbinwithnestedlib/bin/elfbinwithnestedlib").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation), | ||
// file.NewLocation("/usr/local/bin/elftests/elfbinwithsisterlib/bin/elfwithparallellibbin1").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation), | ||
// file.NewLocation("/usr/local/bin/elftests/elfbinwithsisterlib/bin/elfwithparallellibbin2").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation), | ||
// ), | ||
// Language: "", | ||
// Type: pkg.BinaryPkg, | ||
// Metadata: pkg.ELFBinaryPackageNoteJSONPayload{ | ||
// Type: "testfixture", | ||
// Vendor: "syft", | ||
// System: "syftsys", | ||
// SourceRepo: "https://github.com/someone/somewhere.git", | ||
// Commit: "5534c38d0ffef9a3f83154f0b7a7fb6ab0ab6dbb", | ||
// }, | ||
// }, | ||
// }...) | ||
// | ||
// // add executables | ||
// | ||
// libstdcCoord := file.Coordinates{ | ||
// RealPath: "/usr/lib64/libstdc++.so.6.0.25", | ||
// } | ||
// | ||
// glibcCoord := file.Coordinates{ | ||
// RealPath: "/usr/lib64/libc.so.6", | ||
// } | ||
// | ||
// accessor := builder.(sbomsync.Accessor) | ||
// accessor.WriteToSBOM(func(s *sbom.SBOM) { | ||
// // add the libstdc++ executable | ||
// s.Artifacts.Executables[libstdcCoord] = file.Executable{ | ||
// Format: "elf", | ||
// HasExports: true, | ||
// HasEntrypoint: true, | ||
// ImportedLibraries: []string{ | ||
// "libm.so.6", | ||
// "libc.so.6", | ||
// "ld-linux-aarch64.so.1", | ||
// "libgcc_s.so.1", | ||
// }, | ||
// } | ||
// }) | ||
// | ||
// return accessor | ||
// }(), | ||
// want: []artifact.Relationship{}, | ||
//}, | ||
//{ | ||
// name: "binary elf cataloger test fixture", | ||
// resolver: nil, | ||
// accessor: func() sbomsync.Accessor { | ||
// s := sbom.SBOM{ | ||
// Artifacts: sbom.Artifacts{ | ||
// Packages: pkg.NewCollection(), | ||
// }, | ||
// } | ||
// builder := sbomsync.NewBuilder(&s) | ||
// | ||
// fixtureName := "elf-test-fixtures" | ||
// img := imagetest.GetFixtureImage(t, "docker-archive", fixtureName) | ||
// | ||
// src := stereoscopesource.New(img, stereoscopesource.ImageConfig{ | ||
// Reference: fixtureName, | ||
// }) | ||
// | ||
// r, err := src.FileResolver(source.SquashedScope) | ||
// require.NoError(t, err) | ||
// | ||
// cat := binary.NewELFPackageCataloger() | ||
// pkgs, relationships, err := cat.Catalog(context.Background(), r) | ||
// require.NoError(t, err) | ||
// | ||
// builder.AddPackages(pkgs...) | ||
// builder.AddRelationships(relationships...) | ||
// | ||
// return builder.(sbomsync.Accessor) | ||
// }(), | ||
// want: []artifact.Relationship{}, | ||
//}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
relationships := NewDependencyRelationships(tt.resolver, tt.accessor) | ||
if !reflect.DeepEqual(relationships, tt.want) { | ||
t.Errorf("NewDependencyRelationships() = %v, want %v", relationships, tt.want) | ||
if d := cmp.Diff(tt.want, relationships); d != "" { | ||
t.Errorf("unexpected relationships (-want, +got): %s", d) | ||
} | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../syft/pkg/cataloger/binary/test-fixtures/elf-test-fixtures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters