mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 06:56:40 +01:00
Split the sbom.Format interface by encode and decode use cases (#2186)
* 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>
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/anchore/syft/internal/log"
|
||||
)
|
||||
|
||||
var _ io.ReadSeekCloser = (*bufferedSeekReader)(nil)
|
||||
|
||||
// bufferedSeekReader wraps an io.ReadCloser to provide io.Seeker functionality.
|
||||
// It only supports seeking from the start and cannot seek past what has already been read.
|
||||
type bufferedSeekReader struct {
|
||||
r io.ReadCloser
|
||||
buf *bytes.Reader
|
||||
data []byte
|
||||
pos int64
|
||||
closed bool
|
||||
}
|
||||
|
||||
func NewBufferedSeeker(rc io.ReadCloser) io.ReadSeekCloser {
|
||||
return &bufferedSeekReader{
|
||||
r: rc,
|
||||
}
|
||||
}
|
||||
|
||||
func (bs *bufferedSeekReader) Read(p []byte) (int, error) {
|
||||
if bs.closed {
|
||||
return 0, errors.New("cannot read from closed reader")
|
||||
}
|
||||
if bs.pos == int64(len(bs.data)) {
|
||||
// if we're at the end of our buffer, read more data into it
|
||||
tmp := make([]byte, len(p))
|
||||
|
||||
n, err := bs.r.Read(tmp)
|
||||
if err != nil && err != io.EOF {
|
||||
return 0, err
|
||||
} else if err == io.EOF {
|
||||
bs.closed = true
|
||||
}
|
||||
bs.data = append(bs.data, tmp[:n]...)
|
||||
bs.buf = bytes.NewReader(bs.data)
|
||||
}
|
||||
|
||||
n, err := bs.buf.ReadAt(p, bs.pos)
|
||||
if err != nil && err != io.EOF {
|
||||
log.WithFields("error", err).Trace("buffered seek reader failed to read from underlying reader")
|
||||
}
|
||||
bs.pos += int64(n)
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (bs *bufferedSeekReader) Seek(offset int64, whence int) (int64, error) {
|
||||
var abs int64
|
||||
switch whence {
|
||||
case io.SeekStart:
|
||||
abs = offset
|
||||
case io.SeekCurrent:
|
||||
abs = bs.pos + offset
|
||||
case io.SeekEnd:
|
||||
return 0, errors.New("'SeekEnd' not supported")
|
||||
default:
|
||||
return 0, errors.New("invalid seek option")
|
||||
}
|
||||
|
||||
if abs < 0 {
|
||||
return 0, errors.New("unable to seek before start")
|
||||
}
|
||||
|
||||
if abs > int64(len(bs.data)) {
|
||||
return 0, errors.New("unable to seek past read data")
|
||||
}
|
||||
|
||||
bs.pos = abs
|
||||
return bs.pos, nil
|
||||
}
|
||||
|
||||
func (bs *bufferedSeekReader) Close() error {
|
||||
bs.closed = true
|
||||
return bs.r.Close()
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestBufferedSeeker_Read(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
initialData string
|
||||
readLengths []int
|
||||
expectedReads []string
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "go case (read)",
|
||||
initialData: "Hello, World!",
|
||||
readLengths: []int{5},
|
||||
expectedReads: []string{"Hello"},
|
||||
},
|
||||
{
|
||||
name: "multiple reads",
|
||||
initialData: "Hello, World!",
|
||||
readLengths: []int{5, 8},
|
||||
expectedReads: []string{"Hello", ", World!"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
bs := NewBufferedSeeker(io.NopCloser(bytes.NewBufferString(tt.initialData)))
|
||||
|
||||
for i, length := range tt.readLengths {
|
||||
buf := make([]byte, length)
|
||||
n, err := bs.Read(buf)
|
||||
|
||||
if !tt.expectError {
|
||||
assert.NoError(t, err)
|
||||
assert.Equalf(t, tt.expectedReads[i], string(buf[:n]), "read index %d", i)
|
||||
} else {
|
||||
assert.Error(t, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBufferedSeeker_Seek(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
initialData string
|
||||
readLengths []int
|
||||
seekOffsets []int64
|
||||
seekWhence []int
|
||||
expectedReads []string
|
||||
seekError require.ErrorAssertionFunc
|
||||
readError require.ErrorAssertionFunc
|
||||
}{
|
||||
{
|
||||
name: "seek start 0 without read first",
|
||||
initialData: "Hello, World!",
|
||||
readLengths: []int{5},
|
||||
seekOffsets: []int64{0},
|
||||
seekWhence: []int{io.SeekStart},
|
||||
expectedReads: []string{"Hello"},
|
||||
},
|
||||
{
|
||||
name: "read + seek back",
|
||||
initialData: "Hello, World!",
|
||||
readLengths: []int{5, 8, 8},
|
||||
seekOffsets: []int64{-1, -1, 2},
|
||||
seekWhence: []int{io.SeekStart, io.SeekStart, io.SeekStart},
|
||||
expectedReads: []string{"Hello", ", World!", "llo, Wor"},
|
||||
},
|
||||
{
|
||||
name: "seek past read data",
|
||||
initialData: "Hello, World!",
|
||||
readLengths: []int{5},
|
||||
seekOffsets: []int64{20},
|
||||
seekWhence: []int{io.SeekStart},
|
||||
expectedReads: []string{""},
|
||||
seekError: require.Error,
|
||||
},
|
||||
{
|
||||
name: "seek to end",
|
||||
initialData: "Hello, World!",
|
||||
readLengths: []int{-1},
|
||||
seekOffsets: []int64{20},
|
||||
seekWhence: []int{io.SeekEnd},
|
||||
expectedReads: []string{""},
|
||||
seekError: require.Error,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.seekError == nil {
|
||||
tt.seekError = require.NoError
|
||||
}
|
||||
if tt.readError == nil {
|
||||
tt.readError = require.NoError
|
||||
}
|
||||
|
||||
bs := NewBufferedSeeker(io.NopCloser(bytes.NewBufferString(tt.initialData)))
|
||||
|
||||
for i, length := range tt.readLengths {
|
||||
if len(tt.seekOffsets) > i && tt.seekOffsets[i] >= 0 {
|
||||
_, err := bs.Seek(tt.seekOffsets[i], tt.seekWhence[i])
|
||||
tt.seekError(t, err)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if length >= 0 {
|
||||
buf := make([]byte, length)
|
||||
n, err := bs.Read(buf)
|
||||
tt.readError(t, err)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
assert.Equalf(t, tt.expectedReads[i], string(buf[:n]), "read index %d", i)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBufferedSeeker_Close(t *testing.T) {
|
||||
bs := NewBufferedSeeker(io.NopCloser(bytes.NewBufferString("Hello, World!")))
|
||||
err := bs.Close()
|
||||
assert.NoError(t, err)
|
||||
n, err := bs.Read(make([]byte, 1))
|
||||
assert.Equal(t, 0, n)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user