From 8c4e033db388e096c151bac044335c83c3072356 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 17 Dec 2020 19:54:38 +0300 Subject: [PATCH] [#80] core/object: Add SG-object format validation Signed-off-by: Leonard Lyubich --- pkg/core/object/fmt.go | 17 +++++++++++++++++ pkg/core/object/fmt_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/pkg/core/object/fmt.go b/pkg/core/object/fmt.go index 54452d95f..36ea25f94 100644 --- a/pkg/core/object/fmt.go +++ b/pkg/core/object/fmt.go @@ -5,6 +5,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/owner" + "github.com/nspcc-dev/neofs-api-go/pkg/storagegroup" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/pkg/errors" ) @@ -140,6 +141,22 @@ func (v *FormatValidator) ValidateContent(o *Object) error { if v.deleteHandler != nil { v.deleteHandler.DeleteObjects(o.Address(), addrList...) } + case object.TypeStorageGroup: + if len(o.Payload()) == 0 { + return errors.Errorf("(%T) empty payload in SG", v) + } + + sg := storagegroup.New() + + if err := sg.Unmarshal(o.Payload()); err != nil { + return errors.Wrapf(err, "(%T) could not unmarshal SG content", v) + } + + for _, id := range sg.Members() { + if id == nil { + return errors.Errorf("(%T) empty member in SG", v) + } + } } return nil diff --git a/pkg/core/object/fmt_test.go b/pkg/core/object/fmt_test.go index 8c2941a30..6ce4b4f14 100644 --- a/pkg/core/object/fmt_test.go +++ b/pkg/core/object/fmt_test.go @@ -9,6 +9,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/container" "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/owner" + "github.com/nspcc-dev/neofs-api-go/pkg/storagegroup" "github.com/nspcc-dev/neofs-api-go/pkg/token" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/pkg/util/test" @@ -129,4 +130,30 @@ func TestFormatValidator_Validate(t *testing.T) { require.NoError(t, v.ValidateContent(obj.Object())) }) + + t.Run("storage group content", func(t *testing.T) { + obj := NewRaw() + obj.SetType(object.TypeStorageGroup) + + require.Error(t, v.ValidateContent(obj.Object())) + + content := storagegroup.New() + content.SetMembers([]*object.ID{nil}) + + data, err := content.Marshal() + require.NoError(t, err) + + obj.SetPayload(data) + + require.Error(t, v.ValidateContent(obj.Object())) + + content.SetMembers([]*object.ID{testObjectID(t)}) + + data, err = content.Marshal() + require.NoError(t, err) + + obj.SetPayload(data) + + require.NoError(t, v.ValidateContent(obj.Object())) + }) }