2016-01-15 06:43:06 -05:00
|
|
|
package dokodemo_test
|
2015-11-01 16:17:49 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
|
|
|
|
_ "github.com/v2ray/v2ray-core/proxy/freedom"
|
2015-11-29 08:45:32 -05:00
|
|
|
"github.com/v2ray/v2ray-core/shell/point"
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
|
|
|
"github.com/v2ray/v2ray-core/testing/assert"
|
2015-11-01 16:17:49 -05:00
|
|
|
"github.com/v2ray/v2ray-core/testing/servers/tcp"
|
2015-11-10 18:08:43 -05:00
|
|
|
"github.com/v2ray/v2ray-core/testing/servers/udp"
|
2015-11-01 16:17:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDokodemoTCP(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-01 16:17:49 -05:00
|
|
|
|
|
|
|
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()
|
2016-01-17 15:43:10 -05:00
|
|
|
config := &point.Config{
|
|
|
|
Port: pointPort,
|
|
|
|
InboundConfig: &point.ConnectionConfig{
|
|
|
|
Protocol: "dokodemo-door",
|
|
|
|
Settings: []byte(`{
|
2016-01-02 11:40:51 -05:00
|
|
|
"address": "127.0.0.1",
|
|
|
|
"port": ` + port.String() + `,
|
|
|
|
"network": "tcp",
|
|
|
|
"timeout": 0
|
|
|
|
}`),
|
2015-11-01 16:17:49 -05:00
|
|
|
},
|
2016-01-17 15:43:10 -05:00
|
|
|
OutboundConfig: &point.ConnectionConfig{
|
|
|
|
Protocol: "freedom",
|
|
|
|
Settings: nil,
|
2015-11-01 16:17:49 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-17 15:43:10 -05:00
|
|
|
point, err := point.NewPoint(config)
|
2015-11-01 16:17:49 -05:00
|
|
|
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()
|
|
|
|
|
2015-12-02 10:41:19 -05:00
|
|
|
assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
|
2015-11-01 16:17:49 -05:00
|
|
|
}
|
2015-11-10 18:08:43 -05:00
|
|
|
|
|
|
|
func TestDokodemoUDP(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-10 18:08:43 -05:00
|
|
|
|
|
|
|
port := v2nettesting.PickPort()
|
|
|
|
|
|
|
|
data2Send := "Data to be sent to remote."
|
|
|
|
|
|
|
|
udpServer := &udp.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 := udpServer.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
pointPort := v2nettesting.PickPort()
|
2016-01-17 15:43:10 -05:00
|
|
|
config := &point.Config{
|
|
|
|
Port: pointPort,
|
|
|
|
InboundConfig: &point.ConnectionConfig{
|
|
|
|
Protocol: "dokodemo-door",
|
|
|
|
Settings: []byte(`{
|
2016-01-02 11:40:51 -05:00
|
|
|
"address": "127.0.0.1",
|
|
|
|
"port": ` + port.String() + `,
|
|
|
|
"network": "udp",
|
|
|
|
"timeout": 0
|
|
|
|
}`),
|
2015-11-10 18:08:43 -05:00
|
|
|
},
|
2016-01-17 15:43:10 -05:00
|
|
|
OutboundConfig: &point.ConnectionConfig{
|
|
|
|
Protocol: "freedom",
|
|
|
|
Settings: nil,
|
2015-11-10 18:08:43 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-17 15:43:10 -05:00
|
|
|
point, err := point.NewPoint(config)
|
2015-11-10 18:08:43 -05:00
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
err = point.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
|
|
|
Port: int(pointPort),
|
|
|
|
Zone: "",
|
|
|
|
})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
udpClient.Write([]byte(data2Send))
|
|
|
|
|
|
|
|
response := make([]byte, 1024)
|
|
|
|
nBytes, err := udpClient.Read(response)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
udpClient.Close()
|
|
|
|
|
2015-12-02 10:41:19 -05:00
|
|
|
assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
|
2015-11-10 18:08:43 -05:00
|
|
|
}
|