frp/tests/func_test.go

98 lines
2.6 KiB
Go
Raw Normal View History

2017-03-10 00:52:32 +08:00
package tests
2016-05-17 19:13:37 +08:00
import (
2017-03-09 02:03:47 +08:00
"bufio"
2017-03-28 00:27:30 +08:00
"bytes"
2016-05-17 19:13:37 +08:00
"fmt"
"io/ioutil"
2017-03-28 00:27:30 +08:00
"net"
2016-05-17 19:13:37 +08:00
"net/http"
"strings"
"testing"
"time"
2017-03-28 00:27:30 +08:00
frpNet "github.com/fatedier/frp/utils/net"
2017-12-16 23:59:46 +08:00
"github.com/stretchr/testify/assert"
2016-05-17 19:13:37 +08:00
)
var (
2017-12-16 23:59:46 +08:00
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 int64 = 10701
TEST_TCP_FRP_PORT int64 = 10801
TEST_TCP_EC_FRP_PORT int64 = 10901
TEST_TCP_ECHO_STR string = "tcp type:" + TEST_STR
TEST_UDP_PORT int64 = 10702
TEST_UDP_FRP_PORT int64 = 10802
TEST_UDP_ECHO_STR string = "udp type:" + TEST_STR
TEST_UNIX_DOMAIN_ADDR string = "/tmp/frp_echo_server.sock"
TEST_UNIX_DOMAIN_FRP_PORT int64 = 10803
TEST_UNIX_DOMAIN_STR string = "unix domain type:" + TEST_STR
TEST_HTTP_PORT int64 = 10704
TEST_HTTP_FRP_PORT int64 = 10804
TEST_HTTP_WEB01_STR string = "http web01:" + TEST_STR
2016-05-17 19:13:37 +08:00
)
2017-03-10 00:52:32 +08:00
func init() {
2017-12-16 23:59:46 +08:00
go StartTcpEchoServer()
2017-03-28 00:27:30 +08:00
go StartUdpEchoServer()
go StartUnixDomainServer()
2017-12-16 23:59:46 +08:00
go StartHttpServer()
2017-03-10 00:52:32 +08:00
time.Sleep(500 * time.Millisecond)
}
2017-12-16 23:59:46 +08:00
func TestTcpServer(t *testing.T) {
assert := assert.New(t)
// Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_TCP_FRP_PORT)
res, err := sendTcpMsg(addr, TEST_TCP_ECHO_STR)
assert.NoError(err)
assert.Equal(TEST_TCP_ECHO_STR, res)
2016-05-17 19:13:37 +08:00
2017-12-16 23:59:46 +08:00
// Encrytion and compression
addr = fmt.Sprintf("127.0.0.1:%d", TEST_TCP_EC_FRP_PORT)
res, err = sendTcpMsg(addr, TEST_TCP_ECHO_STR)
assert.NoError(err)
assert.Equal(TEST_TCP_ECHO_STR, res)
}
2016-05-17 19:13:37 +08:00
2017-12-16 23:59:46 +08:00
func TestUdpEchoServer(t *testing.T) {
assert := assert.New(t)
// Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_UDP_FRP_PORT)
res, err := sendUdpMsg(addr, TEST_UDP_ECHO_STR)
assert.NoError(err)
assert.Equal(TEST_UDP_ECHO_STR, res)
2016-05-17 19:13:37 +08:00
2017-12-16 23:59:46 +08:00
func TestUnixDomainServer(t *testing.T) {
assert := assert.New(t)
// Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_UNIX_DOMAIN_FRP_PORT)
res, err := sendTcpMsg(addr, TEST_UNIX_DOMAIN_STR)
assert.NoError(err)
assert.Equal(TEST_UNIX_DOMAIN_STR, res)
2016-05-17 19:13:37 +08:00
}
func TestHttpServer(t *testing.T) {
client := &http.Client{}
req, _ := http.NewRequest("GET", fmt.Sprintf("http://127.0.0.1:%d", HTTP_PORT), nil)
res, err := client.Do(req)
if err != nil {
t.Fatalf("do http request error: %v", err)
}
if res.StatusCode == 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("read from http server error: %v", err)
}
bodystr := string(body)
if bodystr != HTTP_RES_STR {
t.Fatalf("content from http server error [%s], correct string is [%s]", bodystr, HTTP_RES_STR)
}
} else {
t.Fatalf("http code from http server error [%d]", res.StatusCode)
}
}