Repository: grpc/grpc-go
File: credentials/credentials_ext_test.go (Go)

Code before:
		t.Fatalf("EmptyCall() should fail")
	}
	s, ok := status.FromError(err)
	if !ok {
		t.Fatalf("unexpected error: %v", err)
	}
	if s.Code() != codes.Unavailable {
		t.Fatalf("EmptyCall() = _, %v, want _, error code: %v", s.Code(), codes.Unavailable)
	}
}

// Tests the grpc.CallAuthority option with TLS credentials. This test verifies
// that the provided authority is correctly propagated to the server when using TLS.
// It covers both positive and negative cases:  correct authority and incorrect
// authority, expecting the RPC to fail with `UNAVAILABLE` status code error in
// the latter case.
func (s) TestAuthorityCallOptionsWithTLSCreds(t *testing.T) {
	tests := []struct {
		name         string
		expectedAuth string
		wantRPCError bool
	}{
		{
			name:         "CorrectAuthorityWithTLS",
			expectedAuth: "auth.test.example.com",
			wantRPCError: false,
		},
		{
			name:         "IncorrectAuthorityWithTLS",
			expectedAuth: "auth.example.com",
			wantRPCError: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ss := &stubserver.StubServer{
				EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
					return authorityChecker(ctx, tt.expectedAuth)
				},
			}
			if err := ss.StartServer(grpc.Creds(credentials.NewServerTLSFromCert(&cert))); err != nil {
				t.Fatalf("Error starting endpoint server: %v", err)
			}
			defer ss.Stop()

			cc, err := grpc.NewClient(ss.Address, grpc.WithTransportCredentials(creds))
			if err != nil {
				t.Fatalf("grpc.NewClient(%q) = %v", ss.Address, err)
			}
			defer cc.Close()


Review comment (security):
What if you simplified this by making this field `wantStatus`. So, in this subtest the field will be set to `codes.Ok` and the next one will have `codes.Unavailable`. And you wont need the `checkUnavailableRPCError` helper function at all.

The last line can be rewritten as
client := testgrpc.NewTestServiceClient(cc)
if _, err :=client.EmptyCall(ctx, &testpb.Empty{}, grpc.CallAuthority(tt.expectedAuth)); status.Code(err) != tt.wantStatus {
  t.Fatalf("EmptyCall returned status: %v, want %v", status.Code(err), tt.wantStatus)
}


Code after:
			}
			return &testpb.Empty{}, nil
		},
	}
	if err := ss.StartServer(grpc.Creds(credentials.NewServerTLSFromCert(&cert))); err != nil {
		t.Fatalf("Error starting endpoint server: %v", err)
	}
	defer ss.Stop()

	cc, err := grpc.NewClient(ss.Address, grpc.WithTransportCredentials(creds))
	if err != nil {
		t.Fatalf("grpc.NewClient(%q) = %v", ss.Address, err)
	}
	defer cc.Close()

	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	if _, err = testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{}, grpc.CallAuthority(authority)); status.Code(err) != codes.OK {
		t.Fatalf("EmptyCall() returned status %v, want %v", status.Code(err), codes.OK)
	}

}

// Tests the `grpc.CallAuthority` option with TLS credentials. This test verifies
// that the RPC fails with `UNAVAILABLE` status code and doesn't reach the server
// when an incorrect authority is used.
func (s) TestIncorrectAuthorityWithTLS(t *testing.T) {
	cert, err := tls.LoadX509KeyPair(testdata.Path("x509/server1_cert.pem"), testdata.Path("x509/server1_key.pem"))
	if err != nil {
		t.Fatalf("Failed to load key pair: %s", err)
	}
	creds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), "x.test.example.com")
	if err != nil {
		t.Fatalf("Failed to create credentials %v", err)
	}

	serverCalled := make(chan struct{})
	ss := &stubserver.StubServer{
		EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
			close(serverCalled)
			return nil, nil
		},
	}
	if err := ss.StartServer(grpc.Creds(credentials.NewServerTLSFromCert(&cert))); err != nil {
		t.Fatalf("Error starting endpoint server: %v", err)
	}
	defer ss.Stop()

	cc, err := grpc.NewClient(ss.Address, grpc.WithTransportCredentials(creds))
	if err != nil {