payload: add a check for zero-length address list

Which is also present in C# code. Thanks, @AnnaShaleva.
This commit is contained in:
Roman Khimov 2020-10-08 13:26:18 +03:00
parent 5abec520c7
commit a44cb99df6
2 changed files with 30 additions and 6 deletions

View file

@ -80,6 +80,9 @@ func NewAddressList(n int) *AddressList {
// DecodeBinary implements Serializable interface.
func (p *AddressList) DecodeBinary(br *io.BinReader) {
br.ReadArray(&p.Addrs, MaxAddrsCount)
if len(p.Addrs) == 0 {
br.Err = errors.New("no addresses listed")
}
}
// EncodeBinary implements Serializable interface.

View file

@ -9,6 +9,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/network/capability"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncodeDecodeAddress(t *testing.T) {
@ -36,18 +37,38 @@ func TestEncodeDecodeAddress(t *testing.T) {
testserdes.EncodeDecodeBinary(t, addr, new(AddressAndTime))
}
func TestEncodeDecodeAddressList(t *testing.T) {
var lenList uint8 = 4
addrList := NewAddressList(int(lenList))
for i := 0; i < int(lenList); i++ {
e, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:200%d", i))
addrList.Addrs[i] = NewAddressAndTime(e, time.Now(), capability.Capabilities{
func fillAddressList(al *AddressList) {
for i := 0; i < len(al.Addrs); i++ {
e, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:20%d", i))
al.Addrs[i] = NewAddressAndTime(e, time.Now(), capability.Capabilities{
{
Type: capability.TCPServer,
Data: &capability.Server{Port: 123},
},
})
}
}
func TestEncodeDecodeAddressList(t *testing.T) {
var lenList uint8 = 4
addrList := NewAddressList(int(lenList))
fillAddressList(addrList)
testserdes.EncodeDecodeBinary(t, addrList, new(AddressList))
}
func TestEncodeDecodeBadAddressList(t *testing.T) {
var newAL = new(AddressList)
addrList := NewAddressList(MaxAddrsCount + 1)
fillAddressList(addrList)
bin, err := testserdes.EncodeBinary(addrList)
require.NoError(t, err)
err = testserdes.DecodeBinary(bin, newAL)
require.Error(t, err)
addrList = NewAddressList(0)
bin, err = testserdes.EncodeBinary(addrList)
require.NoError(t, err)
err = testserdes.DecodeBinary(bin, newAL)
require.Error(t, err)
}