This repository has been archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_test.go
123 lines (105 loc) · 2.38 KB
/
server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package remoton
import (
"bufio"
"crypto/tls"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
)
//TestDialAndListen test websocket tunnel
func TestDialAndListen(t *testing.T) {
maxConns := 10
mux := http.NewServeMux()
mux.Handle("/remoton/", http.StripPrefix("/remoton", NewServer(
func(authToken string, r *http.Request) bool {
return authToken == "testsrv"
},
func() string {
return "testid"
})))
ts := httptest.NewTLSServer(mux)
defer ts.Close()
rclient := Client{Prefix: "/remoton", TLSConfig: &tls.Config{
InsecureSkipVerify: true,
}}
session, err := rclient.NewSession(ts.URL, "testsrv")
if err != nil {
t.Fatal(err)
}
defer session.Destroy()
go func() {
listener := session.Listen("test")
for i := 0; i < maxConns; i++ {
lconn, err := listener.Accept()
if err != nil {
t.Error(err)
}
data, _ := bufio.NewReader(lconn).ReadString('\n')
if strings.TrimSpace(data) != "transfer" {
t.Errorf("want %v get %v", "transfer", data)
} else {
lconn.Write([]byte("feedback"))
lconn.Write([]byte{'\n'})
}
}
}()
wg := &sync.WaitGroup{}
worker := func() {
go func() {
dconn, err := session.Dial("test")
if err != nil {
t.Fatal(err)
}
dconn.Write([]byte("transfer"))
dconn.Write([]byte{'\n'})
data, err := bufio.NewReader(dconn).ReadString('\n')
if err != nil {
t.Error(err)
}
if strings.TrimSpace(data) != "feedback" {
t.Errorf("want %v get %v", "feedback", data)
}
dconn.Close()
wg.Done()
}()
}
for i := 0; i < maxConns; i++ {
wg.Add(1)
worker()
}
wg.Wait()
}
//TestDialAndListenTCP tcp tunnel
func TestDialAndListenTCP(t *testing.T) {
ts := httptest.NewTLSServer(NewServer(
func(authToken string, r *http.Request) bool {
return authToken == "testsrv"
}, func() string {
return "testid"
}))
defer ts.Close()
rclient := Client{Prefix: "", TLSConfig: &tls.Config{
InsecureSkipVerify: true,
}}
session, err := rclient.NewSession(ts.URL, "testsrv")
if err != nil {
t.Fatal(err)
}
defer session.Destroy()
go func() {
listener := session.ListenTCP("test")
lconn, err := listener.Accept()
if err != nil {
t.Error(err)
}
data, _ := bufio.NewReader(lconn).ReadString('\n')
if data != "transfer" {
t.Errorf("want %v get %v", "transfer", data)
}
}()
dconn, err := session.DialTCP("test")
dconn.Write([]byte("transfer"))
dconn.Close()
}