@@ -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"
@@ -67,6 +75,44 @@ func transformOpts(opts TransportTestCaseOpts) []config.Option {
67
75
return libp2pOpts
68
76
}
69
77
78
+ func selfSignedTLSConfig (t * testing.T ) * tls.Config {
79
+ t .Helper ()
80
+ priv , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
81
+ require .NoError (t , err )
82
+
83
+ notBefore := time .Now ()
84
+ notAfter := notBefore .Add (365 * 24 * time .Hour )
85
+
86
+ serialNumberLimit := new (big.Int ).Lsh (big .NewInt (1 ), 128 )
87
+ serialNumber , err := rand .Int (rand .Reader , serialNumberLimit )
88
+ require .NoError (t , err )
89
+
90
+ certTemplate := x509.Certificate {
91
+ SerialNumber : serialNumber ,
92
+ Subject : pkix.Name {
93
+ Organization : []string {"Test" },
94
+ },
95
+ NotBefore : notBefore ,
96
+ NotAfter : notAfter ,
97
+ KeyUsage : x509 .KeyUsageKeyEncipherment | x509 .KeyUsageDigitalSignature ,
98
+ ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageServerAuth },
99
+ BasicConstraintsValid : true ,
100
+ }
101
+
102
+ derBytes , err := x509 .CreateCertificate (rand .Reader , & certTemplate , & certTemplate , & priv .PublicKey , priv )
103
+ require .NoError (t , err )
104
+
105
+ cert := tls.Certificate {
106
+ Certificate : [][]byte {derBytes },
107
+ PrivateKey : priv ,
108
+ }
109
+
110
+ tlsConfig := & tls.Config {
111
+ Certificates : []tls.Certificate {cert },
112
+ }
113
+ return tlsConfig
114
+ }
115
+
70
116
var transportsToTest = []TransportTestCase {
71
117
{
72
118
Name : "TCP / Noise / Yamux" ,
@@ -88,7 +134,7 @@ var transportsToTest = []TransportTestCase{
88
134
Name : "TCP / TLS / Yamux" ,
89
135
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
90
136
libp2pOpts := transformOpts (opts )
91
- libp2pOpts = append (libp2pOpts , libp2p .Security (tls .ID , tls .New ))
137
+ libp2pOpts = append (libp2pOpts , libp2p .Security (libp2ptls .ID , libp2ptls .New ))
92
138
libp2pOpts = append (libp2pOpts , libp2p .Muxer (yamux .ID , yamux .DefaultTransport ))
93
139
if opts .NoListen {
94
140
libp2pOpts = append (libp2pOpts , libp2p .NoListenAddrs )
@@ -105,7 +151,7 @@ var transportsToTest = []TransportTestCase{
105
151
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
106
152
libp2pOpts := transformOpts (opts )
107
153
libp2pOpts = append (libp2pOpts , libp2p .ShareTCPListener ())
108
- libp2pOpts = append (libp2pOpts , libp2p .Security (tls .ID , tls .New ))
154
+ libp2pOpts = append (libp2pOpts , libp2p .Security (libp2ptls .ID , libp2ptls .New ))
109
155
libp2pOpts = append (libp2pOpts , libp2p .Muxer (yamux .ID , yamux .DefaultTransport ))
110
156
if opts .NoListen {
111
157
libp2pOpts = append (libp2pOpts , libp2p .NoListenAddrs )
@@ -122,7 +168,7 @@ var transportsToTest = []TransportTestCase{
122
168
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
123
169
libp2pOpts := transformOpts (opts )
124
170
libp2pOpts = append (libp2pOpts , libp2p .ShareTCPListener ())
125
- libp2pOpts = append (libp2pOpts , libp2p .Security (tls .ID , tls .New ))
171
+ libp2pOpts = append (libp2pOpts , libp2p .Security (libp2ptls .ID , libp2ptls .New ))
126
172
libp2pOpts = append (libp2pOpts , libp2p .Muxer (yamux .ID , yamux .DefaultTransport ))
127
173
libp2pOpts = append (libp2pOpts , libp2p .Transport (tcp .NewTCPTransport , tcp .WithMetrics ()))
128
174
if opts .NoListen {
@@ -139,7 +185,7 @@ var transportsToTest = []TransportTestCase{
139
185
Name : "TCP-WithMetrics / TLS / Yamux" ,
140
186
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
141
187
libp2pOpts := transformOpts (opts )
142
- libp2pOpts = append (libp2pOpts , libp2p .Security (tls .ID , tls .New ))
188
+ libp2pOpts = append (libp2pOpts , libp2p .Security (libp2ptls .ID , libp2ptls .New ))
143
189
libp2pOpts = append (libp2pOpts , libp2p .Muxer (yamux .ID , yamux .DefaultTransport ))
144
190
libp2pOpts = append (libp2pOpts , libp2p .Transport (tcp .NewTCPTransport , tcp .WithMetrics ()))
145
191
if opts .NoListen {
@@ -167,6 +213,23 @@ var transportsToTest = []TransportTestCase{
167
213
return h
168
214
},
169
215
},
216
+ {
217
+ Name : "WebSocket-Secured-Shared" ,
218
+ HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
219
+ libp2pOpts := transformOpts (opts )
220
+ libp2pOpts = append (libp2pOpts , libp2p .ShareTCPListener ())
221
+ if opts .NoListen {
222
+ config := tls.Config {InsecureSkipVerify : true }
223
+ libp2pOpts = append (libp2pOpts , libp2p .NoListenAddrs , libp2p .Transport (websocket .New , websocket .WithTLSClientConfig (& config )))
224
+ } else {
225
+ config := selfSignedTLSConfig (t )
226
+ libp2pOpts = append (libp2pOpts , libp2p .ListenAddrStrings ("/ip4/127.0.0.1/tcp/0/sni/localhost/tls/ws" ), libp2p .Transport (websocket .New , websocket .WithTLSConfig (config )))
227
+ }
228
+ h , err := libp2p .New (libp2pOpts ... )
229
+ require .NoError (t , err )
230
+ return h
231
+ },
232
+ },
170
233
{
171
234
Name : "WebSocket" ,
172
235
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
@@ -181,6 +244,22 @@ var transportsToTest = []TransportTestCase{
181
244
return h
182
245
},
183
246
},
247
+ {
248
+ Name : "WebSocket-Secured" ,
249
+ HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
250
+ libp2pOpts := transformOpts (opts )
251
+ if opts .NoListen {
252
+ config := tls.Config {InsecureSkipVerify : true }
253
+ libp2pOpts = append (libp2pOpts , libp2p .NoListenAddrs , libp2p .Transport (websocket .New , websocket .WithTLSClientConfig (& config )))
254
+ } else {
255
+ config := selfSignedTLSConfig (t )
256
+ libp2pOpts = append (libp2pOpts , libp2p .ListenAddrStrings ("/ip4/127.0.0.1/tcp/0/sni/localhost/tls/ws" ), libp2p .Transport (websocket .New , websocket .WithTLSConfig (config )))
257
+ }
258
+ h , err := libp2p .New (libp2pOpts ... )
259
+ require .NoError (t , err )
260
+ return h
261
+ },
262
+ },
184
263
{
185
264
Name : "QUIC" ,
186
265
HostGenerator : func (t * testing.T , opts TransportTestCaseOpts ) host.Host {
0 commit comments