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