v2ray-core/transport/internet/kcp/segment_test.go

131 lines
3.1 KiB
Go
Raw Normal View History

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