frostfs-api-go/refs/test/generate.go
Aleksey Savchuk 9c5e32a183
[#106] test: Generate correct data for tests
Some tests are using invalid data that do not meet the requirements of the
FrostFS protocol, e.g.
- Container/Object IDs aren't 32 bytes long
- Signature key and sign have invalid length
- Split IDs aren't valid UUIDv4
and etc.

Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
2024-08-30 13:37:29 +03:00

127 lines
1.8 KiB
Go

package refstest
import (
crand "crypto/rand"
"crypto/sha256"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
)
func GenerateVersion(empty bool) *refs.Version {
m := new(refs.Version)
if !empty {
m.SetMajor(2)
m.SetMinor(1)
}
return m
}
func GenerateOwnerID(empty bool) *refs.OwnerID {
m := new(refs.OwnerID)
if !empty {
id := make([]byte, 25)
_, _ = crand.Read(id)
m.SetValue(id)
}
return m
}
func GenerateAddress(empty bool) *refs.Address {
m := new(refs.Address)
if !empty {
m.SetObjectID(GenerateObjectID(false))
m.SetContainerID(GenerateContainerID(false))
}
return m
}
func GenerateObjectID(empty bool) *refs.ObjectID {
m := new(refs.ObjectID)
if !empty {
id := make([]byte, sha256.Size)
_, _ = crand.Read(id)
m.SetValue(id)
}
return m
}
func GenerateObjectIDs(empty bool) []refs.ObjectID {
var ids []refs.ObjectID
if !empty {
ids = append(ids,
*GenerateObjectID(false),
*GenerateObjectID(false),
)
}
return ids
}
func GenerateContainerID(empty bool) *refs.ContainerID {
m := new(refs.ContainerID)
if !empty {
id := make([]byte, sha256.Size)
_, _ = crand.Read(id)
m.SetValue(id)
}
return m
}
func GenerateContainerIDs(empty bool) []refs.ContainerID {
var res []refs.ContainerID
if !empty {
res = append(res,
*GenerateContainerID(false),
*GenerateContainerID(false),
)
}
return res
}
func GenerateSignature(empty bool) *refs.Signature {
m := new(refs.Signature)
if !empty {
key := make([]byte, 33)
_, _ = crand.Read(key)
sign := make([]byte, 65)
_, _ = crand.Read(sign)
m.SetScheme(refs.ECDSA_SHA512)
m.SetKey(key)
m.SetSign(sign)
}
return m
}
func GenerateChecksum(empty bool) *refs.Checksum {
m := new(refs.Checksum)
if !empty {
cs := make([]byte, sha256.Size)
_, _ = crand.Read(cs)
m.SetType(refs.SHA256)
m.SetSum(cs)
}
return m
}