Skip to content

Commit

Permalink
stop using go-libp2p-testing peer ID generation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Aug 17, 2022
1 parent f087aac commit 0c04675
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 64 deletions.
39 changes: 24 additions & 15 deletions p2p/net/conn-security-multistream/ssms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@ package csms

import (
"context"
"crypto/rand"
"io"
"net"
"sync"
"testing"

tnet "github.com/libp2p/go-libp2p-testing/net"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/sec"
"github.com/libp2p/go-libp2p/core/sec/insecure"

"github.com/stretchr/testify/require"
)

func newPeer(t *testing.T) (crypto.PrivKey, peer.ID) {
priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
require.NoError(t, err)
id, err := peer.IDFromPrivateKey(priv)
require.NoError(t, err)
return priv, id
}

type TransportAdapter struct {
mux *SSMuxer
}
Expand All @@ -30,13 +39,13 @@ func (sm *TransportAdapter) SecureOutbound(ctx context.Context, insecure net.Con
}

func TestCommonProto(t *testing.T) {
idA := tnet.RandIdentityOrFatal(t)
idB := tnet.RandIdentityOrFatal(t)
privA, idA := newPeer(t)
privB, idB := newPeer(t)

var at, bt SSMuxer

atInsecure := insecure.NewWithIdentity(idA.ID(), idA.PrivateKey())
btInsecure := insecure.NewWithIdentity(idB.ID(), idB.PrivateKey())
atInsecure := insecure.NewWithIdentity(idA, privA)
btInsecure := insecure.NewWithIdentity(idB, privB)
at.AddTransport("/plaintext/1.0.0", atInsecure)
bt.AddTransport("/plaintext/1.1.0", btInsecure)
bt.AddTransport("/plaintext/1.0.0", btInsecure)
Expand All @@ -48,7 +57,7 @@ func TestCommonProto(t *testing.T) {
go func() {
conn, err := ln.Accept()
require.NoError(t, err)
c, err := muxB.SecureInbound(context.Background(), conn, idA.ID())
c, err := muxB.SecureInbound(context.Background(), conn, idA)
require.NoError(t, err)
connChan <- c
}()
Expand All @@ -58,29 +67,29 @@ func TestCommonProto(t *testing.T) {
cconn, err := net.Dial("tcp", ln.Addr().String())
require.NoError(t, err)

cc, err := muxA.SecureOutbound(context.Background(), cconn, idB.ID())
cc, err := muxA.SecureOutbound(context.Background(), cconn, idB)
require.NoError(t, err)
require.Equal(t, cc.LocalPeer(), idA.ID())
require.Equal(t, cc.RemotePeer(), idB.ID())
require.Equal(t, cc.LocalPeer(), idA)
require.Equal(t, cc.RemotePeer(), idB)
_, err = cc.Write([]byte("foobar"))
require.NoError(t, err)
require.NoError(t, cc.Close())

sc := <-connChan
require.Equal(t, sc.LocalPeer(), idB.ID())
require.Equal(t, sc.RemotePeer(), idA.ID())
require.Equal(t, sc.LocalPeer(), idB)
require.Equal(t, sc.RemotePeer(), idA)
b, err := io.ReadAll(sc)
require.NoError(t, err)
require.Equal(t, "foobar", string(b))
}

func TestNoCommonProto(t *testing.T) {
idA := tnet.RandIdentityOrFatal(t)
idB := tnet.RandIdentityOrFatal(t)
privA, idA := newPeer(t)
privB, idB := newPeer(t)

var at, bt SSMuxer
atInsecure := insecure.NewWithIdentity(idA.ID(), idA.PrivateKey())
btInsecure := insecure.NewWithIdentity(idB.ID(), idB.PrivateKey())
atInsecure := insecure.NewWithIdentity(idA, privA)
btInsecure := insecure.NewWithIdentity(idB, privB)

at.AddTransport("/plaintext/1.0.0", atInsecure)
bt.AddTransport("/plaintext/1.1.0", btInsecure)
Expand Down
67 changes: 53 additions & 14 deletions p2p/net/mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,85 @@ package mocknet
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"io"
"math"
"sync"
"testing"
"time"

"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"

"github.com/libp2p/go-libp2p-testing/ci"
tnet "github.com/libp2p/go-libp2p-testing/net"
tetc "github.com/libp2p/go-libp2p-testing/etc"
"github.com/libp2p/go-libp2p-testing/race"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)

var lastPort = struct {
port int
sync.Mutex
}{}

// randLocalTCPAddress returns a random multiaddr. it suppresses errors
// for nice composability-- do check the address isn't nil.
//
// NOTE: for real network tests, use a :0 address, so the kernel
// assigns an unused TCP port. otherwise you may get clashes.
func randLocalTCPAddress() ma.Multiaddr {
// chances are it will work out, but it **might** fail if the port is in use
// most ports above 10000 aren't in use by long running processes, so yay.
// (maybe there should be a range of "loopback" ports that are guaranteed
// to be open for the process, but naturally can only talk to self.)

lastPort.Lock()
if lastPort.port == 0 {
lastPort.port = 10000 + tetc.SeededRand.Intn(50000)
}
port := lastPort.port
lastPort.port++
lastPort.Unlock()

addr := fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)
maddr, _ := ma.NewMultiaddr(addr)
return maddr
}

func TestNetworkSetup(t *testing.T) {
ctx := context.Background()
id1 := tnet.RandIdentityOrFatal(t)
id2 := tnet.RandIdentityOrFatal(t)
id3 := tnet.RandIdentityOrFatal(t)
priv1, _, err := crypto.GenerateEd25519Key(rand.Reader)
require.NoError(t, err)
priv2, _, err := crypto.GenerateEd25519Key(rand.Reader)
require.NoError(t, err)
priv3, _, err := crypto.GenerateEd25519Key(rand.Reader)
require.NoError(t, err)
mn := New()
defer mn.Close()

// add peers to mock net

a1 := tnet.RandLocalTCPAddress()
a2 := tnet.RandLocalTCPAddress()
a3 := tnet.RandLocalTCPAddress()
a1 := randLocalTCPAddress()
a2 := randLocalTCPAddress()
a3 := randLocalTCPAddress()

h1, err := mn.AddPeer(id1.PrivateKey(), a1)
h1, err := mn.AddPeer(priv1, a1)
if err != nil {
t.Fatal(err)
}
p1 := h1.ID()

h2, err := mn.AddPeer(id2.PrivateKey(), a2)
h2, err := mn.AddPeer(priv2, a2)
if err != nil {
t.Fatal(err)
}
p2 := h2.ID()

h3, err := mn.AddPeer(id3.PrivateKey(), a3)
h3, err := mn.AddPeer(priv3, a3)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -315,10 +352,12 @@ func TestAdding(t *testing.T) {

var peers []peer.ID
for i := 0; i < 3; i++ {
id := tnet.RandIdentityOrFatal(t)

a := tnet.RandLocalTCPAddress()
h, err := mn.AddPeer(id.PrivateKey(), a)
priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
t.Fatal(err)
}
a := randLocalTCPAddress()
h, err := mn.AddPeer(priv, a)
if err != nil {
t.Fatal(err)
}
Expand Down
43 changes: 26 additions & 17 deletions p2p/net/swarm/dial_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package swarm

import (
"context"
"crypto/rand"
"errors"
"fmt"
"sync"
"testing"
"time"

"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/core/sec/insecure"
"github.com/libp2p/go-libp2p/core/transport"
Expand All @@ -19,22 +22,28 @@ import (
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"

tnet "github.com/libp2p/go-libp2p-testing/net"

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

func newPeer(t *testing.T) (crypto.PrivKey, peer.ID) {
priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
require.NoError(t, err)
id, err := peer.IDFromPrivateKey(priv)
require.NoError(t, err)
return priv, id
}

func makeSwarm(t *testing.T) *Swarm {
p := tnet.RandPeerNetParamsOrFatal(t)
priv, id := newPeer(t)

ps, err := pstoremem.NewPeerstore()
require.NoError(t, err)
ps.AddPubKey(p.ID, p.PubKey)
ps.AddPrivKey(p.ID, p.PrivKey)
ps.AddPubKey(id, priv.GetPublic())
ps.AddPrivKey(id, priv)
t.Cleanup(func() { ps.Close() })

s, err := NewSwarm(p.ID, ps, WithDialTimeout(time.Second))
s, err := NewSwarm(id, ps, WithDialTimeout(time.Second))
require.NoError(t, err)

upgrader := makeUpgrader(t, s)
Expand All @@ -46,11 +55,11 @@ func makeSwarm(t *testing.T) *Swarm {
if err := s.AddTransport(tcpTransport); err != nil {
t.Fatal(err)
}
if err := s.Listen(p.Addr); err != nil {
if err := s.Listen(ma.StringCast("/ip4/0.0.0.0/tcp/0")); err != nil {
t.Fatal(err)
}

quicTransport, err := quic.NewTransport(p.PrivKey, nil, nil, nil)
quicTransport, err := quic.NewTransport(priv, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -170,13 +179,13 @@ func TestDialWorkerLoopFailure(t *testing.T) {
s1 := makeSwarm(t)
defer s1.Close()

p2 := tnet.RandPeerNetParamsOrFatal(t)
_, p2 := newPeer(t)

s1.Peerstore().AddAddrs(p2.ID, []ma.Multiaddr{ma.StringCast("/ip4/11.0.0.1/tcp/1234"), ma.StringCast("/ip4/11.0.0.1/udp/1234/quic")}, peerstore.PermanentAddrTTL)
s1.Peerstore().AddAddrs(p2, []ma.Multiaddr{ma.StringCast("/ip4/11.0.0.1/tcp/1234"), ma.StringCast("/ip4/11.0.0.1/udp/1234/quic")}, peerstore.PermanentAddrTTL)

reqch := make(chan dialRequest)
resch := make(chan dialResponse)
worker := newDialWorker(s1, p2.ID, reqch)
worker := newDialWorker(s1, p2, reqch)
go worker.loop()

reqch <- dialRequest{ctx: context.Background(), resch: resch}
Expand All @@ -195,12 +204,12 @@ func TestDialWorkerLoopConcurrentFailure(t *testing.T) {
s1 := makeSwarm(t)
defer s1.Close()

p2 := tnet.RandPeerNetParamsOrFatal(t)
_, p2 := newPeer(t)

s1.Peerstore().AddAddrs(p2.ID, []ma.Multiaddr{ma.StringCast("/ip4/11.0.0.1/tcp/1234"), ma.StringCast("/ip4/11.0.0.1/udp/1234/quic")}, peerstore.PermanentAddrTTL)
s1.Peerstore().AddAddrs(p2, []ma.Multiaddr{ma.StringCast("/ip4/11.0.0.1/tcp/1234"), ma.StringCast("/ip4/11.0.0.1/udp/1234/quic")}, peerstore.PermanentAddrTTL)

reqch := make(chan dialRequest)
worker := newDialWorker(s1, p2.ID, reqch)
worker := newDialWorker(s1, p2, reqch)
go worker.loop()

const dials = 100
Expand Down Expand Up @@ -285,16 +294,16 @@ func TestDialWorkerLoopConcurrentFailureStress(t *testing.T) {
s1 := makeSwarm(t)
defer s1.Close()

p2 := tnet.RandPeerNetParamsOrFatal(t)
_, p2 := newPeer(t)

var addrs []ma.Multiaddr
for i := 0; i < 16; i++ {
addrs = append(addrs, ma.StringCast(fmt.Sprintf("/ip4/11.0.0.%d/tcp/%d", i%256, 1234+i)))
}
s1.Peerstore().AddAddrs(p2.ID, addrs, peerstore.PermanentAddrTTL)
s1.Peerstore().AddAddrs(p2, addrs, peerstore.PermanentAddrTTL)

reqch := make(chan dialRequest)
worker := newDialWorker(s1, p2.ID, reqch)
worker := newDialWorker(s1, p2, reqch)
go worker.loop()

const dials = 100
Expand Down
Loading

0 comments on commit 0c04675

Please sign in to comment.