Skip to content

Commit

Permalink
layer: Actualize multipart tests
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
  • Loading branch information
smallhive committed Feb 28, 2024
1 parent 113ac41 commit 9b9667d
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 21 deletions.
8 changes: 4 additions & 4 deletions api/handler/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ func prepareHandlerContext(t *testing.T) *handlerContext {
require.NoError(t, err)
anonSigner := user.NewAutoIDSignerRFC6979(anonKey.PrivateKey)

signer := user.NewAutoIDSignerRFC6979(key.PrivateKey)
owner := signer.UserID()

l := zap.NewExample()
tp := layer.NewTestNeoFS()
tp := layer.NewTestNeoFS(signer)

testResolver := &contResolver{layer: tp}

signer := user.NewAutoIDSignerRFC6979(key.PrivateKey)
owner := signer.UserID()

layerCfg := &layer.Config{
Caches: layer.DefaultCachesConfigs(zap.NewExample()),
GateKey: key,
Expand Down
176 changes: 160 additions & 16 deletions api/layer/neofs_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import (
"github.com/nspcc-dev/neofs-sdk-go/checksum"
"github.com/nspcc-dev/neofs-sdk-go/container"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
"github.com/nspcc-dev/neofs-sdk-go/eacl"
"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/session"
"github.com/nspcc-dev/neofs-sdk-go/user"
"github.com/nspcc-dev/tzhash/tz"
)

type TestNeoFS struct {
Expand All @@ -31,13 +33,15 @@ type TestNeoFS struct {
containers map[string]*container.Container
eaclTables map[string]*eacl.Table
currentEpoch uint64
signer neofscrypto.Signer
}

func NewTestNeoFS() *TestNeoFS {
func NewTestNeoFS(signer neofscrypto.Signer) *TestNeoFS {
return &TestNeoFS{
objects: make(map[string]*object.Object),
containers: make(map[string]*container.Container),
eaclTables: make(map[string]*eacl.Table),
signer: signer,
}
}

Expand Down Expand Up @@ -138,32 +142,105 @@ func (t *TestNeoFS) UserContainers(_ context.Context, _ user.ID) ([]cid.ID, erro
}

func (t *TestNeoFS) ReadObject(ctx context.Context, prm PrmObjectRead) (*ObjectPart, error) {
var addr oid.Address
var (
addr oid.Address
)
addr.SetContainer(prm.Container)
addr.SetObject(prm.Object)

sAddr := addr.EncodeToString()

if obj, ok := t.objects[sAddr]; ok {
owner := getOwner(ctx)
if !obj.OwnerID().Equals(owner) {
return nil, ErrAccessDenied
obj, ok := t.objects[sAddr]
if !ok {
// trying to find linking object.
for _, o := range t.objects {
parentID, isSet := o.ParentID()
if !isSet {
continue
}

if !parentID.Equals(prm.Object) {
continue
}

if len(o.Children()) == 0 {
continue
}

// linking object is found.
objPart, err := t.constructMupltipartObject(ctx, prm.Container, o)
if err != nil {
return nil, err
}

obj = objPart.Head

pl, err := io.ReadAll(objPart.Payload)
if err != nil {
return nil, err
}

obj.SetPayload(pl)
ok = true
break
}
}

if !ok {
return nil, fmt.Errorf("object not found %s", addr)
}

owner := getOwner(ctx)
if !obj.OwnerID().Equals(owner) {
return nil, ErrAccessDenied
}

payload := obj.Payload()

if prm.PayloadRange[0]+prm.PayloadRange[1] > 0 {
off := prm.PayloadRange[0]
payload = payload[off : off+prm.PayloadRange[1]]
}

return &ObjectPart{
Head: obj,
Payload: io.NopCloser(bytes.NewReader(payload)),
}, nil
}

func (t *TestNeoFS) constructMupltipartObject(ctx context.Context, containerID cid.ID, linkingObject *object.Object) (*ObjectPart, error) {
if _, isSet := linkingObject.ParentID(); !isSet {
return nil, fmt.Errorf("linking object is invalid")
}

var (
addr oid.Address
headObject = linkingObject.Parent()
payloadReaders = make([]io.Reader, 0, len(linkingObject.Children()))
childList = linkingObject.Children()
)

payload := obj.Payload()
addr.SetContainer(containerID)

if prm.PayloadRange[0]+prm.PayloadRange[1] > 0 {
off := prm.PayloadRange[0]
payload = payload[off : off+prm.PayloadRange[1]]
for _, c := range childList {
addr.SetObject(c)

objPart, err := t.ReadObject(ctx, PrmObjectRead{
Container: containerID,
Object: c,
})

if err != nil {
return nil, fmt.Errorf("child read: %w", err)
}

return &ObjectPart{
Head: obj,
Payload: io.NopCloser(bytes.NewReader(payload)),
}, nil
payloadReaders = append(payloadReaders, objPart.Payload)
}

return nil, fmt.Errorf("object not found %s", addr)
return &ObjectPart{
Head: headObject,
Payload: io.NopCloser(io.MultiReader(payloadReaders...)),
}, nil
}

func (t *TestNeoFS) CreateObject(_ context.Context, prm PrmObjectCreate) (oid.ID, error) {
Expand Down Expand Up @@ -195,6 +272,32 @@ func (t *TestNeoFS) CreateObject(_ context.Context, prm PrmObjectCreate) (oid.ID
obj.SetOwnerID(&prm.Creator)
t.currentEpoch++

if prm.SplitID != "" {
var split object.SplitID
if err := split.Parse(prm.SplitID); err != nil {
return oid.ID{}, fmt.Errorf("split parse: %w", err)
}
obj.SetSplitID(&split)

if prm.SplitPreviousID != nil {
obj.SetPreviousID(*prm.SplitPreviousID)
}

if len(prm.Children) > 0 {
obj.SetChildren(prm.Children...)
}

if prm.HeaderObject != nil {
id, isSet := prm.HeaderObject.ID()
if !isSet {
return oid.ID{}, errors.New("HeaderObject id is not set")
}

obj.SetParentID(id)
obj.SetParent(prm.HeaderObject)
}
}

if len(prm.Locks) > 0 {
var lock object.Lock
lock.WriteMembers(prm.Locks)
Expand All @@ -221,7 +324,48 @@ func (t *TestNeoFS) CreateObject(_ context.Context, prm PrmObjectCreate) (oid.ID
return objID, nil
}

func (t *TestNeoFS) PrepareMulptipartHeader(_ context.Context, header object.Object, _ hash.Hash, _ hash.Hash, _ uint64) (*object.Object, error) {
func (t *TestNeoFS) PrepareMulptipartHeader(_ context.Context, header object.Object, metaChecksum hash.Hash, homomorphicChecksum hash.Hash, payloadLength uint64) (*object.Object, error) {
header.SetCreationEpoch(t.currentEpoch)

var cs checksum.Checksum

var csBytes [sha256.Size]byte
copy(csBytes[:], metaChecksum.Sum(nil))

cs.SetSHA256(csBytes)
header.SetPayloadChecksum(cs)

if homomorphicChecksum != nil {
var csHomoBytes [tz.Size]byte
copy(csHomoBytes[:], homomorphicChecksum.Sum(nil))

cs.SetTillichZemor(csHomoBytes)
header.SetPayloadHomomorphicHash(cs)
}

header.SetPayloadSize(payloadLength)

id, err := header.CalculateID()
if err != nil {
return nil, fmt.Errorf("calculate ID: %w", err)
}

header.SetID(id)

bID, err := id.Marshal()
if err != nil {
return nil, fmt.Errorf("marshal object ID: %w", err)
}

var sig neofscrypto.Signature

err = sig.Calculate(t.signer, bID)
if err != nil {
return nil, fmt.Errorf("sign object ID: %w", err)
}

header.SetSignature(&sig)

return &header, nil
}

Expand Down
2 changes: 1 addition & 1 deletion api/layer/versioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func prepareContext(t *testing.T, cachesConfig ...*CachesConfig) *testContext {
GateKey: key.PublicKey(),
},
})
tp := NewTestNeoFS()
tp := NewTestNeoFS(signer)

bktName := "testbucket1"
bktID, err := tp.CreateContainer(ctx, PrmContainerCreate{
Expand Down

0 comments on commit 9b9667d

Please sign in to comment.