github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/pkg/cataloger/deb/cataloger_test.go (about)

     1  package deb
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/nextlinux/gosbom/gosbom/file"
     7  	"github.com/nextlinux/gosbom/gosbom/pkg"
     8  	"github.com/nextlinux/gosbom/gosbom/pkg/cataloger/internal/pkgtest"
     9  )
    10  
    11  func TestDpkgCataloger(t *testing.T) {
    12  	licenseLocation := file.NewVirtualLocation("/usr/share/doc/libpam-runtime/copyright", "/usr/share/doc/libpam-runtime/copyright")
    13  	expected := []pkg.Package{
    14  		{
    15  			Name:    "libpam-runtime",
    16  			Version: "1.1.8-3.6",
    17  			FoundBy: "dpkgdb-cataloger",
    18  			Licenses: pkg.NewLicenseSet(
    19  				pkg.NewLicenseFromLocations("GPL-1", licenseLocation),
    20  				pkg.NewLicenseFromLocations("GPL-2", licenseLocation),
    21  				pkg.NewLicenseFromLocations("LGPL-2.1", licenseLocation),
    22  			),
    23  			Locations: file.NewLocationSet(
    24  				file.NewVirtualLocation("/var/lib/dpkg/status", "/var/lib/dpkg/status"),
    25  				file.NewVirtualLocation("/var/lib/dpkg/info/libpam-runtime.md5sums", "/var/lib/dpkg/info/libpam-runtime.md5sums"),
    26  				file.NewVirtualLocation("/var/lib/dpkg/info/libpam-runtime.conffiles", "/var/lib/dpkg/info/libpam-runtime.conffiles"),
    27  				file.NewVirtualLocation("/usr/share/doc/libpam-runtime/copyright", "/usr/share/doc/libpam-runtime/copyright"),
    28  			),
    29  			Type:         pkg.DebPkg,
    30  			MetadataType: pkg.DpkgMetadataType,
    31  			Metadata: pkg.DpkgMetadata{
    32  				Package:       "libpam-runtime",
    33  				Source:        "pam",
    34  				Version:       "1.1.8-3.6",
    35  				Architecture:  "all",
    36  				Maintainer:    "Steve Langasek <vorlon@debian.org>",
    37  				InstalledSize: 1016,
    38  				Description: `Runtime support for the PAM library
    39   Contains configuration files and  directories required for
    40   authentication  to work on Debian systems.  This package is required
    41   on almost all installations.`,
    42  				Files: []pkg.DpkgFileRecord{
    43  					{
    44  						Path: "/etc/pam.conf",
    45  						Digest: &file.Digest{
    46  							Algorithm: "md5",
    47  							Value:     "87fc76f18e98ee7d3848f6b81b3391e5",
    48  						},
    49  						IsConfigFile: true,
    50  					},
    51  					{
    52  						Path: "/etc/pam.d/other",
    53  						Digest: &file.Digest{
    54  							Algorithm: "md5",
    55  							Value:     "31aa7f2181889ffb00b87df4126d1701",
    56  						},
    57  						IsConfigFile: true,
    58  					},
    59  					{Path: "/lib/x86_64-linux-gnu/libz.so.1.2.11", Digest: &file.Digest{
    60  						Algorithm: "md5",
    61  						Value:     "55f905631797551d4d936a34c7e73474",
    62  					}},
    63  					{Path: "/usr/share/doc/zlib1g/changelog.Debian.gz", Digest: &file.Digest{
    64  						Algorithm: "md5",
    65  						Value:     "cede84bda30d2380217f97753c8ccf3a",
    66  					}},
    67  					{Path: "/usr/share/doc/zlib1g/changelog.gz", Digest: &file.Digest{
    68  						Algorithm: "md5",
    69  						Value:     "f3c9dafa6da7992c47328b4464f6d122",
    70  					}},
    71  					{Path: "/usr/share/doc/zlib1g/copyright", Digest: &file.Digest{
    72  						Algorithm: "md5",
    73  						Value:     "a4fae96070439a5209a62ae5b8017ab2",
    74  					}},
    75  				},
    76  			},
    77  		},
    78  	}
    79  
    80  	c := NewDpkgdbCataloger()
    81  
    82  	pkgtest.NewCatalogTester().
    83  		WithImageResolver(t, "image-dpkg").
    84  		IgnoreLocationLayer(). // this fixture can be rebuilt, thus the layer ID will change
    85  		Expects(expected, nil).
    86  		TestCataloger(t, c)
    87  }
    88  
    89  func TestCataloger_Globs(t *testing.T) {
    90  	tests := []struct {
    91  		name     string
    92  		fixture  string
    93  		expected []string
    94  	}{
    95  		{
    96  			name:    "obtain db status files",
    97  			fixture: "test-fixtures/glob-paths",
    98  			expected: []string{
    99  				"var/lib/dpkg/status",
   100  				"var/lib/dpkg/status.d/pkg-1.0",
   101  			},
   102  		},
   103  	}
   104  
   105  	for _, test := range tests {
   106  		t.Run(test.name, func(t *testing.T) {
   107  			pkgtest.NewCatalogTester().
   108  				FromDirectory(t, test.fixture).
   109  				ExpectsResolverContentQueries(test.expected).
   110  				TestCataloger(t, NewDpkgdbCataloger())
   111  		})
   112  	}
   113  }