1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-30 15:57:35 -04:00
v2fly/proxy/shadowsocks/protocol_test.go

119 lines
3.0 KiB
Go
Raw Normal View History

2016-01-29 09:09:51 -05:00
package shadowsocks_test
import (
"testing"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/alloc"
2016-10-31 10:24:28 -04:00
"v2ray.com/core/common/loader"
2016-08-20 14:55:45 -04:00
v2net "v2ray.com/core/common/net"
2016-10-31 10:24:28 -04:00
"v2ray.com/core/common/protocol"
2016-08-20 14:55:45 -04:00
. "v2ray.com/core/proxy/shadowsocks"
"v2ray.com/core/testing/assert"
2016-01-29 09:09:51 -05:00
)
2016-10-31 10:24:28 -04:00
func TestUDPEncoding(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-01-29 09:09:51 -05:00
2016-10-31 10:24:28 -04:00
request := &protocol.RequestHeader{
Version: Version,
Command: protocol.RequestCommandUDP,
Address: v2net.LocalHostIP,
Port: 1234,
User: &protocol.User{
Email: "love@v2ray.com",
Account: loader.NewTypedSettings(&Account{
Password: "shadowsocks-password",
CipherType: CipherType_AES_128_CFB,
Ota: Account_Disabled,
}),
},
}
data := alloc.NewLocalBuffer(256).Clear().AppendString("test string")
encodedData, err := EncodeUDPPacket(request, data)
2016-01-29 09:09:51 -05:00
assert.Error(err).IsNil()
2016-02-04 16:10:52 -05:00
2016-10-31 10:24:28 -04:00
decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData)
2016-01-29 09:09:51 -05:00
assert.Error(err).IsNil()
2016-10-31 10:24:28 -04:00
assert.Bytes(decodedData.Value).Equals(data.Value)
assert.Address(decodedRequest.Address).Equals(request.Address)
assert.Port(decodedRequest.Port).Equals(request.Port)
2016-01-29 09:09:51 -05:00
}
2016-01-29 15:55:42 -05:00
2016-10-31 10:24:28 -04:00
func TestTCPRequest(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-04 16:10:52 -05:00
2016-10-31 10:24:28 -04:00
request := &protocol.RequestHeader{
Version: Version,
Command: protocol.RequestCommandTCP,
Address: v2net.LocalHostIP,
Option: RequestOptionOneTimeAuth,
Port: 1234,
User: &protocol.User{
Email: "love@v2ray.com",
Account: loader.NewTypedSettings(&Account{
Password: "tcp-password",
CipherType: CipherType_CHACHA20,
}),
},
}
data := alloc.NewLocalBuffer(256).Clear().AppendString("test string")
2016-11-18 19:50:09 -05:00
cache := alloc.NewBuffer().Clear()
2016-10-31 10:24:28 -04:00
writer, err := WriteTCPRequest(request, cache)
2016-01-29 15:55:42 -05:00
assert.Error(err).IsNil()
2016-10-31 10:24:28 -04:00
writer.Write(data)
2016-01-29 15:55:42 -05:00
2016-10-31 10:24:28 -04: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 15:55:42 -05:00
2016-10-31 10:24:28 -04:00
decodedData, err := reader.Read()
2016-01-29 15:55:42 -05:00
assert.Error(err).IsNil()
2016-10-31 10:24:28 -04:00
assert.Bytes(decodedData.Value).Equals([]byte("test string"))
2016-01-29 15:55:42 -05:00
}
2016-11-06 08:32:04 -05:00
func TestUDPReaderWriter(t *testing.T) {
assert := assert.On(t)
user := &protocol.User{
Account: loader.NewTypedSettings(&Account{
Password: "test-password",
CipherType: CipherType_CHACHA20_IEFT,
}),
}
cache := alloc.NewBuffer().Clear()
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,
}
err := writer.Write(alloc.NewBuffer().Clear().AppendString("test payload"))
assert.Error(err).IsNil()
payload, err := reader.Read()
assert.Error(err).IsNil()
assert.String(payload.String()).Equals("test payload")
err = writer.Write(alloc.NewBuffer().Clear().AppendString("test payload 2"))
assert.Error(err).IsNil()
payload, err = reader.Read()
assert.Error(err).IsNil()
assert.String(payload.String()).Equals("test payload 2")
}