refactor ci test

This commit is contained in:
fatedier 2018-07-11 23:27:47 +08:00
parent 42ee536dae
commit 0d02f291e3
13 changed files with 240 additions and 186 deletions

View File

@ -26,24 +26,18 @@ frpc:
test: gotest test: gotest
gotest: gotest:
go test -v ./assets/... go test -v --cover ./assets/...
go test -v ./client/... go test -v --cover ./client/...
go test -v ./cmd/... go test -v --cover ./cmd/...
go test -v ./models/... go test -v --cover ./models/...
go test -v ./server/... go test -v --cover ./server/...
go test -v ./utils/... go test -v --cover ./utils/...
ci: ci:
cd ./tests && ./run_test.sh && cd - go test -count=1 -v ./tests/...
go test -v ./tests/...
cd ./tests && ./clean_test.sh && cd -
cic:
cd ./tests && ./clean_test.sh && cd -
alltest: gotest ci alltest: gotest ci
clean: clean:
rm -f ./bin/frpc rm -f ./bin/frpc
rm -f ./bin/frps rm -f ./bin/frps
cd ./tests && ./clean_test.sh && cd -

View File

@ -1,20 +0,0 @@
#!/bin/bash
pid=`ps aux|grep './../bin/frps -c ./conf/auto_test_frps.ini'|grep -v grep|awk {'print $2'}`
if [ -n "${pid}" ]; then
kill ${pid}
fi
pid=`ps aux|grep './../bin/frpc -c ./conf/auto_test_frpc.ini'|grep -v grep|awk {'print $2'}`
if [ -n "${pid}" ]; then
kill ${pid}
fi
pid=`ps aux|grep './../bin/frpc -c ./conf/auto_test_frpc_visitor.ini'|grep -v grep|awk {'print $2'}`
if [ -n "${pid}" ]; then
kill ${pid}
fi
rm -f ./frps.log
rm -f ./frpc.log
rm -f ./frpc_visitor.log

View File

@ -1,7 +1,7 @@
[common] [common]
server_addr = 127.0.0.1 server_addr = 127.0.0.1
server_port = 10700 server_port = 10700
log_file = ./frpc.log log_file = console
# debug, info, warn, error # debug, info, warn, error
log_level = debug log_level = debug
token = 123456 token = 123456

View File

@ -1,7 +1,7 @@
[common] [common]
server_addr = 0.0.0.0 server_addr = 0.0.0.0
server_port = 10700 server_port = 10700
log_file = ./frpc_visitor.log log_file = console
# debug, info, warn, error # debug, info, warn, error
log_level = debug log_level = debug
token = 123456 token = 123456

View File

@ -2,7 +2,7 @@
bind_addr = 0.0.0.0 bind_addr = 0.0.0.0
bind_port = 10700 bind_port = 10700
vhost_http_port = 10804 vhost_http_port = 10804
log_file = ./frps.log log_file = console
log_level = debug log_level = debug
token = 123456 token = 123456
allow_ports = 10000-20000,20002,30000-50000 allow_ports = 10000-20000,20002,30000-50000

9
tests/config/config.go Normal file
View File

@ -0,0 +1,9 @@
package util
import (
"io/ioutil"
)
func GenerateConfigFile(path string, content string) error {
return ioutil.WriteFile(path, []byte(content), 0666)
}

65
tests/consts/consts.go Normal file
View File

