-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathssl_test.go
934 lines (758 loc) · 23.8 KB
/
ssl_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
// Copyright (C) 2017. See AUTHORS.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this 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 tongsuogo_test
import (
"bytes"
"crypto/rand"
"crypto/tls"
"io"
"net"
"sync"
"testing"
"time"
ts "github.com/tongsuo-project/tongsuo-go-sdk"
"github.com/tongsuo-project/tongsuo-go-sdk/crypto"
"github.com/tongsuo-project/tongsuo-go-sdk/utils"
)
const (
certBytes = `-----BEGIN CERTIFICATE-----
MIIExjCCAy6gAwIBAgIRAMqZUO0eR6sVZ3A8iG8bJK8wDQYJKoZIhvcNAQELBQAw
ezEeMBwGA1UEChMVbWtjZXJ0IGRldmVsb3BtZW50IENBMSgwJgYDVQQLDB90b21z
YXd5ZXJAQi1GRzc5TUw3SC0wNDQ4LmxvY2FsMS8wLQYDVQQDDCZta2NlcnQgdG9t
c2F3eWVyQEItRkc3OU1MN0gtMDQ0OC5sb2NhbDAeFw0yMjA0MDcwMzU5NDVaFw0z
MjA0MDcwMzU5NDVaMHsxHjAcBgNVBAoTFW1rY2VydCBkZXZlbG9wbWVudCBDQTEo
MCYGA1UECwwfdG9tc2F3eWVyQEItRkc3OU1MN0gtMDQ0OC5sb2NhbDEvMC0GA1UE
AwwmbWtjZXJ0IHRvbXNhd3llckBCLUZHNzlNTDdILTA0NDgubG9jYWwwggGiMA0G
CSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQDCiPTlT2P5jzEazzFX2fT7pToolJLp
4yf3EvgJb2qjFumY1fBk2q6MtYfQutx4Xfhfq+obdTqauFAq3aDjLBYY6bFCT064
mrAetUU9nKF6JLjPHBQP8KexnlyrVo4of89qZadpp83KUF2PfejcJBdkFGSJg1qd
0kY+NCxWN8jojTzLZE5hTZVPiNQCgTgEQnFYKZHhMiliRmDULfZYtzuRZ5DtmOM9
7vQEOdPKz3lUYL4mcIWQjcK6FVvrlJCQ+IpoNQT0gO1G9IvSkxW/HnU4o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IDGpE4IWe
TDoukqQ8peoffk6mtiCnph9Cl2uqAgmmX+GyunEMIdF/ySG0CCcfz180GsQCucax
+AxW2R7NJMAHvfeaYoLtSMYEVTS8sSpuIbRTfGuxbmMOD8a03gU6AA==
-----END RSA PRIVATE KEY-----
`
)
// NetPipe creates a TCP connection pipe and returns two connections.
func NetPipe(tb testing.TB) (net.Conn, net.Conn) {
tb.Helper()
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
tb.Fatal(err)
}
defer lis.Close()
// Use Future pattern to create client connection asynchronously
clientFuture := utils.NewFuture()
go func() {
clientFuture.Set(net.Dial(lis.Addr().Network(), lis.Addr().String()))
}()
var (
errs utils.ErrorGroup
conn net.Conn
ok bool
)
serverConn, err := lis.Accept()
errs.Add(err)
clientConn, err := clientFuture.Get()
errs.Add(err)
if clientConn != nil {
conn, ok = clientConn.(net.Conn)
if !ok {
tb.Fatal("clientConn is not a net.Conn")
}
}
err = errs.Finalize()
if err == nil {
return serverConn, conn
}
if serverConn != nil {
err := serverConn.Close()
if err != nil {
tb.Fatal(err)
}
}
if clientConn != nil {
err := conn.Close()
if err != nil {
tb.Fatal(err)
}
}
tb.Fatal(err)
return nil, nil
}
// HandshakingConn interface extends net.Conn interface with Handshake method.
type HandshakingConn interface {
net.Conn
Handshake() error
}
// SimpleConnTest tests simple SSL/TLS connections.
func SimpleConnTest(tb testing.TB, constructor func(
t testing.TB, conn1, conn2 net.Conn) (sslconn1, sslconn2 HandshakingConn),
) {
tb.Helper()
// Create network pipe
serverConn, clientConn := NetPipe(tb)
defer serverConn.Close()
defer clientConn.Close()
data := "first test string\n"
// Create SSL/TLS connections using provided constructor
server, client := constructor(tb, serverConn, clientConn)
defer closeBoth(server, client)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
err := client.Handshake()
if err != nil {
tb.Fatal(err)
}
_, err = io.Copy(client, bytes.NewReader([]byte(data)))
if err != nil {
tb.Fatal(err)
}
err = client.Close()
if err != nil {
tb.Fatal(err)
}
}()
go func() {
defer wg.Done()
defer server.Close()
err := server.Handshake()
if err != nil {
tb.Fatal(err)
}
buf := bytes.NewBuffer(make([]byte, 0, len(data)))
_, err = io.CopyN(buf, server, int64(len(data)))
if err != nil {
tb.Fatal(err)
}
if buf.String() != data {
tb.Fatal("mismatched data")
}
}()
wg.Wait()
}
// closeBoth closes two connections.
func closeBoth(closer1, closer2 io.Closer) {
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
closer1.Close()
}()
go func() {
defer wg.Done()
closer2.Close()
}()
wg.Wait()
}
// ClosingTest tests connection closing scenarios.
func ClosingTest(tb testing.TB, constructor func(
t testing.TB, conn1, conn2 net.Conn) (sslconn1, sslconn2 HandshakingConn),
) {
tb.Helper()
runTest := func(serverWrites bool) {
// Create network pipe
serverConn, clientConn := NetPipe(tb)
defer serverConn.Close()
defer clientConn.Close()
server, client := constructor(tb, serverConn, clientConn)
defer closeBoth(server, client)
// Determine who writes and who reads based on server_writes parameter
var sslconn1, sslconn2 HandshakingConn
if serverWrites {
sslconn1 = server
sslconn2 = client
} else {
sslconn1 = client
sslconn2 = server
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
_, err := sslconn1.Write([]byte("hello"))
if err != nil {
tb.Fatal(err)
}
sslconn1.Close()
}()
go func() {
defer wg.Done()
data, err := io.ReadAll(sslconn2)
if err != nil {
tb.Fatal(err)
}
if !bytes.Equal(data, []byte("hello")) {
tb.Fatal("bytes don't match")
}
}()
wg.Wait()
}
// Test both client writing and server writing scenarios
runTest(false)
runTest(true)
}
// ThroughputBenchmark benchmarks SSL/TLS connection throughput.
func ThroughputBenchmark(b *testing.B, constructor func(
t testing.TB, conn1, conn2 net.Conn) (sslconn1, sslconn2 HandshakingConn),
) {
b.Helper()
// Create network pipe
serverConn, clientConn := NetPipe(b)
defer serverConn.Close()
defer clientConn.Close()
// Create SSL/TLS connections
server, client := constructor(b, serverConn, clientConn)
defer closeBoth(server, client)
// Set benchmark parameters
b.SetBytes(1024)
data := make([]byte, b.N*1024)
_, err := io.ReadFull(rand.Reader, data)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
_, err = io.Copy(client, bytes.NewReader(data))
if err != nil {
b.Error(err)
}
}()
go func() {
defer wg.Done()
buf := &bytes.Buffer{}
_, err = io.CopyN(buf, server, int64(len(data)))
if err != nil {
b.Error(err)
}
if !bytes.Equal(buf.Bytes(), data) {
b.Error("mismatched data")
}
}()
wg.Wait()
b.StopTimer()
}
// StdlibConstructor creates standard library SSL/TLS connections.
func StdlibConstructor(tb testing.TB, serverConn, clientConn net.Conn) (HandshakingConn, HandshakingConn) {
tb.Helper()
cert, err := tls.X509KeyPair([]byte(certBytes), []byte(keyBytes))
if err != nil {
tb.Fatal(err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true,
CipherSuites: []uint16{tls.TLS_RSA_WITH_AES_128_CBC_SHA},
}
server := tls.Server(serverConn, tlsConfig)
client := tls.Client(clientConn, tlsConfig)
return server, client
}
// StdlibTLSv13Constructor creates standard library SSL/TLS connections with TLSv1.3.
func StdlibTLSv13Constructor(tb testing.TB, serverConn, clientConn net.Conn) (HandshakingConn, HandshakingConn) {
tb.Helper()
cert, err := tls.X509KeyPair([]byte(certBytes), []byte(keyBytes))
if err != nil {
tb.Fatal(err)
}
tlsConfig := &tls.Config{}
tlsConfig.Certificates = []tls.Certificate{cert}
tlsConfig.InsecureSkipVerify = true
tlsConfig.MinVersion = tls.VersionTLS13
tlsConfig.MaxVersion = tls.VersionTLS13
server := tls.Server(serverConn, tlsConfig)
client := tls.Client(clientConn, tlsConfig)
return server, client
}
// passThruVerify is used to pass through certificate verification.
func passThruVerify(tb testing.TB) func(bool, *ts.CertificateStoreCtx) bool {
tb.Helper()
check := func(ok bool, store *ts.CertificateStoreCtx) bool {
cert := store.GetCurrentCert()
if cert == nil {
tb.Fatalf("Could not obtain cert from store\n")
}
sn := cert.GetSerialNumberHex()
if len(sn) == 0 {
tb.Fatalf("Could not obtain serial number from cert")
}
return ok
}
return check
}
// OpenSSLConstructor creates OpenSSL SSL/TLS connections.
func OpenSSLConstructor(tb testing.TB, serverConn, clientConn net.Conn) (HandshakingConn, HandshakingConn) {
tb.Helper()
ctx, err := ts.NewCtx()
if err != nil {
tb.Fatal(err)
}
ctx.SetVerify(ts.VerifyNone, passThruVerify(tb))
key, err := crypto.LoadPrivateKeyFromPEM([]byte(keyBytes))
if err != nil {
tb.Fatal(err)
}
err = ctx.UsePrivateKey(key)
if err != nil {
tb.Fatal(err)
}
cert, err := crypto.LoadCertificateFromPEM([]byte(certBytes))
if err != nil {
tb.Fatal(err)
}
err = ctx.UseCertificate(cert)
if err != nil {
tb.Fatal(err)
}
err = ctx.SetCipherList("AES128-SHA")
if err != nil {
tb.Fatal(err)
}
server, err := ts.Server(serverConn, ctx)
if err != nil {
tb.Fatal(err)
}
client, err := ts.Client(clientConn, ctx)
if err != nil {
tb.Fatal(err)
}
return server, client
}
// OpenSSLTLSv3Constructor function is used to create SSL/TLS connections for OpenSSL and TLSv3.
func OpenSSLTLSv3Constructor(tb testing.TB, serverConn, clientConn net.Conn) (HandshakingConn, HandshakingConn) {
tb.Helper()
ctx, err := ts.NewCtxWithVersion(ts.SSLv3)
if err != nil {
tb.Fatal(err)
}
ctx.SetVerify(ts.VerifyNone, passThruVerify(tb))
key, err := crypto.LoadPrivateKeyFromPEM([]byte(keyBytes))
if err != nil {
tb.Fatal(err)
}
err = ctx.UsePrivateKey(key)
if err != nil {
tb.Fatal(err)
}
cert, err := crypto.LoadCertificateFromPEM([]byte(certBytes))
if err != nil {
tb.Fatal(err)
}
err = ctx.UseCertificate(cert)
if err != nil {
tb.Fatal(err)
}
err = ctx.SetCipherList("AES128-SHA")
if err != nil {
tb.Fatal(err)
}
server, err := ts.Server(serverConn, ctx)
if err != nil {
tb.Fatal(err)
}
client, err := ts.Client(clientConn, ctx)
if err != nil {
tb.Fatal(err)
}
return server, client
}
// StdlibOpenSSLConstructor function is used to create SSL/TLS connections for the standard library and OpenSSL.
func StdlibOpenSSLConstructor(tb testing.TB, serverConn, clientConn net.Conn) (HandshakingConn, HandshakingConn) {
tb.Helper()
serverStd, _ := StdlibConstructor(tb, serverConn, clientConn)
_, clientSsl := OpenSSLConstructor(tb, serverConn, clientConn)
return serverStd, clientSsl
}
// OpenSSLStdlibConstructor function is used to create SSL/TLS connections for OpenSSL and the standard library.
func OpenSSLStdlibConstructor(tb testing.TB, serverConn, clientConn net.Conn) (HandshakingConn, HandshakingConn) {
tb.Helper()
_, clientStd := StdlibConstructor(tb, serverConn, clientConn)
serverSsl, _ := OpenSSLConstructor(tb, serverConn, clientConn)
return serverSsl, clientStd
}
// TestStdlibSimple function is used to test simple connections of the standard library.
func TestStdlibSimple(t *testing.T) {
t.Parallel()
SimpleConnTest(t, StdlibConstructor)
}
// TestStdlibTLSv13Simple function is used to test simple connections of the standard library with TLSv1.3.
func TestStdlibTLSv13Simple(t *testing.T) {
t.Parallel()
SimpleConnTest(t, StdlibTLSv13Constructor)
}
// TestOpenSSLSimple function is used to test simple connections of OpenSSL.
func TestOpenSSLSimple(t *testing.T) {
t.Parallel()
SimpleConnTest(t, OpenSSLConstructor)
}
// TestStdlibClosing function is used to test closing connections of the standard library.
func TestStdlibClosing(t *testing.T) {
t.Parallel()
ClosingTest(t, StdlibConstructor)
}
// TestStdlibTLSv13Closing function is used to test closing connections of the standard library with TLSv1.3.
func TestStdlibTLSv13Closing(t *testing.T) {
t.Parallel()
ClosingTest(t, StdlibTLSv13Constructor)
}
func TestOpenSSLClosing(t *testing.T) {
t.Parallel()
ClosingTest(t, OpenSSLConstructor)
}
// BenchmarkStdlibThroughput function is used to benchmark the throughput of the standard library.
func BenchmarkStdlibThroughput(b *testing.B) {
ThroughputBenchmark(b, StdlibConstructor)
}
// BenchmarkStdlibTLSv13Throughput function is used to benchmark the throughput of the standard library with TLSv1.3.
func BenchmarkStdlibTLSv13Throughput(b *testing.B) {
ThroughputBenchmark(b, StdlibTLSv13Constructor)
}
// BenchmarkOpenSSLThroughput function is used to benchmark the throughput of OpenSSL.
func BenchmarkOpenSSLThroughput(b *testing.B) {
ThroughputBenchmark(b, OpenSSLConstructor)
}
// TestStdlibOpenSSLSimple function is used to test simple connections of the standard library and OpenSSL.
func TestStdlibOpenSSLSimple(t *testing.T) {
t.Parallel()
SimpleConnTest(t, StdlibOpenSSLConstructor)
}
// TestOpenSSLStdlibSimple function is used to test simple connections of OpenSSL and the standard library.
func TestOpenSSLStdlibSimple(t *testing.T) {
t.Parallel()
SimpleConnTest(t, OpenSSLStdlibConstructor)
}
// TestStdlibOpenSSLClosing function is used to test closing connections of the standard library and OpenSSL.
func TestStdlibOpenSSLClosing(t *testing.T) {
t.Parallel()
ClosingTest(t, StdlibOpenSSLConstructor)
}
// TestOpenSSLStdlibClosing function is used to test closing connections of OpenSSL and the standard library.
func TestOpenSSLStdlibClosing(t *testing.T) {
t.Parallel()
ClosingTest(t, OpenSSLStdlibConstructor)
}
// BenchmarkStdlibOpenSSLThroughput function is used to benchmark the throughput of the standard library and OpenSSL.
func BenchmarkStdlibOpenSSLThroughput(b *testing.B) {
ThroughputBenchmark(b, StdlibOpenSSLConstructor)
}
// BenchmarkOpenSSLStdlibThroughput function is used to benchmark the throughput of OpenSSL and the standard library.
func BenchmarkOpenSSLStdlibThroughput(b *testing.B) {
ThroughputBenchmark(b, OpenSSLStdlibConstructor)
}
// FullDuplexRenegotiationTest function is used to test full-duplex renegotiation.
func FullDuplexRenegotiationTest(tb testing.TB, constructor func(
t testing.TB, conn1, conn2 net.Conn) (sslconn1, sslconn2 HandshakingConn),
) {
tb.Helper()
SSLRecordSize := 16 * 1024
serverConn, clientConn := NetPipe(tb)
defer serverConn.Close()
defer clientConn.Close()
// Set test parameters
times := 256
dataLen := 4 * SSLRecordSize
data1 := make([]byte, dataLen)
_, err := io.ReadFull(rand.Reader, data1)
if err != nil {
tb.Fatal(err)
}
data2 := make([]byte, dataLen)
_, err = io.ReadFull(rand.Reader, data2)
if err != nil {
tb.Fatal(err)
}
// Create SSL/TLS connections
server, client := constructor(tb, serverConn, clientConn)
defer closeBoth(server, client)
var wg sync.WaitGroup
sendFunc := func(sender HandshakingConn, data []byte) {
defer wg.Done()
for i := 0; i < times; i++ {
if i == times/2 {
wg.Add(1)
go func() {
defer wg.Done()
err := sender.Handshake()
if err != nil {
tb.Fatal(err)
}
}()
}
_, err := sender.Write(data)
if err != nil {
tb.Fatal(err)
}
}
}
recvFunc := func(receiver net.Conn, data []byte) {
defer wg.Done()
buf := make([]byte, len(data))
for i := 0; i < times; i++ {
n, err := io.ReadFull(receiver, buf)
if err != nil {
tb.Fatal(err)
}
if !bytes.Equal(buf[:n], data) {
tb.Fatal(err)
}
}
}
wg.Add(4)
go recvFunc(server, data1)
go sendFunc(client, data1)
go sendFunc(server, data2)
go recvFunc(client, data2)
wg.Wait()
}
// TestStdlibFullDuplexRenegotiation function is used to test full-duplex renegotiation of the standard library.
func TestStdlibFullDuplexRenegotiation(t *testing.T) {
t.Parallel()
FullDuplexRenegotiationTest(t, StdlibConstructor)
}
// TestOpenSSLFullDuplexRenegotiation function is used to test full-duplex renegotiation of OpenSSL.
func TestOpenSSLFullDuplexRenegotiation(t *testing.T) {
t.Parallel()
FullDuplexRenegotiationTest(t, OpenSSLConstructor)
}
// TestOpenSSLStdlibFullDuplexRenegotiation function is used to test full-duplex renegotiation of OpenSSL and the
// standard library.
func TestOpenSSLStdlibFullDuplexRenegotiation(t *testing.T) {
t.Parallel()
FullDuplexRenegotiationTest(t, OpenSSLStdlibConstructor)
}
// TestStdlibOpenSSLFullDuplexRenegotiation function is used to test full-duplex renegotiation of the standard library
// and OpenSSL.
func TestStdlibOpenSSLFullDuplexRenegotiation(t *testing.T) {
t.Parallel()
FullDuplexRenegotiationTest(t, StdlibOpenSSLConstructor)
}
func startTLSServer(t *testing.T, sslListener net.Listener, payloadSize int64, loops int, sleep time.Duration) {
t.Helper()
for {
conn, err := sslListener.Accept()
if err != nil {
t.Error("failed to accept: ", err)
continue
}
go func() {
defer func() {
err = conn.Close()
if err != nil {
t.Error("failed to close: ", err)
}
}()
for i := 0; i < loops; i++ {
_, err := io.Copy(io.Discard, io.LimitReader(conn, payloadSize))
if err != nil {
t.Error("failed to read: ", err)
return
}
_, err = io.Copy(conn, io.LimitReader(rand.Reader, payloadSize))
if err != nil {
t.Error("failed to write: ", err)
return
}
}
time.Sleep(sleep)
}()
}
}
// LotsOfConns function is used to test the situation of a large number of connections.
func LotsOfConns(t *testing.T, payloadSize int64, loops, clients int,
sleep time.Duration, newListener func(net.Listener) net.Listener,
newClient func(net.Conn) (net.Conn, error),
) {
t.Helper()
tcpListener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
sslListener := newListener(tcpListener)
go startTLSServer(t, sslListener, payloadSize, loops, sleep)
// Create multiple client connections
var wg sync.WaitGroup
for i := 0; i < clients; i++ {
tcpClient, err := net.Dial(tcpListener.Addr().Network(),
tcpListener.Addr().String())
if err != nil {
t.Fatal(err)
}
sslClient, err := newClient(tcpClient)
if err != nil {
t.Fatal(err)
}
wg.Add(1)
go func(_ int) {
defer func() {
err = sslClient.Close()
if err != nil {
t.Error("failed to close: ", err)
}
wg.Done()
}()
for i := 0; i < loops; i++ {
// Write and read data
_, err := io.Copy(sslClient, io.LimitReader(rand.Reader,
payloadSize))
if err != nil {
t.Error("failed to write: ", err)
return
}
_, err = io.Copy(io.Discard,
io.LimitReader(sslClient, payloadSize))
if err != nil {
t.Error("failed to read: ", err)
return
}
}
time.Sleep(sleep)
}(i)
}
wg.Wait()
}
// TestStdlibLotsOfConns function is used to test the situation of a large number of connections of the standard
// library.
func TestStdlibLotsOfConns(t *testing.T) {
t.Parallel()
// Load certificate and configure TLS
tlsCert, err := tls.X509KeyPair([]byte(certBytes), []byte(keyBytes))
if err != nil {
t.Fatal(err)
}
tlsConfig := &tls.Config{}
tlsConfig.Certificates = []tls.Certificate{tlsCert}
tlsConfig.InsecureSkipVerify = true
tlsConfig.CipherSuites = []uint16{tls.TLS_RSA_WITH_AES_128_CBC_SHA}
// Execute large number of connections test
LotsOfConns(t, 1024*64, 10, 100, 0*time.Second,
func(l net.Listener) net.Listener {
return tls.NewListener(l, tlsConfig)
}, func(c net.Conn) (net.Conn, error) {
return tls.Client(c, tlsConfig), nil
})
}
// TestStdlibTLSv13LotsOfConns function is used to test the situation of a large number of connections of the standard
// library with TLSv1.3.
func TestStdlibTLSv13LotsOfConns(t *testing.T) {
t.Parallel()
// Load certificate and configure TLS
tlsCert, err := tls.X509KeyPair([]byte(certBytes), []byte(keyBytes))
if err != nil {
t.Fatal(err)
}
tlsConfig := &tls.Config{}
tlsConfig.Certificates = []tls.Certificate{tlsCert}
tlsConfig.InsecureSkipVerify = true
tlsConfig.MinVersion = tls.VersionTLS13
tlsConfig.MaxVersion = tls.VersionTLS13
// Execute large number of connections test
LotsOfConns(t, 1024*64, 10, 100, 0*time.Second,
func(l net.Listener) net.Listener {
return tls.NewListener(l, tlsConfig)
}, func(c net.Conn) (net.Conn, error) {
return tls.Client(c, tlsConfig), nil
})
}
// TestOpenSSLLotsOfConns function is used to test the situation of a large number of connections of OpenSSL.
func TestOpenSSLLotsOfConns(t *testing.T) {
t.Parallel()
// Create SSL context and configure
ctx, err := ts.NewCtx()
if err != nil {
t.Fatal(err)
}
key, err := crypto.LoadPrivateKeyFromPEM([]byte(keyBytes))
if err != nil {
t.Fatal(err)
}
err = ctx.UsePrivateKey(key)
if err != nil {
t.Fatal(err)
}
cert, err := crypto.LoadCertificateFromPEM([]byte(certBytes))
if err != nil {
t.Fatal(err)
}
err = ctx.UseCertificate(cert)
if err != nil {
t.Fatal(err)
}
err = ctx.SetCipherList("AES128-SHA")
if err != nil {
t.Fatal(err)
}
// Execute large number of connections test
LotsOfConns(t, 1024*64, 10, 100, 0*time.Second,
func(l net.Listener) net.Listener {
return ts.NewListener(l, ctx)
}, func(c net.Conn) (net.Conn, error) {
return ts.Client(c, ctx)
})
}