41 lines
610 B
Go
41 lines
610 B
Go
package node
|
|
|
|
import (
|
|
"net/netip"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestBinWriteRead(t *testing.T) {
|
|
buf := make([]byte, 1024)
|
|
|
|
type Item struct {
|
|
Type byte
|
|
TraceID uint64
|
|
DestAddr netip.AddrPort
|
|
}
|
|
|
|
in := Item{1, 2, netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 22)}
|
|
|
|
buf = newBinWriter(buf).
|
|
Byte(in.Type).
|
|
Uint64(in.TraceID).
|
|
AddrPort(in.DestAddr).
|
|
Build()
|
|
|
|
out := Item{}
|
|
|
|
err := newBinReader(buf).
|
|
Byte(&out.Type).
|
|
Uint64(&out.TraceID).
|
|
AddrPort(&out.DestAddr).
|
|
Error()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(in, out) {
|
|
t.Fatal(in, out)
|
|
}
|
|
}
|