github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/binary/elf_package_cataloger_test.go (about)

     1  package binary
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/anchore/syft/syft/file"
     9  	"github.com/anchore/syft/syft/pkg"
    10  	"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
    11  )
    12  
    13  func Test_ELFPackageCataloger(t *testing.T) {
    14  
    15  	cases := []struct {
    16  		name     string
    17  		fixture  string
    18  		expected []pkg.Package
    19  		wantErr  require.ErrorAssertionFunc
    20  	}{
    21  		{
    22  			name:    "go case",
    23  			fixture: "elf-test-fixtures",
    24  			expected: []pkg.Package{
    25  				{
    26  					Name:    "libhello_world.so",
    27  					Version: "0.01",
    28  					PURL:    "pkg:generic/syftsys/libhello_world.so@0.01",
    29  					Locations: file.NewLocationSet(
    30  						file.NewVirtualLocation("/usr/local/bin/elftests/elfbinwithnestedlib/bin/lib/libhello_world.so", "/usr/local/bin/elftests/elfbinwithnestedlib/bin/lib/libhello_world.so"),
    31  						file.NewVirtualLocation("/usr/local/bin/elftests/elfbinwithsisterlib/lib/libhello_world.so", "/usr/local/bin/elftests/elfbinwithsisterlib/lib/libhello_world.so"),
    32  						file.NewVirtualLocation("/usr/local/bin/elftests/elfbinwithsisterlib/lib/libhello_world2.so", "/usr/local/bin/elftests/elfbinwithsisterlib/lib/libhello_world2.so"),
    33  					),
    34  					Licenses: pkg.NewLicenseSet(
    35  						pkg.License{Value: "MIT", SPDXExpression: "MIT", Type: "declared"},
    36  					),
    37  
    38  					Type: pkg.BinaryPkg,
    39  					Metadata: pkg.ELFBinaryPackageNoteJSONPayload{
    40  						Type:       "testfixture",
    41  						Vendor:     "syft",
    42  						System:     "syftsys",
    43  						SourceRepo: "https://github.com/someone/somewhere.git",
    44  						Commit:     "5534c38d0ffef9a3f83154f0b7a7fb6ab0ab6dbb",
    45  					},
    46  				},
    47  				{
    48  					Name:    "syfttestfixture",
    49  					Version: "0.01",
    50  					PURL:    "pkg:generic/syftsys/syfttestfixture@0.01",
    51  					Locations: file.NewLocationSet(
    52  						file.NewLocation("/usr/local/bin/elftests/elfbinwithnestedlib/bin/elfbinwithnestedlib").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
    53  						file.NewLocation("/usr/local/bin/elftests/elfbinwithsisterlib/bin/elfwithparallellibbin1").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
    54  						file.NewLocation("/usr/local/bin/elftests/elfbinwithsisterlib/bin/elfwithparallellibbin2").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
    55  					),
    56  					Licenses: pkg.NewLicenseSet(
    57  						pkg.License{Value: "MIT", SPDXExpression: "MIT", Type: "declared"},
    58  					),
    59  					Type: pkg.BinaryPkg,
    60  					Metadata: pkg.ELFBinaryPackageNoteJSONPayload{
    61  						Type:       "testfixture",
    62  						Vendor:     "syft",
    63  						System:     "syftsys",
    64  						SourceRepo: "https://github.com/someone/somewhere.git",
    65  						Commit:     "5534c38d0ffef9a3f83154f0b7a7fb6ab0ab6dbb",
    66  					},
    67  				},
    68  			},
    69  			wantErr: require.Error,
    70  		},
    71  		{
    72  			name:    "fedora 64 bit binaries",
    73  			fixture: "image-fedora-64bit",
    74  			expected: []pkg.Package{
    75  				{
    76  					Name:    "coreutils",
    77  					Version: "9.5-3.fc41",
    78  					PURL:    "pkg:rpm/fedora/coreutils@9.5-3.fc41?distro=fedora-40",
    79  					Locations: file.NewLocationSet(
    80  						file.NewLocation("/sha256sum").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
    81  						file.NewLocation("/sha1sum").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
    82  					),
    83  					Licenses: pkg.NewLicenseSet(),
    84  					Type:     pkg.RpmPkg,
    85  					Metadata: pkg.ELFBinaryPackageNoteJSONPayload{
    86  						Type:         "rpm",
    87  						Architecture: "x86_64",
    88  						OSCPE:        "cpe:/o:fedoraproject:fedora:40",
    89  					},
    90  				},
    91  			},
    92  		},
    93  		{
    94  			name:    "fedora 32 bit binaries",
    95  			fixture: "image-fedora-32bit",
    96  			expected: []pkg.Package{
    97  				{
    98  					Name:    "coreutils",
    99  					Version: "9.0-5.fc36",
   100  					PURL:    "pkg:rpm/fedora/coreutils@9.0-5.fc36?distro=fedora-36",
   101  					Locations: file.NewLocationSet(
   102  						file.NewLocation("/sha256sum").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
   103  						file.NewLocation("/sha1sum").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
   104  					),
   105  					Licenses: pkg.NewLicenseSet(),
   106  					Type:     pkg.RpmPkg,
   107  					Metadata: pkg.ELFBinaryPackageNoteJSONPayload{
   108  						Type:         "rpm",
   109  						Architecture: "arm",
   110  						OSCPE:        "cpe:/o:fedoraproject:fedora:36",
   111  					},
   112  				},
   113  			},
   114  		},
   115  	}
   116  
   117  	for _, v := range cases {
   118  		t.Run(v.name, func(t *testing.T) {
   119  			pkgtest.NewCatalogTester().
   120  				WithImageResolver(t, v.fixture).
   121  				IgnoreLocationLayer(). // this fixture can be rebuilt, thus the layer ID will change
   122  				Expects(v.expected, nil).
   123  				WithErrorAssertion(v.wantErr).
   124  				TestCataloger(t, NewELFPackageCataloger())
   125  		})
   126  	}
   127  
   128  }