[#58] core/object: Decouple the payload content validation method

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-30 20:53:12 +03:00 committed by Alex Vanin
parent 107f3097e4
commit 2abb03dbd1
2 changed files with 7 additions and 22 deletions

View file

@ -25,6 +25,8 @@ func NewFormatValidator() *FormatValidator {
// Validate validates object format.
//
// Does not validate payload checksum and content.
//
// Returns nil error if object has valid structure.
func (v *FormatValidator) Validate(obj *Object) error {
if obj == nil {
@ -35,10 +37,6 @@ func (v *FormatValidator) Validate(obj *Object) error {
return errNilCID
}
if err := v.validateContent(obj.GetType(), obj.GetPayload()); err != nil {
return errors.Wrapf(err, "(%T) incorrect content", v)
}
if err := v.validateSignatureKey(obj); err != nil {
return errors.Wrapf(err, "(%T) could not validate signature key", v)
}
@ -88,7 +86,8 @@ func (v *FormatValidator) checkOwnerKey(id *owner.ID, key []byte) error {
return nil
}
func (v *FormatValidator) validateContent(t object.Type, payload []byte) error {
// ValidateContent validates payload content according to object type.
func (v *FormatValidator) ValidateContent(t object.Type, payload []byte) error {
switch t {
case object.TypeTombstone:
if len(payload) == 0 {

View file

@ -105,24 +105,14 @@ func TestFormatValidator_Validate(t *testing.T) {
})
t.Run("tombstone content", func(t *testing.T) {
obj := blankValidObject(t, ownerKey)
obj.SetType(object.TypeTombstone)
require.NoError(t, object.SetIDWithSignature(ownerKey, obj.SDK()))
require.Error(t, v.Validate(obj.Object()))
require.Error(t, v.ValidateContent(object.TypeTombstone, nil))
addr := object.NewAddress()
data, err := addr.ToV2().StableMarshal(nil)
require.NoError(t, err)
obj.SetPayload(data)
require.NoError(t, object.SetIDWithSignature(ownerKey, obj.SDK()))
require.Error(t, v.Validate(obj.Object()))
require.Error(t, v.ValidateContent(object.TypeTombstone, data))
addr.SetContainerID(testContainerID(t))
addr.SetObjectID(testObjectID(t))
@ -130,10 +120,6 @@ func TestFormatValidator_Validate(t *testing.T) {
data, err = addr.ToV2().StableMarshal(nil)
require.NoError(t, err)
obj.SetPayload(data)
require.NoError(t, object.SetIDWithSignature(ownerKey, obj.SDK()))
require.NoError(t, v.Validate(obj.Object()))
require.NoError(t, v.ValidateContent(object.TypeTombstone, data))
})
}