You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/kcp/segment_test.go

131 lines
2.9 KiB

9 years ago
package kcp_test
import (
"testing"
"v2ray.com/core/common/buf"
. "v2ray.com/ext/assert"
. "v2ray.com/core/transport/internet/kcp"
9 years ago
)
func TestBadSegment(t *testing.T) {
assert := With(t)
9 years ago
seg, buf := ReadSegment(nil)
assert(seg, IsNil)
assert(len(buf), Equals, 0)
9 years ago
}
func TestDataSegment(t *testing.T) {
assert := With(t)
9 years ago
8 years ago
b := buf.NewLocal(512)
b.Append([]byte{'a', 'b', 'c', 'd'})
9 years ago
seg := &DataSegment{
Conv: 1,
8 years ago
Timestamp: 3,
Number: 4,
SendingNext: 5,
Data: b,
}
nBytes := seg.ByteSize()
bytes := make([]byte, nBytes)
seg.Bytes()(bytes)
assert(len(bytes), Equals, nBytes)
8 years ago
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())
8 years ago
}
func Test1ByteDataSegment(t *testing.T) {
assert := With(t)
8 years ago
b := buf.NewLocal(512)
b.AppendBytes('a')
seg := &DataSegment{
Conv: 1,
Timestamp: 3,
Number: 4,
SendingNext: 5,
Data: b,
9 years ago
}
nBytes := seg.ByteSize()
bytes := make([]byte, nBytes)
seg.Bytes()(bytes)
9 years ago
assert(len(bytes), Equals, nBytes)
9 years ago
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())
9 years ago
}
func TestACKSegment(t *testing.T) {
assert := With(t)
9 years ago
seg := &AckSegment{
9 years ago
Conv: 1,
ReceivingWindow: 2,
ReceivingNext: 3,
Timestamp: 10,
9 years ago
NumberList: []uint32{1, 3, 5, 7, 9},
}
nBytes := seg.ByteSize()
bytes := make([]byte, nBytes)
seg.Bytes()(bytes)
9 years ago
assert(len(bytes), Equals, nBytes)
9 years ago
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])
9 years ago
}
}
func TestCmdSegment(t *testing.T) {
assert := With(t)
seg := &CmdOnlySegment{
Conv: 1,
Cmd: CommandPing,
Option: SegmentOptionClose,
SendingNext: 11,
ReceivinNext: 13,
PeerRTO: 15,
}
nBytes := seg.ByteSize()
bytes := make([]byte, nBytes)
seg.Bytes()(bytes)
assert(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.ReceivinNext, Equals, seg.ReceivinNext)
assert(seg2.PeerRTO, Equals, seg.PeerRTO)
}