V2Ray 10 gadi atpakaļ
vecāks
revīzija
03b8c8f9e8

+ 13 - 0
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))
+}

+ 7 - 5
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()
 

+ 11 - 9
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))

+ 9 - 8
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{