v2ray-core/proxy/shadowsocks/protocol_test.go

125 lines
3.1 KiB
Go
Raw Normal View History

2016-01-29 14:09:51 +00:00
package shadowsocks_test
import (
"testing"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
2016-10-31 14:24:28 +00:00
"v2ray.com/core/common/protocol"
2016-12-06 10:03:42 +00:00
"v2ray.com/core/common/serial"
2016-08-20 18:55:45 +00:00
. "v2ray.com/core/proxy/shadowsocks"
"v2ray.com/core/testing/assert"
2016-01-29 14:09:51 +00:00
)
2016-10-31 14:24:28 +00:00
func TestUDPEncoding(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-01-29 14:09:51 +00:00
2016-10-31 14:24:28 +00:00
request := &protocol.RequestHeader{
Version: Version,
Command: protocol.RequestCommandUDP,
Address: v2net.LocalHostIP,
Port: 1234,
User: &protocol.User{
Email: "love@v2ray.com",
2016-12-15 10:51:09 +00:00
Account: serial.ToTypedMessage(&Account{
2016-10-31 14:24:28 +00:00
Password: "shadowsocks-password",
CipherType: CipherType_AES_128_CFB,
Ota: Account_Disabled,
}),
},
}
2016-12-09 11:08:25 +00:00
data := buf.NewLocal(256)
data.AppendSupplier(serial.WriteString("test string"))
2016-10-31 14:24:28 +00:00
encodedData, err := EncodeUDPPacket(request, data)
2016-01-29 14:09:51 +00:00
assert.Error(err).IsNil()
2016-02-04 21:10:52 +00:00
2016-10-31 14:24:28 +00:00
decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData)
2016-01-29 14:09:51 +00:00
assert.Error(err).IsNil()
2016-12-05 14:19:14 +00:00
assert.Bytes(decodedData.Bytes()).Equals(data.Bytes())
2016-10-31 14:24:28 +00:00
assert.Address(decodedRequest.Address).Equals(request.Address)
assert.Port(decodedRequest.Port).Equals(request.Port)
2016-01-29 14:09:51 +00:00
}
2016-01-29 20:55:42 +00:00
2016-10-31 14:24:28 +00:00
func TestTCPRequest(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-02-04 21:10:52 +00:00
2016-10-31 14:24:28 +00:00
request := &protocol.RequestHeader{
Version: Version,
Command: protocol.RequestCommandTCP,
Address: v2net.LocalHostIP,
Option: RequestOptionOneTimeAuth,
Port: 1234,
User: &protocol.User{
Email: "love@v2ray.com",
2016-12-15 10:51:09 +00:00
Account: serial.ToTypedMessage(&Account{
2016-10-31 14:24:28 +00:00
Password: "tcp-password",
CipherType: CipherType_CHACHA20,
}),
},
}
2016-12-09 11:08:25 +00:00
data := buf.NewLocal(256)
data.AppendSupplier(serial.WriteString("test string"))
cache := buf.New()
2016-10-31 14:24:28 +00:00
writer, err := WriteTCPRequest(request, cache)
2016-01-29 20:55:42 +00:00
assert.Error(err).IsNil()
2017-04-15 19:07:23 +00:00
writer.Write(buf.NewMultiBufferValue(data))
2016-01-29 20:55:42 +00:00
2016-10-31 14:24:28 +00:00
decodedRequest, reader, err := ReadTCPSession(request.User, cache)
assert.Error(err).IsNil()
assert.Address(decodedRequest.Address).Equals(request.Address)
assert.Port(decodedRequest.Port).Equals(request.Port)
2016-01-29 20:55:42 +00:00
2016-10-31 14:24:28 +00:00
decodedData, err := reader.Read()
2016-01-29 20:55:42 +00:00
assert.Error(err).IsNil()
2017-04-15 19:07:23 +00:00
assert.String(decodedData[0].String()).Equals("test string")
2016-01-29 20:55:42 +00:00
}
2016-11-06 13:32:04 +00:00
func TestUDPReaderWriter(t *testing.T) {
assert := assert.On(t)
user := &protocol.User{
2016-12-15 10:51:09 +00:00
Account: serial.ToTypedMessage(&Account{
2016-11-06 13:32:04 +00:00
Password: "test-password",
2017-01-03 00:37:27 +00:00
CipherType: CipherType_CHACHA20_IETF,
2016-11-06 13:32:04 +00:00
}),
}
2016-12-09 11:08:25 +00:00
cache := buf.New()
2016-11-06 13:32:04 +00:00
writer := &UDPWriter{
Writer: cache,
Request: &protocol.RequestHeader{
Version: Version,
Address: v2net.DomainAddress("v2ray.com"),
Port: 123,
User: user,
Option: RequestOptionOneTimeAuth,
},
}
reader := &UDPReader{
Reader: cache,
User: user,
}
2016-12-09 11:08:25 +00:00
b := buf.New()
b.AppendSupplier(serial.WriteString("test payload"))
2017-04-15 19:07:23 +00:00
err := writer.Write(buf.NewMultiBufferValue(b))
2016-11-06 13:32:04 +00:00
assert.Error(err).IsNil()
payload, err := reader.Read()
assert.Error(err).IsNil()
2017-04-15 19:07:23 +00:00
assert.String(payload[0].String()).Equals("test payload")
2016-11-06 13:32:04 +00:00
2016-12-09 11:08:25 +00:00
b = buf.New()
b.AppendSupplier(serial.WriteString("test payload 2"))
2017-04-15 19:07:23 +00:00
err = writer.Write(buf.NewMultiBufferValue(b))
2016-11-06 13:32:04 +00:00
assert.Error(err).IsNil()
payload, err = reader.Read()
assert.Error(err).IsNil()
2017-04-15 19:07:23 +00:00
assert.String(payload[0].String()).Equals("test payload 2")
2016-11-06 13:32:04 +00:00
}