From 310474d59239763c62e1abfc3595de4f08986bdd Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 16 Dec 2016 21:39:00 +0100 Subject: [PATCH] init tls test --- common/net/port.go | 7 ++++ testing/scenarios/common.go | 41 ++++++++++++++++++++++++ testing/scenarios/server_env_coverage.go | 15 +++++++++ testing/scenarios/server_env_regular.go | 11 +++++++ testing/scenarios/tls_test.go | 15 +++++++++ 5 files changed, 89 insertions(+) create mode 100644 testing/scenarios/common.go create mode 100644 testing/scenarios/tls_test.go diff --git a/common/net/port.go b/common/net/port.go index fcb1f4484..25f94e229 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 000000000..eb73a5bee --- /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 16a3cfd3c..8a9529906 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 c4671a9f8..23b35761e 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 000000000..75c028d37 --- /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), + }, + }, +}