tests: more ci case

This commit is contained in:
fatedier 2018-01-18 14:53:44 +08:00
parent 37210d9983
commit e155ff056e
5 changed files with 183 additions and 1 deletions

View File

@ -39,11 +39,13 @@ gotest:
go test -v ./server/... go test -v ./server/...
go test -v ./utils/... go test -v ./utils/...
alltest: gotest ci:
cd ./tests && ./run_test.sh && cd - cd ./tests && ./run_test.sh && cd -
go test -v ./tests/... go test -v ./tests/...
cd ./tests && ./clean_test.sh && cd - cd ./tests && ./clean_test.sh && cd -
alltest: gotest ci
clean: clean:
rm -f ./bin/frpc rm -f ./bin/frpc
rm -f ./bin/frps rm -f ./bin/frps

View File

@ -5,6 +5,9 @@ log_file = ./frpc.log
# debug, info, warn, error # debug, info, warn, error
log_level = debug log_level = debug
privilege_token = 123456 privilege_token = 123456
admin_port = 10600
admin_user = abc
admin_pwd = abc
[tcp_normal] [tcp_normal]
type = tcp type = tcp
@ -99,3 +102,45 @@ use_encryption = true
use_compression = true use_compression = true
http_user = test http_user = test
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

View File

@ -5,3 +5,4 @@ vhost_http_port = 10804
log_file = ./frps.log log_file = ./frps.log
log_level = debug log_level = debug
privilege_token = 123456 privilege_token = 123456
privilege_allow_ports = 10000-20000,20002,30000-40000

View File

@ -2,13 +2,22 @@ package tests
import ( import (
"fmt" "fmt"
"strings"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/fatedier/frp/client"
"github.com/fatedier/frp/server"
) )
var ( 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_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_PORT int = 10701
TEST_TCP_FRP_PORT int = 10801 TEST_TCP_FRP_PORT int = 10801
@ -33,6 +42,14 @@ var (
TEST_STCP_FRP_PORT int = 10805 TEST_STCP_FRP_PORT int = 10805
TEST_STCP_EC_FRP_PORT int = 10905 TEST_STCP_EC_FRP_PORT int = 10905
TEST_STCP_ECHO_STR string = "stcp type:" + TEST_STR 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() { func init() {
@ -155,3 +172,57 @@ func TestHttp(t *testing.T) {
assert.Equal(401, code) 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)
}
}

View File

@ -2,15 +2,78 @@ package tests
import ( import (
"encoding/base64" "encoding/base64"
"encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"strings"
"time" "time"
"github.com/fatedier/frp/client"
frpNet "github.com/fatedier/frp/utils/net" 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) { 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 {