From 78f87c4f6012709b25702f8f108ec263eaa432cb Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 11 Oct 2016 11:44:30 +0200 Subject: [PATCH] test case for cmd only segment --- transport/internet/kcp/segment.go | 2 +- transport/internet/kcp/segment_test.go | 27 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/transport/internet/kcp/segment.go b/transport/internet/kcp/segment.go index 11482fc5..7d31d951 100644 --- a/transport/internet/kcp/segment.go +++ b/transport/internet/kcp/segment.go @@ -149,7 +149,7 @@ func (this *CmdOnlySegment) Release() { } func ReadSegment(buf []byte) (Segment, []byte) { - if len(buf) <= 4 { + if len(buf) < 4 { return nil, nil } diff --git a/transport/internet/kcp/segment_test.go b/transport/internet/kcp/segment_test.go index 6e63c773..c071cdb5 100644 --- a/transport/internet/kcp/segment_test.go +++ b/transport/internet/kcp/segment_test.go @@ -69,3 +69,30 @@ func TestACKSegment(t *testing.T) { assert.Uint32(seg2.NumberList[i]).Equals(seg.NumberList[i]) } } + +func TestCmdSegment(t *testing.T) { + assert := assert.On(t) + + seg := &CmdOnlySegment{ + Conv: 1, + Command: CommandPing, + Option: SegmentOptionClose, + SendingNext: 11, + ReceivinNext: 13, + PeerRTO: 15, + } + + nBytes := seg.ByteSize() + bytes := seg.Bytes(nil) + + assert.Int(len(bytes)).Equals(nBytes) + + iseg, _ := ReadSegment(bytes) + seg2 := iseg.(*CmdOnlySegment) + assert.Uint16(seg2.Conv).Equals(seg.Conv) + assert.Byte(byte(seg2.Command)).Equals(byte(seg.Command)) + assert.Byte(byte(seg2.Option)).Equals(byte(seg.Option)) + assert.Uint32(seg2.SendingNext).Equals(seg.SendingNext) + assert.Uint32(seg2.ReceivinNext).Equals(seg.ReceivinNext) + assert.Uint32(seg2.PeerRTO).Equals(seg.PeerRTO) +}