From 2c571718d06d635f1cb5ca7f8bcb134a222f5357 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 18 May 2020 12:47:13 +0300 Subject: [PATCH 1/2] object: add unit test of HeadRequest.ReadSignedData method --- object/sign_test.go | 26 ++++++++++++++++++++++++++ object/types.go | 5 +++++ 2 files changed, 31 insertions(+) diff --git a/object/sign_test.go b/object/sign_test.go index 4df1c2b..fb57baa 100644 --- a/object/sign_test.go +++ b/object/sign_test.go @@ -187,3 +187,29 @@ func TestSignVerifyRequests(t *testing.T) { } } } + +func TestHeadRequest_ReadSignedData(t *testing.T) { + t.Run("full headers", func(t *testing.T) { + req := new(HeadRequest) + + // allocate buffer for reading + 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) + + _, err = req.ReadSignedData(buf) + require.NoError(t, err) + + require.Equal(t, byte(0), buf[0]) + }) +} diff --git a/object/types.go b/object/types.go index c8d3f25..4edf5f4 100644 --- a/object/types.go +++ b/object/types.go @@ -386,3 +386,8 @@ func Stringify(dst io.Writer, obj *Object) error { return nil } + +// SetFullHeaders is a FullHeaders field setter. +func (m *HeadRequest) SetFullHeaders(v bool) { + m.FullHeaders = v +} From 1931bd590dbcee33e904a99d822ee3e494b777d5 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 18 May 2020 12:48:04 +0300 Subject: [PATCH 2/2] 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. --- object/sign.go | 2 ++ object/sign_test.go | 36 +++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/object/sign.go b/object/sign.go index 1ed3efa..def7bc3 100644 --- a/object/sign.go +++ b/object/sign.go @@ -82,6 +82,8 @@ func (m HeadRequest) ReadSignedData(p []byte) (int, error) { if m.GetFullHeaders() { p[0] = 1 + } else { + p[0] = 0 } off := 1 diff --git a/object/sign_test.go b/object/sign_test.go index fb57baa..abf4549 100644 --- a/object/sign_test.go +++ b/object/sign_test.go @@ -1,6 +1,7 @@ package object import ( + "crypto/rand" "testing" "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) { req := new(HeadRequest) - // allocate buffer for reading - 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 + // unset FullHeaders flag 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.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 +}