2018-03-09 15:55:25 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-09-09 14:54:38 +00:00
|
|
|
"net"
|
2018-03-09 15:55:25 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
|
2020-05-22 09:59:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/capability"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-08 10:26:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-09 15:55:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncodeDecodeAddress(t *testing.T) {
|
|
|
|
var (
|
2019-09-09 14:54:38 +00:00
|
|
|
e, _ = net.ResolveTCPAddr("tcp", "127.0.0.1:2000")
|
2019-08-29 15:39:46 +00:00
|
|
|
ts = time.Now()
|
2020-05-22 09:59:18 +00:00
|
|
|
addr = NewAddressAndTime(e, ts, capability.Capabilities{
|
|
|
|
{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{Port: uint16(e.Port)},
|
|
|
|
},
|
|
|
|
})
|
2018-03-09 15:55:25 +00:00
|
|
|
)
|
|
|
|
|
2019-08-29 15:39:46 +00:00
|
|
|
assert.Equal(t, ts.UTC().Unix(), int64(addr.Timestamp))
|
2019-09-09 14:54:38 +00:00
|
|
|
aatip := make(net.IP, 16)
|
|
|
|
copy(aatip, addr.IP[:])
|
|
|
|
assert.Equal(t, e.IP, aatip)
|
2020-05-22 09:59:18 +00:00
|
|
|
assert.Equal(t, 1, len(addr.Capabilities))
|
|
|
|
assert.Equal(t, capability.Capability{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{Port: uint16(e.Port)},
|
|
|
|
}, addr.Capabilities[0])
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, addr, new(AddressAndTime))
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 10:26:18 +00:00
|
|
|
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{
|
2020-05-22 09:59:18 +00:00
|
|
|
{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{Port: 123},
|
|
|
|
},
|
|
|
|
})
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
2020-10-08 10:26:18 +00:00
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2020-10-08 10:26:18 +00:00
|
|
|
func TestEncodeDecodeAddressList(t *testing.T) {
|
|
|
|
var lenList uint8 = 4
|
|
|
|
addrList := NewAddressList(int(lenList))
|
|
|
|
fillAddressList(addrList)
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, addrList, new(AddressList))
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
2020-10-08 10:26:18 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|