Skip to content

Commit

Permalink
rpc/client: Add unit test for TLS handshake failure
Browse files Browse the repository at this point in the history
Inspired by nspcc-dev/neofs-node#2561.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Feb 28, 2024
1 parent fb2ed14 commit a134213
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
6 changes: 3 additions & 3 deletions rpc/client/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c *Client) createGRPCClient(ctx context.Context) (err error) {

var errInvalidEndpoint = errors.New("invalid endpoint options")

func (c *Client) openGRPCConn(ctx context.Context) error {
func (c *Client) openGRPCConn(ctx context.Context, extraDialOpts ...grpcstd.DialOption) error {
if c.conn != nil {
return nil
}
Expand All @@ -51,10 +51,10 @@ func (c *Client) openGRPCConn(ctx context.Context) error {
dialCtx, cancel := context.WithTimeout(ctx, c.dialTimeout)
var err error

c.conn, err = grpcstd.DialContext(dialCtx, c.addr,
c.conn, err = grpcstd.DialContext(dialCtx, c.addr, append([]grpcstd.DialOption{
grpcstd.WithTransportCredentials(creds),
grpcstd.WithBlock(),
)
}, extraDialOpts...)...)

cancel()

Expand Down
35 changes: 35 additions & 0 deletions rpc/client/connect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package client

import (
"context"
"crypto/tls"
"net"
"testing"
"time"

"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
)

func TestClient_Init(t *testing.T) {
t.Run("TLS handshake failure", func(t *testing.T) {
lis := bufconn.Listen(1024) // size does not matter in this test

srv := grpc.NewServer()
t.Cleanup(srv.Stop)
go func() { _ = srv.Serve(lis) }()

c := New(WithNetworkURIAddress("grpcs://any:54321", new(tls.Config))...)

ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
t.Cleanup(cancel)

err := c.openGRPCConn(ctx, grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
return lis.DialContext(ctx)
}))
// error is not wrapped properly, so we can do nothing more to check it.
// Text from stdlib tls.Conn.HandshakeContext.
require.ErrorContains(t, err, "first record does not look like a TLS handshake")
})
}

0 comments on commit a134213

Please sign in to comment.