-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser_test.go
68 lines (53 loc) · 1.24 KB
/
parser_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
package y3
import (
"io"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStreamParser1(t *testing.T) {
data := []byte{0x01, 0x03, 0x01, 0x02, 0x03}
reader := &pr{buf: data}
p, err := ReadPacket(reader)
assert.NoError(t, err)
assert.Equal(t, data, p)
}
func TestStreamParser2(t *testing.T) {
data := []byte{0x01, 0x03, 0x01, 0x02, 0x03, 0x04}
reader := &pr{buf: data}
p, err := ReadPacket(reader)
assert.NoError(t, err)
assert.Equal(t, data[:5], p)
}
func TestStreamParser3(t *testing.T) {
data := []byte{0x01, 0x03, 0x01, 0x02}
reader := &pr{buf: data}
p, err := ReadPacket(reader)
assert.ErrorIs(t, err, ErrMalformed)
assert.Equal(t, []byte(nil), p)
}
func TestStreamParser4(t *testing.T) {
data := []byte{}
reader := &pr{buf: data}
p, err := ReadPacket(reader)
assert.ErrorIs(t, err, ErrMalformed)
assert.Equal(t, []byte(nil), p)
}
func TestStreamParser5(t *testing.T) {
data := []byte{0x01}
reader := &pr{buf: data}
p, err := ReadPacket(reader)
assert.ErrorIs(t, err, ErrMalformed)
assert.Equal(t, []byte(nil), p)
}
type pr struct {
buf []byte
off int
}
func (pr *pr) Read(buf []byte) (int, error) {
if pr.off >= len(pr.buf) {
return 0, io.EOF
}
copy(buf, []byte{pr.buf[pr.off]})
pr.off++
return 1, nil
}