From 96da76c5b039f9dd064796b92fdd08e88d036ca2 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 2 May 2017 00:39:10 +0200 Subject: [PATCH] test case for vmess udp --- testing/scenarios/vmess_test.go | 134 ++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/testing/scenarios/vmess_test.go b/testing/scenarios/vmess_test.go index b91a28cfd..75ba41e3f 100644 --- a/testing/scenarios/vmess_test.go +++ b/testing/scenarios/vmess_test.go @@ -21,6 +21,7 @@ import ( "v2ray.com/core/proxy/vmess/outbound" "v2ray.com/core/testing/assert" "v2ray.com/core/testing/servers/tcp" + "v2ray.com/core/testing/servers/udp" "v2ray.com/core/transport/internet" ) @@ -282,6 +283,139 @@ func TestVMessGCM(t *testing.T) { CloseAllServers() } +func TestVMessGCMUDP(t *testing.T) { + assert := assert.On(t) + + udpServer := udp.Server{ + MsgProcessor: xor, + } + dest, err := udpServer.Start() + assert.Error(err).IsNil() + defer udpServer.Close() + + userID := protocol.NewID(uuid.New()) + serverPort := pickPort() + serverConfig := &core.Config{ + Inbound: []*proxyman.InboundHandlerConfig{ + { + ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ + PortRange: v2net.SinglePortRange(serverPort), + Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), + }), + ProxySettings: serial.ToTypedMessage(&inbound.Config{ + User: []*protocol.User{ + { + Account: serial.ToTypedMessage(&vmess.Account{ + Id: userID.String(), + AlterId: 64, + }), + }, + }, + }), + }, + }, + Outbound: []*proxyman.OutboundHandlerConfig{ + { + ProxySettings: serial.ToTypedMessage(&freedom.Config{}), + }, + }, + App: []*serial.TypedMessage{ + serial.ToTypedMessage(&log.Config{ + ErrorLogLevel: log.LogLevel_Debug, + ErrorLogType: log.LogType_Console, + }), + }, + } + + clientPort := pickPort() + clientConfig := &core.Config{ + Inbound: []*proxyman.InboundHandlerConfig{ + { + ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ + PortRange: v2net.SinglePortRange(clientPort), + Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), + }), + ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ + Address: v2net.NewIPOrDomain(dest.Address), + Port: uint32(dest.Port), + NetworkList: &v2net.NetworkList{ + Network: []v2net.Network{v2net.Network_UDP}, + }, + }), + }, + }, + Outbound: []*proxyman.OutboundHandlerConfig{ + { + ProxySettings: serial.ToTypedMessage(&outbound.Config{ + Receiver: []*protocol.ServerEndpoint{ + { + Address: v2net.NewIPOrDomain(v2net.LocalHostIP), + Port: uint32(serverPort), + User: []*protocol.User{ + { + Account: serial.ToTypedMessage(&vmess.Account{ + Id: userID.String(), + AlterId: 64, + SecuritySettings: &protocol.SecurityConfig{ + Type: protocol.SecurityType_AES128_GCM, + }, + }), + }, + }, + }, + }, + }), + }, + }, + App: []*serial.TypedMessage{ + serial.ToTypedMessage(&log.Config{ + ErrorLogLevel: log.LogLevel_Debug, + ErrorLogType: log.LogType_Console, + }), + }, + } + + assert.Error(InitializeServerConfig(serverConfig)).IsNil() + assert.Error(InitializeServerConfig(clientConfig)).IsNil() + + var wg sync.WaitGroup + wg.Add(10) + for i := 0; i < 10; i++ { + go func() { + conn, err := net.DialUDP("udp", nil, &net.UDPAddr{ + IP: []byte{127, 0, 0, 1}, + Port: int(clientPort), + }) + assert.Error(err).IsNil() + + payload := make([]byte, 1024) + rand.Read(payload) + + nBytes, err := conn.Write([]byte(payload)) + assert.Error(err).IsNil() + assert.Int(nBytes).Equals(len(payload)) + + payload1 := make([]byte, 1024) + rand.Read(payload1) + nBytes, err = conn.Write([]byte(payload1)) + assert.Error(err).IsNil() + assert.Int(nBytes).Equals(len(payload1)) + + response := readFrom(conn, time.Second*5, 1024) + assert.Bytes(response).Equals(xor([]byte(payload))) + + response = readFrom(conn, time.Second*5, 1024) + assert.Bytes(response).Equals(xor([]byte(payload1))) + + assert.Error(conn.Close()).IsNil() + wg.Done() + }() + } + wg.Wait() + + CloseAllServers() +} + func TestVMessChacha20(t *testing.T) { assert := assert.On(t)