mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 17:46:58 -05:00
clean up test cases for dokodemo
This commit is contained in:
parent
77f5c15dbc
commit
3212337aa3
28
app/dispatcher/testing/dispatcher.go
Normal file
28
app/dispatcher/testing/dispatcher.go
Normal file
@ -0,0 +1,28 @@
|
||||
package testing
|
||||
|
||||
import (
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/transport/ray"
|
||||
)
|
||||
|
||||
type TestPacketDispatcher struct {
|
||||
LastPacket v2net.Packet
|
||||
Handler func(packet v2net.Packet, traffic ray.OutboundRay)
|
||||
}
|
||||
|
||||
func (this *TestPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
|
||||
traffic := ray.NewRay()
|
||||
this.LastPacket = packet
|
||||
if this.Handler == nil {
|
||||
go func() {
|
||||
for payload := range traffic.OutboundInput() {
|
||||
traffic.OutboundOutput() <- payload.Prepend([]byte("Processed: "))
|
||||
}
|
||||
close(traffic.OutboundOutput())
|
||||
}()
|
||||
} else {
|
||||
go this.Handler(packet, traffic)
|
||||
}
|
||||
|
||||
return traffic
|
||||
}
|
@ -4,61 +4,37 @@ import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
testdispatcher "github.com/v2ray/v2ray-core/app/dispatcher/testing"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
|
||||
_ "github.com/v2ray/v2ray-core/proxy/freedom"
|
||||
"github.com/v2ray/v2ray-core/shell/point"
|
||||
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
|
||||
. "github.com/v2ray/v2ray-core/proxy/dokodemo"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
"github.com/v2ray/v2ray-core/testing/servers/tcp"
|
||||
"github.com/v2ray/v2ray-core/testing/servers/udp"
|
||||
)
|
||||
|
||||
func TestDokodemoTCP(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
port := v2nettesting.PickPort()
|
||||
testPacketDispatcher := &testdispatcher.TestPacketDispatcher{}
|
||||
|
||||
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()
|
||||
dokodemo := NewDokodemoDoor(&Config{
|
||||
Address: v2net.IPAddress([]byte{1, 2, 3, 4}),
|
||||
Port: 128,
|
||||
Network: v2net.TCPNetwork.AsList(),
|
||||
Timeout: 600,
|
||||
}, testPacketDispatcher)
|
||||
|
||||
pointPort := v2nettesting.PickPort()
|
||||
config := &point.Config{
|
||||
Port: pointPort,
|
||||
InboundConfig: &point.ConnectionConfig{
|
||||
Protocol: "dokodemo-door",
|
||||
Settings: []byte(`{
|
||||
"address": "127.0.0.1",
|
||||
"port": ` + port.String() + `,
|
||||
"network": "tcp",
|
||||
"timeout": 0
|
||||
}`),
|
||||
},
|
||||
OutboundConfig: &point.ConnectionConfig{
|
||||
Protocol: "freedom",
|
||||
Settings: nil,
|
||||
},
|
||||
}
|
||||
|
||||
point, err := point.NewPoint(config)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
err = point.Start()
|
||||
port := v2nettesting.PickPort()
|
||||
err := dokodemo.Listen(port)
|
||||
assert.Error(err).IsNil()
|
||||
netassert.Port(port).Equals(dokodemo.Port())
|
||||
|
||||
tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
||||
IP: []byte{127, 0, 0, 1},
|
||||
Port: int(pointPort),
|
||||
Port: int(port),
|
||||
Zone: "",
|
||||
})
|
||||
assert.Error(err).IsNil()
|
||||
@ -72,54 +48,33 @@ func TestDokodemoTCP(t *testing.T) {
|
||||
tcpClient.Close()
|
||||
|
||||
assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
|
||||
assert.Bool(testPacketDispatcher.LastPacket.Destination().IsTCP()).IsTrue()
|
||||
netassert.Address(testPacketDispatcher.LastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{1, 2, 3, 4}))
|
||||
netassert.Port(testPacketDispatcher.LastPacket.Destination().Port()).Equals(128)
|
||||
}
|
||||
|
||||
func TestDokodemoUDP(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
port := v2nettesting.PickPort()
|
||||
testPacketDispatcher := &testdispatcher.TestPacketDispatcher{}
|
||||
|
||||
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()
|
||||
dokodemo := NewDokodemoDoor(&Config{
|
||||
Address: v2net.IPAddress([]byte{5, 6, 7, 8}),
|
||||
Port: 256,
|
||||
Network: v2net.UDPNetwork.AsList(),
|
||||
Timeout: 600,
|
||||
}, testPacketDispatcher)
|
||||
|
||||
pointPort := v2nettesting.PickPort()
|
||||
config := &point.Config{
|
||||
Port: pointPort,
|
||||
InboundConfig: &point.ConnectionConfig{
|
||||
Protocol: "dokodemo-door",
|
||||
Settings: []byte(`{
|
||||
"address": "127.0.0.1",
|
||||
"port": ` + port.String() + `,
|
||||
"network": "udp",
|
||||
"timeout": 0
|
||||
}`),
|
||||
},
|
||||
OutboundConfig: &point.ConnectionConfig{
|
||||
Protocol: "freedom",
|
||||
Settings: nil,
|
||||
},
|
||||
}
|
||||
|
||||
point, err := point.NewPoint(config)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
err = point.Start()
|
||||
port := v2nettesting.PickPort()
|
||||
err := dokodemo.Listen(port)
|
||||
assert.Error(err).IsNil()
|
||||
netassert.Port(port).Equals(dokodemo.Port())
|
||||
|
||||
udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{
|
||||
IP: []byte{127, 0, 0, 1},
|
||||
Port: int(pointPort),
|
||||
Port: int(port),
|
||||
Zone: "",
|
||||
})
|
||||
assert.Error(err).IsNil()
|
||||
@ -132,4 +87,7 @@ func TestDokodemoUDP(t *testing.T) {
|
||||
udpClient.Close()
|
||||
|
||||
assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
|
||||
assert.Bool(testPacketDispatcher.LastPacket.Destination().IsUDP()).IsTrue()
|
||||
netassert.Address(testPacketDispatcher.LastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{5, 6, 7, 8}))
|
||||
netassert.Port(testPacketDispatcher.LastPacket.Destination().Port()).Equals(256)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user