From 1592151621722488848971cf4b86983cec665409 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 18 Aug 2020 12:01:08 +0300 Subject: [PATCH] Add stable marshal for session package Signed-off-by: Alex Vanin --- v2/session/marshal.go | 93 ++++++++++++++++++++++++++++++++++++++ v2/session/marshal_test.go | 67 +++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 v2/session/marshal.go create mode 100644 v2/session/marshal_test.go diff --git a/v2/session/marshal.go b/v2/session/marshal.go new file mode 100644 index 00000000..2256b900 --- /dev/null +++ b/v2/session/marshal.go @@ -0,0 +1,93 @@ +package session + +import ( + "github.com/nspcc-dev/neofs-api-go/util/proto" +) + +const ( + createReqBodyOwnerField = 1 + createReqBodyLifetimeField = 2 + + createRespBodyIDField = 1 + createRespBodyKeyField = 2 +) + +func (c *CreateRequestBody) StableMarshal(buf []byte) ([]byte, error) { + if c == nil { + return []byte{}, nil + } + + if buf == nil { + buf = make([]byte, c.StableSize()) + } + + var ( + offset, n int + err error + ) + + n, err = proto.NestedStructureMarshal(createReqBodyOwnerField, buf[offset:], c.ownerID) + if err != nil { + return nil, err + } + + offset += n + + _, err = proto.NestedStructureMarshal(createReqBodyLifetimeField, buf[offset:], c.lifetime) + if err != nil { + return nil, err + } + + return buf, nil +} + +func (c *CreateRequestBody) StableSize() (size int) { + if c == nil { + return 0 + } + + size += proto.NestedStructureSize(createReqBodyOwnerField, c.ownerID) + size += proto.NestedStructureSize(createReqBodyLifetimeField, c.lifetime) + + return size +} + +func (c *CreateResponseBody) StableMarshal(buf []byte) ([]byte, error) { + if c == nil { + return []byte{}, nil + } + + if buf == nil { + buf = make([]byte, c.StableSize()) + } + + var ( + offset, n int + err error + ) + + n, err = proto.BytesMarshal(createRespBodyIDField, buf[offset:], c.id) + if err != nil { + return nil, err + } + + offset += n + + _, err = proto.BytesMarshal(createRespBodyKeyField, buf[offset:], c.sessionKey) + if err != nil { + return nil, err + } + + return buf, nil +} + +func (c *CreateResponseBody) StableSize() (size int) { + if c == nil { + return 0 + } + + size += proto.BytesSize(createRespBodyIDField, c.id) + size += proto.BytesSize(createRespBodyKeyField, c.sessionKey) + + return size +} diff --git a/v2/session/marshal_test.go b/v2/session/marshal_test.go new file mode 100644 index 00000000..685a4775 --- /dev/null +++ b/v2/session/marshal_test.go @@ -0,0 +1,67 @@ +package session_test + +import ( + "testing" + + "github.com/nspcc-dev/neofs-api-go/v2/refs" + "github.com/nspcc-dev/neofs-api-go/v2/service" + "github.com/nspcc-dev/neofs-api-go/v2/session" + grpc "github.com/nspcc-dev/neofs-api-go/v2/session/grpc" + "github.com/stretchr/testify/require" +) + +func TestCreateRequestBody_StableMarshal(t *testing.T) { + requestFrom := generateCreateSessionRequestBody("Owner ID") + transport := new(grpc.CreateRequest_Body) + + t.Run("non empty", func(t *testing.T) { + wire, err := requestFrom.StableMarshal(nil) + require.NoError(t, err) + + err = transport.Unmarshal(wire) + require.NoError(t, err) + + requestTo := session.CreateRequestBodyFromGRPCMessage(transport) + require.Equal(t, requestFrom, requestTo) + }) +} + +func TestCreateResponseBody_StableMarshal(t *testing.T) { + responseFrom := generateCreateSessionResponseBody("ID", "Session Public Key") + transport := new(grpc.CreateResponse_Body) + + t.Run("non empty", func(t *testing.T) { + wire, err := responseFrom.StableMarshal(nil) + require.NoError(t, err) + + err = transport.Unmarshal(wire) + require.NoError(t, err) + + responseTo := session.CreateResponseBodyFromGRPCMessage(transport) + require.Equal(t, responseFrom, responseTo) + }) +} + +func generateCreateSessionRequestBody(id string) *session.CreateRequestBody { + lifetime := new(service.TokenLifetime) + lifetime.SetIat(1) + lifetime.SetNbf(2) + lifetime.SetExp(3) + + owner := new(refs.OwnerID) + owner.SetValue([]byte(id)) + + s := new(session.CreateRequestBody) + s.SetOwnerID(owner) + s.SetLifetime(lifetime) + + return s +} + +func generateCreateSessionResponseBody(id, key string) *session.CreateResponseBody { + s := new(session.CreateResponseBody) + s.SetID([]byte(id)) + s.SetSessionKey([]byte(key)) + + return s +}