This commit is contained in:
fatedier
2018-07-12 00:31:21 +08:00
parent 0d02f291e3
commit d74b45be5d
9 changed files with 123 additions and 9 deletions

193
tests/ci/auto_test_frpc.ini Normal file
View File

@@ -0,0 +1,193 @@
[common]
server_addr = 127.0.0.1
server_port = 10700
log_file = console
# debug, info, warn, error
log_level = debug
token = 123456
admin_port = 10600
admin_user = abc
admin_pwd = abc
[tcp_normal]
type = tcp
local_ip = 127.0.0.1
local_port = 10701
remote_port = 10801
[tcp_ec]
type = tcp
local_ip = 127.0.0.1
local_port = 10701
remote_port = 10901
use_encryption = true
use_compression = true
[tcp_group1]
type = tcp
local_ip = 127.0.0.1
local_port = 10701
remote_port = 10802
group = test1
group_key = 123
[tcp_group2]
type = tcp
local_ip = 127.0.0.1
local_port = 10702
remote_port = 10802
group = test1
group_key = 123
[udp_normal]
type = udp
local_ip = 127.0.0.1
local_port = 10702
remote_port = 10802
[udp_ec]
type = udp
local_ip = 127.0.0.1
local_port = 10702
remote_port = 10902
use_encryption = true
use_compression = true
[unix_domain]
type = tcp
remote_port = 10803
plugin = unix_domain_socket
plugin_unix_path = /tmp/frp_echo_server.sock
[stcp]
type = stcp
sk = abcdefg
local_ip = 127.0.0.1
local_port = 10701
[stcp_ec]
type = stcp
sk = abc
local_ip = 127.0.0.1
local_port = 10701
use_encryption = true
use_compression = true
[web01]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = 127.0.0.1
[web02]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = test2.frp.com
host_header_rewrite = test2.frp.com
use_encryption = true
use_compression = true
[web03]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = test3.frp.com
use_encryption = true
use_compression = true
host_header_rewrite = test3.frp.com
locations = /,/foo
[web04]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = test3.frp.com
use_encryption = true
use_compression = true
host_header_rewrite = test3.frp.com
locations = /bar
[web05]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = test5.frp.com
host_header_rewrite = test5.frp.com
use_encryption = true
use_compression = true
http_user = test
http_user = test
[web06]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = test6.frp.com
host_header_rewrite = test6.frp.com
header_X-From-Where = frp
[subhost01]
type = http
local_ip = 127.0.0.1
local_port = 10704
subdomain = test01
[subhost02]
type = http
local_ip = 127.0.0.1
local_port = 10704
subdomain = test02
[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
[http_proxy]
type = tcp
plugin = http_proxy
remote_port = 0
[range:range_tcp]
type = tcp
local_ip = 127.0.0.1
local_port = 30000-30001,30003
remote_port = 30000-30001,30003

View File

@@ -0,0 +1,25 @@
[common]
server_addr = 0.0.0.0
server_port = 10700
log_file = console
# debug, info, warn, error
log_level = debug
token = 123456
[stcp_visitor]
type = stcp
role = visitor
server_name = stcp
sk = abcdefg
bind_addr = 127.0.0.1
bind_port = 10805
[stcp_ec_visitor]
type = stcp
role = visitor
server_name = stcp_ec
sk = abc
bind_addr = 127.0.0.1
bind_port = 10905
use_encryption = true
use_compression = true

View File

@@ -0,0 +1,9 @@
[common]
bind_addr = 0.0.0.0
bind_port = 10700
vhost_http_port = 10804
log_file = console
log_level = debug
token = 123456
allow_ports = 10000-20000,20002,30000-50000
subdomain_host = sub.com

1
tests/ci/cmd_test.go Normal file
View File

@@ -0,0 +1 @@
package ci

322
tests/ci/normal_test.go Normal file
View File

@@ -0,0 +1,322 @@
package ci
import (
"fmt"
"net/http"
"net/url"
"strings"
"testing"
"time"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/fatedier/frp/client"
"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"
)
func init() {
go mock.StartTcpEchoServer(consts.TEST_TCP_PORT)
go mock.StartTcpEchoServer2(consts.TEST_TCP2_PORT)
go mock.StartUdpEchoServer(consts.TEST_UDP_PORT)
go mock.StartUnixDomainServer(consts.TEST_UNIX_DOMAIN_ADDR)
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)
}
func runFrps() error {
p := util.NewProcess(consts.FRPS_BIN_PATH, []string{"-c", "./auto_test_frps.ini"})
return p.Start()
}
func runFrpc() error {
p := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./auto_test_frpc.ini"})
return p.Start()
}
func runFrpcVisitor() error {
p := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./auto_test_frpc_visitor.ini"})
return p.Start()
}
func TestTcp(t *testing.T) {
assert := assert.New(t)
// Normal
addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP_FRP_PORT)
res, err := util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
assert.NoError(err)
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
// Encrytion and compression
addr = fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP_EC_FRP_PORT)
res, err = util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
assert.NoError(err)
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
}
func TestUdp(t *testing.T) {
assert := assert.New(t)
// Normal
addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_UDP_FRP_PORT)
res, err := util.SendUdpMsg(addr, consts.TEST_UDP_ECHO_STR)
assert.NoError(err)
assert.Equal(consts.TEST_UDP_ECHO_STR, res)
// Encrytion and compression
addr = fmt.Sprintf("127.0.0.1:%d", consts.TEST_UDP_EC_FRP_PORT)
res, err = util.SendUdpMsg(addr, consts.TEST_UDP_ECHO_STR)
assert.NoError(err)
assert.Equal(consts.TEST_UDP_ECHO_STR, res)
}
func TestUnixDomain(t *testing.T) {
assert := assert.New(t)
// Normal
addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_UNIX_DOMAIN_FRP_PORT)
res, err := util.SendTcpMsg(addr, consts.TEST_UNIX_DOMAIN_STR)
if assert.NoError(err) {
assert.Equal(consts.TEST_UNIX_DOMAIN_STR, res)
}
}
func TestStcp(t *testing.T) {
assert := assert.New(t)
// Normal
addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_STCP_FRP_PORT)
res, err := util.SendTcpMsg(addr, consts.TEST_STCP_ECHO_STR)
if assert.NoError(err) {
assert.Equal(consts.TEST_STCP_ECHO_STR, res)
}
// Encrytion and compression
addr = fmt.Sprintf("127.0.0.1:%d", consts.TEST_STCP_EC_FRP_PORT)
res, err = util.SendTcpMsg(addr, consts.TEST_STCP_ECHO_STR)
if assert.NoError(err) {
assert.Equal(consts.TEST_STCP_ECHO_STR, res)
}
}
func TestHttp(t *testing.T) {
assert := assert.New(t)
// web01
code, body, _, err := util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "", nil, "")
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
}
// web02
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) {
assert.Equal(200, code)
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
}
// error host header
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) {
assert.Equal(404, code)
}
// web03
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) {
assert.Equal(200, code)
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
}
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) {
assert.Equal(200, code)
assert.Equal(consts.TEST_HTTP_FOO_STR, body)
}
// web04
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) {
assert.Equal(200, code)
assert.Equal(consts.TEST_HTTP_BAR_STR, body)
}
// web05
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) {
assert.Equal(401, code)
}
headers := make(map[string]string)
headers["Authorization"] = util.BasicAuth("test", "test")
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) {
assert.Equal(401, code)
}
// web06
var header http.Header
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) {
assert.Equal(200, code)
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
assert.Equal("true", header.Get("X-Header-Set"))
}
// subhost01
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) {
assert.Equal(200, code)
assert.Equal("test01.sub.com", body)
}
// subhost02
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) {
assert.Equal(200, code)
assert.Equal("test02.sub.com", body)
}
}
func TestWebSocket(t *testing.T) {
assert := assert.New(t)
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)
assert.NoError(err)
defer c.Close()
err = c.WriteMessage(websocket.TextMessage, []byte(consts.TEST_HTTP_NORMAL_STR))
assert.NoError(err)
_, msg, err := c.ReadMessage()
assert.NoError(err)
assert.Equal(consts.TEST_HTTP_NORMAL_STR, string(msg))
}
func TestAllowPorts(t *testing.T) {
assert := assert.New(t)
// Port not allowed
status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpPortNotAllowed)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusStartErr, status.Status)
assert.True(strings.Contains(status.Err, ports.ErrPortNotAllowed.Error()))
}
status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyUdpPortNotAllowed)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusStartErr, status.Status)
assert.True(strings.Contains(status.Err, ports.ErrPortNotAllowed.Error()))
}
status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpPortUnavailable)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusStartErr, status.Status)
assert.True(strings.Contains(status.Err, ports.ErrPortUnAvailable.Error()))
}
// Port normal
status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpPortNormal)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusRunning, status.Status)
}
status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyUdpPortNormal)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusRunning, status.Status)
}
}
func TestRandomPort(t *testing.T) {
assert := assert.New(t)
// tcp
status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpRandomPort)
if assert.NoError(err) {
addr := status.RemoteAddr
res, err := util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
assert.NoError(err)
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
}
// udp
status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyUdpRandomPort)
if assert.NoError(err) {
addr := status.RemoteAddr
res, err := util.SendUdpMsg(addr, consts.TEST_UDP_ECHO_STR)
assert.NoError(err)
assert.Equal(consts.TEST_UDP_ECHO_STR, res)
}
}
func TestPluginHttpProxy(t *testing.T) {
assert := assert.New(t)
status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyHttpProxy)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusRunning, status.Status)
// http proxy
addr := status.RemoteAddr
code, body, _, err := util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT),
"", nil, "http://"+addr)
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
}
// connect method
conn, err := gnet.DialTcpByProxy("http://"+addr, fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP_FRP_PORT))
if assert.NoError(err) {
res, err := util.SendTcpMsgByConn(conn, consts.TEST_TCP_ECHO_STR)
assert.NoError(err)
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
}
}
}
func TestRangePortsMapping(t *testing.T) {
assert := assert.New(t)
for i := 0; i < 3; i++ {
name := fmt.Sprintf("%s_%d", consts.ProxyRangeTcpPrefix, i)
status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, name)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusRunning, status.Status)
}
}
}
func TestGroup(t *testing.T) {
assert := assert.New(t)
var (
p1 int
p2 int
)
addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP2_FRP_PORT)
for i := 0; i < 6; i++ {
res, err := util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
assert.NoError(err)
switch res {
case consts.TEST_TCP_ECHO_STR:
p1++
case consts.TEST_TCP_ECHO_STR + consts.TEST_TCP_ECHO_STR:
p2++
}
}
assert.True(p1 > 0 && p2 > 0, "group proxies load balancing")
}

