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

Code before:
// Tests that the MinVersion of tls.Config is set to 1.2 if it is not already
// set by the user.
func (s) TestTLS_MinVersion12(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	serverTLS := &tls.Config{
		// MinVersion should be set to 1.2 by gRPC by default.
		Certificates: []tls.Certificate{serverCert},
	}
	noOpGetCfgForClient := serverTLS.Clone()
	noOpGetCfgForClient.GetConfigForClient = func(*tls.ClientHelloInfo) (*tls.Config, error) {
		return nil, nil
	}

	testCases := []struct {
		name      string
		serverTLS *tls.Config
	}{
		{
			name:      "base_case",
			serverTLS: serverTLS,
		},
		{
			name:      "fallback_to_base",
			serverTLS: noOpGetCfgForClient,
		},
		{
			name: "dynamic_using_get_config_for_client",
			serverTLS: &tls.Config{
				GetConfigForClient: func(*tls.ClientHelloInfo) (*tls.Config, error) {
					return serverTLS, nil
				},
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			// Create server creds without a minimum version.
			serverCreds := credentials.NewTLS(tc.serverTLS)
			ss := stubserver.StubServer{
				EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
					return &testpb.Empty{}, nil
				},
			}

			// Create client creds that supports V1.0-V1.1.
			clientCreds := credentials.NewTLS(&tls.Config{
				ServerName: serverName,
				RootCAs:    certPool,

Review comment (suggestion):
Optional:

```golang
	serverTLS: func() *tls.Config {
		cfg := serverTLS.Clone()
		cfg.GetConfigForClient = func(*tls.ClientHelloInfo) (*tls.Config, error) { return nil, nil }
		return cfg
	}()
```

This will get rid of the `noOpGetCfgForClient` var which is only used by this subtest.

Code after:
// Tests that the MinVersion of tls.Config is set to 1.2 if it is not already
// set by the user.
func (s) TestTLS_MinVersion12(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	testCases := []struct {
		name      string
		serverTLS func() *tls.Config
	}{
		{
			name: "base_case",
			serverTLS: func() *tls.Config {
				return &tls.Config{
					// MinVersion should be set to 1.2 by gRPC by default.
					Certificates: []tls.Certificate{serverCert},
				}
			},
		},
		{
			name: "fallback_to_base",
			serverTLS: func() *tls.Config {
				config := &tls.Config{
					// MinVersion should be set to 1.2 by gRPC by default.
					Certificates: []tls.Certificate{serverCert},
				}
				config.GetConfigForClient = func(*tls.ClientHelloInfo) (*tls.Config, error) {
					return nil, nil
				}
				return config
			},
		},
		{
			name: "dynamic_using_get_config_for_client",
			serverTLS: func() *tls.Config {
				return &tls.Config{
					GetConfigForClient: func(*tls.ClientHelloInfo) (*tls.Config, error) {
						return &tls.Config{
							// MinVersion should be set to 1.2 by gRPC by default.
							Certificates: []tls.Certificate{serverCert},
						}, nil
					},
				}
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			// Create server creds without a minimum version.
			serverCreds := credentials.NewTLS(tc.serverTLS())