From f720aba928d24f80428df66efca6815ae2b97c98 Mon Sep 17 00:00:00 2001 From: Krzysztof Sychla Date: Mon, 8 Jul 2024 15:02:31 +0200 Subject: [PATCH] [#61844] linux-client: download: Add unit tests --- .../download/cached_fetcher_test.go | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 devices/linux-client/download/cached_fetcher_test.go diff --git a/devices/linux-client/download/cached_fetcher_test.go b/devices/linux-client/download/cached_fetcher_test.go new file mode 100644 index 0000000..b0921ee --- /dev/null +++ b/devices/linux-client/download/cached_fetcher_test.go @@ -0,0 +1,72 @@ +package download + +import ( + "bytes" + "github.com/stretchr/testify/assert" + "io" + "testing" +) + +var FirstTestData = []byte{0x60, 0x09, 0x20, 0x01, 0x30, 0x07} +var SecondTestData = []byte{0x04, 0x00, 0x02, 0x66} + +type BufferCloser struct { + *bytes.Buffer +} + +func (b *BufferCloser) Close() error { return nil } + +func TestReadSaver(t *testing.T) { + readSaver := &ReadSaver{offset: 0} + readSaver.Write(FirstTestData) + + readBackBuffer, err := readNBytes(readSaver, len(FirstTestData)) + + assert.NoError(t, err) + assert.Equal(t, FirstTestData, readBackBuffer) +} + +func TestCombinedSourceReader(t *testing.T) { + firstDataReader := &BufferCloser{bytes.NewBuffer(FirstTestData)} + secondDataReader := &BufferCloser{bytes.NewBuffer(SecondTestData)} + + combinedSourceReader := &CombinedSourceReader{ + firstReader: firstDataReader, + firstReaderEOF: false, + secondReader: secondDataReader, + } + + readBackBuffer, err := readNBytes(combinedSourceReader, len(FirstTestData)+len(SecondTestData)) + + assert.NoError(t, err) + assert.Equal(t, append(FirstTestData, SecondTestData...), readBackBuffer) +} + +func TestTeeReadCloser(t *testing.T) { + readBackBuffer := bytes.NewBuffer([]byte{}) + firstDataReader := &BufferCloser{bytes.NewBuffer(FirstTestData)} + dataWriter := &BufferCloser{readBackBuffer} + + teeReadCloser := &TeeReadCloser{ + reader: firstDataReader, + writer: dataWriter, + } + + _, err := readNBytes(teeReadCloser, len(FirstTestData)) + assert.NoError(t, err) + assert.Equal(t, FirstTestData, readBackBuffer.Bytes()) +} + +func readNBytes(reader io.Reader, length int) ([]byte, error) { + var err error + readBackBuffer := make([]byte, length) + bytesRead := 0 + for { + n, err := reader.Read(readBackBuffer[bytesRead:]) + bytesRead += n + if bytesRead >= length || err != nil { + break + } + } + return readBackBuffer, err +}