2020-12-08 07:51:34 +00:00
|
|
|
package meta_test
|
2020-10-28 14:49:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2021-01-20 18:13:18 +00:00
|
|
|
"math/rand"
|
2020-10-28 14:49:30 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-12-08 07:51:34 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2020-10-28 14:49:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/test"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/checksum"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
ownertest "github.com/nspcc-dev/neofs-sdk-go/owner/test"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/version"
|
2020-12-08 07:51:34 +00:00
|
|
|
"github.com/nspcc-dev/tzhash/tz"
|
2020-10-28 14:49:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// saves "big" object in DB.
|
|
|
|
func putBig(db *meta.DB, obj *object.Object) error {
|
|
|
|
return meta.Put(db, obj, nil)
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func testSelect(t *testing.T, db *meta.DB, cid *cid.ID, fs objectSDK.SearchFilters, exp ...*objectSDK.Address) {
|
2020-12-10 13:17:45 +00:00
|
|
|
res, err := meta.Select(db, cid, fs)
|
2020-10-28 14:49:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, res, len(exp))
|
|
|
|
|
|
|
|
for i := range exp {
|
|
|
|
require.Contains(t, res, exp[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 12:03:55 +00:00
|
|
|
func testOID() *objectSDK.ID {
|
|
|
|
cs := [sha256.Size]byte{}
|
2020-12-08 07:51:34 +00:00
|
|
|
_, _ = rand.Read(cs[:])
|
2020-10-29 12:03:55 +00:00
|
|
|
|
|
|
|
id := objectSDK.NewID()
|
|
|
|
id.SetSHA256(cs)
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func newDB(t testing.TB) *meta.DB {
|
|
|
|
path := t.Name()
|
2020-10-29 12:03:55 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
bdb := meta.New(meta.WithPath(path), meta.WithPermissions(0600))
|
2020-10-29 12:03:55 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, bdb.Open())
|
2020-10-29 12:03:55 +00:00
|
|
|
|
2021-10-27 10:56:30 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
bdb.Close()
|
|
|
|
os.Remove(bdb.DumpInfo().Path)
|
|
|
|
})
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2021-10-27 10:56:30 +00:00
|
|
|
return bdb
|
2020-10-29 16:12:38 +00:00
|
|
|
}
|
2020-11-02 07:37:16 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func generateRawObject(t *testing.T) *object.RawObject {
|
2021-12-01 13:56:48 +00:00
|
|
|
return generateRawObjectWithCID(t, cidtest.ID())
|
2020-11-02 07:37:16 +00:00
|
|
|
}
|
2020-11-02 07:55:35 +00:00
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func generateRawObjectWithCID(t *testing.T, cid *cid.ID) *object.RawObject {
|
2021-11-10 07:08:33 +00:00
|
|
|
version := version.New()
|
2020-12-08 07:51:34 +00:00
|
|
|
version.SetMajor(2)
|
|
|
|
version.SetMinor(1)
|
2020-11-02 07:55:35 +00:00
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
csum := new(checksum.Checksum)
|
2022-01-21 12:15:10 +00:00
|
|
|
csum.SetSHA256(sha256.Sum256(owner.PublicKeyToIDBytes(&test.DecodeKey(-1).PublicKey)))
|
2020-11-02 08:01:55 +00:00
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
csumTZ := new(checksum.Checksum)
|
2020-12-08 07:51:34 +00:00
|
|
|
csumTZ.SetTillichZemor(tz.Sum(csum.Sum()))
|
2020-11-02 08:01:55 +00:00
|
|
|
|
|
|
|
obj := object.NewRaw()
|
|
|
|
obj.SetID(testOID())
|
2021-12-01 13:56:48 +00:00
|
|
|
obj.SetOwnerID(ownertest.ID())
|
2020-12-08 07:51:34 +00:00
|
|
|
obj.SetContainerID(cid)
|
|
|
|
obj.SetVersion(version)
|
|
|
|
obj.SetPayloadChecksum(csum)
|
|
|
|
obj.SetPayloadHomomorphicHash(csumTZ)
|
2021-03-22 16:44:57 +00:00
|
|
|
obj.SetPayload([]byte{1, 2, 3, 4, 5})
|
2020-11-02 08:01:55 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
return obj
|
2020-11-02 08:01:55 +00:00
|
|
|
}
|
2020-11-06 09:41:59 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func generateAddress() *objectSDK.Address {
|
|
|
|
addr := objectSDK.NewAddress()
|
2021-12-01 13:56:48 +00:00
|
|
|
addr.SetContainerID(cidtest.ID())
|
2020-12-08 07:51:34 +00:00
|
|
|
addr.SetObjectID(testOID())
|
2020-11-06 09:41:59 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
return addr
|
|
|
|
}
|
2020-11-06 09:41:59 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func addAttribute(obj *object.RawObject, key, val string) {
|
|
|
|
attr := objectSDK.NewAttribute()
|
|
|
|
attr.SetKey(key)
|
|
|
|
attr.SetValue(val)
|
2020-11-06 09:41:59 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
attrs := obj.Attributes()
|
|
|
|
attrs = append(attrs, attr)
|
|
|
|
obj.SetAttributes(attrs...)
|
2020-11-06 09:41:59 +00:00
|
|
|
}
|