Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
fix: avoid returning typed nils
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Apr 20, 2021
1 parent 2d73b1d commit 221d502
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion swarm_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
Expand Down
13 changes: 13 additions & 0 deletions swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
s := GenSwarm(t, context.Background(), OptDialOnly)
if err := s.Listen(ma.StringCast("/ip4/0.0.0.0/udp/0/quic")); err != nil {
Expand Down

0 comments on commit 221d502

Please sign in to comment.