2021-11-08 10:04:45 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/checksum"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
sessiontest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session/test"
|
|
|
|
usertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user/test"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/version"
|
2021-11-08 10:04:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
func randID(t *testing.T) oid.ID {
|
|
|
|
var id oid.ID
|
2021-11-08 10:04:45 +00:00
|
|
|
id.SetSHA256(randSHA256Checksum(t))
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func randSHA256Checksum(t *testing.T) (cs [sha256.Size]byte) {
|
|
|
|
_, err := rand.Read(cs[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func randTZChecksum(t *testing.T) (cs [64]byte) {
|
|
|
|
_, err := rand.Read(cs[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetID(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
id := randID(t)
|
|
|
|
|
|
|
|
obj.SetID(id)
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
oID, set := obj.ID()
|
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, id, oID)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetPayload(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
payload := make([]byte, 10)
|
|
|
|
_, _ = rand.Read(payload)
|
|
|
|
|
|
|
|
obj.SetPayload(payload)
|
|
|
|
|
|
|
|
require.Equal(t, payload, obj.Payload())
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetVersion(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-03-23 15:35:44 +00:00
|
|
|
var ver version.Version
|
2021-11-08 10:04:45 +00:00
|
|
|
ver.SetMajor(1)
|
|
|
|
ver.SetMinor(2)
|
|
|
|
|
2022-03-23 15:35:44 +00:00
|
|
|
obj.SetVersion(&ver)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-03-23 15:35:44 +00:00
|
|
|
require.Equal(t, ver, *obj.Version())
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetPayloadSize(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
sz := uint64(133)
|
|
|
|
obj.SetPayloadSize(sz)
|
|
|
|
|
|
|
|
require.Equal(t, sz, obj.PayloadSize())
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetContainerID(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2021-11-12 11:19:16 +00:00
|
|
|
cid := cidtest.ID()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
obj.SetContainerID(cid)
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
cID, set := obj.ContainerID()
|
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, cid, cID)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetOwnerID(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 06:30:22 +00:00
|
|
|
ownerID := usertest.ID()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
obj.SetOwnerID(ownerID)
|
|
|
|
|
|
|
|
require.Equal(t, ownerID, obj.OwnerID())
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetCreationEpoch(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
creat := uint64(228)
|
2022-03-03 11:48:55 +00:00
|
|
|
obj.SetCreationEpoch(creat)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
require.Equal(t, creat, obj.CreationEpoch())
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetPayloadChecksum(t *testing.T) {
|
|
|
|
obj := New()
|
2021-03-22 10:56:18 +00:00
|
|
|
var cs checksum.Checksum
|
2021-11-08 10:04:45 +00:00
|
|
|
cs.SetSHA256(randSHA256Checksum(t))
|
|
|
|
|
2021-03-04 11:38:23 +00:00
|
|
|
obj.SetPayloadChecksum(cs)
|
2022-04-12 13:08:09 +00:00
|
|
|
cs2, set := obj.PayloadChecksum()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-12 13:08:09 +00:00
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, cs, cs2)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetPayloadHomomorphicHash(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2021-03-22 10:56:18 +00:00
|
|
|
var cs checksum.Checksum
|
2021-11-08 10:04:45 +00:00
|
|
|
cs.SetTillichZemor(randTZChecksum(t))
|
|
|
|
|
2021-03-04 11:38:23 +00:00
|
|
|
obj.SetPayloadHomomorphicHash(cs)
|
2022-04-12 13:08:09 +00:00
|
|
|
cs2, set := obj.PayloadHomomorphicHash()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-12 13:08:09 +00:00
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, cs, cs2)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetAttributes(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
a1 := NewAttribute()
|
|
|
|
a1.SetKey("key1")
|
|
|
|
a1.SetValue("val1")
|
|
|
|
|
|
|
|
a2 := NewAttribute()
|
|
|
|
a2.SetKey("key2")
|
|
|
|
a2.SetValue("val2")
|
|
|
|
|
2022-03-11 09:00:54 +00:00
|
|
|
obj.SetAttributes(*a1, *a2)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-03-11 09:00:54 +00:00
|
|
|
require.Equal(t, []Attribute{*a1, *a2}, obj.Attributes())
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetPreviousID(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
prev := randID(t)
|
|
|
|
|
|
|
|
obj.SetPreviousID(prev)
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
oID, set := obj.PreviousID()
|
|
|
|
|
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, prev, oID)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetChildren(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
id1 := randID(t)
|
|
|
|
id2 := randID(t)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
obj.SetChildren(id1, id2)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.Equal(t, []oid.ID{id1, id2}, obj.Children())
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetSplitID(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
require.Nil(t, obj.SplitID())
|
|
|
|
|
|
|
|
splitID := NewSplitID()
|
|
|
|
obj.SetSplitID(splitID)
|
|
|
|
|
|
|
|
require.Equal(t, obj.SplitID(), splitID)
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetParent(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
require.Nil(t, obj.Parent())
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
par := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
par.SetID(randID(t))
|
2021-11-12 11:19:16 +00:00
|
|
|
par.SetContainerID(cidtest.ID())
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
obj.SetParent(par)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
require.Equal(t, par, obj.Parent())
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_ToV2(t *testing.T) {
|
2021-11-08 10:04:45 +00:00
|
|
|
objV2 := new(object.Object)
|
|
|
|
objV2.SetPayload([]byte{1, 2, 3})
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
obj := NewFromV2(objV2)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
require.Equal(t, objV2, obj.ToV2())
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetSessionToken(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
tok := sessiontest.ObjectSigned()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
obj.SetSessionToken(tok)
|
|
|
|
|
|
|
|
require.Equal(t, tok, obj.SessionToken())
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetType(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
typ := TypeStorageGroup
|
|
|
|
|
|
|
|
obj.SetType(typ)
|
|
|
|
|
|
|
|
require.Equal(t, typ, obj.Type())
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_CutPayload(t *testing.T) {
|
|
|
|
o1 := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
p1 := []byte{12, 3}
|
|
|
|
o1.SetPayload(p1)
|
|
|
|
|
|
|
|
sz := uint64(13)
|
|
|
|
o1.SetPayloadSize(sz)
|
|
|
|
|
|
|
|
o2 := o1.CutPayload()
|
|
|
|
|
|
|
|
require.Equal(t, sz, o2.PayloadSize())
|
|
|
|
require.Empty(t, o2.Payload())
|
|
|
|
|
|
|
|
sz++
|
|
|
|
o1.SetPayloadSize(sz)
|
|
|
|
|
|
|
|
require.Equal(t, sz, o1.PayloadSize())
|
|
|
|
require.Equal(t, sz, o2.PayloadSize())
|
|
|
|
|
|
|
|
p2 := []byte{4, 5, 6}
|
|
|
|
o2.SetPayload(p2)
|
|
|
|
|
|
|
|
require.Equal(t, p2, o2.Payload())
|
|
|
|
require.Equal(t, p1, o1.Payload())
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_SetParentID(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
id := randID(t)
|
2022-03-03 11:48:55 +00:00
|
|
|
obj.SetParentID(id)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
oID, set := obj.ParentID()
|
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, id, oID)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_ResetRelations(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
obj.SetPreviousID(randID(t))
|
|
|
|
|
|
|
|
obj.ResetRelations()
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
_, set := obj.PreviousID()
|
|
|
|
require.False(t, set)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObject_HasParent(t *testing.T) {
|
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
obj.InitRelations()
|
|
|
|
|
|
|
|
require.True(t, obj.HasParent())
|
|
|
|
|
|
|
|
obj.ResetRelations()
|
|
|
|
|
|
|
|
require.False(t, obj.HasParent())
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func TestObjectEncoding(t *testing.T) {
|
|
|
|
o := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
o.SetID(randID(t))
|
2022-04-12 16:23:16 +00:00
|
|
|
o.SetContainerID(cidtest.ID())
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data, err := o.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
o2 := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
require.NoError(t, o2.Unmarshal(data))
|
|
|
|
|
|
|
|
require.Equal(t, o, o2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := o.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
o2 := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
require.NoError(t, o2.UnmarshalJSON(data))
|
|
|
|
|
|
|
|
require.Equal(t, o, o2)
|
|
|
|
})
|
|
|
|
}
|