github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/test/integration/package_ownership_relationship_test.go (about) 1 package integration 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "testing" 7 8 "github.com/anchore/syft/syft/formats/syftjson" 9 syftjsonModel "github.com/anchore/syft/syft/formats/syftjson/model" 10 "github.com/anchore/syft/syft/pkg" 11 "github.com/anchore/syft/syft/source" 12 ) 13 14 func TestPackageOwnershipRelationships(t *testing.T) { 15 // ensure that the json encoder is applying artifact ownership with an image that has expected ownership relationships 16 tests := []struct { 17 fixture string 18 }{ 19 { 20 fixture: "image-owning-package", 21 }, 22 } 23 24 for _, test := range tests { 25 t.Run(test.fixture, func(t *testing.T) { 26 sbom, _ := catalogFixtureImage(t, test.fixture, source.SquashedScope, nil) 27 28 output := bytes.NewBufferString("") 29 err := syftjson.Format().Encode(output, sbom) 30 if err != nil { 31 t.Fatalf("unable to present: %+v", err) 32 } 33 34 var doc syftjsonModel.Document 35 decoder := json.NewDecoder(output) 36 if err := decoder.Decode(&doc); err != nil { 37 t.Fatalf("unable to decode json doc: %+v", err) 38 } 39 40 if len(doc.ArtifactRelationships) == 0 { 41 t.Errorf("expected to find relationships between packages but found none") 42 } 43 44 }) 45 } 46 47 } 48 49 func TestPackageOwnershipExclusions(t *testing.T) { 50 // ensure that the json encoder is excluding packages by artifact ownership with an image that has expected ownership relationships 51 tests := []struct { 52 name string 53 fixture string 54 }{ 55 { 56 name: "busybox binary is filtered based on ownership relationship", 57 fixture: "image-os-binary-overlap", 58 }, 59 } 60 61 for _, test := range tests { 62 t.Run(test.fixture, func(t *testing.T) { 63 sbom, _ := catalogFixtureImage(t, test.fixture, source.SquashedScope, nil) 64 binaryPackages := make([]pkg.Package, 0) 65 apkPackages := make([]pkg.Package, 0) 66 for p := range sbom.Artifacts.Packages.Enumerate() { 67 if p.Type == pkg.BinaryPkg && p.Name == "busybox" { 68 binaryPackages = append(binaryPackages, p) 69 } 70 if p.Type == pkg.ApkPkg && p.Name == "busybox" { 71 apkPackages = append(apkPackages, p) 72 } 73 } 74 75 if len(binaryPackages) != 0 { 76 packageNames := make([]string, 0) 77 for _, p := range binaryPackages { 78 packageNames = append(packageNames, p.Name) 79 } 80 t.Errorf("expected to find no binary packages but found %d packages: %v", len(binaryPackages), packageNames) 81 } 82 if len(apkPackages) == 0 { 83 t.Errorf("expected to find apk packages but found none") 84 } 85 }) 86 } 87 }