1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-06 10:20:44 -05:00
v2fly/proxy/socks/protocol/socks_test.go

142 lines
3.7 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"
2015-10-08 17:06:12 -04:00
"github.com/v2ray/v2ray-core/common/alloc"
2015-12-02 15:44:01 -05:00
v2net "github.com/v2ray/v2ray-core/common/net"
v2netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
2015-11-02 17:48:47 -05:00
"github.com/v2ray/v2ray-core/transport"
2015-09-07 11:28:36 -04:00
)
2015-09-16 11:29:00 -04:00
func TestHasAuthenticationMethod(t *testing.T) {
v2testing.Current(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) {
v2testing.Current(t)
2015-09-07 16:26:37 -04:00
rawRequest := []byte{
0x05, // version
0x01, // nMethods
0x02, // methods
}
2015-09-17 11:37:04 -04:00
request, _, err := ReadAuthentication(bytes.NewReader(rawRequest))
2015-09-09 09:26:11 -04:00
assert.Error(err).IsNil()
assert.Byte(request.version).Named("Version").Equals(0x05)
assert.Byte(request.nMethods).Named("#Methods").Equals(0x01)
assert.Byte(request.authMethods[0]).Named("Auth Method").Equals(0x02)
2015-09-07 16:26:37 -04:00
}
2015-09-16 11:29:00 -04:00
func TestAuthenticationResponseWrite(t *testing.T) {
v2testing.Current(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) {
v2testing.Current(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()
assert.Byte(request.Version).Named("Version").Equals(0x05)
assert.Byte(request.Command).Named("Command").Equals(0x01)
assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72})
2015-12-02 15:44:01 -05:00
v2netassert.Port(request.Port).Named("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) {
v2testing.Current(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),
}
2015-10-08 17:06:12 -04:00
buffer := alloc.NewSmallBuffer().Clear()
defer buffer.Release()
response.Write(buffer)
expectedBytes := []byte{
socksVersion,
ErrorSuccess,
byte(0x00),
AddrTypeIPv4,
0x72, 0x72, 0x72, 0x72,
byte(0x00), byte(0x035),
}
2015-10-08 17:06:12 -04:00
assert.Bytes(buffer.Value).Named("raw response").Equals(expectedBytes)
}
2015-11-02 17:48:47 -05:00
2015-12-03 17:31:04 -05:00
func TestSetIPv6(t *testing.T) {
v2testing.Current(t)
response := NewSocks5Response()
response.SetIPv6([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
buffer := alloc.NewSmallBuffer().Clear()
defer buffer.Release()
response.Write(buffer)
assert.Bytes(buffer.Value).Equals([]byte{
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) {
v2testing.Current(t)
response := NewSocks5Response()
response.SetDomain("v2ray.com")
buffer := alloc.NewSmallBuffer().Clear()
defer buffer.Release()
response.Write(buffer)
assert.Bytes(buffer.Value).Equals([]byte{
socksVersion, 0, 0, AddrTypeDomain, 9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0})
}
2015-11-02 17:48:47 -05:00
func TestEOF(t *testing.T) {
v2testing.Current(t)
2015-11-02 17:48:47 -05:00
_, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 0)))
assert.Error(err).Equals(io.EOF)
}
func TestSignleByte(t *testing.T) {
v2testing.Current(t)
2015-11-02 17:48:47 -05:00
_, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 1)))
assert.Error(err).Equals(transport.ErrorCorruptedPacket)
2015-11-02 17:48:47 -05:00
}