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"
|
|
|
|
|
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"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func testSelect(t *testing.T, db *meta.DB, cid *cid.ID, fs object.SearchFilters, exp ...*addressSDK.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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func testOID() *oidSDK.ID {
|
2020-10-29 12:03:55 +00:00
|
|
|
cs := [sha256.Size]byte{}
|
2020-12-08 07:51:34 +00:00
|
|
|
_, _ = rand.Read(cs[:])
|
2020-10-29 12:03:55 +00:00
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
id := oidSDK.NewID()
|
2020-10-29 12:03:55 +00:00
|
|
|
id.SetSHA256(cs)
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:34:31 +00:00
|
|
|
func newDB(t testing.TB, opts ...meta.Option) *meta.DB {
|
2020-12-08 07:51:34 +00:00
|
|
|
path := t.Name()
|
2020-10-29 12:03:55 +00:00
|
|
|
|
2022-03-21 13:34:31 +00:00
|
|
|
bdb := meta.New(append([]meta.Option{meta.WithPath(path), meta.WithPermissions(0600)},
|
|
|
|
opts...)...)
|
2020-10-29 12:03:55 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, bdb.Open())
|
2022-04-26 19:49:59 +00:00
|
|
|
require.NoError(t, bdb.Init())
|
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
|
|
|
|
2022-03-21 13:34:31 +00:00
|
|
|
func generateObject(t testing.TB) *object.Object {
|
2022-03-03 14:19:05 +00:00
|
|
|
return generateObjectWithCID(t, cidtest.ID())
|
2020-11-02 07:37:16 +00:00
|
|
|
}
|
2020-11-02 07:55:35 +00:00
|
|
|
|
2022-03-21 13:34:31 +00:00
|
|
|
func generateObjectWithCID(t testing.TB, cid *cid.ID) *object.Object {
|
2022-05-11 14:58:52 +00:00
|
|
|
var ver version.Version
|
|
|
|
ver.SetMajor(2)
|
|
|
|
ver.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
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := object.New()
|
2020-11-02 08:01:55 +00:00
|
|
|
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)
|
2022-05-11 14:58:52 +00:00
|
|
|
obj.SetVersion(&ver)
|
2020-12-08 07:51:34 +00:00
|
|
|
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
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func generateAddress() *addressSDK.Address {
|
|
|
|
addr := addressSDK.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
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func addAttribute(obj *object.Object, key, val string) {
|
2022-03-15 12:11:35 +00:00
|
|
|
var attr object.Attribute
|
2020-12-08 07:51:34 +00:00
|
|
|
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
|
|
|
}
|