From 5ec30c3ad03d88f56747b0064e3d4c1b6bfa35b4 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 4 Dec 2015 16:32:42 +0000 Subject: [PATCH] more test cases --- testing/scenarios/data/test_1_client.json | 74 +++++++++++++++++++++- testing/scenarios/data/test_1_server.json | 16 ++++- testing/scenarios/data/test_3_client.json | 57 +++++++++++++++++ testing/scenarios/data/test_3_server.json | 18 ++++++ testing/scenarios/dokodemo_test.go | 6 +- testing/scenarios/router_test.go | 77 +++++++++++++++++++++++ testing/scenarios/server_env.go | 20 ++++++ testing/scenarios/socks_end_test.go | 24 +------ 8 files changed, 264 insertions(+), 28 deletions(-) create mode 100644 testing/scenarios/data/test_3_client.json create mode 100644 testing/scenarios/data/test_3_server.json create mode 100644 testing/scenarios/router_test.go diff --git a/testing/scenarios/data/test_1_client.json b/testing/scenarios/data/test_1_client.json index c833e891..6c26e904 100644 --- a/testing/scenarios/data/test_1_client.json +++ b/testing/scenarios/data/test_1_client.json @@ -21,5 +21,77 @@ } ] } - } + }, + "outboundDetour": [ + { + "protocol": "vmess", + "settings": { + "vnext": [ + { + "address": "127.0.0.1", + "port": 50005, + "users": [ + {"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f"} + ] + } + ] + } + }, + { + "protocol": "vmess", + "settings": { + "vnext": [ + { + "address": "127.0.0.1", + "port": 50006, + "users": [ + {"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f"} + ] + } + ] + } + }, + { + "protocol": "vmess", + "settings": { + "vnext": [ + { + "address": "127.0.0.1", + "port": 50007, + "users": [ + {"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f"} + ] + } + ] + } + }, + { + "protocol": "vmess", + "settings": { + "vnext": [ + { + "address": "127.0.0.1", + "port": 50008, + "users": [ + {"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f"} + ] + } + ] + } + }, + { + "protocol": "vmess", + "settings": { + "vnext": [ + { + "address": "127.0.0.1", + "port": 50009, + "users": [ + {"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f"} + ] + } + ] + } + } + ] } diff --git a/testing/scenarios/data/test_1_server.json b/testing/scenarios/data/test_1_server.json index 1e3d1c86..36841edc 100644 --- a/testing/scenarios/data/test_1_server.json +++ b/testing/scenarios/data/test_1_server.json @@ -14,5 +14,19 @@ "outbound": { "protocol": "freedom", "settings": {} - } + }, + "inboundDetour": [ + { + "protocol": "vmess", + "port": "50005-50009", + "settings": { + "clients": [ + { + "id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f", + "level": 1 + } + ] + } + } + ] } diff --git a/testing/scenarios/data/test_3_client.json b/testing/scenarios/data/test_3_client.json new file mode 100644 index 00000000..60f3d8f2 --- /dev/null +++ b/testing/scenarios/data/test_3_client.json @@ -0,0 +1,57 @@ +{ + "port": 50020, + "inbound": { + "protocol": "dokodemo-door", + "settings": { + "address": "127.0.0.1", + "port": 50024, + "network": "tcp", + "timeout": 0 + } + }, + "outbound": { + "protocol": "vmess", + "settings": { + "vnext": [ + { + "address": "127.0.0.1", + "port": 50021, + "users": [ + {"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f"} + ] + } + ] + } + }, + "inboundDetour": [ + { + "protocol": "dokodemo-door", + "port": 50022, + "settings": { + "address": "127.0.0.1", + "port": 50025, + "network": "tcp", + "timeout": 0 + } + } + ], + "outboundDetour": [ + { + "protocol": "blackhole", + "tag": "blocked", + "settings": {} + } + ], + "routing": { + "strategy": "rules", + "settings": { + "rules": [ + { + "type": "field", + "port": "50025-50029", + "outboundTag": "blocked" + } + ] + } + } +} diff --git a/testing/scenarios/data/test_3_server.json b/testing/scenarios/data/test_3_server.json new file mode 100644 index 00000000..d3f21ea6 --- /dev/null +++ b/testing/scenarios/data/test_3_server.json @@ -0,0 +1,18 @@ +{ + "port": 50021, + "inbound": { + "protocol": "vmess", + "settings": { + "clients": [ + { + "id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f", + "level": 1 + } + ] + } + }, + "outbound": { + "protocol": "freedom", + "settings": {} + } +} diff --git a/testing/scenarios/dokodemo_test.go b/testing/scenarios/dokodemo_test.go index 7d4f6fe1..26eaf938 100644 --- a/testing/scenarios/dokodemo_test.go +++ b/testing/scenarios/dokodemo_test.go @@ -25,11 +25,7 @@ func TestDokodemoTCP(t *testing.T) { _, err := tcpServer.Start() assert.Error(err).IsNil() - err = InitializeServer(TestFile("test_2_client.json")) - assert.Error(err).IsNil() - - err = InitializeServer(TestFile("test_2_server.json")) - assert.Error(err).IsNil() + assert.Error(InitializeServerSetOnce("test_2")).IsNil() dokodemoPortStart := v2net.Port(50011) dokodemoPortEnd := v2net.Port(50015) diff --git a/testing/scenarios/router_test.go b/testing/scenarios/router_test.go new file mode 100644 index 00000000..831fb2e8 --- /dev/null +++ b/testing/scenarios/router_test.go @@ -0,0 +1,77 @@ +package scenarios + +import ( + "net" + "testing" + + v2net "github.com/v2ray/v2ray-core/common/net" + v2testing "github.com/v2ray/v2ray-core/testing" + "github.com/v2ray/v2ray-core/testing/assert" + "github.com/v2ray/v2ray-core/testing/servers/tcp" +) + +func TestRouter(t *testing.T) { + v2testing.Current(t) + + tcpServer := &tcp.Server{ + Port: v2net.Port(50024), + MsgProcessor: func(data []byte) []byte { + buffer := make([]byte, 0, 2048) + buffer = append(buffer, []byte("Processed: ")...) + buffer = append(buffer, data...) + return buffer + }, + } + _, err := tcpServer.Start() + assert.Error(err).IsNil() + + tcpServer2Accessed := false + tcpServer2 := &tcp.Server{ + Port: v2net.Port(50025), + MsgProcessor: func(data []byte) []byte { + tcpServer2Accessed = true + return data + }, + } + _, err = tcpServer2.Start() + assert.Error(err).IsNil() + + assert.Error(InitializeServerSetOnce("test_3")).IsNil() + + conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ + IP: []byte{127, 0, 0, 1}, + Port: int(50020), + }) + + payload := "direct dokodemo request." + nBytes, err := conn.Write([]byte(payload)) + 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.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes])) + conn.Close() + + conn, err = net.DialTCP("tcp", nil, &net.TCPAddr{ + IP: []byte{127, 0, 0, 1}, + Port: int(50022), + }) + + payload = "blocked dokodemo request." + nBytes, err = conn.Write([]byte(payload)) + assert.Error(err).IsNil() + assert.Int(nBytes).Equals(len(payload)) + + conn.CloseWrite() + + response = make([]byte, 1024) + nBytes, err = conn.Read(response) + assert.Error(err).IsNotNil() + assert.Int(nBytes).Equals(0) + assert.Bool(tcpServer2Accessed).IsFalse() + conn.Close() +} diff --git a/testing/scenarios/server_env.go b/testing/scenarios/server_env.go index 7333d751..2d8489f4 100644 --- a/testing/scenarios/server_env.go +++ b/testing/scenarios/server_env.go @@ -24,10 +24,30 @@ import ( _ "github.com/v2ray/v2ray-core/proxy/vmess/config/json" ) +var ( + serverup = make(map[string]bool) +) + func TestFile(filename string) string { return filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "v2ray", "v2ray-core", "testing", "scenarios", "data", filename) } +func InitializeServerSetOnce(testcase string) error { + if up, found := serverup[testcase]; found && up { + return nil + } + err := InitializeServer(TestFile(testcase + "_server.json")) + if err != nil { + return err + } + err = InitializeServer(TestFile(testcase + "_client.json")) + if err != nil { + return err + } + serverup[testcase] = true + return nil +} + func InitializeServer(configFile string) error { config, err := jsonconf.LoadConfig(configFile) if err != nil { diff --git a/testing/scenarios/socks_end_test.go b/testing/scenarios/socks_end_test.go index 66108681..c8b58e02 100644 --- a/testing/scenarios/socks_end_test.go +++ b/testing/scenarios/socks_end_test.go @@ -16,24 +16,6 @@ var ( serverUp = false ) -func setupServer() error { - if serverUp { - return nil - } - err := InitializeServer(TestFile("test_1_client.json")) - if err != nil { - return err - } - - err = InitializeServer(TestFile("test_1_server.json")) - if err != nil { - return err - } - - serverUp = true - return nil -} - func TestTCPConnection(t *testing.T) { v2testing.Current(t) @@ -50,7 +32,7 @@ func TestTCPConnection(t *testing.T) { _, err := tcpServer.Start() assert.Error(err).IsNil() - assert.Error(setupServer()).IsNil() + assert.Error(InitializeServerSetOnce("test_1")).IsNil() socksPort := v2net.Port(50000) @@ -117,7 +99,7 @@ func TestTCPBind(t *testing.T) { _, err := tcpServer.Start() assert.Error(err).IsNil() - assert.Error(setupServer()).IsNil() + assert.Error(InitializeServerSetOnce("test_1")).IsNil() socksPort := v2net.Port(50000) @@ -165,7 +147,7 @@ func TestUDPAssociate(t *testing.T) { _, err := udpServer.Start() assert.Error(err).IsNil() - assert.Error(setupServer()).IsNil() + assert.Error(InitializeServerSetOnce("test_1")).IsNil() socksPort := v2net.Port(50000)