github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/kernel/cataloger_test.go (about)

     1  package kernel
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/anchore/syft/syft/artifact"
     8  	"github.com/anchore/syft/syft/cpe"
     9  	"github.com/anchore/syft/syft/file"
    10  	"github.com/anchore/syft/syft/pkg"
    11  	"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
    12  )
    13  
    14  func Test_KernelCataloger(t *testing.T) {
    15  	ctx := context.TODO()
    16  	kernelPkg := pkg.Package{
    17  		Name:    "linux-kernel",
    18  		Version: "6.0.7-301.fc37.x86_64",
    19  		FoundBy: "linux-kernel-cataloger",
    20  		Locations: file.NewLocationSet(
    21  			file.NewVirtualLocation(
    22  				"/lib/modules/6.0.7-301.fc37.x86_64/vmlinuz",
    23  				"/lib/modules/6.0.7-301.fc37.x86_64/vmlinuz",
    24  			),
    25  		),
    26  		Type: pkg.LinuxKernelPkg,
    27  		PURL: "pkg:generic/linux-kernel@6.0.7-301.fc37.x86_64",
    28  		CPEs: []cpe.CPE{cpe.Must("cpe:2.3:o:linux:linux_kernel:6.0.7-301.fc37.x86_64:*:*:*:*:*:*:*", cpe.NVDDictionaryLookupSource)},
    29  		Metadata: pkg.LinuxKernel{
    30  			Name:            "",
    31  			Architecture:    "x86",
    32  			Version:         "6.0.7-301.fc37.x86_64",
    33  			ExtendedVersion: "6.0.7-301.fc37.x86_64 (mockbuild@bkernel01.iad2.fedoraproject.org) #1 SMP PREEMPT_DYNAMIC Fri Nov 4 18:35:48 UTC 2022",
    34  			BuildTime:       "",
    35  			Author:          "",
    36  			Format:          "bzImage",
    37  			RWRootFS:        false,
    38  			SwapDevice:      0,
    39  			RootDevice:      0,
    40  			VideoMode:       "Video mode 65535",
    41  		},
    42  	}
    43  
    44  	kernelModulePkg := pkg.Package{
    45  		Name:    "ttynull",
    46  		Version: "",
    47  		FoundBy: "linux-kernel-cataloger",
    48  		Locations: file.NewLocationSet(
    49  			file.NewVirtualLocation("/lib/modules/6.0.7-301.fc37.x86_64/kernel/drivers/tty/ttynull.ko",
    50  				"/lib/modules/6.0.7-301.fc37.x86_64/kernel/drivers/tty/ttynull.ko",
    51  			),
    52  		),
    53  		Licenses: pkg.NewLicenseSet(
    54  			pkg.NewLicenseFromLocationsWithContext(ctx, "GPL v2",
    55  				file.NewVirtualLocation(
    56  					"/lib/modules/6.0.7-301.fc37.x86_64/kernel/drivers/tty/ttynull.ko",
    57  					"/lib/modules/6.0.7-301.fc37.x86_64/kernel/drivers/tty/ttynull.ko",
    58  				),
    59  			),
    60  		),
    61  		Type: pkg.LinuxKernelModulePkg,
    62  		PURL: "pkg:generic/ttynull",
    63  		Metadata: pkg.LinuxKernelModule{
    64  			Name:          "ttynull",
    65  			Version:       "",
    66  			SourceVersion: "",
    67  			License:       "GPL v2",
    68  			Path:          "/lib/modules/6.0.7-301.fc37.x86_64/kernel/drivers/tty/ttynull.ko",
    69  			Description:   "",
    70  			KernelVersion: "6.0.7-301.fc37.x86_64",
    71  			VersionMagic:  "6.0.7-301.fc37.x86_64 SMP preempt mod_unload ",
    72  			Parameters:    map[string]pkg.LinuxKernelModuleParameter{},
    73  		},
    74  	}
    75  
    76  	expectedPkgs := []pkg.Package{
    77  		kernelPkg,
    78  		kernelModulePkg,
    79  	}
    80  	expectedRelationships := []artifact.Relationship{
    81  		{
    82  			From: kernelPkg,
    83  			To:   kernelModulePkg,
    84  			Type: artifact.DependencyOfRelationship,
    85  		},
    86  	}
    87  
    88  	pkgtest.NewCatalogTester().
    89  		WithImageResolver(t, "image-kernel-and-modules").
    90  		IgnoreLocationLayer().
    91  		Expects(expectedPkgs, expectedRelationships).
    92  		TestCataloger(t,
    93  			NewLinuxKernelCataloger(
    94  				LinuxKernelCatalogerConfig{
    95  					CatalogModules: true,
    96  				},
    97  			),
    98  		)
    99  }