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-11-13 07:36:29 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-17 11:53:21 +00:00
|
|
|
)
|
|
|
|
|
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
|
2019-10-17 09:25:36 +00:00
|
|
|
bin = []byte{0xef, 0xbe, 0xad, 0xde}
|
2019-09-15 12:54:53 +00:00
|
|
|
)
|
|
|
|
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
|
2019-10-17 09:25:36 +00:00
|
|
|
bin = []byte{0xde, 0xad, 0xbe, 0xef}
|
2019-09-15 12:54:53 +00:00
|
|
|
)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
func TestBufBinWriter_Len(t *testing.T) {
|
|
|
|
val := []byte{0xde}
|
|
|
|
bw := NewBufBinWriter()
|
|
|
|
bw.WriteLE(val)
|
|
|
|
require.Equal(t, 1, bw.Len())
|
|
|
|
}
|
|
|
|
|
2019-09-15 12:54:53 +00:00
|
|
|
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)
|
2019-11-22 10:34:06 +00:00
|
|
|
bw.WriteVarBytes([]byte{0x55, 0xaa})
|
2019-09-15 12:54:53 +00:00
|
|
|
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 (
|
2019-10-17 09:25:36 +00:00
|
|
|
str = "teststring"
|
2019-09-15 12:54:53 +00:00
|
|
|
)
|
|
|
|
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)
|
|
|
|
}
|
2019-11-13 07:36:29 +00:00
|
|
|
|
2019-12-03 13:20:06 +00:00
|
|
|
func TestWriteBytes(t *testing.T) {
|
|
|
|
var (
|
|
|
|
bin = []byte{0xde, 0xad, 0xbe, 0xef}
|
|
|
|
)
|
|
|
|
bw := NewBufBinWriter()
|
|
|
|
bw.WriteBytes(bin)
|
|
|
|
assert.Nil(t, bw.Err)
|
|
|
|
buf := bw.Bytes()
|
|
|
|
assert.Equal(t, 4, len(buf))
|
|
|
|
assert.Equal(t, byte(0xde), buf[0])
|
2019-12-06 14:40:47 +00:00
|
|
|
|
|
|
|
bw = NewBufBinWriter()
|
|
|
|
bw.Err = errors.New("smth bad")
|
|
|
|
bw.WriteBytes(bin)
|
|
|
|
assert.Equal(t, 0, bw.Len())
|
2019-12-03 13:20:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 07:36:29 +00:00
|
|
|
type testSerializable uint16
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (t testSerializable) EncodeBinary(w *BinWriter) {
|
|
|
|
w.WriteLE(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (t *testSerializable) DecodeBinary(r *BinReader) {
|
|
|
|
r.ReadLE(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBinWriter_WriteArray(t *testing.T) {
|
|
|
|
var arr [3]testSerializable
|
|
|
|
for i := range arr {
|
|
|
|
arr[i] = testSerializable(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []byte{3, 0, 0, 1, 0, 2, 0}
|
|
|
|
|
|
|
|
w := NewBufBinWriter()
|
|
|
|
w.WriteArray(arr)
|
|
|
|
require.NoError(t, w.Err)
|
|
|
|
require.Equal(t, expected, w.Bytes())
|
|
|
|
|
|
|
|
w.Reset()
|
|
|
|
w.WriteArray(arr[:])
|
|
|
|
require.NoError(t, w.Err)
|
|
|
|
require.Equal(t, expected, w.Bytes())
|
|
|
|
|
|
|
|
arrS := make([]Serializable, len(arr))
|
|
|
|
for i := range arrS {
|
|
|
|
arrS[i] = &arr[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Reset()
|
|
|
|
w.WriteArray(arr)
|
|
|
|
require.NoError(t, w.Err)
|
|
|
|
require.Equal(t, expected, w.Bytes())
|
|
|
|
|
|
|
|
w.Reset()
|
|
|
|
require.Panics(t, func() { w.WriteArray(1) })
|
|
|
|
|
|
|
|
w.Reset()
|
|
|
|
w.Err = errors.New("error")
|
|
|
|
w.WriteArray(arr[:])
|
|
|
|
require.Error(t, w.Err)
|
|
|
|
require.Equal(t, w.Bytes(), []byte(nil))
|
|
|
|
|
|
|
|
w.Reset()
|
|
|
|
w.Err = errors.New("error")
|
|
|
|
require.Panics(t, func() { w.WriteArray([]int{}) })
|
|
|
|
|
|
|
|
w.Reset()
|
|
|
|
w.Err = errors.New("error")
|
|
|
|
require.Panics(t, func() { w.WriteArray(make(chan testSerializable)) })
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBinReader_ReadArray(t *testing.T) {
|
|
|
|
data := []byte{3, 0, 0, 1, 0, 2, 0}
|
|
|
|
elems := []testSerializable{0, 1, 2}
|
2019-11-14 07:50:03 +00:00
|
|
|
|
|
|
|
r := NewBinReaderFromBuf(data)
|
|
|
|
arrPtr := []*testSerializable{}
|
|
|
|
r.ReadArray(&arrPtr)
|
|
|
|
require.Equal(t, []*testSerializable{&elems[0], &elems[1], &elems[2]}, arrPtr)
|
2019-11-13 07:36:29 +00:00
|
|
|
|
|
|
|
r = NewBinReaderFromBuf(data)
|
2019-11-14 07:50:03 +00:00
|
|
|
arrVal := []testSerializable{}
|
|
|
|
r.ReadArray(&arrVal)
|
|
|
|
require.NoError(t, r.Err)
|
|
|
|
require.Equal(t, elems, arrVal)
|
|
|
|
|
2019-11-15 19:33:28 +00:00
|
|
|
r = NewBinReaderFromBuf(data)
|
|
|
|
arrVal = []testSerializable{}
|
|
|
|
r.ReadArray(&arrVal, 3)
|
|
|
|
require.NoError(t, r.Err)
|
|
|
|
require.Equal(t, elems, arrVal)
|
|
|
|
|
|
|
|
r = NewBinReaderFromBuf(data)
|
|
|
|
arrVal = []testSerializable{}
|
|
|
|
r.ReadArray(&arrVal, 2)
|
|
|
|
require.Error(t, r.Err)
|
|
|
|
|
2019-11-14 07:50:03 +00:00
|
|
|
r = NewBinReaderFromBuf([]byte{0})
|
|
|
|
r.ReadArray(&arrVal)
|
|
|
|
require.NoError(t, r.Err)
|
|
|
|
require.Equal(t, []testSerializable{}, arrVal)
|
|
|
|
|
|
|
|
r = NewBinReaderFromBuf([]byte{0})
|
|
|
|
r.Err = errors.New("error")
|
|
|
|
arrVal = ([]testSerializable)(nil)
|
|
|
|
r.ReadArray(&arrVal)
|
|
|
|
require.Error(t, r.Err)
|
|
|
|
require.Equal(t, ([]testSerializable)(nil), arrVal)
|
|
|
|
|
|
|
|
r = NewBinReaderFromBuf([]byte{0})
|
2019-11-13 07:36:29 +00:00
|
|
|
r.Err = errors.New("error")
|
2019-11-14 07:50:03 +00:00
|
|
|
arrPtr = ([]*testSerializable)(nil)
|
|
|
|
r.ReadArray(&arrVal)
|
2019-11-13 07:36:29 +00:00
|
|
|
require.Error(t, r.Err)
|
2019-11-14 07:50:03 +00:00
|
|
|
require.Equal(t, ([]*testSerializable)(nil), arrPtr)
|
2019-11-13 07:36:29 +00:00
|
|
|
|
|
|
|
r = NewBinReaderFromBuf([]byte{0})
|
2019-11-14 07:50:03 +00:00
|
|
|
arrVal = []testSerializable{1, 2}
|
|
|
|
r.ReadArray(&arrVal)
|
2019-11-13 07:36:29 +00:00
|
|
|
require.NoError(t, r.Err)
|
2019-11-14 07:50:03 +00:00
|
|
|
require.Equal(t, []testSerializable{}, arrVal)
|
|
|
|
|
|
|
|
r = NewBinReaderFromBuf([]byte{0})
|
|
|
|
r.Err = errors.New("error")
|
|
|
|
require.Panics(t, func() { r.ReadArray(&[]*int{}) })
|
2019-11-13 07:36:29 +00:00
|
|
|
|
|
|
|
r = NewBinReaderFromBuf([]byte{0})
|
2019-11-14 07:50:03 +00:00
|
|
|
r.Err = errors.New("error")
|
|
|
|
require.Panics(t, func() { r.ReadArray(&[]int{}) })
|
|
|
|
|
|
|
|
r = NewBinReaderFromBuf([]byte{0})
|
|
|
|
r.Err = errors.New("error")
|
2019-11-13 07:36:29 +00:00
|
|
|
require.Panics(t, func() { r.ReadArray(0) })
|
|
|
|
}
|