neoneo-go/pkg/network/payload/inventory_test.go
Roman Khimov 5bf00db2c9 io: move BinReader/BinWriter there, redo Serializable with it
The logic here is that we'll have all binary encoding/decoding done via our io
package, which simplifies error handling. This functionality doesn't belong to
util, so it's moved.

This also expands BufBinWriter with Reset() method to fit the needs of core
package.
2019-09-16 23:39:51 +03:00

39 lines
903 B
Go

package payload
import (
"testing"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/io"
. "github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
)
func TestInventoryEncodeDecode(t *testing.T) {
hashes := []Uint256{
hash.Sha256([]byte("a")),
hash.Sha256([]byte("b")),
}
inv := NewInventory(BlockType, hashes)
buf := io.NewBufBinWriter()
err := inv.EncodeBinary(buf.BinWriter)
assert.Nil(t, err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
invDecode := &Inventory{}
err = invDecode.DecodeBinary(r)
assert.Nil(t, err)
assert.Equal(t, inv, invDecode)
}
func TestEmptyInv(t *testing.T) {
msgInv := NewInventory(TXType, []Uint256{})
buf := io.NewBufBinWriter()
err := msgInv.EncodeBinary(buf.BinWriter)
assert.Nil(t, err)
assert.Equal(t, []byte{byte(TXType), 0}, buf.Bytes())
assert.Equal(t, 0, len(msgInv.Hashes))
}