Add stable marshal for session package

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-08-18 12:01:08 +03:00 committed by Stanislav Bogatyrev
parent 7a55865f74
commit 1592151621
2 changed files with 160 additions and 0 deletions

93
v2/session/marshal.go Normal file
View file

@ -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
}

View file

@ -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
}