neo-go/pkg/network/message_test.go

102 lines
2.3 KiB
Go
Raw Normal View History

2018-01-26 18:04:13 +00:00
package network
import (
"bytes"
"reflect"
"testing"
2018-01-27 15:00:28 +00:00
"github.com/anthdm/neo-go/pkg/network/payload"
2018-01-26 18:04:13 +00:00
)
2018-01-27 15:00:28 +00:00
// func TestNewMessage(t *testing.T) {
// payload := []byte{}
// m := newMessage(ModeTestNet, cmdVersion, payload)
2018-01-26 18:04:13 +00:00
2018-01-27 15:00:28 +00:00
// if have, want := m.Length, uint32(0); want != have {
// t.Errorf("want %d have %d", want, have)
// }
// if have, want := len(m.Command), 12; want != have {
// t.Errorf("want %d have %d", want, have)
// }
2018-01-26 18:04:13 +00:00
2018-01-27 15:00:28 +00:00
// sum := sumSHA256(sumSHA256(payload))[:4]
// sumuint32 := binary.LittleEndian.Uint32(sum)
// if have, want := m.Checksum, sumuint32; want != have {
// t.Errorf("want %d have %d", want, have)
// }
// }
2018-01-26 18:04:13 +00:00
func TestMessageEncodeDecode(t *testing.T) {
2018-01-27 15:00:28 +00:00
m := newMessage(ModeTestNet, cmdVersion, nil)
2018-01-26 18:04:13 +00:00
buf := &bytes.Buffer{}
if err := m.encode(buf); err != nil {
t.Error(err)
}
2018-01-26 20:43:07 +00:00
if n := len(buf.Bytes()); n < minMessageSize {
t.Fatalf("message should be at least %d bytes got %d", minMessageSize, n)
}
if n := len(buf.Bytes()); n > minMessageSize {
t.Fatalf("message without a payload should be exact %d bytes got %d", minMessageSize, n)
}
2018-01-26 18:04:13 +00:00
md := &Message{}
if err := md.decode(buf); err != nil {
t.Error(err)
}
if !reflect.DeepEqual(m, md) {
t.Errorf("both messages should be equal: %v != %v", m, md)
}
}
2018-01-27 15:00:28 +00:00
func TestMessageEncodeDecodeWithVersion(t *testing.T) {
p := payload.NewVersion(2000, "/neo/", 0, true)
m := newMessage(ModeTestNet, cmdVersion, p)
2018-01-26 18:04:13 +00:00
buf := &bytes.Buffer{}
if err := m.encode(buf); err != nil {
t.Error(err)
}
2018-01-27 15:00:28 +00:00
t.Log(buf.Len())
2018-01-26 18:04:13 +00:00
2018-01-27 15:00:28 +00:00
m1 := &Message{}
if err := m1.decode(buf); err != nil {
2018-01-26 18:04:13 +00:00
t.Fatal(err)
}
2018-01-27 15:00:28 +00:00
p1 := m1.Payload.(*payload.Version)
2018-01-26 18:04:13 +00:00
2018-01-27 15:00:28 +00:00
t.Log(p1)
2018-01-26 18:04:13 +00:00
}
2018-01-27 15:00:28 +00:00
2018-01-27 15:47:43 +00:00
func TestMessageInvalidChecksum(t *testing.T) {
m := newMessage(ModeTestNet, cmdVersion, nil)
m.Checksum = 1337
2018-01-27 15:00:28 +00:00
2018-01-27 15:47:43 +00:00
buf := &bytes.Buffer{}
if err := m.encode(buf); err != nil {
t.Error(err)
}
2018-01-27 15:00:28 +00:00
2018-01-27 15:47:43 +00:00
md := &Message{}
if err := md.decode(buf); err == nil {
t.Error("decode should failed with checkum mismatch error")
}
}
2018-01-27 15:00:28 +00:00
// func TestNewVersionPayload(t *testing.T) {
// ua := "/neo/0.0.1/"
// p := newVersionPayload(3000, ua, 0, true)
// b, err := p.encode()
// if err != nil {
// t.Fatal(err)
// }
// pd := &Version{}
// if err := pd.decode(b); err != nil {
// t.Fatal(err)
// }
// if !reflect.DeepEqual(p, pd) {
// t.Errorf("both payloads should be equal: %v != %v", p, pd)
// }
// }