frp/tests/ci/normal_test.go

343 lines
10 KiB
Go
Raw Normal View History

2018-07-11 18:31:21 +02:00
package ci
2016-05-17 13:13:37 +02:00
import (
"fmt"
2018-05-20 17:55:22 +02:00
"net/http"
2018-01-23 07:49:04 +01:00
"net/url"
2018-07-12 09:23:34 +02:00
"os"
2018-01-18 07:53:44 +01:00
"strings"
2016-05-17 13:13:37 +02:00
"testing"
"time"
2018-01-23 07:49:04 +01:00
"github.com/gorilla/websocket"
2017-12-16 16:59:46 +01:00
"github.com/stretchr/testify/assert"
2018-01-18 07:53:44 +01:00
2018-12-09 15:06:22 +01:00
"github.com/fatedier/frp/client/proxy"
2018-05-23 08:39:12 +02:00
"github.com/fatedier/frp/server/ports"
2018-07-11 17:27:47 +02:00
"github.com/fatedier/frp/tests/consts"
"github.com/fatedier/frp/tests/mock"
"github.com/fatedier/frp/tests/util"
2018-05-08 18:57:55 +02:00
gnet "github.com/fatedier/golib/net"
2016-05-17 13:13:37 +02:00
)
2018-07-12 09:23:34 +02:00
func TestMain(m *testing.M) {
2018-12-09 14:56:46 +01:00
var err error
tcpEcho1 := mock.NewEchoServer(consts.TEST_TCP_PORT, 1, "")
tcpEcho2 := mock.NewEchoServer(consts.TEST_TCP2_PORT, 2, "")
if err = tcpEcho1.Start(); err != nil {
panic(err)
}
if err = tcpEcho2.Start(); err != nil {
panic(err)
}
2018-07-11 17:27:47 +02:00
go mock.StartUdpEchoServer(consts.TEST_UDP_PORT)
go mock.StartUnixDomainServer(consts.TEST_UNIX_DOMAIN_ADDR)
go mock.StartHttpServer(consts.TEST_HTTP_PORT)
2018-07-12 09:23:34 +02:00
p1 := util.NewProcess(consts.FRPS_BIN_PATH, []string{"-c", "./auto_test_frps.ini"})
if err = p1.Start(); err != nil {
2018-07-11 17:27:47 +02:00
panic(err)
}
2018-07-12 09:23:34 +02:00
time.Sleep(200 * time.Millisecond)
p2 := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./auto_test_frpc.ini"})
if err = p2.Start(); err != nil {
2018-07-11 17:27:47 +02:00
panic(err)
}
2018-07-12 09:23:34 +02:00
p3 := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./auto_test_frpc_visitor.ini"})
if err = p3.Start(); err != nil {
2018-07-11 17:27:47 +02:00
panic(err)
}
2017-03-09 17:52:32 +01:00
time.Sleep(500 * time.Millisecond)
2018-07-11 17:27:47 +02:00
2018-07-12 09:23:34 +02:00
exitCode := m.Run()
p1.Stop()
p2.Stop()
p3.Stop()
os.Exit(exitCode)
2018-07-11 17:27:47 +02:00
}
2017-12-18 12:35:09 +01:00
func TestTcp(t *testing.T) {
2017-12-16 16:59:46 +01:00
assert := assert.New(t)
// Normal
2018-07-11 17:27:47 +02:00
addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP_FRP_PORT)
res, err := util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
2017-12-16 16:59:46 +01:00
assert.NoError(err)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
2016-05-17 13:13:37 +02:00
2017-12-16 16:59:46 +01:00
// Encrytion and compression
2018-07-11 17:27:47 +02:00
addr = fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP_EC_FRP_PORT)
res, err = util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
2017-12-16 16:59:46 +01:00
assert.NoError(err)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
2017-12-16 16:59:46 +01:00
}
2016-05-17 13:13:37 +02:00
2017-12-18 12:35:09 +01:00
func TestUdp(t *testing.T) {
2017-12-16 16:59:46 +01:00
assert := assert.New(t)
// Normal
2018-07-11 17:27:47 +02:00
addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_UDP_FRP_PORT)
res, err := util.SendUdpMsg(addr, consts.TEST_UDP_ECHO_STR)
2017-12-16 16:59:46 +01:00
assert.NoError(err)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_UDP_ECHO_STR, res)
2016-05-17 13:13:37 +02:00
2017-12-18 12:35:09 +01:00
// Encrytion and compression
2018-07-11 17:27:47 +02:00
addr = fmt.Sprintf("127.0.0.1:%d", consts.TEST_UDP_EC_FRP_PORT)
res, err = util.SendUdpMsg(addr, consts.TEST_UDP_ECHO_STR)
2017-12-18 12:35:09 +01:00
assert.NoError(err)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_UDP_ECHO_STR, res)
2017-12-18 12:35:09 +01:00
}
func TestUnixDomain(t *testing.T) {
2017-12-16 16:59:46 +01:00
assert := assert.New(t)
// Normal
2018-07-11 17:27:47 +02:00
addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_UNIX_DOMAIN_FRP_PORT)
res, err := util.SendTcpMsg(addr, consts.TEST_UNIX_DOMAIN_STR)
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_UNIX_DOMAIN_STR, res)
2017-12-18 12:35:09 +01:00
}
}
func TestStcp(t *testing.T) {
assert := assert.New(t)
// Normal
2018-07-11 17:27:47 +02:00
addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_STCP_FRP_PORT)
res, err := util.SendTcpMsg(addr, consts.TEST_STCP_ECHO_STR)
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_STCP_ECHO_STR, res)
2017-12-18 12:35:09 +01:00
}
// Encrytion and compression
2018-07-11 17:27:47 +02:00
addr = fmt.Sprintf("127.0.0.1:%d", consts.TEST_STCP_EC_FRP_PORT)
res, err = util.SendTcpMsg(addr, consts.TEST_STCP_ECHO_STR)
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_STCP_ECHO_STR, res)
2017-12-18 12:35:09 +01:00
}
2016-05-17 13:13:37 +02:00
}
2017-12-18 12:35:09 +01:00
func TestHttp(t *testing.T) {
assert := assert.New(t)
// web01
2018-07-11 17:27:47 +02:00
code, body, _, err := util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
2017-12-18 12:35:09 +01:00
}
// web02
2018-07-11 17:27:47 +02:00
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test2.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
2017-12-18 12:35:09 +01:00
}
// error host header
2018-07-11 17:27:47 +02:00
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "errorhost.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(404, code)
}
// web03
2018-07-11 17:27:47 +02:00
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
2016-05-17 13:13:37 +02:00
}
2017-12-18 12:35:09 +01:00
2018-07-11 17:27:47 +02:00
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/foo", consts.TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_HTTP_FOO_STR, body)
2017-12-18 12:35:09 +01:00
}
// web04
2018-07-11 17:27:47 +02:00
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/bar", consts.TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_HTTP_BAR_STR, body)
2017-12-18 12:35:09 +01:00
}
// web05
2018-07-11 17:27:47 +02:00
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test5.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(401, code)
}
2018-05-20 17:55:22 +02:00
headers := make(map[string]string)
2018-07-11 17:27:47 +02:00
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, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(401, code)
2016-05-17 13:13:37 +02:00
}
2018-01-22 07:16:46 +01:00
2018-05-20 17:55:22 +02:00
// web06
var header http.Header
2018-07-11 17:27:47 +02:00
code, body, header, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test6.frp.com", nil, "")
2018-05-20 17:55:22 +02:00
if assert.NoError(err) {
assert.Equal(200, code)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
2018-05-20 17:55:22 +02:00
assert.Equal("true", header.Get("X-Header-Set"))
}
// wildcard_http
// test.frp1.com match *.frp1.com
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test.frp1.com", nil, "")
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
}
// new.test.frp1.com also match *.frp1.com
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "new.test.frp1.com", nil, "")
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
}
2018-01-22 07:16:46 +01:00
// subhost01
2018-07-11 17:27:47 +02:00
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test01.sub.com", nil, "")
2018-01-22 07:16:46 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal("test01.sub.com", body)
}
// subhost02
2018-07-11 17:27:47 +02:00
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test02.sub.com", nil, "")
2018-01-22 07:16:46 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal("test02.sub.com", body)
}
2016-05-17 13:13:37 +02:00
}
2018-01-18 07:53:44 +01:00
2018-01-23 07:49:04 +01:00
func TestWebSocket(t *testing.T) {
assert := assert.New(t)
2018-07-11 17:27:47 +02:00
u := url.URL{Scheme: "ws", Host: fmt.Sprintf("%s:%d", "127.0.0.1", consts.TEST_HTTP_FRP_PORT), Path: "/ws"}
2018-01-23 07:49:04 +01:00
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
assert.NoError(err)
defer c.Close()
2018-07-11 17:27:47 +02:00
err = c.WriteMessage(websocket.TextMessage, []byte(consts.TEST_HTTP_NORMAL_STR))
2018-01-23 07:49:04 +01:00
assert.NoError(err)
_, msg, err := c.ReadMessage()
assert.NoError(err)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_HTTP_NORMAL_STR, string(msg))
2018-01-23 07:49:04 +01:00
}
2018-04-23 19:26:05 +02:00
func TestAllowPorts(t *testing.T) {
2018-01-18 07:53:44 +01:00
assert := assert.New(t)
// Port not allowed
2018-07-11 17:27:47 +02:00
status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpPortNotAllowed)
2018-01-18 07:53:44 +01:00
if assert.NoError(err) {
2018-12-09 15:06:22 +01:00
assert.Equal(proxy.ProxyStatusStartErr, status.Status)
2018-05-23 08:39:12 +02:00
assert.True(strings.Contains(status.Err, ports.ErrPortNotAllowed.Error()))
2018-01-18 07:53:44 +01:00
}
2018-07-11 17:27:47 +02:00
status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyUdpPortNotAllowed)
2018-01-18 07:53:44 +01:00
if assert.NoError(err) {
2018-12-09 15:06:22 +01:00
assert.Equal(proxy.ProxyStatusStartErr, status.Status)
2018-05-23 08:39:12 +02:00
assert.True(strings.Contains(status.Err, ports.ErrPortNotAllowed.Error()))
2018-01-18 07:53:44 +01:00
}
2018-07-11 17:27:47 +02:00
status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpPortUnavailable)
2018-01-18 07:53:44 +01:00
if assert.NoError(err) {
2018-12-09 15:06:22 +01:00
assert.Equal(proxy.ProxyStatusStartErr, status.Status)
2018-05-23 08:39:12 +02:00
assert.True(strings.Contains(status.Err, ports.ErrPortUnAvailable.Error()))
2018-01-18 07:53:44 +01:00
}
// Port normal
2018-07-11 17:27:47 +02:00
status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpPortNormal)
2018-01-18 07:53:44 +01:00
if assert.NoError(err) {
2018-12-09 15:06:22 +01:00
assert.Equal(proxy.ProxyStatusRunning, status.Status)
2018-01-18 07:53:44 +01:00
}
2018-07-11 17:27:47 +02:00
status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyUdpPortNormal)
2018-01-18 07:53:44 +01:00
if assert.NoError(err) {
2018-12-09 15:06:22 +01:00
assert.Equal(proxy.ProxyStatusRunning, status.Status)
2018-01-18 07:53:44 +01:00
}
}
func TestRandomPort(t *testing.T) {
assert := assert.New(t)
// tcp
2018-07-11 17:27:47 +02:00
status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyTcpRandomPort)
2018-01-18 07:53:44 +01:00
if assert.NoError(err) {
addr := status.RemoteAddr
2018-07-11 17:27:47 +02:00
res, err := util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
2018-01-18 07:53:44 +01:00
assert.NoError(err)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
2018-01-18 07:53:44 +01:00
}
// udp
2018-07-11 17:27:47 +02:00
status, err = util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyUdpRandomPort)
2018-01-18 07:53:44 +01:00
if assert.NoError(err) {
addr := status.RemoteAddr
2018-07-11 17:27:47 +02:00
res, err := util.SendUdpMsg(addr, consts.TEST_UDP_ECHO_STR)
2018-01-18 07:53:44 +01:00
assert.NoError(err)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_UDP_ECHO_STR, res)
2018-01-18 07:53:44 +01:00
}
}
2018-01-23 10:11:59 +01:00
func TestPluginHttpProxy(t *testing.T) {
assert := assert.New(t)
2018-07-11 17:27:47 +02:00
status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, consts.ProxyHttpProxy)
2018-01-23 10:11:59 +01:00
if assert.NoError(err) {
2018-12-09 15:06:22 +01:00
assert.Equal(proxy.ProxyStatusRunning, status.Status)
2018-01-23 10:11:59 +01:00
// http proxy
addr := status.RemoteAddr
2018-07-11 17:27:47 +02:00
code, body, _, err := util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT),
2018-01-23 10:11:59 +01:00
"", nil, "http://"+addr)
if assert.NoError(err) {
assert.Equal(200, code)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
2018-01-23 10:11:59 +01:00
}
// connect method
2018-07-11 17:27:47 +02:00
conn, err := gnet.DialTcpByProxy("http://"+addr, fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP_FRP_PORT))
2018-01-23 10:11:59 +01:00
if assert.NoError(err) {
2018-07-11 17:27:47 +02:00
res, err := util.SendTcpMsgByConn(conn, consts.TEST_TCP_ECHO_STR)
2018-01-23 10:11:59 +01:00
assert.NoError(err)
2018-07-11 17:27:47 +02:00
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
2018-01-23 10:11:59 +01:00
}
}
}
func TestRangePortsMapping(t *testing.T) {
assert := assert.New(t)
for i := 0; i < 3; i++ {
2018-07-11 17:27:47 +02:00
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) {
2018-12-09 15:06:22 +01:00
assert.Equal(proxy.ProxyStatusRunning, status.Status)
}
}
}
2018-05-23 08:39:12 +02:00
func TestGroup(t *testing.T) {
assert := assert.New(t)
var (
p1 int
p2 int
)
2018-07-11 17:27:47 +02:00
addr := fmt.Sprintf("127.0.0.1:%d", consts.TEST_TCP2_FRP_PORT)
2018-05-23 08:39:12 +02:00
for i := 0; i < 6; i++ {
2018-07-11 17:27:47 +02:00
res, err := util.SendTcpMsg(addr, consts.TEST_TCP_ECHO_STR)
2018-05-23 08:39:12 +02:00
assert.NoError(err)
switch res {
2018-07-11 17:27:47 +02:00
case consts.TEST_TCP_ECHO_STR:
2018-05-23 08:39:12 +02:00
p1++
2018-07-11 17:27:47 +02:00
case consts.TEST_TCP_ECHO_STR + consts.TEST_TCP_ECHO_STR:
2018-05-23 08:39:12 +02:00
p2++
}
}
assert.True(p1 > 0 && p2 > 0, "group proxies load balancing")
}