@@ -3,10 +3,16 @@ package transport_integration
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "crypto/ecdsa"
7
+ "crypto/elliptic"
6
8
"crypto/rand"
9
+ "crypto/tls"
10
+ "crypto/x509"
11
+ "crypto/x509/pkix"
7
12
"errors"
8
13
"fmt"
9
14
"io"
15
+ "math/big"
10
16
"net"
11
17
"runtime"
12
18
"strings"
@@ -15,6 +21,8 @@ import (
15
21
"testing"
16
22
"time"
17
23
24
+ libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"
25
+
18
26
"github.com/libp2p/go-libp2p"
19
27
"github.com/libp2p/go-libp2p/config"
20
28
"github.com/libp2p/go-libp2p/core/connmgr"
@@ -30,9 +38,9 @@ import (
30
38
"github.com/libp2p/go-libp2p/p2p/net/swarm"
31
39
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
32
40
"github.com/libp2p/go-libp2p/p2p/security/noise"
33
- tls "github.com/libp2p/go-libp2p/p2p/security/tls"
34
41
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
35
42
libp2pwebrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc"
43
+ "github.com/libp2p/go-libp2p/p2p/transport/websocket"
36
44
"go.uber.org/mock/gomock"
37
45
38
46
ma "github.com/multiformats/go-multiaddr"
@@ -68,6 +76,44 @@ func transformOpts(opts TransportTestCaseOpts) []config.Option {
68
76
return libp2pOpts
69
77
}
70
78
79
+ func selfSignedTLSConfig (t * testing.T ) * tls.Config {
80
+ t .Helper ()
81
+ priv , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
82
+ require .NoError (t , err )
83
+
84
+ notBefore := time .Now ()
85
+ notAfter := notBefore .Add (365 * 24 * time .Hour )
86
+
87
+ serialNumberLimit := new (big.Int ).Lsh (big .NewInt (1 ), 128 )
88
+ serialNumber , err := rand .Int (rand .Reader , serialNumberLimit )
89
+ require .NoError (t , err )
90
+
91
+ certTemplate := x509.Certificate {
92
+ SerialNumber : serialNumber ,
93
+ Subject : pkix.Name {
94
+ Organization : []string {"Test" },
95
+ },
96
+ NotBefore : notBefore ,
97
+ NotAfter : notAfter ,
98
+ KeyUsage : x509 .KeyUsageKeyEncipherment | x509 .KeyUsageDigitalSignature ,
99
+ ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageServerAuth },
100
+ BasicConstraintsValid : true ,
101
+ }
102
+
103
+ derBytes , err := x509 .CreateCertificate (rand .Reader , & certTemplate , & certTemplate , & priv .PublicKey , priv )
104
+ require .NoError (t , err )
105
+
106
+ cert := tls.Certificate {
107
+ Certificate : [][]byte {derBytes },
108
+ PrivateKey : priv ,
109
+ }
110
+
111
+ tlsConfig := & tls.Config {
112
+ Certificates : []tls.Certificate {cert },
113
+ }
114
+ return tlsConfig
115
+ }
116
+
71
117
var transportsToTest = []TransportTestCase {
72
118
{
73
119
Name : "TCP / Noise / Yamux" ,
@@ -89,7 +135,7 @@ var transportsToTest = []TransportTestCase{
89
135
Name : "TCP / TLS / Yamux" ,
90
136
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
91
137
libp2pOpts := transformOpts (opts )
92
- libp2pOpts = append (libp2pOpts , libp2p .Security (tls .ID , tls .New ))
138
+ libp2pOpts = append (libp2pOpts , libp2p .Security (libp2ptls .ID , libp2ptls .New ))
93
139
libp2pOpts = append (libp2pOpts , libp2p .Muxer (yamux .ID , yamux .DefaultTransport ))
94
140
if opts .NoListen {
95
141
libp2pOpts = append (libp2pOpts , libp2p .NoListenAddrs )
@@ -106,7 +152,7 @@ var transportsToTest = []TransportTestCase{
106
152
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
107
153
libp2pOpts := transformOpts (opts )
108
154
libp2pOpts = append (libp2pOpts , libp2p .ShareTCPListener ())
109
- libp2pOpts = append (libp2pOpts , libp2p .Security (tls .ID , tls .New ))
155
+ libp2pOpts = append (libp2pOpts , libp2p .Security (libp2ptls .ID , libp2ptls .New ))
110
156
libp2pOpts = append (libp2pOpts , libp2p .Muxer (yamux .ID , yamux .DefaultTransport ))
111
157
if opts .NoListen {
112
158
libp2pOpts = append (libp2pOpts , libp2p .NoListenAddrs )
@@ -123,7 +169,7 @@ var transportsToTest = []TransportTestCase{
123
169
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
124
170
libp2pOpts := transformOpts (opts )
125
171
libp2pOpts = append (libp2pOpts , libp2p .ShareTCPListener ())
126
- libp2pOpts = append (libp2pOpts , libp2p .Security (tls .ID , tls .New ))
172
+ libp2pOpts = append (libp2pOpts , libp2p .Security (libp2ptls .ID , libp2ptls .New ))
127
173
libp2pOpts = append (libp2pOpts , libp2p .Muxer (yamux .ID , yamux .DefaultTransport ))
128
174
libp2pOpts = append (libp2pOpts , libp2p .Transport (tcp .NewTCPTransport , tcp .WithMetrics ()))
129
175
if opts .NoListen {
@@ -140,7 +186,7 @@ var transportsToTest = []TransportTestCase{
140
186
Name : "TCP-WithMetrics / TLS / Yamux" ,
141
187
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
142
188
libp2pOpts := transformOpts (opts )
143
- libp2pOpts = append (libp2pOpts , libp2p .Security (tls .ID , tls .New ))
189
+ libp2pOpts = append (libp2pOpts , libp2p .Security (libp2ptls .ID , libp2ptls .New ))
144
190
libp2pOpts = append (libp2pOpts , libp2p .Muxer (yamux .ID , yamux .DefaultTransport ))
145
191
libp2pOpts = append (libp2pOpts , libp2p .Transport (tcp .NewTCPTransport , tcp .WithMetrics ()))
146
192
if opts .NoListen {
@@ -168,6 +214,23 @@ var transportsToTest = []TransportTestCase{
168
214
return h
169
215
},
170
216
},
217
+ {
218
+ Name : "WebSocket-Secured-Shared" ,
219
+ HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
220
+ libp2pOpts := transformOpts (opts )
221
+ libp2pOpts = append (libp2pOpts , libp2p .ShareTCPListener ())
222
+ if opts .NoListen {
223
+ config := tls.Config {InsecureSkipVerify : true }
224
+ libp2pOpts = append (libp2pOpts , libp2p .NoListenAddrs , libp2p .Transport (websocket .New , websocket .WithTLSClientConfig (& config )))
225
+ } else {
226
+ config := selfSignedTLSConfig (t )
227
+ libp2pOpts = append (libp2pOpts , libp2p .ListenAddrStrings ("/ip4/127.0.0.1/tcp/0/sni/localhost/tls/ws" ), libp2p .Transport (websocket .New , websocket .WithTLSConfig (config )))
228
+ }
229
+ h , err := libp2p .New (libp2pOpts ... )
230
+ require .NoError (t , err )
231
+ return h
232
+ },
233
+ },
171
234
{
172
235
Name : "WebSocket" ,
173
236
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
@@ -182,6 +245,22 @@ var transportsToTest = []TransportTestCase{
182
245
return h
183
246
},
184
247
},
248
+ {
249
+ Name : "WebSocket-Secured" ,
250
+ HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
251
+ libp2pOpts := transformOpts (opts )
252
+ if opts .NoListen {
253
+ config := tls.Config {InsecureSkipVerify : true }
254
+ libp2pOpts = append (libp2pOpts , libp2p .NoListenAddrs , libp2p .Transport (websocket .New , websocket .WithTLSClientConfig (& config )))
255
+ } else {
256
+ config := selfSignedTLSConfig (t )
257
+ libp2pOpts = append (libp2pOpts , libp2p .ListenAddrStrings ("/ip4/127.0.0.1/tcp/0/sni/localhost/tls/ws" ), libp2p .Transport (websocket .New , websocket .WithTLSConfig (config )))
258
+ }
259
+ h , err := libp2p .New (libp2pOpts ... )
260
+ require .NoError (t , err )
261
+ return h
262
+ },
263
+ },
185
264
{
186
265
Name : "QUIC" ,
187
266
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
0 commit comments