[#80] core/object: Add SG-object format validation

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-12-17 19:54:38 +03:00 committed by Alex Vanin
parent f7ca4a8dce
commit 8c4e033db3
2 changed files with 44 additions and 0 deletions

View file

@ -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

View file

@ -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()))
})
}