@@ -2,6 +2,7 @@ package testing
22
33import (
44 "crypto/rand"
5+ "net"
56 "testing"
67 "time"
78
@@ -24,21 +25,26 @@ import (
2425 libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
2526 "github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
2627 "github.com/libp2p/go-libp2p/p2p/transport/tcp"
28+ libp2pwebrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc"
29+ libp2pwebtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"
2730
2831 ma "github.com/multiformats/go-multiaddr"
32+ manet "github.com/multiformats/go-multiaddr/net"
2933 "github.com/quic-go/quic-go"
3034 "github.com/stretchr/testify/require"
3135)
3236
3337type config struct {
34- disableReuseport bool
35- dialOnly bool
36- disableTCP bool
37- disableQUIC bool
38- connectionGater connmgr.ConnectionGater
39- sk crypto.PrivKey
40- swarmOpts []swarm.Option
41- eventBus event.Bus
38+ disableReuseport bool
39+ dialOnly bool
40+ disableTCP bool
41+ disableQUIC bool
42+ disableWebTransport bool
43+ disableWebRTC bool
44+ connectionGater connmgr.ConnectionGater
45+ sk crypto.PrivKey
46+ swarmOpts []swarm.Option
47+ eventBus event.Bus
4248 clock
4349}
4450
@@ -88,6 +94,16 @@ var OptDisableQUIC Option = func(_ testing.TB, c *config) {
8894 c .disableQUIC = true
8995}
9096
97+ // OptDisableWebTransport disables WebTransport.
98+ var OptDisableWebTransport Option = func (_ testing.TB , c * config ) {
99+ c .disableWebTransport = true
100+ }
101+
102+ // OptDisableWebRTC disables WebRTC.
103+ var OptDisableWebRTC Option = func (_ testing.TB , c * config ) {
104+ c .disableWebRTC = true
105+ }
106+
91107// OptConnGater configures the given connection gater on the test
92108func OptConnGater (cg connmgr.ConnectionGater ) Option {
93109 return func (_ testing.TB , c * config ) {
@@ -175,8 +191,10 @@ func GenSwarm(t testing.TB, opts ...Option) *swarm.Swarm {
175191 }
176192 }
177193 }
194+ var quicListenAddr ma.Multiaddr
195+ var reuse * quicreuse.ConnManager
178196 if ! cfg .disableQUIC {
179- reuse , err : = quicreuse .NewConnManager (quic.StatelessResetKey {}, quic.TokenGeneratorKey {})
197+ reuse , err = quicreuse .NewConnManager (quic.StatelessResetKey {}, quic.TokenGeneratorKey {})
180198 if err != nil {
181199 t .Fatal (err )
182200 }
@@ -191,6 +209,75 @@ func GenSwarm(t testing.TB, opts ...Option) *swarm.Swarm {
191209 if err := s .Listen (ma .StringCast ("/ip4/127.0.0.1/udp/0/quic-v1" )); err != nil {
192210 t .Fatal (err )
193211 }
212+ for _ , a := range s .ListenAddresses () {
213+ if _ , err := a .ValueForProtocol (ma .P_QUIC_V1 ); err == nil {
214+ quicListenAddr = a
215+ break
216+ }
217+ }
218+ }
219+ }
220+ if ! cfg .disableWebTransport {
221+ if reuse == nil {
222+ reuse , err = quicreuse .NewConnManager (quic.StatelessResetKey {}, quic.TokenGeneratorKey {})
223+ if err != nil {
224+ t .Fatal (err )
225+ }
226+ }
227+ wtTransport , err := libp2pwebtransport .New (priv , nil , reuse , cfg .connectionGater , nil )
228+ if err != nil {
229+ t .Fatal (err )
230+ }
231+ if err := s .AddTransport (wtTransport ); err != nil {
232+ t .Fatal (err )
233+ }
234+ if ! cfg .dialOnly {
235+ listenAddr := ma .StringCast ("/ip4/127.0.0.1/udp/0/quic-v1/webtransport" )
236+ if quicListenAddr != nil {
237+ listenAddr = quicListenAddr .Encapsulate (ma .StringCast ("/webtransport" ))
238+ }
239+ if err := s .Listen (listenAddr ); err != nil {
240+ t .Fatal (err )
241+ }
242+ }
243+ }
244+
245+ if ! cfg .disableWebRTC {
246+ listenUDPFn := func (network string , laddr * net.UDPAddr ) (net.PacketConn , error ) {
247+ hasQuicAddrPortFor := func (network string , laddr * net.UDPAddr ) bool {
248+ quicAddrPorts := map [string ]struct {}{}
249+ for _ , addr := range s .ListenAddresses () {
250+ if _ , err := addr .ValueForProtocol (ma .P_QUIC_V1 ); err == nil {
251+ netw , addr , err := manet .DialArgs (addr )
252+ if err != nil {
253+ return false
254+ }
255+ quicAddrPorts [netw + "_" + addr ] = struct {}{}
256+ }
257+ }
258+ _ , ok := quicAddrPorts [network + "_" + laddr .String ()]
259+ return ok
260+ }
261+ if hasQuicAddrPortFor (network , laddr ) {
262+ return reuse .SharedNonQUICPacketConn (network , laddr )
263+ }
264+ return net .ListenUDP (network , laddr )
265+ }
266+ wrtcTransport , err := libp2pwebrtc .New (priv , nil , cfg .connectionGater , nil , listenUDPFn )
267+ if err != nil {
268+ t .Fatal (err )
269+ }
270+ if err := s .AddTransport (wrtcTransport ); err != nil {
271+ t .Fatal (err )
272+ }
273+ if ! cfg .dialOnly {
274+ listenAddr := ma .StringCast ("/ip4/127.0.0.1/udp/0/webrtc-direct" )
275+ if quicListenAddr != nil {
276+ listenAddr = quicListenAddr .Decapsulate (ma .StringCast ("/quic-v1" )).Encapsulate (ma .StringCast ("/webrtc-direct" ))
277+ }
278+ if err := s .Listen (listenAddr ); err != nil {
279+ t .Fatal (err )
280+ }
194281 }
195282 }
196283 if ! cfg .dialOnly {
0 commit comments