From cf70026c7e422f55cd08d7765916abc46e22cd9f Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 30 Sep 2020 20:24:05 +0300 Subject: [PATCH] [#164] sdk/container: Implement Equal method on ID Signed-off-by: Leonard Lyubich --- pkg/container/id.go | 9 +++++++++ pkg/container/id_test.go | 23 +++++++++++++++++++++++ pkg/object/id.go | 4 ++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/pkg/container/id.go b/pkg/container/id.go index dae7bf40..795f6447 100644 --- a/pkg/container/id.go +++ b/pkg/container/id.go @@ -1,6 +1,7 @@ package container import ( + "bytes" "crypto/sha256" "github.com/nspcc-dev/neofs-api-go/v2/refs" @@ -30,3 +31,11 @@ func (id *ID) SetSHA256(v [sha256.Size]byte) { func (id *ID) ToV2() *refs.ContainerID { return (*refs.ContainerID)(id) } + +// Equal returns true if identifiers are identical. +func (id *ID) Equal(id2 *ID) bool { + return bytes.Equal( + (*refs.ContainerID)(id).GetValue(), + (*refs.ContainerID)(id2).GetValue(), + ) +} diff --git a/pkg/container/id_test.go b/pkg/container/id_test.go index a5ae05e4..36037718 100644 --- a/pkg/container/id_test.go +++ b/pkg/container/id_test.go @@ -22,3 +22,26 @@ func TestIDV2_0(t *testing.T) { require.Equal(t, checksum[:], cidV2.GetValue()) } + +func randSHA256Checksum(t *testing.T) (cs [sha256.Size]byte) { + _, err := rand.Read(cs[:]) + require.NoError(t, err) + + return +} + +func TestID_Equal(t *testing.T) { + cs := randSHA256Checksum(t) + + id1 := NewID() + id1.SetSHA256(cs) + + id2 := NewID() + id2.SetSHA256(cs) + + id3 := NewID() + id3.SetSHA256(randSHA256Checksum(t)) + + require.True(t, id1.Equal(id2)) + require.False(t, id1.Equal(id3)) +} diff --git a/pkg/object/id.go b/pkg/object/id.go index c7eb5736..4eefbf5a 100644 --- a/pkg/object/id.go +++ b/pkg/object/id.go @@ -30,8 +30,8 @@ func (id *ID) SetSHA256(v [sha256.Size]byte) { // Equal returns true if identifiers are identical. func (id *ID) Equal(id2 *ID) bool { return bytes.Equal( - (*ID)(id).ToV2().GetValue(), - (*ID)(id2).ToV2().GetValue(), + (*refs.ObjectID)(id).GetValue(), + (*refs.ObjectID)(id2).GetValue(), ) }