Skip to content

Commit

Permalink
WIP: client: Test that request messages are constructed as expected
Browse files Browse the repository at this point in the history
Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Nov 21, 2024
1 parent ce07a83 commit 67c6c9f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
76 changes: 75 additions & 1 deletion client/accounting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package client

import (
"context"
"errors"
"fmt"
"testing"

v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
protoaccounting "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test"
"github.com/nspcc-dev/neofs-sdk-go/user"
"github.com/nspcc-dev/neofs-sdk-go/version"
"github.com/stretchr/testify/require"
)

Expand All @@ -18,9 +22,79 @@ func newTestAccountingClient(t testing.TB, srv protoaccounting.AccountingService

type testGetBalanceServer struct {
protoaccounting.UnimplementedAccountingServiceServer

// client input
xhdrs []string
}

func (x *testGetBalanceServer) verifyBalanceRequest(req *protoaccounting.BalanceRequest) error {
// signatures
var reqV2 v2accounting.BalanceRequest
if err := reqV2.FromGRPCMessage(req); err != nil {
panic(err)
}
if err := verifyServiceMessage(&reqV2); err != nil {
return newInvalidRequestVerificationHeaderErr(err)
}
// meta header
metaHdr := req.MetaHeader
curVersion := version.Current()
switch {
case metaHdr == nil:
return newInvalidRequestErr(errors.New("missing meta header"))
case metaHdr.Version == nil:
return newInvalidRequestMetaHeaderErr(errors.New("missing protocol version"))
case metaHdr.Version.Major != curVersion.Major() || metaHdr.Version.Minor != curVersion.Minor():
return newInvalidRequestMetaHeaderErr(fmt.Errorf("wrong protocol version v%d.%d, expected %s",
metaHdr.Version.Major, metaHdr.Version.Minor, curVersion))
case metaHdr.Epoch != 0:
return newInvalidRequestMetaHeaderErr(fmt.Errorf("non-zero epoch #%d", metaHdr.Epoch))
case metaHdr.Ttl != 2:
return newInvalidRequestMetaHeaderErr(fmt.Errorf("wrong TTL %d, expected 2", metaHdr.Epoch))
case metaHdr.SessionToken != nil:
return newInvalidRequestMetaHeaderErr(errors.New("session token attached while should not be"))
case metaHdr.BearerToken != nil:
return newInvalidRequestMetaHeaderErr(errors.New("bearer token attached while should not be"))
case metaHdr.MagicNumber != 0:
return newInvalidRequestMetaHeaderErr(fmt.Errorf("non-zero network magic #%d", metaHdr.MagicNumber))
case metaHdr.Origin != nil:
return newInvalidRequestMetaHeaderErr(errors.New("origin header is presented while should not be"))
case len(metaHdr.XHeaders) != len(x.xhdrs)/2:
return newInvalidRequestMetaHeaderErr(fmt.Errorf("number of x-headers %d differs parameterized %d",
len(metaHdr.XHeaders), len(x.xhdrs)/2))
}
for i := range metaHdr.XHeaders {
if metaHdr.XHeaders[i].Key != x.xhdrs[2*i] {
return newInvalidRequestMetaHeaderErr(fmt.Errorf("x-header #%d key %q does not equal parameterized %q",
i, metaHdr.XHeaders[i].Key, x.xhdrs[2*i]))
}
if metaHdr.XHeaders[i].Value != x.xhdrs[2*i+1] {
return newInvalidRequestMetaHeaderErr(fmt.Errorf("x-header #%d value %q does not equal parameterized %q",
i, metaHdr.XHeaders[i].Value, x.xhdrs[2*i+1]))
}
}
// body
switch {
case req.Body == nil:
return newInvalidRequestBodyErr(errors.New("missing body"))
case req.Body.OwnerId == nil:
return newErrMissingRequestBodyField("account")
}
var usrV2 refs.OwnerID
if err := usrV2.FromGRPCMessage(req.Body.OwnerId); err != nil {
panic(err)
}
if err := new(user.ID).ReadFromV2(usrV2); err != nil {
return newErrInvalidRequestField("account", err)
}
return nil
}

func (x *testGetBalanceServer) Balance(context.Context, *protoaccounting.BalanceRequest) (*protoaccounting.BalanceResponse, error) {
func (x *testGetBalanceServer) Balance(_ context.Context, req *protoaccounting.BalanceRequest) (*protoaccounting.BalanceResponse, error) {
if err := x.verifyBalanceRequest(req); err != nil {
return nil, err
}

resp := protoaccounting.BalanceResponse{
Body: &protoaccounting.BalanceResponse_Body{
Balance: new(protoaccounting.Decimal),
Expand Down
25 changes: 25 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"fmt"
"net"
"testing"

Expand All @@ -24,6 +25,30 @@ func init() {
statusErr.SetMessage("test status error")
}

func newInvalidRequestErr(cause error) error {
return fmt.Errorf("invalid request: %w", cause)
}

func newInvalidRequestMetaHeaderErr(cause error) error {
return newInvalidRequestErr(fmt.Errorf("invalid meta header: %w", cause))
}

func newInvalidRequestVerificationHeaderErr(cause error) error {
return newInvalidRequestErr(fmt.Errorf("invalid verification header: %w", cause))
}

func newInvalidRequestBodyErr(cause error) error {
return newInvalidRequestErr(fmt.Errorf("invalid body: %w", cause))
}

func newErrMissingRequestBodyField(name string) error {
return newInvalidRequestBodyErr(fmt.Errorf("missing %s field", name))
}

func newErrInvalidRequestField(name string, err error) error {
return newInvalidRequestBodyErr(fmt.Errorf("invalid %s field: %w", name, err))
}

// pairs service spec and implementation to-be-registered in some [grpc.Server].
type testService struct {
desc *grpc.ServiceDesc
Expand Down

0 comments on commit 67c6c9f

Please sign in to comment.