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 Mar 15, 2024
1 parent 7945178 commit 4188fe1
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 20 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
153 changes: 138 additions & 15 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 @@ -144,26 +148,97 @@ func (t *TestNeoFS) ReadObject(ctx context.Context, prm PrmObjectRead) (*ObjectP

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

Check warning on line 157 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L153-L157

Added lines #L153 - L157 were not covered by tests
}

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

Check warning on line 161 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L160-L161

Added lines #L160 - L161 were not covered by tests
}

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

Check warning on line 165 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L164-L165

Added lines #L164 - L165 were not covered by tests
}

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

Check warning on line 172 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L169-L172

Added lines #L169 - L172 were not covered by tests

obj = objPart.Head

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

Check warning on line 179 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L174-L179

Added lines #L174 - L179 were not covered by tests

obj.SetPayload(pl)
ok = true
break

Check warning on line 183 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L181-L183

Added lines #L181 - L183 were not covered by tests
}
}

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

Check warning on line 189 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L188-L189

Added lines #L188 - L189 were not covered by tests

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

Check warning on line 194 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L193-L194

Added lines #L193 - L194 were not covered by tests

payload := obj.Payload()

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

Check warning on line 201 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L199-L201

Added lines #L199 - L201 were not covered by tests

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")
}

Check warning on line 212 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L209-L212

Added lines #L209 - L212 were not covered by tests

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

Check warning on line 219 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L214-L219

Added lines #L214 - L219 were not covered by tests

addr.SetContainer(containerID)

Check warning on line 221 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L221

Added line #L221 was not covered by tests

for _, c := range childList {
addr.SetObject(c)

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

if prm.PayloadRange[0]+prm.PayloadRange[1] > 0 {
off := prm.PayloadRange[0]
payload = payload[off : off+prm.PayloadRange[1]]
if err != nil {
return nil, fmt.Errorf("child read: %w", err)

Check warning on line 232 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L223-L232

Added lines #L223 - L232 were not covered by tests
}

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

Check warning on line 235 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L235

Added line #L235 was not covered by tests
}

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

Check warning on line 241 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L238-L241

Added lines #L238 - L241 were not covered by tests
}

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

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

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

Check warning on line 282 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L274-L282

Added lines #L274 - L282 were not covered by tests

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

Check warning on line 286 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L284-L286

Added lines #L284 - L286 were not covered by tests

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

Check warning on line 292 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L288-L292

Added lines #L288 - L292 were not covered by tests

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

Check warning on line 295 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L294-L295

Added lines #L294 - L295 were not covered by tests
}
}

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

func (t *TestNeoFS) FinalizeObjectWithPayloadChecksums(_ context.Context, header object.Object, _ hash.Hash, _ hash.Hash, _ uint64) (*object.Object, error) {
func (t *TestNeoFS) FinalizeObjectWithPayloadChecksums(_ 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)
}

Check warning on line 342 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L325-L342

Added lines #L325 - L342 were not covered by tests

header.SetPayloadSize(payloadLength)
if err := header.SetIDWithSignature(t.signer); err != nil {
return nil, fmt.Errorf("setIDWithSignature: %w", err)
}
return &header, nil

Check warning on line 348 in api/layer/neofs_mock.go

View check run for this annotation

Codecov / codecov/patch

api/layer/neofs_mock.go#L344-L348

Added lines #L344 - L348 were not covered by tests
}

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 4188fe1

Please sign in to comment.