From f5b442fe3bf0b649e2b7409f679ceacc0bd34d9c Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Wed, 14 Oct 2020 22:03:49 +0300 Subject: [PATCH] [#147] Work around object package - implement Stringer interface and Parse method for object.ID - increase test coverage closes #147 Signed-off-by: Evgeniy Kulikov --- pkg/object/id.go | 26 +++++++++++++++++++++++++ pkg/object/id_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/pkg/object/id.go b/pkg/object/id.go index 4eefbf5a..1fcb5958 100644 --- a/pkg/object/id.go +++ b/pkg/object/id.go @@ -4,12 +4,19 @@ import ( "bytes" "crypto/sha256" + "github.com/mr-tron/base58" + "github.com/nspcc-dev/neofs-api-go/internal" "github.com/nspcc-dev/neofs-api-go/v2/refs" + "github.com/pkg/errors" ) // ID represents v2-compatible object identifier. type ID refs.ObjectID +// ErrBadID should be returned when bytes slice hasn't sha256.Size +// Notice: if byte slice changed, please, replace error message. +const ErrBadID = internal.Error("object.ID should be 32 bytes length") + // NewIDFromV2 wraps v2 ObjectID message to ID. func NewIDFromV2(idV2 *refs.ObjectID) *ID { return (*ID)(idV2) @@ -39,3 +46,22 @@ func (id *ID) Equal(id2 *ID) bool { func (id *ID) ToV2() *refs.ObjectID { return (*refs.ObjectID)(id) } + +// Parse converts base58 string representation into ID. +func (id *ID) Parse(s string) error { + data, err := base58.Decode(s) + if err != nil { + return errors.Wrap(err, "could not parse object.ID from string") + } else if len(data) != sha256.Size { + return ErrBadID + } + + (*refs.ObjectID)(id).SetValue(data) + + return nil +} + +// String returns base58 string representation of ID. +func (id *ID) String() string { + return base58.Encode((*refs.ObjectID)(id).GetValue()) +} diff --git a/pkg/object/id_test.go b/pkg/object/id_test.go index 373fa01d..e56ff960 100644 --- a/pkg/object/id_test.go +++ b/pkg/object/id_test.go @@ -3,8 +3,10 @@ package object import ( "crypto/rand" "crypto/sha256" + "strconv" "testing" + "github.com/mr-tron/base58" "github.com/stretchr/testify/require" ) @@ -38,3 +40,46 @@ func TestID_Equal(t *testing.T) { require.True(t, id1.Equal(id2)) require.False(t, id1.Equal(id3)) } + +func TestID_Parse(t *testing.T) { + t.Run("should parse successful", func(t *testing.T) { + for i := 0; i < 10; i++ { + t.Run(strconv.Itoa(i), func(t *testing.T) { + cs := randSHA256Checksum(t) + str := base58.Encode(cs[:]) + oid := NewID() + + require.NoError(t, oid.Parse(str)) + require.Equal(t, cs[:], oid.ToV2().GetValue()) + }) + } + }) + + t.Run("should failure on parse", func(t *testing.T) { + for i := 0; i < 10; i++ { + j := i + t.Run(strconv.Itoa(j), func(t *testing.T) { + cs := []byte{1, 2, 3, 4, 5, byte(j)} + str := base58.Encode(cs) + oid := NewID() + + require.EqualError(t, oid.Parse(str), ErrBadID.Error()) + }) + } + }) +} + +func TestID_String(t *testing.T) { + t.Run("should be equal", func(t *testing.T) { + for i := 0; i < 10; i++ { + t.Run(strconv.Itoa(i), func(t *testing.T) { + cs := randSHA256Checksum(t) + str := base58.Encode(cs[:]) + oid := NewID() + + require.NoError(t, oid.Parse(str)) + require.Equal(t, str, oid.String()) + }) + } + }) +}