test: remove dll files and updates tests to use versionResources (#2276)

* test: remove dll files and updates tests to use versionResources

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>

* test: update integration tests with dot net coverage

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>

* chore: move test cases to appropriate blocks

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>

* fix: chmod only the dll

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>

* fix: add primary annotation key to packages

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>

* chore: bump number of packages with new dotnet package

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>

---------

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
This commit is contained in:
Christopher Angelo Phillips
2023-10-31 15:33:52 -04:00
committed by GitHub
parent 59c56a5666
commit ee4e69285a
7 changed files with 103 additions and 67 deletions
@@ -42,16 +42,26 @@ func parseDotnetPortableExecutable(_ file.Resolver, _ *generic.Environment, f fi
return nil, nil, nil
}
dotNetPkg, err := buildDotNetPackage(versionResources, f)
if err != nil {
// this is not a fatal error, just log and continue
// TODO: consider this case for "known unknowns" (same goes for cases below)
log.Tracef("unable to build dotnet package: %w", err)
return nil, nil, nil
}
return []pkg.Package{dotNetPkg}, nil, nil
}
func buildDotNetPackage(versionResources map[string]string, f file.LocationReadCloser) (dnpkg pkg.Package, err error) {
name := findName(versionResources)
if name == "" {
log.Tracef("unable to find FileDescription, or ProductName in PE file: %s", f.RealPath)
return nil, nil, nil
return dnpkg, fmt.Errorf("unable to find FileDescription, or ProductName in PE file: %s", f.RealPath)
}
version := findVersion(versionResources)
if strings.TrimSpace(version) == "" {
log.Tracef("unable to find FileVersion in PE file: %s", f.RealPath)
return nil, nil, nil
return dnpkg, fmt.Errorf("unable to find FileVersion in PE file: %s", f.RealPath)
}
purl := packageurl.NewPackageURL(
@@ -73,18 +83,19 @@ func parseDotnetPortableExecutable(_ file.Resolver, _ *generic.Environment, f fi
ProductVersion: versionResources["ProductVersion"],
}
p := pkg.Package{
dnpkg = pkg.Package{
Name: name,
Version: version,
Locations: file.NewLocationSet(f.Location),
Locations: file.NewLocationSet(f.Location.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)),
Type: pkg.DotnetPkg,
Language: pkg.Dotnet,
PURL: purl,
Metadata: metadata,
}
p.SetID()
dnpkg.SetID()
return []pkg.Package{p}, nil, nil
return dnpkg, nil
}
func findVersion(versionResources map[string]string) string {
@@ -3,7 +3,8 @@ package dotnet
import (
"testing"
"github.com/anchore/syft/syft/artifact"
"github.com/stretchr/testify/assert"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
@@ -11,73 +12,81 @@ import (
func TestParseDotnetPortableExecutable(t *testing.T) {
tests := []struct {
fixture string
expected []pkg.Package
name string
versionResources map[string]string
expectedPackage pkg.Package
}{
{
fixture: "test-fixtures/System.Buffers.dll",
expected: []pkg.Package{
{
Name: "System.Buffers",
Version: "7.0.923.36201",
Type: pkg.DotnetPkg,
PURL: "pkg:nuget/System.Buffers@7.0.923.36201",
Metadata: pkg.DotnetPortableExecutableEntry{
AssemblyVersion: "7.0.0.0",
LegalCopyright: "© Microsoft Corporation. All rights reserved.",
Comments: "System.Buffers",
InternalName: "System.Buffers.dll",
CompanyName: "Microsoft Corporation",
ProductName: "Microsoft® .NET",
ProductVersion: "7.0.9+8e9a17b2216f51a5788f8b1c467a4cf3b769e7d7",
},
name: "dotnet package with extra version info",
versionResources: map[string]string{
"InternalName": "Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll",
"FileVersion": "3.14.40721.0918 xxxfffdddjjjj",
"FileDescription": "Active Directory Authentication Library",
"ProductName": "Active Directory Authentication Library",
"Comments": "",
"CompanyName": "Microsoft Corporation",
"LegalTrademarks": "",
"LegalCopyright": "Copyright (c) Microsoft Corporation. All rights reserved.",
"OriginalFilename": "Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll",
"ProductVersion": "c61f043686a544863efc014114c42e844f905336",
"Assembly Version": "3.14.2.11",
},
expectedPackage: pkg.Package{
Name: "ActiveDirectoryAuthenticationLibrary",
Version: "3.14.40721.0918",
Locations: file.NewLocationSet(file.NewLocation("").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)),
Type: pkg.DotnetPkg,
Language: pkg.Dotnet,
PURL: "pkg:nuget/ActiveDirectoryAuthenticationLibrary@3.14.40721.0918",
Metadata: pkg.DotnetPortableExecutableEntry{
AssemblyVersion: "3.14.2.11",
LegalCopyright: "Copyright (c) Microsoft Corporation. All rights reserved.",
InternalName: "Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll",
CompanyName: "Microsoft Corporation",
ProductName: "Active Directory Authentication Library",
ProductVersion: "c61f043686a544863efc014114c42e844f905336",
},
},
},
{
fixture: "test-fixtures/Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll",
expected: []pkg.Package{
{
Name: "ActiveDirectoryAuthenticationLibrary",
Version: "3.14.40721.0918",
Type: pkg.DotnetPkg,
PURL: "pkg:nuget/ActiveDirectoryAuthenticationLibrary@3.14.40721.0918",
Metadata: pkg.DotnetPortableExecutableEntry{
AssemblyVersion: "3.14.2.11",
LegalCopyright: "Copyright (c) Microsoft Corporation. All rights reserved.",
InternalName: "Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll",
CompanyName: "Microsoft Corporation",
ProductName: "Active Directory Authentication Library",
ProductVersion: "c61f043686a544863efc014114c42e844f905336",
},
},
name: "dotnet package with malformed field and extended version",
versionResources: map[string]string{
"CompanyName": "Microsoft Corporation",
"FileDescription": "äbFileVersion",
"FileVersion": "4.6.25512.01 built by: dlab-DDVSOWINAGE016. Commit Hash: d0d5c7b49271cadb6d97de26d8e623e98abdc8db",
"InternalName": "äbFileVersion",
"LegalCopyright": "© Microsoft Corporation. All rights reserved.",
"OriginalFilename": "TProductName",
"ProductName": "Microsoft® .NET Framework",
"ProductVersion": "4.6.25512.01 built by: dlab-DDVSOWINAGE016. Commit Hash: d0d5c7b49271cadb6d97de26d8e623e98abdc8db",
},
},
{
fixture: "test-fixtures/sni.dll",
expected: []pkg.Package{
{
Name: "bFileVersion",
Version: "4.6.25512.01",
Type: pkg.DotnetPkg,
PURL: "pkg:nuget/bFileVersion@4.6.25512.01",
Metadata: pkg.DotnetPortableExecutableEntry{
LegalCopyright: "© Microsoft Corporation. All rights reserved.",
CompanyName: "Microsoft Corporation",
ProductName: "Microsoft® .NET Framework",
ProductVersion: "4.6.25512.01 built by: dlab-DDVSOWINAGE016. Commit Hash: d0d5c7b49271cadb6d97de26d8e623e98abdc8db",
},
expectedPackage: pkg.Package{
Name: "bFileVersion",
Version: "4.6.25512.01",
Locations: file.NewLocationSet(
file.NewLocation("").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)),
Type: pkg.DotnetPkg,
Language: pkg.Dotnet,
PURL: "pkg:nuget/bFileVersion@4.6.25512.01",
Metadata: pkg.DotnetPortableExecutableEntry{
LegalCopyright: "© Microsoft Corporation. All rights reserved.",
InternalName: "äb\x01FileVersion",
CompanyName: "Microsoft Corporation",
ProductName: "Microsoft® .NET Framework",
ProductVersion: "4.6.25512.01 built by: dlab-DDVSOWINAGE016. Commit Hash: d0d5c7b49271cadb6d97de26d8e623e98abdc8db",
},
},
},
}
for _, tc := range tests {
t.Run(tc.fixture, func(t *testing.T) {
fixtureLocationSet := file.NewLocationSet(file.NewLocation(tc.fixture))
tc.expected[0].Locations = fixtureLocationSet
var expectedRelationships []artifact.Relationship
pkgtest.TestFileParser(t, tc.fixture, parseDotnetPortableExecutable, tc.expected, expectedRelationships)
t.Run(tc.name, func(t *testing.T) {
f := file.LocationReadCloser{
Location: file.NewLocation(""),
}
got, err := buildDotNetPackage(tc.versionResources, f)
assert.NoErrorf(t, err, "failed to build package from version resources: %+v", tc.versionResources)
pkgtest.AssertPackagesEqual(t, tc.expectedPackage, got)
})
}
}