Skip to content

Commit

Permalink
Add unit test for unexpected eof
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero authored and rvagg committed Apr 4, 2023
1 parent 0981f85 commit 83a0e93
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions cid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 83a0e93

Please sign in to comment.