From 03b8c8f9e88573b95e7b67f540b97463bb410c8c Mon Sep 17 00:00:00 2001 From: V2Ray Date: Sun, 1 Nov 2015 21:32:08 +0100 Subject: [PATCH] Port picker --- common/net/testing/port.go | 13 +++++++++++++ proxy/freedom/freedom_test.go | 12 +++++++----- proxy/socks/socks_test.go | 20 +++++++++++--------- proxy/vmess/vmess_test.go | 17 +++++++++-------- 4 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 common/net/testing/port.go diff --git a/common/net/testing/port.go b/common/net/testing/port.go new file mode 100644 index 00000000..7bf6a084 --- /dev/null +++ b/common/net/testing/port.go @@ -0,0 +1,13 @@ +package testing + +import ( + "sync/atomic" +) + +var ( + port = int32(30000) +) + +func PickPort() uint16 { + return uint16(atomic.AddInt32(&port, 1)) +} diff --git a/proxy/freedom/freedom_test.go b/proxy/freedom/freedom_test.go index 9af915b9..34233d58 100644 --- a/proxy/freedom/freedom_test.go +++ b/proxy/freedom/freedom_test.go @@ -2,6 +2,7 @@ package freedom import ( "bytes" + "fmt" "io/ioutil" "net" "testing" @@ -11,6 +12,7 @@ import ( "github.com/v2ray/v2ray-core/app/point" "github.com/v2ray/v2ray-core/common/alloc" v2net "github.com/v2ray/v2ray-core/common/net" + v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" "github.com/v2ray/v2ray-core/proxy/common/connhandler" _ "github.com/v2ray/v2ray-core/proxy/socks" "github.com/v2ray/v2ray-core/proxy/socks/config/json" @@ -47,7 +49,7 @@ func TestUDPSend(t *testing.T) { connhandler.RegisterInboundConnectionHandlerFactory("mock_ich", ich) - pointPort := uint16(38724) + pointPort := v2nettesting.PickPort() config := mocks.Config{ PortValue: pointPort, InboundConfigValue: &mocks.ConnectionConfig{ @@ -75,7 +77,7 @@ func TestUDPSend(t *testing.T) { func TestSocksTcpConnect(t *testing.T) { assert := unit.Assert(t) - port := uint16(38293) + port := v2nettesting.PickPort() data2Send := "Data to be sent to remote" @@ -91,7 +93,7 @@ func TestSocksTcpConnect(t *testing.T) { _, err := tcpServer.Start() assert.Error(err).IsNil() - pointPort := uint16(38724) + pointPort := v2nettesting.PickPort() config := mocks.Config{ PortValue: pointPort, InboundConfigValue: &mocks.ConnectionConfig{ @@ -112,10 +114,10 @@ func TestSocksTcpConnect(t *testing.T) { err = point.Start() assert.Error(err).IsNil() - socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:38724", nil, proxy.Direct) + socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", pointPort), nil, proxy.Direct) assert.Error(err).IsNil() - targetServer := "127.0.0.1:38293" + targetServer := fmt.Sprintf("127.0.0.1:%d", port) conn, err := socks5Client.Dial("tcp", targetServer) assert.Error(err).IsNil() diff --git a/proxy/socks/socks_test.go b/proxy/socks/socks_test.go index ade8b9f0..93bf11bd 100644 --- a/proxy/socks/socks_test.go +++ b/proxy/socks/socks_test.go @@ -2,6 +2,7 @@ package socks import ( "bytes" + "fmt" "io/ioutil" "net" "testing" @@ -9,6 +10,7 @@ import ( "golang.org/x/net/proxy" "github.com/v2ray/v2ray-core/app/point" + v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" "github.com/v2ray/v2ray-core/proxy/common/connhandler" "github.com/v2ray/v2ray-core/proxy/socks/config/json" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" @@ -18,7 +20,7 @@ import ( func TestSocksTcpConnect(t *testing.T) { assert := unit.Assert(t) - port := uint16(12385) + port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) @@ -49,7 +51,7 @@ func TestSocksTcpConnect(t *testing.T) { err = point.Start() assert.Error(err).IsNil() - socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12385", nil, proxy.Direct) + socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), nil, proxy.Direct) assert.Error(err).IsNil() targetServer := "google.com:80" @@ -73,7 +75,7 @@ func TestSocksTcpConnect(t *testing.T) { func TestSocksTcpConnectWithUserPass(t *testing.T) { assert := unit.Assert(t) - port := uint16(12386) + port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) @@ -110,7 +112,7 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) { err = point.Start() assert.Error(err).IsNil() - socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12386", &proxy.Auth{"userx", "passy"}, proxy.Direct) + socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), &proxy.Auth{"userx", "passy"}, proxy.Direct) assert.Error(err).IsNil() targetServer := "1.2.3.4:443" @@ -134,7 +136,7 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) { func TestSocksTcpConnectWithWrongUserPass(t *testing.T) { assert := unit.Assert(t) - port := uint16(12389) + port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) @@ -171,7 +173,7 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) { err = point.Start() assert.Error(err).IsNil() - socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12389", &proxy.Auth{"userx", "passz"}, proxy.Direct) + socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), &proxy.Auth{"userx", "passz"}, proxy.Direct) assert.Error(err).IsNil() targetServer := "1.2.3.4:443" @@ -181,7 +183,7 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) { func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) { assert := unit.Assert(t) - port := uint16(38405) + port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) @@ -218,7 +220,7 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) { err = point.Start() assert.Error(err).IsNil() - socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:38405", nil, proxy.Direct) + socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), nil, proxy.Direct) assert.Error(err).IsNil() targetServer := "1.2.3.4:443" @@ -228,7 +230,7 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) { func TestSocksUdpSend(t *testing.T) { assert := unit.Assert(t) - port := uint16(12372) + port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) diff --git a/proxy/vmess/vmess_test.go b/proxy/vmess/vmess_test.go index 097272d3..230decc2 100644 --- a/proxy/vmess/vmess_test.go +++ b/proxy/vmess/vmess_test.go @@ -7,6 +7,7 @@ import ( "github.com/v2ray/v2ray-core/app/point" "github.com/v2ray/v2ray-core/common/alloc" v2net "github.com/v2ray/v2ray-core/common/net" + v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" "github.com/v2ray/v2ray-core/proxy/common/connhandler" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" "github.com/v2ray/v2ray-core/proxy/vmess/config" @@ -21,7 +22,9 @@ func TestVMessInAndOut(t *testing.T) { testAccount, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") assert.Error(err).IsNil() - portA := uint16(17392) + portA := v2nettesting.PickPort() + portB := v2nettesting.PickPort() + ichConnInput := []byte("The data to be send to outbound server.") ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) ich := &proxymocks.InboundConnectionHandler{ @@ -42,7 +45,7 @@ func TestVMessInAndOut(t *testing.T) { SettingsValue: &json.Outbound{ []*json.ConfigTarget{ &json.ConfigTarget{ - Address: v2net.IPAddress([]byte{127, 0, 0, 1}, 13829), + Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB), TCPEnabled: true, Users: []*json.ConfigUser{ &json.ConfigUser{Id: testAccount}, @@ -59,8 +62,6 @@ func TestVMessInAndOut(t *testing.T) { err = pointA.Start() assert.Error(err).IsNil() - portB := uint16(13829) - ochConnInput := []byte("The data to be returned to inbound server.") ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) och := &proxymocks.OutboundConnectionHandler{ @@ -105,7 +106,9 @@ func TestVMessInAndOutUDP(t *testing.T) { testAccount, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") assert.Error(err).IsNil() - portA := uint16(17394) + portA := v2nettesting.PickPort() + portB := v2nettesting.PickPort() + ichConnInput := []byte("The data to be send to outbound server.") ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) ich := &proxymocks.InboundConnectionHandler{ @@ -126,7 +129,7 @@ func TestVMessInAndOutUDP(t *testing.T) { SettingsValue: &json.Outbound{ []*json.ConfigTarget{ &json.ConfigTarget{ - Address: v2net.IPAddress([]byte{127, 0, 0, 1}, 13841), + Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB), UDPEnabled: true, Users: []*json.ConfigUser{ &json.ConfigUser{Id: testAccount}, @@ -143,8 +146,6 @@ func TestVMessInAndOutUDP(t *testing.T) { err = pointA.Start() assert.Error(err).IsNil() - portB := uint16(13841) - ochConnInput := []byte("The data to be returned to inbound server.") ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) och := &proxymocks.OutboundConnectionHandler{