[#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:
Leonard Lyubich 2020-12-11 11:03:27 +03:00 committed by Alex Vanin
parent 4f5d5c7e45
commit 510e9ff2ec
7 changed files with 30 additions and 234 deletions

2
go.mod
View file

@ -17,7 +17,7 @@ require (
github.com/multiformats/go-multihash v0.0.13 // indirect github.com/multiformats/go-multihash v0.0.13 // indirect
github.com/nspcc-dev/hrw v1.0.9 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/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/neofs-crypto v0.3.0
github.com/nspcc-dev/tzhash v1.4.0 github.com/nspcc-dev/tzhash v1.4.0
github.com/panjf2000/ants/v2 v2.3.0 github.com/panjf2000/ants/v2 v2.3.0

4
go.sum
View file

@ -283,8 +283,8 @@ github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:
github.com/nspcc-dev/neo-go v0.91.0/go.mod h1:G6HdOWvzQ6tlvFdvFSN/PgCzLPN/X/X4d5hTjFRUDcc= github.com/nspcc-dev/neo-go v0.91.0/go.mod h1:G6HdOWvzQ6tlvFdvFSN/PgCzLPN/X/X4d5hTjFRUDcc=
github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b h1:gk5bZgpWOehaDVKI5vBDkcjXTpRkKqcvIb1h/vHnBH4= github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b h1:gk5bZgpWOehaDVKI5vBDkcjXTpRkKqcvIb1h/vHnBH4=
github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b/go.mod h1:9s7LNp2lMgf0caH2t0sam4+WT2SIauXozwP0AdBqnEo= github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b/go.mod h1:9s7LNp2lMgf0caH2t0sam4+WT2SIauXozwP0AdBqnEo=
github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201208072327-139660c6ff59 h1:OfeINXdkMrlfdqsVK5ECYi7vgt9TpO/R3JMQcnQndXI= github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201210152623-803c91b3eb2b h1:crUr2jA84f0cQabYAN40uTYNTlu3xI9ZoB0BUBooQTA=
github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201208072327-139660c6ff59/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201210152623-803c91b3eb2b/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA= github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA=
github.com/nspcc-dev/neofs-crypto v0.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw= github.com/nspcc-dev/neofs-crypto v0.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw=
github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnBg0L4ifM= github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnBg0L4ifM=

View file

@ -108,32 +108,37 @@ func (v *FormatValidator) checkOwnerKey(id *owner.ID, key []byte) error {
} }
// ValidateContent validates payload content according to object type. // 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() { switch o.Type() {
case object.TypeTombstone: case object.TypeTombstone:
if len(o.Payload()) == 0 { if len(o.Payload()) == 0 {
return errors.Errorf("(%T) empty payload in tombstone", v) return errors.Errorf("(%T) empty payload in tombstone", v)
} }
content, err := TombstoneContentFromBytes(o.Payload()) tombstone := object.NewTombstone()
if err != nil {
return errors.Wrapf(err, "(%T) could not parse tombstone content", err) 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 { for _, id := range idList {
if addr.ContainerID() == nil || addr.ObjectID() == nil { if id == nil {
return errors.Errorf("(%T) empty address reference in tombstone", v) return errors.Errorf("(%T) empty member in tombstone", v)
}
} }
tsAddr := new(object.Address) a := object.NewAddress()
tsAddr.SetContainerID(o.ContainerID()) a.SetContainerID(cid)
tsAddr.SetObjectID(o.ID()) a.SetObjectID(id)
addrList = append(addrList, a)
}
if v.deleteHandler != nil { if v.deleteHandler != nil {
v.deleteHandler.DeleteObjects(tsAddr, addrList...) v.deleteHandler.DeleteObjects(o.Address(), addrList...)
} }
} }

View file

@ -108,28 +108,25 @@ func TestFormatValidator_Validate(t *testing.T) {
obj := NewRaw() obj := NewRaw()
obj.SetType(object.TypeTombstone) 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() data, err := content.Marshal()
content.SetAddressList(addr)
data, err := content.MarshalBinary()
require.NoError(t, err) require.NoError(t, err)
obj.SetPayload(data) obj.SetPayload(data)
require.Error(t, v.ValidateContent(obj.Object().SDK())) require.Error(t, v.ValidateContent(obj.Object()))
addr.SetContainerID(testContainerID(t)) content.SetMembers([]*object.ID{testObjectID(t)})
addr.SetObjectID(testObjectID(t))
data, err = content.MarshalBinary() data, err = content.Marshal()
require.NoError(t, err) require.NoError(t, err)
obj.SetPayload(data) obj.SetPayload(data)
require.NoError(t, v.ValidateContent(obj.Object().SDK())) require.NoError(t, v.ValidateContent(obj.Object()))
}) })
} }

View file

@ -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
}

View file

@ -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)
}

View file

@ -65,7 +65,7 @@ func (t *distributedTarget) Close() (*transformer.AccessIdentifiers, error) {
t.obj.SetPayload(payload) 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) return nil, errors.Wrapf(err, "(%T) could not validate payload content", t)
} }