frp/tests/func_test.go

120 lines
2.9 KiB
Go
Raw Normal View History

2017-03-09 17:52:32 +01:00
package tests
2016-05-17 13:13:37 +02:00
import (
2017-03-08 19:03:47 +01:00
"bufio"
2017-03-27 18:27:30 +02:00
"bytes"
2016-05-17 13:13:37 +02:00
"fmt"
"io/ioutil"
2017-03-27 18:27:30 +02:00
"net"
2016-05-17 13:13:37 +02:00
"net/http"
"strings"
"testing"
"time"
2017-03-27 18:27:30 +02:00
frpNet "github.com/fatedier/frp/utils/net"
2016-05-17 13:13:37 +02:00
)
var (
ECHO_PORT int64 = 10711
2017-03-27 18:27:30 +02:00
UDP_ECHO_PORT int64 = 10712
2016-05-17 13:13:37 +02:00
HTTP_PORT int64 = 10710
ECHO_TEST_STR string = "Hello World\n"
HTTP_RES_STR string = "Hello World"
)
2017-03-09 17:52:32 +01:00
func init() {
go StartEchoServer()
2017-03-27 18:27:30 +02:00
go StartUdpEchoServer()
2017-03-09 17:52:32 +01:00
go StartHttpServer()
go StartUnixDomainServer()
2017-03-09 17:52:32 +01:00
time.Sleep(500 * time.Millisecond)
}
2016-05-17 13:13:37 +02:00
func TestEchoServer(t *testing.T) {
2017-03-27 18:27:30 +02:00
c, err := frpNet.ConnectTcpServer(fmt.Sprintf("127.0.0.1:%d", ECHO_PORT))
2016-05-17 13:13:37 +02:00
if err != nil {
t.Fatalf("connect to echo server error: %v", err)
}
timer := time.Now().Add(time.Duration(5) * time.Second)
c.SetDeadline(timer)
2017-03-09 17:52:32 +01:00
c.Write([]byte(ECHO_TEST_STR + "\n"))
2016-05-17 13:13:37 +02:00
2017-03-08 19:03:47 +01:00
br := bufio.NewReader(c)
buf, err := br.ReadString('\n')
2016-05-17 13:13:37 +02:00
if err != nil {
t.Fatalf("read from echo server error: %v", err)
}
2017-03-08 19:03:47 +01:00
if ECHO_TEST_STR != buf {
t.Fatalf("content error, send [%s], get [%s]", strings.Trim(ECHO_TEST_STR, "\n"), strings.Trim(buf, "\n"))
2016-05-17 13:13:37 +02: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)
}
}
2017-03-27 18:27:30 +02:00
func TestUdpEchoServer(t *testing.T) {
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:10712")
if err != nil {
t.Fatalf("do udp request error: %v", err)
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
t.Fatalf("dial udp server error: %v", err)
}
defer conn.Close()
_, err = conn.Write([]byte("hello frp\n"))
if err != nil {
t.Fatalf("write to udp server error: %v", err)
}
data := make([]byte, 20)
n, err := conn.Read(data)
if err != nil {
t.Fatalf("read from udp server error: %v", err)
}
if string(bytes.TrimSpace(data[:n])) != "hello frp" {
t.Fatalf("message got from udp server error, get %s", string(data[:n-1]))
}
}
func TestUnixDomainServer(t *testing.T) {
c, err := frpNet.ConnectTcpServer(fmt.Sprintf("127.0.0.1:%d", 10704))
if err != nil {
t.Fatalf("connect to echo server error: %v", err)
}
timer := time.Now().Add(time.Duration(5) * time.Second)
c.SetDeadline(timer)
c.Write([]byte(ECHO_TEST_STR + "\n"))
br := bufio.NewReader(c)
buf, err := br.ReadString('\n')
if err != nil {
t.Fatalf("read from echo server error: %v", err)
}
if ECHO_TEST_STR != buf {
t.Fatalf("content error, send [%s], get [%s]", strings.Trim(ECHO_TEST_STR, "\n"), strings.Trim(buf, "\n"))
}
}