mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 23:16:30 +01:00
* split up sbom.Format into encode and decode ops Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update cmd pkg to inject format configs Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * bump cyclonedx schema to 1.5 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * redact image metadata from github encoder tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add more testing around format decoder identify Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add test case for format version options Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix cli tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix CLI test Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * [wip] - review comments Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * keep encoder creation out of post load function Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * keep decider and identify functions Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add a few more doc comments Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * remove format encoder default function helpers Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * address PR feedback Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * move back to streaming based decode functions Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * with common convention for encoder constructors Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix tests and allow for encoders to be created from cli options Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix cli tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * buffer reads from stdin to support seeking Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
119 lines
2.7 KiB
Go
119 lines
2.7 KiB
Go
package cyclonedxxml
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/anchore/syft/syft/sbom"
|
|
)
|
|
|
|
func TestDecoder_Decode(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
file string
|
|
err bool
|
|
distro string
|
|
packages []string
|
|
}{
|
|
{
|
|
name: "dir-scan",
|
|
file: "snapshot/TestCycloneDxDirectoryEncoder.golden",
|
|
distro: "debian:1.2.3",
|
|
packages: []string{"package-1:1.0.1", "package-2:2.0.1"},
|
|
},
|
|
{
|
|
name: "image-scan",
|
|
file: "snapshot/TestCycloneDxImageEncoder.golden",
|
|
distro: "debian:1.2.3",
|
|
packages: []string{"package-1:1.0.1", "package-2:2.0.1"},
|
|
},
|
|
{
|
|
name: "not-an-sbom",
|
|
file: "bad-sbom",
|
|
err: true,
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.file, func(t *testing.T) {
|
|
reader, err := os.Open(filepath.Join("test-fixtures", test.file))
|
|
require.NoError(t, err)
|
|
|
|
dec := NewFormatDecoder()
|
|
|
|
formatID, formatVersion := dec.Identify(reader)
|
|
if test.err {
|
|
assert.Equal(t, sbom.FormatID(""), formatID)
|
|
assert.Equal(t, "", formatVersion)
|
|
|
|
_, decodeID, decodeVersion, err := dec.Decode(reader)
|
|
require.Error(t, err)
|
|
assert.Equal(t, sbom.FormatID(""), decodeID)
|
|
assert.Equal(t, "", decodeVersion)
|
|
|
|
return
|
|
}
|
|
assert.Equal(t, ID, formatID)
|
|
assert.NotEmpty(t, formatVersion)
|
|
|
|
bom, decodeID, decodeVersion, err := dec.Decode(reader)
|
|
require.NotNil(t, bom)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, ID, decodeID)
|
|
assert.Equal(t, formatVersion, decodeVersion)
|
|
|
|
split := strings.SplitN(test.distro, ":", 2)
|
|
distroName := split[0]
|
|
distroVersion := split[1]
|
|
assert.Equal(t, bom.Artifacts.LinuxDistribution.ID, distroName)
|
|
assert.Equal(t, bom.Artifacts.LinuxDistribution.Version, distroVersion)
|
|
|
|
var pkgs []string
|
|
for p := range bom.Artifacts.Packages.Enumerate() {
|
|
pkgs = append(pkgs, fmt.Sprintf("%s:%s", p.Name, p.Version))
|
|
}
|
|
|
|
assert.ElementsMatch(t, test.packages, pkgs)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDecoder_Identify(t *testing.T) {
|
|
type testCase struct {
|
|
name string
|
|
file string
|
|
id sbom.FormatID
|
|
version string
|
|
}
|
|
|
|
var cases []testCase
|
|
|
|
for _, version := range SupportedVersions() {
|
|
cases = append(cases, testCase{
|
|
name: fmt.Sprintf("v%s schema", version),
|
|
file: fmt.Sprintf("test-fixtures/identify/%s.xml", version),
|
|
id: ID,
|
|
version: version,
|
|
})
|
|
}
|
|
|
|
for _, test := range cases {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
reader, err := os.Open(test.file)
|
|
require.NoError(t, err)
|
|
|
|
dec := NewFormatDecoder()
|
|
|
|
formatID, formatVersion := dec.Identify(reader)
|
|
assert.Equal(t, test.id, formatID)
|
|
assert.Equal(t, test.version, formatVersion)
|
|
})
|
|
}
|
|
}
|