mirror of
https://github.com/anchore/syft
synced 2024-11-13 23:57:07 +00:00
Fix panic on empty sbom (#917)
* Implement fmt.Stringer with format.ID Signed-off-by: Dan Luhring <dan+github@luhrings.com> * Add failing test for formats processing empty SBOMs Signed-off-by: Dan Luhring <dan+github@luhrings.com> * Account for nil SPDX document during Syft model conversion Signed-off-by: Dan Luhring <dan+github@luhrings.com>
This commit is contained in:
parent
cc2c0e57a0
commit
a7db43f5ec
3 changed files with 36 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
||||||
package spdxhelpers
|
package spdxhelpers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -17,6 +18,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func ToSyftModel(doc *spdx.Document2_2) (*sbom.SBOM, error) {
|
func ToSyftModel(doc *spdx.Document2_2) (*sbom.SBOM, error) {
|
||||||
|
if doc == nil {
|
||||||
|
return nil, errors.New("cannot convert SPDX document to Syft model because document is nil")
|
||||||
|
}
|
||||||
|
|
||||||
spdxIDMap := make(map[string]interface{})
|
spdxIDMap := make(map[string]interface{})
|
||||||
|
|
||||||
s := &sbom.SBOM{
|
s := &sbom.SBOM{
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package syft
|
package syft
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -41,6 +42,31 @@ func TestIdentify(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFormats_EmptyInput(t *testing.T) {
|
||||||
|
for _, format := range formats {
|
||||||
|
t.Run(format.ID().String(), func(t *testing.T) {
|
||||||
|
t.Run("format.Decode", func(t *testing.T) {
|
||||||
|
input := bytes.NewReader(nil)
|
||||||
|
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
decodedSBOM, err := format.Decode(input)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, decodedSBOM)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("format.Validate", func(t *testing.T) {
|
||||||
|
input := bytes.NewReader(nil)
|
||||||
|
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
err := format.Validate(input)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFormatByName(t *testing.T) {
|
func TestFormatByName(t *testing.T) {
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
|
|
@ -13,6 +13,11 @@ var (
|
||||||
|
|
||||||
type FormatID string
|
type FormatID string
|
||||||
|
|
||||||
|
// String returns a string representation of the FormatID.
|
||||||
|
func (f FormatID) String() string {
|
||||||
|
return string(f)
|
||||||
|
}
|
||||||
|
|
||||||
type Format interface {
|
type Format interface {
|
||||||
ID() FormatID
|
ID() FormatID
|
||||||
Encode(io.Writer, SBOM) error
|
Encode(io.Writer, SBOM) error
|
||||||
|
|
Loading…
Reference in a new issue