Debian Codegraph @

crt/aws-c-http/tests/test_localhost_integ.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 #include <aws/testing/aws_test_harness.h>
6
7 #include <aws/http/private/hpack.h>
8
9 #include <aws/common/clock.h>
10 #include <aws/common/condition_variable.h>
11 #include <aws/common/device_random.h>
12 #include <aws/common/environment.h>
13 #include <aws/common/string.h>
14 #include <aws/http/connection.h>
15 #include <aws/http/request_response.h>
16 #include <aws/io/channel_bootstrap.h>
17 #include <aws/io/event_loop.h>
18 #include <aws/io/logging.h>
19 #include <aws/io/socket.h>
20 #include <aws/io/stream.h>
21 #include <aws/io/tls_channel_handler.h>
22
23 #include "h2_test_helper.h"
24
25 static int s_tester_on_headers(
26 struct aws_http_stream *stream,
27 enum aws_http_header_block header_block,
28 const struct aws_http_header *header_array,
29 size_t num_headers,
30 void *user_data) {
31 (void)stream;
32 (void)header_block;
33 struct aws_http_headers *received_headers = (struct aws_http_headers *)user_data;
34
35 for (size_t i = 0; i < num_headers; ++i) {
36 ASSERT_SUCCESS(aws_http_headers_add_header(received_headers, &header_array[i]));
37 }
38 return AWS_OP_SUCCESS;
39 }
40
41 static bool s_check_headers_received(
42 const struct aws_http_headers *received_headers,
43 const struct aws_http_headers *headers_to_check) {
44 for (size_t i = 0; i < aws_http_headers_count(headers_to_check); i++) {
45 struct aws_http_header header;
46 if (aws_http_headers_get_index(headers_to_check, i, &header)) {
47 return false;
48 }
49 struct aws_http_header received_header;
50 if (aws_http_headers_get_index(received_headers, i + 1, &received_header)) {
51 /* Not found */
52 return false;
53 }
54 if (!aws_byte_cursor_eq(&received_header.value, &header.value) ||
55 !aws_byte_cursor_eq(&received_header.name, &header.name)) {
56 return false;
57 }
58 }
59 return true;
60 }
61
62 struct tester {
63 struct aws_allocator *alloc;
64
65 struct aws_event_loop_group *event_loop_group;
66 struct aws_host_resolver *host_resolver;
67 struct aws_client_bootstrap *client_bootstrap;
68 struct aws_tls_ctx_options tls_ctx_options;
69 struct aws_tls_ctx *tls_ctx;
70 struct aws_tls_connection_options tls_connection_options;
71 struct aws_http_connection *connection;
72
73 struct aws_mutex wait_lock;
74 struct aws_condition_variable wait_cvar;
75
76 bool shutdown_finished;
77 size_t wait_for_stream_completed_count;
78 size_t stream_completed_count;
79 size_t stream_complete_errors;
80 size_t stream_200_count;
81 size_t stream_status_not_200_count;
82
83 uint64_t num_sen_received;
84 int stream_completed_error_code;
85 bool stream_completed_with_200;
86
87 size_t download_body_len;
88 size_t content_len;
89
90 int wait_result;
91 };
92
93 static struct tester s_tester;
94
95 #define DEFINE_HEADER(NAME, VALUE) \
96 { \
97 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \
98 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \
99 }
100
101 enum {
102 TESTER_TIMEOUT_SEC = 60, /* Give enough time for non-sudo users to enter password */
103 };
104
105 static void s_on_connection_setup(struct aws_http_connection *connection, int error_code, void *user_data) {
106
107 struct tester *tester = user_data;
108 AWS_FATAL_ASSERT(aws_mutex_lock(&tester->wait_lock) == AWS_OP_SUCCESS);
109
110 if (error_code) {
111 tester->wait_result = error_code;
112 goto done;
113 }
114 tester->connection = connection;
115 done:
116 AWS_FATAL_ASSERT(aws_mutex_unlock(&tester->wait_lock) == AWS_OP_SUCCESS);
117 aws_condition_variable_notify_one(&tester->wait_cvar);
118 }
119
120 static void s_on_connection_shutdown(struct aws_http_connection *connection, int error_code, void *user_data) {
121
122 (void)connection;
123 (void)error_code;
124 struct tester *tester = user_data;
125 AWS_FATAL_ASSERT(aws_mutex_lock(&tester->wait_lock) == AWS_OP_SUCCESS);
126
127 tester->shutdown_finished = true;
128
129 AWS_FATAL_ASSERT(aws_mutex_unlock(&tester->wait_lock) == AWS_OP_SUCCESS);
130 aws_condition_variable_notify_one(&tester->wait_cvar);
131 }
132
133 static bool s_is_connected(void *context) {
134 struct tester *tester = context;
135
136 return tester->connection != NULL;
137 }
138
139 static int s_wait_on_connection_connected(struct tester *tester) {
140
141 ASSERT_SUCCESS(aws_mutex_lock(&tester->wait_lock));
142 int signal_error = aws_condition_variable_wait_pred(&tester->wait_cvar, &tester->wait_lock, s_is_connected, tester);
143 ASSERT_SUCCESS(aws_mutex_unlock(&tester->wait_lock));
144 return signal_error;
145 }
146
147 static bool s_is_shutdown(void *context) {
148 struct tester *tester = context;
149
150 return tester->shutdown_finished;
151 }
152
153 static int s_wait_on_connection_shutdown(struct tester *tester) {
154
155 ASSERT_SUCCESS(aws_mutex_lock(&tester->wait_lock));
156 int signal_error = aws_condition_variable_wait_pred(&tester->wait_cvar, &tester->wait_lock, s_is_shutdown, tester);
157 ASSERT_SUCCESS(aws_mutex_unlock(&tester->wait_lock));
158 return signal_error;
159 }
160
161 static bool s_is_stream_completed_count_at_least(void *context) {
162 (void)context;
163 return s_tester.wait_for_stream_completed_count <= s_tester.stream_completed_count;
164 }
165
166 static int s_wait_on_streams_completed_count(size_t count) {
167
168 ASSERT_SUCCESS(aws_mutex_lock(&s_tester.wait_lock));
169 s_tester.wait_for_stream_completed_count = count;
170 int signal_error = aws_condition_variable_wait_pred(
171 &s_tester.wait_cvar, &s_tester.wait_lock, s_is_stream_completed_count_at_least, &s_tester);
172 ASSERT_SUCCESS(aws_mutex_unlock(&s_tester.wait_lock));
173 return signal_error;
174 }
175
176 static void s_tester_on_stream_completed(struct aws_http_stream *stream, int error_code, void *user_data) {
177 (void)user_data;
178 (void)stream;
179 AWS_FATAL_ASSERT(aws_mutex_lock(&s_tester.wait_lock) == AWS_OP_SUCCESS);
180 if (error_code) {
181 ++s_tester.stream_complete_errors;
182 s_tester.stream_completed_error_code = error_code;
183 } else {
184 int status = 0;
185 if (aws_http_stream_get_incoming_response_status(stream, &status)) {
186 ++s_tester.stream_complete_errors;
187 s_tester.stream_completed_error_code = aws_last_error();
188 } else {
189 if (status / 100 == 2) {
190 s_tester.stream_completed_with_200 = true;
191 ++s_tester.stream_200_count;
192 } else {
193 ++s_tester.stream_status_not_200_count;
194 }
195 }
196 }
197 ++s_tester.stream_completed_count;
198 aws_condition_variable_notify_one(&s_tester.wait_cvar);
199 AWS_FATAL_ASSERT(aws_mutex_unlock(&s_tester.wait_lock) == AWS_OP_SUCCESS);
200 }
201
202 static struct aws_logger s_logger;
203
204 static int s_tester_init(struct tester *tester, struct aws_allocator *allocator, struct aws_byte_cursor host_name) {
205 aws_http_library_init(allocator);
206
207 ASSERT_SUCCESS(aws_mutex_init(&tester->wait_lock));
208 ASSERT_SUCCESS(aws_condition_variable_init(&tester->wait_cvar));
209 tester->event_loop_group = aws_event_loop_group_new_default(allocator, 1, NULL);
210
211 struct aws_host_resolver_default_options resolver_options = {
212 .el_group = tester->event_loop_group,
213 .max_entries = 8,
214 };
215
216 tester->host_resolver = aws_host_resolver_new_default(allocator, &resolver_options);
217 /* Create http connection */
218 struct aws_client_bootstrap_options bootstrap_options = {
219 .event_loop_group = tester->event_loop_group,
220 .host_resolver = tester->host_resolver,
221 };
222 tester->client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
223
224 aws_tls_ctx_options_init_default_client(&tester->tls_ctx_options, allocator);
225 aws_tls_ctx_options_set_alpn_list(&tester->tls_ctx_options, "h2");
226 /* Turn off peer verification as a localhost cert used */
227 tester->tls_ctx_options.verify_peer = false;
228
229 tester->tls_ctx = aws_tls_client_ctx_new(allocator, &tester->tls_ctx_options);
230 aws_tls_connection_options_init_from_ctx(&tester->tls_connection_options, tester->tls_ctx);
231 aws_tls_connection_options_set_server_name(&tester->tls_connection_options, allocator, &host_name);
232 struct aws_socket_options socket_options = {
233 .type = AWS_SOCKET_STREAM,
234 .connect_timeout_ms =
235 (uint32_t)aws_timestamp_convert(TESTER_TIMEOUT_SEC, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL),
236 .keep_alive_timeout_sec = 0,
237 .keepalive = false,
238 .keep_alive_interval_sec = 0,
239 };
240 struct aws_http_connection_monitoring_options monitor_opt = {
241 .allowable_throughput_failure_interval_seconds = 2,
242 .minimum_throughput_bytes_per_second = 1000,
243 };
244 struct aws_http_client_connection_options client_options = {
245 .self_size = sizeof(struct aws_http_client_connection_options),
246 .allocator = allocator,
247 .bootstrap = tester->client_bootstrap,
248 .host_name = host_name,
249 .port = 3443,
250 .socket_options = &socket_options,
251 .user_data = tester,
252 .tls_options = &tester->tls_connection_options,
253 .on_setup = s_on_connection_setup,
254 .on_shutdown = s_on_connection_shutdown,
255 .monitoring_options = &monitor_opt,
256 };
257 ASSERT_SUCCESS(aws_http_client_connect(&client_options));
258 struct aws_logger_standard_options logger_options = {
259 .level = AWS_LOG_LEVEL_DEBUG, /* We are stress testing, and if this ever failed, the default trace level log is
260 too much to handle, let's do debug level instead */
261 .file = stderr,
262 };
263
264 aws_logger_init_standard(&s_logger, allocator, &logger_options);
265 aws_logger_set(&s_logger);
266 return AWS_OP_SUCCESS;
267 }
268
269 static int s_tester_clean_up(struct tester *tester) {
270 aws_http_connection_release(tester->connection);
271 ASSERT_SUCCESS(s_wait_on_connection_shutdown(tester));
272
273 aws_tls_connection_options_clean_up(&tester->tls_connection_options);
274 aws_tls_ctx_release(tester->tls_ctx);
275 aws_tls_ctx_options_clean_up(&tester->tls_ctx_options);
276 aws_client_bootstrap_release(tester->client_bootstrap);
277 aws_host_resolver_release(tester->host_resolver);
278 aws_event_loop_group_release(tester->event_loop_group);
279
280 aws_mutex_clean_up(&tester->wait_lock);
281 aws_http_library_clean_up();
282 aws_logger_clean_up(&s_logger);
283 return AWS_OP_SUCCESS;
284 }
285
286 AWS_STATIC_STRING_FROM_LITERAL(s_http_localhost_env_var, "AWS_TEST_LOCALHOST_HOST");
287
288 static int s_test_hpack_stress_helper(struct aws_allocator *allocator, bool compression) {
289 /* Test that makes tons of streams with all sorts of headers to stress hpack */
290 struct aws_string *http_localhost_host = NULL;
291 if (aws_get_environment_value(allocator, s_http_localhost_env_var, &http_localhost_host) ||
292 http_localhost_host == NULL) {
293 /* The envrionment variable is not set, default to localhost */
294 http_localhost_host = aws_string_new_from_c_str(allocator, "localhost");
295 }
296 struct aws_byte_cursor host_name = aws_byte_cursor_from_string(http_localhost_host);
297 ASSERT_SUCCESS(s_tester_init(&s_tester, allocator, host_name));
298 /* wait for connection connected */
299 ASSERT_SUCCESS(s_wait_on_connection_connected(&s_tester));
300 // localhost/echo is an echo server that will return the headers of your request from the body.
301 struct aws_http_header request_headers_src[] = {
302 DEFINE_HEADER(":method", "GET"),
303 DEFINE_HEADER(":scheme", "https"),
304 DEFINE_HEADER(":path", "/echo"),
305 {
306 .name = aws_byte_cursor_from_c_str(":authority"),
307 .value = host_name,
308 },
309 };
310
311 size_t num_to_acquire = 2000;
312 size_t num_headers_to_make = 100;
313
314 /* Use a pool of headers and a pool of values, pick up randomly from both pool to stress hpack */
315 size_t headers_pool_size = 500;
316 size_t values_pool_size = 66;
317
318 for (size_t i = 0; i < num_to_acquire; i++) {
319 struct aws_http_message *request = aws_http2_message_new_request(allocator);
320 ASSERT_NOT_NULL(request);
321 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
322 struct aws_http_headers *request_headers = aws_http_message_get_headers(request);
323 struct aws_http_headers *test_headers =
324 aws_http_headers_new(allocator); /* as request headers has the pesudo headers, make a copy of the real
325 headers to check the result */
326 for (size_t j = 0; j < num_headers_to_make; j++) {
327 char test_header_str[256];
328 uint64_t random_64_bit_num = 0;
329 aws_device_random_u64(&random_64_bit_num);
330
331 size_t headers = (size_t)random_64_bit_num % headers_pool_size;
332 snprintf(test_header_str, sizeof(test_header_str), "crttest-%zu", headers);
333 char test_value_str[256];
334 size_t value = (size_t)random_64_bit_num % values_pool_size;
335 snprintf(test_value_str, sizeof(test_value_str), "value-%zu", value);
336
337 struct aws_http_header request_header = {
338 .compression =
339 compression
340 ? random_64_bit_num % 3
341 : AWS_HTTP_HEADER_COMPRESSION_USE_CACHE, // With random type of compression, make sure it works
342 .name = aws_byte_cursor_from_c_str(test_header_str),
343 .value = aws_byte_cursor_from_c_str(test_value_str),
344 };
345 ASSERT_SUCCESS(aws_http_headers_add_header(request_headers, &request_header));
346 ASSERT_SUCCESS(aws_http_headers_add_header(test_headers, &request_header));
347 }
348 struct aws_http_headers *received_headers = aws_http_headers_new(allocator);
349 struct aws_http_make_request_options request_options = {
350 .self_size = sizeof(request_options),
351 .request = request,
352 .user_data = received_headers,
353 .on_response_headers = s_tester_on_headers,
354 .on_complete = s_tester_on_stream_completed,
355 };
356 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
357 ASSERT_NOT_NULL(stream);
358 aws_http_stream_activate(stream);
359 aws_http_stream_release(stream);
360
361 /* Wait for the stream to complete */
362 ASSERT_SUCCESS(s_wait_on_streams_completed_count(1));
363 --s_tester.stream_completed_count;
364 ASSERT_TRUE(s_tester.stream_completed_with_200);
365 ASSERT_TRUE(s_check_headers_received(received_headers, test_headers));
366
367 aws_http_message_release(request);
368 aws_http_headers_release(test_headers);
369 aws_http_headers_release(received_headers);
370 }
371
372 aws_string_destroy(http_localhost_host);
373 const struct aws_socket_endpoint *remote_endpoint = aws_http_connection_get_remote_endpoint(s_tester.connection);
374 ASSERT_NOT_NULL(remote_endpoint);
375 struct aws_byte_cursor remote_ip = aws_byte_cursor_from_c_str(remote_endpoint->address);
376 /* Local host IP should always be 127.0.0.1 */
377 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&remote_ip, "127.0.0.1"));
378 return s_tester_clean_up(&s_tester);
379 }
380
381 AWS_TEST_CASE(localhost_integ_hpack_stress, test_hpack_stress)
382 static int test_hpack_stress(struct aws_allocator *allocator, void *ctx) {
383 (void)ctx;
384 return s_test_hpack_stress_helper(allocator, false /*compression*/);
385 }
386
387 AWS_TEST_CASE(localhost_integ_hpack_compression_stress, test_hpack_compression_stress)
388 static int test_hpack_compression_stress(struct aws_allocator *allocator, void *ctx) {
389 (void)ctx;
390 return s_test_hpack_stress_helper(allocator, true /*compression*/);
391 }
392
393 static int s_tester_on_put_body(struct aws_http_stream *stream, const struct aws_byte_cursor *data, void *user_data) {
394
395 (void)stream;
396 (void)user_data;
397 /* Response is JSON as string: "{\n \"bytes\": 2500000000\n}" - extract the number */
398 struct aws_byte_cursor bytes_key = aws_byte_cursor_from_c_str("\"bytes\": ");
399 struct aws_byte_cursor found;
400
401 if (aws_byte_cursor_find_exact(data, &bytes_key, &found) == AWS_OP_SUCCESS) {
402 struct aws_byte_cursor value_cursor = *data;
403 value_cursor.ptr = found.ptr + bytes_key.len;
404 value_cursor.len = data->len - (size_t)(found.ptr - data->ptr) - bytes_key.len - 2;
405 aws_byte_cursor_utf8_parse_u64(value_cursor, &s_tester.num_sen_received);
406 }
407
408 return AWS_OP_SUCCESS;
409 }
410
411 /* Test that upload a 2.5GB data to local server */
412 AWS_TEST_CASE(localhost_integ_h2_upload_stress, s_localhost_integ_h2_upload_stress)
413 static int s_localhost_integ_h2_upload_stress(struct aws_allocator *allocator, void *ctx) {
414 (void)ctx;
415 s_tester.alloc = allocator;
416
417 size_t length = 2500000000UL;
418
419 struct aws_string *http_localhost_host = NULL;
420 if (aws_get_environment_value(allocator, s_http_localhost_env_var, &http_localhost_host) ||
421 http_localhost_host == NULL) {
422 /* The envrionment variable is not set, default to localhost */
423 http_localhost_host = aws_string_new_from_c_str(allocator, "localhost");
424 }
425 struct aws_byte_cursor host_name = aws_byte_cursor_from_string(http_localhost_host);
426 ASSERT_SUCCESS(s_tester_init(&s_tester, allocator, host_name));
427 /* wait for connection connected */
428 ASSERT_SUCCESS(s_wait_on_connection_connected(&s_tester));
429 char content_length_sprintf_buffer[128] = "";
430 snprintf(content_length_sprintf_buffer, sizeof(content_length_sprintf_buffer), "%zu", length);
431
432 struct aws_http_header request_headers_src[] = {
433 DEFINE_HEADER(":method", "PUT"),
434 DEFINE_HEADER(":scheme", "https"),
435 DEFINE_HEADER(":path", "/echo"),
436 {
437 .name = aws_byte_cursor_from_c_str(":authority"),
438 .value = host_name,
439 },
440 {
441 .name = aws_byte_cursor_from_c_str("content_length"),
442 .value = aws_byte_cursor_from_c_str(content_length_sprintf_buffer),
443 },
444 DEFINE_HEADER("x-upload-test", "true"),
445 };
446 struct aws_http_message *request = aws_http2_message_new_request(allocator);
447 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
448 struct aws_input_stream *body_stream = aws_input_stream_tester_upload_new(allocator, length);
449 aws_http_message_set_body_stream(request, body_stream);
450 aws_input_stream_release(body_stream);
451
452 struct aws_http_make_request_options request_options = {
453 .self_size = sizeof(request_options),
454 .request = request,
455 .on_complete = s_tester_on_stream_completed,
456 .on_response_body = s_tester_on_put_body,
457 };
458 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
459 ASSERT_NOT_NULL(stream);
460 aws_http_stream_activate(stream);
461 aws_http_stream_release(stream);
462
463 /* Wait for the stream to complete */
464 ASSERT_SUCCESS(s_wait_on_streams_completed_count(1));
465 ASSERT_UINT_EQUALS(s_tester.num_sen_received, length);
466 ASSERT_TRUE(s_tester.stream_completed_with_200);
467
468 aws_http_message_release(request);
469 aws_string_destroy(http_localhost_host);
470 return s_tester_clean_up(&s_tester);
471 }
472
473 static int s_tester_on_download_body(
474 struct aws_http_stream *stream,
475 const struct aws_byte_cursor *data,
476 void *user_data) {
477
478 (void)stream;
479 (void)user_data;
480 s_tester.download_body_len += data->len;
481
482 return AWS_OP_SUCCESS;
483 }
484 /* Test that download a 2.5GB data from local server */
485 AWS_TEST_CASE(localhost_integ_h2_download_stress, s_localhost_integ_h2_download_stress)
486 static int s_localhost_integ_h2_download_stress(struct aws_allocator *allocator, void *ctx) {
487 (void)ctx;
488 s_tester.alloc = allocator;
489 size_t length = 2500000000UL; /* over int max, which it the max for settings */
490
491 struct aws_string *http_localhost_host = NULL;
492 if (aws_get_environment_value(allocator, s_http_localhost_env_var, &http_localhost_host) ||
493 http_localhost_host == NULL) {
494 /* The envrionment variable is not set, default to localhost */
495 http_localhost_host = aws_string_new_from_c_str(allocator, "localhost");
496 }
497 struct aws_byte_cursor host_name = aws_byte_cursor_from_string(http_localhost_host);
498 ASSERT_SUCCESS(s_tester_init(&s_tester, allocator, host_name));
499 /* wait for connection connected */
500 ASSERT_SUCCESS(s_wait_on_connection_connected(&s_tester));
501
502 char length_sprintf_buffer[128] = "";
503 snprintf(length_sprintf_buffer, sizeof(length_sprintf_buffer), "%zu", length);
504
505 struct aws_http_header request_headers_src[] = {
506 DEFINE_HEADER(":method", "GET"),
507 DEFINE_HEADER(":scheme", "https"),
508 DEFINE_HEADER(":path", "/echo"),
509 {
510 .name = aws_byte_cursor_from_c_str(":authority"),
511 .value = host_name,
512 },
513 {
514 .name = aws_byte_cursor_from_c_str("x-repeat-data"),
515 .value = aws_byte_cursor_from_c_str(length_sprintf_buffer),
516 },
517 };
518 struct aws_http_message *request = aws_http2_message_new_request(allocator);
519 ASSERT_NOT_NULL(request);
520 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
521
522 struct aws_http_make_request_options request_options = {
523 .self_size = sizeof(request_options),
524 .request = request,
525 .on_complete = s_tester_on_stream_completed,
526 .on_response_body = s_tester_on_download_body,
527 };
528 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
529 ASSERT_NOT_NULL(stream);
530 aws_http_stream_activate(stream);
531 aws_http_stream_release(stream);
532
533 /* Wait for the stream to complete */
534 ASSERT_SUCCESS(s_wait_on_streams_completed_count(1));
535 ASSERT_UINT_EQUALS(s_tester.download_body_len, length);
536 ASSERT_TRUE(s_tester.stream_completed_with_200);
537
538 aws_http_message_release(request);
539 aws_string_destroy(http_localhost_host);
540 return s_tester_clean_up(&s_tester);
541 }