diff --git a/Makefile b/Makefile index 10decfb9..18b6aaf5 100644 --- a/Makefile +++ b/Makefile @@ -39,11 +39,13 @@ gotest: go test -v ./server/... go test -v ./utils/... -alltest: gotest +ci: cd ./tests && ./run_test.sh && cd - go test -v ./tests/... cd ./tests && ./clean_test.sh && cd - +alltest: gotest ci + clean: rm -f ./bin/frpc rm -f ./bin/frps diff --git a/tests/conf/auto_test_frpc.ini b/tests/conf/auto_test_frpc.ini index 2d243e80..231f4d94 100644 --- a/tests/conf/auto_test_frpc.ini +++ b/tests/conf/auto_test_frpc.ini @@ -5,6 +5,9 @@ log_file = ./frpc.log # debug, info, warn, error log_level = debug privilege_token = 123456 +admin_port = 10600 +admin_user = abc +admin_pwd = abc [tcp_normal] type = tcp @@ -99,3 +102,45 @@ use_encryption = true use_compression = true http_user = test http_user = test + +[tcp_port_not_allowed] +type = tcp +local_ip = 127.0.0.1 +local_port = 10701 +remote_port = 20001 + +[tcp_port_unavailable] +type =tcp +local_ip = 127.0.0.1 +local_port = 10701 +remote_port = 10700 + +[tcp_port_normal] +type = tcp +local_ip = 127.0.0.1 +local_port = 10701 +remote_port = 20002 + +[tcp_random_port] +type = tcp +local_ip = 127.0.0.1 +local_port = 10701 +remote_port = 0 + +[udp_port_not_allowed] +type = udp +local_ip = 127.0.0.1 +local_port = 10702 +remote_port = 20001 + +[udp_port_normal] +type = udp +local_ip = 127.0.0.1 +local_port = 10702 +remote_port = 20002 + +[udp_random_port] +type = udp +local_ip = 127.0.0.1 +local_port = 10702 +remote_port = 0 diff --git a/tests/conf/auto_test_frps.ini b/tests/conf/auto_test_frps.ini index c03dc146..1bc3a82e 100644 --- a/tests/conf/auto_test_frps.ini +++ b/tests/conf/auto_test_frps.ini @@ -5,3 +5,4 @@ vhost_http_port = 10804 log_file = ./frps.log log_level = debug privilege_token = 123456 +privilege_allow_ports = 10000-20000,20002,30000-40000 diff --git a/tests/func_test.go b/tests/func_test.go index 1f154089..39494b3b 100644 --- a/tests/func_test.go +++ b/tests/func_test.go @@ -2,13 +2,22 @@ package tests import ( "fmt" + "strings" "testing" "time" "github.com/stretchr/testify/assert" + + "github.com/fatedier/frp/client" + "github.com/fatedier/frp/server" ) 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_TCP_FRP_PORT int = 10801 @@ -33,6 +42,14 @@ var ( 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" ) func init() { @@ -155,3 +172,57 @@ func TestHttp(t *testing.T) { assert.Equal(401, code) } } + +func TestPrivilegeAllowPorts(t *testing.T) { + assert := assert.New(t) + // Port not allowed + status, err := getProxyStatus(ProxyTcpPortNotAllowed) + if assert.NoError(err) { + assert.Equal(client.ProxyStatusStartErr, status.Status) + assert.True(strings.Contains(status.Err, server.ErrPortNotAllowed.Error())) + } + + status, err = getProxyStatus(ProxyUdpPortNotAllowed) + if assert.NoError(err) { + assert.Equal(client.ProxyStatusStartErr, status.Status) + assert.True(strings.Contains(status.Err, server.ErrPortNotAllowed.Error())) + } + + status, err = getProxyStatus(ProxyTcpPortUnavailable) + if assert.NoError(err) { + assert.Equal(client.ProxyStatusStartErr, status.Status) + assert.True(strings.Contains(status.Err, server.ErrPortUnAvailable.Error())) + } + + // Port normal + status, err = getProxyStatus(ProxyTcpPortNormal) + if assert.NoError(err) { + assert.Equal(client.ProxyStatusRunning, status.Status) + } + + status, err = getProxyStatus(ProxyUdpPortNormal) + if assert.NoError(err) { + assert.Equal(client.ProxyStatusRunning, status.Status) + } +} + +func TestRandomPort(t *testing.T) { + assert := assert.New(t) + // tcp + status, err := getProxyStatus(ProxyTcpRandomPort) + if assert.NoError(err) { + addr := status.RemoteAddr + res, err := sendTcpMsg(addr, TEST_TCP_ECHO_STR) + assert.NoError(err) + assert.Equal(TEST_TCP_ECHO_STR, res) + } + + // udp + status, err = getProxyStatus(ProxyUdpRandomPort) + if assert.NoError(err) { + addr := status.RemoteAddr + res, err := sendUdpMsg(addr, TEST_UDP_ECHO_STR) + assert.NoError(err) + assert.Equal(TEST_UDP_ECHO_STR, res) + } +} diff --git a/tests/util.go b/tests/util.go index 9eed5802..5cf0f6a5 100644 --- a/tests/util.go +++ b/tests/util.go @@ -2,15 +2,78 @@ package tests import ( "encoding/base64" + "encoding/json" + "errors" "fmt" "io/ioutil" "net" "net/http" + "strings" "time" + "github.com/fatedier/frp/client" frpNet "github.com/fatedier/frp/utils/net" ) +func getProxyStatus(name string) (status *client.ProxyStatusResp, err error) { + req, err := http.NewRequest("GET", "http://"+ADMIN_ADDR+"/api/status", nil) + if err != nil { + return status, err + } + + authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(ADMIN_USER+":"+ADMIN_PWD)) + req.Header.Add("Authorization", authStr) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return status, err + } else { + if resp.StatusCode != 200 { + return status, fmt.Errorf("admin api status code [%d]", resp.StatusCode) + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return status, err + } + allStatus := &client.StatusResp{} + err = json.Unmarshal(body, &allStatus) + if err != nil { + return status, fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(string(body))) + } + for _, s := range allStatus.Tcp { + if s.Name == name { + return &s, nil + } + } + for _, s := range allStatus.Udp { + if s.Name == name { + return &s, nil + } + } + for _, s := range allStatus.Http { + if s.Name == name { + return &s, nil + } + } + for _, s := range allStatus.Https { + if s.Name == name { + return &s, nil + } + } + for _, s := range allStatus.Stcp { + if s.Name == name { + return &s, nil + } + } + for _, s := range allStatus.Xtcp { + if s.Name == name { + return &s, nil + } + } + } + return status, errors.New("no proxy status found") +} + func sendTcpMsg(addr string, msg string) (res string, err error) { c, err := frpNet.ConnectTcpServer(addr) if err != nil {