From 83a0e939a40026ddb4131959c507ef7a49df9c7b Mon Sep 17 00:00:00 2001 From: gammazero Date: Mon, 3 Apr 2023 08:18:57 -0700 Subject: [PATCH] Add unit test for unexpected eof --- cid.go | 3 +++ cid_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/cid.go b/cid.go index 6e36764..f182424 100644 --- a/cid.go +++ b/cid.go @@ -717,6 +717,9 @@ func (r *bufByteReader) ReadByte() (byte, error) { // It's recommended to supply a reader that buffers and implements io.ByteReader, // as CidFromReader has to do many single-byte reads to decode varints. // If the argument only implements io.Reader, single-byte Read calls are used instead. +// +// If the Reader is found to yield zero bytes, an io.EOF error is returned directly, in all +// other error cases, an ErrInvalidCid, wrapping the original error, is returned. func CidFromReader(r io.Reader) (int, Cid, error) { // 64 bytes is enough for any CIDv0, // and it's enough for most CIDv1s in practice. diff --git a/cid_test.go b/cid_test.go index f0c6ad5..36fba76 100644 --- a/cid_test.go +++ b/cid_test.go @@ -795,6 +795,16 @@ func TestFromReaderNoData(t *testing.T) { if n != 0 { t.Fatal("Expected 0 data") } + + // Read byte indicatiing more data to and check error is ErrInvalidCid. + _, _, err = CidFromReader(bytes.NewReader([]byte{0x80})) + if !errors.Is(err, ErrInvalidCid{}) { + t.Fatal("Expected ErrInvalidCid error") + } + // Check for expected wrapped error. + if !errors.Is(err, io.ErrUnexpectedEOF) { + t.Fatal("Expected error", io.ErrUnexpectedEOF) + } } func TestBadParse(t *testing.T) {