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"
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
addr = NewAddressAndTime(e, ts)
|
2019-09-16 09:18:13 +00:00
|
|
|
buf = io.NewBufBinWriter()
|
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)
|
|
|
|
assert.Equal(t, e.Port, int(addr.Port))
|
2019-09-16 09:18:13 +00:00
|
|
|
err := addr.EncodeBinary(buf.BinWriter)
|
2019-08-29 14:33:53 +00:00
|
|
|
assert.Nil(t, err)
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
b := buf.Bytes()
|
|
|
|
r := io.NewBinReaderFromBuf(b)
|
2018-03-09 15:55:25 +00:00
|
|
|
addrDecode := &AddressAndTime{}
|
2019-09-16 09:18:13 +00:00
|
|
|
err = addrDecode.DecodeBinary(r)
|
2019-08-29 14:33:53 +00:00
|
|
|
assert.Nil(t, err)
|
2018-03-09 15:55:25 +00:00
|
|
|
|
|
|
|
assert.Equal(t, addr, addrDecode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncodeDecodeAddressList(t *testing.T) {
|
|
|
|
var lenList uint8 = 4
|
2019-09-13 17:38:34 +00:00
|
|
|
addrList := NewAddressList(int(lenList))
|
2018-03-09 15:55:25 +00:00
|
|
|
for i := 0; i < int(lenList); i++ {
|
2019-09-09 14:54:38 +00:00
|
|
|
e, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:200%d", i))
|
2018-03-09 15:55:25 +00:00
|
|
|
addrList.Addrs[i] = NewAddressAndTime(e, time.Now())
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
err := addrList.EncodeBinary(buf.BinWriter)
|
2019-08-29 14:33:53 +00:00
|
|
|
assert.Nil(t, err)
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
b := buf.Bytes()
|
|
|
|
r := io.NewBinReaderFromBuf(b)
|
2018-03-09 15:55:25 +00:00
|
|
|
addrListDecode := &AddressList{}
|
2019-09-16 09:18:13 +00:00
|
|
|
err = addrListDecode.DecodeBinary(r)
|
2019-08-29 14:33:53 +00:00
|
|
|
assert.Nil(t, err)
|
2018-03-09 15:55:25 +00:00
|
|
|
|
|
|
|
assert.Equal(t, addrList, addrListDecode)
|
|
|
|
}
|