neoneo-go/pkg/network/message_test.go

69 lines
1.6 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
2018-02-01 18:54:23 +00:00
"github.com/CityOfZion/neo-go/pkg/network/payload"
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(12227, 2000, "/neo:2.6.0/", 0, true)
2018-01-27 15:00:28 +00:00
m := newMessage(ModeTestNet, cmdVersion, p)
2018-01-26 18:04:13 +00:00
buf := new(bytes.Buffer)
2018-01-26 18:04:13 +00:00
if err := m.encode(buf); err != nil {
t.Error(err)
}
mDecode := &Message{}
if err := mDecode.decode(buf); err != nil {
2018-01-26 18:04:13 +00:00
t.Fatal(err)
}
if !reflect.DeepEqual(m, mDecode) {
t.Fatalf("expected both messages to be equal %v and %v", m, mDecode)
}
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) {
p := payload.NewVersion(1111, 3000, "/NEO:2.6.0/", 0, true)
m := newMessage(ModeTestNet, cmdVersion, p)
2018-01-27 15:47:43 +00:00
m.Checksum = 1337
2018-01-27 15:00:28 +00:00
buf := new(bytes.Buffer)
2018-01-27 15:47:43 +00:00
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 && err != errChecksumMismatch {
t.Fatalf("decode should fail with %s", errChecksumMismatch)
2018-01-27 15:47:43 +00:00
}
}