testing.go 5.15 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1 2 3 4 5 6
package testing

import (
	"context"
	"testing"

7 8
	"github.com/libp2p/go-libp2p-core/connmgr"
	"github.com/libp2p/go-libp2p-core/control"
9
	"github.com/libp2p/go-libp2p-core/crypto"
10 11
	"github.com/libp2p/go-libp2p-core/metrics"
	"github.com/libp2p/go-libp2p-core/network"
12
	"github.com/libp2p/go-libp2p-core/peer"
13
	"github.com/libp2p/go-libp2p-core/peerstore"
Steven Allen's avatar
Steven Allen committed
14
	"github.com/libp2p/go-libp2p-core/sec/insecure"
15

Steven Allen's avatar
Steven Allen committed
16
	csms "github.com/libp2p/go-conn-security-multistream"
Steven Allen's avatar
Steven Allen committed
17
	pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
18 19
	swarm "github.com/libp2p/go-libp2p-swarm"
	"github.com/libp2p/go-libp2p-testing/net"
Steven Allen's avatar
Steven Allen committed
20
	tptu "github.com/libp2p/go-libp2p-transport-upgrader"
21 22
	yamux "github.com/libp2p/go-libp2p-yamux"
	msmux "github.com/libp2p/go-stream-muxer-multistream"
23
	"github.com/libp2p/go-tcp-transport"
Steven Allen's avatar
Steven Allen committed
24

25 26
	goprocess "github.com/jbenet/goprocess"
	ma "github.com/multiformats/go-multiaddr"
Steven Allen's avatar
Steven Allen committed
27 28 29 30 31
)

type config struct {
	disableReuseport bool
	dialOnly         bool
32
	connectionGater  connmgr.ConnectionGater
33
	sk               crypto.PrivKey
Steven Allen's avatar
Steven Allen committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
}

// Option is an option that can be passed when constructing a test swarm.
type Option func(*testing.T, *config)

// OptDisableReuseport disables reuseport in this test swarm.
var OptDisableReuseport Option = func(_ *testing.T, c *config) {
	c.disableReuseport = true
}

// OptDialOnly prevents the test swarm from listening.
var OptDialOnly Option = func(_ *testing.T, c *config) {
	c.dialOnly = true
}

49 50 51 52 53 54 55
// OptConnGater configures the given connection gater on the test
func OptConnGater(cg connmgr.ConnectionGater) Option {
	return func(_ *testing.T, c *config) {
		c.connectionGater = cg
	}
}

56 57 58 59 60 61 62
// OptPeerPrivateKey configures the peer private key which is then used to derive the public key and peer ID.
func OptPeerPrivateKey(sk crypto.PrivKey) Option {
	return func(_ *testing.T, c *config) {
		c.sk = sk
	}
}

Steven Allen's avatar
Steven Allen committed
63 64 65 66 67
// GenUpgrader creates a new connection upgrader for use with this swarm.
func GenUpgrader(n *swarm.Swarm) *tptu.Upgrader {
	id := n.LocalPeer()
	pk := n.Peerstore().PrivKey(id)
	secMuxer := new(csms.SSMuxer)
Steven Allen's avatar
Steven Allen committed
68
	secMuxer.AddTransport(insecure.ID, insecure.NewWithIdentity(id, pk))
Steven Allen's avatar
Steven Allen committed
69 70 71 72 73

	stMuxer := msmux.NewBlankTransport()
	stMuxer.AddTransport("/yamux/1.0.0", yamux.DefaultTransport)

	return &tptu.Upgrader{
74 75
		Secure: secMuxer,
		Muxer:  stMuxer,
Steven Allen's avatar
Steven Allen committed
76 77 78 79 80 81 82 83 84 85 86
	}

}

