-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Benji Visser <[email protected]>
- Loading branch information
Showing
1 changed file
with
0 additions
and
159 deletions.
There are no files selected for viewing
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,17 +1,12 @@ | ||
package pkg | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/anchore/syft/syft/artifact" | ||
"github.com/anchore/syft/syft/cpe" | ||
"github.com/anchore/syft/syft/file" | ||
syftFile "github.com/anchore/syft/syft/file" | ||
"github.com/anchore/syft/syft/linux" | ||
syftPkg "github.com/anchore/syft/syft/pkg" | ||
"github.com/anchore/syft/syft/sbom" | ||
"github.com/scylladb/go-set" | ||
"github.com/scylladb/go-set/strset" | ||
"github.com/stretchr/testify/assert" | ||
|
@@ -656,157 +651,3 @@ func Test_getNameAndELVersion(t *testing.T) { | |
func intRef(i int) *int { | ||
return &i | ||
} | ||
|
||
func Test_RemovePackagesByOverlap(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
sbom *sbom.SBOM | ||
expectedPackages []string | ||
}{ | ||
{ | ||
name: "includes all packages without overlap", | ||
sbom: catalogWithOverlaps( | ||
[]string{":[email protected]", "apk:[email protected]", "binary:[email protected]"}, | ||
[]string{}), | ||
expectedPackages: []string{":[email protected]", "apk:[email protected]", "binary:[email protected]"}, | ||
}, | ||
{ | ||
name: "excludes single package by overlap", | ||
sbom: catalogWithOverlaps( | ||
[]string{"apk:[email protected]", "apk:[email protected]", "binary:[email protected]"}, | ||
[]string{"apk:[email protected] -> binary:[email protected]"}), | ||
expectedPackages: []string{"apk:[email protected]", "apk:[email protected]"}, | ||
}, | ||
{ | ||
name: "does not exclude if OS package owns OS package", | ||
sbom: catalogWithOverlaps( | ||
[]string{"rpm:[email protected]", "rpm:[email protected]"}, | ||
[]string{"rpm:[email protected] -> rpm:[email protected]"}), | ||
expectedPackages: []string{"rpm:[email protected]", "rpm:[email protected]"}, | ||
}, | ||
{ | ||
name: "does not exclude if owning package is non-OS", | ||
sbom: catalogWithOverlaps( | ||
[]string{"python:[email protected]", "python:[email protected]"}, | ||
[]string{"python:[email protected] -> python:[email protected]"}), | ||
expectedPackages: []string{"python:[email protected]", "python:[email protected]"}, | ||
}, | ||
{ | ||
name: "excludes multiple package by overlap", | ||
sbom: catalogWithOverlaps( | ||
[]string{"apk:[email protected]", "apk:[email protected]", "binary:[email protected]", "apk:[email protected]", "binary:[email protected]"}, | ||
[]string{"apk:[email protected] -> binary:[email protected]", "apk:[email protected] -> binary:[email protected]"}), | ||
expectedPackages: []string{"apk:[email protected]", "apk:[email protected]", "apk:[email protected]"}, | ||
}, | ||
{ | ||
name: "does not exclude with different types", | ||
sbom: catalogWithOverlaps( | ||
[]string{"rpm:[email protected]", "apk:[email protected]"}, | ||
[]string{"rpm:[email protected] -> apk:[email protected]"}), | ||
expectedPackages: []string{"apk:[email protected]", "rpm:[email protected]"}, | ||
}, | ||
{ | ||
name: "does not exclude if OS package owns OS package", | ||
sbom: catalogWithOverlaps( | ||
[]string{"rpm:[email protected]", "rpm:[email protected]"}, | ||
[]string{"rpm:[email protected] -> rpm:[email protected]"}), | ||
expectedPackages: []string{"rpm:[email protected]", "rpm:[email protected]"}, | ||
}, | ||
{ | ||
name: "does not exclude if owning package is non-OS", | ||
sbom: catalogWithOverlaps( | ||
[]string{"python:[email protected]", "python:[email protected]"}, | ||
[]string{"python:[email protected] -> python:[email protected]"}), | ||
expectedPackages: []string{"python:[email protected]", "python:[email protected]"}, | ||
}, | ||
{ | ||
name: "python bindings for system RPM install", | ||
sbom: withDistro(catalogWithOverlaps( | ||
[]string{"rpm:[email protected]", "python:[email protected]"}, | ||
[]string{"rpm:[email protected] -> python:[email protected]"}), "rhel"), | ||
expectedPackages: []string{"rpm:[email protected]"}, | ||
}, | ||
{ | ||
name: "amzn linux doesn't remove packages in this way", | ||
sbom: withDistro(catalogWithOverlaps( | ||
[]string{"rpm:[email protected]", "python:[email protected]"}, | ||
[]string{"rpm:[email protected] -> python:[email protected]"}), "amzn"), | ||
expectedPackages: []string{"rpm:[email protected]", "python:[email protected]"}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
catalog := removePackagesByOverlap(test.sbom.Artifacts.Packages, test.sbom.Relationships) | ||
pkgs := FromCollection(catalog, SynthesisConfig{}) | ||
var pkgNames []string | ||
for _, p := range pkgs { | ||
pkgNames = append(pkgNames, fmt.Sprintf("%s:%s@%s", p.Type, p.Name, p.Version)) | ||
} | ||
assert.EqualValues(t, test.expectedPackages, pkgNames) | ||
}) | ||
} | ||
} | ||
|
||
func catalogWithOverlaps(packages []string, overlaps []string) *sbom.SBOM { | ||
var pkgs []syftPkg.Package | ||
var relationships []artifact.Relationship | ||
|
||
toPkg := func(str string) syftPkg.Package { | ||
var typ, name, version string | ||
s := strings.Split(strings.TrimSpace(str), ":") | ||
if len(s) > 1 { | ||
typ = s[0] | ||
str = s[1] | ||
} | ||
s = strings.Split(str, "@") | ||
name = s[0] | ||
if len(s) > 1 { | ||
version = s[1] | ||
} | ||
|
||
p := syftPkg.Package{ | ||
Type: syftPkg.Type(typ), | ||
Name: name, | ||
Version: version, | ||
} | ||
p.SetID() | ||
|
||
return p | ||
} | ||
|
||
for _, pkg := range packages { | ||
p := toPkg(pkg) | ||
pkgs = append(pkgs, p) | ||
} | ||
|
||
for _, overlap := range overlaps { | ||
parts := strings.Split(overlap, "->") | ||
if len(parts) < 2 { | ||
panic("invalid overlap, use -> to specify, e.g.: pkg1->pkg2") | ||
} | ||
from := toPkg(parts[0]) | ||
to := toPkg(parts[1]) | ||
|
||
relationships = append(relationships, artifact.Relationship{ | ||
From: from, | ||
To: to, | ||
Type: artifact.OwnershipByFileOverlapRelationship, | ||
}) | ||
} | ||
|
||
catalog := syftPkg.NewCollection(pkgs...) | ||
|
||
return &sbom.SBOM{ | ||
Artifacts: sbom.Artifacts{ | ||
Packages: catalog, | ||
}, | ||
Relationships: relationships, | ||
} | ||
} | ||
|
||
func withDistro(s *sbom.SBOM, id string) *sbom.SBOM { | ||
s.Artifacts.LinuxDistribution = &linux.Release{ | ||
ID: id, | ||
} | ||
return s | ||
} |