2016-06-27 16:22:01 -04:00
|
|
|
package kcp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-02-10 09:02:28 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
. "github.com/v2fly/v2ray-core/v5/transport/internet/kcp"
|
2016-06-27 16:22:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBadSegment(t *testing.T) {
|
|
|
|
seg, buf := ReadSegment(nil)
|
2019-02-10 09:02:28 -05:00
|
|
|
if seg != nil {
|
|
|
|
t.Error("non-nil seg")
|
|
|
|
}
|
|
|
|
if len(buf) != 0 {
|
|
|
|
t.Error("buf len: ", len(buf))
|
|
|
|
}
|
2016-06-27 16:22:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDataSegment(t *testing.T) {
|
|
|
|
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
|
|
|
|
|
|
|
iseg, _ := ReadSegment(bytes)
|
|
|
|
seg2 := iseg.(*DataSegment)
|
2019-02-10 09:02:28 -05:00
|
|
|
if r := cmp.Diff(seg2, seg, cmpopts.IgnoreUnexported(DataSegment{})); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
|
|
|
if r := cmp.Diff(seg2.Data().Bytes(), seg.Data().Bytes()); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
2017-01-04 11:23:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test1ByteDataSegment(t *testing.T) {
|
|
|
|
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
|
|
|
|
|
|
|
iseg, _ := ReadSegment(bytes)
|
|
|
|
seg2 := iseg.(*DataSegment)
|
2019-02-10 09:02:28 -05:00
|
|
|
if r := cmp.Diff(seg2, seg, cmpopts.IgnoreUnexported(DataSegment{})); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
|
|
|
if r := cmp.Diff(seg2.Data().Bytes(), seg.Data().Bytes()); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
2016-06-27 16:22:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestACKSegment(t *testing.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,
|
2016-08-05 14:59:33 -04:00
|
|
|
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
|
|
|
|
|
|
|
iseg, _ := ReadSegment(bytes)
|
2016-07-02 16:17:41 -04:00
|
|
|
seg2 := iseg.(*AckSegment)
|
2019-02-10 09:02:28 -05:00
|
|
|
if r := cmp.Diff(seg2, seg); r != "" {
|
|
|
|
t.Error(r)
|
2016-06-27 16:22:01 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-11 05:44:30 -04:00
|
|
|
|
|
|
|
func TestCmdSegment(t *testing.T) {
|
|
|
|
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
|
|
|
|
|
|
|
iseg, _ := ReadSegment(bytes)
|
|
|
|
seg2 := iseg.(*CmdOnlySegment)
|
2019-02-10 09:02:28 -05:00
|
|
|
if r := cmp.Diff(seg2, seg); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
2016-10-11 05:44:30 -04:00
|
|
|
}
|