2020-12-08 07:51:34 +00:00
|
|
|
package meta_test
|
2020-10-28 14:49:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
|
|
|
"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"
|
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)
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func testSelect(t *testing.T, db *meta.DB, fs objectSDK.SearchFilters, exp ...*objectSDK.Address) {
|
2020-12-08 09:56:14 +00:00
|
|
|
res, err := meta.Select(db, 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 testCID() *container.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 := container.NewID()
|
|
|
|
id.SetSHA256(cs)
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
return bdb
|
2020-10-29 12:03:55 +00:00
|
|
|
}
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func releaseDB(db *meta.DB) {
|
|
|
|
db.Close()
|
|
|
|
os.Remove(db.DumpInfo().Path)
|
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 {
|
|
|
|
return generateRawObjectWithCID(t, testCID())
|
2020-11-02 07:37:16 +00:00
|
|
|
}
|
2020-11-02 07:55:35 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func generateRawObjectWithCID(t *testing.T, cid *container.ID) *object.RawObject {
|
|
|
|
version := pkg.NewVersion()
|
|
|
|
version.SetMajor(2)
|
|
|
|
version.SetMinor(1)
|
2020-11-02 07:55:35 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
w, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(-1).PublicKey)
|
2020-11-02 07:55:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
ownerID := owner.NewID()
|
|
|
|
ownerID.SetNeo3Wallet(w)
|
2020-11-02 08:01:55 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
csum := new(pkg.Checksum)
|
|
|
|
csum.SetSHA256(sha256.Sum256(w.Bytes()))
|
2020-11-02 08:01:55 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
csumTZ := new(pkg.Checksum)
|
|
|
|
csumTZ.SetTillichZemor(tz.Sum(csum.Sum()))
|
2020-11-02 08:01:55 +00:00
|
|
|
|
|
|
|
obj := object.NewRaw()
|
|
|
|
obj.SetID(testOID())
|
2020-12-08 07:51:34 +00:00
|
|
|
obj.SetOwnerID(ownerID)
|
|
|
|
obj.SetContainerID(cid)
|
|
|
|
obj.SetVersion(version)
|
|
|
|
obj.SetPayloadChecksum(csum)
|
|
|
|
obj.SetPayloadHomomorphicHash(csumTZ)
|
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()
|
|
|
|
addr.SetContainerID(testCID())
|
|
|
|
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
|
|
|
}
|