Debian Codegraph @

crt/aws-c-http/tests/test_tls.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/request_response.h>
8
9 #include <aws/common/clock.h>
10 #include <aws/common/condition_variable.h>
11 #include <aws/common/mutex.h>
12 #include <aws/io/channel_bootstrap.h>
13 #include <aws/io/event_loop.h>
14 #include <aws/io/host_resolver.h>
15 #include <aws/io/logging.h>
16 #include <aws/io/socket.h>
17 #include <aws/io/tls_channel_handler.h>
18 #include <aws/io/uri.h>
19 #include <aws/testing/aws_test_harness.h>
20
21 /* Singleton used by tests in this file */
22 struct test_ctx {
23 struct aws_allocator *alloc;
24 struct aws_event_loop_group *event_loop_group;
25 struct aws_host_resolver *host_resolver;
26 struct aws_tls_ctx *tls_ctx;
27 struct aws_client_bootstrap *client_bootstrap;
28 struct aws_http_connection *client_connection;
29 struct aws_http_stream *stream;
30
31 size_t body_size;
32 bool stream_complete;
33 bool client_connection_is_shutdown;
34
35 struct aws_mutex wait_lock;
36 struct aws_condition_variable wait_cvar;
37 int wait_result;
38 };
39
40 static const uint32_t TEST_TIMEOUT_SEC = 4;
41
42 static void s_on_connection_setup(struct aws_http_connection *connection, int error_code, void *user_data) {
43 struct test_ctx *test = user_data;
44 AWS_FATAL_ASSERT(aws_mutex_lock(&test->wait_lock) == AWS_OP_SUCCESS);
45
46 test->client_connection = connection;
47 test->wait_result = error_code;
48
49 AWS_FATAL_ASSERT(aws_mutex_unlock(&test->wait_lock) == AWS_OP_SUCCESS);
50 aws_condition_variable_notify_one(&test->wait_cvar);
51 }
52
53 static void s_on_connection_shutdown(struct aws_http_connection *connection, int error_code, void *user_data) {
54 (void)connection;
55 struct test_ctx *test = user_data;
56 AWS_FATAL_ASSERT(aws_mutex_lock(&test->wait_lock) == AWS_OP_SUCCESS);
57
58 test->client_connection_is_shutdown = true;
59 test->wait_result = error_code;
60
61 aws_condition_variable_notify_one(&test->wait_cvar);
62 AWS_FATAL_ASSERT(aws_mutex_unlock(&test->wait_lock) == AWS_OP_SUCCESS);
63 }
64
65 static int s_test_wait(struct test_ctx *test, bool (*pred)(void *user_data)) {
66 ASSERT_SUCCESS(aws_mutex_lock(&test->wait_lock));
67 int wait_result = aws_condition_variable_wait_pred(&test->wait_cvar, &test->wait_lock, pred, test);
68 ASSERT_SUCCESS(aws_mutex_unlock(&test->wait_lock));
69 ASSERT_SUCCESS(wait_result);
70 return AWS_OP_SUCCESS;
71 }
72
73 static bool s_test_connection_setup_pred(void *user_data) {
74 struct test_ctx *test = user_data;
75 return test->wait_result || test->client_connection;
76 }
77
78 static bool s_test_connection_shutdown_pred(void *user_data) {
79 struct test_ctx *test = user_data;
80 return test->wait_result || test->client_connection_is_shutdown;
81 }
82
83 static int s_on_stream_body(struct aws_http_stream *stream, const struct aws_byte_cursor *data, void *user_data) {
84 (void)stream;
85 struct test_ctx *test = user_data;
86
87 AWS_FATAL_ASSERT(aws_mutex_lock(&test->wait_lock) == AWS_OP_SUCCESS);
88 test->body_size += data->len;
89 AWS_FATAL_ASSERT(aws_mutex_unlock(&test->wait_lock) == AWS_OP_SUCCESS);
90
91 return AWS_OP_SUCCESS;
92 }
93
94 static void s_on_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
95 (void)stream;
96 struct test_ctx *test = user_data;
97
98 AWS_FATAL_ASSERT(aws_mutex_lock(&test->wait_lock) == AWS_OP_SUCCESS);
99 test->wait_result = error_code;
100 test->stream_complete = true;
101 AWS_FATAL_ASSERT(aws_mutex_unlock(&test->wait_lock) == AWS_OP_SUCCESS);
102
103 aws_condition_variable_notify_one(&test->wait_cvar);
104 }
105
106 static bool s_stream_wait_pred(void *user_data) {
107 struct test_ctx *test = user_data;
108 return test->wait_result || test->stream_complete;
109 }
110
111 static int s_test_ctx_init(struct aws_allocator *allocator, struct test_ctx *test, bool h2_required) {
112
113 AWS_ZERO_STRUCT(*test);
114 test->alloc = allocator;
115
116 aws_mutex_init(&test->wait_lock);
117 aws_condition_variable_init(&test->wait_cvar);
118
119 test->event_loop_group = aws_event_loop_group_new_default(test->alloc, 1, NULL);
120
121 struct aws_host_resolver_default_options resolver_options = {
122 .el_group = test->event_loop_group,
123 .max_entries = 1,
124 };
125
126 test->host_resolver = aws_host_resolver_new_default(test->alloc, &resolver_options);
127
128 struct aws_client_bootstrap_options bootstrap_options = {
129 .event_loop_group = test->event_loop_group,
130 .host_resolver = test->host_resolver,
131 };
132 ASSERT_NOT_NULL(test->client_bootstrap = aws_client_bootstrap_new(test->alloc, &bootstrap_options));
133 struct aws_tls_ctx_options tls_ctx_options;
134 aws_tls_ctx_options_init_default_client(&tls_ctx_options, allocator);
135 char *apln = h2_required ? "h2" : "http/1.1";
136 aws_tls_ctx_options_set_alpn_list(&tls_ctx_options, apln);
137 ASSERT_NOT_NULL(test->tls_ctx = aws_tls_client_ctx_new(allocator, &tls_ctx_options));
138
139 aws_tls_ctx_options_clean_up(&tls_ctx_options);
140 return AWS_OP_SUCCESS;
141 }
142
143 static void s_test_ctx_clean_up(struct test_ctx *test) {
144 aws_client_bootstrap_release(test->client_bootstrap);
145 aws_host_resolver_release(test->host_resolver);
146 aws_event_loop_group_release(test->event_loop_group);
147 aws_tls_ctx_release(test->tls_ctx);
148
149 aws_mutex_clean_up(&test->wait_lock);
150 aws_condition_variable_clean_up(&test->wait_cvar);
151 }
152
153 static int s_test_tls_download_medium_file_general(
154 struct aws_allocator *allocator,
155 struct aws_byte_cursor url,
156 bool h2_required) {
157
158 aws_http_library_init(allocator);
159 struct aws_uri uri;
160 aws_uri_init_parse(&uri, allocator, &url);
161
162 struct aws_socket_options socket_options = {
163 .type = AWS_SOCKET_STREAM,
164 .domain = AWS_SOCKET_IPV4,
165 .connect_timeout_ms =
166 (uint32_t)aws_timestamp_convert(TEST_TIMEOUT_SEC, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL),
167 };
168
169 struct test_ctx test;
170 ASSERT_SUCCESS(s_test_ctx_init(allocator, &test, h2_required));
171
172 struct aws_http2_setting settings_array[] = {
173 {.id = AWS_HTTP2_SETTINGS_ENABLE_PUSH, .value = 0},
174 };
175
176 struct aws_http2_connection_options http2_options = {
177 .initial_settings_array = settings_array,
178 .num_initial_settings = AWS_ARRAY_SIZE(settings_array),
179 };
180
181 struct aws_tls_connection_options tls_connection_options;
182 aws_tls_connection_options_init_from_ctx(&tls_connection_options, test.tls_ctx);
183 aws_tls_connection_options_set_server_name(
184 &tls_connection_options, allocator, (struct aws_byte_cursor *)aws_uri_host_name(&uri));
185 struct aws_http_client_connection_options http_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT;
186 http_options.allocator = test.alloc;
187 http_options.bootstrap = test.client_bootstrap;
188 http_options.host_name = *aws_uri_host_name(&uri);
189 http_options.port = 443;
190 http_options.on_setup = s_on_connection_setup;
191 http_options.on_shutdown = s_on_connection_shutdown;
192 http_options.socket_options = &socket_options;
193 http_options.tls_options = &tls_connection_options;
194 http_options.http2_options = &http2_options;
195 http_options.user_data = &test;
196
197 ASSERT_SUCCESS(aws_http_client_connect(&http_options));
198 ASSERT_SUCCESS(s_test_wait(&test, s_test_connection_setup_pred));
199 ASSERT_INT_EQUALS(0, test.wait_result);
200 ASSERT_NOT_NULL(test.client_connection);
201 if (h2_required) {
202 ASSERT_INT_EQUALS(aws_http_connection_get_version(test.client_connection), AWS_HTTP_VERSION_2);
203 } else {
204 ASSERT_INT_EQUALS(aws_http_connection_get_version(test.client_connection), AWS_HTTP_VERSION_1_1);
205 }
206
207 struct aws_http_message *request = aws_http_message_new_request(allocator);
208 ASSERT_NOT_NULL(request);
209 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_http_method_get));
210 ASSERT_SUCCESS(aws_http_message_set_request_path(request, *aws_uri_path_and_query(&uri)));
211
212 struct aws_http_header header_host = {
213 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
214 .value = *aws_uri_host_name(&uri),
215 };
216 ASSERT_SUCCESS(aws_http_message_add_header(request, header_host));
217
218 struct aws_http_make_request_options req_options = {
219 .self_size = sizeof(req_options),
220 .request = request,
221 .on_response_body = s_on_stream_body,
222 .on_complete = s_on_stream_complete,
223 .user_data = &test,
224 };
225
226 ASSERT_NOT_NULL(test.stream = aws_http_connection_make_request(test.client_connection, &req_options));
227 aws_http_stream_activate(test.stream);
228
229 /* wait for the request to complete */
230 s_test_wait(&test, s_stream_wait_pred);
231
232 ASSERT_INT_EQUALS(14428801, test.body_size);
233
234 aws_http_message_destroy(request);
235 aws_http_stream_release(test.stream);
236 test.stream = NULL;
237
238 aws_http_connection_release(test.client_connection);
239 ASSERT_SUCCESS(s_test_wait(&test, s_test_connection_shutdown_pred));
240 aws_tls_connection_options_clean_up(&tls_connection_options);
241 s_test_ctx_clean_up(&test);
242 aws_uri_clean_up(&uri);
243 aws_http_library_clean_up();
244
245 return AWS_OP_SUCCESS;
246 }
247
248 static int s_test_tls_download_medium_file_h1(struct aws_allocator *allocator, void *ctx) {
249 (void)ctx;
250 struct aws_byte_cursor url =
251 aws_byte_cursor_from_c_str("https://aws-crt-test-stuff.s3.amazonaws.com/http_test_doc.txt");
252 ASSERT_SUCCESS(s_test_tls_download_medium_file_general(allocator, url, false /*h2_required*/));
253 return AWS_OP_SUCCESS;
254 }
255 AWS_TEST_CASE(tls_download_medium_file_h1, s_test_tls_download_medium_file_h1);
256
257 static int s_tls_download_medium_file_h2(struct aws_allocator *allocator, void *ctx) {
258 (void)ctx;
259 /* The cloudfront domain for aws-crt-test-stuff */
260 struct aws_byte_cursor url = aws_byte_cursor_from_c_str("https://d1cz66xoahf9cl.cloudfront.net/http_test_doc.txt");
261 ASSERT_SUCCESS(s_test_tls_download_medium_file_general(allocator, url, true /*h2_required*/));
262 return AWS_OP_SUCCESS;
263 }
264 AWS_TEST_CASE(tls_download_medium_file_h2, s_tls_download_medium_file_h2);
265
266 static int s_test_tls_download_shutdown_with_window_size_0(struct aws_allocator *allocator, void *ctx) {
267 (void)ctx;
268
269 aws_http_library_init(allocator);
270 struct aws_byte_cursor uri_str =
271 aws_byte_cursor_from_c_str("https://aws-crt-test-stuff.s3.amazonaws.com/http_test_doc.txt");
272 struct aws_uri uri;
273 AWS_ZERO_STRUCT(uri);
274 aws_uri_init_parse(&uri, allocator, &uri_str);
275
276 size_t window_size = 20 * 1024;
277
278 struct aws_socket_options socket_options = {
279 .type = AWS_SOCKET_STREAM,
280 .domain = AWS_SOCKET_IPV4,
281 .connect_timeout_ms =
282 (uint32_t)aws_timestamp_convert(TEST_TIMEOUT_SEC, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL),
283 };
284
285 struct test_ctx test;
286 ASSERT_SUCCESS(s_test_ctx_init(allocator, &test, false));
287 struct aws_tls_connection_options tls_connection_options;
288 aws_tls_connection_options_init_from_ctx(&tls_connection_options, test.tls_ctx);
289 aws_tls_connection_options_set_server_name(
290 &tls_connection_options, allocator, (struct aws_byte_cursor *)aws_uri_host_name(&uri));
291 struct aws_http_client_connection_options http_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT;
292 http_options.allocator = test.alloc;
293 http_options.bootstrap = test.client_bootstrap;
294 http_options.host_name = *aws_uri_host_name(&uri);
295 http_options.port = 443;
296 http_options.on_setup = s_on_connection_setup;
297 http_options.on_shutdown = s_on_connection_shutdown;
298 http_options.socket_options = &socket_options;
299 http_options.tls_options = &tls_connection_options;
300 http_options.user_data = &test;
301 http_options.manual_window_management = true;
302 http_options.initial_window_size = window_size;
303
304 ASSERT_SUCCESS(aws_http_client_connect(&http_options));
305 ASSERT_SUCCESS(s_test_wait(&test, s_test_connection_setup_pred));
306 ASSERT_INT_EQUALS(0, test.wait_result);
307 ASSERT_NOT_NULL(test.client_connection);
308 ASSERT_INT_EQUALS(aws_http_connection_get_version(test.client_connection), AWS_HTTP_VERSION_1_1);
309
310 struct aws_http_message *request = aws_http_message_new_request(allocator);
311 ASSERT_NOT_NULL(request);
312 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_http_method_get));
313 ASSERT_SUCCESS(aws_http_message_set_request_path(request, *aws_uri_path_and_query(&uri)));
314
315 struct aws_http_header header_host = {
316 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
317 .value = *aws_uri_host_name(&uri),
318 };
319 ASSERT_SUCCESS(aws_http_message_add_header(request, header_host));
320
321 struct aws_http_make_request_options req_options = {
322 .self_size = sizeof(req_options),
323 .request = request,
324 .on_response_body = s_on_stream_body,
325 .on_complete = s_on_stream_complete,
326 .user_data = &test,
327 };
328
329 ASSERT_NOT_NULL(test.stream = aws_http_connection_make_request(test.client_connection, &req_options));
330 aws_http_stream_activate(test.stream);
331
332 /* wait for the request to hit the window limitation. */
333 aws_thread_current_sleep(aws_timestamp_convert(2, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
334 aws_http_connection_close(test.client_connection);
335
336 aws_http_stream_release(test.stream);
337 test.stream = NULL;
338
339 aws_http_connection_release(test.client_connection);
340 ASSERT_SUCCESS(s_test_wait(&test, s_stream_wait_pred));
341 /* Reset the wait error. */
342 test.wait_result = 0;
343 ASSERT_SUCCESS(s_test_wait(&test, s_test_connection_shutdown_pred));
344 ASSERT_INT_EQUALS(window_size, test.body_size);
345
346 aws_http_message_destroy(request);
347 aws_tls_connection_options_clean_up(&tls_connection_options);
348 s_test_ctx_clean_up(&test);
349 aws_uri_clean_up(&uri);
350 aws_http_library_clean_up();
351 return AWS_OP_SUCCESS;
352 }
353
354 AWS_TEST_CASE(tls_download_shutdown_with_window_size_0, s_test_tls_download_shutdown_with_window_size_0);