2016-01-15 06:43:06 -05:00
|
|
|
package dokodemo_test
|
2015-11-01 16:17:49 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2016-02-04 07:13:15 -05:00
|
|
|
testdispatcher "github.com/v2ray/v2ray-core/app/dispatcher/testing"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-11-01 16:17:49 -05:00
|
|
|
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
|
2016-02-04 07:13:15 -05:00
|
|
|
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
|
|
|
|
. "github.com/v2ray/v2ray-core/proxy/dokodemo"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func TestDokodemoTCP(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-01 16:17:49 -05:00
|
|
|
|
2016-02-04 07:27:35 -05:00
|
|
|
testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
|
2015-11-01 16:17:49 -05:00
|
|
|
|
|
|
|
data2Send := "Data to be sent to remote."
|
|
|
|
|
2016-02-04 07:13:15 -05:00
|
|
|
dokodemo := NewDokodemoDoor(&Config{
|
|
|
|
Address: v2net.IPAddress([]byte{1, 2, 3, 4}),
|
|
|
|
Port: 128,
|
|
|
|
Network: v2net.TCPNetwork.AsList(),
|
|
|
|
Timeout: 600,
|
|
|
|
}, testPacketDispatcher)
|
2015-11-01 16:17:49 -05:00
|
|
|
|
2016-02-04 07:13:15 -05:00
|
|
|
port := v2nettesting.PickPort()
|
|
|
|
err := dokodemo.Listen(port)
|
2015-11-01 16:17:49 -05:00
|
|
|
assert.Error(err).IsNil()
|
2016-02-04 07:13:15 -05:00
|
|
|
netassert.Port(port).Equals(dokodemo.Port())
|
2015-11-01 16:17:49 -05:00
|
|
|
|
|
|
|
tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
2016-02-04 07:13:15 -05:00
|
|
|
Port: int(port),
|
2015-11-01 16:17:49 -05:00
|
|
|
Zone: "",
|
|
|
|
})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
tcpClient.Write([]byte(data2Send))
|
|
|
|
tcpClient.CloseWrite()
|
|
|
|
|
2016-02-04 07:27:35 -05:00
|
|
|
lastPacket := <-testPacketDispatcher.LastPacket
|
|
|
|
|
2015-11-01 16:17:49 -05:00
|
|
|
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]))
|
2016-02-04 07:27:35 -05:00
|
|
|
assert.Bool(lastPacket.Destination().IsTCP()).IsTrue()
|
|
|
|
netassert.Address(lastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{1, 2, 3, 4}))
|
|
|
|
netassert.Port(lastPacket.Destination().Port()).Equals(128)
|
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
|
|
|
|
2016-02-04 07:27:35 -05:00
|
|
|
testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
|
2015-11-10 18:08:43 -05:00
|
|
|
|
|
|
|
data2Send := "Data to be sent to remote."
|
|
|
|
|
2016-02-04 07:13:15 -05:00
|
|
|
dokodemo := NewDokodemoDoor(&Config{
|
|
|
|
Address: v2net.IPAddress([]byte{5, 6, 7, 8}),
|
|
|
|
Port: 256,
|
|
|
|
Network: v2net.UDPNetwork.AsList(),
|
|
|
|
Timeout: 600,
|
|
|
|
}, testPacketDispatcher)
|
2015-11-10 18:08:43 -05:00
|
|
|
|
2016-02-04 07:13:15 -05:00
|
|
|
port := v2nettesting.PickPort()
|
|
|
|
err := dokodemo.Listen(port)
|
2015-11-10 18:08:43 -05:00
|
|
|
assert.Error(err).IsNil()
|
2016-02-04 07:13:15 -05:00
|
|
|
netassert.Port(port).Equals(dokodemo.Port())
|
2015-11-10 18:08:43 -05:00
|
|
|
|
|
|
|
udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
2016-02-04 07:13:15 -05:00
|
|
|
Port: int(port),
|
2015-11-10 18:08:43 -05:00
|
|
|
Zone: "",
|
|
|
|
})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
udpClient.Write([]byte(data2Send))
|
|
|
|
udpClient.Close()
|
|
|
|
|
2016-02-04 07:27:35 -05:00
|
|
|
lastPacket := <-testPacketDispatcher.LastPacket
|
|
|
|
|
|
|
|
assert.StringLiteral(data2Send).Equals(string(lastPacket.Chunk().Value))
|
|
|
|
assert.Bool(lastPacket.Destination().IsUDP()).IsTrue()
|
|
|
|
netassert.Address(lastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{5, 6, 7, 8}))
|
|
|
|
netassert.Port(lastPacket.Destination().Port()).Equals(256)
|
2015-11-10 18:08:43 -05:00
|
|
|
}
|