Debian Codegraph @

crt/aws-c-http/tests/test_connection.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/http/connection.h>
7 #include <aws/http/private/connection_impl.h>
8 #include <aws/http/proxy.h>
9 #include <aws/http/server.h>
10
11 #include <aws/common/clock.h>
12 #include <aws/common/condition_variable.h>
13 #include <aws/common/hash_table.h>
14 #include <aws/common/logging.h>
15 #include <aws/common/string.h>
16 #include <aws/common/thread.h>
17 #include <aws/io/channel_bootstrap.h>
18 #include <aws/io/event_loop.h>
19 #include <aws/io/socket.h>
20 #include <aws/io/tls_channel_handler.h>
21 #include <aws/testing/aws_test_harness.h>
22
23 #ifdef _MSC_VER
24 # pragma warning(disable : 4204) /* non-constant aggregate initializer */
25 #endif
26
27 #ifdef _WIN32
28 # define LOCAL_SOCK_TEST_FORMAT "\\\\.\\pipe\\testsock-%s"
29 #else
30 # define LOCAL_SOCK_TEST_FORMAT "testsock-%s.sock"
31 #endif
32
33 enum {
34 TESTER_TIMEOUT_SEC = 60, /* Give enough time for non-sudo users to enter password */
35 };
36
37 /* Options for setting up `tester` singleton */
38 struct tester_options {
39 struct aws_allocator *alloc;
40 bool tls;
41 char *server_alpn_list;
42 char *client_alpn_list;
43 bool no_connection; /* don't connect server to client */
44 bool pin_event_loop;
45 bool use_tcp; /* otherwise uses domain sockets */
46 };
47
48 /* Singleton used by tests in this file */
49 struct tester {
50 struct aws_allocator *alloc;
51 struct aws_event_loop_group *server_event_loop_group;
52 struct aws_event_loop_group *client_event_loop_group;
53 struct aws_host_resolver *host_resolver;
54 struct aws_server_bootstrap *server_bootstrap;
55 struct aws_http_server *server;
56 struct aws_client_bootstrap *client_bootstrap;
57 struct aws_http_client_connection_options client_options;
58
59 int server_connection_num;
60 int client_connection_num;
61 int wait_server_connection_num;
62 int wait_client_connection_num;
63 struct aws_http_connection *server_connections[10];
64 struct aws_http_connection *client_connections[10];
65
66 struct aws_socket_endpoint endpoint;
67 struct aws_socket_options socket_options;
68
69 int client_connection_is_shutdown;
70 int server_connection_is_shutdown;
71 int wait_client_connection_is_shutdown;
72 int wait_server_connection_is_shutdown;
73
74 bool server_is_shutdown;
75 struct aws_http_connection *new_client_connection;
76 bool new_client_shut_down;
77 bool new_client_setup_finished;
78
79 enum aws_http_version connection_version;
80
81 /* Tls context */
82 struct aws_tls_ctx_options server_ctx_options;
83 struct aws_tls_ctx_options client_ctx_options;
84 struct aws_tls_ctx *server_ctx;
85 struct aws_tls_ctx *client_ctx;
86 struct aws_tls_connection_options server_tls_connection_options;
87 struct aws_tls_connection_options client_tls_connection_options;
88 struct aws_byte_buf negotiated_protocol;
89
90 /* If we need to wait for some async process*/
91 struct aws_mutex wait_lock;
92 struct aws_condition_variable wait_cvar;
93 /* we need wait result for both server side and client side */
94 int server_wait_result;
95 int client_wait_result;
96 };
97
98 static struct aws_http_stream *s_tester_on_incoming_request(struct aws_http_connection *connection, void *user_data) {
99 (void)connection;
100 (void)user_data;
101 aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
102 return NULL;
103 }
104
105 static void s_tester_http_server_on_destroy(void *user_data) {
106 struct tester *tester = user_data;
107 AWS_FATAL_ASSERT(aws_mutex_lock(&tester->wait_lock) == AWS_OP_SUCCESS);
108 tester->server_is_shutdown = true;
109 tester->server = NULL;
110 AWS_FATAL_ASSERT(aws_mutex_unlock(&tester->wait_lock) == AWS_OP_SUCCESS);
111 aws_condition_variable_notify_one(&tester->wait_cvar);
112 }
113
114 static void s_tester_on_server_connection_shutdown(
115 struct aws_http_connection *connection,
116 int error_code,
117 void *user_data) {
118
119 (void)connection;
120 (void)error_code;
121 struct tester *tester = user_data;
122 AWS_FATAL_ASSERT(aws_mutex_lock(&tester->wait_lock) == AWS_OP_SUCCESS);
123
124 tester->server_connection_is_shutdown++;
125
126 AWS_FATAL_ASSERT(aws_mutex_unlock(&tester->wait_lock) == AWS_OP_SUCCESS);
127 aws_condition_variable_notify_one(&tester->wait_cvar);
128 }
129
130 static void s_tester_on_server_connection_setup(
131 struct aws_http_server *server,
132 struct aws_http_connection *connection,
133 int error_code,
134 void *user_data) {
135
136 (void)server;
137 struct tester *tester = user_data;
138 AWS_FATAL_ASSERT(aws_mutex_lock(&tester->wait_lock) == AWS_OP_SUCCESS);
139
140 if (error_code) {
141 tester->server_wait_result = error_code;
142 goto done;
143 }
144
145 struct aws_http_server_connection_options options = AWS_HTTP_SERVER_CONNECTION_OPTIONS_INIT;
146 options.connection_user_data = tester;
147 options.on_incoming_request = s_tester_on_incoming_request;
148 options.on_shutdown = s_tester_on_server_connection_shutdown;
149
150 int err = aws_http_connection_configure_server(connection, &options);
151 if (err) {
152 tester->server_wait_result = aws_last_error();
153 goto done;
154 }
155
156 tester->server_connections[tester->server_connection_num++] = connection;
157 done:
158 AWS_FATAL_ASSERT(aws_mutex_unlock(&tester->wait_lock) == AWS_OP_SUCCESS);
159 aws_condition_variable_notify_one(&tester->wait_cvar);
160 }
161
162 static void s_tester_on_client_connection_setup(
163 struct aws_http_connection *connection,
164 int error_code,
165 void *user_data) {
166
167 struct tester *tester = user_data;
168 AWS_FATAL_ASSERT(aws_mutex_lock(&tester->wait_lock) == AWS_OP_SUCCESS);
169
170 if (error_code) {
171 tester->client_wait_result = error_code;
172 goto done;
173 }
174 tester->connection_version = aws_http_connection_get_version(connection);
175 tester->client_connections[tester->client_connection_num++] = connection;
176 done:
177 AWS_FATAL_ASSERT(aws_mutex_unlock(&tester->wait_lock) == AWS_OP_SUCCESS);
178 aws_condition_variable_notify_one(&tester->wait_cvar);
179 }
180
181 static void s_tester_on_client_connection_shutdown(
182 struct aws_http_connection *connection,
183 int error_code,
184 void *user_data) {
185
186 (void)connection;
187 (void)error_code;
188 struct tester *tester = user_data;
189 AWS_FATAL_ASSERT(aws_mutex_lock(&tester->wait_lock) == AWS_OP_SUCCESS);
190
191 tester->client_connection_is_shutdown++;
192
193 AWS_FATAL_ASSERT(aws_mutex_unlock(&tester->wait_lock) == AWS_OP_SUCCESS);
194 aws_condition_variable_notify_one(&tester->wait_cvar);
195 }
196
197 static int s_tester_wait(struct tester *tester, bool (*pred)(void *user_data)) {
198 int local_wait_result;
199 ASSERT_SUCCESS(aws_mutex_lock(&tester->wait_lock));
200 int err = aws_condition_variable_wait_for_pred(
201 &tester->wait_cvar,
202 &tester->wait_lock,
203 aws_timestamp_convert(TESTER_TIMEOUT_SEC, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL),
204 pred,
205 tester);
206 if (tester->server_wait_result) {
207 local_wait_result = tester->server_wait_result;
208 } else {
209 local_wait_result = tester->client_wait_result;
210 }
211 tester->server_wait_result = 0;
212 tester->client_wait_result = 0;
213 ASSERT_SUCCESS(aws_mutex_unlock(&tester->wait_lock));
214 ASSERT_SUCCESS(err);
215 if (local_wait_result) {
216 return aws_raise_error(local_wait_result);
217 }
218 return AWS_OP_SUCCESS;
219 }
220
221 static bool s_tester_connection_setup_pred(void *user_data) {
222 struct tester *tester = user_data;
223 return (tester->server_wait_result || tester->client_wait_result) ||
224 (tester->client_connection_num == tester->wait_client_connection_num &&
225 tester->server_connection_num == tester->wait_server_connection_num);
226 }
227
228 static bool s_tester_connection_shutdown_pred(void *user_data) {
229 struct tester *tester = user_data;
230 return (tester->server_wait_result || tester->client_wait_result) ||
231 (tester->client_connection_is_shutdown == tester->wait_client_connection_is_shutdown &&
232 tester->server_connection_is_shutdown == tester->wait_server_connection_is_shutdown);
233 }
234
235 static bool s_tester_server_shutdown_pred(void *user_data) {
236 struct tester *tester = user_data;
237 return tester->server_is_shutdown;
238 }
239
240 static void s_client_connection_options_init_tester(
241 struct aws_http_client_connection_options *client_options,
242 struct tester *tester) {
243 struct aws_client_bootstrap_options bootstrap_options = {
244 .event_loop_group = tester->client_event_loop_group,
245 .host_resolver = tester->host_resolver,
246 };
247 tester->client_bootstrap = aws_client_bootstrap_new(tester->alloc, &bootstrap_options);
248 AWS_FATAL_ASSERT(tester->client_bootstrap != NULL);
249 client_options->allocator = tester->alloc;
250 client_options->bootstrap = tester->client_bootstrap;
251 client_options->host_name = aws_byte_cursor_from_c_str(tester->endpoint.address);
252 client_options->port = tester->endpoint.port;
253 client_options->socket_options = &tester->socket_options;
254 client_options->user_data = tester;
255 client_options->on_setup = s_tester_on_client_connection_setup;
256 client_options->on_shutdown = s_tester_on_client_connection_shutdown;
257 }
258
259 static int s_tls_client_opt_tester_init(
260 struct tester *tester,
261 const char *alpn_list,
262 struct aws_byte_cursor server_name) {
263
264 aws_tls_ctx_options_init_default_client(&tester->client_ctx_options, tester->alloc);
265 aws_tls_ctx_options_override_default_trust_store_from_path(&tester->client_ctx_options, NULL, "unittests.crt");
266
267 tester->client_ctx = aws_tls_client_ctx_new(tester->alloc, &tester->client_ctx_options);
268 aws_tls_connection_options_init_from_ctx(&tester->client_tls_connection_options, tester->client_ctx);
269 aws_tls_connection_options_set_alpn_list(&tester->client_tls_connection_options, tester->alloc, alpn_list);
270
271 aws_tls_connection_options_set_server_name(&tester->client_tls_connection_options, tester->alloc, &server_name);
272
273 return AWS_OP_SUCCESS;
274 }
275
276 static int s_tls_server_opt_tester_init(struct tester *tester, const char *alpn_list) {
277
278 #ifdef __APPLE__
279 struct aws_byte_cursor pwd_cur = aws_byte_cursor_from_c_str("1234");
280 ASSERT_SUCCESS(aws_tls_ctx_options_init_server_pkcs12_from_path(
281 &tester->server_ctx_options, tester->alloc, "unittests.p12", &pwd_cur));
282 #else
283 ASSERT_SUCCESS(aws_tls_ctx_options_init_default_server_from_path(
284 &tester->server_ctx_options, tester->alloc, "unittests.crt", "unittests.key"));
285 #endif /* __APPLE__ */
286 aws_tls_ctx_options_set_alpn_list(&tester->server_ctx_options, alpn_list);
287 tester->server_ctx = aws_tls_server_ctx_new(tester->alloc, &tester->server_ctx_options);
288 ASSERT_NOT_NULL(tester->server_ctx);
289
290 aws_tls_connection_options_init_from_ctx(&tester->server_tls_connection_options, tester->server_ctx);
291 return AWS_OP_SUCCESS;
292 }
293
294 static int s_tester_init(struct tester *tester, const struct tester_options *options) {
295 AWS_ZERO_STRUCT(*tester);
296
297 tester->alloc = options->alloc;
298
299 aws_http_library_init(options->alloc);
300 ASSERT_SUCCESS(aws_mutex_init(&tester->wait_lock));
301 ASSERT_SUCCESS(aws_condition_variable_init(&tester->wait_cvar));
302
303 /*
304 * The current http testing framework has several issues that hinder testing event loop pinning:
305 * (1) Server shutdown can crash with memory corruption if the server uses an event loop group with more than one
306 * thread
307 * (2) s_tester_wait mixes results from both client and server and once you unlink them out of the same, single-
308 * threaded event loop, the test assumptions start breaking due to different serializations of io events.
309 *
310 * This leads to a self-defeating situation: in order to test event loop pinning we need event loop groups with
311 * many threads, but as soon as we use one, existing tests start breaking.
312 *
313 * Event loop pinning is a critical blocker for an upcoming release, so rather than trying to figure out the
314 * underlying race condition within the http testing framework (I suspect it's socket listener related), we
315 * instead add some complexity to the testing framework such that
316 * (1) Existing tests continue to use a single event loop group with one thread
317 * (2) The event loop pinning test uses two event loop groups, the server elg with a single thread and the
318 * client elg with many threads to actually test pinning.
319 */
320 tester->server_event_loop_group = aws_event_loop_group_new_default(tester->alloc, 1, NULL);
321 if (options->pin_event_loop) {
322 tester->client_event_loop_group = aws_event_loop_group_new_default(tester->alloc, 16, NULL);
323 } else {
324 tester->client_event_loop_group = aws_event_loop_group_acquire(tester->server_event_loop_group);
325 }
326
327 struct aws_host_resolver_default_options resolver_options = {
328 .el_group = tester->client_event_loop_group,
329 .max_entries = 8,
330 };
331
332 tester->host_resolver = aws_host_resolver_new_default(tester->alloc, &resolver_options);
333 tester->server_bootstrap = aws_server_bootstrap_new(tester->alloc, tester->server_event_loop_group);
334 ASSERT_NOT_NULL(tester->server_bootstrap);
335
336 struct aws_socket_options socket_options = {
337 .type = AWS_SOCKET_STREAM,
338 .domain = options->use_tcp ? AWS_SOCKET_IPV4 : AWS_SOCKET_LOCAL,
339 .connect_timeout_ms =
340 (uint32_t)aws_timestamp_convert(TESTER_TIMEOUT_SEC, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL),
341 };
342 tester->socket_options = socket_options;
343
344 struct aws_socket_endpoint endpoint;
345 AWS_ZERO_STRUCT(endpoint);
346
347 if (options->use_tcp) {
348 snprintf(endpoint.address, sizeof(endpoint.address), "127.0.0.1");
349 } else {
350 aws_socket_endpoint_init_local_address_for_test(&endpoint);
351 }
352
353 tester->endpoint = endpoint;
354
355 /* Create server (listening socket) */
356 struct aws_http_server_options server_options = AWS_HTTP_SERVER_OPTIONS_INIT;
357 server_options.allocator = tester->alloc;
358 server_options.bootstrap = tester->server_bootstrap;
359 server_options.endpoint = &tester->endpoint;
360 server_options.socket_options = &tester->socket_options;
361 server_options.server_user_data = tester;
362 server_options.on_incoming_connection = s_tester_on_server_connection_setup;
363 server_options.on_destroy_complete = s_tester_http_server_on_destroy;
364 if (options->tls) {
365 ASSERT_SUCCESS(s_tls_server_opt_tester_init(
366 tester, options->server_alpn_list ? options->server_alpn_list : "h2;http/1.1"));
367 server_options.tls_options = &tester->server_tls_connection_options;
368 }
369
370 tester->server = aws_http_server_new(&server_options);
371 ASSERT_NOT_NULL(tester->server);
372
373 /*
374 * localhost server binds to any port, so let's get the final listener endpoint whether or not we're making
375 * connections to it.
376 */
377 if (options->use_tcp) {
378 tester->endpoint = *aws_http_server_get_listener_endpoint(tester->server);
379 }
380
381 /* If test doesn't need a connection, we're done setting up. */
382 if (options->no_connection) {
383 return AWS_OP_SUCCESS;
384 }
385
386 /* Connect */
387 struct aws_http_client_connection_options client_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT;
388 s_client_connection_options_init_tester(&client_options, tester);
389 if (options->tls) {
390 ASSERT_SUCCESS(s_tls_client_opt_tester_init(
391 tester,
392 options->client_alpn_list ? options->client_alpn_list : "h2;http/1.1",
393 aws_byte_cursor_from_c_str("localhost")));
394 client_options.tls_options = &tester->client_tls_connection_options;
395 }
396
397 if (options->pin_event_loop) {
398 client_options.requested_event_loop = aws_event_loop_group_get_next_loop(tester->client_event_loop_group);
399 }
400
401 tester->client_options = client_options;
402
403 tester->server_connection_num = 0;
404 tester->client_connection_num = 0;
405 ASSERT_SUCCESS(aws_http_client_connect(&tester->client_options));
406
407 /* Wait for server & client connections to finish setup */
408 tester->wait_client_connection_num = 1;
409 tester->wait_server_connection_num = 1;
410 ASSERT_SUCCESS(s_tester_wait(tester, s_tester_connection_setup_pred));
411
412 return AWS_OP_SUCCESS;
413 }
414
415 static int s_tester_clean_up(struct tester *tester) {
416 if (tester->server) {
417 /* server is not shut down by test, let's shut down the server here */
418 aws_http_server_release(tester->server);
419 /* wait for the server to finish shutdown process */
420 ASSERT_SUCCESS(s_tester_wait(tester, s_tester_server_shutdown_pred));
421 }
422 if (tester->server_ctx) {
423 aws_tls_connection_options_clean_up(&tester->server_tls_connection_options);
424 aws_tls_ctx_release(tester->server_ctx);
425 aws_tls_ctx_options_clean_up(&tester->server_ctx_options);
426 }
427 if (tester->client_ctx) {
428 aws_tls_connection_options_clean_up(&tester->client_tls_connection_options);
429 aws_tls_ctx_release(tester->client_ctx);
430 aws_tls_ctx_options_clean_up(&tester->client_ctx_options);
431 }
432 aws_byte_buf_clean_up(&tester->negotiated_protocol);
433 aws_server_bootstrap_release(tester->server_bootstrap);
434 aws_client_bootstrap_release(tester->client_bootstrap);
435 aws_host_resolver_release(tester->host_resolver);
436 aws_event_loop_group_release(tester->client_event_loop_group);
437 aws_event_loop_group_release(tester->server_event_loop_group);
438
439 aws_http_library_clean_up();
440 aws_mutex_clean_up(&tester->wait_lock);
441
442 return AWS_OP_SUCCESS;
443 }
444
445 static int s_test_server_new_destroy(struct aws_allocator *allocator, void *ctx) {
446 (void)ctx;
447 struct tester_options options = {
448 .alloc = allocator,
449 .no_connection = true,
450 };
451 struct tester tester;
452 ASSERT_SUCCESS(s_tester_init(&tester, &options));
453
454 ASSERT_SUCCESS(s_tester_clean_up(&tester));
455 return AWS_OP_SUCCESS;
456 }
457 AWS_TEST_CASE(server_new_destroy, s_test_server_new_destroy);
458
459 static int s_test_server_new_destroy_tcp(struct aws_allocator *allocator, void *ctx) {
460 (void)ctx;
461 struct tester_options options = {.alloc = allocator, .no_connection = true, .use_tcp = true};
462 struct tester tester;
463 ASSERT_SUCCESS(s_tester_init(&tester, &options));
464
465 const struct aws_socket_endpoint *listener_endpoint = aws_http_server_get_listener_endpoint(tester.server);
466 ASSERT_TRUE(listener_endpoint->port > 0);
467
468 ASSERT_SUCCESS(s_tester_clean_up(&tester));
469 return AWS_OP_SUCCESS;
470 }
471 AWS_TEST_CASE(server_new_destroy_tcp, s_test_server_new_destroy_tcp);
472
473 void release_all_client_connections(struct tester *tester) {
474 for (int i = 0; i < tester->client_connection_num; i++) {
475 aws_http_connection_release(tester->client_connections[i]);
476 }
477 /* wait for all the connections to shutdown */
478 tester->wait_client_connection_is_shutdown = tester->client_connection_num;
479 }
480
481 void release_all_server_connections(struct tester *tester) {
482 for (int i = 0; i < tester->server_connection_num; i++) {
483 aws_http_connection_release(tester->server_connections[i]);
484 }
485 /* wait for all the connections to shutdown */
486 tester->wait_server_connection_is_shutdown = tester->server_connection_num;
487 }
488
489 static int s_test_connection_setup_shutdown(struct aws_allocator *allocator, void *ctx) {
490 (void)ctx;
491 struct tester_options options = {
492 .alloc = allocator,
493 };
494 struct tester tester;
495 ASSERT_SUCCESS(s_tester_init(&tester, &options));
496
497 release_all_client_connections(&tester);
498 release_all_server_connections(&tester);
499 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_shutdown_pred));
500
501 ASSERT_SUCCESS(s_tester_clean_up(&tester));
502 return AWS_OP_SUCCESS;
503 }
504 AWS_TEST_CASE(connection_setup_shutdown, s_test_connection_setup_shutdown);
505
506 static int s_test_connection_setup_shutdown_tls(struct aws_allocator *allocator, void *ctx) {
507 (void)ctx;
508
509 struct tester_options options = {
510 .alloc = allocator,
511 .tls = true,
512 };
513 struct tester tester;
514 ASSERT_SUCCESS(s_tester_init(&tester, &options));
515
516 release_all_client_connections(&tester);
517 release_all_server_connections(&tester);
518 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_shutdown_pred));
519
520 ASSERT_SUCCESS(s_tester_clean_up(&tester));
521 return AWS_OP_SUCCESS;
522 }
523 AWS_TEST_CASE(connection_setup_shutdown_tls, s_test_connection_setup_shutdown_tls);
524
525 static int s_test_connection_setup_shutdown_proxy_setting_on_ev_not_found(struct aws_allocator *allocator, void *ctx) {
526 (void)ctx;
527 struct tester_options options = {
528 .alloc = allocator,
529 .no_connection = true,
530 };
531 struct tester tester;
532 ASSERT_SUCCESS(s_tester_init(&tester, &options));
533 struct aws_http_client_connection_options client_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT;
534 struct proxy_env_var_settings proxy_ev_settings;
535 AWS_ZERO_STRUCT(proxy_ev_settings);
536 proxy_ev_settings.env_var_type = AWS_HPEV_ENABLE;
537 client_options.proxy_ev_settings = &proxy_ev_settings;
538
539 s_client_connection_options_init_tester(&client_options, &tester);
540 tester.client_options = client_options;
541
542 tester.server_connection_num = 0;
543 tester.client_connection_num = 0;
544 ASSERT_SUCCESS(aws_http_client_connect(&tester.client_options));
545
546 /* Wait for server & client connections to finish setup */
547 tester.wait_client_connection_num = 1;
548 tester.wait_server_connection_num = 1;
549 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_setup_pred));
550
551 release_all_client_connections(&tester);
552 release_all_server_connections(&tester);
553 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_shutdown_pred));
554
555 ASSERT_SUCCESS(s_tester_clean_up(&tester));
556 return AWS_OP_SUCCESS;
557 }
558 AWS_TEST_CASE(
559 connection_setup_shutdown_proxy_setting_on_ev_not_found,
560 s_test_connection_setup_shutdown_proxy_setting_on_ev_not_found);
561
562 static int s_test_connection_h2_prior_knowledge(struct aws_allocator *allocator, void *ctx) {
563 (void)ctx;
564 struct tester_options options = {
565 .alloc = allocator,
566 .no_connection = true,
567 };
568 struct tester tester;
569 ASSERT_SUCCESS(s_tester_init(&tester, &options));
570
571 /* Connect */
572 struct aws_http_client_connection_options client_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT;
573 s_client_connection_options_init_tester(&client_options, &tester);
574 client_options.prior_knowledge_http2 = true;
575 tester.client_options = client_options;
576
577 tester.server_connection_num = 0;
578 tester.client_connection_num = 0;
579 ASSERT_SUCCESS(aws_http_client_connect(&tester.client_options));
580
581 /* Wait for server & client connections to finish setup */
582 tester.wait_client_connection_num = 1;
583 tester.wait_server_connection_num = 1;
584 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_setup_pred));
585
586 /* Assert that we made an http2 connection */
587 ASSERT_INT_EQUALS(tester.connection_version, AWS_HTTP_VERSION_2);
588
589 /* clean up */
590 release_all_client_connections(&tester);
591 release_all_server_connections(&tester);
592 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_shutdown_pred));
593
594 ASSERT_SUCCESS(s_tester_clean_up(&tester));
595 return AWS_OP_SUCCESS;
596 }
597 AWS_TEST_CASE(connection_h2_prior_knowledge, s_test_connection_h2_prior_knowledge);
598
599 static int s_test_connection_h2_prior_knowledge_not_work_with_tls(struct aws_allocator *allocator, void *ctx) {
600 (void)ctx;
601 struct tester_options options = {
602 .alloc = allocator,
603 .no_connection = true,
604 .tls = true,
605 .server_alpn_list = "http/1.1",
606 };
607 struct tester tester;
608 ASSERT_SUCCESS(s_tester_init(&tester, &options));
609
610 /* Connect with prior knowledge */
611 struct aws_http_client_connection_options client_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT;
612 s_client_connection_options_init_tester(&client_options, &tester);
613 ASSERT_SUCCESS(s_tls_client_opt_tester_init(&tester, "http/1.1", aws_byte_cursor_from_c_str("localhost")));
614 client_options.tls_options = &tester.client_tls_connection_options;
615 client_options.prior_knowledge_http2 = true;
616 tester.client_options = client_options;
617
618 tester.server_connection_num = 0;
619 tester.client_connection_num = 0;
620 /* prior knowledge only works with cleartext TCP */
621 ASSERT_FAILS(aws_http_client_connect(&tester.client_options));
622
623 ASSERT_SUCCESS(s_tester_clean_up(&tester));
624 return AWS_OP_SUCCESS;
625 }
626 AWS_TEST_CASE(connection_h2_prior_knowledge_not_work_with_tls, s_test_connection_h2_prior_knowledge_not_work_with_tls);
627
628 static void s_on_tester_negotiation_result(
629 struct aws_channel_handler *handler,
630 struct aws_channel_slot *slot,
631 int err_code,
632 void *user_data) {
633
634 (void)slot;
635 (void)err_code;
636 struct tester *tester = (struct tester *)user_data;
637 struct aws_byte_buf src = aws_tls_handler_protocol(handler);
638 aws_byte_buf_init_copy(&tester->negotiated_protocol, tester->alloc, &src);
639 }
640
641 static int s_test_connection_customized_alpn(struct aws_allocator *allocator, void *ctx) {
642 (void)ctx;
643 char customized_alpn_string[] = "myh2";
644 enum aws_http_version expected_version = AWS_HTTP_VERSION_2;
645 struct tester_options options = {
646 .alloc = allocator,
647 .no_connection = true,
648 .tls = true,
649 .server_alpn_list = "myh2;myh1.1;h2;http/1.1",
650 };
651 struct tester tester;
652 ASSERT_SUCCESS(s_tester_init(&tester, &options));
653
654 /* Connect with ALPN and the customized alpn string map */
655 struct aws_http_client_connection_options client_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT;
656 s_client_connection_options_init_tester(&client_options, &tester);
657 ASSERT_SUCCESS(
658 s_tls_client_opt_tester_init(&tester, customized_alpn_string, aws_byte_cursor_from_c_str("localhost")));
659 aws_tls_connection_options_set_callbacks(
660 &tester.client_tls_connection_options, s_on_tester_negotiation_result, NULL, NULL, &tester);
661 client_options.tls_options = &tester.client_tls_connection_options;
662 /* create the alpn map */
663 struct aws_hash_table alpn_map;
664 AWS_ZERO_STRUCT(alpn_map);
665 ASSERT_SUCCESS(aws_http_alpn_map_init(allocator, &alpn_map));
666 /* We don't need to clean up the string as the map will own the string */
667 struct aws_string *alpn_string = aws_string_new_from_c_str(allocator, customized_alpn_string);
668 ASSERT_SUCCESS(aws_hash_table_put(&alpn_map, alpn_string, (void *)(size_t)expected_version, NULL));
669 client_options.alpn_string_map = &alpn_map;
670 tester.client_options = client_options;
671
672 tester.server_connection_num = 0;
673 tester.client_connection_num = 0;
674 ASSERT_SUCCESS(aws_http_client_connect(&tester.client_options));
675 /* We should be safe to free the map */
676 aws_hash_table_clean_up(&alpn_map);
677
678 /* Wait for server & client connections to finish setup */
679 tester.wait_client_connection_num = 1;
680 tester.wait_server_connection_num = 1;
681 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_setup_pred));
682
683 #ifndef __APPLE__ /* Server side ALPN doesn't work for MacOS */
684 /* Assert that we have the negotiated protocol and the expected version */
685 ASSERT_INT_EQUALS(tester.connection_version, expected_version);
686 ASSERT_TRUE(aws_byte_buf_eq_c_str(&tester.negotiated_protocol, customized_alpn_string));
687 #endif
688 /* clean up */
689 release_all_client_connections(&tester);
690 release_all_server_connections(&tester);
691 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_shutdown_pred));
692
693 ASSERT_SUCCESS(s_tester_clean_up(&tester));
694 return AWS_OP_SUCCESS;
695 }
696 AWS_TEST_CASE(connection_customized_alpn, s_test_connection_customized_alpn);
697
698 static int s_test_connection_customized_alpn_error_with_unknown_return_string(
699 struct aws_allocator *allocator,
700 void *ctx) {
701 (void)ctx;
702 char customized_alpn_string[] = "myh2";
703 struct tester_options options = {
704 .alloc = allocator,
705 .no_connection = true,
706 .tls = true,
707 .server_alpn_list = "myh2;myh1.1;h2;http/1.1",
708 };
709 struct tester tester;
710 ASSERT_SUCCESS(s_tester_init(&tester, &options));
711
712 /* Connect with ALPN and the customized alpn string map */
713 struct aws_http_client_connection_options client_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT;
714 s_client_connection_options_init_tester(&client_options, &tester);
715 ASSERT_SUCCESS(
716 s_tls_client_opt_tester_init(&tester, customized_alpn_string, aws_byte_cursor_from_c_str("localhost")));
717 aws_tls_connection_options_set_callbacks(
718 &tester.client_tls_connection_options, s_on_tester_negotiation_result, NULL, NULL, &tester);
719 client_options.tls_options = &tester.client_tls_connection_options;
720 /* create the alpn map */
721 struct aws_hash_table alpn_map;
722 AWS_ZERO_STRUCT(alpn_map);
723 ASSERT_SUCCESS(aws_http_alpn_map_init(allocator, &alpn_map));
724 /* put an empty ALPN map, you will not found the returned string, and should error out when trying to connect*/
725 client_options.alpn_string_map = &alpn_map;
726 tester.client_options = client_options;
727
728 tester.server_connection_num = 0;
729 tester.client_connection_num = 0;
730 ASSERT_SUCCESS(aws_http_client_connect(&tester.client_options));
731 /* We should be safe to free the map */
732 aws_hash_table_clean_up(&alpn_map);
733
734 /* Wait for server & client connections to finish setup */
735 tester.wait_client_connection_num = 1;
736 tester.wait_server_connection_num = 1;
737
738 #ifndef __APPLE__ /* Server side ALPN doesn't work for MacOS */
739 ASSERT_FAILS(s_tester_wait(&tester, s_tester_connection_setup_pred));
740 /* Assert that we have the negotiated protocol and error returned from callback */
741 ASSERT_TRUE(aws_byte_buf_eq_c_str(&tester.negotiated_protocol, customized_alpn_string));
742 ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_HTTP_UNSUPPORTED_PROTOCOL);
743 #else
744 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_setup_pred));
745 #endif
746 /* clean up */
747 release_all_client_connections(&tester);
748 release_all_server_connections(&tester);
749 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_shutdown_pred));
750
751 ASSERT_SUCCESS(s_tester_clean_up(&tester));
752 return AWS_OP_SUCCESS;
753 }
754 AWS_TEST_CASE(
755 connection_customized_alpn_error_with_unknown_return_string,
756 s_test_connection_customized_alpn_error_with_unknown_return_string);
757
758 static int s_test_connection_destroy_server_with_connection_existing(struct aws_allocator *allocator, void *ctx) {
759 (void)ctx;
760 struct tester_options options = {
761 .alloc = allocator,
762 };
763 struct tester tester;
764 ASSERT_SUCCESS(s_tester_init(&tester, &options));
765
766 aws_http_server_release(tester.server);
767 /* wait for all connections to be shut down */
768 tester.wait_client_connection_is_shutdown = tester.client_connection_num;
769 tester.wait_server_connection_is_shutdown = tester.server_connection_num;
770 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_shutdown_pred));
771
772 /* check the server is destroyed */
773 ASSERT_TRUE(tester.server_is_shutdown);
774 /* release memory */
775 release_all_client_connections(&tester);
776 release_all_server_connections(&tester);
777
778 ASSERT_SUCCESS(s_tester_clean_up(&tester));
779 return AWS_OP_SUCCESS;
780 }
781 AWS_TEST_CASE(
782 connection_destroy_server_with_connection_existing,
783 s_test_connection_destroy_server_with_connection_existing);
784
785 /* multiple connections */
786 static int s_test_connection_destroy_server_with_multiple_connections_existing(
787 struct aws_allocator *allocator,
788 void *ctx) {
789 (void)ctx;
790 struct tester_options options = {
791 .alloc = allocator,
792 };
793 struct tester tester;
794 ASSERT_SUCCESS(s_tester_init(&tester, &options));
795
796 /* more connections! */
797 int more_connection_num = 1;
798 /* set waiting condition */
799 tester.wait_client_connection_num += more_connection_num;
800 tester.wait_server_connection_num += more_connection_num;
801 /* connect */
802 for (int i = 0; i < more_connection_num; i++) {
803 ASSERT_SUCCESS(aws_http_client_connect(&tester.client_options));
804 }
805 /* wait for connections */
806 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_setup_pred));
807
808 aws_http_server_release(tester.server);
809 /* wait for all connections to be shut down */
810 tester.wait_client_connection_is_shutdown = tester.client_connection_num;
811 tester.wait_server_connection_is_shutdown = tester.server_connection_num;
812 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_shutdown_pred));
813
814 /* check the server is destroyed */
815 ASSERT_TRUE(tester.server_is_shutdown);
816 /* release memory */
817 release_all_client_connections(&tester);
818 release_all_server_connections(&tester);
819
820 ASSERT_SUCCESS(s_tester_clean_up(&tester));
821 return AWS_OP_SUCCESS;
822 }
823 AWS_TEST_CASE(
824 connection_destroy_server_with_multiple_connections_existing,
825 s_test_connection_destroy_server_with_multiple_connections_existing);
826
827 static void s_block_task(struct aws_task *task, void *arg, enum aws_task_status status) {
828 (void)status;
829 /* sleep for 2 sec */
830 struct tester *tester = arg;
831 aws_thread_current_sleep(2000000000);
832 aws_mem_release(tester->alloc, task);
833 }
834
835 static void s_tester_on_new_client_connection_setup(
836 struct aws_http_connection *connection,
837 int error_code,
838 void *user_data) {
839
840 struct tester *tester = user_data;
841 AWS_FATAL_ASSERT(aws_mutex_lock(&tester->wait_lock) == AWS_OP_SUCCESS);
842 tester->new_client_setup_finished = true;
843 if (error_code) {
844 tester->client_wait_result = error_code;
845 goto done;
846 }
847 tester->new_client_connection = connection;
848 done:
849 AWS_FATAL_ASSERT(aws_mutex_unlock(&tester->wait_lock) == AWS_OP_SUCCESS);
850 aws_condition_variable_notify_one(&tester->wait_cvar);
851 }
852
853 static void s_tester_on_new_client_connection_shutdown(
854 struct aws_http_connection *connection,
855 int error_code,
856 void *user_data) {
857
858 (void)connection;
859 (void)error_code;
860 struct tester *tester = user_data;
861 AWS_FATAL_ASSERT(aws_mutex_lock(&tester->wait_lock) == AWS_OP_SUCCESS);
862
863 tester->new_client_shut_down = true;
864
865 AWS_FATAL_ASSERT(aws_mutex_unlock(&tester->wait_lock) == AWS_OP_SUCCESS);
866 aws_condition_variable_notify_one(&tester->wait_cvar);
867 }
868
869 static bool s_tester_new_client_setup_pred(void *user_data) {
870 struct tester *tester = user_data;
871 return tester->new_client_setup_finished;
872 }
873
874 static bool s_tester_new_client_shutdown_pred(void *user_data) {
875 struct tester *tester = user_data;
876 return tester->new_client_shut_down;
877 }
878
879 /* when we shutdown the server, no more new connection will be accepted */
880 static int s_test_connection_server_shutting_down_new_connection_setup_fail(
881 struct aws_allocator *allocator,
882 void *ctx) {
883 (void)ctx;
884 struct tester_options options = {
885 .alloc = allocator,
886 };
887 struct tester tester;
888 ASSERT_SUCCESS(s_tester_init(&tester, &options));
889
890 /* Connect */
891 struct aws_socket_options socket_options = {
892 .type = AWS_SOCKET_STREAM,
893 .domain = AWS_SOCKET_LOCAL,
894 .connect_timeout_ms =
895 (uint32_t)aws_timestamp_convert(TESTER_TIMEOUT_SEC, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL),
896 };
897 /* create a new eventloop for the new connection and block the new connection. Waiting server to begin shutting
898 * down. */
899 struct aws_event_loop_group *event_loop_group = aws_event_loop_group_new_default(allocator, 1, NULL);
900
901 /* get the first eventloop, which will be the eventloop for client to connect */
902 struct aws_event_loop *current_eventloop = aws_event_loop_group_get_loop_at(event_loop_group, 0);
903 struct aws_task *block_task = aws_mem_acquire(allocator, sizeof(struct aws_task));
904 aws_task_init(block_task, s_block_task, &tester, "wait_a_bit");
905 aws_event_loop_schedule_task_now(current_eventloop, block_task);
906
907 /* get the first eventloop of tester, which will be the eventloop for server listener socket, block the listener
908 * socket */
909 struct aws_event_loop *server_eventloop = aws_event_loop_group_get_loop_at(tester.server_event_loop_group, 0);
910 struct aws_task *server_block_task = aws_mem_acquire(allocator, sizeof(struct aws_task));
911 aws_task_init(server_block_task, s_block_task, &tester, "wait_a_bit");
912 aws_event_loop_schedule_task_now(server_eventloop, server_block_task);
913
914 struct aws_client_bootstrap_options bootstrap_options = {
915 .event_loop_group = event_loop_group,
916 .host_resolver = tester.host_resolver,
917 };
918 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
919 struct aws_http_client_connection_options client_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT;
920 client_options.allocator = tester.alloc;
921 client_options.bootstrap = bootstrap;
922 client_options.host_name = aws_byte_cursor_from_c_str(tester.endpoint.address);
923 client_options.port = tester.endpoint.port;
924 client_options.socket_options = &socket_options;
925 client_options.user_data = &tester;
926 client_options.on_setup = s_tester_on_new_client_connection_setup;
927 client_options.on_shutdown = s_tester_on_new_client_connection_shutdown;
928
929 /* new connection will be blocked for 2 sec */
930 tester.wait_server_connection_num++;
931 ASSERT_SUCCESS(aws_http_client_connect(&client_options));
932
933 /* shutting down the server */
934 aws_http_server_release(tester.server);
935 /* the server side connection failed with error code, closed */
936 ASSERT_FAILS(s_tester_wait(&tester, s_tester_connection_setup_pred));
937 /* wait for the client side connection */
938 s_tester_wait(&tester, s_tester_new_client_setup_pred);
939
940 if (tester.new_client_connection && !tester.client_connection_is_shutdown) {
941 /* wait for it to shut down, we do not need to call shut down, the socket will know */
942 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_new_client_shutdown_pred));
943 }
944
945 if (tester.new_client_connection) {
946 aws_http_connection_release(tester.new_client_connection);
947 }
948
949 /* wait for the old connections to be shut down */
950 tester.wait_client_connection_is_shutdown = tester.client_connection_num;
951 tester.wait_server_connection_is_shutdown = tester.server_connection_num;
952 /* assert the new connection fail to set up in user's perspective */
953 ASSERT_TRUE(tester.client_connection_num == 1);
954 ASSERT_TRUE(tester.server_connection_num == 1);
955 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_shutdown_pred));
956
957 /* release memory */
958 release_all_client_connections(&tester);
959 release_all_server_connections(&tester);
960 aws_client_bootstrap_release(bootstrap);
961 aws_event_loop_group_release(event_loop_group);
962 ASSERT_SUCCESS(s_tester_clean_up(&tester));
963
964 return AWS_OP_SUCCESS;
965 }
966 AWS_TEST_CASE(
967 connection_server_shutting_down_new_connection_setup_fail,
968 s_test_connection_server_shutting_down_new_connection_setup_fail);
969
970 static int s_test_connection_setup_shutdown_pinned_event_loop(struct aws_allocator *allocator, void *ctx) {
971 (void)ctx;
972 struct tester_options options = {
973 .alloc = allocator,
974 .pin_event_loop = true,
975 };
976 struct tester tester;
977 ASSERT_SUCCESS(s_tester_init(&tester, &options));
978
979 for (int i = 0; i < tester.client_connection_num; i++) {
980 struct aws_http_connection *connection = tester.client_connections[i];
981 ASSERT_PTR_EQUALS(
982 tester.client_options.requested_event_loop, aws_channel_get_event_loop(connection->channel_slot->channel));
983 }
984
985 release_all_client_connections(&tester);
986 release_all_server_connections(&tester);
987 ASSERT_SUCCESS(s_tester_wait(&tester, s_tester_connection_shutdown_pred));
988
989 ASSERT_SUCCESS(s_tester_clean_up(&tester));
990 return AWS_OP_SUCCESS;
991 }
992 AWS_TEST_CASE(connection_setup_shutdown_pinned_event_loop, s_test_connection_setup_shutdown_pinned_event_loop);