Unverified Commit a73c29ad authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #257 from libp2p/fix/typed-nil

fix: avoid returning typed nils
parents 2d73b1d6 221d5023
...@@ -226,7 +226,12 @@ func (s *Swarm) DialPeer(ctx context.Context, p peer.ID) (network.Conn, error) { ...@@ -226,7 +226,12 @@ func (s *Swarm) DialPeer(ctx context.Context, p peer.ID) (network.Conn, error) {
return nil, &DialError{Peer: p, Cause: ErrGaterDisallowedConnection} return nil, &DialError{Peer: p, Cause: ErrGaterDisallowedConnection}
} }
return s.dialPeer(ctx, p) // Avoid typed nil issues.
c, err := s.dialPeer(ctx, p)
if err != nil {
return nil, err
}
return c, nil
} }
// internal dial method that returns an unwrapped conn // internal dial method that returns an unwrapped conn
......
...@@ -412,6 +412,19 @@ func TestCloseWithOpenStreams(t *testing.T) { ...@@ -412,6 +412,19 @@ func TestCloseWithOpenStreams(t *testing.T) {
} }
} }
func TestTypedNilConn(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := GenSwarm(t, ctx)
defer s.Close()
// We can't dial ourselves.
c, err := s.DialPeer(ctx, s.LocalPeer())
require.Error(t, err)
// If we fail to dial, the connection should be nil.
require.True(t, c == nil)
}
func TestPreventDialListenAddr(t *testing.T) { func TestPreventDialListenAddr(t *testing.T) {
s := GenSwarm(t, context.Background(), OptDialOnly) s := GenSwarm(t, context.Background(), OptDialOnly)
if err := s.Listen(ma.StringCast("/ip4/0.0.0.0/udp/0/quic")); err != nil { if err := s.Listen(ma.StringCast("/ip4/0.0.0.0/udp/0/quic")); err != nil {
......
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