From d4327ec018ef99e46d2874db03aeca5bd551a74c Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 19:55:32 +0300 Subject: [PATCH] [#302] pkg/reputation: Convert nil `PeerID` to nil message Document that `PeerID.ToV2` method return `nil` when called on `nil`. Document that `PeerIDFromV2` function return `nil` when called on `nil`. Write corresponding unit tests. Signed-off-by: Pavel Karpy --- pkg/reputation/peer.go | 4 ++++ pkg/reputation/peer_test.go | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pkg/reputation/peer.go b/pkg/reputation/peer.go index f35b9ea..af7bec1 100644 --- a/pkg/reputation/peer.go +++ b/pkg/reputation/peer.go @@ -17,6 +17,8 @@ func NewPeerID() *PeerID { } // PeerIDFromV2 converts NeoFS API v2 reputation.PeerID message to PeerID. +// +// Nil reputation.PeerID converts to nil. func PeerIDFromV2(id *reputation.PeerID) *PeerID { return (*PeerID)(id) } @@ -28,6 +30,8 @@ func (x *PeerID) SetPublicKey(v [crypto.PublicKeyCompressedSize]byte) { } // ToV2 converts PeerID to NeoFS API v2 reputation.PeerID message. +// +// Nil PeerID converts to nil. func (x *PeerID) ToV2() *reputation.PeerID { return (*reputation.PeerID)(x) } diff --git a/pkg/reputation/peer_test.go b/pkg/reputation/peer_test.go index 4a63546..b226cf9 100644 --- a/pkg/reputation/peer_test.go +++ b/pkg/reputation/peer_test.go @@ -5,13 +5,22 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/reputation" reputationtest "github.com/nspcc-dev/neofs-api-go/pkg/reputation/test" + reputationV2 "github.com/nspcc-dev/neofs-api-go/v2/reputation" "github.com/stretchr/testify/require" ) func TestPeerID_ToV2(t *testing.T) { - peerID := reputationtest.GeneratePeerID() + t.Run("nil", func(t *testing.T) { + var x *reputation.PeerID - require.Equal(t, peerID, reputation.PeerIDFromV2(peerID.ToV2())) + require.Nil(t, x.ToV2()) + }) + + t.Run("nil", func(t *testing.T) { + peerID := reputationtest.GeneratePeerID() + + require.Equal(t, peerID, reputation.PeerIDFromV2(peerID.ToV2())) + }) } func TestPeerID_String(t *testing.T) { @@ -50,3 +59,11 @@ func TestPeerIDEncoding(t *testing.T) { require.Equal(t, id, id2) }) } + +func TestPeerIDFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *reputationV2.PeerID + + require.Nil(t, reputation.PeerIDFromV2(x)) + }) +}