diff --git a/pkg/network/payload/address.go b/pkg/network/payload/address.go index 04e925aed..13e9e9743 100644 --- a/pkg/network/payload/address.go +++ b/pkg/network/payload/address.go @@ -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. diff --git a/pkg/network/payload/address_test.go b/pkg/network/payload/address_test.go index 6aa29414c..b429a8a24 100644 --- a/pkg/network/payload/address_test.go +++ b/pkg/network/payload/address_test.go @@ -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) +}