From 79884057533fee2c9698ca372394209189072f3d Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 24 Dec 2020 12:28:33 +0300 Subject: [PATCH] [#194] pkg/container: Implement container constructor with format check Implement NewVerifiedFromV2 function that verifies format of NeoFS API V2 Container message. Signed-off-by: Leonard Lyubich --- pkg/container/container.go | 4 ++++ pkg/container/fmt.go | 27 +++++++++++++++++++++ pkg/container/fmt_test.go | 49 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 pkg/container/fmt.go create mode 100644 pkg/container/fmt_test.go diff --git a/pkg/container/container.go b/pkg/container/container.go index 2c94938..f4a52a1 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -42,6 +42,10 @@ func (c *Container) ToV2() *container.Container { return &c.v2 } +// NewVerifiedFromV2 constructs Container from NeoFS API V2 Container message. +// +// Does not perform if message meets NeoFS API V2 specification. To do this +// use NewVerifiedFromV2 constructor. func NewContainerFromV2(c *container.Container) *Container { cnr := new(Container) diff --git a/pkg/container/fmt.go b/pkg/container/fmt.go new file mode 100644 index 0000000..8579bea --- /dev/null +++ b/pkg/container/fmt.go @@ -0,0 +1,27 @@ +package container + +import ( + "github.com/nspcc-dev/neofs-api-go/pkg" + "github.com/nspcc-dev/neofs-api-go/v2/container" + "github.com/pkg/errors" +) + +// NewVerifiedFromV2 constructs Container from NeoFS API V2 Container message. +// Returns error if message does not meet NeoFS API V2 specification. +// +// Additionally checks if message carries supported version. +func NewVerifiedFromV2(cnrV2 *container.Container) (*Container, error) { + cnr := NewContainerFromV2(cnrV2) + + // check version support + if err := pkg.IsSupportedVersion(cnr.Version()); err != nil { + return nil, err + } + + // check nonce format + if _, err := cnr.NonceUUID(); err != nil { + return nil, errors.Wrap(err, "invalid nonce") + } + + return cnr, nil +} diff --git a/pkg/container/fmt_test.go b/pkg/container/fmt_test.go new file mode 100644 index 0000000..c27bdde --- /dev/null +++ b/pkg/container/fmt_test.go @@ -0,0 +1,49 @@ +package container_test + +import ( + "testing" + + "github.com/google/uuid" + "github.com/nspcc-dev/neofs-api-go/pkg" + "github.com/nspcc-dev/neofs-api-go/pkg/container" + containerV2 "github.com/nspcc-dev/neofs-api-go/v2/container" + "github.com/stretchr/testify/require" +) + +func TestNewVerifiedFromV2(t *testing.T) { + cnrV2 := new(containerV2.Container) + + errAssert := func() { + _, err := container.NewVerifiedFromV2(cnrV2) + require.Error(t, err) + } + + // set unsupported version + v := pkg.SDKVersion() + v.SetMajor(0) + require.Error(t, pkg.IsSupportedVersion(v)) + cnrV2.SetVersion(v.ToV2()) + + errAssert() + + // set supported version + v.SetMajor(2) + require.NoError(t, pkg.IsSupportedVersion(v)) + cnrV2.SetVersion(v.ToV2()) + + errAssert() + + // set invalid nonce + nonce := []byte{1, 2, 3} + cnrV2.SetNonce(nonce) + + errAssert() + + // set valid nonce + uid := uuid.New() + data, _ := uid.MarshalBinary() + cnrV2.SetNonce(data) + + _, err := container.NewVerifiedFromV2(cnrV2) + require.NoError(t, err) +}