1 /*
2 *
3 * Copyright 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 <vector>
20
21 #include <grpc/grpc.h>
22 #include <grpc/grpc_security.h>
23 #include <grpc/impl/codegen/grpc_types.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/gprpp/host_port.h"
29 #include "src/core/lib/gprpp/thd.h"
30 #include "src/core/lib/iomgr/exec_ctx.h"
31 #include "src/core/lib/iomgr/load_file.h"
32 #include "test/core/util/port.h"
33 #include "test/core/util/test_config.h"
34
35 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
36 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
37 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
38
39 typedef struct test_fixture {
40 const char* name;
41 void (*add_server_port)(grpc_server* server, const char* addr);
42 // Have the creds here so all the channels will share the same one to enabled
43 // subchannel sharing if needed.
44 grpc_channel_credentials* creds;
45 } test_fixture;
46
47 #define NUM_CONNECTIONS 100
48
49 typedef struct {
50 grpc_server* server;
51 grpc_completion_queue* cq;
52 } server_thread_args;
53
server_thread_func(void * args)54 static void server_thread_func(void* args) {
55 server_thread_args* a = static_cast<server_thread_args*>(args);
56 grpc_event ev = grpc_completion_queue_next(
57 a->cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
58 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
59 GPR_ASSERT(ev.tag == nullptr);
60 GPR_ASSERT(ev.success == true);
61 }
62
create_test_channel(const char * addr,grpc_channel_credentials * creds,bool share_subchannel)63 static grpc_channel* create_test_channel(const char* addr,
64 grpc_channel_credentials* creds,
65 bool share_subchannel) {
66 grpc_channel* channel = nullptr;
67 std::vector<grpc_arg> args;
68 args.push_back(grpc_channel_arg_integer_create(
69 const_cast<char*>(GRPC_ARG_USE_LOCAL_SUBCHANNEL_POOL),
70 !share_subchannel));
71 if (creds != nullptr) {
72 args.push_back(grpc_channel_arg_string_create(
73 const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
74 const_cast<char*>("foo.test.google.fr")));
75 }
76 grpc_channel_args channel_args = {args.size(), args.data()};
77 if (creds != nullptr) {
78 channel = grpc_secure_channel_create(creds, addr, &channel_args, nullptr);
79 } else {
80 channel = grpc_insecure_channel_create(addr, &channel_args, nullptr);
81 }
82 return channel;
83 }
84
run_test(const test_fixture * fixture,bool share_subchannel)85 static void run_test(const test_fixture* fixture, bool share_subchannel) {
86 gpr_log(GPR_INFO, "TEST: %s sharing subchannel: %d", fixture->name,
87 share_subchannel);
88
89 std::string addr =
90 grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die());
91
92 grpc_server* server = grpc_server_create(nullptr, nullptr);
93 fixture->add_server_port(server, addr.c_str());
94 grpc_completion_queue* server_cq =
95 grpc_completion_queue_create_for_next(nullptr);
96 grpc_server_register_completion_queue(server, server_cq, nullptr);
97 grpc_server_start(server);
98
99 server_thread_args sta = {server, server_cq};
100 grpc_core::Thread server_thread("grpc_server", server_thread_func, &sta);
101 server_thread.Start();
102
103 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
104 grpc_channel* channels[NUM_CONNECTIONS];
105 for (size_t i = 0; i < NUM_CONNECTIONS; i++) {
106 channels[i] =
107 create_test_channel(addr.c_str(), fixture->creds, share_subchannel);
108
109 gpr_timespec connect_deadline = grpc_timeout_seconds_to_deadline(30);
110 grpc_connectivity_state state;
111 while ((state = grpc_channel_check_connectivity_state(channels[i], 1)) !=
112 GRPC_CHANNEL_READY) {
113 grpc_channel_watch_connectivity_state(channels[i], state,
114 connect_deadline, cq, nullptr);
115 grpc_event ev = grpc_completion_queue_next(
116 cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
117 /* check that the watcher from "watch state" was free'd */
118 GPR_ASSERT(grpc_channel_num_external_connectivity_watchers(channels[i]) ==
119 0);
120 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
121 GPR_ASSERT(ev.tag == nullptr);
122 GPR_ASSERT(ev.success == true);
123 }
124 }
125
126 grpc_server_shutdown_and_notify(server, server_cq, nullptr);
127 server_thread.Join();
128
129 grpc_completion_queue_shutdown(server_cq);
130 grpc_completion_queue_shutdown(cq);
131
132 while (grpc_completion_queue_next(server_cq,
133 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr)
134 .type != GRPC_QUEUE_SHUTDOWN)
135 ;
136 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
137 nullptr)
138 .type != GRPC_QUEUE_SHUTDOWN)
139 ;
140
141 for (size_t i = 0; i < NUM_CONNECTIONS; i++) {
142 grpc_channel_destroy(channels[i]);
143 }
144
145 grpc_server_destroy(server);
146 grpc_completion_queue_destroy(server_cq);
147 grpc_completion_queue_destroy(cq);
148 }
149
insecure_test_add_port(grpc_server * server,const char * addr)150 static void insecure_test_add_port(grpc_server* server, const char* addr) {
151 grpc_server_add_insecure_http2_port(server, addr);
152 }
153
secure_test_add_port(grpc_server * server,const char * addr)154 static void secure_test_add_port(grpc_server* server, const char* addr) {
155 grpc_slice cert_slice, key_slice;
156 GPR_ASSERT(GRPC_LOG_IF_ERROR(
157 "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
158 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
159 grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
160 const char* server_cert =
161 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
162 const char* server_key =
163 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
164 grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
165 grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
166 nullptr, &pem_key_cert_pair, 1, 0, nullptr);
167 grpc_slice_unref(cert_slice);
168 grpc_slice_unref(key_slice);
169 grpc_server_add_secure_http2_port(server, addr, ssl_creds);
170 grpc_server_credentials_release(ssl_creds);
171 }
172
main(int argc,char ** argv)173 int main(int argc, char** argv) {
174 grpc::testing::TestEnvironment env(argc, argv);
175 grpc_init();
176
177 const test_fixture insecure_test = {
178 "insecure",
179 insecure_test_add_port,
180 nullptr,
181 };
182 run_test(&insecure_test, /*share_subchannel=*/true);
183 run_test(&insecure_test, /*share_subchannel=*/false);
184
185 grpc_slice ca_slice;
186 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
187 grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
188 const char* test_root_cert =
189 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
190 grpc_channel_credentials* ssl_creds =
191 grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr);
192 grpc_slice_unref(ca_slice);
193 const test_fixture secure_test = {
194 "secure",
195 secure_test_add_port,
196 ssl_creds,
197 };
198 run_test(&secure_test, /*share_subchannel=*/true);
199 run_test(&secure_test, /*share_subchannel=*/false);
200 grpc_channel_credentials_release(ssl_creds);
201
202 grpc_shutdown();
203 }
204