1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/proxy/socks/protocol/socks_test.go

178 lines
4.3 KiB
Go
Raw Normal View History

package protocol
2015-09-07 11:28:36 -04:00
import (
2015-09-07 16:26:37 -04:00
"bytes"
2015-11-02 17:48:47 -05:00
"io"
2015-09-07 11:28:36 -04:00
"testing"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2016-12-09 06:08:25 -05:00
"v2ray.com/core/common/crypto"
2016-08-20 14:55:45 -04:00
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/testing/assert"
2015-09-07 11:28:36 -04:00
)
2015-09-16 11:29:00 -04:00
func TestHasAuthenticationMethod(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-09-16 11:29:00 -04:00
request := Socks5AuthenticationRequest{
version: socksVersion,
nMethods: byte(0x02),
authMethods: [256]byte{0x01, 0x02},
}
assert.Bool(request.HasAuthMethod(byte(0x01))).IsTrue()
request.authMethods[0] = byte(0x03)
assert.Bool(request.HasAuthMethod(byte(0x01))).IsFalse()
}
2015-09-07 16:26:37 -04:00
func TestAuthenticationRequestRead(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-12-09 06:08:25 -05:00
buffer := buf.New()
2016-12-06 05:03:42 -05:00
buffer.AppendBytes(
2015-09-07 16:26:37 -04:00
0x05, // version
0x01, // nMethods
0x02, // methods
2016-02-05 05:21:13 -05:00
)
request, _, err := ReadAuthentication(buffer)
2015-09-09 09:26:11 -04:00
assert.Error(err).IsNil()
2016-05-24 15:55:46 -04:00
assert.Byte(request.version).Equals(0x05)
assert.Byte(request.nMethods).Equals(0x01)
assert.Byte(request.authMethods[0]).Equals(0x02)
2015-09-07 16:26:37 -04:00
}
2015-09-16 11:29:00 -04:00
func TestAuthenticationResponseWrite(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-09-16 11:29:00 -04:00
response := NewAuthenticationResponse(byte(0x05))
2015-09-16 11:29:00 -04:00
buffer := bytes.NewBuffer(make([]byte, 0, 10))
2015-09-16 15:13:13 -04:00
WriteAuthentication(buffer, response)
2015-09-16 11:29:00 -04:00
assert.Bytes(buffer.Bytes()).Equals([]byte{socksVersion, byte(0x05)})
}
2015-09-07 16:26:37 -04:00
func TestRequestRead(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-09-07 16:26:37 -04:00
rawRequest := []byte{
0x05, // version
0x01, // cmd connect
0x00, // reserved
0x01, // ipv4 type
0x72, 0x72, 0x72, 0x72, // 114.114.114.114
0x00, 0x35, // port 53
}
request, err := ReadRequest(bytes.NewReader(rawRequest))
2015-09-09 09:26:11 -04:00
assert.Error(err).IsNil()
2016-05-24 15:55:46 -04:00
assert.Byte(request.Version).Equals(0x05)
assert.Byte(request.Command).Equals(0x01)
assert.Byte(request.AddrType).Equals(0x01)
assert.Bytes(request.IPv4[:]).Equals([]byte{0x72, 0x72, 0x72, 0x72})
assert.Port(request.Port).Equals(v2net.Port(53))
2015-09-07 16:26:37 -04:00
}
2015-10-08 17:06:12 -04:00
func TestResponseWrite(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
response := Socks5Response{
socksVersion,
ErrorSuccess,
AddrTypeIPv4,
[4]byte{0x72, 0x72, 0x72, 0x72},
"",
[16]byte{},
2015-12-02 15:44:01 -05:00
v2net.Port(53),
}
2016-12-09 06:08:25 -05:00
buffer := buf.NewLocal(2048)
2015-10-08 17:06:12 -04:00
defer buffer.Release()
response.Write(buffer)
expectedBytes := []byte{
socksVersion,
ErrorSuccess,
byte(0x00),
AddrTypeIPv4,
0x72, 0x72, 0x72, 0x72,
byte(0x00), byte(0x035),
}
2016-12-05 09:19:14 -05:00
assert.Bytes(buffer.Bytes()).Equals(expectedBytes)
}
2015-11-02 17:48:47 -05:00
2015-12-03 17:31:04 -05:00
func TestSetIPv6(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-12-03 17:31:04 -05:00
response := NewSocks5Response()
response.SetIPv6([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
2016-12-09 06:08:25 -05:00
buffer := buf.NewLocal(2048)
2015-12-03 17:31:04 -05:00
defer buffer.Release()
response.Write(buffer)
2016-12-05 09:19:14 -05:00
assert.Bytes(buffer.Bytes()).Equals([]byte{
2015-12-03 17:31:04 -05:00
socksVersion, 0, 0, AddrTypeIPv6, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 0})
}
func TestSetDomain(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-12-03 17:31:04 -05:00
response := NewSocks5Response()
response.SetDomain("v2ray.com")
2016-12-09 06:08:25 -05:00
buffer := buf.NewLocal(2048)
2015-12-03 17:31:04 -05:00
defer buffer.Release()
response.Write(buffer)
2016-12-05 09:19:14 -05:00
assert.Bytes(buffer.Bytes()).Equals([]byte{
2015-12-03 17:31:04 -05:00
socksVersion, 0, 0, AddrTypeDomain, 9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0})
}
2016-02-05 05:21:13 -05:00
func TestEmptyAuthRequest(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-11-02 17:48:47 -05:00
2016-12-09 06:08:25 -05:00
_, _, err := ReadAuthentication(buf.New())
2015-11-02 17:48:47 -05:00
assert.Error(err).Equals(io.EOF)
}
2016-02-05 05:21:13 -05:00
func TestSingleByteAuthRequest(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-11-02 17:48:47 -05:00
_, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 1)))
2016-11-19 08:38:13 -05:00
assert.Error(err).IsNotNil()
2015-11-02 17:48:47 -05:00
}
2016-02-05 05:21:13 -05:00
func TestZeroAuthenticationMethod(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-05 05:21:13 -05:00
2016-12-09 06:08:25 -05:00
buffer := buf.New()
2016-12-06 05:03:42 -05:00
buffer.AppendBytes(5, 0)
2016-02-05 05:21:13 -05:00
_, _, err := ReadAuthentication(buffer)
2016-12-08 18:11:05 -05:00
assert.Error(err).Equals(crypto.ErrAuthenticationFailed)
2016-02-05 05:21:13 -05:00
}
func TestWrongProtocolVersion(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-05 05:21:13 -05:00
2016-12-09 06:08:25 -05:00
buffer := buf.New()
2016-12-06 05:03:42 -05:00
buffer.AppendBytes(6, 1, 0)
2016-02-05 05:21:13 -05:00
_, _, err := ReadAuthentication(buffer)
2016-06-27 02:53:35 -04:00
assert.Error(err).Equals(proxy.ErrInvalidProtocolVersion)
2016-02-05 05:21:13 -05:00
}
func TestEmptyRequest(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-05 05:21:13 -05:00
2016-12-09 06:08:25 -05:00
_, err := ReadRequest(buf.New())
2016-02-05 05:21:13 -05:00
assert.Error(err).Equals(io.EOF)
}
func TestIPv6Request(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-05 05:21:13 -05:00
2016-12-09 06:08:25 -05:00
b := buf.New()
2016-12-06 05:03:42 -05:00
b.AppendBytes(5, 1, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 8)
request, err := ReadRequest(b)
2016-02-05 05:21:13 -05:00
assert.Error(err).IsNil()
assert.Byte(request.Command).Equals(1)
assert.Bytes(request.IPv6[:]).Equals([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6})
2016-05-24 09:29:08 -04:00
assert.Port(request.Port).Equals(8)
2016-02-05 05:21:13 -05:00
}