forked from tarantool/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dial_test.go
948 lines (822 loc) · 20.9 KB
/
dial_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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
package tarantool_test
import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"net"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tarantool/go-iproto"
"github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/test_helpers"
)
type mockErrorDialer struct {
err error
}
func (m mockErrorDialer) Dial(ctx context.Context,
opts tarantool.DialOpts) (tarantool.Conn, error) {
return nil, m.err
}
func TestDialer_Dial_error(t *testing.T) {
const errMsg = "any msg"
dialer := mockErrorDialer{
err: errors.New(errMsg),
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
assert.Nil(t, conn)
assert.ErrorContains(t, err, errMsg)
}
type mockPassedDialer struct {
ctx context.Context
opts tarantool.DialOpts
}
func (m *mockPassedDialer) Dial(ctx context.Context,
opts tarantool.DialOpts) (tarantool.Conn, error) {
m.opts = opts
if ctx != m.ctx {
return nil, errors.New("wrong context")
}
return nil, errors.New("does not matter")
}
func TestDialer_Dial_passedOpts(t *testing.T) {
dialer := &mockPassedDialer{}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
dialer.ctx = ctx
dialOpts := tarantool.DialOpts{
IoTimeout: opts.Timeout,
}
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{
Timeout: opts.Timeout,
})
assert.Nil(t, conn)
assert.NotNil(t, err)
assert.NotEqual(t, err.Error(), "wrong context")
assert.Equal(t, dialOpts, dialer.opts)
}
type mockIoConn struct {
addr net.Addr
// Addr calls count.
addrCnt int
// Sends an event on Read()/Write()/Flush().
read, written chan struct{}
// Read()/Write() buffers.
readbuf, writebuf bytes.Buffer
// Calls readWg/writeWg.Wait() in Read()/Flush().
readWg, writeWg sync.WaitGroup
// How many times to wait before a wg.Wait() call.
readWgDelay, writeWgDelay int
// Write()/Read()/Flush()/Close() calls count.
writeCnt, readCnt, flushCnt, closeCnt int
// Greeting()/ProtocolInfo() calls count.
greetingCnt, infoCnt int
// Value for Greeting().
greeting tarantool.Greeting
// Value for ProtocolInfo().
info tarantool.ProtocolInfo
}
func (m *mockIoConn) Read(b []byte) (int, error) {
m.readCnt++
if m.readWgDelay == 0 {
m.readWg.Wait()
}
m.readWgDelay--
ret, err := m.readbuf.Read(b)
if ret != 0 && m.read != nil {
m.read <- struct{}{}
}
return ret, err
}
func (m *mockIoConn) Write(b []byte) (int, error) {
m.writeCnt++
if m.writeWgDelay == 0 {
m.writeWg.Wait()
}
m.writeWgDelay--
ret, err := m.writebuf.Write(b)
if m.written != nil {
m.written <- struct{}{}
}
return ret, err
}
func (m *mockIoConn) Flush() error {
m.flushCnt++
return nil
}
func (m *mockIoConn) Close() error {
m.closeCnt++
return nil
}
func (m *mockIoConn) Greeting() tarantool.Greeting {
m.greetingCnt++
return m.greeting
}
func (m *mockIoConn) ProtocolInfo() tarantool.ProtocolInfo {
m.infoCnt++
return m.info
}
func (m *mockIoConn) Addr() net.Addr {
m.addrCnt++
return m.addr
}
type mockIoDialer struct {
init func(conn *mockIoConn)
conn *mockIoConn
}
func newMockIoConn() *mockIoConn {
conn := new(mockIoConn)
conn.readWg.Add(1)
conn.writeWg.Add(1)
return conn
}
func (m *mockIoDialer) Dial(ctx context.Context, opts tarantool.DialOpts) (tarantool.Conn, error) {
m.conn = newMockIoConn()
if m.init != nil {
m.init(m.conn)
}
return m.conn, nil
}
func dialIo(t *testing.T,
init func(conn *mockIoConn)) (*tarantool.Connection, mockIoDialer) {
t.Helper()
dialer := mockIoDialer{
init: init,
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := tarantool.Connect(ctx, &dialer,
tarantool.Opts{
Timeout: 1000 * time.Second, // Avoid pings.
SkipSchema: true,
})
require.Nil(t, err)
require.NotNil(t, conn)
return conn, dialer
}
func TestConn_Close(t *testing.T) {
conn, dialer := dialIo(t, nil)
conn.Close()
assert.Equal(t, 1, dialer.conn.closeCnt)
dialer.conn.readWg.Done()
dialer.conn.writeWg.Done()
}
type stubAddr struct {
str string
}
func (a stubAddr) String() string {
return a.str
}
func (a stubAddr) Network() string {
return "stub"
}
func TestConn_Addr(t *testing.T) {
const addr = "any"
conn, dialer := dialIo(t, func(conn *mockIoConn) {
conn.addr = stubAddr{str: addr}
})
defer func() {
dialer.conn.readWg.Done()
dialer.conn.writeWg.Done()
conn.Close()
}()
assert.Equal(t, addr, conn.Addr().String())
assert.Equal(t, 1, dialer.conn.addrCnt)
}
func TestConn_Greeting(t *testing.T) {
greeting := tarantool.Greeting{
Version: "any",
Salt: "salt",
}
conn, dialer := dialIo(t, func(conn *mockIoConn) {
conn.greeting = greeting
})
defer func() {
dialer.conn.readWg.Done()
dialer.conn.writeWg.Done()
conn.Close()
}()
assert.Equal(t, &greeting, conn.Greeting)
assert.Equal(t, 1, dialer.conn.greetingCnt)
}
func TestConn_ProtocolInfo(t *testing.T) {
info := tarantool.ProtocolInfo{
Auth: tarantool.ChapSha1Auth,
Version: 33,
Features: []iproto.Feature{
iproto.IPROTO_FEATURE_ERROR_EXTENSION,
},
}
conn, dialer := dialIo(t, func(conn *mockIoConn) {
conn.info = info
})
defer func() {
dialer.conn.readWg.Done()
dialer.conn.writeWg.Done()
conn.Close()
}()
assert.Equal(t, info, conn.ProtocolInfo())
assert.Equal(t, 1, dialer.conn.infoCnt)
}
func TestConn_ReadWrite(t *testing.T) {
conn, dialer := dialIo(t, func(conn *mockIoConn) {
conn.read = make(chan struct{})
conn.written = make(chan struct{})
conn.writeWgDelay = 1
conn.readbuf.Write([]byte{
0xce, 0x00, 0x00, 0x00, 0x0a, // Length.
0x82, // Header map.
0x00, 0x00,
0x01, 0xce, 0x00, 0x00, 0x00, 0x02,
0x80, // Body map.
})
})
defer func() {
dialer.conn.writeWg.Done()
conn.Close()
}()
fut := conn.Do(tarantool.NewPingRequest())
<-dialer.conn.written
dialer.conn.written = nil
dialer.conn.readWg.Done()
<-dialer.conn.read
<-dialer.conn.read
assert.Equal(t, []byte{
0xce, 0x00, 0x00, 0x00, 0xa, // Length.
0x82, // Header map.
0x00, 0x40,
0x01, 0xce, 0x00, 0x00, 0x00, 0x02,
0x80, // Empty map.
}, dialer.conn.writebuf.Bytes())
_, err := fut.Get()
assert.Nil(t, err)
}
func TestConn_ContextCancel(t *testing.T) {
dialer := tarantool.NetDialer{Address: "127.0.0.1:8080"}
ctx, cancel := context.WithCancel(context.Background())
cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
assert.Nil(t, conn)
assert.NotNil(t, err)
assert.Truef(t, strings.Contains(err.Error(), "operation was canceled"),
fmt.Sprintf("unexpected error, expected to contain %s, got %v",
"operation was canceled", err))
}
func genSalt() [64]byte {
salt := [64]byte{}
for i := 0; i < 44; i++ {
salt[i] = 'a'
}
return salt
}
var (
testDialUser = "test"
testDialPass = "test"
testDialVersion = [64]byte{'t', 'e', 's', 't'}
// Salt with end zeros.
testDialSalt = genSalt()
idRequestExpected = []byte{
0xce, 0x00, 0x00, 0x00, 29, // Length.
0x82, // Header map.
0x00, 0x49,
0x01, 0xce, 0x00, 0x00, 0x00, 0x00,
0x82, // Data map.
0x54,
0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, // Version.
0x55,
0x97, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // Features.
}
idResponseTyped = tarantool.ProtocolInfo{
Version: 6,
Features: []iproto.Feature{iproto.Feature(1), iproto.Feature(21)},
Auth: tarantool.ChapSha1Auth,
}
idResponse = []byte{
0xce, 0x00, 0x00, 0x00, 37, // Length.
0x83, // Header map.
0x00, 0xce, 0x00, 0x00, 0x00, 0x00,
0x01, 0xce, 0x00, 0x00, 0x00, 0x00,
0x05, 0xce, 0x00, 0x00, 0x00, 0x61,
0x83, // Data map.
0x54,
0x06, // Version.
0x55,
0x92, 0x01, 0x15, // Features.
0x5b,
0xa9, 'c', 'h', 'a', 'p', '-', 's', 'h', 'a', '1',
}
idResponseNotSupported = []byte{
0xce, 0x00, 0x00, 0x00, 25, // Length.
0x83, // Header map.
0x00, 0xce, 0x00, 0x00, 0x80, 0x30,
0x01, 0xce, 0x00, 0x00, 0x00, 0x00,
0x05, 0xce, 0x00, 0x00, 0x00, 0x61,
0x81,
0x31,
0xa3, 'e', 'r', 'r',
}
authRequestExpectedChapSha1 = []byte{
0xce, 0x00, 0x00, 0x00, 57, // Length.
0x82, // Header map.
0x00, 0x07,
0x01, 0xce, 0x00, 0x00, 0x00, 0x00,
0x82, // Data map.
0xce, 0x00, 0x00, 0x00, 0x23,
0xa4, 't', 'e', 's', 't', // Login.
0xce, 0x00, 0x00, 0x00, 0x21,
0x92, // Tuple.
0xa9, 'c', 'h', 'a', 'p', '-', 's', 'h', 'a', '1',
// Scramble.
0xb4, 0x1b, 0xd4, 0x20, 0x45, 0x73, 0x22,
0xcf, 0xab, 0x05, 0x03, 0xf3, 0x89, 0x4b,
0xfe, 0xc7, 0x24, 0x5a, 0xe6, 0xe8, 0x31,
}
authRequestExpectedPapSha256 = []byte{
0xce, 0x00, 0x00, 0x00, 0x2a, // Length.
0x82, // Header map.
0x00, 0x07,
0x01, 0xce, 0x00, 0x00, 0x00, 0x00,
0x82, // Data map.
0xce, 0x00, 0x00, 0x00, 0x23,
0xa4, 't', 'e', 's', 't', // Login.
0xce, 0x00, 0x00, 0x00, 0x21,
0x92, // Tuple.
0xaa, 'p', 'a', 'p', '-', 's', 'h', 'a', '2', '5', '6',
0xa4, 't', 'e', 's', 't',
}
okResponse = []byte{
0xce, 0x00, 0x00, 0x00, 19, // Length.
0x83, // Header map.
0x00, 0xce, 0x00, 0x00, 0x00, 0x00,
0x01, 0xce, 0x00, 0x00, 0x00, 0x00,
0x05, 0xce, 0x00, 0x00, 0x00, 0x61,
}
errResponse = []byte{0xce}
)
type testDialOpts struct {
name string
wantErr bool
expectedErr string
expectedProtocolInfo tarantool.ProtocolInfo
// These options configure the behavior of the server.
isErrGreeting bool
isErrId bool
isIdUnsupported bool
isErrAuth bool
isEmptyAuth bool
}
type dialServerActual struct {
IdRequest []byte
AuthRequest []byte
}
func testDialAccept(opts testDialOpts, l net.Listener) chan dialServerActual {
ch := make(chan dialServerActual, 1)
go func() {
client, err := l.Accept()
if err != nil {
return
}
defer client.Close()
if opts.isErrGreeting {
client.Write(errResponse)
return
} else {
// Write greeting.
client.Write(testDialVersion[:])
client.Write(testDialSalt[:])
}
// Read Id request.
idRequestActual := make([]byte, len(idRequestExpected))
client.Read(idRequestActual)
// Make Id response.
if opts.isErrId {
client.Write(errResponse)
} else if opts.isIdUnsupported {
client.Write(idResponseNotSupported)
} else {
client.Write(idResponse)
}
// Read Auth request.
authRequestExpected := authRequestExpectedChapSha1
if opts.isEmptyAuth {
authRequestExpected = []byte{}
}
authRequestActual := make([]byte, len(authRequestExpected))
client.Read(authRequestActual)
// Make Auth response.
if opts.isErrAuth {
client.Write(errResponse)
} else {
client.Write(okResponse)
}
ch <- dialServerActual{
IdRequest: idRequestActual,
AuthRequest: authRequestActual,
}
}()
return ch
}
func testDialer(t *testing.T, l net.Listener, dialer tarantool.Dialer,
opts testDialOpts) {
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
ch := testDialAccept(opts, l)
conn, err := dialer.Dial(ctx, tarantool.DialOpts{
IoTimeout: time.Second * 2,
})
if opts.wantErr {
require.Error(t, err)
require.Contains(t, err.Error(), opts.expectedErr)
return
}
require.NoError(t, err)
require.Equal(t, opts.expectedProtocolInfo, conn.ProtocolInfo())
require.Equal(t, testDialVersion[:], []byte(conn.Greeting().Version))
require.Equal(t, testDialSalt[:44], []byte(conn.Greeting().Salt))
actual := <-ch
require.Equal(t, idRequestExpected, actual.IdRequest)
authRequestExpected := authRequestExpectedChapSha1
if opts.isEmptyAuth {
authRequestExpected = []byte{}
}
require.Equal(t, authRequestExpected, actual.AuthRequest)
conn.Close()
}
func TestNetDialer_Dial(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer l.Close()
dialer := tarantool.NetDialer{
Address: l.Addr().String(),
User: testDialUser,
Password: testDialPass,
}
cases := []testDialOpts{
{
name: "all is ok",
expectedProtocolInfo: idResponseTyped.Clone(),
},
{
name: "id request unsupported",
expectedProtocolInfo: tarantool.ProtocolInfo{},
isIdUnsupported: true,
},
{
name: "greeting response error",
wantErr: true,
expectedErr: "failed to read greeting",
isErrGreeting: true,
},
{
name: "id response error",
wantErr: true,
expectedErr: "failed to identify",
isErrId: true,
},
{
name: "auth response error",
wantErr: true,
expectedErr: "failed to authenticate",
isErrAuth: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
testDialer(t, l, dialer, tc)
})
}
}
func TestNetDialer_Dial_requirements(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer l.Close()
dialer := tarantool.NetDialer{
Address: l.Addr().String(),
User: testDialUser,
Password: testDialPass,
RequiredProtocolInfo: tarantool.ProtocolInfo{
Features: []iproto.Feature{42},
},
}
testDialAccept(testDialOpts{}, l)
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
if err == nil {
conn.Close()
}
require.Error(t, err)
require.Contains(t, err.Error(), "invalid server protocol")
}
func TestFdDialer_Dial(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer l.Close()
addr := l.Addr().String()
cases := []testDialOpts{
{
name: "all is ok",
expectedProtocolInfo: idResponseTyped.Clone(),
isEmptyAuth: true,
},
{
name: "id request unsupported",
expectedProtocolInfo: tarantool.ProtocolInfo{},
isIdUnsupported: true,
isEmptyAuth: true,
},
{
name: "greeting response error",
wantErr: true,
expectedErr: "failed to read greeting",
isErrGreeting: true,
},
{
name: "id response error",
wantErr: true,
expectedErr: "failed to identify",
isErrId: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
sock, err := net.Dial("tcp", addr)
require.NoError(t, err)
f, err := sock.(*net.TCPConn).File()
require.NoError(t, err)
dialer := tarantool.FdDialer{
Fd: f.Fd(),
}
testDialer(t, l, dialer, tc)
})
}
}
func TestFdDialer_Dial_requirements(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer l.Close()
addr := l.Addr().String()
sock, err := net.Dial("tcp", addr)
require.NoError(t, err)
f, err := sock.(*net.TCPConn).File()
require.NoError(t, err)
dialer := tarantool.FdDialer{
Fd: f.Fd(),
RequiredProtocolInfo: tarantool.ProtocolInfo{
Features: []iproto.Feature{42},
},
}
testDialAccept(testDialOpts{}, l)
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
if err == nil {
conn.Close()
}
require.Error(t, err)
require.Contains(t, err.Error(), "invalid server protocol")
}
func TestAuthDialer_Dial_DialerError(t *testing.T) {
dialer := tarantool.AuthDialer{
Dialer: mockErrorDialer{
err: fmt.Errorf("some error"),
},
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
if conn != nil {
conn.Close()
}
assert.NotNil(t, err)
assert.EqualError(t, err, "some error")
}
func TestAuthDialer_Dial_NoSalt(t *testing.T) {
dialer := tarantool.AuthDialer{
Dialer: &mockIoDialer{
init: func(conn *mockIoConn) {
conn.greeting = tarantool.Greeting{
Salt: "",
}
},
},
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
assert.NotNil(t, err)
assert.ErrorContains(t, err, "an invalid connection without salt")
if conn != nil {
conn.Close()
t.Errorf("connection is not nil")
}
}
func TestAuthDialer_Dial(t *testing.T) {
salt := fmt.Sprintf("%s", testDialSalt)
salt = base64.StdEncoding.EncodeToString([]byte(salt))
dialer := mockIoDialer{
init: func(conn *mockIoConn) {
conn.greeting.Salt = salt
conn.writeWgDelay = 1
conn.readWgDelay = 2
conn.readbuf.Write(okResponse)
},
}
defer func() {
dialer.conn.writeWg.Done()
}()
authDialer := tarantool.AuthDialer{
Dialer: &dialer,
Username: "test",
Password: "test",
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := authDialer.Dial(ctx, tarantool.DialOpts{})
if conn != nil {
conn.Close()
}
assert.NoError(t, err)
assert.NotNil(t, conn)
assert.Equal(t, authRequestExpectedChapSha1[:41], dialer.conn.writebuf.Bytes()[:41])
}
func TestAuthDialer_Dial_PapSha256Auth(t *testing.T) {
salt := fmt.Sprintf("%s", testDialSalt)
salt = base64.StdEncoding.EncodeToString([]byte(salt))
dialer := mockIoDialer{
init: func(conn *mockIoConn) {
conn.greeting.Salt = salt
conn.writeWgDelay = 1
conn.readWgDelay = 2
conn.readbuf.Write(okResponse)
},
}
defer func() {
dialer.conn.writeWg.Done()
}()
authDialer := tarantool.AuthDialer{
Dialer: &dialer,
Username: "test",
Password: "test",
Auth: tarantool.PapSha256Auth,
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := authDialer.Dial(ctx, tarantool.DialOpts{})
if conn != nil {
conn.Close()
}
assert.NoError(t, err)
assert.NotNil(t, conn)
assert.Equal(t, authRequestExpectedPapSha256[:41], dialer.conn.writebuf.Bytes()[:41])
}
func TestProtocolDialer_Dial_DialerError(t *testing.T) {
dialer := tarantool.ProtocolDialer{
Dialer: mockErrorDialer{
err: fmt.Errorf("some error"),
},
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
if conn != nil {
conn.Close()
}
assert.NotNil(t, err)
assert.EqualError(t, err, "some error")
}
func TestProtocolDialer_Dial_IdentifyFailed(t *testing.T) {
dialer := tarantool.ProtocolDialer{
Dialer: &mockIoDialer{
init: func(conn *mockIoConn) {
conn.info = tarantool.ProtocolInfo{Version: 1}
conn.writeWgDelay = 1
conn.readWgDelay = 2
conn.readbuf.Write(errResponse)
},
},
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
assert.NotNil(t, err)
assert.ErrorContains(t, err, "failed to identify")
if conn != nil {
conn.Close()
t.Errorf("connection is not nil")
}
}
func TestProtocolDialer_Dial_WrongInfo(t *testing.T) {
dialer := tarantool.ProtocolDialer{
Dialer: &mockIoDialer{
init: func(conn *mockIoConn) {
conn.info = tarantool.ProtocolInfo{Version: 1}
conn.writeWgDelay = 1
conn.readWgDelay = 2
conn.readbuf.Write(idResponse)
},
},
RequiredProtocolInfo: validProtocolInfo,
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
assert.NotNil(t, err)
assert.ErrorContains(t, err, "invalid server protocol")
if conn != nil {
conn.Close()
t.Errorf("connection is not nil")
}
}
func TestProtocolDialer_Dial(t *testing.T) {
protoInfo := tarantool.ProtocolInfo{
Auth: tarantool.ChapSha1Auth,
Version: 6,
Features: []iproto.Feature{0x01, 0x15},
}
dialer := tarantool.ProtocolDialer{
Dialer: &mockIoDialer{
init: func(conn *mockIoConn) {
conn.info = tarantool.ProtocolInfo{Version: 1}
conn.writeWgDelay = 1
conn.readWgDelay = 2
conn.readbuf.Write(idResponse)
},
},
RequiredProtocolInfo: protoInfo,
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
if conn != nil {
conn.Close()
}
assert.NoError(t, err)
assert.NotNil(t, conn)
assert.Equal(t, protoInfo, conn.ProtocolInfo())
}
func TestGreetingDialer_Dial_DialerError(t *testing.T) {
dialer := tarantool.GreetingDialer{
Dialer: mockErrorDialer{
err: fmt.Errorf("some error"),
},
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
if conn != nil {
conn.Close()
}
assert.NotNil(t, err)
assert.EqualError(t, err, "some error")
}
func TestGreetingDialer_Dial_GreetingFailed(t *testing.T) {
dialer := tarantool.GreetingDialer{
Dialer: &mockIoDialer{
init: func(conn *mockIoConn) {
conn.writeWgDelay = 1
conn.readWgDelay = 2
conn.readbuf.Write(errResponse)
},
},
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
assert.NotNil(t, err)
assert.ErrorContains(t, err, "failed to read greeting")
if conn != nil {
conn.Close()
t.Errorf("connection is not nil")
}
}
func TestGreetingDialer_Dial(t *testing.T) {
dialer := tarantool.GreetingDialer{
Dialer: &mockIoDialer{
init: func(conn *mockIoConn) {
conn.info = tarantool.ProtocolInfo{Version: 1}
conn.writeWgDelay = 1
conn.readWgDelay = 3
conn.readbuf.Write(append(testDialVersion[:], testDialSalt[:]...))
conn.readbuf.Write(idResponse)
},
},
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := dialer.Dial(ctx, tarantool.DialOpts{})
if conn != nil {
conn.Close()
}
assert.NoError(t, err)
assert.NotNil(t, conn)
assert.Equal(t, string(testDialVersion[:]), conn.Greeting().Version)
assert.Equal(t, string(testDialSalt[:44]), conn.Greeting().Salt)
}