Skip to content

Commit

Permalink
test zero-length packet
Browse files Browse the repository at this point in the history
  • Loading branch information
fanweixiao committed Feb 21, 2021
1 parent 3df1ee6 commit 455234a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cover:
$(GO) test github.com/yomorun/y3-codec-golang/pkg -coverprofile=prof.out && $(GO) tool cover -html=prof.out && rm prof.out

test:
$(GO) test -v api.go api_test.go
$(GO) test -v api.go api_test.go stream_api.go stream_api_test.go

test-spec:
$(GO) test -v github.com/yomorun/y3-codec-golang/pkg/spec
1 change: 1 addition & 0 deletions stream_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/yomorun/y3-codec-golang/pkg/spec"
)

// StreamDecoder decode Y3 Packet from a io.Reader
type StreamDecoder struct {
errState bool
tagbuf []byte
Expand Down
20 changes: 13 additions & 7 deletions stream_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package y3

import (
"bytes"
"log"
"testing"

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

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

// as reader
r := bytes.NewReader(data)
// create steam decoder
pr := NewStreamDecoder(r)

times := 0
times := 1

// handler
pr.OnPacket(func(p *spec.Packet) {
log.Printf("==>OnPacket: %v", p)
if times == 0 {
if times == 1 {
compareBytes(t, p.GetTagBuffer(), []byte{0x01}, "T")
compareBytes(t, p.GetLengthBuffer(), []byte{0x01}, "L")
compareBytes(t, p.GetValueBuffer(), []byte{0x01}, "V")
}
if times == 1 {
if times == 2 {
compareBytes(t, p.GetTagBuffer(), []byte{0x02}, "T")
compareBytes(t, p.GetLengthBuffer(), []byte{0x02}, "L")
compareBytes(t, p.GetValueBuffer(), []byte{0x01, 0x02}, "V")
}
if times == 2 {
if times == 3 {
compareBytes(t, p.GetTagBuffer(), []byte{0x03}, "T")
compareBytes(t, p.GetLengthBuffer(), []byte{0x00}, "L")
compareBytes(t, p.GetValueBuffer(), []byte{}, "V")
if p.Length != 0 {
t.Errorf("Packet:Tag=[% X] Length should be 0, actual=%d", p.GetTagBuffer(), p.Length)
}
}
if times == 4 {
compareBytes(t, p.GetTagBuffer(), []byte{0x04}, "T")
compareBytes(t, p.GetLengthBuffer(), []byte{0x03}, "L")
compareBytes(t, p.GetValueBuffer(), []byte{0x01, 0x02, 0x03}, "V")
}
Expand Down

0 comments on commit 455234a

Please sign in to comment.