3 * Copyright 2015-2016 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "test/cpp/util/create_test_channel.h"
21 #include <grpc/support/log.h>
22 #include <grpcpp/create_channel.h>
23 #include <grpcpp/security/credentials.h>
25 #include "test/cpp/util/test_credentials_provider.h"
31 const char kProdTlsCredentialsType[] = "prod_ssl";
33 class SslCredentialProvider : public testing::CredentialTypeProvider {
35 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
36 grpc::ChannelArguments* args) override {
37 return SslCredentials(SslCredentialsOptions());
39 std::shared_ptr<ServerCredentials> GetServerCredentials() override {
44 gpr_once g_once_init_add_prod_ssl_provider = GPR_ONCE_INIT;
45 // Register ssl with non-test roots type to the credentials provider.
46 void AddProdSslType() {
47 testing::GetCredentialsProvider()->AddSecureType(
48 kProdTlsCredentialsType, std::unique_ptr<testing::CredentialTypeProvider>(
49 new SslCredentialProvider));
54 // When cred_type is 'ssl', if server is empty, override_hostname is used to
55 // create channel. Otherwise, connect to server and override hostname if
56 // override_hostname is provided.
57 // When cred_type is not 'ssl', override_hostname is ignored.
58 // Set use_prod_root to true to use the SSL root for connecting to google.
59 // In this case, path to the roots pem file must be set via environment variable
60 // GRPC_DEFAULT_SSL_ROOTS_FILE_PATH.
61 // Otherwise, root for test SSL cert will be used.
62 // creds will be used to create a channel when cred_type is 'ssl'.
65 // "1.1.1.1:12345", "ssl", "override.hostname.com", false, creds);
66 // CreateTestChannel("test.google.com:443", "ssl", "", true, creds);
68 // CreateTestChannel("", "ssl", "test.google.com:443", true, creds);
69 std::shared_ptr<Channel> CreateTestChannel(
70 const grpc::string& server, const grpc::string& cred_type,
71 const grpc::string& override_hostname, bool use_prod_roots,
72 const std::shared_ptr<CallCredentials>& creds,
73 const ChannelArguments& args) {
74 return CreateTestChannel(server, cred_type, override_hostname, use_prod_roots,
76 /*interceptor_creators=*/{});
79 std::shared_ptr<Channel> CreateTestChannel(
80 const grpc::string& server, const grpc::string& override_hostname,
81 testing::transport_security security_type, bool use_prod_roots,
82 const std::shared_ptr<CallCredentials>& creds,
83 const ChannelArguments& args) {
84 return CreateTestChannel(server, override_hostname, security_type,
85 use_prod_roots, creds, args,
86 /*interceptor_creators=*/{});
89 std::shared_ptr<Channel> CreateTestChannel(
90 const grpc::string& server, const grpc::string& override_hostname,
91 testing::transport_security security_type, bool use_prod_roots,
92 const std::shared_ptr<CallCredentials>& creds) {
93 return CreateTestChannel(server, override_hostname, security_type,
94 use_prod_roots, creds, ChannelArguments());
97 std::shared_ptr<Channel> CreateTestChannel(
98 const grpc::string& server, const grpc::string& override_hostname,
99 testing::transport_security security_type, bool use_prod_roots) {
100 return CreateTestChannel(server, override_hostname, security_type,
101 use_prod_roots, std::shared_ptr<CallCredentials>());
104 // Shortcut for end2end and interop tests.
105 std::shared_ptr<Channel> CreateTestChannel(
106 const grpc::string& server, testing::transport_security security_type) {
107 return CreateTestChannel(server, "foo.test.google.fr", security_type, false);
110 std::shared_ptr<Channel> CreateTestChannel(
111 const grpc::string& server, const grpc::string& credential_type,
112 const std::shared_ptr<CallCredentials>& creds) {
113 ChannelArguments channel_args;
114 std::shared_ptr<ChannelCredentials> channel_creds =
115 testing::GetCredentialsProvider()->GetChannelCredentials(credential_type,
117 GPR_ASSERT(channel_creds != nullptr);
119 channel_creds = CompositeChannelCredentials(channel_creds, creds);
121 return CreateCustomChannel(server, channel_creds, channel_args);
124 std::shared_ptr<Channel> CreateTestChannel(
125 const grpc::string& server, const grpc::string& cred_type,
126 const grpc::string& override_hostname, bool use_prod_roots,
127 const std::shared_ptr<CallCredentials>& creds, const ChannelArguments& args,
129 std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
130 interceptor_creators) {
131 ChannelArguments channel_args(args);
132 std::shared_ptr<ChannelCredentials> channel_creds;
133 if (cred_type.empty()) {
134 if (interceptor_creators.empty()) {
135 return CreateCustomChannel(server, InsecureChannelCredentials(), args);
137 return experimental::CreateCustomChannelWithInterceptors(
138 server, InsecureChannelCredentials(), args,
139 std::move(interceptor_creators));
141 } else if (cred_type == testing::kTlsCredentialsType) { // cred_type == "ssl"
142 if (use_prod_roots) {
143 gpr_once_init(&g_once_init_add_prod_ssl_provider, &AddProdSslType);
144 channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
145 kProdTlsCredentialsType, &channel_args);
146 if (!server.empty() && !override_hostname.empty()) {
147 channel_args.SetSslTargetNameOverride(override_hostname);
150 // override_hostname is discarded as the provider handles it.
151 channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
152 testing::kTlsCredentialsType, &channel_args);
154 GPR_ASSERT(channel_creds != nullptr);
156 const grpc::string& connect_to =
157 server.empty() ? override_hostname : server;
159 channel_creds = CompositeChannelCredentials(channel_creds, creds);
161 if (interceptor_creators.empty()) {
162 return CreateCustomChannel(connect_to, channel_creds, channel_args);
164 return experimental::CreateCustomChannelWithInterceptors(
165 connect_to, channel_creds, channel_args,
166 std::move(interceptor_creators));
169 channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
170 cred_type, &channel_args);
171 GPR_ASSERT(channel_creds != nullptr);
173 if (interceptor_creators.empty()) {
174 return CreateCustomChannel(server, channel_creds, args);
176 return experimental::CreateCustomChannelWithInterceptors(
177 server, channel_creds, args, std::move(interceptor_creators));
182 std::shared_ptr<Channel> CreateTestChannel(
183 const grpc::string& server, const grpc::string& override_hostname,
184 testing::transport_security security_type, bool use_prod_roots,
185 const std::shared_ptr<CallCredentials>& creds, const ChannelArguments& args,
187 std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
188 interceptor_creators) {
189 grpc::string credential_type =
190 security_type == testing::ALTS
191 ? testing::kAltsCredentialsType
192 : (security_type == testing::TLS ? testing::kTlsCredentialsType
193 : testing::kInsecureCredentialsType);
194 return CreateTestChannel(server, credential_type, override_hostname,
195 use_prod_roots, creds, args,
196 std::move(interceptor_creators));
199 std::shared_ptr<Channel> CreateTestChannel(
200 const grpc::string& server, const grpc::string& override_hostname,
201 testing::transport_security security_type, bool use_prod_roots,
202 const std::shared_ptr<CallCredentials>& creds,
204 std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
205 interceptor_creators) {
206 return CreateTestChannel(server, override_hostname, security_type,
207 use_prod_roots, creds, ChannelArguments(),
208 std::move(interceptor_creators));
211 std::shared_ptr<Channel> CreateTestChannel(
212 const grpc::string& server, const grpc::string& credential_type,
213 const std::shared_ptr<CallCredentials>& creds,
215 std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
216 interceptor_creators) {
217 ChannelArguments channel_args;
218 std::shared_ptr<ChannelCredentials> channel_creds =
219 testing::GetCredentialsProvider()->GetChannelCredentials(credential_type,
221 GPR_ASSERT(channel_creds != nullptr);
223 channel_creds = CompositeChannelCredentials(channel_creds, creds);
225 return experimental::CreateCustomChannelWithInterceptors(
226 server, channel_creds, channel_args, std::move(interceptor_creators));