forked from perlin-network/noise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg_test.go
148 lines (117 loc) · 3.89 KB
/
msg_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package noise
import (
"github.com/perlin-network/noise/payload"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"testing"
"testing/quick"
)
func TestEncodeMessage(t *testing.T) {
resetOpcodes()
o := RegisterMessage(Opcode(123), (*testMsg)(nil))
assert.Equal(t, o, RegisterMessage(o, (*testMsg)(nil)))
p := newPeer(nil, nil)
f := func(msg testMsg) bool {
bytes, err := p.EncodeMessage(msg)
assert.Nil(t, err)
assert.Equal(t, append([]byte{byte(o)}, msg.Write()...), bytes)
return true
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func TestDecodeMessage(t *testing.T) {
resetOpcodes()
o := RegisterMessage(Opcode(45), (*testMsg)(nil))
assert.Equal(t, o, RegisterMessage(o, (*testMsg)(nil)))
p := newPeer(nil, nil)
f := func(msg testMsg) bool {
resultO, resultM, err := p.DecodeMessage(append([]byte{byte(o)}, msg.Write()...))
assert.Nil(t, err)
assert.Equal(t, o, resultO)
assert.Equal(t, msg, resultM)
return true
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func TestEncodeMessageError(t *testing.T) {
resetOpcodes()
var err error
p := newPeer(nil, nil)
// test encode before register opcode
_, err = p.EncodeMessage(testMsg{Text: "hello"})
assert.Error(t, err)
o := RegisterMessage(NextAvailableOpcode(), (*testMsg)(nil))
assert.Equal(t, o, RegisterMessage(o, (*testMsg)(nil)))
_, err = p.EncodeMessage(testMsg{Text: "hello"})
assert.Nil(t, err)
// test encode header error
var headerError = errors.New("encode header error")
p.OnEncodeHeader(func(node *Node, peer *Peer, header, msg []byte) (bytes []byte, e error) {
return nil, headerError
})
_, err = p.EncodeMessage(testMsg{Text: "hello"})
assert.Error(t, err)
assert.Equal(t, headerError, errors.Cause(err))
// test encode footer error
var footerError = errors.New("encode footer error")
p.OnEncodeFooter(func(node *Node, peer *Peer, header, msg []byte) (bytes []byte, e error) {
return nil, footerError
})
_, err = p.EncodeMessage(testMsg{Text: "hello"})
assert.Error(t, err)
assert.Equal(t, footerError, errors.Cause(err))
}
func TestDecodeMessageError(t *testing.T) {
resetOpcodes()
p := newPeer(nil, nil)
testMessage := testMsg{Text: "hello"}
var o = NextAvailableOpcode()
var resultO Opcode
var resultM Message
var err error
// test decode before register opcode
resultO, resultM, err = p.DecodeMessage(append([]byte{byte(o)}, testMessage.Write()...))
assert.Equal(t, o, resultO)
assert.Nil(t, resultM)
assert.Error(t, err)
assert.Equal(t, o, RegisterMessage(o, (*testMsg)(nil)))
// test decode invalid opcode
resultO, resultM, err = p.DecodeMessage(append([]byte{222}, testMessage.Write()...))
assert.Equal(t, Opcode(byte(222)), resultO)
assert.Nil(t, resultM)
assert.Error(t, err)
// test decode nil
resultO, resultM, err = p.DecodeMessage(nil)
assert.Equal(t, OpcodeNil, resultO)
assert.Equal(t, nil, resultM)
assert.Error(t, err)
// test decode with nil content
resultO, resultM, err = p.DecodeMessage(append([]byte{byte(o)}))
assert.Equal(t, o, resultO)
assert.Nil(t, resultM)
assert.Error(t, err)
// test encode header error
var headerError = errors.New("decode header error")
p.OnDecodeHeader(func(node *Node, peer *Peer, reader payload.Reader) error {
return headerError
})
resultO, resultM, err = p.DecodeMessage(append([]byte{byte(o)}, testMessage.Write()...))
assert.Equal(t, OpcodeNil, resultO)
assert.Equal(t, nil, resultM)
assert.Error(t, err)
assert.Equal(t, headerError, errors.Cause(err))
// test encode footer error
var footerError = errors.New("decode footer error")
p.OnDecodeFooter(func(node *Node, peer *Peer, msg []byte, reader payload.Reader) error {
return footerError
})
resultO, resultM, err = p.DecodeMessage(append([]byte{byte(o)}, testMessage.Write()...))
assert.Equal(t, OpcodeNil, resultO)
assert.Equal(t, nil, resultM)
assert.Error(t, err)
assert.Equal(t, footerError, errors.Cause(err))
}