util: add tests for BinaryReader/BinaryWriter

100% coverage.
This commit is contained in:
Roman Khimov 2019-09-15 15:54:53 +03:00
parent 72fc880182
commit 031a260cb1

View file

@ -1,11 +1,122 @@
package util
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
// mocks io.Reader and io.Writer, always fails to Write() or Read().
type badRW struct{}
func (w *badRW) Write(p []byte) (int, error) {
return 0, errors.New("it always fails")
}
func (w *badRW) Read(p []byte) (int, error) {
return w.Write(p)
}
func TestWriteLE(t *testing.T) {
var (
val uint32 = 0xdeadbeef
readval uint32
bin []byte = []byte{0xef, 0xbe, 0xad, 0xde}
)
bw := NewBufBinWriter()
bw.WriteLE(val)
assert.Nil(t, bw.Err)
wrotebin := bw.Bytes()
assert.Equal(t, wrotebin, bin)
br := NewBinReaderFromBuf(bin)
br.ReadLE(&readval)
assert.Nil(t, br.Err)
assert.Equal(t, val, readval)
}
func TestWriteBE(t *testing.T) {
var (
val uint32 = 0xdeadbeef
readval uint32
bin []byte = []byte{0xde, 0xad, 0xbe, 0xef}
)
bw := NewBufBinWriter()
bw.WriteBE(val)
assert.Nil(t, bw.Err)
wrotebin := bw.Bytes()
assert.Equal(t, wrotebin, bin)
br := NewBinReaderFromBuf(bin)
br.ReadBE(&readval)
assert.Nil(t, br.Err)
assert.Equal(t, val, readval)
}
func TestWriterErrHandling(t *testing.T) {
var badio = &badRW{}
bw := NewBinWriterFromIO(badio)
bw.WriteLE(uint32(0))
assert.NotNil(t, bw.Err)
// these should work (without panic), preserving the Err
bw.WriteLE(uint32(0))
bw.WriteBE(uint32(0))
bw.WriteVarUint(0)
bw.WriteBytes([]byte{0x55, 0xaa})
bw.WriteString("neo")
assert.NotNil(t, bw.Err)
}
func TestReaderErrHandling(t *testing.T) {
var (
i uint32 = 0xdeadbeef
iorig = i
badio = &badRW{}
)
br := NewBinReaderFromIO(badio)
br.ReadLE(&i)
assert.NotNil(t, br.Err)
// i shouldn't change
assert.Equal(t, i, iorig)
// these should work (without panic), preserving the Err
br.ReadLE(&i)
br.ReadBE(&i)
assert.Equal(t, i, iorig)
val := br.ReadVarUint()
assert.Equal(t, val, uint64(0))
b := br.ReadBytes()
assert.Equal(t, b, []byte{})
s := br.ReadString()
assert.Equal(t, s, "")
assert.NotNil(t, br.Err)
}
func TestBufBinWriterErr(t *testing.T) {
bw := NewBufBinWriter()
bw.WriteLE(uint32(0))
assert.Nil(t, bw.Err)
// inject error
bw.Err = errors.New("oopsie")
res := bw.Bytes()
assert.NotNil(t, bw.Err)
assert.Nil(t, res)
}
func TestWriteString(t *testing.T) {
var (
str string = "teststring"
)
bw := NewBufBinWriter()
bw.WriteString(str)
assert.Nil(t, bw.Err)
wrotebin := bw.Bytes()
// +1 byte for length
assert.Equal(t, len(wrotebin), len(str)+1)
br := NewBinReaderFromBuf(wrotebin)
readstr := br.ReadString()
assert.Nil(t, br.Err)
assert.Equal(t, str, readstr)
}
func TestWriteVarUint1(t *testing.T) {
var (
val = uint64(1)
@ -15,6 +126,10 @@ func TestWriteVarUint1(t *testing.T) {
assert.Nil(t, bw.Err)
buf := bw.Bytes()
assert.Equal(t, 1, len(buf))
br := NewBinReaderFromBuf(buf)
res := br.ReadVarUint()
assert.Nil(t, br.Err)
assert.Equal(t, val, res)
}
func TestWriteVarUint1000(t *testing.T) {