Skip to content

Commit

Permalink
convert dokodemo scenario test to protobuf based
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jan 5, 2017
1 parent f772102 commit a704d7b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 78 deletions.
4 changes: 4 additions & 0 deletions common/net/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ func (v NetworkList) HasNetwork(network Network) bool {
func (v NetworkList) Get(idx int) Network {
return v.Network[idx]
}

func (v NetworkList) Size() int {
return len(v.Network)
}
4 changes: 4 additions & 0 deletions proxy/dokodemo/dokodemo.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func (v *DokodemoDoor) Start() error {
}
v.accepting = true

if v.config.NetworkList == nil || v.config.NetworkList.Size() == 0 {
return errors.New("DokodemoDoor: No network specified.")
}

if v.config.NetworkList.HasNetwork(v2net.Network_TCP) {
err := v.ListenTCP()
if err != nil {
Expand Down
39 changes: 0 additions & 39 deletions testing/scenarios/data/test_2_client.json

This file was deleted.

22 changes: 0 additions & 22 deletions testing/scenarios/data/test_2_server.json

This file was deleted.

95 changes: 78 additions & 17 deletions testing/scenarios/dokodemo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,96 @@ import (
"net"
"testing"

"v2ray.com/core"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
"v2ray.com/core/common/uuid"
"v2ray.com/core/proxy/dokodemo"
"v2ray.com/core/proxy/freedom"
"v2ray.com/core/proxy/vmess"
"v2ray.com/core/proxy/vmess/inbound"
"v2ray.com/core/proxy/vmess/outbound"
"v2ray.com/core/testing/assert"
"v2ray.com/core/testing/servers/tcp"
)

func TestDokodemoTCP(t *testing.T) {
assert := assert.On(t)

tcpServer := &tcp.Server{
Port: v2net.Port(50016),
MsgProcessor: func(data []byte) []byte {
buffer := make([]byte, 0, 2048)
buffer = append(buffer, []byte("Processed: ")...)
buffer = append(buffer, data...)
return buffer
},
tcpServer := tcp.Server{
MsgProcessor: xor,
}
_, err := tcpServer.Start()
dest, err := tcpServer.Start()
assert.Error(err).IsNil()
defer tcpServer.Close()

assert.Error(InitializeServerSetOnce("test_2")).IsNil()
userID := protocol.NewID(uuid.New())
serverPort := pickPort()
serverConfig := &core.Config{
Inbound: []*core.InboundConnectionConfig{
{
PortRange: v2net.SinglePortRange(serverPort),
ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
Settings: serial.ToTypedMessage(&inbound.Config{
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundConnectionConfig{
{
Settings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}

dokodemoPortStart := v2net.Port(50011)
dokodemoPortEnd := v2net.Port(50015)
clientPort := uint32(pickPort())
clientPortRange := uint32(5)
clientConfig := &core.Config{
Inbound: []*core.InboundConnectionConfig{
{
PortRange: &v2net.PortRange{From: clientPort, To: clientPort + clientPortRange},
ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
Settings: serial.ToTypedMessage(&dokodemo.Config{
Address: v2net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP},
},
}),
},
},
Outbound: []*core.OutboundConnectionConfig{
{
Settings: serial.ToTypedMessage(&outbound.Config{
Receiver: []*protocol.ServerEndpoint{
{
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
}),
},
},
},
},
}),
},
},
}

for port := dokodemoPortStart; port <= dokodemoPortEnd; port++ {
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
assert.Error(InitializeServerConfig(clientConfig)).IsNil()

for port := clientPort; port <= clientPort+clientPortRange; port++ {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(port),
Expand All @@ -41,13 +104,11 @@ func TestDokodemoTCP(t *testing.T) {
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(len(payload))

conn.CloseWrite()

response := make([]byte, 1024)
nBytes, err = conn.Read(response)
assert.Error(err).IsNil()
assert.String("Processed: " + payload).Equals(string(response[:nBytes]))
conn.Close()
assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload)))
assert.Error(conn.Close()).IsNil()
}

CloseAllServers()
Expand Down
2 changes: 2 additions & 0 deletions testing/scenarios/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestSimpleTLSConnection(t *testing.T) {
}
dest, err := tcpServer.Start()
assert.Error(err).IsNil()
defer tcpServer.Close()

userID := protocol.NewID(uuid.New())
serverPort := pickPort()
Expand Down Expand Up @@ -154,6 +155,7 @@ func TestTLSConnectionReuse(t *testing.T) {
}
dest, err := tcpServer.Start()
assert.Error(err).IsNil()
defer tcpServer.Close()

userID := protocol.NewID(uuid.New())
serverPort := pickPort()
Expand Down

0 comments on commit a704d7b

Please sign in to comment.