Skip to content

Commit 455234a

Browse files
committed
test zero-length packet
1 parent 3df1ee6 commit 455234a

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cover:
2121
$(GO) test github.com/yomorun/y3-codec-golang/pkg -coverprofile=prof.out && $(GO) tool cover -html=prof.out && rm prof.out
2222

2323
test:
24-
$(GO) test -v api.go api_test.go
24+
$(GO) test -v api.go api_test.go stream_api.go stream_api_test.go
2525

2626
test-spec:
2727
$(GO) test -v github.com/yomorun/y3-codec-golang/pkg/spec

stream_api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/yomorun/y3-codec-golang/pkg/spec"
99
)
1010

11+
// StreamDecoder decode Y3 Packet from a io.Reader
1112
type StreamDecoder struct {
1213
errState bool
1314
tagbuf []byte

stream_api_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package y3
22

33
import (
44
"bytes"
5-
"log"
65
"testing"
76

87
"github.com/yomorun/y3-codec-golang/pkg/spec"
@@ -105,30 +104,37 @@ func TestStreamDecode4(t *testing.T) {
105104
}
106105

107106
func TestStreamDecode5(t *testing.T) {
108-
data := []byte{0x01, 0x01, 0x01, 0x02, 0x02, 0x01, 0x02, 0x03, 0x03, 0x01, 0x02, 0x03}
107+
data := []byte{0x01, 0x01, 0x01, 0x02, 0x02, 0x01, 0x02, 0x03, 0x00, 0x04, 0x03, 0x01, 0x02, 0x03}
109108

110109
// as reader
111110
r := bytes.NewReader(data)
112111
// create steam decoder
113112
pr := NewStreamDecoder(r)
114113

115-
times := 0
114+
times := 1
116115

117116
// handler
118117
pr.OnPacket(func(p *spec.Packet) {
119-
log.Printf("==>OnPacket: %v", p)
120-
if times == 0 {
118+
if times == 1 {
121119
compareBytes(t, p.GetTagBuffer(), []byte{0x01}, "T")
122120
compareBytes(t, p.GetLengthBuffer(), []byte{0x01}, "L")
123121
compareBytes(t, p.GetValueBuffer(), []byte{0x01}, "V")
124122
}
125-
if times == 1 {
123+
if times == 2 {
126124
compareBytes(t, p.GetTagBuffer(), []byte{0x02}, "T")
127125
compareBytes(t, p.GetLengthBuffer(), []byte{0x02}, "L")
128126
compareBytes(t, p.GetValueBuffer(), []byte{0x01, 0x02}, "V")
129127
}
130-
if times == 2 {
128+
if times == 3 {
131129
compareBytes(t, p.GetTagBuffer(), []byte{0x03}, "T")
130+
compareBytes(t, p.GetLengthBuffer(), []byte{0x00}, "L")
131+
compareBytes(t, p.GetValueBuffer(), []byte{}, "V")
132+
if p.Length != 0 {
133+
t.Errorf("Packet:Tag=[% X] Length should be 0, actual=%d", p.GetTagBuffer(), p.Length)
134+
}
135+
}
136+
if times == 4 {
137+
compareBytes(t, p.GetTagBuffer(), []byte{0x04}, "T")
132138
compareBytes(t, p.GetLengthBuffer(), []byte{0x03}, "L")
133139
compareBytes(t, p.GetValueBuffer(), []byte{0x01, 0x02, 0x03}, "V")
134140
}

0 commit comments

Comments
 (0)