github.com/castai/kvisor@v1.7.1-0.20240516114728-b3572a2607b5/pkg/net/packet/socks5_test.go (about) 1 package packet 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 var socks5ClientRequestData = []byte{ 10 0x05, 0x02, 0x00, 0x01, 11 } 12 13 var socks5ServerMethodSelectData = []byte{ 14 0x05, 0x00, 15 } 16 17 var socks5ClientConnectRequestIPv4Data = []byte{ 18 0x05, 19 0x01, // connect 20 0x00, 21 0x01, // IPv4 22 // IP data of 142.250.185.99 23 0x8e, 0xfa, 0xb9, 0x63, 24 // Port 80 25 0x00, 0x50, 26 } 27 28 var socks5ClientConnectRequestIPv6Data = []byte{ 29 0x05, 30 0x01, // connect 31 0x00, 32 0x04, // IPv6 33 // IP data of 2001:db8::68 34 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 35 // Port 80 36 0x00, 0x50, 37 } 38 39 var socks5ClientConnectRequestDomainNameData = []byte{ 40 0x05, 41 0x01, // connect 42 0x00, 43 0x03, // Domainname 44 0x07, 'c', 'a', 's', 't', '.', 'a', 'i', 45 // Port 80 46 0x00, 0x50, 47 } 48 49 var socks5ServerConnectResponseIPv4Data = []byte{ 50 0x05, 51 0x00, // success 52 0x00, 53 0x01, //IPv4 54 // IP data of 10.244.0.22 55 0x0a, 0xf4, 0x00, 0x16, 56 // Port 52330 57 0xcc, 0x6a, 58 } 59 60 var socks5ServerConnectResponseIPv6Data = []byte{ 61 0x05, 62 0x00, // success 63 0x00, 64 0x04, //IPv6 65 // IP data of fe80::0202:b3ff:fe1e:8329 66 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0xb3, 0xff, 0xfe, 0x1e, 0x83, 0x29, 67 // Port 52330 68 0xcc, 0x6a, 69 } 70 71 func TestParseSOCKS5(t *testing.T) { 72 type testCase struct { 73 title string 74 payload []byte 75 expectedResult SOCKS5Message 76 expectedError error 77 } 78 79 testCases := []testCase{ 80 { 81 title: "parse client request data message", 82 payload: socks5ClientRequestData, 83 expectedResult: SOCKS5InitialClientRequest{}, 84 }, 85 { 86 title: "parse server method select message", 87 payload: socks5ServerMethodSelectData, 88 expectedResult: SOCKS5InitialServerResponse{}, 89 }, 90 { 91 title: "parse client connect ipv4 request", 92 payload: socks5ClientConnectRequestIPv4Data, 93 expectedResult: SOCKS5RequestOrReply{ 94 CmdOrReply: 0x01, 95 AddressType: SOCKS5AddressTypeIPv4, 96 Address: []byte{0x8e, 0xfa, 0xb9, 0x63}, 97 Port: 80, 98 }, 99 }, 100 { 101 title: "parse client connect ipv6 request", 102 payload: socks5ClientConnectRequestIPv6Data, 103 expectedResult: SOCKS5RequestOrReply{ 104 CmdOrReply: 0x01, 105 AddressType: SOCKS5AddressTypeIPv6, 106 Address: []byte{0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68}, 107 Port: 80, 108 }, 109 }, 110 { 111 title: "parse client connect domain name request", 112 payload: socks5ClientConnectRequestDomainNameData, 113 expectedResult: SOCKS5RequestOrReply{ 114 CmdOrReply: 0x01, 115 AddressType: SOCKS5AddressTypeDomainName, 116 Address: []byte("cast.ai"), 117 Port: 80, 118 }, 119 }, 120 { 121 title: "parse server connect ipv4 response", 122 payload: socks5ServerConnectResponseIPv4Data, 123 expectedResult: SOCKS5RequestOrReply{ 124 CmdOrReply: 0x00, 125 AddressType: SOCKS5AddressTypeIPv4, 126 Address: []byte{0x0a, 0xf4, 0x00, 0x16}, 127 Port: 52330, 128 }, 129 }, 130 { 131 title: "parse server connect ipv6 response", 132 payload: socks5ServerConnectResponseIPv6Data, 133 expectedResult: SOCKS5RequestOrReply{ 134 CmdOrReply: 0x00, 135 AddressType: SOCKS5AddressTypeIPv6, 136 Address: []byte{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0xb3, 0xff, 0xfe, 0x1e, 0x83, 0x29}, 137 Port: 52330, 138 }, 139 }, 140 { 141 title: "should fail for payload not starting with 5", 142 payload: []byte{0x01, 0x00}, 143 expectedError: ErrSOCKS5InvalidVersion, 144 }, 145 { 146 title: "should fail when providing invalid address type", 147 payload: []byte{0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 148 expectedError: ErrSOCKS5InvalidAddressType, 149 }, 150 { 151 title: "should fail when providing invalid message", 152 payload: []byte{0x05, 0x01, 0x04, 0x01}, 153 expectedError: ErrSOCKS5InvalidMessage, 154 }, 155 } 156 157 for _, test := range testCases { 158 t.Run(test.title, func(t *testing.T) { 159 r := require.New(t) 160 msg, err := ParseSOCKS5(test.payload) 161 if test.expectedError != nil { 162 r.Error(err) 163 r.ErrorIs(err, test.expectedError) 164 return 165 } 166 167 r.NoError(err) 168 r.Equal(test.expectedResult, msg) 169 }) 170 } 171 }