2018-01-30 11:56:36 +01:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-08-23 18:50:45 +03:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
2019-09-16 12:18:13 +03:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-02-01 10:54:23 -08:00
|
|
|
. "github.com/CityOfZion/neo-go/pkg/util"
|
2019-08-29 17:33:53 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-01-30 11:56:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInventoryEncodeDecode(t *testing.T) {
|
|
|
|
hashes := []Uint256{
|
2019-08-23 18:50:45 +03:00
|
|
|
hash.Sha256([]byte("a")),
|
|
|
|
hash.Sha256([]byte("b")),
|
2018-01-30 11:56:36 +01:00
|
|
|
}
|
|
|
|
inv := NewInventory(BlockType, hashes)
|
|
|
|
|
2019-09-16 12:18:13 +03:00
|
|
|
buf := io.NewBufBinWriter()
|
2019-09-16 19:31:49 +03:00
|
|
|
inv.EncodeBinary(buf.BinWriter)
|
|
|
|
assert.Nil(t, buf.Err)
|
2018-01-30 11:56:36 +01:00
|
|
|
|
2019-09-16 12:18:13 +03:00
|
|
|
b := buf.Bytes()
|
|
|
|
r := io.NewBinReaderFromBuf(b)
|
2018-01-30 11:56:36 +01:00
|
|
|
invDecode := &Inventory{}
|
2019-09-16 19:31:49 +03:00
|
|
|
invDecode.DecodeBinary(r)
|
|
|
|
assert.Nil(t, r.Err)
|
2019-08-29 17:33:53 +03:00
|
|
|
assert.Equal(t, inv, invDecode)
|
2018-01-30 11:56:36 +01:00
|
|
|
}
|
2019-08-29 20:27:16 +03:00
|
|
|
|
|
|
|
func TestEmptyInv(t *testing.T) {
|
|
|
|
msgInv := NewInventory(TXType, []Uint256{})
|
|
|
|
|
2019-09-16 12:18:13 +03:00
|
|
|
buf := io.NewBufBinWriter()
|
2019-09-16 19:31:49 +03:00
|
|
|
msgInv.EncodeBinary(buf.BinWriter)
|
|
|
|
assert.Nil(t, buf.Err)
|
2019-08-29 20:27:16 +03:00
|
|
|
assert.Equal(t, []byte{byte(TXType), 0}, buf.Bytes())
|
|
|
|
assert.Equal(t, 0, len(msgInv.Hashes))
|
|
|
|
}
|