2018-03-09 15:55:25 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2019-09-09 14:54:38 +00:00
|
|
|
"net"
|
2018-03-09 15:55:25 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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)
|
2018-03-09 15:55:25 +00:00
|
|
|
buf = new(bytes.Buffer)
|
|
|
|
)
|
|
|
|
|
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-08-29 14:33:53 +00:00
|
|
|
err := addr.EncodeBinary(buf)
|
|
|
|
assert.Nil(t, err)
|
2018-03-09 15:55:25 +00:00
|
|
|
|
|
|
|
addrDecode := &AddressAndTime{}
|
2019-08-29 14:33:53 +00:00
|
|
|
err = addrDecode.DecodeBinary(buf)
|
|
|
|
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
|
|
|
|
addrList := &AddressList{make([]*AddressAndTime, lenList)}
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2019-08-29 14:33:53 +00:00
|
|
|
err := addrList.EncodeBinary(buf)
|
|
|
|
assert.Nil(t, err)
|
2018-03-09 15:55:25 +00:00
|
|
|
|
|
|
|
addrListDecode := &AddressList{}
|
2019-08-29 14:33:53 +00:00
|
|
|
err = addrListDecode.DecodeBinary(buf)
|
|
|
|
assert.Nil(t, err)
|
2018-03-09 15:55:25 +00:00
|
|
|
|
|
|
|
assert.Equal(t, addrList, addrListDecode)
|
|
|
|
}
|