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

131 lines
2.9 KiB
Go
Raw Normal View History

2016-06-27 16:22:01 -04:00
package kcp_test
import (
"testing"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
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
2016-12-09 06:08:25 -05:00
b := buf.NewLocal(512)
2016-12-06 05:03:42 -05:00
b.Append([]byte{'a', 'b', 'c', 'd'})
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,
Data: b,
}
nBytes := seg.ByteSize()
bytes := make([]byte, nBytes)
seg.Bytes()(bytes)
2017-10-24 10:15:35 -04:00
assert(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)
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
b := buf.NewLocal(512)
b.AppendBytes('a')
seg := &DataSegment{
Conv: 1,
2016-06-29 04:34:34 -04:00
Timestamp: 3,
Number: 4,
SendingNext: 5,
2016-12-06 05:03:42 -05:00
Data: b,
2016-06-27 16:22:01 -04:00
}
nBytes := seg.ByteSize()
2016-12-06 05:03:42 -05:00
bytes := make([]byte, nBytes)
seg.Bytes()(bytes)
2016-06-27 16:22:01 -04:00
2017-10-24 10:15:35 -04:00
assert(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)
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)
seg.Bytes()(bytes)
2016-06-27 16:22:01 -04:00
2017-10-24 10:15:35 -04:00
assert(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{
Conv: 1,
2016-12-08 10:27:41 -05:00
Cmd: CommandPing,
2016-10-11 05:44:30 -04:00
Option: SegmentOptionClose,
SendingNext: 11,
ReceivinNext: 13,
PeerRTO: 15,
}
nBytes := seg.ByteSize()
2016-12-06 05:03:42 -05:00
bytes := make([]byte, nBytes)
seg.Bytes()(bytes)
2016-10-11 05:44:30 -04:00
2017-10-24 10:15:35 -04:00
assert(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)
assert(seg2.ReceivinNext, Equals, seg.ReceivinNext)
assert(seg2.PeerRTO, Equals, seg.PeerRTO)
2016-10-11 05:44:30 -04:00
}