2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2021-01-20 18:13:18 +00:00
|
|
|
"math/rand"
|
2020-11-26 14:26:53 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-31 11:27:58 +00:00
|
|
|
cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test"
|
2020-11-26 14:26:53 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testSHA256() (h [sha256.Size]byte) {
|
|
|
|
rand.Read(h[:])
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAddress() *objectSDK.Address {
|
|
|
|
oid := objectSDK.NewID()
|
|
|
|
oid.SetSHA256(testSHA256())
|
|
|
|
|
|
|
|
addr := objectSDK.NewAddress()
|
|
|
|
addr.SetObjectID(oid)
|
2021-05-31 11:27:58 +00:00
|
|
|
addr.SetContainerID(cidtest.Generate())
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPutGet(t *testing.T, blz *Blobovnicza, sz uint64, expPut, expGet error) *objectSDK.Address {
|
2020-12-02 09:58:42 +00:00
|
|
|
// create binary object
|
|
|
|
data := make([]byte, sz)
|
|
|
|
|
|
|
|
addr := testAddress()
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
// try to save object in Blobovnicza
|
|
|
|
pPut := new(PutPrm)
|
2020-12-02 09:58:42 +00:00
|
|
|
pPut.SetAddress(addr)
|
|
|
|
pPut.SetMarshaledObject(data)
|
2020-11-26 14:26:53 +00:00
|
|
|
_, err := blz.Put(pPut)
|
|
|
|
require.True(t, errors.Is(err, expPut))
|
|
|
|
|
|
|
|
if expPut != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
testGet(t, blz, addr, data, expGet)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
return addr
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
func testGet(t *testing.T, blz *Blobovnicza, addr *objectSDK.Address, expObj []byte, expErr error) {
|
2020-11-26 14:26:53 +00:00
|
|
|
pGet := new(GetPrm)
|
|
|
|
pGet.SetAddress(addr)
|
|
|
|
|
|
|
|
// try to read object from Blobovnicza
|
|
|
|
res, err := blz.Get(pGet)
|
|
|
|
require.True(t, errors.Is(err, expErr))
|
|
|
|
|
|
|
|
if expErr == nil {
|
|
|
|
require.Equal(t, expObj, res.Object())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlobovnicza(t *testing.T) {
|
2021-01-20 18:13:18 +00:00
|
|
|
rand.Seed(1024)
|
|
|
|
|
2020-11-26 14:26:53 +00:00
|
|
|
p := "./test_blz"
|
|
|
|
|
|
|
|
sizeLim := uint64(256 * 1 << 10) // 256KB
|
|
|
|
objSizeLim := sizeLim / 2
|
|
|
|
|
|
|
|
// create Blobovnicza instance
|
|
|
|
blz := New(
|
|
|
|
WithPath(p),
|
|
|
|
WithObjectSizeLimit(objSizeLim),
|
|
|
|
WithFullSizeLimit(sizeLim),
|
|
|
|
WithLogger(test.NewLogger(false)),
|
|
|
|
)
|
|
|
|
|
|
|
|
defer os.Remove(p)
|
|
|
|
|
|
|
|
// open Blobovnicza
|
|
|
|
require.NoError(t, blz.Open())
|
|
|
|
|
|
|
|
// initialize Blobovnicza
|
|
|
|
require.NoError(t, blz.Init())
|
|
|
|
|
|
|
|
// try to read non-existent address
|
2020-11-30 16:39:05 +00:00
|
|
|
testGet(t, blz, testAddress(), nil, object.ErrNotFound)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
filled := uint64(15 * 1 << 10)
|
|
|
|
|
|
|
|
// test object 15KB
|
|
|
|
addr := testPutGet(t, blz, filled, nil, nil)
|
|
|
|
|
|
|
|
// remove the object
|
|
|
|
dPrm := new(DeletePrm)
|
|
|
|
dPrm.SetAddress(addr)
|
|
|
|
|
|
|
|
_, err := blz.Delete(dPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// should return 404
|
2020-11-30 16:39:05 +00:00
|
|
|
testGet(t, blz, addr, nil, object.ErrNotFound)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
// fill Blobovnicza fully
|
|
|
|
for ; filled < sizeLim; filled += objSizeLim {
|
|
|
|
testPutGet(t, blz, objSizeLim, nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// from now objects should not be saved
|
|
|
|
testPutGet(t, blz, 1024, ErrFull, nil)
|
|
|
|
|
|
|
|
require.NoError(t, blz.Close())
|
|
|
|
}
|