@ -0,0 +1,65 @@
package consts
import "path/filepath"
var (
FRPS_BIN_PATH = "../bin/frps"
FRPC_BIN_PATH = "../bin/frpc"
SERVER_ADDR = "127.0.0.1"
ADMIN_ADDR = "127.0.0.1:10600"
ADMIN_USER = "abc"
ADMIN_PWD = "abc"
TEST_STR = "frp is a fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet."
TEST_TCP_PORT int = 10701
TEST_TCP2_PORT int = 10702
TEST_TCP_FRP_PORT int = 10801
TEST_TCP2_FRP_PORT int = 10802
TEST_TCP_EC_FRP_PORT int = 10901
TEST_TCP_ECHO_STR string = "tcp type:" + TEST_STR
TEST_UDP_PORT int = 10702
TEST_UDP_FRP_PORT int = 10802
TEST_UDP_EC_FRP_PORT int = 10902
TEST_UDP_ECHO_STR string = "udp type:" + TEST_STR
TEST_UNIX_DOMAIN_ADDR string = "/tmp/frp_echo_server.sock"
TEST_UNIX_DOMAIN_FRP_PORT int = 10803
TEST_UNIX_DOMAIN_STR string = "unix domain type:" + TEST_STR
TEST_HTTP_PORT int = 10704
TEST_HTTP_FRP_PORT int = 10804
TEST_HTTP_NORMAL_STR string = "http normal string: " + TEST_STR
TEST_HTTP_FOO_STR string = "http foo string: " + TEST_STR
TEST_HTTP_BAR_STR string = "http bar string: " + TEST_STR
TEST_STCP_FRP_PORT int = 10805
TEST_STCP_EC_FRP_PORT int = 10905
TEST_STCP_ECHO_STR string = "stcp type:" + TEST_STR
ProxyTcpPortNotAllowed string = "tcp_port_not_allowed"
ProxyTcpPortUnavailable string = "tcp_port_unavailable"
ProxyTcpPortNormal string = "tcp_port_normal"
ProxyTcpRandomPort string = "tcp_random_port"
ProxyUdpPortNotAllowed string = "udp_port_not_allowed"
ProxyUdpPortNormal string = "udp_port_normal"
ProxyUdpRandomPort string = "udp_random_port"
ProxyHttpProxy string = "http_proxy"
ProxyRangeTcpPrefix string = "range_tcp"
)
func init() {
if path, err := filepath.Abs(FRPS_BIN_PATH); err != nil {
panic(err)
} else {
FRPS_BIN_PATH = path
}
if path, err := filepath.Abs(FRPC_BIN_PATH); err != nil {
panic(err)
} else {
FRPC_BIN_PATH = path
}
}

View File

