frp/tests/ci/normal_test.go

251 lines
7.4 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"
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
2018-12-09 22:06:22 +08:00
"github.com/fatedier/frp/client/proxy"
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-12-09 21:56:46 +08: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)
}
2020-05-24 17:48:37 +08:00
go mock.StartUDPEchoServer(consts.TEST_UDP_PORT)
2018-07-11 23:27:47 +08:00
go mock.StartUnixDomainServer(consts.TEST_UNIX_DOMAIN_ADDR)
2020-05-24 17:48:37 +08:00
go mock.StartHTTPServer(consts.TEST_HTTP_PORT)
2018-07-11 23:27:47 +08:00
2018-07-12 15:23:34 +08:00
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)
}
time.Sleep(500 * time.Millisecond)
2018-07-12 15:23:34 +08:00
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
}
2020-05-24 17:48:37 +08:00
func TestHTTP(t *testing.T) {
2017-12-18 19:35:09 +08:00
assert := assert.New(t)
// web01
2020-05-24 17:48:37 +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
2020-05-24 17:48:37 +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
2020-05-24 17:48:37 +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
2020-05-24 17:48:37 +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
2020-05-24 17:48:37 +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
2020-05-24 17:48:37 +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
2020-05-24 17:48:37 +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")
2020-05-24 17:48:37 +08:00
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
2020-05-24 17:48:37 +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"))
}
// wildcard_http
// test.frp1.com match *.frp1.com
2020-05-24 17:48:37 +08:00
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
2020-05-24 17:48:37 +08:00
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 14:16:46 +08:00
// subhost01
2020-05-24 17:48:37 +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
2020-05-24 17:48:37 +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-01-18 14:53:44 +08:00
func TestRandomPort(t *testing.T) {
assert := assert.New(t)
// tcp
2020-05-24 17:48:37 +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
2020-05-24 17:48:37 +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
2020-05-24 17:48:37 +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
2020-05-24 17:48:37 +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
2020-05-24 17:48:37 +08:00
func TestPluginHTTPProxy(t *testing.T) {
2018-01-23 17:11:59 +08:00
assert := assert.New(t)
2020-05-24 17:48:37 +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) {
2020-05-24 17:48:37 +08:00
assert.Equal(proxy.ProxyPhaseRunning, status.Status)
2018-01-23 17:11:59 +08:00
// http proxy
addr := status.RemoteAddr
2020-05-24 17:48:37 +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) {
2020-05-24 17:48:37 +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++ {
2020-05-24 17:48:37 +08:00
name := fmt.Sprintf("%s_%d", consts.ProxyRangeTCPPrefix, i)
2018-07-11 23:27:47 +08:00
status, err := util.GetProxyStatus(consts.ADMIN_ADDR, consts.ADMIN_USER, consts.ADMIN_PWD, name)
if assert.NoError(err) {
2020-05-24 17:48:37 +08:00
assert.Equal(proxy.ProxyPhaseRunning, 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++ {
2020-05-24 17:48:37 +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")
}