-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobs_test.go
396 lines (363 loc) · 10.6 KB
/
cobs_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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package cobs
import (
"bytes"
"io"
"testing"
)
var testCases = []struct {
name string
dec, enc []byte
}{
{
name: "Empty",
dec: []byte{},
enc: []byte{0x01},
},
{
name: "1 character",
dec: []byte{'1'},
enc: []byte{0x02, '1'},
},
{
name: "1 zero",
dec: []byte{0x00},
enc: []byte{0x01, 0x01},
},
{
name: "2 zeroes",
dec: []byte{0x00, 0x00},
enc: []byte{0x01, 0x01, 0x01},
},
{
name: "3 zeroes",
dec: []byte{0x00, 0x00, 0x00},
enc: []byte{0x01, 0x01, 0x01, 0x01},
},
{
name: "5 characters",
dec: []byte("12345"),
enc: []byte("\x0612345"),
},
{
name: "Embedded zero",
dec: []byte("12345\x006789"),
enc: []byte("\x0612345\x056789"),
},
{
name: "Starting and embedded zero",
dec: []byte("\x0012345\x006789"),
enc: []byte("\x01\x0612345\x056789"),
},
{
name: "Embedded and trailing zero",
dec: []byte("12345\x006789\x00"),
enc: []byte("\x0612345\x056789\x01"),
},
{
name: "253 non-zero bytes",
dec: []byte("0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEF" +
"GHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdef" +
"ghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst012345" +
"6789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst123"),
enc: []byte("\xfe0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789AB" +
"CDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTab" +
"cdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst01" +
"23456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst123"),
},
{
name: "254 non-zero bytes",
dec: []byte("0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEF" +
"GHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdef" +
"ghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst012345" +
"6789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst1234"),
enc: []byte("\xff0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789AB" +
"CDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTab" +
"cdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst01" +
"23456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst1234"),
},
{
name: "255 non-zero bytes",
dec: []byte("0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEF" +
"GHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdef" +
"ghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst012345" +
"6789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst12345"),
enc: []byte("\xff0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789AB" +
"CDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTab" +
"cdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst01" +
"23456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst1234\x025"),
},
{
name: "zero followed by 255 non-zero bytes",
dec: []byte("\x000123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789AB" +
"CDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTab" +
"cdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst01" +
"23456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst12345"),
enc: []byte("\x01\xff0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst01234567" +
"89ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQR" +
"STabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqr" +
"st0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst1234\x025"),
},
{
name: "253 non-zero bytes followed by zero",
dec: []byte("0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEF" +
"GHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdef" +
"ghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst012345" +
"6789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst123\x00"),
enc: []byte("\xfe0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789AB" +
"CDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTab" +
"cdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst01" +
"23456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst123\x01"),
},
{
name: "254 non-zero bytes followed by zero",
dec: []byte("0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEF" +
"GHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdef" +
"ghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst012345" +
"6789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst1234\x00"),
enc: []byte("\xff0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789AB" +
"CDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTab" +
"cdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst01" +
"23456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst1234\x01\x01"),
},
{
name: "255 non-zero bytes followed by zero",
dec: []byte("0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEF" +
"GHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdef" +
"ghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst012345" +
"6789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst12345\x00"),
enc: []byte("\xff0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789AB" +
"CDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTab" +
"cdefghijklmnopqrst0123456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst01" +
"23456789ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst1234\x025\x01"),
},
}
func TestEncode(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
enc, err := Encode(tc.dec)
if err != nil {
t.Errorf("encode error: %v", err)
}
if !bytes.Equal(enc, tc.enc) {
t.Errorf("got %v, want %v", enc, tc.enc)
}
})
}
}
func TestDecode(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
dec, err := Decode(tc.enc)
if err != nil {
t.Errorf("decode error: %v", err)
}
if !bytes.Equal(dec, tc.dec) {
t.Errorf("got %v, want %v", dec, tc.dec)
}
})
}
}
func TestWriter(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
buf := bytes.NewBuffer(make([]byte, 0, len(tc.enc)))
d := NewDecoder(buf)
e := NewEncoder(d)
n, err := e.Write(tc.dec)
if err != nil {
t.Errorf("writer error: %v", err)
}
err = e.Close()
if err != nil {
t.Errorf("writer close error: %v", err)
}
if d.NeedsMoreData() {
t.Error("writer incomplete decode data")
}
if n != len(tc.dec) {
t.Errorf("writer length got %d, want %d", n, len(tc.dec))
}
if !bytes.Equal(buf.Bytes(), tc.dec) {
t.Errorf("got %v, want %v", buf.Bytes(), tc.dec)
}
})
}
}
func TestStream(t *testing.T) {
pr, pw := io.Pipe()
go func() {
defer pw.Close()
e := NewEncoder(pw)
for _, tc := range testCases {
_, err := e.Write(tc.dec)
if err != nil {
t.Errorf("stream encode error: %v", err)
}
err = e.Close()
if err != nil {
t.Errorf("stream close error: %v", err)
}
_, err = pw.Write([]byte{Delimiter})
if err != nil {
t.Errorf("stream delimiter error: %v", err)
}
}
}()
var buf bytes.Buffer
d := NewDecoder(&buf)
for _, tc := range testCases {
_, err := io.Copy(d, pr)
if err != EOD {
t.Error("stream decode EOD missing")
}
if d.NeedsMoreData() {
t.Error("stream decode frame incomplete")
}
if !bytes.Equal(buf.Bytes(), tc.dec) {
t.Errorf("stream decode got %v, want %v", buf.Bytes(), tc.dec)
}
buf.Reset()
t.Logf("stream decode %s", tc.name)
}
}
func FuzzEncodeDecode(f *testing.F) {
for _, tc := range testCases {
f.Add(tc.dec)
}
f.Fuzz(func(t *testing.T, a []byte) {
enc, err := Encode(a)
if err != nil {
t.Errorf("fuzz encode error: %v", err)
}
if i := bytes.IndexByte(enc, Delimiter); i != -1 {
t.Errorf("fuzz encode %v has delimiter at %d", enc, i)
}
dec, err := Decode(enc)
if err != nil {
t.Errorf("fuzz decode error: %v", err)
}
if !bytes.Equal(dec, a) {
t.Errorf("fuzz decode got %v want %v", dec, a)
}
})
}
func FuzzChainWriter(f *testing.F) {
for _, tc := range testCases {
f.Add(tc.dec)
}
f.Fuzz(func(t *testing.T, a []byte) {
var buf bytes.Buffer
d := NewDecoder(&buf)
e := NewEncoder(d)
n, err := e.Write(a)
if err != nil {
t.Errorf("fuzz chain error: %v", err)
}
if n != len(a) {
t.Errorf("fuzz chain length got %d want %d", n, len(a))
}
err = e.Close()
if err != nil {
t.Errorf("fuzz chain close error: %v", err)
}
if d.NeedsMoreData() {
t.Error("fuzz chain incomplete decode data")
}
if !bytes.Equal(buf.Bytes(), a) {
t.Errorf("fuzz chain got %v want %v", buf.Bytes(), a)
}
})
}
var incompleteFrame = []struct {
name string
data []byte
}{
{
name: "Missing single byte",
data: []byte{0x02},
},
{
name: "2 zeroes and missing end",
data: []byte{0x01, 0x01, 0x05},
},
}
func TestDecodeIncomplete(t *testing.T) {
for _, tc := range incompleteFrame {
t.Run(tc.name, func(t *testing.T) {
_, err := Decode(tc.data)
if err != ErrIncompleteFrame {
t.Errorf("Unexpected decode incomplete error: %v", err)
}
})
}
}
var unexpectedDelimiter = []struct {
name string
data []byte
}{
{
name: "Missing byte before delimiter",
data: []byte{0x02, 0x00},
},
{
name: "Unexpected embedded zero",
data: []byte("\x061234\x005\x056789"),
},
}
func TestDecodeUnexpectedEOD(t *testing.T) {
for _, tc := range unexpectedDelimiter {
t.Run(tc.name, func(t *testing.T) {
_, err := Decode(tc.data)
if err != ErrUnexpectedEOD {
t.Errorf("Unexpected decode EOD error: %v", err)
}
})
}
}
// https://github.com/golang/go/issues/54111
type LimitedWriter struct {
W io.Writer // underlying writer
N int64 // max bytes remaining
Err error // error to be returned once limit is reached
}
func (lw *LimitedWriter) Write(p []byte) (int, error) {
if lw.N < 1 {
return 0, lw.Err
}
if lw.N < int64(len(p)) {
p = p[:lw.N]
}
n, err := lw.W.Write(p)
lw.N -= int64(n)
return n, err
}
func TestEncodeError(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
e := NewEncoder(&LimitedWriter{io.Discard, 0, io.EOF})
_, err := e.Write(tc.dec)
// err can be nil if no groups have been flushed, call close
if err == nil {
err = e.Close()
}
if err != io.EOF {
t.Errorf("Unexpected error: %v", err)
}
})
}
}
func TestDecodeError(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// The empty string is expected to have no writes
if len(tc.dec) == 0 {
t.SkipNow()
}
d := NewDecoder(&LimitedWriter{io.Discard, 0, io.EOF})
_, err := d.Write(tc.enc)
if err != io.EOF {
t.Errorf("Unexpected error: %v", err)
}
})
}
}