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