1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 14:26:26 -05:00
v2fly/proxy/shadowsocks/protocol_test.go

193 lines
4.4 KiB
Go
Raw Normal View History

2016-01-29 09:09:51 -05:00
package shadowsocks_test
import (
"testing"
2019-02-02 16:19:30 -05:00
"github.com/google/go-cmp/cmp"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/protocol"
. "github.com/v2fly/v2ray-core/v5/proxy/shadowsocks"
2016-01-29 09:09:51 -05:00
)
2018-08-26 18:11:32 -04:00
func toAccount(a *Account) protocol.Account {
account, err := a.AsAccount()
common.Must(err)
return account
}
func equalRequestHeader(x, y *protocol.RequestHeader) bool {
return cmp.Equal(x, y, cmp.Comparer(func(x, y protocol.RequestHeader) bool {
return x == y
}))
}
2016-10-31 10:24:28 -04:00
func TestUDPEncoding(t *testing.T) {
request := &protocol.RequestHeader{
Version: Version,
Command: protocol.RequestCommandUDP,
Address: net.LocalHostIP,
2016-10-31 10:24:28 -04:00
Port: 1234,
2018-08-26 18:11:32 -04:00
User: &protocol.MemoryUser{
Email: "love@v2fly.org",
2018-08-26 18:11:32 -04:00
Account: toAccount(&Account{
Password: "password",
CipherType: CipherType_AES_128_GCM,
2016-10-31 10:24:28 -04:00
}),
},
}
2018-08-16 06:05:33 -04:00
data := buf.New()
2018-11-02 16:34:04 -04:00
common.Must2(data.WriteString("test string"))
2017-04-21 08:51:09 -04:00
encodedData, err := EncodeUDPPacket(request, data.Bytes())
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-02-04 16:10:52 -05:00
2016-10-31 10:24:28 -04:00
decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData)
2019-02-02 16:19:30 -05:00
common.Must(err)
if r := cmp.Diff(decodedData.Bytes(), data.Bytes()); r != "" {
t.Error("data: ", r)
}
if equalRequestHeader(decodedRequest, request) == false {
t.Error("different request")
2019-02-02 16:19:30 -05:00
}
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) {
2017-04-26 17:46:19 -04:00
cases := []struct {
request *protocol.RequestHeader
payload []byte
}{
{
request: &protocol.RequestHeader{
Version: Version,
Command: protocol.RequestCommandTCP,
Address: net.LocalHostIP,
2017-04-26 17:46:19 -04:00
Port: 1234,
2018-08-26 18:11:32 -04:00
User: &protocol.MemoryUser{
Email: "love@v2fly.org",
2018-08-26 18:11:32 -04:00
Account: toAccount(&Account{
2017-04-26 17:46:19 -04:00
Password: "tcp-password",
CipherType: CipherType_AES_128_GCM,
2017-04-26 17:46:19 -04:00
}),
},
},
payload: []byte("test string"),
},
{
request: &protocol.RequestHeader{
Version: Version,
Command: protocol.RequestCommandTCP,
Address: net.LocalHostIPv6,
2017-04-26 17:46:19 -04:00
Port: 1234,
2018-08-26 18:11:32 -04:00
User: &protocol.MemoryUser{
Email: "love@v2fly.org",
2018-08-26 18:11:32 -04:00
Account: toAccount(&Account{
2017-04-26 17:46:19 -04:00
Password: "password",
CipherType: CipherType_AES_256_GCM,
2017-04-26 17:46:19 -04:00
}),
},
},
payload: []byte("test string"),
},
{
request: &protocol.RequestHeader{
Version: Version,
Command: protocol.RequestCommandTCP,
Address: net.DomainAddress("v2fly.org"),
2017-04-26 17:46:19 -04:00
Port: 1234,
2018-08-26 18:11:32 -04:00
User: &protocol.MemoryUser{
Email: "love@v2fly.org",
2018-08-26 18:11:32 -04:00
Account: toAccount(&Account{
2017-04-26 17:46:19 -04:00
Password: "password",
CipherType: CipherType_CHACHA20_POLY1305,
2017-04-26 17:46:19 -04:00
}),
},
},
payload: []byte("test string"),
2016-10-31 10:24:28 -04:00
},
}
2017-04-26 17:46:19 -04:00
runTest := func(request *protocol.RequestHeader, payload []byte) {
data := buf.New()
2018-08-16 06:05:33 -04:00
common.Must2(data.Write(payload))
2016-10-31 10:24:28 -04:00
2017-04-26 17:46:19 -04:00
cache := buf.New()
defer cache.Release()
2016-01-29 15:55:42 -05:00
2017-04-26 17:46:19 -04:00
writer, err := WriteTCPRequest(request, cache)
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-01-29 15:55:42 -05:00
2019-02-02 16:19:30 -05:00
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data}))
2017-04-26 17:46:19 -04:00
decodedRequest, reader, err := ReadTCPSession(request.User, cache)
2019-02-02 16:19:30 -05:00
common.Must(err)
if equalRequestHeader(decodedRequest, request) == false {
t.Error("different request")
2019-02-02 16:19:30 -05:00
}
2017-04-26 17:46:19 -04:00
2017-11-09 16:33:15 -05:00
decodedData, err := reader.ReadMultiBuffer()
2019-02-02 16:19:30 -05:00
common.Must(err)
if r := cmp.Diff(decodedData[0].Bytes(), payload); r != "" {
t.Error("data: ", r)
}
2017-04-26 17:46:19 -04:00
}
for _, test := range cases {
runTest(test.request, test.payload)
}
2016-01-29 15:55:42 -05:00
}
2016-11-06 08:32:04 -05:00
func TestUDPReaderWriter(t *testing.T) {
2018-08-26 18:11:32 -04:00
user := &protocol.MemoryUser{
Account: toAccount(&Account{
2016-11-06 08:32:04 -05:00
Password: "test-password",
CipherType: CipherType_CHACHA20_POLY1305,
2016-11-06 08:32:04 -05:00
}),
}
2016-12-09 06:08:25 -05:00
cache := buf.New()
2018-08-16 06:05:33 -04:00
defer cache.Release()
2018-07-31 07:43:27 -04:00
writer := &buf.SequentialWriter{Writer: &UDPWriter{
2016-11-06 08:32:04 -05:00
Writer: cache,
Request: &protocol.RequestHeader{
Version: Version,
Address: net.DomainAddress("v2fly.org"),
2016-11-06 08:32:04 -05:00
Port: 123,
User: user,
},
2018-07-31 07:43:27 -04:00
}}
2016-11-06 08:32:04 -05:00
reader := &UDPReader{
Reader: cache,
User: user,
}
2018-08-16 06:05:33 -04:00
{
b := buf.New()
2018-11-02 16:34:04 -04:00
common.Must2(b.WriteString("test payload"))
2019-02-02 16:19:30 -05:00
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
2016-11-06 08:32:04 -05:00
2018-08-16 06:05:33 -04:00
payload, err := reader.ReadMultiBuffer()
2019-02-02 16:19:30 -05:00
common.Must(err)
if payload[0].String() != "test payload" {
t.Error("unexpected output: ", payload[0].String())
}
2018-08-16 06:05:33 -04:00
}
2016-11-06 08:32:04 -05:00
2018-08-16 06:05:33 -04:00
{
b := buf.New()
2018-11-02 16:34:04 -04:00
common.Must2(b.WriteString("test payload 2"))
2019-02-02 16:19:30 -05:00
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
2016-11-06 08:32:04 -05:00
2018-08-16 06:05:33 -04:00
payload, err := reader.ReadMultiBuffer()
2019-02-02 16:19:30 -05:00
common.Must(err)
if payload[0].String() != "test payload 2" {
t.Error("unexpected output: ", payload[0].String())
}
2018-08-16 06:05:33 -04:00
}
2016-11-06 08:32:04 -05:00
}