1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 02:35:23 +00:00
v2fly/testing/scenarios/common.go

118 lines
2.2 KiB
Go
Raw Normal View History

2016-12-16 20:39:00 +00:00
package scenarios
import (
2017-04-08 23:43:25 +00:00
"errors"
2017-03-31 15:12:33 +00:00
"fmt"
2017-01-29 08:02:19 +00:00
"io"
2017-03-31 15:12:33 +00:00
"io/ioutil"
2017-01-13 23:27:45 +00:00
"net"
2017-03-31 15:12:33 +00:00
"os"
"os/exec"
"path/filepath"
"runtime"
"sync"
2016-12-16 20:39:00 +00:00
"time"
2016-12-30 22:12:00 +00:00
"github.com/golang/protobuf/proto"
2016-12-16 20:39:00 +00:00
"v2ray.com/core"
2017-03-31 15:12:33 +00:00
"v2ray.com/core/app/log"
2017-01-16 13:18:13 +00:00
"v2ray.com/core/common"
2016-12-16 20:39:00 +00:00
v2net "v2ray.com/core/common/net"
2017-03-31 15:12:33 +00:00
"v2ray.com/core/common/retry"
2016-12-16 20:39:00 +00:00
)
func pickPort() v2net.Port {
2017-01-16 13:18:13 +00:00
listener, err := net.Listen("tcp4", ":0")
common.Must(err)
defer listener.Close()
addr := listener.Addr().(*net.TCPAddr)
return v2net.Port(addr.Port)
2016-12-16 20:39:00 +00:00
}
2016-12-30 22:12:00 +00:00
func xor(b []byte) []byte {
r := make([]byte, len(b))
for i, v := range b {
r[i] = v ^ 'c'
}
return r
}
func readFrom(conn net.Conn, timeout time.Duration, length int) []byte {
2017-02-06 14:36:55 +00:00
b := make([]byte, length)
2016-12-30 22:12:00 +00:00
deadline := time.Now().Add(timeout)
conn.SetReadDeadline(deadline)
2017-04-03 22:17:29 +00:00
n, err := io.ReadFull(conn, b[:length])
if err != nil {
fmt.Println("Unexpected error from readFrom:", err)
}
2017-01-29 08:02:19 +00:00
return b[:n]
2016-12-30 22:12:00 +00:00
}
2016-12-16 20:39:00 +00:00
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
}
2017-03-31 15:12:33 +00:00
var (
runningServers = make([]*exec.Cmd, 0, 10)
testBinaryPath string
testBinaryPathGen sync.Once
)
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)
})
}
func GetSourcePath() string {
return filepath.Join("v2ray.com", "core", "main")
}
func CloseAllServers() {
2017-04-06 19:13:17 +00:00
log.Trace(errors.New("Closing all servers."))
2017-03-31 15:12:33 +00:00
for _, server := range runningServers {
server.Process.Signal(os.Interrupt)
server.Process.Wait()
}
runningServers = make([]*exec.Cmd, 0, 10)
2017-04-06 19:13:17 +00:00
log.Trace(errors.New("All server closed."))
2017-03-31 15:12:33 +00:00
}