79a5e13d9932cee047bcf13ed74595598a0fdee4
[platform/upstream/grpc.git] / test / cpp / util / create_test_channel.cc
1 /*
2  *
3  * Copyright 2015-2016 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19 #include "test/cpp/util/create_test_channel.h"
20
21 #include <grpc/support/log.h>
22 #include <grpcpp/create_channel.h>
23 #include <grpcpp/security/credentials.h>
24
25 #include "test/cpp/util/test_credentials_provider.h"
26
27 namespace grpc {
28
29 namespace {
30
31 const char kProdTlsCredentialsType[] = "prod_ssl";
32
33 class SslCredentialProvider : public testing::CredentialTypeProvider {
34  public:
35   std::shared_ptr<ChannelCredentials> GetChannelCredentials(
36       grpc::ChannelArguments* args) override {
37     return SslCredentials(SslCredentialsOptions());
38   }
39   std::shared_ptr<ServerCredentials> GetServerCredentials() override {
40     return nullptr;
41   }
42 };
43
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));
50 }
51
52 }  // namespace
53
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'.
63 // Use examples:
64 //   CreateTestChannel(
65 //       "1.1.1.1:12345", "ssl", "override.hostname.com", false, creds);
66 //   CreateTestChannel("test.google.com:443", "ssl", "", true, creds);
67 //   same as above
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,
75                            creds, args,
76                            /*interceptor_creators=*/{});
77 }
78
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=*/{});
87 }
88
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());
95 }
96
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>());
102 }
103
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);
108 }
109
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,
116                                                                &channel_args);
117   GPR_ASSERT(channel_creds != nullptr);
118   if (creds.get()) {
119     channel_creds = CompositeChannelCredentials(channel_creds, creds);
120   }
121   return CreateCustomChannel(server, channel_creds, channel_args);
122 }
123
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,
128     std::vector<
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);
136     } else {
137       return experimental::CreateCustomChannelWithInterceptors(
138           server, InsecureChannelCredentials(), args,
139           std::move(interceptor_creators));
140     }
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);
148       }
149     } else {
150       // override_hostname is discarded as the provider handles it.
151       channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
152           testing::kTlsCredentialsType, &channel_args);
153     }
154     GPR_ASSERT(channel_creds != nullptr);
155
156     const grpc::string& connect_to =
157         server.empty() ? override_hostname : server;
158     if (creds.get()) {
159       channel_creds = CompositeChannelCredentials(channel_creds, creds);
160     }
161     if (interceptor_creators.empty()) {
162       return CreateCustomChannel(connect_to, channel_creds, channel_args);
163     } else {
164       return experimental::CreateCustomChannelWithInterceptors(
165           connect_to, channel_creds, channel_args,
166           std::move(interceptor_creators));
167     }
168   } else {
169     channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
170         cred_type, &channel_args);
171     GPR_ASSERT(channel_creds != nullptr);
172
173     if (interceptor_creators.empty()) {
174       return CreateCustomChannel(server, channel_creds, args);
175     } else {
176       return experimental::CreateCustomChannelWithInterceptors(
177           server, channel_creds, args, std::move(interceptor_creators));
178     }
179   }
180 }
181
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,
186     std::vector<
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));
197 }
198
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,
203     std::vector<
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));
209 }
210
211 std::shared_ptr<Channel> CreateTestChannel(
212     const grpc::string& server, const grpc::string& credential_type,
213     const std::shared_ptr<CallCredentials>& creds,
214     std::vector<
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,
220                                                                &channel_args);
221   GPR_ASSERT(channel_creds != nullptr);
222   if (creds.get()) {
223     channel_creds = CompositeChannelCredentials(channel_creds, creds);
224   }
225   return experimental::CreateCustomChannelWithInterceptors(
226       server, channel_creds, channel_args, std::move(interceptor_creators));
227 }
228
229 }  // namespace grpc