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