diff --git a/test/cpp/util/create_test_channel.cc b/test/cpp/util/create_test_channel.cc index afac38abd3cee34bf5282619f573afc5beffe19d..14b566fb12da3fe2f59e709ccd21320bfebfc37e 100644 --- a/test/cpp/util/create_test_channel.cc +++ b/test/cpp/util/create_test_channel.cc @@ -40,8 +40,17 @@ namespace grpc { -std::shared_ptr<ChannelInterface> CreateTestChannel(const grpc::string& server, - bool enable_ssl) { +// When ssl is enabled, if server is empty, override_hostname is used to +// create channel. Otherwise, connect to server and override hostname if +// override_hostname is provided. +// When ssl is not enabled, override_hostname is ignored. +// Use examples: +// CreateTestChannel("1.1.1.1:12345", "override.hostname.com", true); +// CreateTestChannel("test.google.com:443", "", true); +// CreateTestChannel("", "test.google.com:443", true); // same as above +std::shared_ptr<ChannelInterface> CreateTestChannel( + const grpc::string& server, const grpc::string& override_hostname, + bool enable_ssl) { ChannelArguments channel_args; if (enable_ssl) { SslCredentialsOptions ssl_opts = { @@ -52,11 +61,21 @@ std::shared_ptr<ChannelInterface> CreateTestChannel(const grpc::string& server, std::unique_ptr<Credentials> creds = CredentialsFactory::SslCredentials(ssl_opts); - channel_args.SetSslTargetNameOverride("foo.test.google.com"); - return CreateChannel(server, creds, channel_args); + if (!server.empty() && !override_hostname.empty()) { + channel_args.SetSslTargetNameOverride(override_hostname); + } + const grpc::string& connect_to = + server.empty() ? override_hostname : server; + return CreateChannel(connect_to, creds, channel_args); } else { return CreateChannel(server, channel_args); } } +// Shortcut for end2end and interop tests. +std::shared_ptr<ChannelInterface> CreateTestChannel(const grpc::string& server, + bool enable_ssl) { + return CreateTestChannel(server, "foo.test.google.com", enable_ssl); +} + } // namespace grpc diff --git a/test/cpp/util/create_test_channel.h b/test/cpp/util/create_test_channel.h index c705e59ddf8a4462d8e6b15ef9d207fe4d4ce747..10e301cb21cb525892c941c962307769064f3019 100644 --- a/test/cpp/util/create_test_channel.h +++ b/test/cpp/util/create_test_channel.h @@ -41,6 +41,10 @@ namespace grpc { class ChannelInterface; +std::shared_ptr<ChannelInterface> CreateTestChannel( + const grpc::string& server, const grpc::string& override_hostname, + bool enable_ssl); + std::shared_ptr<ChannelInterface> CreateTestChannel(const grpc::string& server, bool enable_ssl);