github.com/imannamdari/v2ray-core/v5@v5.0.5/transport/internet/kcp/segment_test.go (about) 1 package kcp_test 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/google/go-cmp/cmp/cmpopts" 8 9 . "github.com/imannamdari/v2ray-core/v5/transport/internet/kcp" 10 ) 11 12 func TestBadSegment(t *testing.T) { 13 seg, buf := ReadSegment(nil) 14 if seg != nil { 15 t.Error("non-nil seg") 16 } 17 if len(buf) != 0 { 18 t.Error("buf len: ", len(buf)) 19 } 20 } 21 22 func TestDataSegment(t *testing.T) { 23 seg := &DataSegment{ 24 Conv: 1, 25 Timestamp: 3, 26 Number: 4, 27 SendingNext: 5, 28 } 29 seg.Data().Write([]byte{'a', 'b', 'c', 'd'}) 30 31 nBytes := seg.ByteSize() 32 bytes := make([]byte, nBytes) 33 seg.Serialize(bytes) 34 35 iseg, _ := ReadSegment(bytes) 36 seg2 := iseg.(*DataSegment) 37 if r := cmp.Diff(seg2, seg, cmpopts.IgnoreUnexported(DataSegment{})); r != "" { 38 t.Error(r) 39 } 40 if r := cmp.Diff(seg2.Data().Bytes(), seg.Data().Bytes()); r != "" { 41 t.Error(r) 42 } 43 } 44 45 func Test1ByteDataSegment(t *testing.T) { 46 seg := &DataSegment{ 47 Conv: 1, 48 Timestamp: 3, 49 Number: 4, 50 SendingNext: 5, 51 } 52 seg.Data().WriteByte('a') 53 54 nBytes := seg.ByteSize() 55 bytes := make([]byte, nBytes) 56 seg.Serialize(bytes) 57 58 iseg, _ := ReadSegment(bytes) 59 seg2 := iseg.(*DataSegment) 60 if r := cmp.Diff(seg2, seg, cmpopts.IgnoreUnexported(DataSegment{})); r != "" { 61 t.Error(r) 62 } 63 if r := cmp.Diff(seg2.Data().Bytes(), seg.Data().Bytes()); r != "" { 64 t.Error(r) 65 } 66 } 67 68 func TestACKSegment(t *testing.T) { 69 seg := &AckSegment{ 70 Conv: 1, 71 ReceivingWindow: 2, 72 ReceivingNext: 3, 73 Timestamp: 10, 74 NumberList: []uint32{1, 3, 5, 7, 9}, 75 } 76 77 nBytes := seg.ByteSize() 78 bytes := make([]byte, nBytes) 79 seg.Serialize(bytes) 80 81 iseg, _ := ReadSegment(bytes) 82 seg2 := iseg.(*AckSegment) 83 if r := cmp.Diff(seg2, seg); r != "" { 84 t.Error(r) 85 } 86 } 87 88 func TestCmdSegment(t *testing.T) { 89 seg := &CmdOnlySegment{ 90 Conv: 1, 91 Cmd: CommandPing, 92 Option: SegmentOptionClose, 93 SendingNext: 11, 94 ReceivingNext: 13, 95 PeerRTO: 15, 96 } 97 98 nBytes := seg.ByteSize() 99 bytes := make([]byte, nBytes) 100 seg.Serialize(bytes) 101 102 iseg, _ := ReadSegment(bytes) 103 seg2 := iseg.(*CmdOnlySegment) 104 if r := cmp.Diff(seg2, seg); r != "" { 105 t.Error(r) 106 } 107 }