frostfs-api-go/session/create_test.go
Leonard Lyubich 74e917810a service: support broken apart signable payload of the requests
In previous implementation service package provided types and functions
that wrapped signing/verification of data with session token.
This allowed us to use these functions for signing / verification of
service requests of other packages. To support the expansion of messages
with additional parts that need to be signed, you must be able to easily
expand the signed data with new parts.

To achieve the described goal, this commit makes the following changes:

  * adds GroupSignedPayloads and GroupVerifyPayloads functions;

  * renames SignedDataWithToken to RequestData, DataWithTokenSignAccumulator
    to RequestSignedData, DataWithTokenSignSource to RequestVerifyData;

  * renames SignDataWithSessionToken/VerifyAccumulatedSignaturesWithToken
    function to SignRequestData/VerifyRequestData and makes it to use
    GroupSignedPayloads/GroupVerifyPayloads internally.
2020-06-10 20:37:10 +03:00

103 lines
2.2 KiB
Go

package session
import (
"context"
"crypto/ecdsa"
"testing"
"github.com/nspcc-dev/neofs-api-go/service"
crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/neofs-crypto/test"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
type testSessionClient struct {
fn func(*CreateRequest)
resp *CreateResponse
err error
}
func (s testSessionClient) Create(ctx context.Context, in *CreateRequest, opts ...grpc.CallOption) (*CreateResponse, error) {
if s.fn != nil {
s.fn(in)
}
return s.resp, s.err
}
func TestNewGRPCCreator(t *testing.T) {
var (
err error
conn = new(grpc.ClientConn)
sk = new(ecdsa.PrivateKey)
)
// nil client connection
_, err = NewGRPCCreator(nil, sk)
require.EqualError(t, err, ErrNilGPRCClientConn.Error())
// nil private key
_, err = NewGRPCCreator(conn, nil)
require.EqualError(t, err, crypto.ErrEmptyPrivateKey.Error())
// valid params
res, err := NewGRPCCreator(conn, sk)
require.NoError(t, err)
v := res.(*gRPCCreator)
require.Equal(t, conn, v.conn)
require.Equal(t, sk, v.key)
require.NotNil(t, v.clientFunc)
}
func TestGRPCCreator_Create(t *testing.T) {
ctx := context.TODO()
s := new(gRPCCreator)
// nil CreateParamsSource
_, err := s.Create(ctx, nil)
require.EqualError(t, err, ErrNilCreateParamsSource.Error())
var (
ownerID = OwnerID{1, 2, 3}
created = uint64(2)
expired = uint64(4)
)
p := NewParams()
p.SetOwnerID(ownerID)
p.SetCreationEpoch(created)
p.SetExpirationEpoch(expired)
// nil private key
_, err = s.Create(ctx, p)
require.Error(t, err)
// create test private key
s.key = test.DecodeKey(0)
// create test client
c := &testSessionClient{
fn: func(req *CreateRequest) {
require.Equal(t, ownerID, req.GetOwnerID())
require.Equal(t, created, req.CreationEpoch())
require.Equal(t, expired, req.ExpirationEpoch())
require.NoError(t, service.VerifyRequestData(req))
},
resp: &CreateResponse{
ID: TokenID{1, 2, 3},
SessionKey: []byte{1, 2, 3},
},
err: errors.New("test error"),
}
s.clientFunc = func(*grpc.ClientConn) SessionClient {
return c
}
res, err := s.Create(ctx, p)
require.EqualError(t, err, c.err.Error())
require.Equal(t, c.resp, res)
}