mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -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"
|
"net"
|
||||||
"testing"
|
"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"
|
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
|
||||||
_ "github.com/v2ray/v2ray-core/proxy/freedom"
|
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
|
||||||
"github.com/v2ray/v2ray-core/shell/point"
|
. "github.com/v2ray/v2ray-core/proxy/dokodemo"
|
||||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
"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) {
|
func TestDokodemoTCP(t *testing.T) {
|
||||||
v2testing.Current(t)
|
v2testing.Current(t)
|
||||||
|
|
||||||
port := v2nettesting.PickPort()
|
testPacketDispatcher := &testdispatcher.TestPacketDispatcher{}
|
||||||
|
|
||||||
data2Send := "Data to be sent to remote."
|
data2Send := "Data to be sent to remote."
|
||||||
|
|
||||||
tcpServer := &tcp.Server{
|
dokodemo := NewDokodemoDoor(&Config{
|
||||||
Port: port,
|
Address: v2net.IPAddress([]byte{1, 2, 3, 4}),
|
||||||
MsgProcessor: func(data []byte) []byte {
|
Port: 128,
|
||||||
buffer := make([]byte, 0, 2048)
|
Network: v2net.TCPNetwork.AsList(),
|
||||||
buffer = append(buffer, []byte("Processed: ")...)
|
Timeout: 600,
|
||||||
buffer = append(buffer, data...)
|
}, testPacketDispatcher)
|
||||||
return buffer
|
|
||||||
},
|
|
||||||
}
|
|
||||||
_, err := tcpServer.Start()
|
|
||||||
assert.Error(err).IsNil()
|
|
||||||
|
|
||||||
pointPort := v2nettesting.PickPort()
|
port := v2nettesting.PickPort()
|
||||||
config := &point.Config{
|
err := dokodemo.Listen(port)
|
||||||
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()
|
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
netassert.Port(port).Equals(dokodemo.Port())
|
||||||
|
|
||||||
tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
||||||
IP: []byte{127, 0, 0, 1},
|
IP: []byte{127, 0, 0, 1},
|
||||||
Port: int(pointPort),
|
Port: int(port),
|
||||||
Zone: "",
|
Zone: "",
|
||||||
})
|
})
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
@ -72,54 +48,33 @@ func TestDokodemoTCP(t *testing.T) {
|
|||||||
tcpClient.Close()
|
tcpClient.Close()
|
||||||
|
|
||||||
assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
|
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) {
|
func TestDokodemoUDP(t *testing.T) {
|
||||||
v2testing.Current(t)
|
v2testing.Current(t)
|
||||||
|
|
||||||
port := v2nettesting.PickPort()
|
testPacketDispatcher := &testdispatcher.TestPacketDispatcher{}
|
||||||
|
|
||||||
data2Send := "Data to be sent to remote."
|
data2Send := "Data to be sent to remote."
|
||||||
|
|
||||||
udpServer := &udp.Server{
|
dokodemo := NewDokodemoDoor(&Config{
|
||||||
Port: port,
|
Address: v2net.IPAddress([]byte{5, 6, 7, 8}),
|
||||||
MsgProcessor: func(data []byte) []byte {
|
Port: 256,
|
||||||
buffer := make([]byte, 0, 2048)
|
Network: v2net.UDPNetwork.AsList(),
|
||||||
buffer = append(buffer, []byte("Processed: ")...)
|
Timeout: 600,
|
||||||
buffer = append(buffer, data...)
|
}, testPacketDispatcher)
|
||||||
return buffer
|
|
||||||
},
|
|
||||||
}
|
|
||||||
_, err := udpServer.Start()
|
|
||||||
assert.Error(err).IsNil()
|
|
||||||
|
|
||||||
pointPort := v2nettesting.PickPort()
|
port := v2nettesting.PickPort()
|
||||||
config := &point.Config{
|
err := dokodemo.Listen(port)
|
||||||
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()
|
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
netassert.Port(port).Equals(dokodemo.Port())
|
||||||
|
|
||||||
udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{
|
udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{
|
||||||
IP: []byte{127, 0, 0, 1},
|
IP: []byte{127, 0, 0, 1},
|
||||||
Port: int(pointPort),
|
Port: int(port),
|
||||||
Zone: "",
|
Zone: "",
|
||||||
})
|
})
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
@ -132,4 +87,7 @@ func TestDokodemoUDP(t *testing.T) {
|
|||||||
udpClient.Close()
|
udpClient.Close()
|
||||||
|
|
||||||
assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
|
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