[#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. // Validate validates object format.
// //
// Does not validate payload checksum and content.
//
// Returns nil error if object has valid structure. // Returns nil error if object has valid structure.
func (v *FormatValidator) Validate(obj *Object) error { func (v *FormatValidator) Validate(obj *Object) error {
if obj == nil { if obj == nil {
@ -35,10 +37,6 @@ func (v *FormatValidator) Validate(obj *Object) error {
return errNilCID 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 { if err := v.validateSignatureKey(obj); err != nil {
return errors.Wrapf(err, "(%T) could not validate signature key", v) 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 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 { switch t {
case object.TypeTombstone: case object.TypeTombstone:
if len(payload) == 0 { if len(payload) == 0 {

View file

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