github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/format/decoders_collection_test.go (about) 1 package format 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 12 "github.com/anchore/syft/syft/format/spdxjson" 13 "github.com/anchore/syft/syft/format/syftjson" 14 "github.com/anchore/syft/syft/sbom" 15 ) 16 17 func TestIdentify(t *testing.T) { 18 tests := []struct { 19 fixture string 20 id sbom.FormatID 21 version string 22 }{ 23 { 24 fixture: "test-fixtures/alpine-syft.json", 25 id: syftjson.ID, 26 version: "1.1.0", 27 }, 28 } 29 for _, test := range tests { 30 t.Run(test.fixture, func(t *testing.T) { 31 reader, err := os.Open(test.fixture) 32 assert.NoError(t, err) 33 34 id, version := Identify(reader) 35 assert.Equal(t, test.id, id) 36 assert.Equal(t, test.version, version) 37 38 }) 39 } 40 } 41 42 func TestDecodeUnseekable(t *testing.T) { 43 reader, err := os.Open("spdxjson/test-fixtures/spdx/example7-go-module.spdx.json") 44 assert.NoError(t, err) 45 46 // io.NopCloser wraps the reader in a non-seekable type 47 unseekableReader := io.NopCloser(reader) 48 _, formatID, _, err := Decode(unseekableReader) 49 assert.NoError(t, err) 50 assert.Equal(t, spdxjson.ID, formatID) 51 } 52 53 func TestFormats_EmptyInput(t *testing.T) { 54 for _, format := range Decoders() { 55 name := strings.Split(fmt.Sprintf("%#v", format), "{")[0] 56 57 t.Run(name, func(t *testing.T) { 58 t.Run("Decode", func(t *testing.T) { 59 assert.NotPanics(t, func() { 60 decodedSBOM, _, _, err := format.Decode(nil) 61 assert.Error(t, err) 62 assert.Nil(t, decodedSBOM) 63 }) 64 }) 65 66 t.Run("Identify", func(t *testing.T) { 67 assert.NotPanics(t, func() { 68 id, version := format.Identify(nil) 69 assert.Empty(t, id) 70 assert.Empty(t, version) 71 }) 72 }) 73 }) 74 } 75 }