Skip to content

Commit

Permalink
remove overlap test
Browse files Browse the repository at this point in the history
Signed-off-by: Benji Visser <[email protected]>
  • Loading branch information
noqcks committed Sep 19, 2023
1 parent 848afcf commit 906c104
Showing 1 changed file with 0 additions and 159 deletions.
159 changes: 0 additions & 159 deletions xeol/pkg/package_test.go
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"
Expand Down Expand Up @@ -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
}

0 comments on commit 906c104

Please sign in to comment.