frostfs-node/pkg/local_object_storage/internal/testutil/object.go

76 lines
1.9 KiB
Go
Raw Normal View History

package testutil
import (
"crypto/sha256"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/checksum"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
usertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user/test"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/version"
"git.frostfs.info/TrueCloudLab/tzhash/tz"
"golang.org/x/exp/rand"
)
const defaultDataSize = 32
func GenerateObject() *objectSDK.Object {
return GenerateObjectWithCID(cidtest.ID())
}
func GenerateObjectWithSize(sz int) *objectSDK.Object {
data := make([]byte, sz)
_, _ = rand.Read(data)
return GenerateObjectWithCIDWithPayload(cidtest.ID(), data)
}
func GenerateObjectWithCID(cnr cid.ID) *objectSDK.Object {
data := make([]byte, defaultDataSize)
_, _ = rand.Read(data)
return GenerateObjectWithCIDWithPayload(cnr, data)
}
func GenerateObjectWithCIDWithPayload(cnr cid.ID, data []byte) *objectSDK.Object {
var ver version.Version
ver.SetMajor(2)
ver.SetMinor(1)
var csum checksum.Checksum
csum.SetSHA256(sha256.Sum256(data))
var csumTZ checksum.Checksum
csumTZ.SetTillichZemor(tz.Sum(csum.Value()))
obj := objectSDK.New()
obj.SetID(oidtest.ID())
obj.SetOwnerID(usertest.ID())
obj.SetContainerID(cnr)
obj.SetVersion(&ver)
obj.SetPayload(data)
obj.SetPayloadSize(uint64(len(data)))
obj.SetPayloadChecksum(csum)
obj.SetPayloadHomomorphicHash(csumTZ)
return obj
}
func AddAttribute(obj *objectSDK.Object, key, val string) {
var attr objectSDK.Attribute
attr.SetKey(key)
attr.SetValue(val)
attrs := obj.Attributes()
attrs = append(attrs, attr)
obj.SetAttributes(attrs...)
}
func AddPayload(obj *objectSDK.Object, size int) {
buf := make([]byte, size)
_, _ = rand.Read(buf)
obj.SetPayload(buf)
obj.SetPayloadSize(uint64(size))
}