neoneo-go/pkg/network/payload/addr_test.go

57 lines
1.2 KiB
Go
Raw Normal View History

2018-01-27 15:00:28 +00:00
package payload
import (
2018-01-28 15:18:48 +00:00
"bytes"
"fmt"
"reflect"
2018-01-27 15:00:28 +00:00
"testing"
2018-01-29 18:17:49 +00:00
"github.com/anthdm/neo-go/pkg/util"
2018-01-27 15:00:28 +00:00
)
2018-01-28 15:18:48 +00:00
func TestEncodeDecodeAddr(t *testing.T) {
2018-01-29 18:17:49 +00:00
e, err := util.EndpointFromString("127.0.0.1:2000")
2018-01-28 15:18:48 +00:00
if err != nil {
t.Fatal(err)
}
addr := NewAddrWithTime(e)
buf := new(bytes.Buffer)
if err := addr.EncodeBinary(buf); err != nil {
t.Fatal(err)
}
addrDecode := &AddrWithTime{}
if err := addrDecode.DecodeBinary(buf); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(addr, addrDecode) {
t.Fatalf("expected both addr payloads to be equal: %v and %v", addr, addrDecode)
}
}
func TestEncodeDecodeAddressList(t *testing.T) {
var lenList uint8 = 4
addrs := make([]*AddrWithTime, lenList)
for i := 0; i < int(lenList); i++ {
2018-01-29 18:17:49 +00:00
e, _ := util.EndpointFromString(fmt.Sprintf("127.0.0.1:200%d", i))
2018-01-28 15:18:48 +00:00
addrs[i] = NewAddrWithTime(e)
}
buf := new(bytes.Buffer)
addrList := &AddressList{addrs}
if err := addrList.EncodeBinary(buf); err != nil {
t.Fatal(err)
}
addrListDecode := &AddressList{}
if err := addrListDecode.DecodeBinary(buf); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(addrList, addrListDecode) {
t.Fatalf("expected both address list payloads to be equal: %v and %v", addrList, addrListDecode)
}
2018-01-27 15:00:28 +00:00
}