View File

@@ -0,0 +1 @@
package ci

108
tests/ci/reload_test.go Normal file
View File

@@ -0,0 +1,108 @@
package ci
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/fatedier/frp/tests/config"
"github.com/fatedier/frp/tests/consts"
"github.com/fatedier/frp/tests/util"
)
const FRPS_CONF = `
[common]
server_addr = 127.0.0.1
server_port = 10700
log_file = console
# debug, info, warn, error
log_level = debug
token = 123456
admin_port = 10600
admin_user = abc
admin_pwd = abc
`
const FRPC_CONF_1 = `
[common]
server_addr = 127.0.0.1
server_port = 20000
log_file = console
# debug, info, warn, error
log_level = debug
token = 123456
admin_port = 21000
admin_user = abc
admin_pwd = abc
[tcp]
type = tcp
local_port = 10701
remote_port = 20801
# change remote port
[tcp2]
type = tcp
local_port = 10701
remote_port = 20802
# delete
[tcp3]
type = tcp
local_port = 10701
remote_port = 20803
`
const FRPC_CONF_2 = `
[common]
server_addr = 127.0.0.1
server_port = 20000
log_file = console
# debug, info, warn, error
log_level = debug
token = 123456
admin_port = 21000
admin_user = abc
admin_pwd = abc
[tcp]
type = tcp
local_port = 10701
remote_port = 20801
[tcp2]
type = tcp
local_port = 10701
remote_port = 20902
`
func TestReload(t *testing.T) {
assert := assert.New(t)
frpsCfgPath, err := config.GenerateConfigFile("./auto_test_frps.ini", FRPS_CONF)
if assert.NoError(err) {
defer os.Remove(frpsCfgPath)
}
frpcCfgPath, err := config.GenerateConfigFile("./auto_test_frpc.ini", FRPC_CONF_1)
if assert.NoError(err) {
defer os.Remove(frpcCfgPath)
}
frpsProcess := util.NewProcess(consts.FRPS_BIN_PATH, []string{"-c", frpsCfgPath})
err = frpsProcess.Start()
if assert.NoError(err) {
defer frpsProcess.Stop()
}
time.Sleep(200 * time.Millisecond)
frpcProcess := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", frpcCfgPath})
err = frpcProcess.Start()
if assert.NoError(err) {
defer frpcProcess.Stop()
}
// TODO
}