From 25391111ad265ccec282be7759774fab6141a5f0 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 23 Jun 2021 17:20:13 +0300 Subject: [PATCH] [#633] object: Disallow empty attribute values Values of object attributes must not be empty according to NeoFS specification. Make `FormatValidator.Validate` method to return an error if at least one attribute has empty value. Signed-off-by: Leonard Lyubich --- pkg/core/object/fmt.go | 9 ++++++++- pkg/core/object/fmt_test.go | 14 +++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/core/object/fmt.go b/pkg/core/object/fmt.go index 40e07e74..b0741e15 100644 --- a/pkg/core/object/fmt.go +++ b/pkg/core/object/fmt.go @@ -231,7 +231,10 @@ func expirationEpochAttribute(obj *Object) (uint64, error) { return 0, errNoExpirationEpoch } -var errDuplAttr = errors.New("duplication of attributes detected") +var ( + errDuplAttr = errors.New("duplication of attributes detected") + errEmptyAttrVal = errors.New("empty attribute value") +) func (v *FormatValidator) checkAttributes(obj *Object) error { as := obj.Attributes() @@ -245,6 +248,10 @@ func (v *FormatValidator) checkAttributes(obj *Object) error { return errDuplAttr } + if a.Value() == "" { + return errEmptyAttrVal + } + mUnique[key] = struct{}{} } diff --git a/pkg/core/object/fmt_test.go b/pkg/core/object/fmt_test.go index c23ee28b..f186182b 100644 --- a/pkg/core/object/fmt_test.go +++ b/pkg/core/object/fmt_test.go @@ -222,7 +222,7 @@ func TestFormatValidator_Validate(t *testing.T) { t.Run("attributes", func(t *testing.T) { t.Run("duplication", func(t *testing.T) { - obj := blankValidObject(t, ownerKey) + obj := blankValidObject(t, &ownerKey.PrivateKey) a1 := object.NewAttribute() a1.SetKey("key1") @@ -242,5 +242,17 @@ func TestFormatValidator_Validate(t *testing.T) { err = v.checkAttributes(obj.Object()) require.Equal(t, errDuplAttr, err) }) + + t.Run("empty value", func(t *testing.T) { + obj := blankValidObject(t, &ownerKey.PrivateKey) + + a := object.NewAttribute() + a.SetKey("key") + + obj.SetAttributes(a) + + err := v.checkAttributes(obj.Object()) + require.Equal(t, errEmptyAttrVal, err) + }) }) }