Skip to content

Commit

Permalink
test: add tests demonstrating the ability to discover a peer's peerID…
Browse files Browse the repository at this point in the history
… during security negotiation
  • Loading branch information
aschmahmann committed Aug 17, 2023
1 parent a0a9c37 commit cb2fa85
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions p2p/test/transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ import (
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/sec"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
"github.com/libp2p/go-libp2p/p2p/net/swarm"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
"github.com/libp2p/go-libp2p/p2p/security/noise"
tls "github.com/libp2p/go-libp2p/p2p/security/tls"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -607,3 +613,122 @@ func TestStreamReadDeadline(t *testing.T) {
})
}
}

func TestDiscoverPeerIDFromSecurityNegotiation(t *testing.T) {
// extracts the peerID of the dialed peer from the error
extractPeerIDFromError := func(inputErr error) (peer.ID, error) {
var dialErr *swarm.DialError
if !errors.As(inputErr, &dialErr) {
return "", inputErr
}
innerErr := dialErr.DialErrors[0].Cause

var peerIDMismatchErr sec.ErrPeerIDMismatch
if errors.As(innerErr, &peerIDMismatchErr) {
return peerIDMismatchErr.Actual, nil
}

return "", inputErr
}

// runs a test to verify we can extract the peer ID from a target with just its address
runTest := func(t *testing.T, h host.Host) {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Use a bogus peer ID so that when we connect to the target we get an error telling
// us the targets real peer ID
bogusPeerId, err := peer.Decode("QmadAdJ3f63JyNs65X7HHzqDwV53ynvCcKtNFvdNaz3nhk")
if err != nil {
t.Fatal("the hard coded bogus peerID is invalid")
}

ai := &peer.AddrInfo{
ID: bogusPeerId,
Addrs: []multiaddr.Multiaddr{h.Addrs()[0]},
}

testHost, err := libp2p.New()
if err != nil {
t.Fatal(err)
}

// Try connecting with the bogus peer ID
if err := testHost.Connect(ctx, *ai); err != nil {
// Extract the actual peer ID from the error
newPeerId, err := extractPeerIDFromError(err)
if err != nil {
t.Fatal(err)
}
ai.ID = newPeerId

// Make sure the new ID is what we expected
if ai.ID != h.ID() {
t.Fatalf("peerID mismatch: expected %s, got %s", h.ID(), ai.ID)
}

// and just to double-check try connecting again to make sure it works
if err := testHost.Connect(ctx, *ai); err != nil {
t.Fatal(err)
}
}
}

t.Run("tcp+tls", func(t *testing.T) {
h, err := libp2p.New(
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Security(tls.ID, tls.New),
libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"),
)
if err != nil {
t.Fatal(err)
}
runTest(t, h)
})
t.Run("tcp+noise", func(t *testing.T) {
h, err := libp2p.New(
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Security(noise.ID, noise.New),
libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"),
)
if err != nil {
t.Fatal(err)
}
runTest(t, h)
})
t.Run("ws+tls", func(t *testing.T) {
h, err := libp2p.New(
libp2p.Transport(ws.New),
libp2p.Security(tls.ID, tls.New),
libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0/ws"),
)
if err != nil {
t.Fatal(err)
}
runTest(t, h)
})
t.Run("ws+noise", func(t *testing.T) {
h, err := libp2p.New(
libp2p.Transport(ws.New),
libp2p.Security(noise.ID, noise.New),
libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0/ws"),
)
if err != nil {
t.Fatal(err)
}
runTest(t, h)
})
t.Run("quic-v1", func(t *testing.T) {
h, err := libp2p.New(
libp2p.Transport(quic.NewTransport),
libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/quic-v1"),
)
if err != nil {
t.Fatal(err)
}
runTest(t, h)
})
// Note: WebTransport was omitted because it requires a certificate hash which is not purely derivable from the
// peerID which means discovering the peerID is not particularly valuable.
}

0 comments on commit cb2fa85

Please sign in to comment.