[#1556] Upgrade NeoFS SDK Go with changed container API

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-06-28 10:01:05 +03:00 committed by LeL
parent f699e82ea7
commit c165d1a9b5
36 changed files with 207 additions and 480 deletions

View file

@ -1,54 +0,0 @@
package container
import (
"errors"
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/core/version"
"github.com/nspcc-dev/neofs-sdk-go/container"
)
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.
//
// It is expected that if a container fails this test,
// it will not be approved by the inner ring.
func CheckFormat(c *container.Container) error {
if c.PlacementPolicy() == nil {
return errNilPolicy
}
if v := c.Version(); v == nil || !version.IsValid(*v) {
return fmt.Errorf("incorrect version %s", v)
}
if c.OwnerID() == nil {
return errors.New("missing owner")
}
if _, err := c.NonceUUID(); err != nil {
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

@ -1,73 +0,0 @@
package container
import (
"testing"
"github.com/google/uuid"
"github.com/nspcc-dev/neofs-node/pkg/util/test"
"github.com/nspcc-dev/neofs-sdk-go/container"
netmaptest "github.com/nspcc-dev/neofs-sdk-go/netmap/test"
"github.com/nspcc-dev/neofs-sdk-go/user"
"github.com/nspcc-dev/neofs-sdk-go/version"
"github.com/stretchr/testify/require"
)
func TestCheckFormat(t *testing.T) {
c := container.New()
require.Error(t, CheckFormat(c))
policy := netmaptest.PlacementPolicy()
c.SetPlacementPolicy(&policy)
require.Error(t, CheckFormat(c))
ver := version.Current()
c.SetVersion(&ver)
require.Error(t, CheckFormat(c))
var idUser user.ID
user.IDFromKey(&idUser, test.DecodeKey(-1).PublicKey)
c.SetOwnerID(&idUser)
// set incorrect nonce
cV2 := c.ToV2()
cV2.SetNonce([]byte{1, 2, 3})
c = container.NewContainerFromV2(cV2)
require.Error(t, CheckFormat(c))
c.SetNonceUUID(uuid.New())
require.NoError(t, CheckFormat(c))
// set empty value attribute
var attr1 container.Attribute
attr1.SetKey("attr")
attrs := container.Attributes{attr1}
c.SetAttributes(attrs)
require.ErrorIs(t, CheckFormat(c), errEmptyAttribute)
// add same key attribute
var attr2 container.Attribute
attr2.SetKey(attr1.Key())
attr2.SetValue("val")
attrs[0].SetValue(attr2.Value())
attrs = append(attrs, attr2)
c.SetAttributes(attrs)
require.ErrorIs(t, CheckFormat(c), errRepeatedAttributes)
attrs[1].SetKey(attr1.Key() + "smth")
c.SetAttributes(attrs)
require.NoError(t, CheckFormat(c))
}

View file

@ -14,7 +14,7 @@ import (
// Container groups information about the NeoFS container stored in the NeoFS network.
type Container struct {
// Container structure.
Value *container.Container
Value container.Container
// Signature of the Value.
Signature neofscrypto.Signature