-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improved Python binary detection (#1648)
- Loading branch information
Showing
15 changed files
with
299 additions
and
100 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
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 |
---|---|---|
|
@@ -4,8 +4,11 @@ import ( | |
"errors" | ||
"fmt" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
|
@@ -68,25 +71,6 @@ func Test_Cataloger_DefaultClassifiers_PositiveCases(t *testing.T) { | |
Metadata: metadata("postgresql-binary"), | ||
}, | ||
}, | ||
{ | ||
name: "positive-python-duplicates", | ||
fixtureDir: "test-fixtures/classifiers/positive/python-duplicates", | ||
expected: pkg.Package{ | ||
Name: "python", | ||
Version: "3.8.16", | ||
Type: "binary", | ||
PURL: "pkg:generic/[email protected]", | ||
Locations: locations("dir/python3.8", "python3.8", "libpython3.8.so", "patchlevel.h"), | ||
Metadata: pkg.BinaryMetadata{ | ||
Matches: []pkg.ClassifierMatch{ | ||
match("python-binary", "dir/python3.8"), | ||
match("python-binary", "python3.8"), | ||
match("python-binary-lib", "libpython3.8.so"), | ||
match("cpython-source", "patchlevel.h"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "positive-traefik-2.9.6", | ||
fixtureDir: "test-fixtures/classifiers/positive/traefik-2.9.6", | ||
|
@@ -314,26 +298,82 @@ func Test_Cataloger_DefaultClassifiers_PositiveCases(t *testing.T) { | |
Metadata: metadata("python-binary-lib"), | ||
}, | ||
}, | ||
{ | ||
name: "positive-python-3.11.2-from-shared-lib", | ||
fixtureDir: "test-fixtures/classifiers/dynamic/python-binary-shared-lib-3.11", | ||
expected: pkg.Package{ | ||
Name: "python", | ||
Version: "3.11.2", | ||
PURL: "pkg:generic/[email protected]", | ||
Locations: locations("python3", "libpython3.11.so.1.0"), | ||
Metadata: pkg.BinaryMetadata{ | ||
Matches: []pkg.ClassifierMatch{ | ||
match("python-binary", "python3"), | ||
match("python-binary", "libpython3.11.so.1.0"), | ||
match("python-binary-lib", "libpython3.11.so.1.0"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "positive-python-3.9-from-shared-redhat-lib", | ||
fixtureDir: "test-fixtures/classifiers/dynamic/python-binary-shared-lib-redhat-3.9", | ||
expected: pkg.Package{ | ||
Name: "python", | ||
Version: "3.9.13", | ||
PURL: "pkg:generic/[email protected]", | ||
Locations: locations("python3.9", "libpython3.9.so.1.0"), | ||
Metadata: pkg.BinaryMetadata{ | ||
Matches: []pkg.ClassifierMatch{ | ||
match("python-binary", "python3.9"), | ||
match("python-binary", "libpython3.9.so.1.0"), | ||
match("python-binary-lib", "libpython3.9.so.1.0"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "positive-python-binary-with-version-3.9", | ||
fixtureDir: "test-fixtures/classifiers/dynamic/python-binary-with-version-3.9", | ||
expected: pkg.Package{ | ||
Name: "python", | ||
Version: "3.9.2", | ||
PURL: "pkg:generic/[email protected]", | ||
Locations: locations("python3.9"), | ||
Metadata: pkg.BinaryMetadata{ | ||
Matches: []pkg.ClassifierMatch{ | ||
match("python-binary", "python3.9"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "positive-python3.6", | ||
fixtureDir: "test-fixtures/classifiers/positive/python-binary-3.6", | ||
expected: pkg.Package{ | ||
Name: "python", | ||
Version: "3.6.3a-vZ9", | ||
PURL: "pkg:generic/[email protected].3a-vZ9", | ||
Version: "3.6.3", | ||
PURL: "pkg:generic/[email protected].3", | ||
Locations: locations("python3.6"), | ||
Metadata: metadata("python-binary"), | ||
}, | ||
}, | ||
{ | ||
name: "positive-patchlevel.h", | ||
fixtureDir: "test-fixtures/classifiers/positive/python-source-3.9", | ||
name: "positive-python-duplicates", | ||
fixtureDir: "test-fixtures/classifiers/positive/python-duplicates", | ||
expected: pkg.Package{ | ||
Name: "python", | ||
Version: "3.9-aZ5", | ||
PURL: "pkg:generic/[email protected]", | ||
Locations: locations("patchlevel.h"), | ||
Metadata: metadata("cpython-source"), | ||
Version: "3.8.16", | ||
Type: "binary", | ||
PURL: "pkg:generic/[email protected]", | ||
Locations: locations("dir/python3.8", "python3.8", "libpython3.8.so"), | ||
Metadata: pkg.BinaryMetadata{ | ||
Matches: []pkg.ClassifierMatch{ | ||
match("python-binary", "dir/python3.8"), | ||
match("python-binary", "python3.8"), | ||
match("python-binary-lib", "libpython3.8.so"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
|
@@ -491,17 +531,6 @@ func Test_Cataloger_DefaultClassifiers_PositiveCases(t *testing.T) { | |
require.NoError(t, err) | ||
|
||
for _, p := range packages { | ||
expectedLocations := test.expected.Locations.ToSlice() | ||
gotLocations := p.Locations.ToSlice() | ||
require.Len(t, gotLocations, len(expectedLocations)) | ||
|
||
for i, expectedLocation := range expectedLocations { | ||
gotLocation := gotLocations[i] | ||
if expectedLocation.RealPath != gotLocation.RealPath { | ||
t.Fatalf("locations do not match; expected: %v got: %v", expectedLocations, gotLocations) | ||
} | ||
} | ||
|
||
assertPackagesAreEqual(t, test.expected, p) | ||
} | ||
}) | ||
|
@@ -611,6 +640,21 @@ func match(classifier string, paths ...string) pkg.ClassifierMatch { | |
} | ||
|
||
func assertPackagesAreEqual(t *testing.T, expected pkg.Package, p pkg.Package) { | ||
var failMessages []string | ||
expectedLocations := expected.Locations.ToSlice() | ||
gotLocations := p.Locations.ToSlice() | ||
|
||
if len(expectedLocations) != len(gotLocations) { | ||
failMessages = append(failMessages, "locations are not equal length") | ||
} else { | ||
for i, expectedLocation := range expectedLocations { | ||
gotLocation := gotLocations[i] | ||
if expectedLocation.RealPath != gotLocation.RealPath { | ||
failMessages = append(failMessages, fmt.Sprintf("locations do not match; expected: %v got: %v", expectedLocation.RealPath, gotLocation.RealPath)) | ||
} | ||
} | ||
} | ||
|
||
m1 := expected.Metadata.(pkg.BinaryMetadata).Matches | ||
m2 := p.Metadata.(pkg.BinaryMetadata).Matches | ||
matches := true | ||
|
@@ -633,17 +677,26 @@ func assertPackagesAreEqual(t *testing.T, expected pkg.Package, p pkg.Package) { | |
} else { | ||
matches = false | ||
} | ||
|
||
if !matches { | ||
failMessages = append(failMessages, "classifier matches not equal") | ||
} | ||
if expected.Name != p.Name || | ||
expected.Version != p.Version || | ||
expected.PURL != p.PURL || | ||
!matches { | ||
assert.Failf(t, "packages not equal", "%v != %v", stringifyPkg(expected), stringifyPkg(p)) | ||
expected.PURL != p.PURL { | ||
failMessages = append(failMessages, "packages do not match") | ||
} | ||
} | ||
|
||
func stringifyPkg(p pkg.Package) string { | ||
matches := p.Metadata.(pkg.BinaryMetadata).Matches | ||
return fmt.Sprintf("(name=%s, version=%s, purl=%s, matches=%+v)", p.Name, p.Version, p.PURL, matches) | ||
if len(failMessages) > 0 { | ||
assert.Failf(t, strings.Join(failMessages, "; "), "diff: %s", | ||
cmp.Diff(expected, p, | ||
cmp.Transformer("Locations", func(l source.LocationSet) []source.Location { | ||
return l.ToSlice() | ||
}), | ||
cmpopts.IgnoreUnexported(pkg.Package{}, source.Location{}), | ||
cmpopts.IgnoreFields(pkg.Package{}, "CPEs", "FoundBy", "MetadataType", "Type"), | ||
)) | ||
} | ||
} | ||
|
||
type panicyResolver struct { | ||
|
Oops, something went wrong.