-
Notifications
You must be signed in to change notification settings - Fork 216
/
p2p_test.go
87 lines (67 loc) · 2.38 KB
/
p2p_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package eos
import (
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
)
func TestP2PMessage_UnmarshalBinaryRead(t *testing.T) {
hexString := `09000000050100000019000000`
decoded, err := hex.DecodeString(hexString)
if err != nil {
t.Error(err)
}
var s P2PMessageEnvelope
assert.NoError(t, UnmarshalBinary(decoded, &s))
assert.Equal(t, uint32(9), s.Length)
assert.Equal(t, P2PMessageType(5), s.Type)
assert.Equal(t, []byte{0x1, 0x0, 0x0, 0x0, 0x19, 0x0, 0x0, 0x0}, s.Payload)
}
func TestP2PMessage_DecodePayload(t *testing.T) {
hexString := `2100000002000000000000000000000000000000004016e1d216df26150000000000000000`
decoded, err := hex.DecodeString(hexString)
if err != nil {
t.Error(err)
}
var p2pMessage P2PMessageEnvelope
assert.NoError(t, UnmarshalBinary(decoded, &p2pMessage))
var timeMessage TimeMessage
assert.NoError(t, p2pMessage.DecodePayload(&timeMessage))
//todo : more assert
}
func TestP2PMessage_AsMessage(t *testing.T) {
hexString := `2100000002000000000000000000000000000000004016e1d216df26150000000000000000`
decoded, err := hex.DecodeString(hexString)
if err != nil {
t.Error(err)
}
var p2pMessage P2PMessageEnvelope
assert.NoError(t, UnmarshalBinary(decoded, &p2pMessage))
msg, err := p2pMessage.AsMessage()
assert.NoError(t, err)
assert.IsType(t, &TimeMessage{}, msg)
}
func TestMessageType_Name(t *testing.T) {
type Case struct {
Type P2PMessageType
ExpectedName interface{}
OK bool
}
cases := []Case{
{Type: HandshakeMessageType, ExpectedName: "Handshake", OK: true},
{Type: GoAwayMessageType, ExpectedName: "GoAway", OK: true},
{Type: TimeMessageType, ExpectedName: "Time", OK: true},
{Type: NoticeMessageType, ExpectedName: "Notice", OK: true},
{Type: RequestMessageType, ExpectedName: "Request", OK: true},
{Type: SyncRequestMessageType, ExpectedName: "SyncRequest", OK: true},
{Type: SignedBlockSummaryMessageType, ExpectedName: "SignedBlockSummary", OK: true},
{Type: SignedBlockMessageType, ExpectedName: "SignedBlock", OK: true},
{Type: SignedTransactionMessageType, ExpectedName: "SignedTransaction", OK: true},
{Type: PackedTransactionMessageType, ExpectedName: "PackedTransaction", OK: true},
{Type: P2PMessageType(100), ExpectedName: "Unknown", OK: false},
}
for _, c := range cases {
name, ok := c.Type.Name()
assert.Equal(t, c.OK, ok)
assert.Equal(t, c.ExpectedName, name)
}
}