[#243] core/object: Replace test content of tombstone with an API structure
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
4f5d5c7e45
commit
510e9ff2ec
7 changed files with 28 additions and 232 deletions
2
go.mod
2
go.mod
|
@ -17,7 +17,7 @@ require (
|
|||
github.com/multiformats/go-multihash v0.0.13 // indirect
|
||||
github.com/nspcc-dev/hrw v1.0.9
|
||||
github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b
|
||||
github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201208072327-139660c6ff59
|
||||
github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201210152623-803c91b3eb2b
|
||||
github.com/nspcc-dev/neofs-crypto v0.3.0
|
||||
github.com/nspcc-dev/tzhash v1.4.0
|
||||
github.com/panjf2000/ants/v2 v2.3.0
|
||||
|
|
BIN
go.sum
BIN
go.sum
Binary file not shown.
|
@ -108,32 +108,37 @@ func (v *FormatValidator) checkOwnerKey(id *owner.ID, key []byte) error {
|
|||
}
|
||||
|
||||
// ValidateContent validates payload content according to object type.
|
||||
func (v *FormatValidator) ValidateContent(o *object.Object) error {
|
||||
func (v *FormatValidator) ValidateContent(o *Object) error {
|
||||
switch o.Type() {
|
||||
case object.TypeTombstone:
|
||||
if len(o.Payload()) == 0 {
|
||||
return errors.Errorf("(%T) empty payload in tombstone", v)
|
||||
}
|
||||
|
||||
content, err := TombstoneContentFromBytes(o.Payload())
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "(%T) could not parse tombstone content", err)
|
||||
tombstone := object.NewTombstone()
|
||||
|
||||
if err := tombstone.Unmarshal(o.Payload()); err != nil {
|
||||
return errors.Wrapf(err, "(%T) could not unmarshal tombstone content", v)
|
||||
}
|
||||
|
||||
addrList := content.GetAddressList()
|
||||
cid := o.ContainerID()
|
||||
idList := tombstone.Members()
|
||||
addrList := make([]*object.Address, 0, len(idList))
|
||||
|
||||
for _, addr := range addrList {
|
||||
if addr.ContainerID() == nil || addr.ObjectID() == nil {
|
||||
return errors.Errorf("(%T) empty address reference in tombstone", v)
|
||||
for _, id := range idList {
|
||||
if id == nil {
|
||||
return errors.Errorf("(%T) empty member in tombstone", v)
|
||||
}
|
||||
}
|
||||
|
||||
tsAddr := new(object.Address)
|
||||
tsAddr.SetContainerID(o.ContainerID())
|
||||
tsAddr.SetObjectID(o.ID())
|
||||
a := object.NewAddress()
|
||||
a.SetContainerID(cid)
|
||||
a.SetObjectID(id)
|
||||
|
||||
addrList = append(addrList, a)
|
||||
}
|
||||
|
||||
if v.deleteHandler != nil {
|
||||
v.deleteHandler.DeleteObjects(tsAddr, addrList...)
|
||||
v.deleteHandler.DeleteObjects(o.Address(), addrList...)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -108,28 +108,25 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
obj := NewRaw()
|
||||
obj.SetType(object.TypeTombstone)
|
||||
|
||||
require.Error(t, v.ValidateContent(obj.Object().SDK()))
|
||||
require.Error(t, v.ValidateContent(obj.Object()))
|
||||
|
||||
addr := object.NewAddress()
|
||||
content := object.NewTombstone()
|
||||
content.SetMembers([]*object.ID{nil})
|
||||
|
||||
content := NewTombstoneContent()
|
||||
content.SetAddressList(addr)
|
||||
|
||||
data, err := content.MarshalBinary()
|
||||
data, err := content.Marshal()
|
||||
require.NoError(t, err)
|
||||
|
||||
obj.SetPayload(data)
|
||||
|
||||
require.Error(t, v.ValidateContent(obj.Object().SDK()))
|
||||
require.Error(t, v.ValidateContent(obj.Object()))
|
||||
|
||||
addr.SetContainerID(testContainerID(t))
|
||||
addr.SetObjectID(testObjectID(t))
|
||||
content.SetMembers([]*object.ID{testObjectID(t)})
|
||||
|
||||
data, err = content.MarshalBinary()
|
||||
data, err = content.Marshal()
|
||||
require.NoError(t, err)
|
||||
|
||||
obj.SetPayload(data)
|
||||
|
||||
require.NoError(t, v.ValidateContent(obj.Object().SDK()))
|
||||
require.NoError(t, v.ValidateContent(obj.Object()))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,163 +0,0 @@
|
|||
package object
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||
"github.com/nspcc-dev/neofs-api-go/util/proto"
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// FIXME: replace this code to neofs-api-go
|
||||
|
||||
const addrListField = 1
|
||||
|
||||
type TombstoneContent TombstoneContentV2
|
||||
|
||||
type TombstoneContentV2 struct {
|
||||
addrList []*refs.Address
|
||||
}
|
||||
|
||||
func NewTombstoneContentFromV2(cV2 *TombstoneContentV2) *TombstoneContent {
|
||||
return (*TombstoneContent)(cV2)
|
||||
}
|
||||
|
||||
func NewTombstoneContent() *TombstoneContent {
|
||||
return NewTombstoneContentFromV2(new(TombstoneContentV2))
|
||||
}
|
||||
|
||||
func (c *TombstoneContent) MarshalBinary() ([]byte, error) {
|
||||
return c.ToV2().StableMarshal(nil)
|
||||
}
|
||||
|
||||
func (c *TombstoneContent) SetAddressList(v ...*object.Address) {
|
||||
if c != nil {
|
||||
addrV2 := make([]*refs.Address, 0, len(v))
|
||||
|
||||
for i := range v {
|
||||
addrV2 = append(addrV2, v[i].ToV2())
|
||||
}
|
||||
|
||||
(*TombstoneContentV2)(c).SetAddressList(addrV2)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TombstoneContent) GetAddressList() []*object.Address {
|
||||
if c != nil {
|
||||
addrV2 := (*TombstoneContentV2)(c).GetAddressList()
|
||||
addr := make([]*object.Address, 0, len(addrV2))
|
||||
|
||||
for i := range addrV2 {
|
||||
addr = append(addr, object.NewAddressFromV2(addrV2[i]))
|
||||
}
|
||||
|
||||
return addr
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *TombstoneContent) ToV2() *TombstoneContentV2 {
|
||||
return (*TombstoneContentV2)(c)
|
||||
}
|
||||
|
||||
func (c *TombstoneContentV2) StableMarshal(buf []byte) ([]byte, error) {
|
||||
if c == nil {
|
||||
return make([]byte, 0), nil
|
||||
}
|
||||
|
||||
if buf == nil {
|
||||
buf = make([]byte, c.StableSize())
|
||||
}
|
||||
|
||||
offset := 0
|
||||
|
||||
for i := range c.addrList {
|
||||
n, err := proto.NestedStructureMarshal(addrListField, buf[offset:], c.addrList[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
offset += n
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (c *TombstoneContentV2) StableSize() (sz int) {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for i := range c.addrList {
|
||||
sz += proto.NestedStructureSize(addrListField, c.addrList[i])
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *TombstoneContentV2) StableUnmarshal(data []byte) error {
|
||||
c.addrList = c.addrList[:0]
|
||||
|
||||
for len(data) > 0 {
|
||||
expPrefix, _ := proto.NestedStructurePrefix(addrListField)
|
||||
|
||||
prefix, ln := binary.Uvarint(data)
|
||||
if ln <= 0 {
|
||||
return io.ErrUnexpectedEOF
|
||||
} else if expPrefix != prefix {
|
||||
return errors.Errorf("wrong prefix %d", prefix)
|
||||
}
|
||||
|
||||
data = data[ln:]
|
||||
|
||||
sz, ln := binary.Uvarint(data)
|
||||
if ln <= 0 {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
|
||||
data = data[ln:]
|
||||
|
||||
addr := new(refs.Address)
|
||||
if err := addr.Unmarshal(data[:sz]); err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
c.addrList = append(c.addrList, addr)
|
||||
|
||||
data = data[sz:]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *TombstoneContentV2) SetAddressList(v []*refs.Address) {
|
||||
if c != nil {
|
||||
c.addrList = v
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TombstoneContentV2) GetAddressList() []*refs.Address {
|
||||
if c != nil {
|
||||
return c.addrList
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TombstoneContentFromBytes(data []byte) (*TombstoneContent, error) {
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
cV2 := new(TombstoneContentV2)
|
||||
if err := cV2.StableUnmarshal(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewTombstoneContentFromV2(cV2), nil
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
package object
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTombstoneContent_MarshalBinary(t *testing.T) {
|
||||
cid1 := container.NewID()
|
||||
cid1.SetSHA256([sha256.Size]byte{1, 2})
|
||||
|
||||
id1 := object.NewID()
|
||||
id1.SetSHA256([sha256.Size]byte{3, 4})
|
||||
|
||||
addr1 := object.NewAddress()
|
||||
addr1.SetObjectID(id1)
|
||||
addr1.SetContainerID(cid1)
|
||||
|
||||
cid2 := container.NewID()
|
||||
cid2.SetSHA256([sha256.Size]byte{5, 6})
|
||||
|
||||
id2 := object.NewID()
|
||||
id2.SetSHA256([sha256.Size]byte{7, 8})
|
||||
|
||||
addr2 := object.NewAddress()
|
||||
addr2.SetObjectID(id2)
|
||||
addr2.SetContainerID(cid2)
|
||||
|
||||
c := NewTombstoneContent()
|
||||
c.SetAddressList(addr1, addr2)
|
||||
|
||||
data, err := c.MarshalBinary()
|
||||
require.NoError(t, err)
|
||||
|
||||
c2, err := TombstoneContentFromBytes(data)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, c, c2)
|
||||
}
|
|
@ -65,7 +65,7 @@ func (t *distributedTarget) Close() (*transformer.AccessIdentifiers, error) {
|
|||
|
||||
t.obj.SetPayload(payload)
|
||||
|
||||
if err := t.fmt.ValidateContent(t.obj.Object().SDK()); err != nil {
|
||||
if err := t.fmt.ValidateContent(t.obj.Object()); err != nil {
|
||||
return nil, errors.Wrapf(err, "(%T) could not validate payload content", t)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue