object: implement signing payload methods on GetRequest message

This commit is contained in:
Leonard Lyubich 2020-05-06 13:33:03 +03:00
parent 78f435a905
commit 439221cea8
2 changed files with 59 additions and 6 deletions

View file

@ -1,5 +1,9 @@
package object package object
import (
"io"
)
// SignedData returns marshaled payload of the Put request. // SignedData returns marshaled payload of the Put request.
// //
// If payload is nil, ErrHeaderNotFound returns. // If payload is nil, ErrHeaderNotFound returns.
@ -43,3 +47,39 @@ func (m PutRequest) SignedDataSize() int {
return r.Size() return r.Size()
} }
// SignedData returns marshaled Address field.
//
// Resulting error is always nil.
func (m GetRequest) SignedData() ([]byte, error) {
addr := m.GetAddress()
return append(
addr.CID.Bytes(),
addr.ObjectID.Bytes()...,
), nil
}
// ReadSignedData copies marshaled Address field to passed buffer.
//
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m GetRequest) ReadSignedData(p []byte) error {
addr := m.GetAddress()
if len(p) < addr.CID.Size()+addr.ObjectID.Size() {
return io.ErrUnexpectedEOF
}
off := copy(p, addr.CID.Bytes())
copy(p[off:], addr.ObjectID.Bytes())
return nil
}
// SignedDataSize returns the size of object address.
func (m GetRequest) SignedDataSize() int {
addr := m.GetAddress()
return addr.CID.Size() + addr.ObjectID.Size()
}

View file

@ -19,14 +19,14 @@ func TestSignVerifyRequests(t *testing.T) {
} }
items := []struct { items := []struct {
constructor func() sigType constructor func() sigType
bodyCorrupt []func(sigType) payloadCorrupt []func(sigType)
}{ }{
{ // PutRequest.PutHeader { // PutRequest.PutHeader
constructor: func() sigType { constructor: func() sigType {
return MakePutRequestHeader(new(Object)) return MakePutRequestHeader(new(Object))
}, },
bodyCorrupt: []func(sigType){ payloadCorrupt: []func(sigType){
func(s sigType) { func(s sigType) {
obj := s.(*PutRequest).GetR().(*PutRequest_Header).Header.GetObject() obj := s.(*PutRequest).GetR().(*PutRequest_Header).Header.GetObject()
obj.SystemHeader.PayloadLength++ obj.SystemHeader.PayloadLength++
@ -37,13 +37,26 @@ func TestSignVerifyRequests(t *testing.T) {
constructor: func() sigType { constructor: func() sigType {
return MakePutRequestChunk(make([]byte, 10)) return MakePutRequestChunk(make([]byte, 10))
}, },
bodyCorrupt: []func(sigType){ payloadCorrupt: []func(sigType){
func(s sigType) { func(s sigType) {
h := s.(*PutRequest).GetR().(*PutRequest_Chunk) h := s.(*PutRequest).GetR().(*PutRequest_Chunk)
h.Chunk[0]++ h.Chunk[0]++
}, },
}, },
}, },
{ // GetRequest
constructor: func() sigType {
return new(GetRequest)
},
payloadCorrupt: []func(sigType){
func(s sigType) {
s.(*GetRequest).Address.CID[0]++
},
func(s sigType) {
s.(*GetRequest).Address.ObjectID[0]++
},
},
},
} }
for _, item := range items { for _, item := range items {
@ -62,8 +75,8 @@ func TestSignVerifyRequests(t *testing.T) {
require.Error(t, service.VerifyAccumulatedSignaturesWithToken(v)) require.Error(t, service.VerifyAccumulatedSignaturesWithToken(v))
} }
{ // body corruptions { // payload corruptions
for _, corruption := range item.bodyCorrupt { for _, corruption := range item.payloadCorrupt {
v := item.constructor() v := item.constructor()
require.NoError(t, service.SignDataWithSessionToken(sk, v)) require.NoError(t, service.SignDataWithSessionToken(sk, v))