-
Keith Zantow committed with GitHub 1 year ago1 parent 8f6a317f
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
-
-
-
-
-
-
-
skipped 1 lines 2 2 3 3 import ( 4 4 "errors" 5 + "fmt" 5 6 "io" 6 7 "testing" 7 8 skipped 19 lines 27 28 Version: "15beta4", 28 29 Type: "binary", 29 30 PURL: "pkg:generic/postgresql@15beta4", 30 - Locations: singleLocation("postgres"), 31 - Metadata: pkg.BinaryMetadata{ 32 - Classifier: "postgresql-binary", 33 - }, 31 + Locations: locations("postgres"), 32 + Metadata: metadata("postgresql-binary"), 34 33 }, 35 34 }, 36 35 { skipped 4 lines 41 40 Version: "15.1", 42 41 Type: "binary", 43 42 PURL: "pkg:generic/[email protected]", 44 - Locations: singleLocation("postgres"), 45 - Metadata: pkg.BinaryMetadata{ 46 - Classifier: "postgresql-binary", 47 - }, 43 + Locations: locations("postgres"), 44 + Metadata: metadata("postgresql-binary"), 48 45 }, 49 46 }, 50 47 { skipped 4 lines 55 52 Version: "9.6.24", 56 53 Type: "binary", 57 54 PURL: "pkg:generic/[email protected]", 58 - Locations: singleLocation("postgres"), 59 - Metadata: pkg.BinaryMetadata{ 60 - Classifier: "postgresql-binary", 61 - }, 55 + Locations: locations("postgres"), 56 + Metadata: metadata("postgresql-binary"), 62 57 }, 63 58 }, 64 59 { skipped 4 lines 69 64 Version: "9.5alpha1", 70 65 Type: "binary", 71 66 PURL: "pkg:generic/[email protected]", 72 - Locations: singleLocation("postgres"), 67 + Locations: locations("postgres"), 68 + Metadata: metadata("postgresql-binary"), 69 + }, 70 + }, 71 + { 72 + name: "positive-python-duplicates", 73 + fixtureDir: "test-fixtures/classifiers/positive/python-duplicates", 74 + expected: pkg.Package{ 75 + Name: "python", 76 + Version: "3.8.16", 77 + Type: "binary", 78 + PURL: "pkg:generic/[email protected]", 79 + Locations: locations("dir/python3.8", "python3.8", "libpython3.8.so", "patchlevel.h"), 73 80 Metadata: pkg.BinaryMetadata{ 74 - Classifier: "postgresql-binary", 81 + Matches: []pkg.ClassifierMatch{ 82 + match("python-binary", "dir/python3.8"), 83 + match("python-binary", "python3.8"), 84 + match("python-binary-lib", "libpython3.8.so"), 85 + match("cpython-source", "patchlevel.h"), 86 + }, 75 87 }, 76 88 }, 77 89 }, skipped 5 lines 83 95 Version: "2.9.6", 84 96 Type: "binary", 85 97 PURL: "pkg:generic/[email protected]", 86 - Locations: singleLocation("traefik"), 87 - Metadata: pkg.BinaryMetadata{ 88 - Classifier: "traefik-binary", 89 - }, 98 + Locations: locations("traefik"), 99 + Metadata: metadata("traefik-binary"), 90 100 }, 91 101 }, 92 102 { skipped 4 lines 97 107 Version: "1.7.34", 98 108 Type: "binary", 99 109 PURL: "pkg:generic/[email protected]", 100 - Locations: singleLocation("traefik"), 101 - Metadata: pkg.BinaryMetadata{ 102 - Classifier: "traefik-binary", 103 - }, 110 + Locations: locations("traefik"), 111 + Metadata: metadata("traefik-binary"), 104 112 }, 105 113 }, 106 114 { skipped 4 lines 111 119 Version: "1.6.18", 112 120 Type: "binary", 113 121 PURL: "pkg:generic/[email protected]", 114 - Locations: singleLocation("memcached"), 115 - Metadata: pkg.BinaryMetadata{ 116 - Classifier: "memcached-binary", 117 - }, 122 + Locations: locations("memcached"), 123 + Metadata: metadata("memcached-binary"), 118 124 }, 119 125 }, 120 126 { skipped 4 lines 125 131 Version: "2.4.54", 126 132 Type: "binary", 127 133 PURL: "pkg:generic/[email protected]", 128 - Locations: singleLocation("httpd"), 129 - Metadata: pkg.BinaryMetadata{ 130 - Classifier: "httpd-binary", 131 - }, 134 + Locations: locations("httpd"), 135 + Metadata: metadata("httpd-binary"), 132 136 }, 133 137 }, 134 138 { skipped 4 lines 139 143 Version: "8.2.1", 140 144 Type: "binary", 141 145 PURL: "pkg:generic/[email protected]", 142 - Locations: singleLocation("php"), 143 - Metadata: pkg.BinaryMetadata{ 144 - Classifier: "php-cli-binary", 145 - }, 146 + Locations: locations("php"), 147 + Metadata: metadata("php-cli-binary"), 146 148 }, 147 149 }, 148 150 { skipped 4 lines 153 155 Version: "8.2.1", 154 156 Type: "binary", 155 157 PURL: "pkg:generic/[email protected]", 156 - Locations: singleLocation("php-fpm"), 157 - Metadata: pkg.BinaryMetadata{ 158 - Classifier: "php-fpm-binary", 159 - }, 158 + Locations: locations("php-fpm"), 159 + Metadata: metadata("php-fpm-binary"), 160 160 }, 161 161 }, 162 162 { skipped 4 lines 167 167 Version: "8.2.1", 168 168 Type: "binary", 169 169 PURL: "pkg:generic/[email protected]", 170 - Locations: singleLocation("libphp.so"), 171 - Metadata: pkg.BinaryMetadata{ 172 - Classifier: "php-apache-binary", 173 - }, 170 + Locations: locations("libphp.so"), 171 + Metadata: metadata("php-apache-binary"), 174 172 }, 175 173 }, 176 174 { skipped 46 lines 223 221 Version: "2.8.23", 224 222 Type: "binary", 225 223 PURL: "pkg:generic/[email protected]", 226 - Locations: singleLocation("redis-server"), 227 - Metadata: pkg.BinaryMetadata{ 228 - Classifier: "redis-binary", 229 - }, 224 + Locations: locations("redis-server"), 225 + Metadata: metadata("redis-binary"), 230 226 }, 231 227 }, 232 228 { skipped 4 lines 237 233 Version: "4.0.11", 238 234 Type: "binary", 239 235 PURL: "pkg:generic/[email protected]", 240 - Locations: singleLocation("redis-server"), 241 - Metadata: pkg.BinaryMetadata{ 242 - Classifier: "redis-binary", 243 - }, 236 + Locations: locations("redis-server"), 237 + Metadata: metadata("redis-binary"), 244 238 }, 245 239 }, 246 240 { skipped 4 lines 251 245 Version: "5.0.0", 252 246 Type: "binary", 253 247 PURL: "pkg:generic/[email protected]", 254 - Locations: singleLocation("redis-server"), 255 - Metadata: pkg.BinaryMetadata{ 256 - Classifier: "redis-binary", 257 - }, 248 + Locations: locations("redis-server"), 249 + Metadata: metadata("redis-binary"), 258 250 }, 259 251 }, 260 252 { skipped 4 lines 265 257 Version: "6.0.16", 266 258 Type: "binary", 267 259 PURL: "pkg:generic/[email protected]", 268 - Locations: singleLocation("redis-server"), 269 - Metadata: pkg.BinaryMetadata{ 270 - Classifier: "redis-binary", 271 - }, 260 + Locations: locations("redis-server"), 261 + Metadata: metadata("redis-binary"), 272 262 }, 273 263 }, 274 264 { skipped 4 lines 279 269 Version: "7.0.0", 280 270 Type: "binary", 281 271 PURL: "pkg:generic/[email protected]", 282 - Locations: singleLocation("redis-server"), 283 - Metadata: pkg.BinaryMetadata{ 284 - Classifier: "redis-binary", 285 - }, 272 + Locations: locations("redis-server"), 273 + Metadata: metadata("redis-binary"), 286 274 }, 287 275 }, 288 276 { 289 277 name: "positive-libpython3.7.so", 290 - fixtureDir: "test-fixtures/classifiers/positive", 278 + fixtureDir: "test-fixtures/classifiers/positive/python-binary-lib-3.7", 291 279 expected: pkg.Package{ 292 280 Name: "python", 293 281 Version: "3.7.4a-vZ9", 294 282 PURL: "pkg:generic/[email protected]", 295 - Locations: singleLocation("libpython3.7.so"), 296 - Metadata: pkg.BinaryMetadata{ 297 - Classifier: "python-binary-lib", 298 - }, 283 + Locations: locations("libpython3.7.so"), 284 + Metadata: metadata("python-binary-lib"), 299 285 }, 300 286 }, 301 287 { 302 288 name: "positive-python3.6", 303 - fixtureDir: "test-fixtures/classifiers/positive", 289 + fixtureDir: "test-fixtures/classifiers/positive/python-binary-3.6", 304 290 expected: pkg.Package{ 305 291 Name: "python", 306 292 Version: "3.6.3a-vZ9", 307 293 PURL: "pkg:generic/[email protected]", 308 - Locations: singleLocation("python3.6"), 309 - Metadata: pkg.BinaryMetadata{ 310 - Classifier: "python-binary", 311 - }, 294 + Locations: locations("python3.6"), 295 + Metadata: metadata("python-binary"), 312 296 }, 313 297 }, 314 298 { 315 299 name: "positive-patchlevel.h", 316 - fixtureDir: "test-fixtures/classifiers/positive", 300 + fixtureDir: "test-fixtures/classifiers/positive/python-source-3.9", 317 301 expected: pkg.Package{ 318 302 Name: "python", 319 303 Version: "3.9-aZ5", 320 304 PURL: "pkg:generic/[email protected]", 321 - Locations: singleLocation("patchlevel.h"), 322 - Metadata: pkg.BinaryMetadata{ 323 - Classifier: "cpython-source", 324 - }, 305 + Locations: locations("patchlevel.h"), 306 + Metadata: metadata("cpython-source"), 325 307 }, 326 308 }, 327 309 { 328 310 name: "positive-go", 329 - fixtureDir: "test-fixtures/classifiers/positive", 311 + fixtureDir: "test-fixtures/classifiers/positive/go-1.14", 330 312 expected: pkg.Package{ 331 313 Name: "go", 332 314 Version: "1.14", 333 315 PURL: "pkg:generic/[email protected]", 334 - Locations: singleLocation("go"), 335 - Metadata: pkg.BinaryMetadata{ 336 - Classifier: "go-binary", 337 - }, 316 + Locations: locations("go"), 317 + Metadata: metadata("go-binary"), 338 318 }, 339 319 }, 340 320 { 341 321 name: "positive-node", 342 - fixtureDir: "test-fixtures/classifiers/positive", 322 + fixtureDir: "test-fixtures/classifiers/positive/node-19.2.1", 343 323 expected: pkg.Package{ 344 324 Name: "node", 345 325 Version: "19.2.1", 346 326 PURL: "pkg:generic/[email protected]", 347 - Locations: singleLocation("node"), 348 - Metadata: pkg.BinaryMetadata{ 349 - Classifier: "nodejs-binary", 350 - }, 327 + Locations: locations("node"), 328 + Metadata: metadata("nodejs-binary"), 351 329 }, 352 330 }, 353 331 { 354 332 name: "positive-go-hint", 355 - fixtureDir: "test-fixtures/classifiers/positive", 333 + fixtureDir: "test-fixtures/classifiers/positive/go-hint-1.15", 356 334 expected: pkg.Package{ 357 335 Name: "go", 358 336 Version: "1.15", 359 337 PURL: "pkg:generic/[email protected]", 360 - Locations: singleLocation("VERSION"), 361 - Metadata: pkg.BinaryMetadata{ 362 - Classifier: "go-binary-hint", 363 - }, 338 + Locations: locations("VERSION"), 339 + Metadata: metadata("go-binary-hint"), 364 340 }, 365 341 }, 366 342 { 367 343 name: "positive-busybox", 368 - fixtureDir: "test-fixtures/classifiers/positive", 344 + fixtureDir: "test-fixtures/classifiers/positive/busybox-3.33.3", 369 345 expected: pkg.Package{ 370 346 Name: "busybox", 371 347 Version: "3.33.3", 372 - Locations: singleLocation("["), // note: busybox is a link to [ 373 - Metadata: pkg.BinaryMetadata{ 374 - Classifier: "busybox-binary", 375 - VirtualPath: "busybox", 376 - }, 348 + Locations: locations("["), // note: busybox is a link to [ 349 + Metadata: metadata("busybox-binary", "[", "busybox"), 377 350 }, 378 351 }, 379 352 { skipped 4 lines 384 357 Version: "1.8.0_352-b08", 385 358 Type: "binary", 386 359 PURL: "pkg:generic/[email protected]_352-b08", 387 - Locations: singleLocation("java"), 388 - Metadata: pkg.BinaryMetadata{ 389 - Classifier: "java-binary-openjdk", 390 - VirtualPath: "java", 391 - }, 360 + Locations: locations("java"), 361 + Metadata: metadata("java-binary-openjdk", "java"), 392 362 }, 393 363 }, 394 364 { skipped 4 lines 399 369 Version: "11.0.17+8-LTS", 400 370 Type: "binary", 401 371 PURL: "pkg:generic/[email protected]+8-LTS", 402 - Locations: singleLocation("java"), 403 - Metadata: pkg.BinaryMetadata{ 404 - Classifier: "java-binary-openjdk", 405 - VirtualPath: "java", 406 - }, 372 + Locations: locations("java"), 373 + Metadata: metadata("java-binary-openjdk", "java"), 407 374 }, 408 375 }, 409 376 { skipped 4 lines 414 381 Version: "19.0.1+10-21", 415 382 Type: "binary", 416 383 PURL: "pkg:generic/[email protected]+10-21", 417 - Locations: singleLocation("java"), 418 - Metadata: pkg.BinaryMetadata{ 419 - Classifier: "java-binary-oracle", 420 - VirtualPath: "java", 421 - }, 384 + Locations: locations("java"), 385 + Metadata: metadata("java-binary-oracle", "java"), 422 386 }, 423 387 }, 424 388 { skipped 4 lines 429 393 Version: "19.0.1+10-21", 430 394 Type: "binary", 431 395 PURL: "pkg:generic/[email protected]+10-21", 432 - Locations: singleLocation("java"), 433 - Metadata: pkg.BinaryMetadata{ 434 - Classifier: "java-binary-oracle", 435 - VirtualPath: "java", 436 - }, 396 + Locations: locations("java"), 397 + Metadata: metadata("java-binary-oracle", "java"), 437 398 }, 438 399 }, 439 400 { skipped 4 lines 444 405 Version: "1.8.0-foreman_2022_09_22_15_30-b00", 445 406 Type: "binary", 446 407 PURL: "pkg:generic/[email protected]_2022_09_22_15_30-b00", 447 - Locations: singleLocation("java"), 448 - Metadata: pkg.BinaryMetadata{ 449 - Classifier: "java-binary-ibm", 450 - VirtualPath: "java", 451 - }, 408 + Locations: locations("java"), 409 + Metadata: metadata("java-binary-ibm", "java"), 452 410 }, 453 411 }, 454 412 } skipped 11 lines 466 424 packages, _, err := c.Catalog(resolver) 467 425 require.NoError(t, err) 468 426 469 - ok := false 470 427 for _, p := range packages { 471 - if test.expected.Locations.ToSlice()[0].RealPath == p.Locations.ToSlice()[0].RealPath { 472 - ok = true 473 - assertPackagesAreEqual(t, test.expected, p) 428 + expectedLocations := test.expected.Locations.ToSlice() 429 + gotLocations := p.Locations.ToSlice() 430 + require.Len(t, gotLocations, len(expectedLocations)) 431 + 432 + for i, expectedLocation := range expectedLocations { 433 + gotLocation := gotLocations[i] 434 + if expectedLocation.RealPath != gotLocation.RealPath { 435 + t.Fatalf("locations do not match; expected: %v got: %v", expectedLocations, gotLocations) 436 + } 474 437 } 475 - } 476 438 477 - if !ok { 478 - t.Fatalf("could not find test location=%q", test.expected.Locations.ToSlice()[0].RealPath) 439 + assertPackagesAreEqual(t, test.expected, p) 479 440 } 480 - 481 441 }) 482 442 } 483 443 } skipped 10 lines 494 454 expected: pkg.Package{ 495 455 Name: "busybox", 496 456 Version: "1.35.0", 497 - Locations: singleLocation("/bin/["), 498 - Metadata: pkg.BinaryMetadata{ 499 - Classifier: "busybox-binary", 500 - VirtualPath: "/bin/busybox", 501 - }, 457 + Locations: locations("/bin/["), 458 + Metadata: metadata("busybox-binary", "/bin/[", "/bin/busybox"), 502 459 }, 503 460 }, 504 461 } skipped 12 lines 517 474 packages, _, err := c.Catalog(resolver) 518 475 require.NoError(t, err) 519 476 520 - ok := false 521 477 for _, p := range packages { 522 - if test.expected.Locations.ToSlice()[0].RealPath == p.Locations.ToSlice()[0].RealPath { 523 - ok = true 524 - assertPackagesAreEqual(t, test.expected, p) 478 + expectedLocations := test.expected.Locations.ToSlice() 479 + gotLocations := p.Locations.ToSlice() 480 + require.Len(t, gotLocations, len(expectedLocations)) 481 + 482 + for i, expectedLocation := range expectedLocations { 483 + gotLocation := gotLocations[i] 484 + if expectedLocation.RealPath != gotLocation.RealPath { 485 + t.Fatalf("locations do not match; expected: %v got: %v", expectedLocations, gotLocations) 486 + } 525 487 } 526 - } 527 488 528 - if !ok { 529 - t.Fatalf("could not find test location=%q", test.expected.Locations.ToSlice()[0].RealPath) 489 + assertPackagesAreEqual(t, test.expected, p) 530 490 } 531 - 532 491 }) 533 492 } 534 493 } skipped 12 lines 547 506 assert.Equal(t, 0, len(actualResults)) 548 507 } 549 508 550 - func singleLocation(s string) source.LocationSet { 551 - return source.NewLocationSet(source.NewLocation(s)) 509 + func locations(locations ...string) source.LocationSet { 510 + var locs []source.Location 511 + for _, s := range locations { 512 + locs = append(locs, source.NewLocation(s)) 513 + } 514 + return source.NewLocationSet(locs...) 515 + } 516 + 517 + // metadata paths are: realPath, virtualPath 518 + func metadata(classifier string, paths ...string) pkg.BinaryMetadata { 519 + return pkg.BinaryMetadata{ 520 + Matches: []pkg.ClassifierMatch{ 521 + match(classifier, paths...), 522 + }, 523 + } 524 + } 525 + 526 + // match paths are: realPath, virtualPath 527 + func match(classifier string, paths ...string) pkg.ClassifierMatch { 528 + realPath := "" 529 + if len(paths) > 0 { 530 + realPath = paths[0] 531 + } 532 + virtualPath := "" 533 + if len(paths) > 1 { 534 + virtualPath = paths[1] 535 + } 536 + return pkg.ClassifierMatch{ 537 + Classifier: classifier, 538 + Location: source.Location{ 539 + Coordinates: source.Coordinates{ 540 + RealPath: realPath, 541 + }, 542 + VirtualPath: virtualPath, 543 + }, 544 + } 552 545 } 553 546 554 547 func assertPackagesAreEqual(t *testing.T, expected pkg.Package, p pkg.Package) { 555 - meta1 := expected.Metadata.(pkg.BinaryMetadata) 556 - meta2 := p.Metadata.(pkg.BinaryMetadata) 548 + m1 := expected.Metadata.(pkg.BinaryMetadata).Matches 549 + m2 := p.Metadata.(pkg.BinaryMetadata).Matches 550 + matches := true 551 + if len(m1) == len(m2) { 552 + for i, m1 := range m1 { 553 + m2 := m2[i] 554 + if m1.Classifier != m2.Classifier { 555 + matches = false 556 + break 557 + } 558 + if m1.Location.RealPath != "" && m1.Location.RealPath != m2.Location.RealPath { 559 + matches = false 560 + break 561 + } 562 + if m1.Location.VirtualPath != "" && m1.Location.VirtualPath != m2.Location.VirtualPath { 563 + matches = false 564 + break 565 + } 566 + } 567 + } else { 568 + matches = false 569 + } 557 570 if expected.Name != p.Name || 558 571 expected.Version != p.Version || 559 572 expected.PURL != p.PURL || 560 - meta1.Classifier != meta2.Classifier { 561 - assert.Failf(t, "packages not equal", "%v != %v", expected, p) 573 + !matches { 574 + assert.Failf(t, "packages not equal", "%v != %v", stringifyPkg(expected), stringifyPkg(p)) 562 575 } 576 + } 577 + 578 + func stringifyPkg(p pkg.Package) string { 579 + matches := p.Metadata.(pkg.BinaryMetadata).Matches 580 + return fmt.Sprintf("(name=%s, version=%s, purl=%s, matches=%+v)", p.Name, p.Version, p.PURL, matches) 563 581 } 564 582 565 583 type panicyResolver struct { skipped 20 lines 586 604 return nil, errors.New("not implemented") 587 605 } 588 606 589 - func (p *panicyResolver) HasPath(s string) bool { 607 + func (p *panicyResolver) HasPath(_ string) bool { 590 608 return true 591 609 } 592 610 skipped 36 lines -
-
syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/[ syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/busybox-3.33.3/[Content is identical
-
syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/busybox syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/busybox-3.33.3/busyboxContent is identical
-
syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/go syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/go-1.14/goBinary file.
-
syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/VERSION syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/go-hint-1.15/VERSIONContent is identical
-
syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/node syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/node-19.2.1/nodeContent is identical
-
syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/python3.6 syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/python-binary-3.6/python3.6Content is identical
-
syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/libpython3.7.so syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/python-binary-lib-3.7/libpython3.7.soContent is identical
-
syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/patchlevel.h syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/python-source-3.9/patchlevel.hContent is identical
-