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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion p2p/net/swarm/swarm_dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,5 +426,5 @@ func TestSkipDialingManyDNS(t *testing.T) {

resolved := s.resolveAddrs(context.Background(), peer.AddrInfo{ID: id, Addrs: []ma.Multiaddr{addr}})
require.NoError(t, err)
require.Less(t, len(resolved), 3)
require.Less(t, len(resolved), 3, "got: %v", resolved)
}
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
71 changes: 62 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,25 @@ 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"
"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 +93,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 +190,9 @@ func GenSwarm(t testing.TB, opts ...Option) *swarm.Swarm {
}
}
}
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 @@ -193,6 +209,43 @@ func GenSwarm(t testing.TB, opts ...Option) *swarm.Swarm {
}
}
}
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 {
if err := s.Listen(ma.StringCast("/ip4/127.0.0.1/udp/0/quic-v1/webtransport")); err != nil {
t.Fatal(err)
}
}
}
if !cfg.disableWebRTC {
listenUDPFn := func(network string, laddr *net.UDPAddr) (net.PacketConn, error) {
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 {
if err := s.Listen(ma.StringCast("/ip4/127.0.0.1/udp/0/webrtc-direct")); err != nil {
t.Fatal(err)
}
}
}
if !cfg.dialOnly {
s.Peerstore().AddAddrs(id, s.ListenAddresses(), peerstore.PermanentAddrTTL)
}
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