object: fix signed payload calculation of HeadRequest message

In previous implementation first byte of buffer for HeadRequest
signed payload was set to 1 if FullHeaders flag was set. Otherwise,
this byte remained unchanged. For correct recording of a signed payload,
it is necessary to explicitly set the first byte with the unset flag.
This commit is contained in:
Leonard Lyubich 2020-05-18 12:48:04 +03:00
parent 2c571718d0
commit 1931bd590d
2 changed files with 23 additions and 15 deletions

View file

@ -82,6 +82,8 @@ func (m HeadRequest) ReadSignedData(p []byte) (int, error) {
if m.GetFullHeaders() { if m.GetFullHeaders() {
p[0] = 1 p[0] = 1
} else {
p[0] = 0
} }
off := 1 off := 1

View file

@ -1,6 +1,7 @@
package object package object
import ( import (
"crypto/rand"
"testing" "testing"
"github.com/nspcc-dev/neofs-api-go/service" "github.com/nspcc-dev/neofs-api-go/service"
@ -192,24 +193,29 @@ func TestHeadRequest_ReadSignedData(t *testing.T) {
t.Run("full headers", func(t *testing.T) { t.Run("full headers", func(t *testing.T) {
req := new(HeadRequest) req := new(HeadRequest)
// allocate buffer for reading // unset FullHeaders flag
buf := make([]byte, req.SignedDataSize())
buf[0] = 2
// set FullHeaders option
req.SetFullHeaders(true)
_, err := req.ReadSignedData(buf)
require.NoError(t, err)
require.Equal(t, byte(1), buf[0])
// unset FullHeaders option
req.SetFullHeaders(false) req.SetFullHeaders(false)
_, err = req.ReadSignedData(buf) // allocate two different buffers for reading
buf1 := testData(t, req.SignedDataSize())
buf2 := testData(t, req.SignedDataSize())
// read to both buffers
n1, err := req.ReadSignedData(buf1)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, byte(0), buf[0]) n2, err := req.ReadSignedData(buf2)
require.NoError(t, err)
require.Equal(t, buf1[:n1], buf2[:n2])
}) })
} }
func testData(t *testing.T, sz int) []byte {
data := make([]byte, sz)
_, err := rand.Read(data)
require.NoError(t, err)
return data
}