// GenSwarm generates a new test swarm.
func GenSwarm(t *testing.T, ctx context.Context, opts ...Option) *swarm.Swarm {
	var cfg config
	for _, o := range opts {
		o(t, &cfg)
	}

87 88 89 90 91 92 93 94 95 96 97 98 99 100
	var p tnet.PeerNetParams
	if cfg.sk == nil {
		p = tnet.RandPeerNetParamsOrFatal(t)
	} else {
		pk := cfg.sk.GetPublic()
		id, err := peer.IDFromPublicKey(pk)
		if err != nil {
			t.Fatal(err)
		}
		p.PrivKey = cfg.sk
		p.PubKey = pk
		p.ID = id
		p.Addr = tnet.ZeroLocalTCPAddress
	}
Steven Allen's avatar
Steven Allen committed
101

Steven Allen's avatar
Steven Allen committed
102
	ps := pstoremem.NewPeerstore()
Steven Allen's avatar
Steven Allen committed
103 104
	ps.AddPubKey(p.ID, p.PubKey)
	ps.AddPrivKey(p.ID, p.PrivKey)
105 106
	s := swarm.NewSwarm(ctx, p.ID, ps, metrics.NewBandwidthCounter(), cfg.connectionGater)

107 108 109
	// Call AddChildNoWait because we can't call AddChild after the process
	// may have been closed (e.g., if the context was canceled).
	s.Process().AddChildNoWait(goprocess.WithTeardown(ps.Close))
Steven Allen's avatar
Steven Allen committed
110

111 112 113
	upgrader := GenUpgrader(s)
	upgrader.ConnGater = cfg.connectionGater
	tcpTransport := tcp.NewTCPTransport(upgrader)
Steven Allen's avatar
Steven Allen committed
114 115 116 117 118 119 120 121 122 123 124
	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)
		}

125
		s.Peerstore().AddAddrs(p.ID, s.ListenAddresses(), peerstore.PermanentAddrTTL)
Steven Allen's avatar
Steven Allen committed
126 127 128 129 130 131
	}

	return s
}

// DivulgeAddresses adds swarm a's addresses to swarm b's peerstore.
132
func DivulgeAddresses(a, b network.Network) {
Steven Allen's avatar
Steven Allen committed
133 134
	id := a.LocalPeer()
	addrs := a.Peerstore().Addrs(id)
135
	b.Peerstore().AddAddrs(id, addrs, peerstore.PermanentAddrTTL)
Steven Allen's avatar
Steven Allen committed
136
}
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

// MockConnectionGater is a mock connection gater to be used by the tests.
type MockConnectionGater struct {
	Dial     func(p peer.ID, addr ma.Multiaddr) bool
	PeerDial func(p peer.ID) bool
	Accept   func(c network.ConnMultiaddrs) bool
	Secured  func(network.Direction, peer.ID, network.ConnMultiaddrs) bool
	Upgraded func(c network.Conn) (bool, control.DisconnectReason)
}

func DefaultMockConnectionGater() *MockConnectionGater {
	m := &MockConnectionGater{}
	m.Dial = func(p peer.ID, addr ma.Multiaddr) bool {
		return true
	}

	m.PeerDial = func(p peer.ID) bool {
		return true
	}

	m.Accept = func(c network.ConnMultiaddrs) bool {
		return true
	}

	m.Secured = func(network.Direction, peer.ID, network.ConnMultiaddrs) bool {
		return true
	}

	m.Upgraded = func(c network.Conn) (bool, control.DisconnectReason) {
		return true, 0
	}

	return m
}

func (m *MockConnectionGater) InterceptAddrDial(p peer.ID, addr ma.Multiaddr) (allow bool) {
	return m.Dial(p, addr)
}

func (m *MockConnectionGater) InterceptPeerDial(p peer.ID) (allow bool) {
	return m.PeerDial(p)
}

func (m *MockConnectionGater) InterceptAccept(c network.ConnMultiaddrs) (allow bool) {
	return m.Accept(c)
}

func (m *MockConnectionGater) InterceptSecured(d network.Direction, p peer.ID, c network.ConnMultiaddrs) (allow bool) {
	return m.Secured(d, p, c)
}

func (m *MockConnectionGater) InterceptUpgraded(tc network.Conn) (allow bool, reason control.DisconnectReason) {
	return m.Upgraded(tc)
}