Selaa lähdekoodia

convert dokodemo scenario test to protobuf based

Darien Raymond 8 vuotta sitten
vanhempi
commit
a704d7b6b6

+ 4 - 0
common/net/network.go

@@ -67,3 +67,7 @@ func (v NetworkList) HasNetwork(network Network) bool {
 func (v NetworkList) Get(idx int) Network {
 	return v.Network[idx]
 }
+
+func (v NetworkList) Size() int {
+	return len(v.Network)
+}

+ 4 - 0
proxy/dokodemo/dokodemo.go

@@ -74,6 +74,10 @@ func (v *DokodemoDoor) Start() error {
 	}
 	v.accepting = true
 
+	if v.config.NetworkList == nil || v.config.NetworkList.Size() == 0 {
+		return errors.New("DokodemoDoor: No network specified.")
+	}
+
 	if v.config.NetworkList.HasNetwork(v2net.Network_TCP) {
 		err := v.ListenTCP()
 		if err != nil {

+ 0 - 39
testing/scenarios/data/test_2_client.json

@@ -1,39 +0,0 @@
-{
-  "port": 50010,
-  "inbound": {
-    "listen": "127.0.0.1",
-    "protocol": "socks",
-    "settings": {
-      "auth": "noauth",
-      "udp": false,
-      "ip": "127.0.0.1"
-    }
-  },
-  "outbound": {
-    "protocol": "vmess",
-    "settings": {
-      "vnext": [
-        {
-          "address": "127.0.0.1",
-          "port": 50017,
-          "users": [
-            {"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f"}
-          ]
-        }
-      ]
-    }
-  },
-  "inboundDetour": [
-    {
-      "protocol": "dokodemo-door",
-      "listen": "127.0.0.1",
-      "port": "50011-50015",
-      "settings": {
-        "address": "127.0.0.1",
-        "port": 50016,
-        "network": "tcp",
-        "timeout": 0
-      }
-    }
-  ]
-}

+ 0 - 22
testing/scenarios/data/test_2_server.json

@@ -1,22 +0,0 @@
-{
-  "port": 50017,
-  "log": {
-    "loglevel": "none"
-  },
-  "inbound": {
-    "listen": "127.0.0.1",
-    "protocol": "vmess",
-    "settings": {
-      "clients": [
-        {
-          "id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f",
-          "level": 1
-        }
-      ]
-    }
-  },
-  "outbound": {
-    "protocol": "freedom",
-    "settings": {}
-  }
-}

+ 78 - 17
testing/scenarios/dokodemo_test.go

@@ -4,7 +4,16 @@ import (
 	"net"
 	"testing"
 
+	"v2ray.com/core"
 	v2net "v2ray.com/core/common/net"
+	"v2ray.com/core/common/protocol"
+	"v2ray.com/core/common/serial"
+	"v2ray.com/core/common/uuid"
+	"v2ray.com/core/proxy/dokodemo"
+	"v2ray.com/core/proxy/freedom"
+	"v2ray.com/core/proxy/vmess"
+	"v2ray.com/core/proxy/vmess/inbound"
+	"v2ray.com/core/proxy/vmess/outbound"
 	"v2ray.com/core/testing/assert"
 	"v2ray.com/core/testing/servers/tcp"
 )
@@ -12,25 +21,79 @@ import (
 func TestDokodemoTCP(t *testing.T) {
 	assert := assert.On(t)
 
-	tcpServer := &tcp.Server{
-		Port: v2net.Port(50016),
-		MsgProcessor: func(data []byte) []byte {
-			buffer := make([]byte, 0, 2048)
-			buffer = append(buffer, []byte("Processed: ")...)
-			buffer = append(buffer, data...)
-			return buffer
-		},
+	tcpServer := tcp.Server{
+		MsgProcessor: xor,
 	}
-	_, err := tcpServer.Start()
+	dest, err := tcpServer.Start()
 	assert.Error(err).IsNil()
 	defer tcpServer.Close()
 
-	assert.Error(InitializeServerSetOnce("test_2")).IsNil()
+	userID := protocol.NewID(uuid.New())
+	serverPort := pickPort()
+	serverConfig := &core.Config{
+		Inbound: []*core.InboundConnectionConfig{
+			{
+				PortRange: v2net.SinglePortRange(serverPort),
+				ListenOn:  v2net.NewIPOrDomain(v2net.LocalHostIP),
+				Settings: serial.ToTypedMessage(&inbound.Config{
+					User: []*protocol.User{
+						{
+							Account: serial.ToTypedMessage(&vmess.Account{
+								Id: userID.String(),
+							}),
+						},
+					},
+				}),
+			},
+		},
+		Outbound: []*core.OutboundConnectionConfig{
+			{
+				Settings: serial.ToTypedMessage(&freedom.Config{}),
+			},
+		},
+	}
 
-	dokodemoPortStart := v2net.Port(50011)
-	dokodemoPortEnd := v2net.Port(50015)
+	clientPort := uint32(pickPort())
+	clientPortRange := uint32(5)
+	clientConfig := &core.Config{
+		Inbound: []*core.InboundConnectionConfig{
+			{
+				PortRange: &v2net.PortRange{From: clientPort, To: clientPort + clientPortRange},
+				ListenOn:  v2net.NewIPOrDomain(v2net.LocalHostIP),
+				Settings: serial.ToTypedMessage(&dokodemo.Config{
+					Address: v2net.NewIPOrDomain(dest.Address),
+					Port:    uint32(dest.Port),
+					NetworkList: &v2net.NetworkList{
+						Network: []v2net.Network{v2net.Network_TCP},
+					},
+				}),
+			},
+		},
+		Outbound: []*core.OutboundConnectionConfig{
+			{
+				Settings: 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(),
+									}),
+								},
+							},
+						},
+					},
+				}),
+			},
+		},
+	}
 
-	for port := dokodemoPortStart; port <= dokodemoPortEnd; port++ {
+	assert.Error(InitializeServerConfig(serverConfig)).IsNil()
+	assert.Error(InitializeServerConfig(clientConfig)).IsNil()
+
+	for port := clientPort; port <= clientPort+clientPortRange; port++ {
 		conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
 			IP:   []byte{127, 0, 0, 1},
 			Port: int(port),
@@ -41,13 +104,11 @@ func TestDokodemoTCP(t *testing.T) {
 		assert.Error(err).IsNil()
 		assert.Int(nBytes).Equals(len(payload))
 
-		conn.CloseWrite()
-
 		response := make([]byte, 1024)
 		nBytes, err = conn.Read(response)
 		assert.Error(err).IsNil()
-		assert.String("Processed: " + payload).Equals(string(response[:nBytes]))
-		conn.Close()
+		assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload)))
+		assert.Error(conn.Close()).IsNil()
 	}
 
 	CloseAllServers()

+ 2 - 0
testing/scenarios/tls_test.go

@@ -42,6 +42,7 @@ func TestSimpleTLSConnection(t *testing.T) {
 	}
 	dest, err := tcpServer.Start()
 	assert.Error(err).IsNil()
+	defer tcpServer.Close()
 
 	userID := protocol.NewID(uuid.New())
 	serverPort := pickPort()
@@ -154,6 +155,7 @@ func TestTLSConnectionReuse(t *testing.T) {
 	}
 	dest, err := tcpServer.Start()
 	assert.Error(err).IsNil()
+	defer tcpServer.Close()
 
 	userID := protocol.NewID(uuid.New())
 	serverPort := pickPort()