forked from nanomsg/mangos
/
transport.go
463 lines (424 loc) · 12.1 KB
/
transport.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Copyright 2018 The Mangos Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package test
import (
"bytes"
"crypto/tls"
"net"
"strings"
"sync"
"testing"
"time"
"nanomsg.org/go/mangos/v2"
"nanomsg.org/go/mangos/v2/protocol/rep"
"nanomsg.org/go/mangos/v2/protocol/req"
)
// TranTest provides a common test structure for transports, so that they
// can implement a battery of standard tests.
type TranTest struct {
addr string
tran mangos.Transport
cliCfg *tls.Config
srvCfg *tls.Config
sockRep mangos.Socket
sockReq mangos.Socket
}
// NewTranTest creates a TranTest.
func NewTranTest(tran mangos.Transport, addr string) *TranTest {
tt := &TranTest{addr: addr, tran: tran}
if strings.HasPrefix(tt.addr, "tls+tcp://") || strings.HasPrefix(tt.addr, "wss://") {
tt.cliCfg, _ = GetTLSConfig(false)
tt.srvCfg, _ = GetTLSConfig(true)
}
tt.sockRep, _ = rep.NewSocket()
tt.sockReq, _ = req.NewSocket()
return tt
}
// TestListenAndAccept tests that we can both listen and accept connections
// for the given transport.
func (tt *TranTest) TestListenAndAccept(t *testing.T) {
t.Logf("Establishing listener for %s", tt.addr)
l, err := tt.tran.NewListener(tt.addr, tt.sockRep)
if err != nil {
t.Errorf("NewListener failed: %v", err)
return
}
defer l.Close()
if tt.srvCfg != nil {
if err = l.SetOption(mangos.OptionTLSConfig, tt.srvCfg); err != nil {
t.Errorf("Failed setting TLS config: %v", err)
return
}
}
if err = l.Listen(); err != nil {
t.Errorf("Listen failed: %v", err)
return
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
t.Logf("Connecting on %s", tt.addr)
d, err := tt.tran.NewDialer(tt.addr, tt.sockReq)
if err != nil {
t.Errorf("NewDialer failed: %v", err)
return
}
if tt.cliCfg != nil {
if err = d.SetOption(mangos.OptionTLSConfig, tt.cliCfg); err != nil {
t.Errorf("Failed setting TLS config: %v", err)
return
}
}
client, err := d.Dial()
if err != nil {
t.Errorf("Dial failed: %v", err)
return
}
if v, err := client.GetOption(mangos.OptionLocalAddr); err == nil {
addr := v.(net.Addr)
t.Logf("Dialed on local net %s addr %s", addr.Network(), addr.String())
} else {
t.Logf("err is %v", err.Error())
}
if v, err := client.GetOption(mangos.OptionRemoteAddr); err == nil {
addr := v.(net.Addr)
t.Logf("Dialed remote peer %s addr %s", addr.Network(), addr.String())
}
t.Logf("Connected client: %d (server %d)",
client.LocalProtocol(), client.RemoteProtocol())
}()
server, err := l.Accept()
if err != nil {
t.Errorf("Accept failed: %v", err)
return
}
if v, err := server.GetOption(mangos.OptionLocalAddr); err == nil {
addr := v.(net.Addr)
t.Logf("Accepted on local net %s addr %s", addr.Network(), addr.String())
}
if v, err := server.GetOption(mangos.OptionRemoteAddr); err == nil {
addr := v.(net.Addr)
t.Logf("Accepted remote peer %s addr %s", addr.Network(), addr.String())
}
defer server.Close()
t.Logf("Connected server: %d (client %d)",
server.LocalProtocol(), server.RemoteProtocol())
wg.Wait()
}
// TestDuplicateListen checks to make sure that an attempt to listen
// on a second socket, when another listener is already present, properly
// fails with ErrAddrInUse.
func (tt *TranTest) TestDuplicateListen(t *testing.T) {
var err error
time.Sleep(100 * time.Millisecond)
t.Logf("Testing Duplicate Listen on %s", tt.addr)
l1, err := tt.tran.NewListener(tt.addr, tt.sockRep)
if err != nil {
t.Errorf("NewListener failed: %v", err)
return
}
defer l1.Close()
if tt.srvCfg != nil {
if err = l1.SetOption(mangos.OptionTLSConfig, tt.srvCfg); err != nil {
t.Errorf("Failed setting TLS config: %v", err)
return
}
}
if err = l1.Listen(); err != nil {
t.Errorf("Listen failed: %v", err)
return
}
l2, err := tt.tran.NewListener(tt.addr, tt.sockReq)
if err != nil {
t.Errorf("NewListener faield: %v", err)
return
}
defer l2.Close()
if tt.srvCfg != nil {
if err = l2.SetOption(mangos.OptionTLSConfig, tt.srvCfg); err != nil {
t.Errorf("Failed setting TLS config: %v", err)
return
}
}
if err = l2.Listen(); err == nil {
t.Errorf("Duplicate listen should not be permitted!")
return
}
t.Logf("Got expected error: %v", err)
}
// TestConnRefused tests that attempts to dial to an address without a listener
// properly fail with EConnRefused.
func (tt *TranTest) TestConnRefused(t *testing.T) {
d, err := tt.tran.NewDialer(tt.addr, tt.sockReq)
if err != nil || d == nil {
t.Errorf("New Dialer failed: %v", err)
}
if tt.cliCfg != nil {
if err = d.SetOption(mangos.OptionTLSConfig, tt.cliCfg); err != nil {
t.Errorf("Failed setting TLS config: %v", err)
return
}
}
c, err := d.Dial()
if err == nil || c != nil {
t.Errorf("Connection not refused (%s)!", tt.addr)
return
}
t.Logf("Got expected error: %v", err)
}
// TestSendRecv test that the transport can send and receive. It uses the
// REQ/REP protocol for messages.
func (tt *TranTest) TestSendRecv(t *testing.T) {
ping := []byte("REQUEST_MESSAGE")
ack := []byte("RESPONSE_MESSAGE")
ch := make(chan *mangos.Message)
t.Logf("Establishing REP listener on %s", tt.addr)
l, err := tt.tran.NewListener(tt.addr, tt.sockRep)
if err != nil {
t.Errorf("NewListener failed: %v", err)
return
}
defer l.Close()
if tt.srvCfg != nil {
if err = l.SetOption(mangos.OptionTLSConfig, tt.srvCfg); err != nil {
t.Errorf("Failed setting TLS config: %v", err)
return
}
}
if err = l.Listen(); err != nil {
t.Errorf("Listen failed: %v", err)
return
}
go func() {
defer close(ch)
// Client side
t.Logf("Connecting REQ on %s", tt.addr)
d, err := tt.tran.NewDialer(tt.addr, tt.sockReq)
if err != nil {
t.Errorf("Failed creating Dialer: %v", err)
}
if tt.cliCfg != nil {
if err = d.SetOption(mangos.OptionTLSConfig, tt.cliCfg); err != nil {
t.Errorf("Failed setting TLS config: %v", err)
return
}
}
client, err := d.Dial()
if err != nil {
t.Errorf("Dial failed: %v", err)
return
}
t.Logf("Connected client")
defer client.Close()
req := mangos.NewMessage(len(ping))
req.Body = append(req.Body, ping...)
// Now try to send data
t.Logf("Sending %d bytes", len(req.Body))
err = client.Send(req)
if err != nil {
t.Errorf("Client send error: %v", err)
return
}
t.Logf("Client sent")
rep, err := client.Recv()
if err != nil {
t.Errorf("Client receive error: %v", err)
return
}
if !bytes.Equal(rep.Body, ack) {
t.Errorf("Reply mismatch: %v, %v", rep.Body, ack)
return
}
if len(rep.Header) != 0 {
t.Errorf("Client reply non-empty header: %v",
rep.Header)
return
}
select {
case ch <- rep:
t.Log("Client reply forwarded")
case <-time.After(5 * time.Second): // 5 secs should be plenty
t.Error("Client timeout forwarding reply")
return
}
}()
server, err := l.Accept()
if err != nil {
t.Errorf("Accept failed: %v", err)
return
}
t.Logf("Connected server")
defer server.Close()
// Now we can try to send and receive
req, err := server.Recv()
if err != nil {
t.Errorf("Server receive error: %v", err)
return
}
t.Logf("Server received %d bytes", len(req.Body))
if !bytes.Equal(req.Body, ping) {
t.Errorf("Request mismatch: %v, %v", req.Body, ping)
return
}
if len(req.Header) != 0 {
t.Errorf("Server request non-empty header: %v", req.Header)
return
}
// Now reply
rep := mangos.NewMessage(len(ack))
rep.Body = append(rep.Body, ack...)
t.Logf("Server sending %d bytes", len(rep.Body))
err = server.Send(rep)
if err != nil {
t.Errorf("Server send error: %v", err)
return
}
t.Log("Server reply sent")
// Wait for client to ack reply over back channel.
select {
case nrep := <-ch:
if !bytes.Equal(nrep.Body, ack) {
t.Errorf("Client forward mismatch: %v, %v", ack, rep)
return
}
case <-time.After(5 * time.Second):
t.Error("Client timeout?")
return
}
}
// TestScheme tests the Scheme() entry point on the transport.
func (tt *TranTest) TestScheme(t *testing.T) {
scheme := tt.tran.Scheme()
t.Log("Checking scheme")
if !strings.HasPrefix(tt.addr, scheme+"://") {
t.Errorf("Wrong scheme: addr %s, scheme %s", tt.addr, scheme)
return
}
t.Log("Scheme match")
}
// TestListenerSetOptionInvalid tests passing invalid options to a listener.
func (tt *TranTest) TestListenerSetOptionInvalid(t *testing.T) {
t.Log("Trying invalid listener SetOption")
l, err := tt.tran.NewListener(tt.addr, tt.sockRep)
if err != nil {
t.Errorf("Unable to create listener")
return
}
err = l.SetOption("NO-SUCH-OPTION", true)
switch err {
case mangos.ErrBadOption:
t.Log("Got expected err BadOption")
case nil:
t.Errorf("Got nil err, but expected BadOption!")
default:
t.Errorf("Got unexpected error %v, expected BadOption", err)
}
}
// TestListenerGetOptionInvalid tests trying to get an invalid option on
// a listener.
func (tt *TranTest) TestListenerGetOptionInvalid(t *testing.T) {
t.Log("Trying invalid listener GetOption")
l, err := tt.tran.NewListener(tt.addr, tt.sockRep)
if err != nil {
t.Errorf("Unable to create listener")
return
}
_, err = l.GetOption("NO-SUCH-OPTION")
switch err {
case mangos.ErrBadOption:
t.Log("Got expected err BadOption")
case nil:
t.Errorf("Got nil err, but expected BadOption!")
default:
t.Errorf("Got unexpected error %v, expected BadOption", err)
}
}
// TestDialerSetOptionInvalid tests trying to set an invalid option on a Dialer.
func (tt *TranTest) TestDialerSetOptionInvalid(t *testing.T) {
t.Log("Trying invalid dialer SetOption")
d, err := tt.tran.NewDialer(tt.addr, tt.sockRep)
if err != nil {
t.Errorf("Unable to create dialer")
return
}
err = d.SetOption("NO-SUCH-OPTION", true)
switch err {
case mangos.ErrBadOption:
t.Log("Got expected err BadOption")
case nil:
t.Errorf("Got nil err, but expected BadOption!")
default:
t.Errorf("Got unexpected error %v, expected BadOption", err)
}
}
// TestDialerGetOptionInvalid tests attempting to get an invalid option on
// a Dialer.
func (tt *TranTest) TestDialerGetOptionInvalid(t *testing.T) {
t.Log("Trying invalid listener GetOption")
d, err := tt.tran.NewDialer(tt.addr, tt.sockRep)
if err != nil {
t.Errorf("Unable to create dialer")
return
}
_, err = d.GetOption("NO-SUCH-OPTION")
switch err {
case mangos.ErrBadOption:
t.Log("Got expected err BadOption")
case nil:
t.Errorf("Got nil err, but expected BadOption!")
default:
t.Errorf("Got unexpected error %v, expected BadOption", err)
}
}
// TestDialerBadScheme tests to makes sure that giving a bogus scheme
// to create a dialer fails properly.
func (tt *TranTest) TestDialerBadScheme(t *testing.T) {
t.Logf("NewDialer with bogus scheme")
d, err := tt.tran.NewDialer("bogus://address", tt.sockRep)
if err == nil {
t.Errorf("Expected error, got nil")
} else if d != nil {
t.Errorf("Got non-nil error, and non-nil dialer")
} else {
t.Logf("Got expected error %v", err)
}
}
// TestListenerBadScheme tests to makes sure that giving a bogus scheme
// to create a listener fails properly.
func (tt *TranTest) TestListenerBadScheme(t *testing.T) {
t.Logf("NewListener with bogus scheme")
d, err := tt.tran.NewListener("bogus://address", tt.sockRep)
if err == nil {
t.Errorf("Expected error, got nil")
} else if d != nil {
t.Errorf("Got non-nil error, and non-nil listener")
} else {
t.Logf("Got expected error %v", err)
}
}
// TestAll runs a full battery of standard tests on the transport.
func (tt *TranTest) TestAll(t *testing.T) {
tt.TestScheme(t)
tt.TestListenAndAccept(t)
tt.TestConnRefused(t)
tt.TestDuplicateListen(t)
tt.TestSendRecv(t)
tt.TestDialerSetOptionInvalid(t)
tt.TestDialerGetOptionInvalid(t)
tt.TestListenerSetOptionInvalid(t)
tt.TestListenerGetOptionInvalid(t)
tt.TestDialerBadScheme(t)
tt.TestListenerBadScheme(t)
}