mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
110 lines
2.9 KiB
Go
110 lines
2.9 KiB
Go
package protocol
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"github.com/v2ray/v2ray-core/testing/unit"
|
|
)
|
|
|
|
func TestHasAuthenticationMethod(t *testing.T) {
|
|
assert := unit.Assert(t)
|
|
|
|
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()
|
|
}
|
|
|
|
func TestAuthenticationRequestRead(t *testing.T) {
|
|
assert := unit.Assert(t)
|
|
|
|
rawRequest := []byte{
|
|
0x05, // version
|
|
0x01, // nMethods
|
|
0x02, // methods
|
|
}
|
|
request, _, err := ReadAuthentication(bytes.NewReader(rawRequest))
|
|
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)
|
|
}
|
|
|
|
func TestAuthentication4RequestRead(t *testing.T) {
|
|
assert := unit.Assert(t)
|
|
|
|
rawRequest := []byte{
|
|
0x04, // version
|
|
0x01, // command
|
|
0x00, 0x35,
|
|
0x72, 0x72, 0x72, 0x72,
|
|
}
|
|
_, request4, err := ReadAuthentication(bytes.NewReader(rawRequest))
|
|
assert.Error(err).Equals(ErrorSocksVersion4)
|
|
assert.Byte(request4.Version).Named("Version").Equals(0x04)
|
|
assert.Byte(request4.Command).Named("Command").Equals(0x01)
|
|
assert.Uint16(request4.Port).Named("Port").Equals(53)
|
|
assert.Bytes(request4.IP[:]).Named("IP").Equals([]byte{0x72, 0x72, 0x72, 0x72})
|
|
}
|
|
|
|
func TestAuthenticationResponseWrite(t *testing.T) {
|
|
assert := unit.Assert(t)
|
|
|
|
response := NewAuthenticationResponse(byte(0x05))
|
|
|
|
buffer := bytes.NewBuffer(make([]byte, 0, 10))
|
|
WriteAuthentication(buffer, response)
|
|
assert.Bytes(buffer.Bytes()).Equals([]byte{socksVersion, byte(0x05)})
|
|
}
|
|
|
|
func TestRequestRead(t *testing.T) {
|
|
assert := unit.Assert(t)
|
|
|
|
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))
|
|
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})
|
|
assert.Uint16(request.Port).Named("Port").Equals(53)
|
|
}
|
|
|
|
func TestResponseToBytes(t *testing.T) {
|
|
assert := unit.Assert(t)
|
|
|
|
response := Socks5Response{
|
|
socksVersion,
|
|
ErrorSuccess,
|
|
AddrTypeIPv4,
|
|
[4]byte{0x72, 0x72, 0x72, 0x72},
|
|
"",
|
|
[16]byte{},
|
|
uint16(53),
|
|
}
|
|
rawResponse := response.toBytes()
|
|
expectedBytes := []byte{
|
|
socksVersion,
|
|
ErrorSuccess,
|
|
byte(0x00),
|
|
AddrTypeIPv4,
|
|
0x72, 0x72, 0x72, 0x72,
|
|
byte(0x00), byte(0x035),
|
|
}
|
|
assert.Bytes(rawResponse).Named("raw response").Equals(expectedBytes)
|
|
}
|