frp/tests/ci/normal_test.go

320 lines
9.8 KiB
Go
Raw Normal View History

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