@ -13,194 +13,179 @@ import (
"github.com/fatedier/frp/client" "github.com/fatedier/frp/client"
"github.com/fatedier/frp/server/ports" "github.com/fatedier/frp/server/ports"
"github.com/fatedier/frp/tests/consts"
"github.com/fatedier/frp/tests/mock"
"github.com/fatedier/frp/tests/util"
gnet "github.com/fatedier/golib/net" gnet "github.com/fatedier/golib/net"
) )
var (
SERVER_ADDR = "127.0.0.1"
ADMIN_ADDR = "127.0.0.1:10600"
ADMIN_USER = "abc"
ADMIN_PWD = "abc"
TEST_STR = "frp is a fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet."
TEST_TCP_PORT int = 10701
TEST_TCP2_PORT int = 10702
TEST_TCP_FRP_PORT int = 10801
TEST_TCP2_FRP_PORT int = 10802
TEST_TCP_EC_FRP_PORT int = 10901
TEST_TCP_ECHO_STR string = "tcp type:" + TEST_STR
TEST_UDP_PORT int = 10702
TEST_UDP_FRP_PORT int = 10802
TEST_UDP_EC_FRP_PORT int = 10902
TEST_UDP_ECHO_STR string = "udp type:" + TEST_STR
TEST_UNIX_DOMAIN_ADDR string = "/tmp/frp_echo_server.sock"
TEST_UNIX_DOMAIN_FRP_PORT int = 10803
TEST_UNIX_DOMAIN_STR string = "unix domain type:" + TEST_STR
TEST_HTTP_PORT int = 10704
TEST_HTTP_FRP_PORT int = 10804
TEST_HTTP_NORMAL_STR string = "http normal string: " + TEST_STR
TEST_HTTP_FOO_STR string = "http foo string: " + TEST_STR
TEST_HTTP_BAR_STR string = "http bar string: " + TEST_STR
TEST_STCP_FRP_PORT int = 10805
TEST_STCP_EC_FRP_PORT int = 10905
TEST_STCP_ECHO_STR string = "stcp type:" + TEST_STR
ProxyTcpPortNotAllowed string = "tcp_port_not_allowed"
ProxyTcpPortUnavailable string = "tcp_port_unavailable"
ProxyTcpPortNormal string = "tcp_port_normal"
ProxyTcpRandomPort string = "tcp_random_port"
ProxyUdpPortNotAllowed string = "udp_port_not_allowed"
ProxyUdpPortNormal string = "udp_port_normal"
ProxyUdpRandomPort string = "udp_random_port"
ProxyHttpProxy string = "http_proxy"
ProxyRangeTcpPrefix string = "range_tcp"
)
func init() { func init() {
go StartTcpEchoServer() go mock.StartTcpEchoServer(consts.TEST_TCP_PORT)
go StartTcpEchoServer2() go mock.StartTcpEchoServer2(consts.TEST_TCP2_PORT)
go StartUdpEchoServer() go mock.StartUdpEchoServer(consts.TEST_UDP_PORT)
go StartUnixDomainServer() go mock.StartUnixDomainServer(consts.TEST_UNIX_DOMAIN_ADDR)
go StartHttpServer() go mock.StartHttpServer(consts.TEST_HTTP_PORT)
if err := runFrps(); err != nil {
panic(err)
}
time.Sleep(200 * time.Millisecond)
if err := runFrpc(); err != nil {
panic(err)
}
if err := runFrpcVisitor(); err != nil {
panic(err)
}
time.Sleep(500 * time.Millisecond) time.Sleep(500 * time.Millisecond)
} }
func runFrps() error {
p := util.NewProcess(consts.FRPS_BIN_PATH, []string{"-c", "./config/auto_test_frps.ini"})
return p.Start()
}
func runFrpc() error {
p := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./config/auto_test_frpc.ini"})
return p.Start()
}
func runFrpcVisitor() error {
p := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./config/auto_test_frpc_visitor.ini"})
return p.Start()
}
func TestTcp(t *testing.T) { func TestTcp(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// Normal // Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_TCP_FRP_PORT) addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP_FRP_PORT)
res, err := sendTcpMsg(addr, TEST_TCP_ECHO_STR) res, err := util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
assert.NoError(err) assert.NoError(err)
assert.Equal(TEST_TCP_ECHO_STR, res) assert.Equal(consts.TEST_TCP_ECHO_STR, res)
// Encrytion and compression // Encrytion and compression
addr = fmt.Sprintf("127.0.0.1:%d", TEST_TCP_EC_FRP_PORT) addr = fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP_EC_FRP_PORT)
res, err = sendTcpMsg(addr, TEST_TCP_ECHO_STR) res, err = util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
assert.NoError(err) assert.NoError(err)
assert.Equal(TEST_TCP_ECHO_STR, res) assert.Equal(consts.TEST_TCP_ECHO_STR, res)
} }
func TestUdp(t *testing.T) { func TestUdp(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// Normal // Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_UDP_FRP_PORT) addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_UDP_FRP_PORT)
res, err := sendUdpMsg(addr, TEST_UDP_ECHO_STR) res, err := util.SendUdpMsg(addr, consts.TEST_UDP_ECHO_STR)
assert.NoError(err) assert.NoError(err)
assert.Equal(TEST_UDP_ECHO_STR, res) assert.Equal(consts.TEST_UDP_ECHO_STR, res)
// Encrytion and compression // Encrytion and compression
addr = fmt.Sprintf("127.0.0.1:%d", TEST_UDP_EC_FRP_PORT) addr = fmt.Sprintf("127.0.0.1:%d", consts.TEST_UDP_EC_FRP_PORT)
res, err = sendUdpMsg(addr, TEST_UDP_ECHO_STR) res, err = util.SendUdpMsg(addr, consts.TEST_UDP_ECHO_STR)
assert.NoError(err) assert.NoError(err)
assert.Equal(TEST_UDP_ECHO_STR, res) assert.Equal(consts.TEST_UDP_ECHO_STR, res)
} }
func TestUnixDomain(t *testing.T) { func TestUnixDomain(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// Normal // Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_UNIX_DOMAIN_FRP_PORT) addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_UNIX_DOMAIN_FRP_PORT)
res, err := sendTcpMsg(addr, TEST_UNIX_DOMAIN_STR) res, err := util.SendTcpMsg(addr, consts.TEST_UNIX_DOMAIN_STR)
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(TEST_UNIX_DOMAIN_STR, res) assert.Equal(consts.TEST_UNIX_DOMAIN_STR, res)
} }
} }
func TestStcp(t *testing.T) { func TestStcp(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// Normal // Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_STCP_FRP_PORT) addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_STCP_FRP_PORT)
res, err := sendTcpMsg(addr, TEST_STCP_ECHO_STR) res, err := util.SendTcpMsg(addr, consts.TEST_STCP_ECHO_STR)
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(TEST_STCP_ECHO_STR, res) assert.Equal(consts.TEST_STCP_ECHO_STR, res)
} }
// Encrytion and compression // Encrytion and compression
addr = fmt.Sprintf("127.0.0.1:%d", TEST_STCP_EC_FRP_PORT) addr = fmt.Sprintf("127.0.0.1:%d", consts.TEST_STCP_EC_FRP_PORT)
res, err = sendTcpMsg(addr, TEST_STCP_ECHO_STR) res, err = util.SendTcpMsg(addr, consts.TEST_STCP_ECHO_STR)
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(TEST_STCP_ECHO_STR, res) assert.Equal(consts.TEST_STCP_ECHO_STR, res)
} }
} }
func TestHttp(t *testing.T) { func TestHttp(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// web01 // web01
code, body, _, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "", nil, "") code, body, _, err := util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "", nil, "")
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body) assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
} }
// web02 // web02
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test2.frp.com", nil, "") code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test2.frp.com", nil, "")
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body) assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
} }
// error host header // error host header
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "errorhost.frp.com", nil, "") code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "errorhost.frp.com", nil, "")
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(404, code) assert.Equal(404, code)
} }
// web03 // web03
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "") code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body) assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
} }
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/foo", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "") code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/foo", consts.TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_FOO_STR, body) assert.Equal(consts.TEST_HTTP_FOO_STR, body)
} }
// web04 // web04
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/bar", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "") code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/bar", consts.TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_BAR_STR, body) assert.Equal(consts.TEST_HTTP_BAR_STR, body)
} }
// web05 // web05
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", nil, "") code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test5.frp.com", nil, "")
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(401, code) assert.Equal(401, code)
} }
headers := make(map[string]string) headers := make(map[string]string)
headers["Authorization"] = basicAuth("test", "test") headers["Authorization"] = util.BasicAuth("test", "test")
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", headers, "") code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test5.frp.com", headers, "")
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(401, code) assert.Equal(401, code)
} }
// web06 // web06
var header http.Header var header http.Header
code, body, header, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test6.frp.com", nil, "") code, body, header, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test6.frp.com", nil, "")
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body) assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
assert.Equal("true", header.Get("X-Header-Set")) assert.Equal("true", header.Get("X-Header-Set"))
} }
// subhost01 // subhost01
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test01.sub.com", nil, "") code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test01.sub.com", nil, "")
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal("test01.sub.com", body) assert.Equal("test01.sub.com", body)
} }
// subhost02 // subhost02
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test02.sub.com", nil, "") code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test02.sub.com", nil, "")
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal("test02.sub.com", body) assert.Equal("test02.sub.com", body)
@ -210,47 +195,47 @@ func TestHttp(t *testing.T) {
func TestWebSocket(t *testing.T) { func TestWebSocket(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
u := url.URL{Scheme: "ws", Host: fmt.Sprintf("%s:%d", "127.0.0.1", TEST_HTTP_FRP_PORT), Path: "/ws"} u := url.URL{Scheme: "ws", Host: fmt.Sprintf("%s:%d", "127.0.0.1", consts.TEST_HTTP_FRP_PORT), Path: "/ws"}
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
assert.NoError(err) assert.NoError(err)
defer c.Close() defer c.Close()
err = c.WriteMessage(websocket.TextMessage, []byte(TEST_HTTP_NORMAL_STR)) err = c.WriteMessage(websocket.TextMessage, []byte(consts.TEST_HTTP_NORMAL_STR))
assert.NoError(err) assert.NoError(err)
_, msg, err := c.ReadMessage() _, msg, err := c.ReadMessage()
assert.NoError(err) assert.NoError(err)
assert.Equal(TEST_HTTP_NORMAL_STR, string(msg)) assert.Equal(consts.TEST_HTTP_NORMAL_STR, string(msg))
} }
func TestAllowPorts(t *testing.T) { func TestAllowPorts(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// Port not allowed // Port not allowed
status, err := getProxyStatus(ProxyTcpPortNotAllowed) status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpPortNotAllowed)
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(client.ProxyStatusStartErr, status.Status) assert.Equal(client.ProxyStatusStartErr, status.Status)
assert.True(strings.Contains(status.Err, ports.ErrPortNotAllowed.Error())) assert.True(strings.Contains(status.Err, ports.ErrPortNotAllowed.Error()))
} }
status, err = getProxyStatus(ProxyUdpPortNotAllowed) status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyUdpPortNotAllowed)
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(client.ProxyStatusStartErr, status.Status) assert.Equal(client.ProxyStatusStartErr, status.Status)
assert.True(strings.Contains(status.Err, ports.ErrPortNotAllowed.Error())) assert.True(strings.Contains(status.Err, ports.ErrPortNotAllowed.Error()))
} }
status, err = getProxyStatus(ProxyTcpPortUnavailable) status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpPortUnavailable)
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(client.ProxyStatusStartErr, status.Status) assert.Equal(client.ProxyStatusStartErr, status.Status)
assert.True(strings.Contains(status.Err, ports.ErrPortUnAvailable.Error())) assert.True(strings.Contains(status.Err, ports.ErrPortUnAvailable.Error()))
} }
// Port normal // Port normal
status, err = getProxyStatus(ProxyTcpPortNormal) status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpPortNormal)
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(client.ProxyStatusRunning, status.Status) assert.Equal(client.ProxyStatusRunning, status.Status)
} }
status, err = getProxyStatus(ProxyUdpPortNormal) status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyUdpPortNormal)
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(client.ProxyStatusRunning, status.Status) assert.Equal(client.ProxyStatusRunning, status.Status)
} }
@ -259,45 +244,45 @@ func TestAllowPorts(t *testing.T) {
func TestRandomPort(t *testing.T) { func TestRandomPort(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// tcp // tcp
status, err := getProxyStatus(ProxyTcpRandomPort) status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpRandomPort)
if assert.NoError(err) { if assert.NoError(err) {
addr := status.RemoteAddr addr := status.RemoteAddr
res, err := sendTcpMsg(addr, TEST_TCP_ECHO_STR) res, err := util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
assert.NoError(err) assert.NoError(err)
assert.Equal(TEST_TCP_ECHO_STR, res) assert.Equal(consts.TEST_TCP_ECHO_STR, res)
} }
// udp // udp
status, err = getProxyStatus(ProxyUdpRandomPort) status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyUdpRandomPort)
if assert.NoError(err) { if assert.NoError(err) {
addr := status.RemoteAddr addr := status.RemoteAddr
res, err := sendUdpMsg(addr, TEST_UDP_ECHO_STR) res, err := util.SendUdpMsg(addr, consts.TEST_UDP_ECHO_STR)
assert.NoError(err) assert.NoError(err)
assert.Equal(TEST_UDP_ECHO_STR, res) assert.Equal(consts.TEST_UDP_ECHO_STR, res)
} }
} }
func TestPluginHttpProxy(t *testing.T) { func TestPluginHttpProxy(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
status, err := getProxyStatus(ProxyHttpProxy) status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyHttpProxy)
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(client.ProxyStatusRunning, status.Status) assert.Equal(client.ProxyStatusRunning, status.Status)
// http proxy // http proxy
addr := status.RemoteAddr addr := status.RemoteAddr
code, body, _, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), code, body, _, err := util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT),
"", nil, "http://"+addr) "", nil, "http://"+addr)
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body) assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
} }
// connect method // connect method
conn, err := gnet.DialTcpByProxy("http://"+addr, fmt.Sprintf("127.0.0.1:%d", TEST_TCP_FRP_PORT)) conn, err := gnet.DialTcpByProxy("http://"+addr, fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP_FRP_PORT))
if assert.NoError(err) { if assert.NoError(err) {
res, err := sendTcpMsgByConn(conn, TEST_TCP_ECHO_STR) res, err := util.SendTcpMsgByConn(conn, consts.TEST_TCP_ECHO_STR)
assert.NoError(err) assert.NoError(err)
assert.Equal(TEST_TCP_ECHO_STR, res) assert.Equal(consts.TEST_TCP_ECHO_STR, res)
} }
} }
} }
@ -306,8 +291,8 @@ func TestRangePortsMapping(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
name := fmt.Sprintf("%s_%d", ProxyRangeTcpPrefix, i) name := fmt.Sprintf("%s_%d", consts.ProxyRangeTcpPrefix, i)
status, err := getProxyStatus(name) status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, name)
if assert.NoError(err) { if assert.NoError(err) {
assert.Equal(client.ProxyStatusRunning, status.Status) assert.Equal(client.ProxyStatusRunning, status.Status)
} }
@ -321,15 +306,15 @@ func TestGroup(t *testing.T) {
p1 int p1 int
p2 int p2 int
) )
addr := fmt.Sprintf("127.0.0.1:%d", TEST_TCP2_FRP_PORT) addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP2_FRP_PORT)
for i := 0; i < 6; i++ { for i := 0; i < 6; i++ {
res, err := sendTcpMsg(addr, TEST_TCP_ECHO_STR) res, err := util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
assert.NoError(err) assert.NoError(err)
switch res { switch res {
case TEST_TCP_ECHO_STR: case consts.TEST_TCP_ECHO_STR:
p1++ p1++
case TEST_TCP_ECHO_STR + TEST_TCP_ECHO_STR: case consts.TEST_TCP_ECHO_STR + consts.TEST_TCP_ECHO_STR:
p2++ p2++
} }
} }

