From 0c6fc2b946b34314922c424b66e79a5fc9be6a07 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 23 Oct 2018 22:27:18 +0200 Subject: [PATCH] more test cases --- testing/scenarios/shadowsocks_test.go | 252 +++++++++++++++++++++++++- 1 file changed, 242 insertions(+), 10 deletions(-) diff --git a/testing/scenarios/shadowsocks_test.go b/testing/scenarios/shadowsocks_test.go index 6ade46419..9214e807c 100644 --- a/testing/scenarios/shadowsocks_test.go +++ b/testing/scenarios/shadowsocks_test.go @@ -6,6 +6,9 @@ import ( "testing" "time" + "v2ray.com/core/common" + "v2ray.com/core/common/compare" + "v2ray.com/core" "v2ray.com/core/app/log" "v2ray.com/core/app/proxyman" @@ -260,13 +263,13 @@ func TestShadowsocksAES128UDP(t *testing.T) { } func TestShadowsocksChacha20TCP(t *testing.T) { - assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert(err, IsNil) + common.Must(err) + defer tcpServer.Close() account := serial.ToTypedMessage(&shadowsocks.Account{ @@ -348,34 +351,151 @@ func TestShadowsocksChacha20TCP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert(err, IsNil) + common.Must(err) + + defer CloseAllServers(servers) var wg sync.WaitGroup wg.Add(10) for i := 0; i < 10; i++ { go func() { + defer wg.Done() + conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert(err, IsNil) + common.Must(err) + defer conn.Close() payload := make([]byte, 10240*1024) rand.Read(payload) nBytes, err := conn.Write([]byte(payload)) - assert(err, IsNil) - assert(nBytes, Equals, len(payload)) + common.Must(err) + + if nBytes != len(payload) { + t.Error("only part of payload is written: ", nBytes) + } response := readFrom(conn, time.Second*20, 10240*1024) - assert(response, Equals, xor([]byte(payload))) - assert(conn.Close(), IsNil) - wg.Done() + if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { + t.Error(err) + } }() } wg.Wait() +} - CloseAllServers(servers) +func TestShadowsocksChacha20Poly1305TCP(t *testing.T) { + tcpServer := tcp.Server{ + MsgProcessor: xor, + } + dest, err := tcpServer.Start() + common.Must(err) + + defer tcpServer.Close() + + account := serial.ToTypedMessage(&shadowsocks.Account{ + Password: "shadowsocks-password", + CipherType: shadowsocks.CipherType_CHACHA20_POLY1305, + }) + + serverPort := tcp.PickPort() + serverConfig := &core.Config{ + Inbound: []*core.InboundHandlerConfig{ + { + ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ + PortRange: net.SinglePortRange(serverPort), + Listen: net.NewIPOrDomain(net.LocalHostIP), + }), + ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{ + User: &protocol.User{ + Account: account, + Level: 1, + }, + Network: []net.Network{net.Network_TCP}, + }), + }, + }, + Outbound: []*core.OutboundHandlerConfig{ + { + ProxySettings: serial.ToTypedMessage(&freedom.Config{}), + }, + }, + } + + clientPort := tcp.PickPort() + clientConfig := &core.Config{ + Inbound: []*core.InboundHandlerConfig{ + { + ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ + PortRange: net.SinglePortRange(clientPort), + Listen: net.NewIPOrDomain(net.LocalHostIP), + }), + ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ + Address: net.NewIPOrDomain(dest.Address), + Port: uint32(dest.Port), + NetworkList: &net.NetworkList{ + Network: []net.Network{net.Network_TCP}, + }, + }), + }, + }, + Outbound: []*core.OutboundHandlerConfig{ + { + ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{ + Server: []*protocol.ServerEndpoint{ + { + Address: net.NewIPOrDomain(net.LocalHostIP), + Port: uint32(serverPort), + User: []*protocol.User{ + { + Account: account, + }, + }, + }, + }, + }), + }, + }, + } + + servers, err := InitializeServerConfigs(serverConfig, clientConfig) + common.Must(err) + + defer CloseAllServers(servers) + + var wg sync.WaitGroup + wg.Add(10) + for i := 0; i < 10; i++ { + go func() { + defer wg.Done() + + conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ + IP: []byte{127, 0, 0, 1}, + Port: int(clientPort), + }) + common.Must(err) + defer conn.Close() + + payload := make([]byte, 10240*1024) + rand.Read(payload) + + nBytes, err := conn.Write([]byte(payload)) + common.Must(err) + + if nBytes != len(payload) { + t.Error("only part of payload is written: ", nBytes) + } + + response := readFrom(conn, time.Second*20, 10240*1024) + if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { + t.Error(err) + } + }() + } + wg.Wait() } func TestShadowsocksAES256GCMTCP(t *testing.T) { @@ -737,3 +857,115 @@ func TestShadowsocksAES128GCMUDPMux(t *testing.T) { CloseAllServers(servers) } + +func TestShadowsocksNone(t *testing.T) { + tcpServer := tcp.Server{ + MsgProcessor: xor, + } + dest, err := tcpServer.Start() + common.Must(err) + + defer tcpServer.Close() + + account := serial.ToTypedMessage(&shadowsocks.Account{ + Password: "shadowsocks-password", + CipherType: shadowsocks.CipherType_NONE, + Ota: shadowsocks.Account_Enabled, + }) + + serverPort := tcp.PickPort() + serverConfig := &core.Config{ + Inbound: []*core.InboundHandlerConfig{ + { + ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ + PortRange: net.SinglePortRange(serverPort), + Listen: net.NewIPOrDomain(net.LocalHostIP), + }), + ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{ + User: &protocol.User{ + Account: account, + Level: 1, + }, + Network: []net.Network{net.Network_TCP}, + }), + }, + }, + Outbound: []*core.OutboundHandlerConfig{ + { + ProxySettings: serial.ToTypedMessage(&freedom.Config{}), + }, + }, + } + + clientPort := tcp.PickPort() + clientConfig := &core.Config{ + Inbound: []*core.InboundHandlerConfig{ + { + ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ + PortRange: net.SinglePortRange(clientPort), + Listen: net.NewIPOrDomain(net.LocalHostIP), + }), + ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ + Address: net.NewIPOrDomain(dest.Address), + Port: uint32(dest.Port), + NetworkList: &net.NetworkList{ + Network: []net.Network{net.Network_TCP}, + }, + }), + }, + }, + Outbound: []*core.OutboundHandlerConfig{ + { + ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{ + Server: []*protocol.ServerEndpoint{ + { + Address: net.NewIPOrDomain(net.LocalHostIP), + Port: uint32(serverPort), + User: []*protocol.User{ + { + Account: account, + }, + }, + }, + }, + }), + }, + }, + } + + servers, err := InitializeServerConfigs(serverConfig, clientConfig) + common.Must(err) + + defer CloseAllServers(servers) + + var wg sync.WaitGroup + wg.Add(10) + for i := 0; i < 10; i++ { + go func() { + defer wg.Done() + + conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ + IP: []byte{127, 0, 0, 1}, + Port: int(clientPort), + }) + common.Must(err) + defer conn.Close() + + payload := make([]byte, 10240*1024) + rand.Read(payload) + + nBytes, err := conn.Write([]byte(payload)) + common.Must(err) + + if nBytes != len(payload) { + t.Error("only part of payload is written: ", nBytes) + } + + response := readFrom(conn, time.Second*20, 10240*1024) + if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { + t.Error(err) + } + }() + } + wg.Wait() +}