1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-31 00:08:08 -04:00
v2fly/transport/internet/kcp/segment_test.go

72 lines
1.7 KiB
Go
Raw Normal View History

2016-06-27 16:22:01 -04:00
package kcp_test
import (
"testing"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/testing/assert"
. "github.com/v2ray/v2ray-core/transport/internet/kcp"
)
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)
seg := &DataSegment{
2016-06-29 04:34:34 -04:00
Conv: 1,
Timestamp: 3,
Number: 4,
SendingNext: 5,
2016-07-28 10:24:15 -04:00
Data: alloc.NewLocalBuffer(512).Clear().Append([]byte{'a', 'b', 'c', 'd'}),
2016-06-27 16:22:01 -04:00
}
nBytes := seg.ByteSize()
bytes := seg.Bytes(nil)
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 04:34:34 -04:00
assert.Uint32(seg2.SendingNext).Equals(seg.SendingNext)
2016-06-27 16:22:01 -04:00
assert.Uint32(seg2.Number).Equals(seg.Number)
assert.Bytes(seg2.Data.Value).Equals(seg.Data.Value)
}
func TestACKSegment(t *testing.T) {
assert := assert.On(t)
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
Count: 5,
NumberList: []uint32{1, 3, 5, 7, 9},
}
nBytes := seg.ByteSize()
bytes := seg.Bytes(nil)
assert.Int(len(bytes)).Equals(nBytes)
iseg, _ := ReadSegment(bytes)
2016-07-02 16:17:41 -04:00
seg2 := iseg.(*AckSegment)
2016-06-27 16:22:01 -04:00
assert.Uint16(seg2.Conv).Equals(seg.Conv)
assert.Uint32(seg2.ReceivingWindow).Equals(seg.ReceivingWindow)
2016-06-29 04:34:34 -04:00
assert.Uint32(seg2.ReceivingNext).Equals(seg.ReceivingNext)
2016-06-27 16:22:01 -04:00
assert.Byte(seg2.Count).Equals(seg.Count)
assert.Uint32(seg2.Timestamp).Equals(seg.Timestamp)
2016-06-27 16:22:01 -04:00
for i := byte(0); i < seg2.Count; i++ {
assert.Uint32(seg2.NumberList[i]).Equals(seg.NumberList[i])
}
}