2020-11-25 13:01:53 +02:00
|
|
|
package shadowsocks_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
|
|
. "github.com/xtls/xray-core/proxy/shadowsocks"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func toAccount(a *Account) protocol.Account {
|
|
|
|
account, err := a.AsAccount()
|
|
|
|
common.Must(err)
|
|
|
|
return account
|
|
|
|
}
|
|
|
|
|
2021-09-16 23:13:07 +03:00
|
|
|
func equalRequestHeader(x, y *protocol.RequestHeader) bool {
|
|
|
|
return cmp.Equal(x, y, cmp.Comparer(func(x, y protocol.RequestHeader) bool {
|
|
|
|
return x == y
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:01:53 +02:00
|
|
|
func TestUDPEncoding(t *testing.T) {
|
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandUDP,
|
|
|
|
Address: net.LocalHostIP,
|
|
|
|
Port: 1234,
|
|
|
|
User: &protocol.MemoryUser{
|
|
|
|
Email: "love@example.com",
|
|
|
|
Account: toAccount(&Account{
|
2021-09-16 23:13:07 +03:00
|
|
|
Password: "password",
|
2021-01-19 12:35:30 +02:00
|
|
|
CipherType: CipherType_AES_128_GCM,
|
2020-11-25 13:01:53 +02:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data := buf.New()
|
|
|
|
common.Must2(data.WriteString("test string"))
|
|
|
|
encodedData, err := EncodeUDPPacket(request, data.Bytes())
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-02-12 17:17:31 +02:00
|
|
|
validator := new(Validator)
|
|
|
|
validator.Add(request.User)
|
|
|
|
decodedRequest, decodedData, err := DecodeUDPPacket(validator, encodedData)
|
2020-11-25 13:01:53 +02:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
if r := cmp.Diff(decodedData.Bytes(), data.Bytes()); r != "" {
|
|
|
|
t.Error("data: ", r)
|
|
|
|
}
|
|
|
|
|
2021-09-16 23:13:07 +03:00
|
|
|
if equalRequestHeader(decodedRequest, request) == false {
|
|
|
|
t.Error("different request")
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTCPRequest(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
request *protocol.RequestHeader
|
|
|
|
payload []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
|
|
|
Address: net.LocalHostIP,
|
|
|
|
Port: 1234,
|
|
|
|
User: &protocol.MemoryUser{
|
|
|
|
Email: "love@example.com",
|
|
|
|
Account: toAccount(&Account{
|
|
|
|
Password: "tcp-password",
|
2021-09-16 23:13:07 +03:00
|
|
|
CipherType: CipherType_AES_128_GCM,
|
2020-11-25 13:01:53 +02:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
payload: []byte("test string"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
|
|
|
Address: net.LocalHostIPv6,
|
|
|
|
Port: 1234,
|
|
|
|
User: &protocol.MemoryUser{
|
|
|
|
Email: "love@example.com",
|
|
|
|
Account: toAccount(&Account{
|
|
|
|
Password: "password",
|
2021-01-19 12:35:30 +02:00
|
|
|
CipherType: CipherType_AES_256_GCM,
|
2020-11-25 13:01:53 +02:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
payload: []byte("test string"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
|
|
|
Address: net.DomainAddress("example.com"),
|
|
|
|
Port: 1234,
|
|
|
|
User: &protocol.MemoryUser{
|
|
|
|
Email: "love@example.com",
|
|
|
|
Account: toAccount(&Account{
|
|
|
|
Password: "password",
|
2021-09-16 23:13:07 +03:00
|
|
|
CipherType: CipherType_CHACHA20_POLY1305,
|
2020-11-25 13:01:53 +02:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
payload: []byte("test string"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
runTest := func(request *protocol.RequestHeader, payload []byte) {
|
|
|
|
data := buf.New()
|
|
|
|
common.Must2(data.Write(payload))
|
|
|
|
|
|
|
|
cache := buf.New()
|
|
|
|
defer cache.Release()
|
|
|
|
|
|
|
|
writer, err := WriteTCPRequest(request, cache)
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data}))
|
|
|
|
|
2021-02-12 17:17:31 +02:00
|
|
|
validator := new(Validator)
|
|
|
|
validator.Add(request.User)
|
|
|
|
decodedRequest, reader, err := ReadTCPSession(validator, cache)
|
2020-11-25 13:01:53 +02:00
|
|
|
common.Must(err)
|
2021-09-16 23:13:07 +03:00
|
|
|
if equalRequestHeader(decodedRequest, request) == false {
|
|
|
|
t.Error("different request")
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
decodedData, err := reader.ReadMultiBuffer()
|
|
|
|
common.Must(err)
|
|
|
|
if r := cmp.Diff(decodedData[0].Bytes(), payload); r != "" {
|
|
|
|
t.Error("data: ", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cases {
|
|
|
|
runTest(test.request, test.payload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUDPReaderWriter(t *testing.T) {
|
|
|
|
user := &protocol.MemoryUser{
|
|
|
|
Account: toAccount(&Account{
|
|
|
|
Password: "test-password",
|
2021-01-19 12:35:30 +02:00
|
|
|
CipherType: CipherType_CHACHA20_POLY1305,
|
2020-11-25 13:01:53 +02:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
cache := buf.New()
|
|
|
|
defer cache.Release()
|
|
|
|
|
2020-12-23 15:06:21 +02:00
|
|
|
writer := &UDPWriter{
|
2020-11-25 13:01:53 +02:00
|
|
|
Writer: cache,
|
|
|
|
Request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Address: net.DomainAddress("example.com"),
|
|
|
|
Port: 123,
|
|
|
|
User: user,
|
|
|
|
},
|
2020-12-23 15:06:21 +02:00
|
|
|
}
|
2020-11-25 13:01:53 +02:00
|
|
|
|
|
|
|
reader := &UDPReader{
|
|
|
|
Reader: cache,
|
|
|
|
User: user,
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
b := buf.New()
|
|
|
|
common.Must2(b.WriteString("test payload"))
|
|
|
|
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
|
|
|
|
|
|
|
|
payload, err := reader.ReadMultiBuffer()
|
|
|
|
common.Must(err)
|
|
|
|
if payload[0].String() != "test payload" {
|
|
|
|
t.Error("unexpected output: ", payload[0].String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
b := buf.New()
|
|
|
|
common.Must2(b.WriteString("test payload 2"))
|
|
|
|
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
|
|
|
|
|
|
|
|
payload, err := reader.ReadMultiBuffer()
|
|
|
|
common.Must(err)
|
|
|
|
if payload[0].String() != "test payload 2" {
|
|
|
|
t.Error("unexpected output: ", payload[0].String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|