Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

credentials: local creds implementation #3517

Merged
merged 9 commits into from
May 20, 2020
30 changes: 18 additions & 12 deletions test/local_creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"google.golang.org/grpc/credentials/local"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"

testpb "google.golang.org/grpc/test/grpc_testing"
)

Expand All @@ -48,11 +49,11 @@ func testE2ESucceed(network, address string) error {
switch network {
case "unix":
if secLevel != credentials.PrivacyAndIntegrity {
return nil, status.Errorf(codes.Unauthenticated, "Wrong security level: got %q, want %q", secLevel.String(), credentials.PrivacyAndIntegrity.String())
return nil, status.Errorf(codes.Unauthenticated, fmt.Sprintf("Wrong security level: got %q, want %q", secLevel, credentials.PrivacyAndIntegrity))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status.Errorf uses fmt.Sprintf already. Remove this call here.

}
case "tcp":
if secLevel != credentials.NoSecurity {
return nil, status.Errorf(codes.Unauthenticated, "Wrong security level: got %q, want %q", secLevel.String(), credentials.NoSecurity.String())
return nil, status.Errorf(codes.Unauthenticated, fmt.Sprintf("Wrong security level: got %q, want %q", secLevel, credentials.NoSecurity))
}
}
return &testpb.Empty{}, nil
Expand All @@ -65,6 +66,7 @@ func testE2ESucceed(network, address string) error {

testpb.RegisterTestServiceServer(s, ss)

var err error
lis, err := net.Listen(network, address)
if err != nil {
return fmt.Errorf("Failed to create listener: %v", err)
Expand All @@ -73,27 +75,34 @@ func testE2ESucceed(network, address string) error {
go s.Serve(lis)

var cc *grpc.ClientConn
lisAddr := address

switch network {
case "unix":
cc, err = grpc.Dial(address, grpc.WithTransportCredentials(local.NewCredentials()), grpc.WithContextDialer(
cc, err = grpc.Dial(lisAddr, grpc.WithTransportCredentials(local.NewCredentials()), grpc.WithContextDialer(
func(ctx context.Context, addr string) (net.Conn, error) {
return net.Dial("unix", address)
return net.Dial("unix", addr)
}))
case "tcp":
cc, err = grpc.Dial(lis.Addr().String(), grpc.WithTransportCredentials(local.NewCredentials()))
_, port, err := net.SplitHostPort(lis.Addr().String())
if err != nil {
return fmt.Errorf("Failed to parse listener address: %v", err)
}
lisAddr = "localhost:" + port
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you not do: lisAddr = lis.Addr().String() and remove the parsing?

cc, err = grpc.Dial(lisAddr, grpc.WithTransportCredentials(local.NewCredentials()))
default:
return fmt.Errorf("unsupported network %q", network)
}
if err != nil {
return fmt.Errorf("Failed to dial server: %v, %v", err, lis.Addr().String())
return fmt.Errorf("Failed to dial server: %v, %v", err, lisAddr)
}
defer cc.Close()

c := testpb.NewTestServiceClient(cc)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

if _, err := c.EmptyCall(ctx, &testpb.Empty{}); err != nil {
if _, err = c.EmptyCall(ctx, &testpb.Empty{}); err != nil {
return fmt.Errorf("EmptyCall(_, _) = _, %v; want _, <nil>", err)
}
return nil
Expand Down Expand Up @@ -193,10 +202,7 @@ func testE2EFail(dopts []grpc.DialOption) error {
}

func isExpected(got, want error) bool {
if status.Code(got) == status.Code(want) && strings.Contains(status.Convert(got).Message(), status.Convert(want).Message()) {
return true
}
return false
return status.Code(got) == status.Code(want) && strings.Contains(status.Convert(got).Message(), status.Convert(want).Message())
}

func (s) TestClientFail(t *testing.T) {
Expand All @@ -211,7 +217,7 @@ func (s) TestClientFail(t *testing.T) {
func (s) TestServerFail(t *testing.T) {
// Use insecure at client-side which should lead to server-side failure.
opts := []grpc.DialOption{grpc.WithInsecure()}
want := status.Error(codes.Unavailable, "connection closed")
want := status.Error(codes.Unavailable, "")
if err := testE2EFail(opts); !isExpected(err, want) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplify:

if err := testE2EFail(opts); status.Code(err) != codes.Unavailable {

t.Fatalf("testE2EFail() = %v; want %v", err, want)
}
Expand Down