github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/proxy/shadowsocks/protocol_test.go (about)

     1  package shadowsocks_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  
     8  	"github.com/v2fly/v2ray-core/v5/common"
     9  	"github.com/v2fly/v2ray-core/v5/common/buf"
    10  	"github.com/v2fly/v2ray-core/v5/common/net"
    11  	"github.com/v2fly/v2ray-core/v5/common/protocol"
    12  	. "github.com/v2fly/v2ray-core/v5/proxy/shadowsocks"
    13  )
    14  
    15  func toAccount(a *Account) protocol.Account {
    16  	account, err := a.AsAccount()
    17  	common.Must(err)
    18  	return account
    19  }
    20  
    21  func equalRequestHeader(x, y *protocol.RequestHeader) bool {
    22  	return cmp.Equal(x, y, cmp.Comparer(func(x, y protocol.RequestHeader) bool {
    23  		return x == y
    24  	}))
    25  }
    26  
    27  func TestUDPEncoding(t *testing.T) {
    28  	request := &protocol.RequestHeader{
    29  		Version: Version,
    30  		Command: protocol.RequestCommandUDP,
    31  		Address: net.LocalHostIP,
    32  		Port:    1234,
    33  		User: &protocol.MemoryUser{
    34  			Email: "love@v2fly.org",
    35  			Account: toAccount(&Account{
    36  				Password:   "password",
    37  				CipherType: CipherType_AES_128_GCM,
    38  			}),
    39  		},
    40  	}
    41  
    42  	data := buf.New()
    43  	common.Must2(data.WriteString("test string"))
    44  	encodedData, err := EncodeUDPPacket(request, data.Bytes())
    45  	common.Must(err)
    46  
    47  	decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData)
    48  	common.Must(err)
    49  
    50  	if r := cmp.Diff(decodedData.Bytes(), data.Bytes()); r != "" {
    51  		t.Error("data: ", r)
    52  	}
    53  
    54  	if equalRequestHeader(decodedRequest, request) == false {
    55  		t.Error("different request")
    56  	}
    57  }
    58  
    59  func TestTCPRequest(t *testing.T) {
    60  	cases := []struct {
    61  		request *protocol.RequestHeader
    62  		payload []byte
    63  	}{
    64  		{
    65  			request: &protocol.RequestHeader{
    66  				Version: Version,
    67  				Command: protocol.RequestCommandTCP,
    68  				Address: net.LocalHostIP,
    69  				Port:    1234,
    70  				User: &protocol.MemoryUser{
    71  					Email: "love@v2fly.org",
    72  					Account: toAccount(&Account{
    73  						Password:   "tcp-password",
    74  						CipherType: CipherType_AES_128_GCM,
    75  					}),
    76  				},
    77  			},
    78  			payload: []byte("test string"),
    79  		},
    80  		{
    81  			request: &protocol.RequestHeader{
    82  				Version: Version,
    83  				Command: protocol.RequestCommandTCP,
    84  				Address: net.LocalHostIPv6,
    85  				Port:    1234,
    86  				User: &protocol.MemoryUser{
    87  					Email: "love@v2fly.org",
    88  					Account: toAccount(&Account{
    89  						Password:   "password",
    90  						CipherType: CipherType_AES_256_GCM,
    91  					}),
    92  				},
    93  			},
    94  			payload: []byte("test string"),
    95  		},
    96  		{
    97  			request: &protocol.RequestHeader{
    98  				Version: Version,
    99  				Command: protocol.RequestCommandTCP,
   100  				Address: net.DomainAddress("v2fly.org"),
   101  				Port:    1234,
   102  				User: &protocol.MemoryUser{
   103  					Email: "love@v2fly.org",
   104  					Account: toAccount(&Account{
   105  						Password:   "password",
   106  						CipherType: CipherType_CHACHA20_POLY1305,
   107  					}),
   108  				},
   109  			},
   110  			payload: []byte("test string"),
   111  		},
   112  	}
   113  
   114  	runTest := func(request *protocol.RequestHeader, payload []byte) {
   115  		data := buf.New()
   116  		common.Must2(data.Write(payload))
   117  
   118  		cache := buf.New()
   119  		defer cache.Release()
   120  
   121  		writer, err := WriteTCPRequest(request, cache)
   122  		common.Must(err)
   123  
   124  		common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data}))
   125  
   126  		decodedRequest, reader, err := ReadTCPSession(request.User, cache)
   127  		common.Must(err)
   128  		if equalRequestHeader(decodedRequest, request) == false {
   129  			t.Error("different request")
   130  		}
   131  
   132  		decodedData, err := reader.ReadMultiBuffer()
   133  		common.Must(err)
   134  		if r := cmp.Diff(decodedData[0].Bytes(), payload); r != "" {
   135  			t.Error("data: ", r)
   136  		}
   137  	}
   138  
   139  	for _, test := range cases {
   140  		runTest(test.request, test.payload)
   141  	}
   142  }
   143  
   144  func TestUDPReaderWriter(t *testing.T) {
   145  	user := &protocol.MemoryUser{
   146  		Account: toAccount(&Account{
   147  			Password:   "test-password",
   148  			CipherType: CipherType_CHACHA20_POLY1305,
   149  		}),
   150  	}
   151  	cache := buf.New()
   152  	defer cache.Release()
   153  
   154  	writer := &buf.SequentialWriter{Writer: &UDPWriter{
   155  		Writer: cache,
   156  		Request: &protocol.RequestHeader{
   157  			Version: Version,
   158  			Address: net.DomainAddress("v2fly.org"),
   159  			Port:    123,
   160  			User:    user,
   161  		},
   162  	}}
   163  
   164  	reader := &UDPReader{
   165  		Reader: cache,
   166  		User:   user,
   167  	}
   168  
   169  	{
   170  		b := buf.New()
   171  		common.Must2(b.WriteString("test payload"))
   172  		common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
   173  
   174  		payload, err := reader.ReadMultiBuffer()
   175  		common.Must(err)
   176  		if payload[0].String() != "test payload" {
   177  			t.Error("unexpected output: ", payload[0].String())
   178  		}
   179  	}
   180  
   181  	{
   182  		b := buf.New()
   183  		common.Must2(b.WriteString("test payload 2"))
   184  		common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
   185  
   186  		payload, err := reader.ReadMultiBuffer()
   187  		common.Must(err)
   188  		if payload[0].String() != "test payload 2" {
   189  			t.Error("unexpected output: ", payload[0].String())
   190  		}
   191  	}
   192  }