From d56f38d38eb3c64b216f683e83804c00a7fda6b7 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sat, 28 May 2022 12:05:54 -0400 Subject: [PATCH] Break shadowsocks2022 test into Tcp and Udp - make test smaller - pickPort is more reliable for only Tcp or Udp --- testing/scenarios/shadowsocks_test.go | 101 ++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 15 deletions(-) diff --git a/testing/scenarios/shadowsocks_test.go b/testing/scenarios/shadowsocks_test.go index f4fae9a9..e023b61c 100644 --- a/testing/scenarios/shadowsocks_test.go +++ b/testing/scenarios/shadowsocks_test.go @@ -490,17 +490,27 @@ func TestShadowsocksNone(t *testing.T) { } } -func TestShadowsocks2022(t *testing.T) { +func TestShadowsocks2022Tcp(t *testing.T) { for _, method := range shadowaead_2022.List { password := make([]byte, 32) rand.Read(password) t.Run(method, func(t *testing.T) { - testShadowsocks2022(t, method, base64.StdEncoding.EncodeToString(password)) + testShadowsocks2022Tcp(t, method, base64.StdEncoding.EncodeToString(password)) }) } } -func testShadowsocks2022(t *testing.T, method string, password string) { +func TestShadowsocks2022Udp(t *testing.T) { + for _, method := range shadowaead_2022.List { + password := make([]byte, 32) + rand.Read(password) + t.Run(method, func(t *testing.T) { + testShadowsocks2022Udp(t, method, base64.StdEncoding.EncodeToString(password)) + }) + } +} + +func testShadowsocks2022Tcp(t *testing.T, method string, password string) { tcpServer := tcp.Server{ MsgProcessor: xor, } @@ -508,13 +518,6 @@ func testShadowsocks2022(t *testing.T, method string, password string) { common.Must(err) defer tcpServer.Close() - udpServer := udp.Server{ - MsgProcessor: xor, - } - udpDest, err := udpServer.Start() - common.Must(err) - defer udpServer.Close() - serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ @@ -532,7 +535,7 @@ func testShadowsocks2022(t *testing.T, method string, password string) { ProxySettings: serial.ToTypedMessage(&shadowsocks_2022.ServerConfig{ Method: method, Key: password, - Network: []net.Network{net.Network_TCP, net.Network_UDP}, + Network: []net.Network{net.Network_TCP}, }), }, }, @@ -544,7 +547,6 @@ func testShadowsocks2022(t *testing.T, method string, password string) { } clientPort := tcp.PickPort() - udpClientPort := udp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -564,6 +566,78 @@ func testShadowsocks2022(t *testing.T, method string, password string) { Networks: []net.Network{net.Network_TCP}, }), }, + }, + Outbound: []*core.OutboundHandlerConfig{ + { + ProxySettings: serial.ToTypedMessage(&shadowsocks_2022.ClientConfig{ + Address: net.NewIPOrDomain(net.LocalHostIP), + Port: uint32(serverPort), + Method: method, + Key: password, + }), + }, + }, + } + + servers, err := InitializeServerConfigs(serverConfig, clientConfig) + common.Must(err) + defer CloseAllServers(servers) + + var errGroup errgroup.Group + for i := 0; i < 10; i++ { + errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20)) + } + + if err := errGroup.Wait(); err != nil { + t.Error(err) + } +} + +func testShadowsocks2022Udp(t *testing.T, method string, password string) { + udpServer := udp.Server{ + MsgProcessor: xor, + } + udpDest, err := udpServer.Start() + common.Must(err) + defer udpServer.Close() + + serverPort := udp.PickPort() + serverConfig := &core.Config{ + App: []*serial.TypedMessage{ + serial.ToTypedMessage(&log.Config{ + ErrorLogLevel: clog.Severity_Debug, + ErrorLogType: log.LogType_Console, + }), + }, + Inbound: []*core.InboundHandlerConfig{ + { + ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ + PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}}, + Listen: net.NewIPOrDomain(net.LocalHostIP), + }), + ProxySettings: serial.ToTypedMessage(&shadowsocks_2022.ServerConfig{ + Method: method, + Key: password, + Network: []net.Network{net.Network_UDP}, + }), + }, + }, + Outbound: []*core.OutboundHandlerConfig{ + { + ProxySettings: serial.ToTypedMessage(&freedom.Config{}), + }, + }, + } + + udpClientPort := udp.PickPort() + clientConfig := &core.Config{ + App: []*serial.TypedMessage{ + serial.ToTypedMessage(&log.Config{ + ErrorLogLevel: clog.Severity_Debug, + ErrorLogType: log.LogType_Console, + }), + }, + Inbound: []*core.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(udpClientPort)}}, @@ -593,9 +667,6 @@ func testShadowsocks2022(t *testing.T, method string, password string) { defer CloseAllServers(servers) var errGroup errgroup.Group - for i := 0; i < 10; i++ { - errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20)) - } for i := 0; i < 10; i++ { errGroup.Go(testUDPConn(udpClientPort, 1024, time.Second*5)) }