diff --git a/common/net/port.go b/common/net/port.go index fcb1f448..25f94e22 100644 --- a/common/net/port.go +++ b/common/net/port.go @@ -67,3 +67,10 @@ func (v PortRange) ToPort() Port { func (v PortRange) Contains(port Port) bool { return v.FromPort() <= port && port <= v.ToPort() } + +func SinglePortRange(v Port) *PortRange { + return &PortRange{ + From: uint32(v), + To: uint32(v), + } +} diff --git a/testing/scenarios/common.go b/testing/scenarios/common.go new file mode 100644 index 00000000..eb73a5be --- /dev/null +++ b/testing/scenarios/common.go @@ -0,0 +1,41 @@ +package scenarios + +import ( + "github.com/golang/protobuf/proto" + "sync/atomic" + "time" + "v2ray.com/core" + v2net "v2ray.com/core/common/net" +) + +var ( + port uint32 = 50000 +) + +func pickPort() v2net.Port { + return v2net.Port(atomic.AddUint32(&port, 1)) +} + +func InitializeServerConfig(config *core.Config) error { + err := BuildV2Ray() + if err != nil { + return err + } + + configBytes, err := proto.Marshal(config) + if err != nil { + return err + } + proc := RunV2RayProtobuf(configBytes) + + err = proc.Start() + if err != nil { + return err + } + + time.Sleep(time.Second) + + runningServers = append(runningServers, proc) + + return nil +} diff --git a/testing/scenarios/server_env_coverage.go b/testing/scenarios/server_env_coverage.go index 16a3cfd3..8a952990 100644 --- a/testing/scenarios/server_env_coverage.go +++ b/testing/scenarios/server_env_coverage.go @@ -7,6 +7,7 @@ import ( "os/exec" "path/filepath" + "bytes" "v2ray.com/core/common/uuid" ) @@ -32,3 +33,17 @@ func RunV2Ray(configFile string) *exec.Cmd { return proc } + +func RunV2RayProtobuf(config []byte) *exec.Cmd { + GenTestBinaryPath() + + covDir := filepath.Join(os.Getenv("GOPATH"), "out", "v2ray", "cov") + os.MkdirAll(covDir, os.ModeDir) + profile := uuid.New().String() + ".out" + proc := exec.Command(testBinaryPath, "-config=stdin:", "-format=pb", "-test.run", "TestRunMainForCoverage", "-test.coverprofile", profile, "-test.outputdir", covDir) + proc.Stdin = bytes.NewBuffer(config) + proc.Stderr = os.Stderr + proc.Stdout = os.Stdout + + return proc +} diff --git a/testing/scenarios/server_env_regular.go b/testing/scenarios/server_env_regular.go index c4671a9f..23b35761 100644 --- a/testing/scenarios/server_env_regular.go +++ b/testing/scenarios/server_env_regular.go @@ -3,6 +3,7 @@ package scenarios import ( + "bytes" "fmt" "os" "os/exec" @@ -27,3 +28,13 @@ func RunV2Ray(configFile string) *exec.Cmd { return proc } + +func RunV2RayProtobuf(config []byte) *exec.Cmd { + GenTestBinaryPath() + proc := exec.Command(testBinaryPath, "-config=stdin:", "-format=pb") + proc.Stdin = bytes.NewBuffer(config) + proc.Stderr = os.Stderr + proc.Stdout = os.Stdout + + return proc +} diff --git a/testing/scenarios/tls_test.go b/testing/scenarios/tls_test.go new file mode 100644 index 00000000..75c028d3 --- /dev/null +++ b/testing/scenarios/tls_test.go @@ -0,0 +1,15 @@ +package scenarios + +import ( + "v2ray.com/core" + v2net "v2ray.com/core/common/net" +) + +var clientConfig = &core.Config{ + Inbound: []*core.InboundConnectionConfig{ + { + PortRange: v2net.SinglePortRange(pickPort()), + ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP), + }, + }, +}