View File

@ -1,4 +1,4 @@
package tests package mock
import ( import (
"fmt" "fmt"
@ -10,8 +10,8 @@ import (
frpNet "github.com/fatedier/frp/utils/net" frpNet "github.com/fatedier/frp/utils/net"
) )
func StartTcpEchoServer() { func StartTcpEchoServer(port int) {
l, err := frpNet.ListenTcp("127.0.0.1", TEST_TCP_PORT) l, err := frpNet.ListenTcp("127.0.0.1", port)
if err != nil { if err != nil {
fmt.Printf("echo server listen error: %v\n", err) fmt.Printf("echo server listen error: %v\n", err)
return return
@ -28,8 +28,8 @@ func StartTcpEchoServer() {
} }
} }
func StartTcpEchoServer2() { func StartTcpEchoServer2(port int) {
l, err := frpNet.ListenTcp("127.0.0.1", TEST_TCP2_PORT) l, err := frpNet.ListenTcp("127.0.0.1", port)
if err != nil { if err != nil {
fmt.Printf("echo server2 listen error: %v\n", err) fmt.Printf("echo server2 listen error: %v\n", err)
return return
@ -46,8 +46,8 @@ func StartTcpEchoServer2() {
} }
} }
func StartUdpEchoServer() { func StartUdpEchoServer(port int) {
l, err := frpNet.ListenUDP("127.0.0.1", TEST_UDP_PORT) l, err := frpNet.ListenUDP("127.0.0.1", port)
if err != nil { if err != nil {
fmt.Printf("udp echo server listen error: %v\n", err) fmt.Printf("udp echo server listen error: %v\n", err)
return return
@ -64,8 +64,7 @@ func StartUdpEchoServer() {
} }
} }
func StartUnixDomainServer() { func StartUnixDomainServer(unixPath string) {
unixPath := TEST_UNIX_DOMAIN_ADDR
os.Remove(unixPath) os.Remove(unixPath)
syscall.Umask(0) syscall.Umask(0)
l, err := net.Listen("unix", unixPath) l, err := net.Listen("unix", unixPath)

View File

@ -1,4 +1,4 @@
package tests package mock
import ( import (
"fmt" "fmt"
@ -7,15 +7,17 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/fatedier/frp/tests/consts"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
var upgrader = websocket.Upgrader{} var upgrader = websocket.Upgrader{}
func StartHttpServer() { func StartHttpServer(port int) {
http.HandleFunc("/", handleHttp) http.HandleFunc("/", handleHttp)
http.HandleFunc("/ws", handleWebSocket) http.HandleFunc("/ws", handleWebSocket)
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", TEST_HTTP_PORT), nil) http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil)
} }
func handleWebSocket(w http.ResponseWriter, r *http.Request) { func handleWebSocket(w http.ResponseWriter, r *http.Request) {
@ -58,15 +60,15 @@ func handleHttp(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.Host, "127.0.0.1") || strings.Contains(r.Host, "test2.frp.com") || if strings.Contains(r.Host, "127.0.0.1") || strings.Contains(r.Host, "test2.frp.com") ||
strings.Contains(r.Host, "test5.frp.com") || strings.Contains(r.Host, "test6.frp.com") { strings.Contains(r.Host, "test5.frp.com") || strings.Contains(r.Host, "test6.frp.com") {
w.WriteHeader(200) w.WriteHeader(200)
w.Write([]byte(TEST_HTTP_NORMAL_STR)) w.Write([]byte(consts.TEST_HTTP_NORMAL_STR))
} else if strings.Contains(r.Host, "test3.frp.com") { } else if strings.Contains(r.Host, "test3.frp.com") {
w.WriteHeader(200) w.WriteHeader(200)
if strings.Contains(r.URL.Path, "foo") { if strings.Contains(r.URL.Path, "foo") {
w.Write([]byte(TEST_HTTP_FOO_STR)) w.Write([]byte(consts.TEST_HTTP_FOO_STR))
} else if strings.Contains(r.URL.Path, "bar") { } else if strings.Contains(r.URL.Path, "bar") {
w.Write([]byte(TEST_HTTP_BAR_STR)) w.Write([]byte(consts.TEST_HTTP_BAR_STR))
} else { } else {
w.Write([]byte(TEST_HTTP_NORMAL_STR)) w.Write([]byte(consts.TEST_HTTP_NORMAL_STR))
} }
} else { } else {
w.WriteHeader(404) w.WriteHeader(404)

View File

@ -1,9 +0,0 @@
#!/bin/bash
./../bin/frps -c ./conf/auto_test_frps.ini &
sleep 1
./../bin/frpc -c ./conf/auto_test_frpc.ini &
./../bin/frpc -c ./conf/auto_test_frpc_visitor.ini &
# wait until proxies are connected
sleep 2

29
tests/util/process.go Normal file
View File

@ -0,0 +1,29 @@
package util
import (
"context"
"os/exec"
)
type Process struct {
cmd *exec.Cmd
cancel context.CancelFunc
}
func NewProcess(path string, params []string) *Process {
ctx, cancel := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, path, params...)
return &Process{
cmd: cmd,
cancel: cancel,
}
}
func (p *Process) Start() error {
return p.cmd.Start()
}
func (p *Process) Stop() error {
p.cancel()
return p.cmd.Wait()
}

View File

@ -1,4 +1,4 @@
package tests package util
import ( import (
"encoding/base64" "encoding/base64"
@ -16,13 +16,13 @@ import (
frpNet "github.com/fatedier/frp/utils/net" frpNet "github.com/fatedier/frp/utils/net"
) )
func getProxyStatus(name string) (status *client.ProxyStatusResp, err error) { func GetProxyStatus(statusAddr string, user string, passwd string, name string) (status *client.ProxyStatusResp, err error) {
req, err := http.NewRequest("GET", "http://"+ADMIN_ADDR+"/api/status", nil) req, err := http.NewRequest("GET", "http://"+statusAddr+"/api/status", nil)
if err != nil { if err != nil {
return status, err return status, err
} }
authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(ADMIN_USER+":"+ADMIN_PWD)) authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+passwd))
req.Header.Add("Authorization", authStr) req.Header.Add("Authorization", authStr)
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
@ -75,17 +75,17 @@ func getProxyStatus(name string) (status *client.ProxyStatusResp, err error) {
return status, errors.New("no proxy status found") return status, errors.New("no proxy status found")
} }
func sendTcpMsg(addr string, msg string) (res string, err error) { func SendTcpMsg(addr string, msg string) (res string, err error) {
c, err := frpNet.ConnectTcpServer(addr) c, err := frpNet.ConnectTcpServer(addr)
if err != nil { if err != nil {
err = fmt.Errorf("connect to tcp server error: %v", err) err = fmt.Errorf("connect to tcp server error: %v", err)
return return
} }
defer c.Close() defer c.Close()
return sendTcpMsgByConn(c, msg) return SendTcpMsgByConn(c, msg)
} }
func sendTcpMsgByConn(c net.Conn, msg string) (res string, err error) { func SendTcpMsgByConn(c net.Conn, msg string) (res string, err error) {
timer := time.Now().Add(5 * time.Second) timer := time.Now().Add(5 * time.Second)
c.SetDeadline(timer) c.SetDeadline(timer)
c.Write([]byte(msg)) c.Write([]byte(msg))
@ -99,7 +99,7 @@ func sendTcpMsgByConn(c net.Conn, msg string) (res string, err error) {
return string(buf[:n]), nil return string(buf[:n]), nil
} }
func sendUdpMsg(addr string, msg string) (res string, err error) { func SendUdpMsg(addr string, msg string) (res string, err error) {
udpAddr, errRet := net.ResolveUDPAddr("udp", addr) udpAddr, errRet := net.ResolveUDPAddr("udp", addr)
if errRet != nil { if errRet != nil {
err = fmt.Errorf("resolve udp addr error: %v", err) err = fmt.Errorf("resolve udp addr error: %v", err)
@ -126,7 +126,7 @@ func sendUdpMsg(addr string, msg string) (res string, err error) {
return string(buf[:n]), nil return string(buf[:n]), nil
} }
func sendHttpMsg(method, urlStr string, host string, headers map[string]string, proxy string) (code int, body string, header http.Header, err error) { func SendHttpMsg(method, urlStr string, host string, headers map[string]string, proxy string) (code int, body string, header http.Header, err error) {
req, errRet := http.NewRequest(method, urlStr, nil) req, errRet := http.NewRequest(method, urlStr, nil)
if errRet != nil { if errRet != nil {
err = errRet err = errRet
@ -177,7 +177,7 @@ func sendHttpMsg(method, urlStr string, host string, headers map[string]string,
return return
} }
func basicAuth(username, passwd string) string { func BasicAuth(username, passwd string) string {
auth := username + ":" + passwd auth := username + ":" + passwd
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
} }