1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 09:26:21 -05:00
v2fly/transport/internet/kcp/segment_test.go
2018-11-14 22:55:20 +01:00

126 lines
2.9 KiB
Go

package kcp_test
import (
"testing"
. "v2ray.com/core/transport/internet/kcp"
. "v2ray.com/ext/assert"
)
func TestBadSegment(t *testing.T) {
assert := With(t)
seg, buf := ReadSegment(nil)
assert(seg, IsNil)
assert(len(buf), Equals, 0)
}
func TestDataSegment(t *testing.T) {
assert := With(t)
seg := &DataSegment{
Conv: 1,
Timestamp: 3,
Number: 4,
SendingNext: 5,
}
seg.Data().Write([]byte{'a', 'b', 'c', 'd'})
nBytes := seg.ByteSize()
bytes := make([]byte, nBytes)
seg.Serialize(bytes)
assert(int32(len(bytes)), Equals, nBytes)
iseg, _ := ReadSegment(bytes)
seg2 := iseg.(*DataSegment)
assert(seg2.Conv, Equals, seg.Conv)
assert(seg2.Timestamp, Equals, seg.Timestamp)
assert(seg2.SendingNext, Equals, seg.SendingNext)
assert(seg2.Number, Equals, seg.Number)
assert(seg2.Data().Bytes(), Equals, seg.Data().Bytes())
}
func Test1ByteDataSegment(t *testing.T) {
assert := With(t)
seg := &DataSegment{
Conv: 1,
Timestamp: 3,
Number: 4,
SendingNext: 5,
}
seg.Data().WriteByte('a')
nBytes := seg.ByteSize()
bytes := make([]byte, nBytes)
seg.Serialize(bytes)
assert(int32(len(bytes)), Equals, nBytes)
iseg, _ := ReadSegment(bytes)
seg2 := iseg.(*DataSegment)
assert(seg2.Conv, Equals, seg.Conv)
assert(seg2.Timestamp, Equals, seg.Timestamp)
assert(seg2.SendingNext, Equals, seg.SendingNext)
assert(seg2.Number, Equals, seg.Number)
assert(seg2.Data().Bytes(), Equals, seg.Data().Bytes())
}
func TestACKSegment(t *testing.T) {
assert := With(t)
seg := &AckSegment{
Conv: 1,
ReceivingWindow: 2,
ReceivingNext: 3,
Timestamp: 10,
NumberList: []uint32{1, 3, 5, 7, 9},
}
nBytes := seg.ByteSize()
bytes := make([]byte, nBytes)
seg.Serialize(bytes)
assert(int32(len(bytes)), Equals, nBytes)
iseg, _ := ReadSegment(bytes)
seg2 := iseg.(*AckSegment)
assert(seg2.Conv, Equals, seg.Conv)
assert(seg2.ReceivingWindow, Equals, seg.ReceivingWindow)
assert(seg2.ReceivingNext, Equals, seg.ReceivingNext)
assert(len(seg2.NumberList), Equals, len(seg.NumberList))
assert(seg2.Timestamp, Equals, seg.Timestamp)
for i, number := range seg2.NumberList {
assert(number, Equals, seg.NumberList[i])
}
}
func TestCmdSegment(t *testing.T) {
assert := With(t)
seg := &CmdOnlySegment{
Conv: 1,
Cmd: CommandPing,
Option: SegmentOptionClose,
SendingNext: 11,
ReceivingNext: 13,
PeerRTO: 15,
}
nBytes := seg.ByteSize()
bytes := make([]byte, nBytes)
seg.Serialize(bytes)
assert(int32(len(bytes)), Equals, nBytes)
iseg, _ := ReadSegment(bytes)
seg2 := iseg.(*CmdOnlySegment)
assert(seg2.Conv, Equals, seg.Conv)
assert(byte(seg2.Command()), Equals, byte(seg.Command()))
assert(byte(seg2.Option), Equals, byte(seg.Option))
assert(seg2.SendingNext, Equals, seg.SendingNext)
assert(seg2.ReceivingNext, Equals, seg.ReceivingNext)
assert(seg2.PeerRTO, Equals, seg.PeerRTO)
}