object: implement signing payload methods on DeleteRequest message

This commit is contained in:
Leonard Lyubich 2020-05-06 14:15:07 +03:00
parent 68f83f5470
commit fc0da3c8fc
2 changed files with 60 additions and 33 deletions

View file

@ -4,27 +4,23 @@ import (
"io" "io"
) )
// SignedData returns marshaled payload of the Put request. // SignedData returns payload bytes of the request.
// //
// If payload is nil, ErrHeaderNotFound returns. // If payload is nil, ErrHeaderNotFound returns.
func (m PutRequest) SignedData() ([]byte, error) { func (m PutRequest) SignedData() ([]byte, error) {
r := m.GetR() sz := m.SignedDataSize()
if r == nil { if sz < 0 {
return nil, ErrHeaderNotFound return nil, ErrHeaderNotFound
} }
data := make([]byte, r.Size()) data := make([]byte, sz)
if _, err := r.MarshalTo(data); err != nil { return data, m.ReadSignedData(data)
return nil, err
} }
return data, nil // ReadSignedData copies payload bytes to passed buffer.
}
// ReadSignedData copies marshaled payload of the Put request to passed buffer.
// //
// If payload is nil, ErrHeaderNotFound returns. // If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m PutRequest) ReadSignedData(p []byte) error { func (m PutRequest) ReadSignedData(p []byte) error {
r := m.GetR() r := m.GetR()
if r == nil { if r == nil {
@ -48,16 +44,14 @@ func (m PutRequest) SignedDataSize() int {
return r.Size() return r.Size()
} }
// SignedData returns marshaled Address field. // SignedData returns payload bytes of the request.
//
// Resulting error is always nil.
func (m GetRequest) SignedData() ([]byte, error) { func (m GetRequest) SignedData() ([]byte, error) {
addr := m.GetAddress() data := make([]byte, m.SignedDataSize())
return addressBytes(addr), nil return data, m.ReadSignedData(data)
} }
// ReadSignedData copies marshaled Address field to passed buffer. // ReadSignedData copies payload bytes to passed buffer.
// //
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns. // If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m GetRequest) ReadSignedData(p []byte) error { func (m GetRequest) ReadSignedData(p []byte) error {
@ -74,29 +68,19 @@ func (m GetRequest) ReadSignedData(p []byte) error {
return nil return nil
} }
// SignedDataSize returns the size of object address. // SignedDataSize returns payload size of the request.
func (m GetRequest) SignedDataSize() int { func (m GetRequest) SignedDataSize() int {
return addressSize(m.GetAddress()) return addressSize(m.GetAddress())
} }
// SignedData returns marshaled Address field. // SignedData returns payload bytes of the request.
//
// Resulting error is always nil.
func (m HeadRequest) SignedData() ([]byte, error) { func (m HeadRequest) SignedData() ([]byte, error) {
sz := addressSize(m.Address) data := make([]byte, m.SignedDataSize())
data := make([]byte, sz+1) return data, m.ReadSignedData(data)
if m.GetFullHeaders() {
data[0] = 1
} }
copy(data[1:], addressBytes(m.Address)) // ReadSignedData copies payload bytes to passed buffer.
return data, nil
}
// ReadSignedData copies marshaled Address field to passed buffer.
// //
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns. // If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m HeadRequest) ReadSignedData(p []byte) error { func (m HeadRequest) ReadSignedData(p []byte) error {
@ -115,11 +99,38 @@ func (m HeadRequest) ReadSignedData(p []byte) error {
return nil return nil
} }
// SignedDataSize returns the size of object address. // SignedDataSize returns payload size of the request.
func (m HeadRequest) SignedDataSize() int { func (m HeadRequest) SignedDataSize() int {
return addressSize(m.Address) + 1 return addressSize(m.Address) + 1
} }
// SignedData returns payload bytes of the request.
func (m DeleteRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize())
return data, m.ReadSignedData(data)
}
// ReadSignedData copies payload bytes to passed buffer.
//
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m DeleteRequest) ReadSignedData(p []byte) error {
if len(p) < m.SignedDataSize() {
return io.ErrUnexpectedEOF
}
off := copy(p, m.OwnerID.Bytes())
copy(p[off:], addressBytes(m.Address))
return nil
}
// SignedDataSize returns payload size of the request.
func (m DeleteRequest) SignedDataSize() int {
return m.OwnerID.Size() + addressSize(m.Address)
}
func addressSize(addr Address) int { func addressSize(addr Address) int {
return addr.CID.Size() + addr.ObjectID.Size() return addr.CID.Size() + addr.ObjectID.Size()
} }

View file

@ -73,6 +73,22 @@ func TestSignVerifyRequests(t *testing.T) {
}, },
}, },
}, },
{ // DeleteRequest
constructor: func() sigType {
return new(DeleteRequest)
},
payloadCorrupt: []func(sigType){
func(s sigType) {
s.(*DeleteRequest).OwnerID[0]++
},
func(s sigType) {
s.(*DeleteRequest).Address.CID[0]++
},
func(s sigType) {
s.(*DeleteRequest).Address.ObjectID[0]++
},
},
},
} }
for _, item := range items { for _, item := range items {