Skip to content

Commit

Permalink
swarm: fix multiaddr comparison in ListenClose (#2247)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann authored Apr 8, 2023
1 parent 0699592 commit d996068
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion p2p/net/swarm/swarm_listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (s *Swarm) AddListenAddr(a ma.Multiaddr) error {

func containsMultiaddr(addrs []ma.Multiaddr, addr ma.Multiaddr) bool {
for _, a := range addrs {
if addr == a {
if addr.Equal(a) {
return true
}
}
Expand Down
15 changes: 12 additions & 3 deletions p2p/net/swarm/swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,17 +545,26 @@ func TestListenCloseCount(t *testing.T) {
s := GenSwarm(t, OptDialOnly)
addrsToListen := []ma.Multiaddr{
ma.StringCast("/ip4/0.0.0.0/tcp/0"),
ma.StringCast("/ip4/0.0.0.0/udp/0/quic"),
ma.StringCast("/ip4/0.0.0.0/udp/0/quic-v1"),
}

if err := s.Listen(addrsToListen...); err != nil {
t.Fatal(err)
}
listenedAddrs := s.ListenAddresses()
require.Equal(t, 2, len(listenedAddrs))
var addrToClose ma.Multiaddr
for _, addr := range listenedAddrs {
if _, err := addr.ValueForProtocol(ma.P_QUIC_V1); err == nil {
// make a copy of the address to make sure the multiaddr comparison actually works
addrToClose = ma.StringCast(addr.String())
}
}

s.ListenClose(listenedAddrs...)
s.ListenClose(addrToClose)

remainingAddrs := s.ListenAddresses()
require.Equal(t, 0, len(remainingAddrs))
require.Equal(t, 1, len(remainingAddrs))
_, err := remainingAddrs[0].ValueForProtocol(ma.P_TCP)
require.NoError(t, err, "expected the TCP address to still be present")
}

0 comments on commit d996068

Please sign in to comment.