From a3e0a9f74c147a35b05d484181fd2c66fd06e197 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 3 Nov 2020 12:03:41 +0300 Subject: [PATCH] [#85] core/container: Implement CheckFormat function Implement function that conducts sanity checks of the container data. Signed-off-by: Leonard Lyubich --- go.mod | 2 +- go.sum | 2 ++ pkg/core/container/fmt.go | 35 ++++++++++++++++++++++++++ pkg/core/container/fmt_test.go | 46 ++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 pkg/core/container/fmt.go create mode 100644 pkg/core/container/fmt_test.go diff --git a/go.mod b/go.mod index f1800cef0..23103ca9c 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/multiformats/go-multiaddr-net v0.1.2 // v0.1.1 => v0.1.2 github.com/multiformats/go-multihash v0.0.13 // indirect github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b - github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201029071528-352e99d9b91a + github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201103083623-89a7a946dcd5 github.com/nspcc-dev/neofs-crypto v0.3.0 github.com/nspcc-dev/tzhash v1.4.0 github.com/panjf2000/ants/v2 v2.3.0 diff --git a/go.sum b/go.sum index f97b2706b..9334b943b 100644 --- a/go.sum +++ b/go.sum @@ -277,6 +277,8 @@ github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b h1:gk5bZgp github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b/go.mod h1:9s7LNp2lMgf0caH2t0sam4+WT2SIauXozwP0AdBqnEo= github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201029071528-352e99d9b91a h1:+fYK6dnV+ty2p/PoVLTgN2OgOeyFgl/fFYfIPXdvJYI= github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201029071528-352e99d9b91a/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= +github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201103083623-89a7a946dcd5 h1:V5Yo8EA2S5iAG0hlKwfS7TcZqudPOHp/3MLG5/UpQ74= +github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201103083623-89a7a946dcd5/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA= github.com/nspcc-dev/neofs-crypto v0.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw= github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnBg0L4ifM= diff --git a/pkg/core/container/fmt.go b/pkg/core/container/fmt.go new file mode 100644 index 000000000..e5c45f6fe --- /dev/null +++ b/pkg/core/container/fmt.go @@ -0,0 +1,35 @@ +package container + +import ( + "github.com/google/uuid" + "github.com/nspcc-dev/neofs-api-go/pkg" + "github.com/nspcc-dev/neofs-api-go/pkg/container" + "github.com/nspcc-dev/neofs-api-go/pkg/owner" + "github.com/pkg/errors" +) + +var errNilPolicy = errors.New("placement policy is nil") + +// CheckFormat conducts an initial check of the container data. +// +// It is expected that if a container fails this test, +// it will not be inner-ring approved. +func CheckFormat(c *container.Container) error { + if c.GetPlacementPolicy() == nil { + return errNilPolicy + } + + if err := pkg.IsSupportedVersion(pkg.NewVersionFromV2(c.GetVersion())); err != nil { + return errors.Wrap(err, "incorrect version") + } + + if len(c.GetOwnerID().GetValue()) != owner.NEO3WalletSize { + return errors.Wrap(owner.ErrBadID, "incorrect owner identifier") + } + + if _, err := uuid.FromBytes(c.GetNonce()); err != nil { + return errors.Wrap(err, "incorrect nonce") + } + + return nil +} diff --git a/pkg/core/container/fmt_test.go b/pkg/core/container/fmt_test.go new file mode 100644 index 000000000..f3fdd5f01 --- /dev/null +++ b/pkg/core/container/fmt_test.go @@ -0,0 +1,46 @@ +package container + +import ( + "testing" + + "github.com/google/uuid" + "github.com/nspcc-dev/neofs-api-go/pkg" + "github.com/nspcc-dev/neofs-api-go/pkg/container" + "github.com/nspcc-dev/neofs-api-go/pkg/netmap" + "github.com/nspcc-dev/neofs-api-go/pkg/owner" + "github.com/nspcc-dev/neofs-node/pkg/util/test" + "github.com/stretchr/testify/require" +) + +func TestCheckFormat(t *testing.T) { + c := container.New() + + require.Error(t, CheckFormat(c)) + + policy := new(netmap.PlacementPolicy) + c.SetPlacementPolicy(policy) + + require.Error(t, CheckFormat(c)) + + c.SetVersion(pkg.SDKVersion().ToV2()) + + require.Error(t, CheckFormat(c)) + + wallet, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(-1).PublicKey) + require.NoError(t, err) + + c.SetOwnerID(owner.NewIDFromNeo3Wallet(wallet).ToV2()) + + c.SetNonce(nil) + + require.Error(t, CheckFormat(c)) + + uid, err := uuid.NewRandom() + require.NoError(t, err) + + nonce, _ := uid.MarshalBinary() + + c.SetNonce(nonce) + + require.NoError(t, CheckFormat(c)) +}