2021-11-08 12:25:13 +00:00
|
|
|
package container_test
|
|
|
|
|
|
|
|
import (
|
2022-06-25 09:54:42 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"strconv"
|
2021-11-08 12:25:13 +00:00
|
|
|
"testing"
|
2022-06-25 09:54:42 +00:00
|
|
|
"time"
|
2021-11-08 12:25:13 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-06-25 09:54:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
v2container "github.com/nspcc-dev/neofs-api-go/v2/container"
|
|
|
|
v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
2022-03-23 15:35:44 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-11-08 12:25:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
2022-06-25 09:54:42 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
2021-11-08 12:25:13 +00:00
|
|
|
containertest "github.com/nspcc-dev/neofs-sdk-go/container/test"
|
2022-06-25 09:54:42 +00:00
|
|
|
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
|
2021-11-08 12:25:13 +00:00
|
|
|
netmaptest "github.com/nspcc-dev/neofs-sdk-go/netmap/test"
|
2022-06-25 09:54:42 +00:00
|
|
|
subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id"
|
|
|
|
subnetidtest "github.com/nspcc-dev/neofs-sdk-go/subnet/id/test"
|
2022-04-11 06:30:22 +00:00
|
|
|
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
|
2022-02-09 12:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/version"
|
2021-11-08 12:25:13 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
func TestPlacementPolicyEncoding(t *testing.T) {
|
|
|
|
v := containertest.Container()
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
var v2 container.Container
|
|
|
|
require.NoError(t, v2.Unmarshal(v.Marshal()))
|
|
|
|
|
|
|
|
require.Equal(t, v, v2)
|
|
|
|
})
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := v.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
var v2 container.Container
|
|
|
|
require.NoError(t, v2.UnmarshalJSON(data))
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
require.Equal(t, v, v2)
|
|
|
|
})
|
|
|
|
}
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
func TestContainer_Init(t *testing.T) {
|
|
|
|
val := containertest.Container()
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
val.Init()
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
var msg v2container.Container
|
|
|
|
val.WriteToV2(&msg)
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
binNonce := msg.GetNonce()
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
var nonce uuid.UUID
|
|
|
|
require.NoError(t, nonce.UnmarshalBinary(binNonce))
|
|
|
|
require.EqualValues(t, 4, nonce.Version())
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
verV2 := msg.GetVersion()
|
|
|
|
require.NotNil(t, verV2)
|
|
|
|
|
|
|
|
var ver version.Version
|
2022-07-07 10:51:05 +00:00
|
|
|
require.NoError(t, ver.ReadFromV2(*verV2))
|
2022-06-25 09:54:42 +00:00
|
|
|
|
|
|
|
require.Equal(t, version.Current(), ver)
|
|
|
|
|
|
|
|
var val2 container.Container
|
|
|
|
require.NoError(t, val2.ReadFromV2(msg))
|
|
|
|
|
|
|
|
require.Equal(t, val, val2)
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
func TestContainer_Owner(t *testing.T) {
|
|
|
|
var val container.Container
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
require.Zero(t, val.Owner())
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
val = containertest.Container()
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
owner := *usertest.ID()
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
val.SetOwner(owner)
|
|
|
|
|
|
|
|
var msg v2container.Container
|
|
|
|
val.WriteToV2(&msg)
|
|
|
|
|
|
|
|
var msgOwner refs.OwnerID
|
|
|
|
owner.WriteToV2(&msgOwner)
|
|
|
|
|
|
|
|
require.Equal(t, &msgOwner, msg.GetOwnerID())
|
|
|
|
|
|
|
|
var val2 container.Container
|
|
|
|
require.NoError(t, val2.ReadFromV2(msg))
|
|
|
|
|
|
|
|
require.True(t, val2.Owner().Equals(owner))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainer_BasicACL(t *testing.T) {
|
|
|
|
var val container.Container
|
|
|
|
|
|
|
|
require.Zero(t, val.BasicACL())
|
|
|
|
|
|
|
|
val = containertest.Container()
|
|
|
|
|
|
|
|
basicACL := containertest.BasicACL()
|
|
|
|
val.SetBasicACL(basicACL)
|
|
|
|
|
|
|
|
var msg v2container.Container
|
|
|
|
val.WriteToV2(&msg)
|
|
|
|
|
|
|
|
require.EqualValues(t, basicACL.Bits(), msg.GetBasicACL())
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
var val2 container.Container
|
|
|
|
require.NoError(t, val2.ReadFromV2(msg))
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
require.Equal(t, basicACL, val2.BasicACL())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainer_PlacementPolicy(t *testing.T) {
|
|
|
|
var val container.Container
|
|
|
|
|
|
|
|
require.Zero(t, val.PlacementPolicy())
|
|
|
|
|
|
|
|
val = containertest.Container()
|
|
|
|
|
|
|
|
pp := netmaptest.PlacementPolicy()
|
|
|
|
val.SetPlacementPolicy(pp)
|
|
|
|
|
|
|
|
var msgPolicy v2netmap.PlacementPolicy
|
|
|
|
pp.WriteToV2(&msgPolicy)
|
|
|
|
|
|
|
|
var msg v2container.Container
|
|
|
|
val.WriteToV2(&msg)
|
|
|
|
|
|
|
|
require.Equal(t, &msgPolicy, msg.GetPlacementPolicy())
|
|
|
|
|
|
|
|
var val2 container.Container
|
|
|
|
require.NoError(t, val2.ReadFromV2(msg))
|
|
|
|
|
|
|
|
require.Equal(t, pp, val2.PlacementPolicy())
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertContainsAttribute(t *testing.T, m v2container.Container, key, val string) {
|
|
|
|
var msgAttr v2container.Attribute
|
|
|
|
|
|
|
|
msgAttr.SetKey(key)
|
|
|
|
msgAttr.SetValue(val)
|
|
|
|
require.Contains(t, m.GetAttributes(), msgAttr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainer_Attribute(t *testing.T) {
|
|
|
|
const attrKey1, attrKey2 = "key1", "key2"
|
|
|
|
const attrVal1, attrVal2 = "val1", "val2"
|
|
|
|
|
|
|
|
val := containertest.Container()
|
|
|
|
|
|
|
|
val.SetAttribute(attrKey1, attrVal1)
|
|
|
|
val.SetAttribute(attrKey2, attrVal2)
|
|
|
|
|
|
|
|
var msg v2container.Container
|
|
|
|
val.WriteToV2(&msg)
|
|
|
|
|
|
|
|
require.GreaterOrEqual(t, len(msg.GetAttributes()), 2)
|
|
|
|
assertContainsAttribute(t, msg, attrKey1, attrVal1)
|
|
|
|
assertContainsAttribute(t, msg, attrKey2, attrVal2)
|
|
|
|
|
|
|
|
var val2 container.Container
|
|
|
|
require.NoError(t, val2.ReadFromV2(msg))
|
|
|
|
|
|
|
|
require.Equal(t, attrVal1, val2.Attribute(attrKey1))
|
|
|
|
require.Equal(t, attrVal2, val2.Attribute(attrKey2))
|
|
|
|
|
|
|
|
m := map[string]string{}
|
|
|
|
|
|
|
|
val2.IterateAttributes(func(key, val string) {
|
|
|
|
m[key] = val
|
2021-11-08 12:25:13 +00:00
|
|
|
})
|
2022-06-25 09:54:42 +00:00
|
|
|
|
|
|
|
require.GreaterOrEqual(t, len(m), 2)
|
|
|
|
require.Equal(t, attrVal1, m[attrKey1])
|
|
|
|
require.Equal(t, attrVal2, m[attrKey2])
|
|
|
|
|
|
|
|
val2.SetAttribute(attrKey1, attrVal1+"_")
|
|
|
|
require.Equal(t, attrVal1+"_", val2.Attribute(attrKey1))
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
func TestSetName(t *testing.T) {
|
|
|
|
var val container.Container
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
require.Panics(t, func() {
|
|
|
|
container.SetName(&val, "")
|
2021-11-08 12:25:13 +00:00
|
|
|
})
|
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
val = containertest.Container()
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
const name = "some name"
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
container.SetName(&val, name)
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
var msg v2container.Container
|
|
|
|
val.WriteToV2(&msg)
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
assertContainsAttribute(t, msg, "Name", name)
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
var val2 container.Container
|
|
|
|
require.NoError(t, val2.ReadFromV2(msg))
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
require.Equal(t, name, container.Name(val2))
|
|
|
|
}
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
func TestSetCreationTime(t *testing.T) {
|
|
|
|
var val container.Container
|
2021-11-08 12:25:13 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
require.Zero(t, container.CreatedAt(val).Unix())
|
2022-03-23 15:35:44 +00:00
|
|
|
|
2022-06-25 09:54:42 +00:00
|
|
|
val = containertest.Container()
|
|
|
|
|
|
|
|
creat := time.Now()
|
|
|
|
|
|
|
|
container.SetCreationTime(&val, creat)
|
|
|
|
|
|
|
|
var msg v2container.Container
|
|
|
|
val.WriteToV2(&msg)
|
|
|
|
|
|
|
|
assertContainsAttribute(t, msg, "Timestamp", strconv.FormatInt(creat.Unix(), 10))
|
|
|
|
|
|
|
|
var val2 container.Container
|
|
|
|
require.NoError(t, val2.ReadFromV2(msg))
|
|
|
|
|
|
|
|
require.Equal(t, creat.Unix(), container.CreatedAt(val2).Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetSubnet(t *testing.T) {
|
|
|
|
var val container.Container
|
|
|
|
|
|
|
|
require.True(t, subnetid.IsZero(container.Subnet(val)))
|
|
|
|
|
|
|
|
val = containertest.Container()
|
|
|
|
|
|
|
|
sub := subnetidtest.ID()
|
|
|
|
|
|
|
|
container.SetSubnet(&val, sub)
|
|
|
|
|
|
|
|
var msg v2container.Container
|
|
|
|
val.WriteToV2(&msg)
|
|
|
|
|
|
|
|
assertContainsAttribute(t, msg, v2container.SysAttributeSubnet, sub.EncodeToString())
|
|
|
|
|
|
|
|
var val2 container.Container
|
|
|
|
require.NoError(t, val2.ReadFromV2(msg))
|
|
|
|
|
|
|
|
require.Equal(t, sub, container.Subnet(val))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDisableHomomorphicHashing(t *testing.T) {
|
|
|
|
var val container.Container
|
|
|
|
|
|
|
|
require.False(t, container.IsHomomorphicHashingDisabled(val))
|
|
|
|
|
|
|
|
val = containertest.Container()
|
|
|
|
|
|
|
|
container.DisableHomomorphicHashing(&val)
|
|
|
|
|
|
|
|
var msg v2container.Container
|
|
|
|
val.WriteToV2(&msg)
|
|
|
|
|
|
|
|
assertContainsAttribute(t, msg, v2container.SysAttributePrefix+"DISABLE_HOMOMORPHIC_HASHING", "true")
|
|
|
|
|
|
|
|
var val2 container.Container
|
|
|
|
require.NoError(t, val2.ReadFromV2(msg))
|
|
|
|
|
|
|
|
require.True(t, container.IsHomomorphicHashingDisabled(val2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteDomain(t *testing.T) {
|
|
|
|
var val container.Container
|
|
|
|
|
|
|
|
require.Zero(t, container.ReadDomain(val).Name())
|
|
|
|
|
|
|
|
val = containertest.Container()
|
|
|
|
|
|
|
|
const name = "domain name"
|
|
|
|
|
|
|
|
var d container.Domain
|
|
|
|
d.SetName(name)
|
|
|
|
|
|
|
|
container.WriteDomain(&val, d)
|
|
|
|
|
|
|
|
var msg v2container.Container
|
|
|
|
val.WriteToV2(&msg)
|
|
|
|
|
|
|
|
assertContainsAttribute(t, msg, v2container.SysAttributeName, name)
|
|
|
|
assertContainsAttribute(t, msg, v2container.SysAttributeZone, "container")
|
|
|
|
|
|
|
|
const zone = "domain zone"
|
|
|
|
|
|
|
|
d.SetZone(zone)
|
|
|
|
|
|
|
|
container.WriteDomain(&val, d)
|
|
|
|
|
|
|
|
val.WriteToV2(&msg)
|
|
|
|
|
|
|
|
assertContainsAttribute(t, msg, v2container.SysAttributeZone, zone)
|
|
|
|
|
|
|
|
var val2 container.Container
|
|
|
|
require.NoError(t, val2.ReadFromV2(msg))
|
|
|
|
|
|
|
|
require.Equal(t, d, container.ReadDomain(val2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCalculateID(t *testing.T) {
|
|
|
|
val := containertest.Container()
|
|
|
|
|
|
|
|
require.False(t, container.AssertID(cidtest.ID(), val))
|
|
|
|
|
|
|
|
var id cid.ID
|
|
|
|
container.CalculateID(&id, val)
|
|
|
|
|
|
|
|
var msg refs.ContainerID
|
|
|
|
id.WriteToV2(&msg)
|
|
|
|
|
|
|
|
h := sha256.Sum256(val.Marshal())
|
|
|
|
require.Equal(t, h[:], msg.GetValue())
|
|
|
|
|
|
|
|
var id2 cid.ID
|
|
|
|
require.NoError(t, id2.ReadFromV2(msg))
|
|
|
|
|
|
|
|
require.True(t, container.AssertID(id2, val))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCalculateSignature(t *testing.T) {
|
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
val := containertest.Container()
|
|
|
|
|
|
|
|
var sig neofscrypto.Signature
|
|
|
|
|
|
|
|
require.NoError(t, container.CalculateSignature(&sig, val, key.PrivateKey))
|
|
|
|
|
|
|
|
var msg refs.Signature
|
|
|
|
sig.WriteToV2(&msg)
|
|
|
|
|
|
|
|
var sig2 neofscrypto.Signature
|
2022-07-07 10:51:05 +00:00
|
|
|
require.NoError(t, sig2.ReadFromV2(msg))
|
2022-06-25 09:54:42 +00:00
|
|
|
|
|
|
|
require.True(t, container.VerifySignature(sig2, val))
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|