Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swarm_test: support more transports for GenSwarm #3130

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions p2p/net/swarm/swarm_notif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
. "github.com/libp2p/go-libp2p/p2p/net/swarm"

ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -93,11 +94,29 @@ func TestNotifications(t *testing.T) {
}
}

normalizeAddrs := func(a ma.Multiaddr, isLocal bool) ma.Multiaddr {
// remove certhashes
x, _ := ma.SplitFunc(a, func(c ma.Component) bool {
return c.Protocol().Code == ma.P_CERTHASH
})
// on local addrs, replace 0.0.0.0 with 127.0.0.1
if isLocal {
if manet.IsIPUnspecified(x) {
ip, rest := ma.SplitFirst(x)
if ip.Protocol().Code == ma.P_IP4 {
return ma.StringCast("/ip4/127.0.0.1").Encapsulate(rest)
} else {
return ma.StringCast("/ip6/::1").Encapsulate(rest)
}
}
}
return x
}
complement := func(c network.Conn) (*Swarm, *netNotifiee, *Conn) {
for i, s := range swarms {
for _, c2 := range s.Conns() {
if c.LocalMultiaddr().Equal(c2.RemoteMultiaddr()) &&
c2.LocalMultiaddr().Equal(c.RemoteMultiaddr()) {
if normalizeAddrs(c.LocalMultiaddr(), true).Equal(normalizeAddrs(c2.RemoteMultiaddr(), false)) &&
normalizeAddrs(c2.LocalMultiaddr(), true).Equal(normalizeAddrs(c.RemoteMultiaddr(), false)) {
return s, notifiees[i], c2.(*Conn)
}
}
Expand Down
7 changes: 3 additions & 4 deletions p2p/net/swarm/swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ func makeSwarms(t *testing.T, num int, opts ...Option) []*swarm.Swarm {

func connectSwarms(t *testing.T, ctx context.Context, swarms []*swarm.Swarm) {
var wg sync.WaitGroup
connect := func(s *swarm.Swarm, dst peer.ID, addr ma.Multiaddr) {
// TODO: make a DialAddr func.
s.Peerstore().AddAddr(dst, addr, peerstore.PermanentAddrTTL)
connect := func(s *swarm.Swarm, dst peer.ID, addrs []ma.Multiaddr) {
s.Peerstore().AddAddrs(dst, addrs, peerstore.TempAddrTTL)
if _, err := s.DialPeer(ctx, dst); err != nil {
t.Fatal("error swarm dialing to peer", err)
}
Expand All @@ -92,7 +91,7 @@ func connectSwarms(t *testing.T, ctx context.Context, swarms []*swarm.Swarm) {
for i, s1 := range swarms {
for _, s2 := range swarms[i+1:] {
wg.Add(1)
connect(s1, s2.LocalPeer(), s2.ListenAddresses()[0]) // try the first.
connect(s1, s2.LocalPeer(), s2.ListenAddresses())
}
}
wg.Wait()
Expand Down
105 changes: 96 additions & 9 deletions p2p/net/swarm/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testing

import (
"crypto/rand"
"net"
"testing"
"time"

Expand All @@ -24,21 +25,26 @@ import (
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
libp2pwebrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc"
libp2pwebtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"

ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/quic-go/quic-go"
"github.com/stretchr/testify/require"
)

type config struct {
disableReuseport bool
dialOnly bool
disableTCP bool
disableQUIC bool
connectionGater connmgr.ConnectionGater
sk crypto.PrivKey
swarmOpts []swarm.Option
eventBus event.Bus
disableReuseport bool
dialOnly bool
disableTCP bool
disableQUIC bool
disableWebTransport bool
disableWebRTC bool
connectionGater connmgr.ConnectionGater
sk crypto.PrivKey
swarmOpts []swarm.Option
eventBus event.Bus
clock
}

Expand Down Expand Up @@ -88,6 +94,16 @@ var OptDisableQUIC Option = func(_ testing.TB, c *config) {
c.disableQUIC = true
}

// OptDisableWebTransport disables WebTransport.
var OptDisableWebTransport Option = func(_ testing.TB, c *config) {
c.disableWebTransport = true
}

// OptDisableWebRTC disables WebRTC.
var OptDisableWebRTC Option = func(_ testing.TB, c *config) {
c.disableWebRTC = true
}

// OptConnGater configures the given connection gater on the test
func OptConnGater(cg connmgr.ConnectionGater) Option {
return func(_ testing.TB, c *config) {
Expand Down Expand Up @@ -175,8 +191,10 @@ func GenSwarm(t testing.TB, opts ...Option) *swarm.Swarm {
}
}
}
var quicListenAddr ma.Multiaddr
var reuse *quicreuse.ConnManager
if !cfg.disableQUIC {
reuse, err := quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{})
reuse, err = quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{})
if err != nil {
t.Fatal(err)
}
Expand All @@ -191,6 +209,75 @@ func GenSwarm(t testing.TB, opts ...Option) *swarm.Swarm {
if err := s.Listen(ma.StringCast("/ip4/127.0.0.1/udp/0/quic-v1")); err != nil {
t.Fatal(err)
}
for _, a := range s.ListenAddresses() {
if _, err := a.ValueForProtocol(ma.P_QUIC_V1); err == nil {
quicListenAddr = a
break
}
}
}
}
if !cfg.disableWebTransport {
if reuse == nil {
reuse, err = quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{})
if err != nil {
t.Fatal(err)
}
}
wtTransport, err := libp2pwebtransport.New(priv, nil, reuse, cfg.connectionGater, nil)
if err != nil {
t.Fatal(err)
}
if err := s.AddTransport(wtTransport); err != nil {
t.Fatal(err)
}
if !cfg.dialOnly {
listenAddr := ma.StringCast("/ip4/127.0.0.1/udp/0/quic-v1/webtransport")
if quicListenAddr != nil {
listenAddr = quicListenAddr.Encapsulate(ma.StringCast("/webtransport"))
}
if err := s.Listen(listenAddr); err != nil {
t.Fatal(err)
}
}
}

if !cfg.disableWebRTC {
listenUDPFn := func(network string, laddr *net.UDPAddr) (net.PacketConn, error) {
hasQuicAddrPortFor := func(network string, laddr *net.UDPAddr) bool {
quicAddrPorts := map[string]struct{}{}
for _, addr := range s.ListenAddresses() {
if _, err := addr.ValueForProtocol(ma.P_QUIC_V1); err == nil {
netw, addr, err := manet.DialArgs(addr)
if err != nil {
return false
}
quicAddrPorts[netw+"_"+addr] = struct{}{}
}
}
_, ok := quicAddrPorts[network+"_"+laddr.String()]
return ok
}
if hasQuicAddrPortFor(network, laddr) {
return reuse.SharedNonQUICPacketConn(network, laddr)
}
return net.ListenUDP(network, laddr)
}
wrtcTransport, err := libp2pwebrtc.New(priv, nil, cfg.connectionGater, nil, listenUDPFn)
if err != nil {
t.Fatal(err)
}
if err := s.AddTransport(wrtcTransport); err != nil {
t.Fatal(err)
}
if !cfg.dialOnly {
listenAddr := ma.StringCast("/ip4/127.0.0.1/udp/0/webrtc-direct")
if quicListenAddr != nil {
listenAddr = quicListenAddr.Decapsulate(ma.StringCast("/quic-v1")).Encapsulate(ma.StringCast("/webrtc-direct"))
}
if err := s.Listen(listenAddr); err != nil {
t.Fatal(err)
}
}
}
if !cfg.dialOnly {
Expand Down
2 changes: 1 addition & 1 deletion p2p/protocol/autonatv2/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func newAutoNAT(t testing.TB, dialer host.Host, opts ...AutoNATOption) *AutoNAT
t.Helper()
b := eventbus.NewBus()
h := bhost.NewBlankHost(
swarmt.GenSwarm(t, swarmt.EventBus(b)), bhost.WithEventBus(b))
swarmt.GenSwarm(t, swarmt.EventBus(b), swarmt.OptDisableWebTransport, swarmt.OptDisableWebRTC), bhost.WithEventBus(b))
if dialer == nil {
dialer = bhost.NewBlankHost(
swarmt.GenSwarm(t,
Expand Down
Loading