[#641] ir/container: Add unique attributes check

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-23 15:45:42 +03:00 committed by Alex Vanin
parent 75709deb6f
commit 6efeee5ce0
2 changed files with 46 additions and 1 deletions

View file

@ -9,7 +9,11 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
)
var errNilPolicy = errors.New("placement policy is nil")
var (
errNilPolicy = errors.New("placement policy is nil")
errRepeatedAttributes = errors.New("repeated attributes found")
errEmptyAttribute = errors.New("empty attribute found")
)
// CheckFormat conducts an initial check of the v2 container data.
//
@ -32,5 +36,20 @@ func CheckFormat(c *container.Container) error {
return fmt.Errorf("incorrect nonce: %w", err)
}
// check if there are repeated attributes
attrs := c.Attributes()
uniqueAttr := make(map[string]struct{}, len(attrs))
for _, attr := range attrs {
if _, exists := uniqueAttr[attr.Key()]; exists {
return errRepeatedAttributes
}
if attr.Value() == "" {
return errEmptyAttribute
}
uniqueAttr[attr.Key()] = struct{}{}
}
return nil
}

View file

@ -41,4 +41,30 @@ func TestCheckFormat(t *testing.T) {
c.SetNonceUUID(uuid.New())
require.NoError(t, CheckFormat(c))
// set empty value attribute
attr1 := container.NewAttribute()
attr1.SetKey("attr")
attrs := container.Attributes{attr1}
c.SetAttributes(attrs)
require.ErrorIs(t, CheckFormat(c), errEmptyAttribute)
// add same key attribute
attr2 := container.NewAttribute()
attr2.SetKey(attr1.Key())
attr2.SetValue("val")
attr1.SetValue(attr2.Value())
attrs = append(attrs, attr2)
c.SetAttributes(attrs)
require.ErrorIs(t, CheckFormat(c), errRepeatedAttributes)
attr2.SetKey(attr1.Key() + "smth")
require.NoError(t, CheckFormat(c))
}