2021-11-08 10:52:44 +00:00
|
|
|
package storagegroup_test
|
|
|
|
|
|
|
|
import (
|
2021-03-22 15:04:16 +00:00
|
|
|
"crypto/sha256"
|
2022-05-17 19:15:21 +00:00
|
|
|
"strconv"
|
2021-11-08 10:52:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-05-17 19:15:21 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2021-03-22 15:04:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-11-08 10:52:44 +00:00
|
|
|
storagegroupV2 "github.com/nspcc-dev/neofs-api-go/v2/storagegroup"
|
2021-03-22 15:04:16 +00:00
|
|
|
storagegroupV2test "github.com/nspcc-dev/neofs-api-go/v2/storagegroup/test"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/checksum"
|
2021-11-08 10:52:44 +00:00
|
|
|
checksumtest "github.com/nspcc-dev/neofs-sdk-go/checksum/test"
|
2022-05-17 19:15:21 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-25 16:20:32 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-02-01 10:15:12 +00:00
|
|
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
2021-11-08 10:52:44 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/storagegroup"
|
|
|
|
storagegrouptest "github.com/nspcc-dev/neofs-sdk-go/storagegroup/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStorageGroup(t *testing.T) {
|
2021-03-22 15:04:16 +00:00
|
|
|
var sg storagegroup.StorageGroup
|
2021-11-08 10:52:44 +00:00
|
|
|
|
|
|
|
sz := uint64(13)
|
|
|
|
sg.SetValidationDataSize(sz)
|
|
|
|
require.Equal(t, sz, sg.ValidationDataSize())
|
|
|
|
|
|
|
|
cs := checksumtest.Checksum()
|
|
|
|
sg.SetValidationDataHash(cs)
|
2022-04-12 13:08:09 +00:00
|
|
|
cs2, set := sg.ValidationDataHash()
|
|
|
|
|
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, cs, cs2)
|
2021-11-08 10:52:44 +00:00
|
|
|
|
|
|
|
exp := uint64(33)
|
|
|
|
sg.SetExpirationEpoch(exp)
|
|
|
|
require.Equal(t, exp, sg.ExpirationEpoch())
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
members := []oid.ID{oidtest.ID(), oidtest.ID()}
|
2021-11-08 10:52:44 +00:00
|
|
|
sg.SetMembers(members)
|
|
|
|
require.Equal(t, members, sg.Members())
|
|
|
|
}
|
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
func TestStorageGroup_ReadFromV2(t *testing.T) {
|
|
|
|
t.Run("from zero", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
x storagegroup.StorageGroup
|
|
|
|
v2 storagegroupV2.StorageGroup
|
|
|
|
)
|
|
|
|
|
2022-07-07 10:51:05 +00:00
|
|
|
require.Error(t, x.ReadFromV2(v2))
|
2021-03-22 15:04:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("from non-zero", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
x storagegroup.StorageGroup
|
|
|
|
v2 = storagegroupV2test.GenerateStorageGroup(false)
|
|
|
|
)
|
|
|
|
|
|
|
|
// https://github.com/nspcc-dev/neofs-api-go/issues/394
|
|
|
|
v2.SetMembers(generateOIDList())
|
|
|
|
|
|
|
|
size := v2.GetValidationDataSize()
|
|
|
|
epoch := v2.GetExpirationEpoch()
|
|
|
|
mm := v2.GetMembers()
|
|
|
|
hashV2 := v2.GetValidationHash()
|
|
|
|
|
2022-07-07 10:51:05 +00:00
|
|
|
require.NoError(t, x.ReadFromV2(*v2))
|
2021-03-22 15:04:16 +00:00
|
|
|
|
|
|
|
require.Equal(t, epoch, x.ExpirationEpoch())
|
|
|
|
require.Equal(t, size, x.ValidationDataSize())
|
|
|
|
|
|
|
|
var hash checksum.Checksum
|
2022-07-07 10:51:05 +00:00
|
|
|
require.NoError(t, hash.ReadFromV2(*hashV2))
|
2021-03-22 15:04:16 +00:00
|
|
|
h, set := x.ValidationDataHash()
|
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, hash, h)
|
|
|
|
|
|
|
|
var oidV2 refs.ObjectID
|
|
|
|
|
|
|
|
for i, m := range mm {
|
|
|
|
x.Members()[i].WriteToV2(&oidV2)
|
|
|
|
require.Equal(t, m, oidV2)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-08 10:52:44 +00:00
|
|
|
func TestStorageGroupEncoding(t *testing.T) {
|
2021-11-12 11:19:16 +00:00
|
|
|
sg := storagegrouptest.StorageGroup()
|
2021-11-08 10:52:44 +00:00
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data, err := sg.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
var sg2 storagegroup.StorageGroup
|
2021-11-08 10:52:44 +00:00
|
|
|
require.NoError(t, sg2.Unmarshal(data))
|
|
|
|
|
|
|
|
require.Equal(t, sg, sg2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := sg.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
var sg2 storagegroup.StorageGroup
|
2021-11-08 10:52:44 +00:00
|
|
|
require.NoError(t, sg2.UnmarshalJSON(data))
|
|
|
|
|
|
|
|
require.Equal(t, sg, sg2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
func TestStorageGroup_WriteToV2(t *testing.T) {
|
|
|
|
t.Run("zero to v2", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
x storagegroup.StorageGroup
|
|
|
|
v2 storagegroupV2.StorageGroup
|
|
|
|
)
|
|
|
|
|
|
|
|
x.WriteToV2(&v2)
|
2021-11-08 10:52:44 +00:00
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
require.Nil(t, v2.GetValidationHash())
|
|
|
|
require.Nil(t, v2.GetMembers())
|
|
|
|
require.Zero(t, v2.GetValidationDataSize())
|
|
|
|
require.Zero(t, v2.GetExpirationEpoch())
|
2021-11-08 10:52:44 +00:00
|
|
|
})
|
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
t.Run("non-zero to v2", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
x = storagegrouptest.StorageGroup()
|
|
|
|
v2 storagegroupV2.StorageGroup
|
|
|
|
)
|
|
|
|
|
|
|
|
x.WriteToV2(&v2)
|
|
|
|
|
|
|
|
require.Equal(t, x.ExpirationEpoch(), v2.GetExpirationEpoch())
|
|
|
|
require.Equal(t, x.ValidationDataSize(), v2.GetValidationDataSize())
|
|
|
|
|
|
|
|
var hash checksum.Checksum
|
2022-07-07 10:51:05 +00:00
|
|
|
require.NoError(t, hash.ReadFromV2(*v2.GetValidationHash()))
|
2021-03-22 15:04:16 +00:00
|
|
|
|
|
|
|
h, set := x.ValidationDataHash()
|
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, h, hash)
|
2021-11-08 10:52:44 +00:00
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
var oidV2 refs.ObjectID
|
|
|
|
|
|
|
|
for i, m := range x.Members() {
|
|
|
|
m.WriteToV2(&oidV2)
|
|
|
|
require.Equal(t, oidV2, v2.GetMembers()[i])
|
|
|
|
}
|
2021-11-08 10:52:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
t.Run("default values", func(t *testing.T) {
|
2021-03-22 15:04:16 +00:00
|
|
|
var sg storagegroup.StorageGroup
|
2021-11-08 10:52:44 +00:00
|
|
|
|
|
|
|
// check initial values
|
|
|
|
require.Nil(t, sg.Members())
|
2022-04-12 13:08:09 +00:00
|
|
|
_, set := sg.ValidationDataHash()
|
|
|
|
require.False(t, set)
|
2021-11-08 10:52:44 +00:00
|
|
|
require.Zero(t, sg.ExpirationEpoch())
|
|
|
|
require.Zero(t, sg.ValidationDataSize())
|
2021-03-22 15:04:16 +00:00
|
|
|
})
|
|
|
|
}
|
2021-11-08 10:52:44 +00:00
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
func generateOIDList() []refs.ObjectID {
|
|
|
|
const size = 3
|
2021-11-08 10:52:44 +00:00
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
mmV2 := make([]refs.ObjectID, size)
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
oidV2 := make([]byte, sha256.Size)
|
|
|
|
oidV2[i] = byte(i)
|
|
|
|
|
|
|
|
mmV2[i].SetValue(oidV2)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mmV2
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
2022-06-07 17:34:46 +00:00
|
|
|
|
|
|
|
func TestStorageGroup_SetMembers_DoubleSetting(t *testing.T) {
|
|
|
|
var sg storagegroup.StorageGroup
|
|
|
|
|
|
|
|
mm := []oid.ID{oidtest.ID(), oidtest.ID(), oidtest.ID()} // cap is 3 at least
|
2023-05-16 08:38:27 +00:00
|
|
|
require.NotPanics(t, func() {
|
|
|
|
sg.SetMembers(mm)
|
|
|
|
})
|
2022-06-07 17:34:46 +00:00
|
|
|
|
2023-05-16 08:38:27 +00:00
|
|
|
require.NotPanics(t, func() {
|
|
|
|
// the previous cap is more that a new length;
|
|
|
|
// slicing should not lead to `out of range`
|
|
|
|
// and apply update correctly
|
|
|
|
sg.SetMembers(mm[:1])
|
|
|
|
})
|
2022-06-07 17:34:46 +00:00
|
|
|
}
|
2022-05-17 19:15:21 +00:00
|
|
|
|
|
|
|
func TestStorageGroupFromObject(t *testing.T) {
|
|
|
|
sg := storagegrouptest.StorageGroup()
|
|
|
|
|
|
|
|
var o objectSDK.Object
|
|
|
|
|
|
|
|
var expAttr objectSDK.Attribute
|
|
|
|
expAttr.SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
expAttr.SetValue(strconv.FormatUint(sg.ExpirationEpoch(), 10))
|
|
|
|
|
|
|
|
sgRaw, err := sg.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
o.SetPayload(sgRaw)
|
|
|
|
o.SetType(objectSDK.TypeStorageGroup)
|
|
|
|
|
|
|
|
t.Run("correct object", func(t *testing.T) {
|
|
|
|
o.SetAttributes(objectSDK.Attribute{}, expAttr, objectSDK.Attribute{})
|
|
|
|
|
|
|
|
var sg2 storagegroup.StorageGroup
|
|
|
|
require.NoError(t, storagegroup.ReadFromObject(&sg2, o))
|
|
|
|
require.Equal(t, sg, sg2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("incorrect exp attr", func(t *testing.T) {
|
|
|
|
var sg2 storagegroup.StorageGroup
|
|
|
|
|
|
|
|
expAttr.SetValue(strconv.FormatUint(sg.ExpirationEpoch()+1, 10))
|
|
|
|
o.SetAttributes(expAttr)
|
|
|
|
|
|
|
|
require.Error(t, storagegroup.ReadFromObject(&sg2, o))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("incorrect object type", func(t *testing.T) {
|
|
|
|
var sg2 storagegroup.StorageGroup
|
|
|
|
|
|
|
|
o.SetType(objectSDK.TypeTombstone)
|
|
|
|
require.Error(t, storagegroup.ReadFromObject(&sg2, o))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageGroupToObject(t *testing.T) {
|
|
|
|
sg := storagegrouptest.StorageGroup()
|
|
|
|
|
|
|
|
sgRaw, err := sg.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("empty object", func(t *testing.T) {
|
|
|
|
var o objectSDK.Object
|
|
|
|
storagegroup.WriteToObject(sg, &o)
|
|
|
|
|
|
|
|
exp, found := expFromObj(t, o)
|
|
|
|
require.True(t, found)
|
|
|
|
|
|
|
|
require.Equal(t, sgRaw, o.Payload())
|
|
|
|
require.Equal(t, sg.ExpirationEpoch(), exp)
|
|
|
|
require.Equal(t, objectSDK.TypeStorageGroup, o.Type())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("obj already has exp attr", func(t *testing.T) {
|
|
|
|
var o objectSDK.Object
|
|
|
|
|
|
|
|
var attr objectSDK.Attribute
|
|
|
|
attr.SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
attr.SetValue(strconv.FormatUint(sg.ExpirationEpoch()+1, 10))
|
|
|
|
|
|
|
|
o.SetAttributes(objectSDK.Attribute{}, attr, objectSDK.Attribute{})
|
|
|
|
|
|
|
|
storagegroup.WriteToObject(sg, &o)
|
|
|
|
|
|
|
|
exp, found := expFromObj(t, o)
|
|
|
|
require.True(t, found)
|
|
|
|
|
|
|
|
require.Equal(t, sgRaw, o.Payload())
|
|
|
|
require.Equal(t, sg.ExpirationEpoch(), exp)
|
|
|
|
require.Equal(t, objectSDK.TypeStorageGroup, o.Type())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func expFromObj(t *testing.T, o objectSDK.Object) (uint64, bool) {
|
|
|
|
for _, attr := range o.Attributes() {
|
|
|
|
if attr.Key() == objectV2.SysAttributeExpEpoch {
|
|
|
|
exp, err := strconv.ParseUint(attr.Value(), 10, 64)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return exp, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, false
|
|
|
|
}
|