aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/message_test.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2014-11-04 20:21:44 +0800
committerFelix Lange <fjl@twurst.com>2014-11-22 04:52:45 +0800
commitf38052c499c1fee61423efeddb1f52677f1442e9 (patch)
tree6cc4c4e9739d61edeba9dc62781b2ebdeb0faf11 /p2p/message_test.go
parent8cf9ed0ea588e97f2baf0f834248727e8fbca18f (diff)
downloadgo-tangerine-f38052c499c1fee61423efeddb1f52677f1442e9.tar
go-tangerine-f38052c499c1fee61423efeddb1f52677f1442e9.tar.gz
go-tangerine-f38052c499c1fee61423efeddb1f52677f1442e9.tar.bz2
go-tangerine-f38052c499c1fee61423efeddb1f52677f1442e9.tar.lz
go-tangerine-f38052c499c1fee61423efeddb1f52677f1442e9.tar.xz
go-tangerine-f38052c499c1fee61423efeddb1f52677f1442e9.tar.zst
go-tangerine-f38052c499c1fee61423efeddb1f52677f1442e9.zip
p2p: rework protocol API
Diffstat (limited to 'p2p/message_test.go')
-rw-r--r--p2p/message_test.go75
1 files changed, 52 insertions, 23 deletions
diff --git a/p2p/message_test.go b/p2p/message_test.go
index e9d46f2c3..1edabc4e7 100644
--- a/p2p/message_test.go
+++ b/p2p/message_test.go
@@ -1,38 +1,67 @@
package p2p
import (
+ "bytes"
+ "io/ioutil"
"testing"
+
+ "github.com/ethereum/go-ethereum/ethutil"
)
func TestNewMsg(t *testing.T) {
- msg, _ := NewMsg(3, 1, "000")
- if msg.Code() != 3 {
- t.Errorf("incorrect code %v", msg.Code())
+ msg := NewMsg(3, 1, "000")
+ if msg.Code != 3 {
+ t.Errorf("incorrect code %d, want %d", msg.Code)
}
- data0 := msg.Data().Get(0).Uint()
- data1 := string(msg.Data().Get(1).Bytes())
- if data0 != 1 {
- t.Errorf("incorrect data %v", data0)
+ if msg.Size != 5 {
+ t.Errorf("incorrect size %d, want %d", msg.Size, 5)
}
- if data1 != "000" {
- t.Errorf("incorrect data %v", data1)
+ pl, _ := ioutil.ReadAll(msg.Payload)
+ expect := []byte{0x01, 0x83, 0x30, 0x30, 0x30}
+ if !bytes.Equal(pl, expect) {
+ t.Errorf("incorrect payload content, got %x, want %x", pl, expect)
}
}
func TestEncodeDecodeMsg(t *testing.T) {
- msg, _ := NewMsg(3, 1, "000")
- encoded := msg.Encode(3)
- msg, _ = NewMsgFromBytes(encoded)
- msg.Decode(3)
- if msg.Code() != 3 {
- t.Errorf("incorrect code %v", msg.Code())
- }
- data0 := msg.Data().Get(0).Uint()
- data1 := msg.Data().Get(1).Str()
- if data0 != 1 {
- t.Errorf("incorrect data %v", data0)
- }
- if data1 != "000" {
- t.Errorf("incorrect data %v", data1)
+ msg := NewMsg(3, 1, "000")
+ buf := new(bytes.Buffer)
+ if err := writeMsg(buf, msg); err != nil {
+ t.Fatalf("encodeMsg error: %v", err)
+ }
+
+ t.Logf("encoded: %x", buf.Bytes())
+
+ decmsg, err := readMsg(buf)
+ if err != nil {
+ t.Fatalf("readMsg error: %v", err)
+ }
+ if decmsg.Code != 3 {
+ t.Errorf("incorrect code %d, want %d", decmsg.Code, 3)
+ }
+ if decmsg.Size != 5 {
+ t.Errorf("incorrect size %d, want %d", decmsg.Size, 5)
+ }
+ data, err := decmsg.Data()
+ if err != nil {
+ t.Fatalf("first payload item decode error: %v", err)
+ }
+ if v := data.Get(0).Uint(); v != 1 {
+ t.Errorf("incorrect data[0]: got %v, expected %d", v, 1)
+ }
+ if v := data.Get(1).Str(); v != "000" {
+ t.Errorf("incorrect data[1]: got %q, expected %q", v, "000")
+ }
+}
+
+func TestDecodeRealMsg(t *testing.T) {
+ data := ethutil.Hex2Bytes("2240089100000080f87e8002b5457468657265756d282b2b292f5065657220536572766572204f6e652f76302e372e382f52656c656173652f4c696e75782f672b2bc082765fb84086dd80b7aefd6a6d2e3b93f4f300a86bfb6ef7bdc97cb03f793db6bb")
+ msg, err := readMsg(bytes.NewReader(data))
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+
+ if msg.Code != 0 {
+ t.Errorf("incorrect code %d, want %d", msg.Code, 0)
}
}