Unverified Commit b80b91c8 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #258 from libp2p/fix-connection-gating-tests

run connection gating tests on both TCP and QUIC
parents a73c29ad 093e0404
...@@ -292,6 +292,7 @@ func TestConnectionGating(t *testing.T) { ...@@ -292,6 +292,7 @@ func TestConnectionGating(t *testing.T) {
p1ConnectednessToP2 network.Connectedness p1ConnectednessToP2 network.Connectedness
p2ConnectednessToP1 network.Connectedness p2ConnectednessToP1 network.Connectedness
isP1OutboundErr bool isP1OutboundErr bool
disableOnQUIC bool
}{ }{
"no gating": { "no gating": {
p1ConnectednessToP2: network.Connected, p1ConnectednessToP2: network.Connected,
...@@ -324,6 +325,8 @@ func TestConnectionGating(t *testing.T) { ...@@ -324,6 +325,8 @@ func TestConnectionGating(t *testing.T) {
p1ConnectednessToP2: network.NotConnected, p1ConnectednessToP2: network.NotConnected,
p2ConnectednessToP1: network.NotConnected, p2ConnectednessToP1: network.NotConnected,
isP1OutboundErr: true, isP1OutboundErr: true,
// QUIC gates the connection after completion of the handshake
disableOnQUIC: true,
}, },
"p2 gates inbound peer dial before multiplexing": { "p2 gates inbound peer dial before multiplexing": {
p1Gater: func(c *MockConnectionGater) *MockConnectionGater { p1Gater: func(c *MockConnectionGater) *MockConnectionGater {
...@@ -355,33 +358,43 @@ func TestConnectionGating(t *testing.T) { ...@@ -355,33 +358,43 @@ func TestConnectionGating(t *testing.T) {
} }
for n, tc := range tcs { for n, tc := range tcs {
t.Run(n, func(t *testing.T) { for _, useQuic := range []bool{false, true} {
p1Gater := DefaultMockConnectionGater() trString := "TCP"
p2Gater := DefaultMockConnectionGater() optTransport := OptDisableQUIC
if tc.p1Gater != nil { if useQuic {
p1Gater = tc.p1Gater(p1Gater) if tc.disableOnQUIC {
} continue
if tc.p2Gater != nil { }
p2Gater = tc.p2Gater(p2Gater) trString = "QUIC"
optTransport = OptDisableTCP
} }
t.Run(fmt.Sprintf("%s %s", n, trString), func(t *testing.T) {
p1Gater := DefaultMockConnectionGater()
p2Gater := DefaultMockConnectionGater()
if tc.p1Gater != nil {
p1Gater = tc.p1Gater(p1Gater)
}
if tc.p2Gater != nil {
p2Gater = tc.p2Gater(p2Gater)
}
sw1 := GenSwarm(t, ctx, OptConnGater(p1Gater)) sw1 := GenSwarm(t, ctx, OptConnGater(p1Gater), optTransport)
sw2 := GenSwarm(t, ctx, OptConnGater(p2Gater)) sw2 := GenSwarm(t, ctx, OptConnGater(p2Gater), optTransport)
p1 := sw1.LocalPeer()
p2 := sw2.LocalPeer()
sw1.Peerstore().AddAddr(p2, sw2.ListenAddresses()[0], peerstore.PermanentAddrTTL)
// 1 -> 2
_, err := sw1.DialPeer(ctx, p2)
require.Equal(t, tc.isP1OutboundErr, err != nil, n) p1 := sw1.LocalPeer()
require.Equal(t, tc.p1ConnectednessToP2, sw1.Connectedness(p2), n) p2 := sw2.LocalPeer()
sw1.Peerstore().AddAddr(p2, sw2.ListenAddresses()[0], peerstore.PermanentAddrTTL)
// 1 -> 2
_, err := sw1.DialPeer(ctx, p2)
require.Eventually(t, func() bool { require.Equal(t, tc.isP1OutboundErr, err != nil, n)
return tc.p2ConnectednessToP1 == sw2.Connectedness(p1) require.Equal(t, tc.p1ConnectednessToP2, sw1.Connectedness(p2), n)
}, 2*time.Second, 100*time.Millisecond, n)
})
require.Eventually(t, func() bool {
return tc.p2ConnectednessToP1 == sw2.Connectedness(p1)
}, 2*time.Second, 100*time.Millisecond, n)
})
}
} }
} }
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"context" "context"
"testing" "testing"
csms "github.com/libp2p/go-conn-security-multistream"
"github.com/libp2p/go-libp2p-core/connmgr" "github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/control" "github.com/libp2p/go-libp2p-core/control"
"github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/crypto"
...@@ -12,10 +13,8 @@ import ( ...@@ -12,10 +13,8 @@ import (
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore" "github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/sec/insecure" "github.com/libp2p/go-libp2p-core/sec/insecure"
"github.com/libp2p/go-libp2p-peerstore/pstoremem"
quic "github.com/libp2p/go-libp2p-quic-transport" quic "github.com/libp2p/go-libp2p-quic-transport"
csms "github.com/libp2p/go-conn-security-multistream"
pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
swarm "github.com/libp2p/go-libp2p-swarm" swarm "github.com/libp2p/go-libp2p-swarm"
"github.com/libp2p/go-libp2p-testing/net" "github.com/libp2p/go-libp2p-testing/net"
tptu "github.com/libp2p/go-libp2p-transport-upgrader" tptu "github.com/libp2p/go-libp2p-transport-upgrader"
...@@ -23,13 +22,15 @@ import ( ...@@ -23,13 +22,15 @@ import (
msmux "github.com/libp2p/go-stream-muxer-multistream" msmux "github.com/libp2p/go-stream-muxer-multistream"
"github.com/libp2p/go-tcp-transport" "github.com/libp2p/go-tcp-transport"
goprocess "github.com/jbenet/goprocess" "github.com/jbenet/goprocess"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
) )
type config struct { type config struct {
disableReuseport bool disableReuseport bool
dialOnly bool dialOnly bool
disableTCP bool
disableQUIC bool
connectionGater connmgr.ConnectionGater connectionGater connmgr.ConnectionGater
sk crypto.PrivKey sk crypto.PrivKey
} }
...@@ -47,6 +48,16 @@ var OptDialOnly Option = func(_ *testing.T, c *config) { ...@@ -47,6 +48,16 @@ var OptDialOnly Option = func(_ *testing.T, c *config) {
c.dialOnly = true c.dialOnly = true
} }
// OptDisableTCP disables TCP.
var OptDisableTCP Option = func(_ *testing.T, c *config) {
c.disableTCP = true
}
// OptDisableQUIC disables QUIC.
var OptDisableQUIC Option = func(_ *testing.T, c *config) {
c.disableQUIC = true
}
// OptConnGater configures the given connection gater on the test // OptConnGater configures the given connection gater on the test
func OptConnGater(cg connmgr.ConnectionGater) Option { func OptConnGater(cg connmgr.ConnectionGater) Option {
return func(_ *testing.T, c *config) { return func(_ *testing.T, c *config) {
...@@ -111,33 +122,36 @@ func GenSwarm(t *testing.T, ctx context.Context, opts ...Option) *swarm.Swarm { ...@@ -111,33 +122,36 @@ func GenSwarm(t *testing.T, ctx context.Context, opts ...Option) *swarm.Swarm {
upgrader := GenUpgrader(s) upgrader := GenUpgrader(s)
upgrader.ConnGater = cfg.connectionGater upgrader.ConnGater = cfg.connectionGater
tcpTransport := tcp.NewTCPTransport(upgrader)
tcpTransport.DisableReuseport = cfg.disableReuseport
quicTransport, err := quic.NewTransport(p.PrivKey, nil, nil)
if err != nil {
t.Fatal(err)
}
if err := s.AddTransport(tcpTransport); err != nil {
t.Fatal(err)
}
if err := s.AddTransport(quicTransport); err != nil { if !cfg.disableTCP {
t.Fatal(err) tcpTransport := tcp.NewTCPTransport(upgrader)
tcpTransport.DisableReuseport = cfg.disableReuseport
if err := s.AddTransport(tcpTransport); err != nil {
t.Fatal(err)
}
if !cfg.dialOnly {
if err := s.Listen(p.Addr); err != nil {
t.Fatal(err)
}
}
} }
if !cfg.disableQUIC {
if !cfg.dialOnly { quicTransport, err := quic.NewTransport(p.PrivKey, nil, cfg.connectionGater)
if err := s.Listen(p.Addr); err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := s.Listen(ma.StringCast("/ip4/127.0.0.1/udp/0/quic")); err != nil { if err := s.AddTransport(quicTransport); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !cfg.dialOnly {
if err := s.Listen(ma.StringCast("/ip4/127.0.0.1/udp/0/quic")); err != nil {
t.Fatal(err)
}
}
}
if !cfg.dialOnly {
s.Peerstore().AddAddrs(p.ID, s.ListenAddresses(), peerstore.PermanentAddrTTL) s.Peerstore().AddAddrs(p.ID, s.ListenAddresses(), peerstore.PermanentAddrTTL)
} }
return s return s
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment