Repository: grpc/grpc-go
File: xds/internal/xdsclient/transport/transport_test.go (Go)

Code before:
	}
}

const testDialerCredsBuilderName = "test_dialer_creds"

func init() {
	bootstrap.RegisterCredentials(&testDialerCredsBuilder{})
}

// testDialerCredsBuilder implements the `Credentials` interface defined in
// package `xds/bootstrap` and encapsulates an insecure credential with a
// custom Dialer that specifies how to dial the xDS server.
type testDialerCredsBuilder struct{}

func (t *testDialerCredsBuilder) Build(json.RawMessage) (credentials.Bundle, func(), error) {
	return &testDialerCredsBundle{}, func() {}, nil
}

func (t *testDialerCredsBuilder) Name() string {
	return testDialerCredsBuilderName
}

// testDialerCredsBundle implements the `Bundle` interface defined in package
// `credentials` and encapsulates an insecure credential with a custom Dialer
// that specifies how to dial the xDS server.
type testDialerCredsBundle struct{}

func (t *testDialerCredsBundle) TransportCredentials() credentials.TransportCredentials {
	return insecure.NewCredentials()
}

func (t *testDialerCredsBundle) PerRPCCredentials() credentials.PerRPCCredentials {
	return nil
}

func (t *testDialerCredsBundle) NewWithMode(string) (credentials.Bundle, error) {
	return &testDialerCredsBundle{}, nil
}

func (t *testDialerCredsBundle) Dialer(context.Context, string) (net.Conn, error) {
	return nil, nil
}

func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
	serverCfg, err := internalbootstrap.ServerConfigForTesting(internalbootstrap.ServerConfigTestingOptions{
		URI:          "trafficdirector.googleapis.com:443",
		ChannelCreds: []internalbootstrap.ChannelCreds{{Type: testDialerCredsBuilderName}},
	})
	if err != nil {
		t.Fatalf("Failed to create server config for testing: %v", err)
	}

Review comment (security):
Would embedding an `credentials.Bundle` here and setting it to `insecure.NewBundle` from `testDialerCredsBuilder.Build`, help to get rid of the implementation of the three methods below?

Code after:
	}
}

const testDialerCredsBuilderName = "test_dialer_creds"

// testDialerCredsBuilder implements the `Credentials` interface defined in
// package `xds/bootstrap` and encapsulates an insecure credential with a
// custom Dialer that specifies how to dial the xDS server.
type testDialerCredsBuilder struct{}

func (t *testDialerCredsBuilder) Build(json.RawMessage) (credentials.Bundle, func(), error) {
	return &testDialerCredsBundle{insecure.NewBundle()}, func() {}, nil
}

func (t *testDialerCredsBuilder) Name() string {
	return testDialerCredsBuilderName
}

// testDialerCredsBundle implements the `Bundle` interface defined in package
// `credentials` and encapsulates an insecure credential with a custom Dialer
// that specifies how to dial the xDS server.
type testDialerCredsBundle struct {
	credentials.Bundle
}

func (t *testDialerCredsBundle) Dialer(context.Context, string) (net.Conn, error) {
	return nil, nil
}

func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
	// Override grpc.NewClient with a custom one.
	doptsLen := 0
	customGRPCNewClient := func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
		doptsLen = len(opts)
		return grpc.NewClient(target, opts...)
	}
	oldGRPCNewClient := internal.GRPCNewClient
	internal.GRPCNewClient = customGRPCNewClient
	defer func() { internal.GRPCNewClient = oldGRPCNewClient }()

	bootstrap.RegisterCredentials(&testDialerCredsBuilder{})
	serverCfg, err := internalbootstrap.ServerConfigForTesting(internalbootstrap.ServerConfigTestingOptions{
		URI:          "trafficdirector.googleapis.com:443",
		ChannelCreds: []internalbootstrap.ChannelCreds{{Type: testDialerCredsBuilderName}},
	})
	if err != nil {
		t.Fatalf("Failed to create server config for testing: %v", err)
	}
	if serverCfg.DialerOption() == nil {
		t.Fatalf("Dialer for xDS transport in server config for testing is nil, want non-nil")
	}