2022-06-06 11:09:09 +00:00
|
|
|
package layer
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2022-03-05 08:53:01 +00:00
|
|
|
"time"
|
2022-03-04 13:07:27 +00:00
|
|
|
|
2022-03-04 14:36:30 +00:00
|
|
|
objectv2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2022-06-01 14:50:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/checksum"
|
2022-03-04 13:07:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-04-25 09:57:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2022-03-04 13:07:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestNeoFS struct {
|
2022-06-06 11:09:09 +00:00
|
|
|
NeoFS
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
objects map[string]*object.Object
|
|
|
|
containers map[string]*container.Container
|
|
|
|
currentEpoch uint64
|
|
|
|
}
|
|
|
|
|
2022-05-31 09:10:59 +00:00
|
|
|
const objectSystemAttributeName = "S3-System-name"
|
|
|
|
|
2022-03-04 13:07:27 +00:00
|
|
|
func NewTestNeoFS() *TestNeoFS {
|
|
|
|
return &TestNeoFS{
|
|
|
|
objects: make(map[string]*object.Object),
|
|
|
|
containers: make(map[string]*container.Container),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestNeoFS) CurrentEpoch() uint64 {
|
|
|
|
return t.currentEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestNeoFS) Objects() []*object.Object {
|
|
|
|
res := make([]*object.Object, 0, len(t.objects))
|
|
|
|
|
|
|
|
for _, obj := range t.objects {
|
|
|
|
res = append(res, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestNeoFS) AddObject(key string, obj *object.Object) {
|
|
|
|
t.objects[key] = obj
|
|
|
|
}
|
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
func (t *TestNeoFS) ContainerID(name string) (cid.ID, error) {
|
2022-03-04 13:07:27 +00:00
|
|
|
for id, cnr := range t.containers {
|
2022-06-29 14:43:52 +00:00
|
|
|
if container.Name(*cnr) == name {
|
|
|
|
var cnrID cid.ID
|
2022-06-27 09:08:26 +00:00
|
|
|
return cnrID, cnrID.DecodeString(id)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-27 09:08:26 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("not found")
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
func (t *TestNeoFS) CreateContainer(_ context.Context, prm PrmContainerCreate) (cid.ID, error) {
|
2022-06-29 14:43:52 +00:00
|
|
|
var cnr container.Container
|
|
|
|
cnr.Init()
|
|
|
|
cnr.SetOwner(prm.Creator)
|
|
|
|
cnr.SetPlacementPolicy(prm.Policy)
|
|
|
|
cnr.SetBasicACL(prm.BasicACL)
|
|
|
|
container.SetCreationTime(&cnr, time.Now())
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
if prm.Name != "" {
|
2022-06-30 08:58:51 +00:00
|
|
|
var d container.Domain
|
|
|
|
d.SetName(prm.Name)
|
|
|
|
|
|
|
|
container.WriteDomain(&cnr, d)
|
2022-06-29 14:43:52 +00:00
|
|
|
container.SetName(&cnr, prm.Name)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 14:43:52 +00:00
|
|
|
for i := range prm.AdditionalAttributes {
|
|
|
|
cnr.SetAttribute(prm.AdditionalAttributes[i][0], prm.AdditionalAttributes[i][1])
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, b); err != nil {
|
2022-06-27 09:08:26 +00:00
|
|
|
return cid.ID{}, err
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
var id cid.ID
|
2022-03-04 13:07:27 +00:00
|
|
|
id.SetSHA256(sha256.Sum256(b))
|
2022-06-29 14:43:52 +00:00
|
|
|
t.containers[id.EncodeToString()] = &cnr
|
2022-03-04 13:07:27 +00:00
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
return id, nil
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestNeoFS) Container(_ context.Context, id cid.ID) (*container.Container, error) {
|
|
|
|
for k, v := range t.containers {
|
2022-05-25 17:25:43 +00:00
|
|
|
if k == id.EncodeToString() {
|
2022-03-04 13:07:27 +00:00
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
return nil, fmt.Errorf("container not found %s", id)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
func (t *TestNeoFS) UserContainers(_ context.Context, _ user.ID) ([]cid.ID, error) {
|
2022-03-04 13:07:27 +00:00
|
|
|
var res []cid.ID
|
|
|
|
for k := range t.containers {
|
|
|
|
var idCnr cid.ID
|
2022-04-25 09:57:58 +00:00
|
|
|
if err := idCnr.DecodeString(k); err != nil {
|
2022-03-04 13:07:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = append(res, idCnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
func (t *TestNeoFS) SelectObjects(_ context.Context, prm PrmObjectSelect) ([]oid.ID, error) {
|
2022-03-04 13:07:27 +00:00
|
|
|
filters := object.NewSearchFilters()
|
|
|
|
filters.AddRootFilter()
|
|
|
|
|
|
|
|
if prm.FilePrefix != "" {
|
|
|
|
filters.AddFilter(object.AttributeFileName, prm.FilePrefix, object.MatchCommonPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.ExactAttribute[0] != "" {
|
|
|
|
filters.AddFilter(prm.ExactAttribute[0], prm.ExactAttribute[1], object.MatchStringEqual)
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
cidStr := prm.Container.EncodeToString()
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
var res []oid.ID
|
|
|
|
|
|
|
|
if len(filters) == 1 {
|
|
|
|
for k, v := range t.objects {
|
|
|
|
if strings.Contains(k, cidStr) {
|
2022-04-25 09:57:58 +00:00
|
|
|
id, _ := v.ID()
|
|
|
|
res = append(res, id)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := filters[1]
|
|
|
|
if len(filters) != 2 || filter.Operation() != object.MatchStringEqual ||
|
|
|
|
(filter.Header() != object.AttributeFileName && filter.Header() != objectSystemAttributeName) {
|
|
|
|
return nil, fmt.Errorf("usupported filters")
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range t.objects {
|
|
|
|
if strings.Contains(k, cidStr) && isMatched(v.Attributes(), filter) {
|
2022-04-25 09:57:58 +00:00
|
|
|
id, _ := v.ID()
|
|
|
|
res = append(res, id)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
func (t *TestNeoFS) ReadObject(_ context.Context, prm PrmObjectRead) (*ObjectPart, error) {
|
2022-05-25 17:25:43 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(prm.Container)
|
|
|
|
addr.SetObject(prm.Object)
|
2022-03-04 13:07:27 +00:00
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
sAddr := addr.EncodeToString()
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
if obj, ok := t.objects[sAddr]; ok {
|
2022-06-06 11:09:09 +00:00
|
|
|
return &ObjectPart{
|
2022-03-04 13:07:27 +00:00
|
|
|
Head: obj,
|
|
|
|
Payload: io.NopCloser(bytes.NewReader(obj.Payload())),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
return nil, fmt.Errorf("object not found %s", addr)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
func (t *TestNeoFS) CreateObject(_ context.Context, prm PrmObjectCreate) (*oid.ID, error) {
|
2022-04-25 09:57:58 +00:00
|
|
|
b := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, b); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var id oid.ID
|
|
|
|
id.SetSHA256(sha256.Sum256(b))
|
2022-03-04 13:07:27 +00:00
|
|
|
|
2022-03-17 14:03:06 +00:00
|
|
|
attrs := make([]object.Attribute, 0)
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
if prm.Filename != "" {
|
|
|
|
a := object.NewAttribute()
|
|
|
|
a.SetKey(object.AttributeFileName)
|
|
|
|
a.SetValue(prm.Filename)
|
2022-03-17 14:03:06 +00:00
|
|
|
attrs = append(attrs, *a)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range prm.Attributes {
|
|
|
|
a := object.NewAttribute()
|
|
|
|
a.SetKey(prm.Attributes[i][0])
|
|
|
|
a.SetValue(prm.Attributes[i][1])
|
2022-03-17 14:03:06 +00:00
|
|
|
attrs = append(attrs, *a)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obj := object.New()
|
2022-04-25 09:57:58 +00:00
|
|
|
obj.SetContainerID(prm.Container)
|
2022-03-04 13:07:27 +00:00
|
|
|
obj.SetID(id)
|
|
|
|
obj.SetPayloadSize(prm.PayloadSize)
|
|
|
|
obj.SetAttributes(attrs...)
|
|
|
|
obj.SetCreationEpoch(t.currentEpoch)
|
|
|
|
t.currentEpoch++
|
|
|
|
|
2022-03-04 14:36:30 +00:00
|
|
|
if len(prm.Locks) > 0 {
|
|
|
|
lock := new(object.Lock)
|
|
|
|
lock.WriteMembers(prm.Locks)
|
|
|
|
objectv2.WriteLock(obj.ToV2(), (objectv2.Lock)(*lock))
|
|
|
|
}
|
|
|
|
|
2022-03-04 13:07:27 +00:00
|
|
|
if prm.Payload != nil {
|
|
|
|
all, err := io.ReadAll(prm.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
obj.SetPayload(all)
|
|
|
|
obj.SetPayloadSize(uint64(len(all)))
|
2022-06-01 14:50:30 +00:00
|
|
|
var hash checksum.Checksum
|
|
|
|
checksum.Calculate(&hash, checksum.SHA256, all)
|
|
|
|
obj.SetPayloadChecksum(hash)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
cnrID, _ := obj.ContainerID()
|
|
|
|
objID, _ := obj.ID()
|
|
|
|
|
|
|
|
addr := newAddress(cnrID, objID)
|
2022-05-25 17:25:43 +00:00
|
|
|
t.objects[addr.EncodeToString()] = obj
|
2022-04-25 09:57:58 +00:00
|
|
|
return &objID, nil
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
func (t *TestNeoFS) DeleteObject(_ context.Context, prm PrmObjectDelete) error {
|
2022-05-25 17:25:43 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(prm.Container)
|
|
|
|
addr.SetObject(prm.Object)
|
2022-03-04 13:07:27 +00:00
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
delete(t.objects, addr.EncodeToString())
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
func (t *TestNeoFS) TimeToEpoch(_ context.Context, futureTime time.Time) (uint64, uint64, error) {
|
2022-03-05 08:53:01 +00:00
|
|
|
return t.currentEpoch, t.currentEpoch + uint64(futureTime.Second()), nil
|
|
|
|
}
|
|
|
|
|
2022-03-17 14:03:06 +00:00
|
|
|
func isMatched(attributes []object.Attribute, filter object.SearchFilter) bool {
|
2022-03-04 13:07:27 +00:00
|
|
|
for _, attr := range attributes {
|
|
|
|
if attr.Key() == filter.Header() && attr.Value() == filter.Value() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|