forked from TrueCloudLab/frostfs-api-go
[#147] Work around container package
- implement Stringer interface and Parse method for container.ID - increase test coverage closes #147 Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
parent
28260eb2ff
commit
f5388b553e
2 changed files with 71 additions and 0 deletions
|
@ -4,12 +4,19 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
"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/nspcc-dev/neofs-api-go/v2/refs"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ID represents v2-compatible container identifier.
|
// ID represents v2-compatible container identifier.
|
||||||
type ID refs.ContainerID
|
type ID refs.ContainerID
|
||||||
|
|
||||||
|
// ErrBadID should be returned when bytes slice isn't Container.ID size (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 ContainerID message to ID.
|
// NewIDFromV2 wraps v2 ContainerID message to ID.
|
||||||
func NewIDFromV2(idV2 *refs.ContainerID) *ID {
|
func NewIDFromV2(idV2 *refs.ContainerID) *ID {
|
||||||
return (*ID)(idV2)
|
return (*ID)(idV2)
|
||||||
|
@ -39,3 +46,22 @@ func (id *ID) Equal(id2 *ID) bool {
|
||||||
(*refs.ContainerID)(id2).GetValue(),
|
(*refs.ContainerID)(id2).GetValue(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 container.ID from string")
|
||||||
|
} else if len(data) != sha256.Size {
|
||||||
|
return ErrBadID
|
||||||
|
}
|
||||||
|
|
||||||
|
(*refs.ContainerID)(id).SetValue(data)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns base58 string representation of ID.
|
||||||
|
func (id *ID) String() string {
|
||||||
|
return base58.Encode((*refs.ContainerID)(id).GetValue())
|
||||||
|
}
|
||||||
|
|
|
@ -3,8 +3,10 @@ package container
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mr-tron/base58"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -45,3 +47,46 @@ func TestID_Equal(t *testing.T) {
|
||||||
require.True(t, id1.Equal(id2))
|
require.True(t, id1.Equal(id2))
|
||||||
require.False(t, id1.Equal(id3))
|
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[:])
|
||||||
|
cid := NewID()
|
||||||
|
|
||||||
|
require.NoError(t, cid.Parse(str))
|
||||||
|
require.Equal(t, cs[:], cid.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)
|
||||||
|
cid := NewID()
|
||||||
|
|
||||||
|
require.EqualError(t, cid.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[:])
|
||||||
|
cid := NewID()
|
||||||
|
|
||||||
|
require.NoError(t, cid.Parse(str))
|
||||||
|
require.Equal(t, str, cid.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue