From 1c6b66eacf5f22e80c3bf5885879efaeb9646f5a Mon Sep 17 00:00:00 2001 From: V2Ray Date: Sun, 1 Nov 2015 22:17:49 +0100 Subject: [PATCH] Test case for dokodemo --- proxy/dokodemo/config/json/json.go | 3 -- proxy/dokodemo/dokodemo_test.go | 77 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 proxy/dokodemo/dokodemo_test.go diff --git a/proxy/dokodemo/config/json/json.go b/proxy/dokodemo/config/json/json.go index 0e8f4694d..6015dd6b6 100644 --- a/proxy/dokodemo/config/json/json.go +++ b/proxy/dokodemo/config/json/json.go @@ -1,7 +1,6 @@ package json import ( - v2net "github.com/v2ray/v2ray-core/common/net" v2netjson "github.com/v2ray/v2ray-core/common/net/json" "github.com/v2ray/v2ray-core/proxy/common/config/json" ) @@ -11,8 +10,6 @@ type DokodemoConfig struct { Port int `json:"port"` Network *v2netjson.NetworkList `json:"network"` Timeout int `json:"timeout"` - - address v2net.Address } func init() { diff --git a/proxy/dokodemo/dokodemo_test.go b/proxy/dokodemo/dokodemo_test.go new file mode 100644 index 000000000..d80a450fe --- /dev/null +++ b/proxy/dokodemo/dokodemo_test.go @@ -0,0 +1,77 @@ +package dokodemo + +import ( + "net" + "testing" + + "github.com/v2ray/v2ray-core/app/point" + v2netjson "github.com/v2ray/v2ray-core/common/net/json" + v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" + "github.com/v2ray/v2ray-core/proxy/dokodemo/config/json" + _ "github.com/v2ray/v2ray-core/proxy/freedom" + "github.com/v2ray/v2ray-core/testing/mocks" + "github.com/v2ray/v2ray-core/testing/servers/tcp" + "github.com/v2ray/v2ray-core/testing/unit" +) + +func TestDokodemoTCP(t *testing.T) { + assert := unit.Assert(t) + + port := v2nettesting.PickPort() + + data2Send := "Data to be sent to remote." + + tcpServer := &tcp.Server{ + Port: port, + 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() + + pointPort := v2nettesting.PickPort() + networkList := v2netjson.NetworkList([]string{"tcp"}) + config := mocks.Config{ + PortValue: pointPort, + InboundConfigValue: &mocks.ConnectionConfig{ + ProtocolValue: "dokodemo-door", + SettingsValue: &json.DokodemoConfig{ + Host: "127.0.0.1", + Port: int(port), + Network: &networkList, + Timeout: 0, + }, + }, + OutboundConfigValue: &mocks.ConnectionConfig{ + ProtocolValue: "freedom", + SettingsValue: nil, + }, + } + + point, err := point.NewPoint(&config) + assert.Error(err).IsNil() + + err = point.Start() + assert.Error(err).IsNil() + + tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{ + IP: []byte{127, 0, 0, 1}, + Port: int(pointPort), + Zone: "", + }) + assert.Error(err).IsNil() + + tcpClient.Write([]byte(data2Send)) + tcpClient.CloseWrite() + + response := make([]byte, 1024) + nBytes, err := tcpClient.Read(response) + assert.Error(err).IsNil() + tcpClient.Close() + + assert.String("Processed: " + data2Send).Equals(string(response[:nBytes])) +}