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

     1  package arch
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp/cmpopts"
     7  
     8  	"github.com/anchore/syft/syft/artifact"
     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 TestAlpmCataloger(t *testing.T) {
    15  	gmpDbLocation := file.NewLocation("var/lib/pacman/local/gmp-6.2.1-2/desc")
    16  	treeSitterDbLocation := file.NewLocation("var/lib/pacman/local/tree-sitter-0.22.6-1/desc")
    17  	emacsDbLocation := file.NewLocation("var/lib/pacman/local/emacs-29.3-3/desc")
    18  	fuzzyDbLocation := file.NewLocation("var/lib/pacman/local/fuzzy-1.2-3/desc")
    19  	madeupDbLocation := file.NewLocation("var/lib/pacman/local/madeup-20.30-4/desc")
    20  
    21  	treeSitterPkg := pkg.Package{
    22  		Name:    "tree-sitter",
    23  		Version: "0.22.6-1",
    24  		Type:    pkg.AlpmPkg,
    25  		FoundBy: "alpm-db-cataloger",
    26  		Licenses: pkg.NewLicenseSet(
    27  			pkg.NewLicenseFromLocations("MIT", treeSitterDbLocation),
    28  		),
    29  		Locations: file.NewLocationSet(treeSitterDbLocation),
    30  		Metadata: pkg.AlpmDBEntry{
    31  			BasePackage:  "tree-sitter",
    32  			Package:      "tree-sitter",
    33  			Version:      "0.22.6-1",
    34  			Description:  "Incremental parsing library",
    35  			Architecture: "x86_64",
    36  			Size:         223539,
    37  			Packager:     "Daniel M. Capella <polyzen@archlinux.org>",
    38  			URL:          "https://github.com/tree-sitter/tree-sitter",
    39  			Validation:   "pgp",
    40  			Reason:       1,
    41  			Files:        []pkg.AlpmFileRecord{},
    42  			Backup:       []pkg.AlpmFileRecord{},
    43  			Provides:     []string{"libtree-sitter.so=0-64"},
    44  		},
    45  	}
    46  
    47  	emacsPkg := pkg.Package{
    48  		Name:    "emacs",
    49  		Version: "29.3-3",
    50  		Type:    pkg.AlpmPkg,
    51  		FoundBy: "alpm-db-cataloger",
    52  		Licenses: pkg.NewLicenseSet(
    53  			pkg.NewLicenseFromLocations("GPL3", emacsDbLocation),
    54  		),
    55  		Locations: file.NewLocationSet(emacsDbLocation),
    56  		Metadata: pkg.AlpmDBEntry{
    57  			BasePackage:  "emacs",
    58  			Package:      "emacs",
    59  			Version:      "29.3-3",
    60  			Description:  "The extensible, customizable, self-documenting real-time display editor",
    61  			Architecture: "x86_64",
    62  			Size:         126427862,
    63  			Packager:     "Frederik Schwan <freswa@archlinux.org>",
    64  			URL:          "https://www.gnu.org/software/emacs/emacs.html",
    65  			Validation:   "pgp",
    66  			Files:        []pkg.AlpmFileRecord{},
    67  			Backup:       []pkg.AlpmFileRecord{},
    68  			Depends:      []string{"libtree-sitter.so=0-64"},
    69  		},
    70  	}
    71  
    72  	fuzzyPkg := pkg.Package{
    73  		Name:    "fuzzy",
    74  		Version: "1.2-3",
    75  		Type:    pkg.AlpmPkg,
    76  		FoundBy: "alpm-db-cataloger",
    77  		Locations: file.NewLocationSet(
    78  			fuzzyDbLocation,
    79  			file.NewLocation("var/lib/pacman/local/fuzzy-1.2-3/files"),
    80  		),
    81  		Metadata: pkg.AlpmDBEntry{
    82  			Package: "fuzzy",
    83  			Version: "1.2-3",
    84  			Files:   []pkg.AlpmFileRecord{},
    85  			Backup: []pkg.AlpmFileRecord{
    86  				{
    87  					Path: "/etc/fuzzy.conf",
    88  					Digests: []file.Digest{
    89  						{Algorithm: "md5", Value: "79fce043df7dfc676ae5ecb903762d8b"},
    90  					},
    91  				},
    92  			},
    93  			Depends: []string{"tree-sitter"},
    94  		},
    95  	}
    96  
    97  	madeupPkg := pkg.Package{
    98  		Name:      "madeup",
    99  		Version:   "20.30-4",
   100  		Type:      pkg.AlpmPkg,
   101  		FoundBy:   "alpm-db-cataloger",
   102  		Locations: file.NewLocationSet(madeupDbLocation),
   103  		Metadata: pkg.AlpmDBEntry{
   104  			Package: "madeup",
   105  			Version: "20.30-4",
   106  			Files:   []pkg.AlpmFileRecord{},
   107  			Backup:  []pkg.AlpmFileRecord{},
   108  			Depends: []string{"libtree-sitter.so"},
   109  		},
   110  	}
   111  
   112  	gmpPkg := pkg.Package{
   113  		Name:    "gmp",
   114  		Version: "6.2.1-2",
   115  		Type:    pkg.AlpmPkg,
   116  		FoundBy: "alpm-db-cataloger",
   117  		Licenses: pkg.NewLicenseSet(
   118  			pkg.NewLicenseFromLocations("LGPL3", gmpDbLocation),
   119  			pkg.NewLicenseFromLocations("GPL", gmpDbLocation),
   120  		),
   121  		Locations: file.NewLocationSet(
   122  			gmpDbLocation,
   123  			file.NewLocation("var/lib/pacman/local/gmp-6.2.1-2/files"),
   124  			file.NewLocation("var/lib/pacman/local/gmp-6.2.1-2/mtree"),
   125  		),
   126  		Metadata: pkg.AlpmDBEntry{
   127  			BasePackage:  "gmp",
   128  			Package:      "gmp",
   129  			Version:      "6.2.1-2",
   130  			Description:  "A free library for arbitrary precision arithmetic",
   131  			Architecture: "x86_64",
   132  			Size:         1044438,
   133  			Packager:     "Antonio Rojas <arojas@archlinux.org>",
   134  			URL:          "https://gmplib.org/",
   135  			Validation:   "pgp",
   136  			Reason:       1,
   137  			Depends:      []string{"gcc-libs", "sh", "libtree-sitter.so=1-64"},
   138  			Files: []pkg.AlpmFileRecord{
   139  				{
   140  					Path:    "/usr",
   141  					Type:    "dir",
   142  					Digests: []file.Digest{},
   143  				},
   144  				{
   145  					Path:    "/usr/include",
   146  					Type:    "dir",
   147  					Digests: []file.Digest{},
   148  				},
   149  				{
   150  					Path: "/usr/include/gmp.h",
   151  					Size: "84140",
   152  					Digests: []file.Digest{
   153  						{Algorithm: "md5", Value: "76595f70565c72550eb520809bf86856"},
   154  						{Algorithm: "sha256", Value: "91a614b9202453153fe3b7512d15e89659108b93ce8841c8e13789eb85da9e3a"},
   155  					},
   156  				},
   157  				{
   158  					Path: "/usr/include/gmpxx.h",
   159  					Size: "129113",
   160  					Digests: []file.Digest{
   161  						{Algorithm: "md5", Value: "ea3d21de4bcf7c696799c5c55dd3655b"},
   162  						{Algorithm: "sha256", Value: "0011ae411a0bc1030e07d968b32fdc1343f5ac2a17b7d28f493e7976dde2ac82"},
   163  					},
   164  				},
   165  				{
   166  					Path:    "/usr/lib",
   167  					Type:    "dir",
   168  					Digests: []file.Digest{},
   169  				},
   170  				{
   171  					Path:    "/usr/lib/libgmp.so",
   172  					Type:    "link",
   173  					Link:    "libgmp.so.10.4.1",
   174  					Digests: []file.Digest{},
   175  				},
   176  				{
   177  					Path:    "/usr/lib/libgmp.so.10",
   178  					Type:    "link",
   179  					Link:    "libgmp.so.10.4.1",
   180  					Digests: []file.Digest{},
   181  				},
   182  				{
   183  					Path: "/usr/lib/libgmp.so.10.4.1",
   184  					Size: "663224",
   185  					Digests: []file.Digest{
   186  						{Algorithm: "md5", Value: "d6d03eadacdd9048d5b2adf577e9d722"},
   187  						{Algorithm: "sha256", Value: "39898bd3d8d6785222432fa8b8aef7ce3b7e5bbfc66a52b7c0da09bed4adbe6a"},
   188  					},
   189  				},
   190  				{
   191  					Path:    "/usr/lib/libgmpxx.so",
   192  					Type:    "link",
   193  					Link:    "libgmpxx.so.4.6.1",
   194  					Digests: []file.Digest{},
   195  				},
   196  				{
   197  					Path:    "/usr/lib/libgmpxx.so.4",
   198  					Type:    "link",
   199  					Link:    "libgmpxx.so.4.6.1",
   200  					Digests: []file.Digest{},
   201  				},
   202  				{
   203  					Path: "/usr/lib/libgmpxx.so.4.6.1",
   204  					Size: "30680",
   205  					Digests: []file.Digest{
   206  						{Algorithm: "md5", Value: "dd5f0c4d635fa599fa7f4339c0e8814d"},
   207  						{Algorithm: "sha256", Value: "0ef67cbde4841f58d2e4b41f59425eb87c9eeaf4e649c060b326342c53bedbec"},
   208  					},
   209  				},
   210  				{
   211  					Path:    "/usr/lib/pkgconfig",
   212  					Type:    "dir",
   213  					Digests: []file.Digest{},
   214  				},
   215  				{
   216  					Path: "/usr/lib/pkgconfig/gmp.pc",
   217  					Size: "245",
   218  					Digests: []file.Digest{
   219  						{Algorithm: "md5", Value: "a91a9f1b66218cb77b9cd2cdf341756d"},
   220  						{Algorithm: "sha256", Value: "4e9de547a48c4e443781e9fa702a1ec5a23ee28b4bc520306cff2541a855be37"},
   221  					},
   222  				},
   223  				{
   224  					Path: "/usr/lib/pkgconfig/gmpxx.pc",
   225  					Size: "280",
   226  					Digests: []file.Digest{
   227  						{Algorithm: "md5", Value: "8c0f54e987934352177a6a30a811b001"},
   228  						{Algorithm: "sha256", Value: "fc5dbfbe75977057ba50953d94b9daecf696c9fdfe5b94692b832b44ecca871b"},
   229  					},
   230  				},
   231  				{
   232  					Path:    "/usr/share",
   233  					Type:    "dir",
   234  					Digests: []file.Digest{},
   235  				},
   236  				{
   237  					Path:    "/usr/share/info",
   238  					Type:    "dir",
   239  					Digests: []file.Digest{},
   240  				},
   241  				{
   242  					Path: "/usr/share/info/gmp.info-1.gz",
   243  					Size: "85892",
   244  					Digests: []file.Digest{
   245  						{Algorithm: "md5", Value: "63304d4d2f0247fb8a999fae66a81c19"},
   246  						{Algorithm: "sha256", Value: "86288c1531a2789db5da8b9838b5cde4db07bda230ae11eba23a1f33698bd14e"},
   247  					},
   248  				},
   249  				{
   250  					Path: "/usr/share/info/gmp.info-2.gz",
   251  					Size: "48484",
   252  					Digests: []file.Digest{
   253  						{Algorithm: "md5", Value: "4bb0dadec416d305232cac6eae712ff7"},
   254  						{Algorithm: "sha256", Value: "b7443c1b529588d98a074266087f79b595657ac7274191c34b10a9ceedfa950e"},
   255  					},
   256  				},
   257  				{
   258  					Path: "/usr/share/info/gmp.info.gz",
   259  					Size: "2380",
   260  					Digests: []file.Digest{
   261  						{Algorithm: "md5", Value: "cf6880fb0d862ee1da0d13c3831b5720"},
   262  						{Algorithm: "sha256", Value: "a13c8eecda3f3e5ad1e09773e47a9686f07d9d494eaddf326f3696bbef1548fd"},
   263  					},
   264  				},
   265  			},
   266  			Backup: []pkg.AlpmFileRecord{},
   267  		},
   268  	}
   269  
   270  	expectedPkgs := []pkg.Package{
   271  		treeSitterPkg,
   272  		emacsPkg,
   273  		fuzzyPkg,
   274  		madeupPkg,
   275  		gmpPkg,
   276  	}
   277  
   278  	expectedRelationships := []artifact.Relationship{
   279  		{ // exact spec lookup
   280  			From: treeSitterPkg,
   281  			To:   emacsPkg,
   282  			Type: artifact.DependencyOfRelationship,
   283  		},
   284  		{ // package name lookup
   285  			From: treeSitterPkg,
   286  			To:   fuzzyPkg,
   287  			Type: artifact.DependencyOfRelationship,
   288  		},
   289  		{ // library name lookup
   290  			From: treeSitterPkg,
   291  			To:   madeupPkg,
   292  			Type: artifact.DependencyOfRelationship,
   293  		},
   294  	}
   295  
   296  	pkgtest.NewCatalogTester().
   297  		FromDirectory(t, "test-fixtures/installed").
   298  		WithCompareOptions(cmpopts.IgnoreFields(pkg.AlpmFileRecord{}, "Time")).
   299  		Expects(expectedPkgs, expectedRelationships).
   300  		TestCataloger(t, NewDBCataloger())
   301  
   302  }
   303  
   304  func TestCataloger_Globs(t *testing.T) {
   305  	tests := []struct {
   306  		name     string
   307  		fixture  string
   308  		expected []string
   309  	}{
   310  		{
   311  			name:    "obtain description files",
   312  			fixture: "test-fixtures/glob-paths",
   313  			expected: []string{
   314  				"var/lib/pacman/local/base-1.0/desc",
   315  				"var/lib/pacman/local/dive-0.10.0/desc",
   316  			},
   317  		},
   318  	}
   319  
   320  	for _, test := range tests {
   321  		t.Run(test.name, func(t *testing.T) {
   322  			pkgtest.NewCatalogTester().
   323  				FromDirectory(t, test.fixture).
   324  				ExpectsResolverContentQueries(test.expected).
   325  				IgnoreUnfulfilledPathResponses("var/lib/pacman/local/base-1.0/mtree", "var/lib/pacman/local/dive-0.10.0/mtree").
   326  				TestCataloger(t, NewDBCataloger())
   327  		})
   328  	}
   329  }