github.com/sagernet/netlink@v0.0.0-20240612041022-b9a21c07ac6a/socket_linux_test.go (about) 1 package netlink 2 3 import ( 4 "reflect" 5 "syscall" 6 "testing" 7 ) 8 9 func TestAttrsToInetDiagTCPInfoResp(t *testing.T) { 10 tests := []struct { 11 name string 12 attrs []syscall.NetlinkRouteAttr 13 expected *InetDiagTCPInfoResp 14 wantFail bool 15 }{ 16 { 17 name: "Empty", 18 attrs: []syscall.NetlinkRouteAttr{}, 19 expected: &InetDiagTCPInfoResp{}, 20 }, 21 { 22 name: "BBRInfo Only", 23 attrs: []syscall.NetlinkRouteAttr{ 24 { 25 Attr: syscall.RtAttr{ 26 Len: 20, 27 Type: INET_DIAG_BBRINFO, 28 }, 29 Value: []byte{ 30 100, 0, 0, 0, 0, 0, 0, 0, 31 111, 0, 0, 0, 32 222, 0, 0, 0, 33 123, 0, 0, 0, 34 }, 35 }, 36 }, 37 expected: &InetDiagTCPInfoResp{ 38 TCPBBRInfo: &TCPBBRInfo{ 39 BBRBW: 100, 40 BBRMinRTT: 111, 41 BBRPacingGain: 222, 42 BBRCwndGain: 123, 43 }, 44 }, 45 }, 46 { 47 name: "TCPInfo Only", 48 attrs: []syscall.NetlinkRouteAttr{ 49 { 50 Attr: syscall.RtAttr{ 51 Len: 232, 52 Type: INET_DIAG_INFO, 53 }, 54 Value: tcpInfoData, 55 }, 56 }, 57 expected: &InetDiagTCPInfoResp{ 58 TCPInfo: tcpInfo, 59 }, 60 }, 61 { 62 name: "TCPInfo + TCPBBR", 63 attrs: []syscall.NetlinkRouteAttr{ 64 { 65 Attr: syscall.RtAttr{ 66 Len: 232, 67 Type: INET_DIAG_INFO, 68 }, 69 Value: tcpInfoData, 70 }, 71 { 72 Attr: syscall.RtAttr{ 73 Len: 20, 74 Type: INET_DIAG_BBRINFO, 75 }, 76 Value: []byte{ 77 100, 0, 0, 0, 0, 0, 0, 0, 78 111, 0, 0, 0, 79 222, 0, 0, 0, 80 123, 0, 0, 0, 81 }, 82 }, 83 }, 84 expected: &InetDiagTCPInfoResp{ 85 TCPInfo: tcpInfo, 86 TCPBBRInfo: &TCPBBRInfo{ 87 BBRBW: 100, 88 BBRMinRTT: 111, 89 BBRPacingGain: 222, 90 BBRCwndGain: 123, 91 }, 92 }, 93 }, 94 { 95 name: "TCPBBR + TCPInfo (reverse)", 96 attrs: []syscall.NetlinkRouteAttr{ 97 { 98 Attr: syscall.RtAttr{ 99 Len: 20, 100 Type: INET_DIAG_BBRINFO, 101 }, 102 Value: []byte{ 103 100, 0, 0, 0, 0, 0, 0, 0, 104 111, 0, 0, 0, 105 222, 0, 0, 0, 106 123, 0, 0, 0, 107 }, 108 }, 109 { 110 Attr: syscall.RtAttr{ 111 Len: 232, 112 Type: INET_DIAG_INFO, 113 }, 114 Value: tcpInfoData, 115 }, 116 }, 117 expected: &InetDiagTCPInfoResp{ 118 TCPInfo: tcpInfo, 119 TCPBBRInfo: &TCPBBRInfo{ 120 BBRBW: 100, 121 BBRMinRTT: 111, 122 BBRPacingGain: 222, 123 BBRCwndGain: 123, 124 }, 125 }, 126 }, 127 } 128 129 for _, test := range tests { 130 res, err := attrsToInetDiagTCPInfoResp(test.attrs, nil) 131 if err != nil && !test.wantFail { 132 t.Errorf("Unexpected failure for test %q", test.name) 133 continue 134 } 135 136 if err == nil && test.wantFail { 137 t.Errorf("Unexpected success for test %q", test.name) 138 continue 139 } 140 141 if !reflect.DeepEqual(test.expected, res) { 142 t.Errorf("Unexpected failure for test %q", test.name) 143 } 144 } 145 }