From 651cff80c02e43758229b1923e071f06b2122863 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 | 10 +++++++++- pkg/core/object/fmt_test.go | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/core/object/fmt.go b/pkg/core/object/fmt.go index 40e07e748de..080c7c2160d 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,11 @@ func (v *FormatValidator) checkAttributes(obj *Object) error { return errDuplAttr } + val := a.Value() + if val == "" { + return errEmptyAttrVal + } + mUnique[key] = struct{}{} } diff --git a/pkg/core/object/fmt_test.go b/pkg/core/object/fmt_test.go index c23ee28b803..ccd47ca0426 100644 --- a/pkg/core/object/fmt_test.go +++ b/pkg/core/object/fmt_test.go @@ -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) + + a := object.NewAttribute() + a.SetKey("key") + + obj.SetAttributes(a) + + err := v.checkAttributes(obj.Object()) + require.Equal(t, errEmptyAttrVal, err) + }) }) }