2019-09-16 09:18:13 +00:00
|
|
|
package io
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-15 12:54:53 +00:00
|
|
|
"errors"
|
2018-03-17 11:53:21 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-09-15 12:54:53 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
func TestBufBinWriterReset(t *testing.T) {
|
|
|
|
bw := NewBufBinWriter()
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
bw.WriteLE(uint32(i))
|
|
|
|
assert.Nil(t, bw.Err)
|
|
|
|
_ = bw.Bytes()
|
|
|
|
assert.NotNil(t, bw.Err)
|
|
|
|
bw.Reset()
|
|
|
|
assert.Nil(t, bw.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 12:54:53 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
func TestWriteVarUint1(t *testing.T) {
|
|
|
|
var (
|
|
|
|
val = uint64(1)
|
|
|
|
)
|
2019-09-15 11:58:19 +00:00
|
|
|
bw := NewBufBinWriter()
|
2019-08-28 16:41:56 +00:00
|
|
|
bw.WriteVarUint(val)
|
|
|
|
assert.Nil(t, bw.Err)
|
2019-09-15 11:58:19 +00:00
|
|
|
buf := bw.Bytes()
|
|
|
|
assert.Equal(t, 1, len(buf))
|
2019-09-15 12:54:53 +00:00
|
|
|
br := NewBinReaderFromBuf(buf)
|
|
|
|
res := br.ReadVarUint()
|
|
|
|
assert.Nil(t, br.Err)
|
|
|
|
assert.Equal(t, val, res)
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteVarUint1000(t *testing.T) {
|
|
|
|
var (
|
|
|
|
val = uint64(1000)
|
|
|
|
)
|
2019-09-15 11:58:19 +00:00
|
|
|
bw := NewBufBinWriter()
|
2019-08-28 16:41:56 +00:00
|
|
|
bw.WriteVarUint(val)
|
|
|
|
assert.Nil(t, bw.Err)
|
2019-09-15 11:58:19 +00:00
|
|
|
buf := bw.Bytes()
|
|
|
|
assert.Equal(t, 3, len(buf))
|
|
|
|
assert.Equal(t, byte(0xfd), buf[0])
|
|
|
|
br := NewBinReaderFromBuf(buf)
|
2019-08-28 16:41:56 +00:00
|
|
|
res := br.ReadVarUint()
|
|
|
|
assert.Nil(t, br.Err)
|
2018-03-17 11:53:21 +00:00
|
|
|
assert.Equal(t, val, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteVarUint100000(t *testing.T) {
|
|
|
|
var (
|
|
|
|
val = uint64(100000)
|
|
|
|
)
|
2019-09-15 11:58:19 +00:00
|
|
|
bw := NewBufBinWriter()
|
2019-08-28 16:41:56 +00:00
|
|
|
bw.WriteVarUint(val)
|
|
|
|
assert.Nil(t, bw.Err)
|
2019-09-15 11:58:19 +00:00
|
|
|
buf := bw.Bytes()
|
|
|
|
assert.Equal(t, 5, len(buf))
|
|
|
|
assert.Equal(t, byte(0xfe), buf[0])
|
|
|
|
br := NewBinReaderFromBuf(buf)
|
2019-08-28 16:41:56 +00:00
|
|
|
res := br.ReadVarUint()
|
|
|
|
assert.Nil(t, br.Err)
|
2018-03-17 11:53:21 +00:00
|
|
|
assert.Equal(t, val, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteVarUint100000000000(t *testing.T) {
|
|
|
|
var (
|
|
|
|
val = uint64(1000000000000)
|
|
|
|
)
|
2019-09-15 11:58:19 +00:00
|
|
|
bw := NewBufBinWriter()
|
2019-08-28 16:41:56 +00:00
|
|
|
bw.WriteVarUint(val)
|
|
|
|
assert.Nil(t, bw.Err)
|
2019-09-15 11:58:19 +00:00
|
|
|
buf := bw.Bytes()
|
|
|
|
assert.Equal(t, 9, len(buf))
|
|
|
|
assert.Equal(t, byte(0xff), buf[0])
|
|
|
|
br := NewBinReaderFromBuf(buf)
|
2019-08-28 16:41:56 +00:00
|
|
|
res := br.ReadVarUint()
|
|
|
|
assert.Nil(t, br.Err)
|
2018-03-17 11:53:21 +00:00
|
|
|
assert.Equal(t, val, res)
|
|
|
|
}
|