Debian Codegraph @

test/core/iomgr/tcp_client_posix_test.cc

raw
1 /*
2 *
3 * Copyright 2015 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 <gtest/gtest.h>
20
21 #include "src/core/lib/channel/channel_args.h"
22 #include "src/core/lib/gprpp/time.h"
23 #include "src/core/lib/iomgr/port.h"
24 #include "test/core/util/test_config.h"
25
26 // This test won't work except with posix sockets enabled
27 #ifdef GRPC_POSIX_SOCKET_TCP_CLIENT
28
29 #include <errno.h>
30 #include <netinet/in.h>
31 #include <string.h>
32 #include <sys/socket.h>
33 #include <unistd.h>
34
35 #include <grpc/grpc.h>
36 #include <grpc/support/alloc.h>
37 #include <grpc/support/log.h>
38 #include <grpc/support/time.h>
39
40 #include "src/core/lib/event_engine/channel_args_endpoint_config.h"
41 #include "src/core/lib/iomgr/iomgr.h"
42 #include "src/core/lib/iomgr/pollset_set.h"
43 #include "src/core/lib/iomgr/socket_utils_posix.h"
44 #include "src/core/lib/iomgr/tcp_client.h"
45 #include "src/core/lib/iomgr/timer.h"
46 #include "src/core/lib/resource_quota/api.h"
47
48 static grpc_pollset_set* g_pollset_set;
49 static gpr_mu* g_mu;
50 static grpc_pollset* g_pollset;
51 static int g_connections_complete = 0;
52 static grpc_endpoint* g_connecting = nullptr;
53
54 static grpc_core::Timestamp test_deadline(void) {
55 return grpc_core::Timestamp::FromTimespecRoundUp(
56 grpc_timeout_seconds_to_deadline(10));
57 }
58
59 static void finish_connection() {
60 gpr_mu_lock(g_mu);
61 g_connections_complete++;
62 grpc_core::ExecCtx exec_ctx;
63 ASSERT_TRUE(
64 GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(g_pollset, nullptr)));
65
66 gpr_mu_unlock(g_mu);
67 }
68
69 static void must_succeed(void* /*arg*/, grpc_error_handle error) {
70 ASSERT_NE(g_connecting, nullptr);
71 ASSERT_TRUE(error.ok());
72 grpc_endpoint_shutdown(g_connecting,
73 GRPC_ERROR_CREATE("must_succeed called"));
74 grpc_endpoint_destroy(g_connecting);
75 g_connecting = nullptr;
76 finish_connection();
77 }
78
79 static void must_fail(void* /*arg*/, grpc_error_handle error) {
80 ASSERT_EQ(g_connecting, nullptr);
81 ASSERT_FALSE(error.ok());
82 finish_connection();
83 }
84
85 void test_succeeds(void) {
86 gpr_log(GPR_ERROR, "---- starting test_succeeds() ----");
87 grpc_resolved_address resolved_addr;
88 struct sockaddr_in* addr =
89 reinterpret_cast<struct sockaddr_in*>(resolved_addr.addr);
90 int svr_fd;
91 int r;
92 int connections_complete_before;
93 grpc_closure done;
94 grpc_core::ExecCtx exec_ctx;
95
96 memset(&resolved_addr, 0, sizeof(resolved_addr));
97 resolved_addr.len = static_cast<socklen_t>(sizeof(struct sockaddr_in));
98 addr->sin_family = AF_INET;
99
100 /* create a phony server */
101 svr_fd = socket(AF_INET, SOCK_STREAM, 0);
102 ASSERT_GE(svr_fd, 0);
103 ASSERT_EQ(bind(svr_fd, (struct sockaddr*)addr, (socklen_t)resolved_addr.len),
104 0);
105 ASSERT_EQ(listen(svr_fd, 1), 0);
106
107 gpr_mu_lock(g_mu);
108 connections_complete_before = g_connections_complete;
109 gpr_mu_unlock(g_mu);
110
111 /* connect to it */
112 ASSERT_EQ(getsockname(svr_fd, (struct sockaddr*)addr,
113 (socklen_t*)&resolved_addr.len),
114 0);
115 GRPC_CLOSURE_INIT(&done, must_succeed, nullptr, grpc_schedule_on_exec_ctx);
116 grpc_core::ChannelArgs args = grpc_core::CoreConfiguration::Get()
117 .channel_args_preconditioning()
118 .PreconditionChannelArgs(nullptr);
119 int64_t connection_handle = grpc_tcp_client_connect(
120 &done, &g_connecting, g_pollset_set,
121 grpc_event_engine::experimental::ChannelArgsEndpointConfig(args),
122 &resolved_addr, grpc_core::Timestamp::InfFuture());
123 /* await the connection */
124 do {
125 resolved_addr.len = static_cast<socklen_t>(sizeof(addr));
126 r = accept(svr_fd, reinterpret_cast<struct sockaddr*>(addr),
127 reinterpret_cast<socklen_t*>(&resolved_addr.len));
128 } while (r == -1 && errno == EINTR);
129 ASSERT_GE(r, 0);
130 close(r);
131
132 gpr_mu_lock(g_mu);
133
134 while (g_connections_complete == connections_complete_before) {
135 grpc_pollset_worker* worker = nullptr;
136 ASSERT_TRUE(GRPC_LOG_IF_ERROR(
137 "pollset_work",
138 grpc_pollset_work(g_pollset, &worker,
139 grpc_core::Timestamp::FromTimespecRoundUp(
140 grpc_timeout_seconds_to_deadline(5)))));
141 gpr_mu_unlock(g_mu);
142 grpc_core::ExecCtx::Get()->Flush();
143 gpr_mu_lock(g_mu);
144 }
145
146 gpr_mu_unlock(g_mu);
147
148 // A cancellation attempt should fail because connect already succeeded.
149 ASSERT_EQ(grpc_tcp_client_cancel_connect(connection_handle), false);
150
151 gpr_log(GPR_ERROR, "---- finished test_succeeds() ----");
152 }
153
154 void test_fails(void) {
155 gpr_log(GPR_ERROR, "---- starting test_fails() ----");
156 grpc_resolved_address resolved_addr;
157 struct sockaddr_in* addr =
158 reinterpret_cast<struct sockaddr_in*>(resolved_addr.addr);
159 int connections_complete_before;
160 grpc_closure done;
161 grpc_core::ExecCtx exec_ctx;
162
163 memset(&resolved_addr, 0, sizeof(resolved_addr));
164 resolved_addr.len = static_cast<socklen_t>(sizeof(struct sockaddr_in));
165 addr->sin_family = AF_INET;
166
167 gpr_mu_lock(g_mu);
168 connections_complete_before = g_connections_complete;
169 gpr_mu_unlock(g_mu);
170
171 /* connect to a broken address */
172 GRPC_CLOSURE_INIT(&done, must_fail, nullptr, grpc_schedule_on_exec_ctx);
173 int64_t connection_handle = grpc_tcp_client_connect(
174 &done, &g_connecting, g_pollset_set,
175 grpc_event_engine::experimental::ChannelArgsEndpointConfig(),
176 &resolved_addr, grpc_core::Timestamp::InfFuture());
177 gpr_mu_lock(g_mu);
178
179 /* wait for the connection callback to finish */
180 while (g_connections_complete == connections_complete_before) {
181 grpc_pollset_worker* worker = nullptr;
182 grpc_core::Timestamp polling_deadline = test_deadline();
183 switch (grpc_timer_check(&polling_deadline)) {
184 case GRPC_TIMERS_FIRED:
185 break;
186 case GRPC_TIMERS_NOT_CHECKED:
187 polling_deadline = grpc_core::Timestamp::ProcessEpoch();
188 ABSL_FALLTHROUGH_INTENDED;
189 case GRPC_TIMERS_CHECKED_AND_EMPTY:
190 ASSERT_TRUE(GRPC_LOG_IF_ERROR(
191 "pollset_work",
192 grpc_pollset_work(g_pollset, &worker, polling_deadline)));
193 break;
194 }
195 gpr_mu_unlock(g_mu);
196 grpc_core::ExecCtx::Get()->Flush();
197 gpr_mu_lock(g_mu);
198 }
199
200 gpr_mu_unlock(g_mu);
201
202 // A cancellation attempt should fail because connect already failed.
203 ASSERT_EQ(grpc_tcp_client_cancel_connect(connection_handle), false);
204
205 gpr_log(GPR_ERROR, "---- finished test_fails() ----");
206 }
207
208 void test_connect_cancellation_succeeds(void) {
209 gpr_log(GPR_ERROR, "---- starting test_connect_cancellation_succeeds() ----");
210 grpc_resolved_address resolved_addr;
211 struct sockaddr_in* addr =
212 reinterpret_cast<struct sockaddr_in*>(resolved_addr.addr);
213 int svr_fd;
214 grpc_closure done;
215 grpc_core::ExecCtx exec_ctx;
216
217 memset(&resolved_addr, 0, sizeof(resolved_addr));
218 resolved_addr.len = static_cast<socklen_t>(sizeof(struct sockaddr_in));
219 addr->sin_family = AF_INET;
220
221 /* create a phony server */
222 svr_fd = socket(AF_INET, SOCK_STREAM, 0);
223 ASSERT_GE(svr_fd, 0);
224 ASSERT_EQ(bind(svr_fd, (struct sockaddr*)addr, (socklen_t)resolved_addr.len),
225 0);
226 ASSERT_EQ(listen(svr_fd, 1), 0);
227
228 // connect to it. accept() is not called on the bind socket. So the connection
229 // should appear to be stuck giving ample time to try to cancel it.
230 ASSERT_EQ(getsockname(svr_fd, (struct sockaddr*)addr,
231 (socklen_t*)&resolved_addr.len),
232 0);
233 GRPC_CLOSURE_INIT(&done, must_succeed, nullptr, grpc_schedule_on_exec_ctx);
234 grpc_core::ChannelArgs args = grpc_core::CoreConfiguration::Get()
235 .channel_args_preconditioning()
236 .PreconditionChannelArgs(nullptr);
237 int64_t connection_handle = grpc_tcp_client_connect(
238 &done, &g_connecting, g_pollset_set,
239 grpc_event_engine::experimental::ChannelArgsEndpointConfig(args),
240 &resolved_addr, grpc_core::Timestamp::InfFuture());
241 ASSERT_GT(connection_handle, 0);
242 ASSERT_EQ(grpc_tcp_client_cancel_connect(connection_handle), true);
243 close(svr_fd);
244 gpr_log(GPR_ERROR, "---- finished test_connect_cancellation_succeeds() ----");
245 }
246
247 void test_fails_bad_addr_no_leak(void) {
248 gpr_log(GPR_ERROR, "---- starting test_fails_bad_addr_no_leak() ----");
249 grpc_resolved_address resolved_addr;
250 struct sockaddr_in* addr =
251 reinterpret_cast<struct sockaddr_in*>(resolved_addr.addr);
252 int connections_complete_before;
253 grpc_closure done;
254 grpc_core::ExecCtx exec_ctx;
255 memset(&resolved_addr, 0, sizeof(resolved_addr));
256 resolved_addr.len = static_cast<socklen_t>(sizeof(struct sockaddr_in));
257 // force `grpc_tcp_client_prepare_fd` to fail. contrived, but effective.
258 addr->sin_family = AF_IPX;
259 gpr_mu_lock(g_mu);
260 connections_complete_before = g_connections_complete;
261 gpr_mu_unlock(g_mu);
262 // connect to an invalid address.
263 GRPC_CLOSURE_INIT(&done, must_fail, nullptr, grpc_schedule_on_exec_ctx);
264 grpc_tcp_client_connect(
265 &done, &g_connecting, g_pollset_set,
266 grpc_event_engine::experimental::ChannelArgsEndpointConfig(),
267 &resolved_addr, grpc_core::Timestamp::InfFuture());
268 gpr_mu_lock(g_mu);
269 while (g_connections_complete == connections_complete_before) {
270 grpc_pollset_worker* worker = nullptr;
271 grpc_core::Timestamp polling_deadline = test_deadline();
272 switch (grpc_timer_check(&polling_deadline)) {
273 case GRPC_TIMERS_FIRED:
274 break;
275 case GRPC_TIMERS_NOT_CHECKED:
276 polling_deadline = grpc_core::Timestamp::ProcessEpoch();
277 ABSL_FALLTHROUGH_INTENDED;
278 case GRPC_TIMERS_CHECKED_AND_EMPTY:
279 ASSERT_TRUE(GRPC_LOG_IF_ERROR(
280 "pollset_work",
281 grpc_pollset_work(g_pollset, &worker, polling_deadline)));
282 break;
283 }
284 gpr_mu_unlock(g_mu);
285 grpc_core::ExecCtx::Get()->Flush();
286 gpr_mu_lock(g_mu);
287 }
288 gpr_mu_unlock(g_mu);
289 gpr_log(GPR_ERROR, "---- finished test_fails_bad_addr_no_leak() ----");
290 }
291
292 static void destroy_pollset(void* p, grpc_error_handle /*error*/) {
293 grpc_pollset_destroy(static_cast<grpc_pollset*>(p));
294 }
295
296 TEST(TcpClientPosixTest, MainTest) {
297 grpc_closure destroyed;
298 grpc_init();
299
300 {
301 grpc_core::ExecCtx exec_ctx;
302 g_pollset_set = grpc_pollset_set_create();
303 g_pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
304 grpc_pollset_init(g_pollset, &g_mu);
305 grpc_pollset_set_add_pollset(g_pollset_set, g_pollset);
306
307 test_succeeds();
308 test_connect_cancellation_succeeds();
309 test_fails();
310 test_fails_bad_addr_no_leak();
311 grpc_pollset_set_destroy(g_pollset_set);
312 GRPC_CLOSURE_INIT(&destroyed, destroy_pollset, g_pollset,
313 grpc_schedule_on_exec_ctx);
314 grpc_pollset_shutdown(g_pollset, &destroyed);
315 }
316
317 grpc_shutdown();
318 gpr_free(g_pollset);
319 }
320
321 #endif /* GRPC_POSIX_SOCKET_CLIENT */
322
323 int main(int argc, char** argv) {
324 grpc::testing::TestEnvironment env(&argc, argv);
325 ::testing::InitGoogleTest(&argc, argv);
326 return RUN_ALL_TESTS();
327 }