github.com/sagernet/netlink@v0.0.0-20240612041022-b9a21c07ac6a/tcp_linux_test.go (about)

     1  package netlink
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  var (
     9  	tcpInfoData []byte
    10  	tcpInfo     *TCPInfo
    11  )
    12  
    13  func init() {
    14  	tcpInfoData = []byte{
    15  		1, 0, 0, 0, 0, 7, 120, 1, 96, 216, 3, 0, 64,
    16  		156, 0, 0, 120, 5, 0, 0, 64, 3, 0, 0, 0, 0,
    17  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    18  		0, 0, 0, 236, 216, 0, 0, 0, 0, 0, 0, 56, 216,
    19  		0, 0, 144, 39, 0, 0, 220, 5, 0, 0, 88, 250,
    20  		0, 0, 79, 190, 0, 0, 7, 5, 0, 0, 255, 255,
    21  		255, 127, 10, 0, 0, 0, 168, 5, 0, 0, 3, 0, 0,
    22  		0, 0, 0, 0, 0, 144, 56, 0, 0, 0, 0, 0, 0, 1, 197,
    23  		8, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255,
    24  		255, 255, 157, 42, 0, 0, 0, 0, 0, 0, 148, 26, 0,
    25  		0, 0, 0, 0, 0, 181, 0, 0, 0, 95, 0, 0, 0, 0, 0, 0,
    26  		0, 93, 180, 0, 0, 61, 0, 0, 0, 89, 0, 0, 0, 47, 216,
    27  		1, 0, 0, 0, 0, 0, 32, 65, 23, 0, 0, 0, 0, 0, 0, 0,
    28  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 0, 0,
    29  		0, 0, 0, 0, 0, 156, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    30  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    31  		0, 195, 1, 0,
    32  	}
    33  	tcpInfo = &TCPInfo{
    34  		State:           1,
    35  		Options:         7,
    36  		Snd_wscale:      7,
    37  		Rcv_wscale:      8,
    38  		Rto:             252000,
    39  		Ato:             40000,
    40  		Snd_mss:         1400,
    41  		Rcv_mss:         832,
    42  		Last_data_sent:  55532,
    43  		Last_data_recv:  55352,
    44  		Last_ack_recv:   10128,
    45  		Pmtu:            1500,
    46  		Rcv_ssthresh:    64088,
    47  		Rtt:             48719,
    48  		Rttvar:          1287,
    49  		Snd_ssthresh:    2147483647,
    50  		Snd_cwnd:        10,
    51  		Advmss:          1448,
    52  		Reordering:      3,
    53  		Rcv_space:       14480,
    54  		Pacing_rate:     574721,
    55  		Max_pacing_rate: 18446744073709551615,
    56  		Bytes_acked:     10909,
    57  		Bytes_received:  6804,
    58  		Segs_out:        181,
    59  		Segs_in:         95,
    60  		Min_rtt:         46173,
    61  		Data_segs_in:    61,
    62  		Data_segs_out:   89,
    63  		Delivery_rate:   120879,
    64  		Busy_time:       1524000,
    65  		Delivered:       90,
    66  		Bytes_sent:      10908,
    67  		Snd_wnd:         115456,
    68  	}
    69  }
    70  
    71  func TestTCPInfoDeserialize(t *testing.T) {
    72  	tests := []struct {
    73  		name     string
    74  		input    []byte
    75  		expected *TCPInfo
    76  		wantFail bool
    77  	}{
    78  		{
    79  			name:     "Valid data",
    80  			input:    tcpInfoData,
    81  			expected: tcpInfo,
    82  		},
    83  	}
    84  
    85  	for _, test := range tests {
    86  		tcpbbr := &TCPInfo{}
    87  		err := tcpbbr.deserialize(test.input)
    88  		if err != nil && !test.wantFail {
    89  			t.Errorf("Unexpected failure for test %q", test.name)
    90  			continue
    91  		}
    92  
    93  		if err != nil && test.wantFail {
    94  			continue
    95  		}
    96  
    97  		if !reflect.DeepEqual(test.expected, tcpbbr) {
    98  			t.Errorf("Unexpected failure for test %q", test.name)
    99  		}
   100  	}
   101  }
   102  
   103  func TestTCPBBRInfoDeserialize(t *testing.T) {
   104  	tests := []struct {
   105  		name     string
   106  		input    []byte
   107  		expected *TCPBBRInfo
   108  		wantFail bool
   109  	}{
   110  		{
   111  			name: "Valid data",
   112  			input: []byte{
   113  				100, 0, 0, 0, 0, 0, 0, 0,
   114  				111, 0, 0, 0,
   115  				222, 0, 0, 0,
   116  				123, 0, 0, 0,
   117  			},
   118  			expected: &TCPBBRInfo{
   119  				BBRBW:         100,
   120  				BBRMinRTT:     111,
   121  				BBRPacingGain: 222,
   122  				BBRCwndGain:   123,
   123  			},
   124  		},
   125  		{
   126  			name: "Invalid length",
   127  			input: []byte{
   128  				100, 0, 0, 0, 0, 0, 0, 0,
   129  				111, 0, 0, 0,
   130  				222, 0, 0, 0,
   131  				123, 0, 0,
   132  			},
   133  			wantFail: true,
   134  		},
   135  	}
   136  
   137  	for _, test := range tests {
   138  		tcpbbr := &TCPBBRInfo{}
   139  		err := tcpbbr.deserialize(test.input)
   140  		if err != nil && !test.wantFail {
   141  			t.Errorf("Unexpected failure for test %q", test.name)
   142  			continue
   143  		}
   144  
   145  		if err != nil && test.wantFail {
   146  			continue
   147  		}
   148  
   149  		if !reflect.DeepEqual(test.expected, tcpbbr) {
   150  			t.Errorf("Unexpected failure for test %q", test.name)
   151  		}
   152  	}
   153  }