diff --git a/syft/format/common/cyclonedxhelpers/to_format_model_test.go b/syft/format/common/cyclonedxhelpers/to_format_model_test.go index c3ac1f3b8f7d..d61a9930302b 100644 --- a/syft/format/common/cyclonedxhelpers/to_format_model_test.go +++ b/syft/format/common/cyclonedxhelpers/to_format_model_test.go @@ -2,6 +2,7 @@ package cyclonedxhelpers import ( "fmt" + "github.com/anchore/syft/syft/file" "testing" "github.com/CycloneDX/cyclonedx-go" @@ -143,6 +144,52 @@ func Test_relationships(t *testing.T) { } } +func Test_fileComponents(t *testing.T) { + tests := []struct { + name string + sbom sbom.SBOM + want []cyclonedx.Component + }{ + { + name: "sbom coordinates with file metadata are serialized to cdx", + sbom: sbom.SBOM{ + Artifacts: sbom.Artifacts{ + FileMetadata: map[file.Coordinates]file.Metadata{ + {RealPath: "/test"}: file.Metadata{Path: "/test"}, + }, + FileDigests: map[file.Coordinates][]file.Digest{ + {RealPath: "/test"}: []file.Digest{ + { + Algorithm: "sha256", + Value: "xyz12345", + }, + }, + }, + }, + }, + want: []cyclonedx.Component{ + { + BOMRef: "3f31cb2d98be6c1e", + Name: "/test", + Type: cyclonedx.ComponentTypeFile, + Hashes: &[]cyclonedx.Hash{ + cyclonedx.Hash{Algorithm: "SHA-256", Value: "xyz12345"}, + }, + }, + }, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + cdx := ToFormatModel(test.sbom) + got := *cdx.Components + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("cdx file components mismatch (-want +got):\n%s", diff) + } + }) + } +} + func Test_toBomDescriptor(t *testing.T) { type args struct { name string