mirror of
https://github.com/fatedier/frp.git
synced 2024-11-23 18:49:24 +08:00
test: add function testing case
This commit is contained in:
parent
d3c4401473
commit
c680d87edc
1
.gitignore
vendored
1
.gitignore
vendored
@ -26,6 +26,7 @@ _testmain.go
|
||||
# Self
|
||||
bin/
|
||||
packages/
|
||||
test/bin/
|
||||
|
||||
# Cache
|
||||
*.swp
|
||||
|
@ -9,4 +9,4 @@ install:
|
||||
- make
|
||||
|
||||
script:
|
||||
- make test
|
||||
- make alltest
|
||||
|
34
Makefile
34
Makefile
@ -4,13 +4,18 @@ export GOPATH := $(shell pwd):$(GOPATH)
|
||||
|
||||
all: build
|
||||
|
||||
build: godep fmt frps frpc
|
||||
build: godep fmt frps frpc build_test
|
||||
|
||||
build_test: echo_server http_server
|
||||
|
||||
godep:
|
||||
GOPATH=$(OLDGOPATH) go get github.com/tools/godep
|
||||
|
||||
fmt:
|
||||
godep go fmt ./...
|
||||
go fmt ./src/...
|
||||
@go fmt ./test/echo_server.go
|
||||
@go fmt ./test/http_server.go
|
||||
@go fmt ./test/func_test.go
|
||||
|
||||
frps:
|
||||
godep go build -o bin/frps ./src/frp/cmd/frps
|
||||
@ -18,5 +23,26 @@ frps:
|
||||
frpc:
|
||||
godep go build -o bin/frpc ./src/frp/cmd/frpc
|
||||
|
||||
test:
|
||||
godep go test -v ./...
|
||||
echo_server:
|
||||
godep go build -o test/bin/echo_server ./test/echo_server.go
|
||||
|
||||
http_server:
|
||||
godep go build -o test/bin/http_server ./test/http_server.go
|
||||
|
||||
test: gotest
|
||||
|
||||
gotest:
|
||||
godep go test -v ./src/...
|
||||
|
||||
alltest:
|
||||
cd ./test && sh ./run_test.sh && cd -
|
||||
godep go test -v ./src/...
|
||||
godep go test -v ./test/func_test.go
|
||||
cd ./test && sh ./clean_test.sh && cd -
|
||||
|
||||
clean:
|
||||
rm -f ./bin/frpc
|
||||
rm -f ./bin/frps
|
||||
rm -f ./test/bin/echo_server
|
||||
rm -f ./test/bin/http_server
|
||||
cd ./test && sh ./clean_test.sh && cd -
|
||||
|
24
test/clean_test.sh
Executable file
24
test/clean_test.sh
Executable file
@ -0,0 +1,24 @@
|
||||
#!/bin/bash
|
||||
|
||||
pid=`ps aux|grep './bin/echo_server'|grep -v grep|awk {'print $2'}`
|
||||
if [ -n "${pid}" ]; then
|
||||
kill ${pid}
|
||||
fi
|
||||
|
||||
pid=`ps aux|grep './bin/http_server'|grep -v grep|awk {'print $2'}`
|
||||
if [ -n "${pid}" ]; then
|
||||
kill ${pid}
|
||||
fi
|
||||
|
||||
pid=`ps aux|grep './../bin/frps -c ./conf/auto_test_frps.ini'|grep -v grep|awk {'print $2'}`
|
||||
if [ -n "${pid}" ]; then
|
||||
kill ${pid}
|
||||
fi
|
||||
|
||||
pid=`ps aux|grep './../bin/frpc -c ./conf/auto_test_frpc.ini'|grep -v grep|awk {'print $2'}`
|
||||
if [ -n "${pid}" ]; then
|
||||
kill ${pid}
|
||||
fi
|
||||
|
||||
rm -f ./frps.log
|
||||
rm -f ./frpc.log
|
18
test/conf/auto_test_frpc.ini
Normal file
18
test/conf/auto_test_frpc.ini
Normal file
@ -0,0 +1,18 @@
|
||||
[common]
|
||||
server_addr = 0.0.0.0
|
||||
server_port = 10700
|
||||
log_file = ./frpc.log
|
||||
# debug, info, warn, error
|
||||
log_level = debug
|
||||
auth_token = 123
|
||||
|
||||
[echo]
|
||||
type = tcp
|
||||
local_ip = 127.0.0.1
|
||||
local_port = 10701
|
||||
use_encryption = true
|
||||
|
||||
[web]
|
||||
type = http
|
||||
local_ip = 127.0.0.1
|
||||
local_port = 10702
|
17
test/conf/auto_test_frps.ini
Normal file
17
test/conf/auto_test_frps.ini
Normal file
@ -0,0 +1,17 @@
|
||||
[common]
|
||||
bind_addr = 0.0.0.0
|
||||
bind_port = 10700
|
||||
vhost_http_port = 10710
|
||||
log_file = ./frps.log
|
||||
log_level = debug
|
||||
|
||||
[echo]
|
||||
type = tcp
|
||||
auth_token = 123
|
||||
bind_addr = 0.0.0.0
|
||||
listen_port = 10711
|
||||
|
||||
[web]
|
||||
type = http
|
||||
auth_token = 123
|
||||
custom_domains = 127.0.0.1
|
41
test/echo_server.go
Normal file
41
test/echo_server.go
Normal file
@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"frp/utils/conn"
|
||||
)
|
||||
|
||||
var (
|
||||
PORT int64 = 10701
|
||||
)
|
||||
|
||||
func main() {
|
||||
l, err := conn.Listen("0.0.0.0", PORT)
|
||||
if err != nil {
|
||||
fmt.Printf("echo server listen error: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
fmt.Printf("echo server accept error: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
go echoWorker(c)
|
||||
}
|
||||
}
|
||||
|
||||
func echoWorker(c *conn.Conn) {
|
||||
for {
|
||||
buff, err := c.ReadLine()
|
||||
if err != nil {
|
||||
fmt.Printf("echo server read error: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Write(buff)
|
||||
}
|
||||
}
|
60
test/func_test.go
Normal file
60
test/func_test.go
Normal file
@ -0,0 +1,60 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"frp/utils/conn"
|
||||
)
|
||||
|
||||
var (
|
||||
ECHO_PORT int64 = 10711
|
||||
HTTP_PORT int64 = 10710
|
||||
ECHO_TEST_STR string = "Hello World\n"
|
||||
HTTP_RES_STR string = "Hello World"
|
||||
)
|
||||
|
||||
func TestEchoServer(t *testing.T) {
|
||||
c, err := conn.ConnectServer("0.0.0.0", ECHO_PORT)
|
||||
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(ECHO_TEST_STR)
|
||||
|
||||
buff, err := c.ReadLine()
|
||||
if err != nil {
|
||||
t.Fatalf("read from echo server error: %v", err)
|
||||
}
|
||||
|
||||
if ECHO_TEST_STR != buff {
|
||||
t.Fatalf("content error, send [%s], get [%s]", strings.Trim(ECHO_TEST_STR, "\n"), strings.Trim(buff, "\n"))
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
20
test/http_server.go
Normal file
20
test/http_server.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
PORT int64 = 10702
|
||||
HTTP_RES_STR string = "Hello World"
|
||||
)
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", request)
|
||||
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", PORT), nil)
|
||||
}
|
||||
|
||||
func request(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(HTTP_RES_STR))
|
||||
}
|
34
test/run_test.sh
Executable file
34
test/run_test.sh
Executable file
@ -0,0 +1,34 @@
|
||||
#!/bin/bash
|
||||
|
||||
./bin/echo_server &
|
||||
./bin/http_server &
|
||||
./../bin/frps -c ./conf/auto_test_frps.ini &
|
||||
sleep 1
|
||||
./../bin/frpc -c ./conf/auto_test_frpc.ini &
|
||||
|
||||
# wait until proxies are connected
|
||||
for((i=1; i<15; i++))
|
||||
do
|
||||
sleep 1
|
||||
str=`ss -ant|grep 10700|grep LISTEN`
|
||||
if [ -z "${str}" ]; then
|
||||
echo "kong"
|
||||
continue
|
||||
fi
|
||||
|
||||
str=`ss -ant|grep 10710|grep LISTEN`
|
||||
if [ -z "${str}" ]; then
|
||||
echo "kong"
|
||||
continue
|
||||
fi
|
||||
|
||||
str=`ss -ant|grep 10711|grep LISTEN`
|
||||
if [ -z "${str}" ]; then
|
||||
echo "kong"
|
||||
continue
|
||||
fi
|
||||
|
||||
break
|
||||
done
|
||||
|
||||
sleep 1
|
Loading…
Reference in New Issue
Block a user