Skip to content

Commit

Permalink
test: add file component test to format model
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Phillips <[email protected]>
  • Loading branch information
spiffcs committed Dec 18, 2024
1 parent 41ad8ed commit 3504fdf
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions syft/format/common/cyclonedxhelpers/to_format_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cyclonedxhelpers

import (
"fmt"
"github.com/anchore/syft/syft/file"
"testing"

"github.com/CycloneDX/cyclonedx-go"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3504fdf

Please sign in to comment.