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