neoneo-go/pkg/network/payload/address_test.go
Evgenii Stratonikov 7d91a3a89e pkg: move internal/ package to the root directory
This way we can use it in scripts and cli.
2020-11-24 16:39:56 +03:00

74 lines
1.9 KiB
Go

package payload
import (
"fmt"
"net"
"testing"
"time"
"github.com/nspcc-dev/neo-go/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) {
var (
e, _ = net.ResolveTCPAddr("tcp", "127.0.0.1:2000")
ts = time.Now()
addr = NewAddressAndTime(e, ts, capability.Capabilities{
{
Type: capability.TCPServer,
Data: &capability.Server{Port: uint16(e.Port)},
},
})
)
assert.Equal(t, ts.UTC().Unix(), int64(addr.Timestamp))
aatip := make(net.IP, 16)
copy(aatip, addr.IP[:])
assert.Equal(t, e.IP, aatip)
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])
testserdes.EncodeDecodeBinary(t, addr, new(AddressAndTime))
}
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)
}