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 <grpc/grpc.h>
20 #include <grpc/grpc_security.h>
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/log.h>
23
24 #include "src/core/lib/channel/channel_args.h"
25 #include "src/core/lib/gpr/host_port.h"
26 #include "src/core/lib/gprpp/thd.h"
27 #include "src/core/lib/iomgr/exec_ctx.h"
28 #include "test/core/end2end/data/ssl_test_data.h"
29 #include "test/core/util/port.h"
30 #include "test/core/util/test_config.h"
31
32 typedef struct test_fixture {
33 const char* name;
34 void (*add_server_port)(grpc_server* server, const char* addr);
35 grpc_channel* (*create_channel)(const char* addr);
36 } test_fixture;
37
38 #define NUM_CONNECTIONS 1000
39
40 typedef struct {
41 grpc_server* server;
42 grpc_completion_queue* cq;
43 } server_thread_args;
44
server_thread_func(void * args)45 static void server_thread_func(void* args) {
46 server_thread_args* a = static_cast<server_thread_args*>(args);
47 grpc_event ev = grpc_completion_queue_next(
48 a->cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
49 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
50 GPR_ASSERT(ev.tag == nullptr);
51 GPR_ASSERT(ev.success == true);
52 }
53
run_test(const test_fixture * fixture)54 static void run_test(const test_fixture* fixture) {
55 gpr_log(GPR_INFO, "TEST: %s", fixture->name);
56
57 grpc_init();
58
59 char* addr;
60 gpr_join_host_port(&addr, "localhost", grpc_pick_unused_port_or_die());
61
62 grpc_server* server = grpc_server_create(nullptr, nullptr);
63 fixture->add_server_port(server, addr);
64 grpc_completion_queue* server_cq =
65 grpc_completion_queue_create_for_next(nullptr);
66 grpc_server_register_completion_queue(server, server_cq, nullptr);
67 grpc_server_start(server);
68
69 server_thread_args sta = {server, server_cq};
70 grpc_core::Thread server_thread("grpc_server", server_thread_func, &sta);
71 server_thread.Start();
72
73 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
74 grpc_channel* channels[NUM_CONNECTIONS];
75 for (size_t i = 0; i < NUM_CONNECTIONS; i++) {
76 channels[i] = fixture->create_channel(addr);
77
78 gpr_timespec connect_deadline = grpc_timeout_seconds_to_deadline(30);
79 grpc_connectivity_state state;
80 while ((state = grpc_channel_check_connectivity_state(channels[i], 1)) !=
81 GRPC_CHANNEL_READY) {
82 grpc_channel_watch_connectivity_state(channels[i], state,
83 connect_deadline, cq, nullptr);
84 grpc_event ev = grpc_completion_queue_next(
85 cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
86 /* check that the watcher from "watch state" was free'd */
87 GPR_ASSERT(grpc_channel_num_external_connectivity_watchers(channels[i]) ==
88 0);
89 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
90 GPR_ASSERT(ev.tag == nullptr);
91 GPR_ASSERT(ev.success == true);
92 }
93 }
94
95 grpc_server_shutdown_and_notify(server, server_cq, nullptr);
96 server_thread.Join();
97
98 grpc_completion_queue_shutdown(server_cq);
99 grpc_completion_queue_shutdown(cq);
100
101 while (grpc_completion_queue_next(server_cq,
102 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr)
103 .type != GRPC_QUEUE_SHUTDOWN)
104 ;
105 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
106 nullptr)
107 .type != GRPC_QUEUE_SHUTDOWN)
108 ;
109
110 for (size_t i = 0; i < NUM_CONNECTIONS; i++) {
111 grpc_channel_destroy(channels[i]);
112 }
113
114 grpc_server_destroy(server);
115 grpc_completion_queue_destroy(server_cq);
116 grpc_completion_queue_destroy(cq);
117
118 grpc_shutdown();
119 gpr_free(addr);
120 }
121
insecure_test_add_port(grpc_server * server,const char * addr)122 static void insecure_test_add_port(grpc_server* server, const char* addr) {
123 grpc_server_add_insecure_http2_port(server, addr);
124 }
125
insecure_test_create_channel(const char * addr)126 static grpc_channel* insecure_test_create_channel(const char* addr) {
127 return grpc_insecure_channel_create(addr, nullptr, nullptr);
128 }
129
130 static const test_fixture insecure_test = {
131 "insecure",
132 insecure_test_add_port,
133 insecure_test_create_channel,
134 };
135
secure_test_add_port(grpc_server * server,const char * addr)136 static void secure_test_add_port(grpc_server* server, const char* addr) {
137 grpc_ssl_pem_key_cert_pair pem_cert_key_pair = {test_server1_key,
138 test_server1_cert};
139 grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
140 nullptr, &pem_cert_key_pair, 1, 0, nullptr);
141 grpc_server_add_secure_http2_port(server, addr, ssl_creds);
142 grpc_server_credentials_release(ssl_creds);
143 }
144
secure_test_create_channel(const char * addr)145 static grpc_channel* secure_test_create_channel(const char* addr) {
146 grpc_channel_credentials* ssl_creds =
147 grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr);
148 grpc_arg ssl_name_override = {
149 GRPC_ARG_STRING,
150 const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
151 {const_cast<char*>("foo.test.google.fr")}};
152 grpc_channel_args* new_client_args =
153 grpc_channel_args_copy_and_add(nullptr, &ssl_name_override, 1);
154 grpc_channel* channel =
155 grpc_secure_channel_create(ssl_creds, addr, new_client_args, nullptr);
156 {
157 grpc_core::ExecCtx exec_ctx;
158 grpc_channel_args_destroy(new_client_args);
159 }
160 grpc_channel_credentials_release(ssl_creds);
161 return channel;
162 }
163
164 static const test_fixture secure_test = {
165 "secure",
166 secure_test_add_port,
167 secure_test_create_channel,
168 };
169
main(int argc,char ** argv)170 int main(int argc, char** argv) {
171 grpc_test_init(argc, argv);
172
173 run_test(&insecure_test);
174 run_test(&secure_test);
175 }
176