github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/binary/elf_package_test.go (about)

     1  package binary
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/google/go-cmp/cmp/cmpopts"
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/anchore/syft/syft/file"
    11  	"github.com/anchore/syft/syft/pkg"
    12  )
    13  
    14  func Test_packageURL(t *testing.T) {
    15  	tests := []struct {
    16  		name     string
    17  		notes    elfBinaryPackageNotes
    18  		expected string
    19  	}{
    20  		{
    21  			name: "elf-binary-package-cataloger",
    22  			notes: elfBinaryPackageNotes{
    23  				Name:    "github.com/anchore/syft",
    24  				Version: "v0.1.0",
    25  				ELFBinaryPackageNoteJSONPayload: pkg.ELFBinaryPackageNoteJSONPayload{
    26  					System: "syftsys",
    27  				},
    28  			},
    29  			expected: "pkg:generic/syftsys/github.com/anchore/syft@v0.1.0",
    30  		},
    31  		{
    32  			name: "elf binary package short name",
    33  			notes: elfBinaryPackageNotes{
    34  				Name:    "go.opencensus.io",
    35  				Version: "v0.23.0",
    36  				ELFBinaryPackageNoteJSONPayload: pkg.ELFBinaryPackageNoteJSONPayload{
    37  					System: "syftsys",
    38  				},
    39  			},
    40  			expected: "pkg:generic/syftsys/go.opencensus.io@v0.23.0",
    41  		},
    42  	}
    43  
    44  	for _, test := range tests {
    45  		t.Run(test.name, func(t *testing.T) {
    46  			assert.Equal(t, test.expected, packageURL(test.notes))
    47  		})
    48  	}
    49  }
    50  
    51  func Test_newELFPackage(t *testing.T) {
    52  	tests := []struct {
    53  		name     string
    54  		metadata elfBinaryPackageNotes
    55  		expected pkg.Package
    56  	}{
    57  		{
    58  			name: "elf-binary-package-cataloger",
    59  			metadata: elfBinaryPackageNotes{
    60  				Name:    "syfttestfixture",
    61  				Version: "0.01",
    62  				PURL:    "pkg:generic/syftsys/syfttestfixture@0.01",
    63  				CPE:     "cpe:/o:syft:syftsys_testfixture_syfttestfixture:0.01",
    64  				ELFBinaryPackageNoteJSONPayload: pkg.ELFBinaryPackageNoteJSONPayload{
    65  					Type:   "binary",
    66  					System: "syftsys",
    67  				},
    68  			},
    69  
    70  			expected: pkg.Package{
    71  				Name:    "syfttestfixture",
    72  				Version: "0.01",
    73  				Type:    "binary",
    74  				PURL:    "pkg:generic/syftsys/syfttestfixture@0.01",
    75  				Metadata: pkg.ELFBinaryPackageNoteJSONPayload{
    76  					Type:   "binary",
    77  					System: "syftsys",
    78  				},
    79  			},
    80  		},
    81  	}
    82  
    83  	for _, test := range tests {
    84  		t.Run(test.name, func(t *testing.T) {
    85  			actual := newELFPackage(test.metadata, file.NewLocationSet(), nil)
    86  			if diff := cmp.Diff(test.expected, actual, cmpopts.IgnoreFields(pkg.Package{}, "id"), cmpopts.IgnoreUnexported(pkg.Package{}, file.LocationSet{}, pkg.LicenseSet{})); diff != "" {
    87  				t.Errorf("newELFPackage() mismatch (-want +got):\n%s", diff)
    88  			}
    89  		})
    90  	}
    91  }