2015-12-04 10:49:10 -05:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
2015-12-04 10:49:45 -05:00
|
|
|
"os"
|
2016-06-03 18:38:22 -04:00
|
|
|
"os/exec"
|
2015-12-04 10:49:45 -05:00
|
|
|
"path/filepath"
|
2016-06-04 06:41:48 -04:00
|
|
|
"runtime"
|
2016-06-04 07:27:17 -04:00
|
|
|
"time"
|
2015-12-04 10:49:10 -05:00
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/log"
|
2015-12-04 10:49:10 -05:00
|
|
|
|
2016-11-17 18:01:37 -05:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"sync"
|
2016-11-04 20:00:20 -04:00
|
|
|
_ "v2ray.com/core"
|
2016-11-17 18:01:37 -05:00
|
|
|
"v2ray.com/core/common/retry"
|
2015-12-04 10:49:10 -05:00
|
|
|
)
|
|
|
|
|
2015-12-04 11:32:42 -05:00
|
|
|
var (
|
2016-11-17 18:01:37 -05:00
|
|
|
runningServers = make([]*exec.Cmd, 0, 10)
|
|
|
|
testBinaryPath string
|
|
|
|
testBinaryPathGen sync.Once
|
2015-12-04 11:32:42 -05:00
|
|
|
)
|
|
|
|
|
2016-11-17 18:01:37 -05:00
|
|
|
func GenTestBinaryPath() {
|
|
|
|
testBinaryPathGen.Do(func() {
|
|
|
|
var tempDir string
|
|
|
|
err := retry.Timed(5, 100).On(func() error {
|
|
|
|
dir, err := ioutil.TempDir("", "v2ray")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tempDir = dir
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
file := filepath.Join(tempDir, "v2ray.test")
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
file += ".exe"
|
|
|
|
}
|
|
|
|
testBinaryPath = file
|
|
|
|
fmt.Printf("Generated binary path: %s\n", file)
|
|
|
|
})
|
2016-08-09 16:13:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetSourcePath() string {
|
2016-10-12 10:46:02 -04:00
|
|
|
return filepath.Join("v2ray.com", "core", "main")
|
2016-06-03 18:38:22 -04:00
|
|
|
}
|
|
|
|
|
2015-12-04 10:49:10 -05:00
|
|
|
func TestFile(filename string) string {
|
2016-08-20 15:03:50 -04:00
|
|
|
return filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "scenarios", "data", filename)
|
2015-12-04 10:49:10 -05:00
|
|
|
}
|
|
|
|
|
2015-12-04 11:32:42 -05:00
|
|
|
func InitializeServerSetOnce(testcase string) error {
|
2016-01-28 07:40:00 -05:00
|
|
|
if err := InitializeServerServer(testcase); err != nil {
|
2015-12-04 11:32:42 -05:00
|
|
|
return err
|
|
|
|
}
|
2016-01-28 07:40:00 -05:00
|
|
|
if err := InitializeServerClient(testcase); err != nil {
|
2015-12-04 11:32:42 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-28 07:40:00 -05:00
|
|
|
func InitializeServerServer(testcase string) error {
|
|
|
|
return InitializeServer(TestFile(testcase + "_server.json"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitializeServerClient(testcase string) error {
|
|
|
|
return InitializeServer(TestFile(testcase + "_client.json"))
|
|
|
|
}
|
|
|
|
|
2015-12-04 10:49:10 -05:00
|
|
|
func InitializeServer(configFile string) error {
|
2016-06-03 18:38:22 -04:00
|
|
|
err := BuildV2Ray()
|
2015-12-04 10:49:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:55:36 -04:00
|
|
|
proc := RunV2Ray(configFile)
|
2015-12-04 10:49:10 -05:00
|
|
|
|
2016-06-03 18:38:22 -04:00
|
|
|
err = proc.Start()
|
2015-12-04 10:49:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-03 18:38:22 -04:00
|
|
|
|
2016-06-04 07:27:17 -04:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2016-06-03 18:38:22 -04:00
|
|
|
runningServers = append(runningServers, proc)
|
2015-12-04 10:49:10 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-03 18:33:25 -05:00
|
|
|
|
|
|
|
func CloseAllServers() {
|
2016-06-03 18:38:22 -04:00
|
|
|
log.Info("Closing all servers.")
|
2016-01-03 18:33:25 -05:00
|
|
|
for _, server := range runningServers {
|
2016-08-09 16:13:16 -04:00
|
|
|
server.Process.Signal(os.Interrupt)
|
|
|
|
server.Process.Wait()
|
2016-01-03 18:33:25 -05:00
|
|
|
}
|
2016-06-03 18:38:22 -04:00
|
|
|
runningServers = make([]*exec.Cmd, 0, 10)
|
|
|
|
log.Info("All server closed.")
|
2016-01-03 18:33:25 -05:00
|
|
|
}
|