-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiretypes_test.go
276 lines (227 loc) · 5.48 KB
/
wiretypes_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package mq
import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"reflect"
"strings"
"testing"
)
func Test_firstByte(t *testing.T) {
cases := []struct {
h byte
exp string
}{
{PUBLISH | QoS2 | RETAIN, "PUBLISH -2-r"},
{PUBLISH | QoS3, "PUBLISH -!!-"},
{PUBLISH | DUP | QoS2, "PUBLISH d2--"},
{PUBLISH | QoS1, "PUBLISH --1-"},
{CONNECT, "CONNECT ----"},
}
for _, c := range cases {
if got := firstByte(c.h).String(); got != c.exp {
t.Errorf("String: %q != %q", got, c.exp)
}
}
}
func Test_bits(t *testing.T) {
v := bits(0b0001_0000)
switch {
case !v.Has(0b0001_0000):
t.Error("!Has")
case v.Has(0b0000_0001):
t.Error("Has")
}
var r brokenRW
if _, err := v.ReadFrom(&r); err == nil {
t.Error("expected error")
}
}
func Test_wuint16(t *testing.T) {
b := wuint16(76)
data := make([]byte, b.width())
b.fill(data, 0)
if exp := []byte{0, 76}; !reflect.DeepEqual(data, exp) {
t.Error("unexpected data ", data)
}
var a wuint16
if err := a.UnmarshalBinary(data); err != nil {
t.Error("UnmarshalBinary", err)
}
// before and after are equal
if b != a {
t.Errorf("b(%v) != a(%v)", b, a)
}
if got := a.width(); got != 2 {
t.Error("invalid wuint16 width", got)
}
}
func Test_wuint32(t *testing.T) {
b := wuint32(76)
data := make([]byte, b.width())
b.fill(data, 0)
if exp := []byte{0, 0, 0, 76}; !reflect.DeepEqual(data, exp) {
t.Error("unexpected data ", data)
}
var a wuint32
if err := a.UnmarshalBinary(data); err != nil {
t.Error("UnmarshalBinary", err)
}
// before and after are equal
if b != a {
t.Errorf("b(%v) != a(%v)", b, a)
}
}
func Test_vbint(t *testing.T) {
cases := []struct {
x vbint
exp []byte
}{
{0, []byte{0x00}},
{127, []byte{0x7f}},
{128, []byte{0x80, 0x01}},
{16_383, []byte{0xff, 0x7f}},
{148, []byte{0x94, 0x01}},
{16_384, []byte{0x80, 0x80, 0x01}},
{2_097_151, []byte{0xff, 0xff, 0x7f}},
{2_097_152, []byte{0x80, 0x80, 0x80, 0x01}},
{268_435_455, []byte{0xff, 0xff, 0xff, 0x7f}},
}
for _, c := range cases {
data := make([]byte, c.x.fill(_LEN, 0))
c.x.fill(data, 0)
if !reflect.DeepEqual(data, c.exp) {
t.Log("got", hex.Dump(data))
t.Error("exp", hex.Dump(c.exp))
}
var after vbint
if err := after.UnmarshalBinary(data); err != nil {
t.Error("Unmarshal", data)
}
if after != c.x {
t.Errorf("%v != %v", c.x, after)
}
var afterR vbint
if _, err := afterR.ReadFrom(bytes.NewReader(data)); err != nil {
t.Error(err)
}
if afterR != c.x {
t.Errorf("%v != %v", c.x, afterR)
}
}
var empty vbint
if v := empty.fillProp(nil, 0, 0); v > 0 {
t.Error("empty fill", v)
}
// error case
var v vbint
badData := []byte{0xff, 0xff, 0xff, 0xff, 0x7f}
if err := v.UnmarshalBinary(badData); err == nil {
t.Error("UnmarshalBinary should fail", badData)
}
if err := v.UnmarshalBinary(nil); err == nil {
t.Error("UnmarshalBinary should fail on empty")
}
var r brokenRW
if _, err := v.ReadFrom(&r); err == nil {
t.Error("expected error")
}
large := []byte{0xff, 0xff, 0xff, 0xff, 0xff}
if _, err := v.ReadFrom(bytes.NewReader(large)); err == nil {
t.Error("expected error")
}
}
func Test_wbool(t *testing.T) {
var b wbool // false
data := make([]byte, b.width())
b.fill(data, 0)
var a wbool
if err := a.UnmarshalBinary(data); err != nil {
t.Error("UnmarshalBinary", err)
}
// before and after are equal
if b != a {
t.Errorf("b(%v) != a(%v)", b, a)
}
// error case
data[0] = 3
if err := a.UnmarshalBinary(data); err == nil {
t.Error("UnmarshalBinary should fail")
}
}
func Test_bindata(t *testing.T) {
indata := make([]byte, 64)
if _, err := rand.Read(indata); err != nil {
t.Fatal(err)
}
b := bindata(indata)
data := make([]byte, b.width())
b.fill(data, 0)
var a bindata
if err := a.UnmarshalBinary(data); err != nil {
t.Error("UnmarshalBinary", err)
}
if err := a.UnmarshalBinary([]byte{0x00, 0x00}); err != nil {
t.Error("UnmarshalBinary", err)
}
// before and after are equal
if !reflect.DeepEqual(b, a) {
t.Error("unmarshal -> marshal should be equal", len(b), len(a))
}
// error case
if err := a.UnmarshalBinary(data[:len(data)-4]); err == nil {
t.Error("UnmarshalBinary should fail")
}
}
func Test_UserProp(t *testing.T) {
b := UserProp{"key", "value"}
data := make([]byte, b.width())
b.fill(data, 0)
var a UserProp
if err := a.UnmarshalBinary(data); err != nil {
t.Error("UnmarshalBinary", err, data)
}
// before and after are equal
if !reflect.DeepEqual(b, a) {
t.Error("unmarshal -> marshal should be equal", b, a)
}
if got, exp := a.String(), "key:value"; got != exp {
t.Errorf("%q != %q", got, exp)
}
empty := UserProp{"", "value"} // missing key
if v := empty.fillProp(nil, 0, 0); v > 0 {
t.Error("empty fillProp", v)
}
// error cases
if err := a.UnmarshalBinary(data[:3]); err == nil {
t.Error("UnmarshalBinary should fail on malformed key")
}
if err := a.UnmarshalBinary(data[:7]); err == nil {
t.Error("UnmarshalBinary should fail on malformed value")
}
}
func Test_rawdata(t *testing.T) {
defer func() {
if e := recover(); e == nil {
t.Error("expect panic")
}
}()
var v rawdata
v.fillProp(nil, 0, 0)
}
func Test_Ident(t *testing.T) {
empty := Ident(9)
if v := empty.fillProp(nil, 0, 0); v > 0 {
t.Error("empty fillProp", v)
}
}
var large = wstring(strings.Repeat(" ", maxUint16+1))
type brokenRW struct{}
func (w *brokenRW) Write(data []byte) (int, error) {
return 0, broken
}
func (w *brokenRW) Read(data []byte) (int, error) {
return 0, broken
}
var broken = fmt.Errorf("broken")