Commit 093e0404 authored by Marten Seemann's avatar Marten Seemann

run connection gating tests on both TCP and QUIC

Also disables one test that doesn't work with QUIC, as we always
complete the handshake before gating there.
parent a73c29ad
...@@ -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,7 +358,17 @@ func TestConnectionGating(t *testing.T) { ...@@ -355,7 +358,17 @@ 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} {
trString := "TCP"
optTransport := OptDisableQUIC
if useQuic {
if tc.disableOnQUIC {
continue
}
trString = "QUIC"
optTransport = OptDisableTCP
}
t.Run(fmt.Sprintf("%s %s", n, trString), func(t *testing.T) {
p1Gater := DefaultMockConnectionGater() p1Gater := DefaultMockConnectionGater()
p2Gater := DefaultMockConnectionGater() p2Gater := DefaultMockConnectionGater()
if tc.p1Gater != nil { if tc.p1Gater != nil {
...@@ -365,8 +378,8 @@ func TestConnectionGating(t *testing.T) { ...@@ -365,8 +378,8 @@ func TestConnectionGating(t *testing.T) {
p2Gater = tc.p2Gater(p2Gater) 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() p1 := sw1.LocalPeer()
p2 := sw2.LocalPeer() p2 := sw2.LocalPeer()
...@@ -381,7 +394,7 @@ func TestConnectionGating(t *testing.T) { ...@@ -381,7 +394,7 @@ func TestConnectionGating(t *testing.T) {
return tc.p2ConnectednessToP1 == sw2.Connectedness(p1) return tc.p2ConnectednessToP1 == sw2.Connectedness(p1)
}, 2*time.Second, 100*time.Millisecond, n) }, 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
if !cfg.disableTCP {
tcpTransport := tcp.NewTCPTransport(upgrader) tcpTransport := tcp.NewTCPTransport(upgrader)
tcpTransport.DisableReuseport = cfg.disableReuseport tcpTransport.DisableReuseport = cfg.disableReuseport
if err := s.AddTransport(tcpTransport); err != nil {
quicTransport, err := quic.NewTransport(p.PrivKey, nil, nil)
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !cfg.dialOnly {
if err := s.AddTransport(tcpTransport); err != nil { if err := s.Listen(p.Addr); err != nil {
t.Fatal(err) t.Fatal(err)
} }
}
if err := s.AddTransport(quicTransport); err != nil { }
if !cfg.disableQUIC {
quicTransport, err := quic.NewTransport(p.PrivKey, nil, cfg.connectionGater)
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := s.AddTransport(quicTransport); err != nil {
if !cfg.dialOnly {
if err := s.Listen(p.Addr); 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 { if err := s.Listen(ma.StringCast("/ip4/127.0.0.1/udp/0/quic")); err != nil {
t.Fatal(err) 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