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

Code before:
	}
	serverCreds = credentials.NewServerTLSFromCert(&cert)

	clientCreds, err = credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), "x.test.example.com")
	if err != nil {
		return nil, nil, fmt.Errorf("NewClientTLSFromFile: %w", err)
	}
	return serverCreds, clientCreds, nil
}

// Tests the scenario where the `grpc.CallAuthority` call option is used with
// different transport credentials. The test verifies that the specified
// authority is correctly propagated to the serve when a correct authority is
// used.
func (s) TestCorrectAuthorityWithCreds(t *testing.T) {
	const authority = "auth.test.example.com"

	serverTLS, clientTLS, err := loadTLSCreds()
	if err != nil {
		t.Fatalf("Failed to load TLS credentials: %v", err)
	}

	tests := []struct {
		name         string
		serverCreds  grpc.ServerOption
		clientCreds  grpc.DialOption
		expectedAuth string
	}{
		{
			name:         "Insecure",
			clientCreds:  grpc.WithTransportCredentials(insecure.NewCredentials()),
			expectedAuth: authority,
		},
		{
			name:         "Local",
			clientCreds:  grpc.WithTransportCredentials(local.NewCredentials()),
			expectedAuth: authority,
		},
		{
			name:         "TLS",
			serverCreds:  grpc.Creds(serverTLS),
			clientCreds:  grpc.WithTransportCredentials(clientTLS),
			expectedAuth: authority,
		},
	}

	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) {
					if err := authorityChecker(ctx, tt.expectedAuth); err != nil {

Review comment (suggestion):
You could consider replacing these two fields with a single field which will be a function that returns these two values. So something like
```golang
	tests := []struct {
		...
		creds: func() (grpc.ServerOption, grpc.DialOption),
		...
	}
```
With this approach, you wont need to call `loadTLSCreds` outside the table.

Code after:
		t.Fatalf("Failed to load key pair: %v", err)
		return nil, nil
	}
	serverCreds := grpc.Creds(credentials.NewServerTLSFromCert(&cert))

	clientCreds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), "x.test.example.com")
	if err != nil {
		t.Fatalf("Failed to create client credentials: %v", err)
	}
	return serverCreds, grpc.WithTransportCredentials(clientCreds)
}

// Tests the scenario where the `grpc.CallAuthority` call option is used with
// different transport credentials. The test verifies that the specified
// authority is correctly propagated to the serve when a correct authority is
// used.
func (s) TestCorrectAuthorityWithCreds(t *testing.T) {
	const authority = "auth.test.example.com"

	tests := []struct {
		name         string
		creds        func(t *testing.T) (grpc.ServerOption, grpc.DialOption)
		expectedAuth string
	}{
		{
			name: "Insecure",
			creds: func(t *testing.T) (grpc.ServerOption, grpc.DialOption) {
				c := insecure.NewCredentials()
				return grpc.Creds(c), grpc.WithTransportCredentials(c)
			},
			expectedAuth: authority,
		},
		{
			name: "Local",
			creds: func(t *testing.T) (grpc.ServerOption, grpc.DialOption) {
				c := local.NewCredentials()
				return grpc.Creds(c), grpc.WithTransportCredentials(c)
			},
			expectedAuth: authority,
		},
		{
			name: "TLS",
			creds: func(t *testing.T) (grpc.ServerOption, grpc.DialOption) {
				return loadTLSCreds(t)
			},
			expectedAuth: authority,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {