-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn_test.go
396 lines (335 loc) · 7.51 KB
/
conn_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package gota
import (
"bytes"
"errors"
"io"
"net"
_ "net/http/pprof"
"testing"
"time"
log "github.com/Sirupsen/logrus"
)
func TestNewCCID(t *testing.T) {
cc := NewCCID(1, 2)
if cc.ClientID() != 1 {
t.Errorf("Error client ID: %d", cc.ClientID())
}
if cc.ConnID() != 2 {
t.Errorf("Error conn ID: %d", cc.ConnID())
}
cc = NewCCID(0xFFFFFFFF, 0xFFFFFFFE)
if cc.ClientID() != 0xFFFFFFFF {
t.Errorf("Error client ID: %d", cc.ClientID())
}
if cc.ConnID() != 0xFFFFFFFE {
t.Errorf("Error conn ID: %d", cc.ConnID())
}
}
// buffer is just here to wrap bytes.Buffer to an io.ReadWriteCloser.
// Read about embedding to see how this works.
type buffer struct {
strings [][]byte
rnum int
wnum int
wclosed bool
rclosed bool
}
func (b *buffer) Read(p []byte) (n int, err error) {
l := len(b.strings)
if b.rnum < l {
copy(p, b.strings[b.rnum])
n = len(b.strings[b.rnum])
err = nil
b.rnum++
return
}
n = 0
err = io.EOF
return
}
func (b *buffer) Write(p []byte) (n int, err error) {
l := len(b.strings)
if b.wnum < l {
if bytes.Compare(p, b.strings[b.wnum]) == 0 {
log.Infof("Received test string: \"%s\"", b.strings[b.wnum])
n = len(b.strings[b.wnum])
err = nil
b.wnum++
return
} else {
n = 0
err = errors.New("Mismatch!")
return
}
}
n = 0
err = nil
return
//return b.Buffer.Write(p)
}
// Add a Close method to our buffer so that we satisfy io.ReadWriteCloser.
func (b *buffer) Close() error {
//b.Buffer.Reset()
b.rnum = 0
b.wnum = 0
b.wclosed = true
b.rclosed = true
return nil
}
func (b *buffer) CloseWrite() error {
b.wnum = 0
b.wclosed = true
return nil
}
func (b *buffer) CloseRead() error {
b.rnum = 0
b.rclosed = true
return nil
}
func newBuffer() *buffer {
bufLength := 3
buf := &buffer{
rnum: 0,
wnum: 0,
}
buf.strings = make([][]byte, bufLength)
buf.strings[0] = []byte("One of the most useful data structures in computer science is the hash table.")
buf.strings[1] = []byte("Many hash table implementations exist with varying properties, but in general they offer fast lookups, adds, and deletes. ")
buf.strings[2] = []byte("Go provides a built-in map type that implements a hash table.")
return buf
}
func TestConnHandler_Start(t *testing.T) {
buf := newBuffer()
rc := make(chan *GotaFrame)
wc := make(chan *GotaFrame)
ch := &ConnHandler{
ClientID: 0,
ConnID: 0,
rw: buf,
WriteToTunnelC: wc,
ReadFromTunnelC: rc,
}
ch.Start()
// received
go func() {
for c := range wc {
t.Logf("Received GotaFrame: %s", c)
if c.IsControl() {
continue
}
rc <- c
}
}()
time.Sleep(time.Second * 3)
// close conn
gf := &GotaFrame{
Control: true,
ConnID: 0,
clientID: 0,
SeqNum: TMCloseConnSeq,
Length: 0,
}
rc <- gf
time.Sleep(time.Second * 1)
if !ch.Stopped() {
t.Error("conn handler did not stop")
ch.Stop()
}
if buf.wclosed != true {
t.Error("buffer does not be closed!")
}
}
func TestConnHandler_CreatePeerConn(t *testing.T) {
rc := make(chan *GotaFrame)
wc := make(chan *GotaFrame)
ch := &ConnHandler{
ClientID: 0,
ConnID: 0,
rw: nil,
WriteToTunnelC: wc,
ReadFromTunnelC: rc,
}
result := make(chan bool)
go func() {
result <- ch.CreatePeerConn()
}()
req := <-wc
log.Debugf("received request: %s", req)
if !req.Control || req.SeqNum != TMCreateConnSeq {
t.Error("Error request for create connection")
}
resp := &GotaFrame{
Control: true,
ConnID: 0,
clientID: 0,
SeqNum: TMCreateConnOKSeq,
Length: 0,
}
rc <- resp
if ok := <-result; ok {
t.Log("Create peer conn ok")
} else {
t.Error("Create peer conn error")
}
}
func TestConnManager_handleNewConn(t *testing.T) {
buf := newBuffer()
cm := NewConnManager()
newConnChannel := make(chan io.ReadWriteCloser)
go cm.handleNewConn(newConnChannel)
// new conn
newConnChannel <- buf
time.Sleep(time.Second * 1)
log.Debugf("%s", cm.connHandlerPool)
// fake response for create peer connection
req := <-cm.writeToTunnelC
log.Debugf("received request: %s", req)
if !req.Control || req.SeqNum != TMCreateConnSeq {
t.Error("Error request for create connection")
}
resp := &GotaFrame{
Control: true,
ConnID: 0,
clientID: cm.clientID,
SeqNum: TMCreateConnOKSeq,
Length: 0,
}
cm.connHandlerPool[NewCCID(cm.clientID, 0)].ReadFromTunnelC <- resp
// received
go func() {
for c := range cm.WriteToTunnelChannel() {
t.Logf("Received GotaFrame: %s", c)
if c.IsControl() {
continue
}
cm.connHandlerPool[NewCCID(cm.clientID, 0)].ReadFromTunnelC <- c
}
}()
time.Sleep(time.Second * 3)
// close conn
gf := &GotaFrame{
Control: true,
ConnID: 0,
clientID: cm.clientID,
SeqNum: TMCloseConnSeq,
Length: 0,
}
cm.connHandlerPool[NewCCID(cm.clientID, 0)].ReadFromTunnelC <- gf
time.Sleep(time.Second * 1)
for _, ch := range cm.connHandlerPool {
if !ch.Stopped() {
t.Error("conn handler did not stop normally")
ch.Stop()
}
}
}
func TestConnManager_handleNewCCID(t *testing.T) {
addr := "127.0.0.1:32768"
laddr, _ := net.ResolveTCPAddr("tcp4", addr)
// create a tcp server for testing
go helpListenAndServe(laddr, t)
cm := NewConnManager()
go cm.dispatch()
go cm.handleNewCCID(laddr)
ccid := cm.NewCCIDChannel()
ccid <- NewCCID(0, 0)
go func() {
rc := cm.ReadFromTunnelChannel()
wc := cm.WriteToTunnelChannel()
for gf := range wc {
log.Debugf("forward frame: %s", gf)
rc <- gf
}
}()
//log.Info(cm.connHandlerPool[NewCCID(0, 0)].Stopped())
time.Sleep(time.Second * 10)
cm.Stop()
//log.Info(cm.connHandlerPool[NewCCID(0, 0)].Stopped())
}
func TestConnManager_ListenAndServe(t *testing.T) {
cm := NewConnManager()
addr := "127.0.0.1:32769"
tcpAddr, _ := net.ResolveTCPAddr("tcp", addr)
go cm.ListenAndServe(addr)
time.Sleep(time.Second * 1)
go helpDialAndServe(tcpAddr, t)
rc := cm.ReadFromTunnelChannel()
wc := cm.WriteToTunnelChannel()
// create a peer connection
req := <-wc
log.Debugf("received request: %s", req)
if !req.Control || req.SeqNum != TMCreateConnSeq {
t.Error("Error request for create connection")
}
resp := &GotaFrame{
Control: true,
ConnID: 0,
clientID: cm.clientID,
SeqNum: TMCreateConnOKSeq,
Length: 0,
}
rc <- resp
go func() {
for gf := range wc {
log.Debugf("forward frame: %s", gf)
rc <- gf
}
}()
time.Sleep(time.Second * 10)
//cm.Stop()
}
func helpDialAndServe(raddr *net.TCPAddr, t *testing.T) {
conn, _ := net.DialTCP("tcp", nil, raddr)
helpServe(conn, t)
}
func helpServe(conn *net.TCPConn, t *testing.T) {
buf := newBuffer()
go func() {
data := make([]byte, 65536)
for {
n, err := conn.Read(data)
if n > 0 {
buf.Write(data[:n])
}
if err == io.EOF {
break
}
if err != nil {
t.Errorf("Read from conn error: %s", err)
}
time.Sleep(time.Second * 1)
}
}()
go func() {
data := make([]byte, 65536)
for {
n, err := buf.Read(data)
if n > 0 {
//log.Debugf("try to write buf to connection: %s, %d, %d", data, n, len(data))
_, err = conn.Write(data[:n])
}
if err == io.EOF {
conn.CloseWrite()
break
}
if err != nil {
t.Errorf("Write to conn error: %s", err)
}
time.Sleep(time.Second * 1)
}
}()
}
func helpListenAndServe(laddr *net.TCPAddr, t *testing.T) {
listener, _ := net.ListenTCP("tcp4", laddr)
conn, _ := listener.AcceptTCP()
//c <- conn
log.Debug("new connection received")
helpServe(conn, t)
}
func init() {
log.SetLevel(log.DebugLevel)
// pprof debug
//go func() {
// log.Println(http.ListenAndServe("localhost:6060", nil))
//}()
}