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 grpc_channel* (*create_channel)(const char* addr);
35 } test_fixture;
36
37 static size_t next_tag = 1;
38
channel_idle_start_watch(grpc_channel * channel,grpc_completion_queue * cq)39 static void channel_idle_start_watch(grpc_channel* channel,
40 grpc_completion_queue* cq) {
41 gpr_timespec connect_deadline = grpc_timeout_milliseconds_to_deadline(1);
42 GPR_ASSERT(grpc_channel_check_connectivity_state(channel, 0) ==
43 GRPC_CHANNEL_IDLE);
44
45 grpc_channel_watch_connectivity_state(
46 channel, GRPC_CHANNEL_IDLE, connect_deadline, cq, (void*)(next_tag++));
47 gpr_log(GPR_DEBUG, "number of active connect watchers: %d",
48 grpc_channel_num_external_connectivity_watchers(channel));
49 }
50
channel_idle_poll_for_timeout(grpc_channel * channel,grpc_completion_queue * cq)51 static void channel_idle_poll_for_timeout(grpc_channel* channel,
52 grpc_completion_queue* cq) {
53 grpc_event ev = grpc_completion_queue_next(
54 cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
55
56 /* expect watch_connectivity_state to end with a timeout */
57 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
58 GPR_ASSERT(ev.success == false);
59 GPR_ASSERT(grpc_channel_check_connectivity_state(channel, 0) ==
60 GRPC_CHANNEL_IDLE);
61 }
62
63 /* Test and use the "num_external_watchers" call to make sure
64 * that "connectivity watcher" structs are free'd just after, if
65 * their corresponding timeouts occur. */
run_timeouts_test(const test_fixture * fixture)66 static void run_timeouts_test(const test_fixture* fixture) {
67 gpr_log(GPR_INFO, "TEST: %s", fixture->name);
68
69 char* addr;
70
71 grpc_init();
72
73 gpr_join_host_port(&addr, "localhost", grpc_pick_unused_port_or_die());
74
75 grpc_channel* channel = fixture->create_channel(addr);
76 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
77
78 /* start 1 watcher and then let it time out */
79 channel_idle_start_watch(channel, cq);
80 channel_idle_poll_for_timeout(channel, cq);
81 GPR_ASSERT(grpc_channel_num_external_connectivity_watchers(channel) == 0);
82
83 /* start 3 watchers and then let them all time out */
84 for (size_t i = 1; i <= 3; i++) {
85 channel_idle_start_watch(channel, cq);
86 }
87 for (size_t i = 1; i <= 3; i++) {
88 channel_idle_poll_for_timeout(channel, cq);
89 }
90 GPR_ASSERT(grpc_channel_num_external_connectivity_watchers(channel) == 0);
91
92 /* start 3 watchers, see one time out, start another 3, and then see them all
93 * time out */
94 for (size_t i = 1; i <= 3; i++) {
95 channel_idle_start_watch(channel, cq);
96 }
97 channel_idle_poll_for_timeout(channel, cq);
98 for (size_t i = 3; i <= 5; i++) {
99 channel_idle_start_watch(channel, cq);
100 }
101 for (size_t i = 1; i <= 5; i++) {
102 channel_idle_poll_for_timeout(channel, cq);
103 }
104 GPR_ASSERT(grpc_channel_num_external_connectivity_watchers(channel) == 0);
105
106 grpc_channel_destroy(channel);
107 grpc_completion_queue_shutdown(cq);
108 GPR_ASSERT(grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
109 nullptr)
110 .type == GRPC_QUEUE_SHUTDOWN);
111 grpc_completion_queue_destroy(cq);
112
113 grpc_shutdown();
114 gpr_free(addr);
115 }
116
117 /* An edge scenario; sets channel state to explicitly, and outside
118 * of a polling call. */
run_channel_shutdown_before_timeout_test(const test_fixture * fixture)119 static void run_channel_shutdown_before_timeout_test(
120 const test_fixture* fixture) {
121 gpr_log(GPR_INFO, "TEST: %s", fixture->name);
122
123 char* addr;
124
125 grpc_init();
126
127 gpr_join_host_port(&addr, "localhost", grpc_pick_unused_port_or_die());
128
129 grpc_channel* channel = fixture->create_channel(addr);
130 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
131
132 /* start 1 watcher and then shut down the channel before the timer goes off */
133 GPR_ASSERT(grpc_channel_num_external_connectivity_watchers(channel) == 0);
134
135 /* expecting a 30 second timeout to go off much later than the shutdown. */
136 gpr_timespec connect_deadline = grpc_timeout_seconds_to_deadline(30);
137 GPR_ASSERT(grpc_channel_check_connectivity_state(channel, 0) ==
138 GRPC_CHANNEL_IDLE);
139
140 grpc_channel_watch_connectivity_state(channel, GRPC_CHANNEL_IDLE,
141 connect_deadline, cq, (void*)1);
142 grpc_channel_destroy(channel);
143
144 grpc_event ev = grpc_completion_queue_next(
145 cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
146 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
147 /* expect success with a state transition to CHANNEL_SHUTDOWN */
148 GPR_ASSERT(ev.success == true);
149
150 grpc_completion_queue_shutdown(cq);
151 GPR_ASSERT(grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
152 nullptr)
153 .type == GRPC_QUEUE_SHUTDOWN);
154 grpc_completion_queue_destroy(cq);
155
156 grpc_shutdown();
157 gpr_free(addr);
158 }
159
insecure_test_create_channel(const char * addr)160 static grpc_channel* insecure_test_create_channel(const char* addr) {
161 return grpc_insecure_channel_create(addr, nullptr, nullptr);
162 }
163
164 static const test_fixture insecure_test = {
165 "insecure",
166 insecure_test_create_channel,
167 };
168
secure_test_create_channel(const char * addr)169 static grpc_channel* secure_test_create_channel(const char* addr) {
170 grpc_channel_credentials* ssl_creds =
171 grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr);
172 grpc_arg ssl_name_override = {
173 GRPC_ARG_STRING,
174 const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
175 {const_cast<char*>("foo.test.google.fr")}};
176 grpc_channel_args* new_client_args =
177 grpc_channel_args_copy_and_add(nullptr, &ssl_name_override, 1);
178 grpc_channel* channel =
179 grpc_secure_channel_create(ssl_creds, addr, new_client_args, nullptr);
180 {
181 grpc_core::ExecCtx exec_ctx;
182 grpc_channel_args_destroy(new_client_args);
183 }
184 grpc_channel_credentials_release(ssl_creds);
185 return channel;
186 }
187
188 static const test_fixture secure_test = {
189 "secure",
190 secure_test_create_channel,
191 };
192
main(int argc,char ** argv)193 int main(int argc, char** argv) {
194 grpc_test_init(argc, argv);
195
196 run_timeouts_test(&insecure_test);
197 run_timeouts_test(&secure_test);
198
199 run_channel_shutdown_before_timeout_test(&insecure_test);
200 run_channel_shutdown_before_timeout_test(&secure_test);
201 }
202