1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-07 02:38:10 -05:00
v2fly/transport/internet/kcp/segment_test.go

126 lines
2.9 KiB
Go
Raw Normal View History

2016-06-27 16:22:01 -04:00
package kcp_test
import (
"testing"
2016-08-20 14:55:45 -04:00
. "v2ray.com/core/transport/internet/kcp"
2017-10-26 15:44:22 -04:00
. "v2ray.com/ext/assert"
2016-06-27 16:22:01 -04:00
)
func TestBadSegment(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-06-27 16:22:01 -04:00
seg, buf := ReadSegment(nil)
2017-10-24 10:15:35 -04:00
assert(seg, IsNil)
assert(len(buf), Equals, 0)
2016-06-27 16:22:01 -04:00
}
func TestDataSegment(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-06-27 16:22:01 -04:00
seg := &DataSegment{
2016-06-29 04:34:34 -04:00
Conv: 1,
2017-01-04 11:23:41 -05:00
Timestamp: 3,
Number: 4,
SendingNext: 5,
}
2018-04-19 16:56:55 -04:00
seg.Data().Write([]byte{'a', 'b', 'c', 'd'})
2017-01-04 11:23:41 -05:00
nBytes := seg.ByteSize()
bytes := make([]byte, nBytes)
2018-11-02 16:34:04 -04:00
seg.Serialize(bytes)
2017-01-04 11:23:41 -05:00
2018-04-02 14:00:50 -04:00
assert(int32(len(bytes)), Equals, nBytes)
2017-01-04 11:23:41 -05:00
iseg, _ := ReadSegment(bytes)
seg2 := iseg.(*DataSegment)
2017-10-24 10:15:35 -04:00
assert(seg2.Conv, Equals, seg.Conv)
assert(seg2.Timestamp, Equals, seg.Timestamp)
assert(seg2.SendingNext, Equals, seg.SendingNext)
assert(seg2.Number, Equals, seg.Number)
2017-12-03 16:53:00 -05:00
assert(seg2.Data().Bytes(), Equals, seg.Data().Bytes())
2017-01-04 11:23:41 -05:00
}
func Test1ByteDataSegment(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-01-04 11:23:41 -05:00
seg := &DataSegment{
Conv: 1,
2016-06-29 04:34:34 -04:00
Timestamp: 3,
Number: 4,
SendingNext: 5,
2016-06-27 16:22:01 -04:00
}
2018-11-14 16:55:20 -05:00
seg.Data().WriteByte('a')
2016-06-27 16:22:01 -04:00
nBytes := seg.ByteSize()
2016-12-06 05:03:42 -05:00
bytes := make([]byte, nBytes)
2018-11-02 16:34:04 -04:00
seg.Serialize(bytes)
2016-06-27 16:22:01 -04:00
2018-04-02 14:00:50 -04:00
assert(int32(len(bytes)), Equals, nBytes)
2016-06-27 16:22:01 -04:00
iseg, _ := ReadSegment(bytes)
seg2 := iseg.(*DataSegment)
2017-10-24 10:15:35 -04:00
assert(seg2.Conv, Equals, seg.Conv)
assert(seg2.Timestamp, Equals, seg.Timestamp)
assert(seg2.SendingNext, Equals, seg.SendingNext)
assert(seg2.Number, Equals, seg.Number)
2017-12-03 16:53:00 -05:00
assert(seg2.Data().Bytes(), Equals, seg.Data().Bytes())
2016-06-27 16:22:01 -04:00
}
func TestACKSegment(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-06-27 16:22:01 -04:00
2016-07-02 16:17:41 -04:00
seg := &AckSegment{
2016-06-27 16:22:01 -04:00
Conv: 1,
ReceivingWindow: 2,
2016-06-29 04:34:34 -04:00
ReceivingNext: 3,
Timestamp: 10,
2016-06-27 16:22:01 -04:00
NumberList: []uint32{1, 3, 5, 7, 9},
}
nBytes := seg.ByteSize()
2016-12-06 05:03:42 -05:00
bytes := make([]byte, nBytes)
2018-11-02 16:34:04 -04:00
seg.Serialize(bytes)
2016-06-27 16:22:01 -04:00
2018-04-02 14:00:50 -04:00
assert(int32(len(bytes)), Equals, nBytes)
2016-06-27 16:22:01 -04:00
iseg, _ := ReadSegment(bytes)
2016-07-02 16:17:41 -04:00
seg2 := iseg.(*AckSegment)
2017-10-24 10:15:35 -04:00
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)
2016-12-21 09:37:16 -05:00
for i, number := range seg2.NumberList {
2017-10-24 10:15:35 -04:00
assert(number, Equals, seg.NumberList[i])
2016-06-27 16:22:01 -04:00
}
}
2016-10-11 05:44:30 -04:00
func TestCmdSegment(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-10-11 05:44:30 -04:00
seg := &CmdOnlySegment{
2018-01-17 11:36:14 -05:00
Conv: 1,
Cmd: CommandPing,
Option: SegmentOptionClose,
SendingNext: 11,
ReceivingNext: 13,
PeerRTO: 15,
2016-10-11 05:44:30 -04:00
}
nBytes := seg.ByteSize()
2016-12-06 05:03:42 -05:00
bytes := make([]byte, nBytes)
2018-11-02 16:34:04 -04:00
seg.Serialize(bytes)
2016-10-11 05:44:30 -04:00
2018-04-02 14:00:50 -04:00
assert(int32(len(bytes)), Equals, nBytes)
2016-10-11 05:44:30 -04:00
iseg, _ := ReadSegment(bytes)
seg2 := iseg.(*CmdOnlySegment)
2017-10-24 10:15:35 -04:00
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)
2018-01-17 11:36:14 -05:00
assert(seg2.ReceivingNext, Equals, seg.ReceivingNext)
2017-10-24 10:15:35 -04:00
assert(seg2.PeerRTO, Equals, seg.PeerRTO)
2016-10-11 05:44:30 -04:00
}