Debian Codegraph @

crt/aws-c-http/tests/test_h2_client.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 "h2_test_helper.h"
7 #include "stream_test_helper.h"
8 #include <aws/http/private/h2_connection.h>
9 #include <aws/http/private/h2_stream.h>
10 #include <aws/http/private/request_response_impl.h>
11 #include <aws/http/request_response.h>
12 #include <aws/io/stream.h>
13 #include <aws/testing/io_testing_channel.h>
14
15 #define TEST_CASE(NAME) \
16 AWS_TEST_CASE(NAME, s_test_##NAME); \
17 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
18
19 #define DEFINE_HEADER(NAME, VALUE) \
20 { \
21 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \
22 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \
23 }
24
25 struct connection_user_data {
26 struct aws_allocator *allocator;
27 int initial_settings_error_code;
28 uint32_t last_stream_id;
29 uint32_t http2_error;
30 struct aws_http2_setting remote_settings_array[10];
31 struct aws_byte_buf debug_data;
32 size_t num_settings;
33 };
34
35 static void s_on_initial_settings_completed(
36 struct aws_http_connection *http2_connection,
37 int error_code,
38 void *user_data) {
39
40 (void)http2_connection;
41 struct connection_user_data *data = user_data;
42 data->initial_settings_error_code = error_code;
43 }
44
45 static void s_on_goaway_received(
46 struct aws_http_connection *http2_connection,
47 uint32_t last_stream_id,
48 uint32_t http2_error,
49 const struct aws_byte_cursor debug_data,
50 void *user_data) {
51
52 (void)http2_connection;
53 struct connection_user_data *data = user_data;
54 data->last_stream_id = last_stream_id;
55 data->http2_error = http2_error;
56 if (data->debug_data.capacity != 0) {
57 /* If multiple goaway received, clean up the previous one */
58 aws_byte_buf_clean_up(&data->debug_data);
59 }
60 aws_byte_buf_init_copy_from_cursor(&data->debug_data, data->allocator, debug_data);
61 }
62
63 static void s_on_remote_settings_change(
64 struct aws_http_connection *http2_connection,
65 const struct aws_http2_setting *settings_array,
66 size_t num_settings,
67 void *user_data) {
68
69 (void)http2_connection;
70 struct connection_user_data *data = user_data;
71 if (num_settings) {
72 memcpy(data->remote_settings_array, settings_array, num_settings * sizeof(struct aws_http2_setting));
73 }
74 data->num_settings = num_settings;
75 }
76
77 /* Singleton used by tests in this file */
78 static struct tester {
79 struct aws_allocator *alloc;
80 struct aws_http_connection *connection;
81 struct testing_channel testing_channel;
82 struct h2_fake_peer peer;
83 struct connection_user_data user_data;
84
85 bool no_conn_manual_win_management;
86 } s_tester;
87
88 static int s_tester_init(struct aws_allocator *alloc, void *ctx) {
89 (void)ctx;
90 aws_http_library_init(alloc);
91
92 s_tester.alloc = alloc;
93 AWS_ZERO_STRUCT(s_tester.user_data.debug_data);
94
95 struct aws_testing_channel_options options = {.clock_fn = aws_high_res_clock_get_ticks};
96
97 ASSERT_SUCCESS(testing_channel_init(&s_tester.testing_channel, alloc, &options));
98 struct aws_http2_setting settings_array[] = {
99 {.id = AWS_HTTP2_SETTINGS_ENABLE_PUSH, .value = 0},
100 };
101
102 struct aws_http2_connection_options http2_options = {
103 .initial_settings_array = settings_array,
104 .num_initial_settings = AWS_ARRAY_SIZE(settings_array),
105 .on_initial_settings_completed = s_on_initial_settings_completed,
106 .max_closed_streams = AWS_HTTP2_DEFAULT_MAX_CLOSED_STREAMS,
107 .on_goaway_received = s_on_goaway_received,
108 .on_remote_settings_change = s_on_remote_settings_change,
109 .conn_manual_window_management = !s_tester.no_conn_manual_win_management,
110 /* Disable the batching for window update in tests */
111 .conn_window_size_threshold_to_send_update = UINT32_MAX,
112 .stream_window_size_threshold_to_send_update = UINT32_MAX,
113 };
114
115 s_tester.connection =
116 aws_http_connection_new_http2_client(alloc, false /* manual window management */, 0, &http2_options);
117 ASSERT_NOT_NULL(s_tester.connection);
118
119 {
120 s_tester.user_data.allocator = s_tester.alloc;
121 /* set connection user_data (handled by http-bootstrap in real world) */
122 s_tester.connection->user_data = &s_tester.user_data;
123 /* re-enact marriage vows of http-connection and channel (handled by http-bootstrap in real world) */
124 struct aws_channel_slot *slot = aws_channel_slot_new(s_tester.testing_channel.channel);
125 ASSERT_NOT_NULL(slot);
126 ASSERT_SUCCESS(aws_channel_slot_insert_end(s_tester.testing_channel.channel, slot));
127 ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, &s_tester.connection->channel_handler));
128 s_tester.connection->vtable->on_channel_handler_installed(&s_tester.connection->channel_handler, slot);
129 }
130
131 struct h2_fake_peer_options peer_options = {
132 .alloc = alloc,
133 .testing_channel = &s_tester.testing_channel,
134 .is_server = true,
135 };
136 ASSERT_SUCCESS(h2_fake_peer_init(&s_tester.peer, &peer_options));
137
138 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
139 return AWS_OP_SUCCESS;
140 }
141
142 static int s_tester_clean_up(void) {
143 aws_byte_buf_clean_up(&s_tester.user_data.debug_data);
144 h2_fake_peer_clean_up(&s_tester.peer);
145 aws_http_connection_release(s_tester.connection);
146 ASSERT_SUCCESS(testing_channel_clean_up(&s_tester.testing_channel));
147 aws_http_library_clean_up();
148 return AWS_OP_SUCCESS;
149 }
150
151 /* Test the common setup/teardown used by all tests in this file */
152 TEST_CASE(h2_client_sanity_check) {
153 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
154 return s_tester_clean_up();
155 }
156
157 /* Test that a stream can be created and destroyed. */
158 TEST_CASE(h2_client_stream_create) {
159 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
160
161 /* create request */
162 struct aws_http_message *request = aws_http2_message_new_request(allocator);
163 ASSERT_NOT_NULL(request);
164
165 struct aws_http_header headers[] = {
166 DEFINE_HEADER(":method", "GET"),
167 DEFINE_HEADER(":scheme", "https"),
168 DEFINE_HEADER(":path", "/"),
169 };
170 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
171
172 struct aws_http_make_request_options options = {
173 .self_size = sizeof(options),
174 .request = request,
175 };
176
177 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &options);
178 ASSERT_NOT_NULL(stream);
179 aws_http_stream_activate(stream);
180
181 /* shutdown channel so request can be released */
182 aws_channel_shutdown(s_tester.testing_channel.channel, AWS_ERROR_SUCCESS);
183 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
184 ASSERT_TRUE(testing_channel_is_shutdown_completed(&s_tester.testing_channel));
185
186 /* release request */
187 aws_http_stream_release(stream);
188
189 aws_http_message_release(request);
190
191 return s_tester_clean_up();
192 }
193
194 static void s_stream_cleans_up_on_destroy(void *data) {
195 bool *destroyed = data;
196 *destroyed = true;
197 }
198
199 TEST_CASE(h2_client_stream_release_after_complete) {
200 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
201
202 /* create request */
203 struct aws_http_message *request = aws_http2_message_new_request(allocator);
204 ASSERT_NOT_NULL(request);
205
206 struct aws_http_header headers[] = {
207 DEFINE_HEADER(":method", "GET"),
208 DEFINE_HEADER(":scheme", "https"),
209 DEFINE_HEADER(":path", "/"),
210 };
211 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
212
213 bool destroyed = false;
214 struct aws_http_make_request_options options = {
215 .self_size = sizeof(options),
216 .request = request,
217 .on_destroy = s_stream_cleans_up_on_destroy,
218 .user_data = &destroyed,
219 };
220
221 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &options);
222 ASSERT_NOT_NULL(stream);
223 aws_http_stream_activate(stream);
224
225 /* shutdown channel so request can be released */
226 aws_channel_shutdown(s_tester.testing_channel.channel, AWS_ERROR_SUCCESS);
227 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
228 ASSERT_TRUE(testing_channel_is_shutdown_completed(&s_tester.testing_channel));
229
230 /* release request */
231 ASSERT_FALSE(destroyed);
232 aws_http_stream_release(stream);
233 ASSERT_TRUE(destroyed);
234
235 aws_http_message_release(request);
236
237 return s_tester_clean_up();
238 }
239
240 struct s_callback_invoked {
241 bool destroy_invoked;
242 bool complete_invoked;
243 };
244
245 static void s_unactivated_stream_cleans_up_on_destroy(void *data) {
246 struct s_callback_invoked *callback_data = data;
247 callback_data->destroy_invoked = true;
248 }
249
250 static void s_unactivated_stream_complete(struct aws_http_stream *stream, int error_code, void *data) {
251 (void)stream;
252 (void)error_code;
253 struct s_callback_invoked *callback_data = data;
254 callback_data->complete_invoked = true;
255 }
256
257 TEST_CASE(h2_client_unactivated_stream_cleans_up) {
258 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
259
260 /* create request */
261 struct aws_http_message *request = aws_http2_message_new_request(allocator);
262 ASSERT_NOT_NULL(request);
263
264 struct aws_http_header headers[] = {
265 DEFINE_HEADER(":method", "GET"),
266 DEFINE_HEADER(":scheme", "https"),
267 DEFINE_HEADER(":path", "/"),
268 };
269 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
270 struct s_callback_invoked callback_data = {0};
271 struct aws_http_make_request_options options = {
272 .self_size = sizeof(options),
273 .request = request,
274 .on_destroy = s_unactivated_stream_cleans_up_on_destroy,
275 .on_complete = s_unactivated_stream_complete,
276 .user_data = &callback_data,
277 };
278
279 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &options);
280 ASSERT_NOT_NULL(stream);
281 /* do not activate the stream, that's the test. */
282
283 ASSERT_FALSE(callback_data.destroy_invoked);
284 ASSERT_FALSE(callback_data.complete_invoked);
285 /* shutdown channel so request can be released */
286 aws_channel_shutdown(s_tester.testing_channel.channel, AWS_ERROR_SUCCESS);
287 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
288 ASSERT_TRUE(testing_channel_is_shutdown_completed(&s_tester.testing_channel));
289
290 aws_http_stream_release(stream);
291 ASSERT_TRUE(callback_data.destroy_invoked);
292 ASSERT_FALSE(callback_data.complete_invoked);
293
294 aws_http_message_release(request);
295
296 return s_tester_clean_up();
297 }
298
299 /* Test that client automatically sends the HTTP/2 Connection Preface (magic string, followed by SETTINGS frame) */
300 TEST_CASE(h2_client_connection_preface_sent) {
301 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
302
303 /* Have the fake peer to run its decoder on what the client has written.
304 * The decoder will raise an error if it doesn't receive the "client connection preface string" first. */
305 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
306
307 /* Now check that client sent SETTINGS frame */
308 struct h2_decoded_frame *first_written_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, 0);
309 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_SETTINGS, first_written_frame->type);
310 ASSERT_FALSE(first_written_frame->ack);
311
312 return s_tester_clean_up();
313 }
314
315 static int s_stream_tester_init(struct client_stream_tester *stream_tester, struct aws_http_message *request) {
316 struct client_stream_tester_options options = {
317 .request = request,
318 .connection = s_tester.connection,
319 };
320 return client_stream_tester_init(stream_tester, s_tester.alloc, &options);
321 }
322
323 /* Test that client will automatically send the PING ACK frame back, when the PING frame is received */
324 TEST_CASE(h2_client_auto_ping_ack) {
325 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
326
327 /* Connection preface requires that SETTINGS be sent first (RFC-7540 3.5). */
328 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
329 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
330 uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7};
331
332 struct aws_h2_frame *frame = aws_h2_frame_new_ping(allocator, false /*ack*/, opaque_data);
333 ASSERT_NOT_NULL(frame);
334
335 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, frame));
336 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
337 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
338
339 /* Now check that client sent PING ACK frame, it should be the latest frame received by peer
340 * The last frame should be a ping type with ack on, and identical payload */
341 struct h2_decoded_frame *latest_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
342 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_PING, latest_frame->type);
343 ASSERT_TRUE(latest_frame->ack);
344 ASSERT_BIN_ARRAYS_EQUALS(
345 opaque_data, AWS_HTTP2_PING_DATA_SIZE, latest_frame->ping_opaque_data, AWS_HTTP2_PING_DATA_SIZE);
346
347 return s_tester_clean_up();
348 }
349
350 TEST_CASE(h2_client_auto_ping_ack_higher_priority) {
351 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
352
353 /* get connection preface and acks out of the way */
354 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
355 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
356 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
357 size_t frames_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
358
359 /* send request */
360 struct aws_http_message *request = aws_http2_message_new_request(allocator);
361 ASSERT_NOT_NULL(request);
362
363 struct aws_http_header request_headers_src[] = {
364 DEFINE_HEADER(":method", "POST"),
365 DEFINE_HEADER(":scheme", "https"),
366 DEFINE_HEADER(":path", "/"),
367 };
368 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
369
370 const char *body_src = "hello";
371 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
372 struct aws_input_stream *request_body = aws_input_stream_new_from_cursor(allocator, &body_cursor);
373 aws_http_message_set_body_stream(request, request_body);
374
375 struct client_stream_tester stream_tester;
376 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
377
378 /* Frames for the request are activated. Fake peer send PING frame now */
379 uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7};
380
381 struct aws_h2_frame *frame = aws_h2_frame_new_ping(allocator, false /*ack*/, opaque_data);
382 ASSERT_NOT_NULL(frame);
383
384 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, frame));
385
386 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
387
388 /* validate PING ACK frame has higher priority than the normal request frames, and be received earliest */
389 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
390
391 struct h2_decoded_frame *fastest_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frames_count);
392 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_PING, fastest_frame->type);
393 ASSERT_TRUE(fastest_frame->ack);
394 ASSERT_BIN_ARRAYS_EQUALS(
395 opaque_data, AWS_HTTP2_PING_DATA_SIZE, fastest_frame->ping_opaque_data, AWS_HTTP2_PING_DATA_SIZE);
396
397 /* clean up */
398 aws_http_message_release(request);
399 client_stream_tester_clean_up(&stream_tester);
400 aws_input_stream_release(request_body);
401 return s_tester_clean_up();
402 }
403
404 /* Test client can automatically send SETTINGs ACK */
405 TEST_CASE(h2_client_auto_settings_ack) {
406 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
407
408 /* Connection preface requires that SETTINGS be sent first (RFC-7540 3.5). */
409 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
410 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
411
412 /* Have the fake peer to run its decoder on what the client has written. */
413 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
414
415 /* The Setting ACK frame should be sent back */
416 struct h2_decoded_frame *latest_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
417 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_SETTINGS, latest_frame->type);
418 ASSERT_TRUE(latest_frame->ack);
419
420 return s_tester_clean_up();
421 }
422
423 static int s_compare_headers(const struct aws_http_headers *expected, const struct aws_http_headers *got) {
424
425 ASSERT_UINT_EQUALS(aws_http_headers_count(expected), aws_http_headers_count(got));
426 for (size_t i = 0; i < aws_http_headers_count(expected); ++i) {
427 struct aws_http_header expected_field;
428 aws_http_headers_get_index(expected, i, &expected_field);
429
430 struct aws_http_header got_field;
431 aws_http_headers_get_index(got, i, &got_field);
432
433 ASSERT_TRUE(aws_byte_cursor_eq(&expected_field.name, &got_field.name));
434 ASSERT_TRUE(aws_byte_cursor_eq(&expected_field.value, &got_field.value));
435 ASSERT_INT_EQUALS(expected_field.compression, got_field.compression);
436 }
437
438 return AWS_OP_SUCCESS;
439 }
440
441 /* Test that a simple request/response can be carried to completion.
442 * The request consists of a single HEADERS frame and the response consists of a single HEADERS frame. */
443 TEST_CASE(h2_client_stream_complete) {
444 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
445
446 /* fake peer sends connection preface */
447 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
448 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
449
450 /* send request */
451 struct aws_http_message *request = aws_http2_message_new_request(allocator);
452 ASSERT_NOT_NULL(request);
453
454 struct aws_http_header request_headers_src[] = {
455 DEFINE_HEADER(":method", "GET"),
456 DEFINE_HEADER(":scheme", "https"),
457 DEFINE_HEADER(":path", "/"),
458 };
459 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
460
461 struct client_stream_tester stream_tester;
462 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
463
464 /* validate sent request, */
465 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
466 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
467
468 struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
469 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
470 ASSERT_TRUE(sent_headers_frame->end_stream);
471 ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(request), sent_headers_frame->headers));
472
473 /* fake peer sends response */
474 struct aws_http_header response_headers_src[] = {
475 DEFINE_HEADER(":status", "404"),
476 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
477 };
478
479 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
480 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
481
482 struct aws_h2_frame *response_frame = aws_h2_frame_new_headers(
483 allocator, aws_http_stream_get_id(stream_tester.stream), response_headers, true /*end_stream*/, 0, NULL);
484 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
485
486 /* validate that client received complete response */
487 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
488 ASSERT_TRUE(stream_tester.complete);
489 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
490 ASSERT_INT_EQUALS(404, stream_tester.response_status);
491 ASSERT_SUCCESS(s_compare_headers(response_headers, stream_tester.response_headers));
492
493 ASSERT_TRUE(stream_tester.metrics.receive_end_timestamp_ns > 0);
494 ASSERT_TRUE(stream_tester.metrics.receive_start_timestamp_ns > 0);
495 ASSERT_TRUE(stream_tester.metrics.receive_end_timestamp_ns > stream_tester.metrics.receive_start_timestamp_ns);
496 ASSERT_TRUE(
497 stream_tester.metrics.receiving_duration_ns ==
498 stream_tester.metrics.receive_end_timestamp_ns - stream_tester.metrics.receive_start_timestamp_ns);
499 ASSERT_TRUE(stream_tester.metrics.send_start_timestamp_ns > 0);
500 ASSERT_TRUE(stream_tester.metrics.send_end_timestamp_ns > 0);
501 ASSERT_TRUE(stream_tester.metrics.send_end_timestamp_ns > stream_tester.metrics.send_start_timestamp_ns);
502 ASSERT_TRUE(
503 stream_tester.metrics.sending_duration_ns ==
504 stream_tester.metrics.send_end_timestamp_ns - stream_tester.metrics.send_start_timestamp_ns);
505 ASSERT_TRUE(stream_tester.metrics.stream_id == stream_tester.stream->id);
506
507 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
508
509 /* clean up */
510 aws_http_headers_release(response_headers);
511 aws_http_message_release(request);
512 client_stream_tester_clean_up(&stream_tester);
513 return s_tester_clean_up();
514 }
515
516 /* Calling aws_http_connection_close() should cleanly shut down connection */
517 TEST_CASE(h2_client_close) {
518 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
519
520 /* fake peer sends connection preface */
521 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
522 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
523
524 /* send request */
525 struct aws_http_message *request = aws_http2_message_new_request(allocator);
526 ASSERT_NOT_NULL(request);
527
528 struct aws_http_header request_headers_src[] = {
529 DEFINE_HEADER(":method", "GET"),
530 DEFINE_HEADER(":scheme", "https"),
531 DEFINE_HEADER(":path", "/"),
532 };
533 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
534
535 struct client_stream_tester stream_tester;
536 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
537
538 /* close connection */
539 aws_http_connection_close(s_tester.connection);
540
541 /* connection should immediately lose "open" status */
542 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
543
544 /* finish shutting down */
545 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
546 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
547
548 /* validate that pending streams complete with error */
549 ASSERT_TRUE(stream_tester.complete);
550 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, stream_tester.on_complete_error_code);
551
552 /* validate that GOAWAY sent */
553 struct h2_decoded_frame *goaway =
554 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_GOAWAY, 0, NULL);
555 ASSERT_NOT_NULL(goaway);
556 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_NO_ERROR, goaway->error_code);
557 ASSERT_UINT_EQUALS(0, goaway->goaway_last_stream_id);
558
559 /* clean up */
560 aws_http_message_release(request);
561 client_stream_tester_clean_up(&stream_tester);
562 return s_tester_clean_up();
563 }
564
565 /* Test that client automatically sends the HTTP/2 Connection Preface (magic string, followed by initial SETTINGS frame,
566 * which we disabled the push_promise) And it will not be applied until the SETTINGS ack is received. Once SETTINGS ack
567 * received, the initial settings will be applied and callback will be invoked */
568 TEST_CASE(h2_client_connection_init_settings_applied_after_ack_by_peer) {
569 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
570
571 /* send request */
572 struct aws_http_message *request = aws_http2_message_new_request(allocator);
573 ASSERT_NOT_NULL(request);
574
575 struct aws_http_header request_headers_src[] = {
576 DEFINE_HEADER(":method", "GET"),
577 DEFINE_HEADER(":scheme", "https"),
578 DEFINE_HEADER(":path", "/"),
579 };
580 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
581
582 struct client_stream_tester stream_tester;
583 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
584
585 /* validate sent request, */
586 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
587 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
588
589 struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
590 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
591 ASSERT_TRUE(sent_headers_frame->end_stream);
592 ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(request), sent_headers_frame->headers));
593
594 /* fake peer sends connection preface */
595 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
596 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
597 /* fake peer sends push_promise */
598 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
599
600 /* fake peer sends push request (PUSH_PROMISE) */
601 struct aws_http_header push_request_headers_src[] = {
602 DEFINE_HEADER(":method", "GET"),
603 DEFINE_HEADER(":scheme", "https"),
604 DEFINE_HEADER(":authority", "veryblackpage.com"),
605 DEFINE_HEADER(":path", "/style.css"),
606 };
607 struct aws_http_headers *push_request_headers = aws_http_headers_new(allocator);
608 ASSERT_SUCCESS(aws_http_headers_add_array(
609 push_request_headers, push_request_headers_src, AWS_ARRAY_SIZE(push_request_headers_src)));
610
611 uint32_t promised_stream_id = 2;
612 struct aws_h2_frame *peer_frame =
613 aws_h2_frame_new_push_promise(allocator, stream_id, promised_stream_id, push_request_headers, 0);
614 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
615 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
616 /* validate the connection is still open */
617 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
618
619 /* set initial_settings_error_code as AWS_ERROR_UNKNOWN to make sure callback invoked later */
620 s_tester.user_data.initial_settings_error_code = AWS_ERROR_UNKNOWN;
621 /* fake peer sends setting ack */
622 struct aws_h2_frame *settings_ack_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
623 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, settings_ack_frame));
624 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
625 /* validate the callback invoked */
626 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, s_tester.user_data.initial_settings_error_code);
627 /* fake peer sends another push_promise again, after setting applied, connection will be closed */
628 peer_frame = aws_h2_frame_new_push_promise(allocator, stream_id, promised_stream_id + 2, push_request_headers, 0);
629 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
630 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
631
632 /* validate the connection completed with error */
633 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
634 ASSERT_INT_EQUALS(
635 AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
636 /* clean up */
637 aws_http_headers_release(push_request_headers);
638 aws_http_message_release(request);
639 client_stream_tester_clean_up(&stream_tester);
640 return s_tester_clean_up();
641 }
642
643 /* Test that h2 stream can take a h1 request massega and transfrom it to h2 style to send it. */
644 TEST_CASE(h2_client_stream_with_h1_request_message) {
645 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
646
647 /* fake peer sends connection preface */
648 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
649 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
650
651 /* send an h1 request */
652 struct aws_http_message *request = aws_http_message_new_request(allocator);
653 ASSERT_NOT_NULL(request);
654 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_method(request, aws_http_method_post));
655 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
656 struct aws_http_header request_headers_src[] = {
657 DEFINE_HEADER("Accept", "*/*"),
658 DEFINE_HEADER("Host", "example.com"),
659 DEFINE_HEADER("Content-Length", "5"),
660 DEFINE_HEADER("Upgrade", "HTTP/2.0"), /* Connection-specific header should be skiped */
661 };
662 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
663 /* body */
664 const char *body_src = "hello";
665 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
666 struct aws_input_stream *request_body = aws_input_stream_new_from_cursor(allocator, &body_cursor);
667 aws_http_message_set_body_stream(request, request_body);
668
669 struct client_stream_tester stream_tester;
670 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
671 /* validate sent request (client should have sent SETTINGS, SETTINGS ACK, HEADERS, DATA (END_STREAM) */
672 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
673 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
674 ASSERT_UINT_EQUALS(4, h2_decode_tester_frame_count(&s_tester.peer.decode));
675
676 /* set expected h2 style headers */
677 struct aws_http_header expected_headers_src[] = {
678 DEFINE_HEADER(":method", "POST"),
679 DEFINE_HEADER(":scheme", "https"),
680 DEFINE_HEADER(":authority", "example.com"),
681 DEFINE_HEADER(":path", "/"),
682 DEFINE_HEADER("accept", "*/*"),
683 DEFINE_HEADER("content-length", "5"),
684 };
685 struct aws_http_headers *expected_headers = aws_http_headers_new(allocator);
686 ASSERT_SUCCESS(
687 aws_http_headers_add_array(expected_headers, expected_headers_src, AWS_ARRAY_SIZE(expected_headers_src)));
688
689 struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, 2);
690 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
691 ASSERT_SUCCESS(s_compare_headers(expected_headers, sent_headers_frame->headers));
692
693 struct h2_decoded_frame *sent_data_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, 3);
694 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_DATA, sent_data_frame->type);
695 ASSERT_TRUE(sent_data_frame->end_stream);
696 ASSERT_TRUE(aws_byte_buf_eq_c_str(&sent_data_frame->data, body_src));
697
698 /* clean up */
699 aws_http_headers_release(expected_headers);
700 aws_http_message_release(request);
701 client_stream_tester_clean_up(&stream_tester);
702 aws_input_stream_destroy(request_body);
703 return s_tester_clean_up();
704 }
705
706 /* Test that h2 stream can split the cookies header correctly */
707 TEST_CASE(h2_client_stream_with_cookies_headers) {
708 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
709
710 /* fake peer sends connection preface */
711 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
712 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
713
714 /* send an h1 request */
715 struct aws_http_message *request = aws_http_message_new_request(allocator);
716 ASSERT_NOT_NULL(request);
717 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_method(request, aws_http_method_get));
718 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
719 struct aws_http_header request_headers_src[] = {
720 DEFINE_HEADER("Accept", "*/*"),
721 DEFINE_HEADER("Host", "example.com"),
722 DEFINE_HEADER("cookie", "a=b; c=d; e=f"),
723 };
724 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
725 struct client_stream_tester stream_tester;
726 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
727
728 /* set expected h2 style headers */
729 struct aws_http_header expected_headers_src[] = {
730 DEFINE_HEADER(":method", "GET"),
731 DEFINE_HEADER(":scheme", "https"),
732 DEFINE_HEADER(":authority", "example.com"),
733 DEFINE_HEADER(":path", "/"),
734 DEFINE_HEADER("accept", "*/*"),
735 DEFINE_HEADER("cookie", "a=b; c=d; e=f"),
736 };
737 struct aws_http_headers *expected_headers = aws_http_headers_new(allocator);
738 ASSERT_SUCCESS(
739 aws_http_headers_add_array(expected_headers, expected_headers_src, AWS_ARRAY_SIZE(expected_headers_src)));
740 /* validate sent request, */
741 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
742 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
743
744 struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
745 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
746 ASSERT_TRUE(sent_headers_frame->end_stream);
747 ASSERT_SUCCESS(s_compare_headers(expected_headers, sent_headers_frame->headers));
748
749 /* clean up */
750 aws_http_headers_release(expected_headers);
751 aws_http_message_release(request);
752 client_stream_tester_clean_up(&stream_tester);
753 return s_tester_clean_up();
754 }
755
756 /* Receiving malformed headers should result in a "Stream Error", not a "Connection Error". */
757 TEST_CASE(h2_client_stream_err_malformed_header) {
758 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
759
760 /* fake peer sends connection preface */
761 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
762 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
763
764 /* send request */
765 struct aws_http_message *request = aws_http2_message_new_request(allocator);
766 ASSERT_NOT_NULL(request);
767
768 struct aws_http_header request_headers_src[] = {
769 DEFINE_HEADER(":method", "GET"),
770 DEFINE_HEADER(":scheme", "https"),
771 DEFINE_HEADER(":path", "/"),
772 };
773 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
774
775 struct client_stream_tester stream_tester;
776 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
777 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
778
779 /* fake peer sends response with malformed header */
780 struct aws_http_header response_headers_src[] = {
781 DEFINE_HEADER(":STATUS", "404"), /* uppercase name forbidden in h2 */
782 };
783
784 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
785 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
786
787 struct aws_h2_frame *response_frame = aws_h2_frame_new_headers(
788 allocator, aws_http_stream_get_id(stream_tester.stream), response_headers, true /*end_stream*/, 0, NULL);
789 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
790
791 /* validate that stream completed with error */
792 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
793 ASSERT_TRUE(stream_tester.complete);
794 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
795
796 /* a stream error should not affect the connection */
797 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
798
799 /* validate that stream sent RST_STREAM */
800 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
801 struct h2_decoded_frame *rst_stream_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
802 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_RST_STREAM, rst_stream_frame->type);
803 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, rst_stream_frame->error_code);
804
805 /* clean up */
806 aws_http_headers_release(response_headers);
807 aws_http_message_release(request);
808 client_stream_tester_clean_up(&stream_tester);
809 return s_tester_clean_up();
810 }
811
812 TEST_CASE(h2_client_stream_err_state_forbids_frame) {
813 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
814
815 /* fake peer sends connection preface */
816 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
817 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
818
819 /* send request */
820 struct aws_http_message *request = aws_http2_message_new_request(allocator);
821 ASSERT_NOT_NULL(request);
822
823 struct aws_http_header request_headers_src[] = {
824 DEFINE_HEADER(":method", "PUT"),
825 DEFINE_HEADER(":scheme", "https"),
826 DEFINE_HEADER(":path", "/"),
827 };
828 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
829
830 const char *body_src = "hello";
831 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
832 struct aws_input_stream *request_body = aws_input_stream_new_tester(allocator, body_cursor);
833 /* Prevent END_STREAM from being sent */
834 aws_input_stream_tester_set_max_bytes_per_read(request_body, 0);
835
836 aws_http_message_set_body_stream(request, request_body);
837
838 struct client_stream_tester stream_tester;
839 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
840 /* Execute 1 event-loop tick. Request is sent, but no end_stream received */
841 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
842 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
843
844 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
845
846 struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
847 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
848 ASSERT_FALSE(sent_headers_frame->end_stream);
849 ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(request), sent_headers_frame->headers));
850
851 /* fake peer sends response */
852 struct aws_http_header response_headers_src[] = {
853 DEFINE_HEADER(":status", "404"),
854 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
855 };
856
857 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
858 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
859
860 /* fake peer sends response headers with end_stream set, which cause the stream to be
861 * AWS_H2_STREAM_STATE_HALF_CLOSED_REMOTE */
862 struct aws_h2_frame *response_frame =
863 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
864 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
865
866 /* AWS_H2_STREAM_STATE_HALF_CLOSED_REMOTE will reject body frame */
867 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, body_src, true /*end_stream*/));
868
869 /* validate that stream completed with error */
870 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
871 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
872
873 /* a stream error should not affect the connection */
874 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
875
876 /* validate that stream sent RST_STREAM */
877 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
878 struct h2_decoded_frame *rst_stream_frame =
879 h2_decode_tester_find_stream_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, stream_id, 0, NULL);
880 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_RST_STREAM, rst_stream_frame->type);
881 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_STREAM_CLOSED, rst_stream_frame->error_code);
882
883 /* clean up */
884 aws_http_headers_release(response_headers);
885 aws_http_message_release(request);
886 client_stream_tester_clean_up(&stream_tester);
887 aws_input_stream_release(request_body);
888 return s_tester_clean_up();
889 }
890
891 TEST_CASE(h2_client_conn_err_stream_frames_received_for_idle_stream) {
892 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
893
894 /* fake peer sends connection preface */
895 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
896 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
897
898 /* fake peer sends response to "idle" (aka doesn't exist yet) stream 99 */
899 struct aws_http_header response_headers_src[] = {
900 DEFINE_HEADER(":status", "200"),
901 };
902
903 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
904 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
905
906 struct aws_h2_frame *response_frame =
907 aws_h2_frame_new_headers(allocator, 99 /*stream_id*/, response_headers, true /* end_stream */, 0, NULL);
908 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
909
910 /* validate that connection has closed due to PROTOCOL_ERROR */
911 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
912 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
913 ASSERT_INT_EQUALS(
914 AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
915
916 /* validate that client sent GOAWAY */
917 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
918
919 struct h2_decoded_frame *goaway =
920 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_GOAWAY, 0, NULL);
921 ASSERT_NOT_NULL(goaway);
922 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, goaway->error_code);
923 ASSERT_UINT_EQUALS(0, goaway->goaway_last_stream_id);
924
925 /* clean up */
926 aws_http_headers_release(response_headers);
927 return s_tester_clean_up();
928 }
929
930 /* Peer may have sent certain frames (WINDOW_UPDATE and RST_STREAM) before realizing
931 * that we have closed the stream. These frames should be ignored. */
932 TEST_CASE(h2_client_stream_ignores_some_frames_received_soon_after_closing) {
933 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
934
935 /* fake peer sends connection preface */
936 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
937 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
938
939 /* send request */
940 struct aws_http_message *request = aws_http2_message_new_request(allocator);
941 ASSERT_NOT_NULL(request);
942
943 struct aws_http_header request_headers_src[] = {
944 DEFINE_HEADER(":method", "GET"),
945 DEFINE_HEADER(":scheme", "https"),
946 DEFINE_HEADER(":path", "/"),
947 };
948 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
949
950 struct client_stream_tester stream_tester;
951 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
952
953 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
954 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
955
956 /* fake peer sends complete response */
957 struct aws_http_header response_headers_src[] = {
958 DEFINE_HEADER(":status", "404"),
959 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
960 };
961
962 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
963 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
964
965 struct aws_h2_frame *peer_frame =
966 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
967 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
968
969 /* fake peer sends WINDOW_UPDATE */
970 peer_frame = aws_h2_frame_new_window_update(allocator, stream_id, 99);
971 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
972
973 /* fake peer sends RST_STREAM */
974 peer_frame = aws_h2_frame_new_rst_stream(allocator, stream_id, AWS_HTTP2_ERR_ENHANCE_YOUR_CALM);
975 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
976
977 /* validate that stream completed successfully.
978 * the WINDOW_UPDATE and RST_STREAM should be ignored because
979 * they arrived soon after the client had sent END_STREAM */
980 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
981 ASSERT_TRUE(stream_tester.complete);
982 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
983
984 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
985
986 /* clean up */
987 aws_http_headers_release(response_headers);
988 aws_http_message_release(request);
989 client_stream_tester_clean_up(&stream_tester);
990 return s_tester_clean_up();
991 }
992
993 TEST_CASE(h2_client_stream_receive_info_headers) {
994 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
995
996 /* fake peer sends connection preface */
997 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
998 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
999
1000 /* send request */
1001 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1002 ASSERT_NOT_NULL(request);
1003
1004 struct aws_http_header request_headers_src[] = {
1005 DEFINE_HEADER(":method", "GET"),
1006 DEFINE_HEADER(":scheme", "https"),
1007 DEFINE_HEADER(":path", "/"),
1008 };
1009 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1010
1011 struct client_stream_tester stream_tester;
1012 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1013
1014 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1015 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1016
1017 /* fake peer sends a info-header-block response */
1018 struct aws_http_header info_response_headers_src[] = {
1019 DEFINE_HEADER(":status", "100"),
1020 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:03:49 GMT"),
1021 };
1022 struct aws_http_headers *info_response_headers = aws_http_headers_new(allocator);
1023 aws_http_headers_add_array(
1024 info_response_headers, info_response_headers_src, AWS_ARRAY_SIZE(info_response_headers_src));
1025 struct aws_h2_frame *peer_frame =
1026 aws_h2_frame_new_headers(allocator, stream_id, info_response_headers, false /*end_stream*/, 0, NULL);
1027 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1028
1029 /* check info response */
1030 ASSERT_INT_EQUALS(1, stream_tester.num_info_responses);
1031 ASSERT_SUCCESS(aws_http_message_set_response_status(stream_tester.info_responses[0], 100));
1032 struct aws_http_headers *rev_info_headers = aws_http_message_get_headers(stream_tester.info_responses[0]);
1033 ASSERT_SUCCESS(s_compare_headers(info_response_headers, rev_info_headers));
1034
1035 /* fake peer sends a main-header-block response */
1036 struct aws_http_header response_headers_src[] = {
1037 DEFINE_HEADER(":status", "404"),
1038 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
1039 };
1040 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
1041 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
1042 peer_frame = aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
1043 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1044
1045 /* validate that client received complete response */
1046 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1047 ASSERT_TRUE(stream_tester.complete);
1048 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
1049 ASSERT_INT_EQUALS(404, stream_tester.response_status);
1050 ASSERT_SUCCESS(s_compare_headers(response_headers, stream_tester.response_headers));
1051
1052 /* clean up */
1053 aws_http_headers_release(response_headers);
1054 aws_http_headers_release(info_response_headers);
1055 aws_http_message_release(request);
1056 client_stream_tester_clean_up(&stream_tester);
1057 return s_tester_clean_up();
1058 }
1059
1060 TEST_CASE(h2_client_stream_err_receive_info_headers_after_main) {
1061 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1062
1063 /* fake peer sends connection preface */
1064 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1065 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1066
1067 /* send request */
1068 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1069 ASSERT_NOT_NULL(request);
1070
1071 struct aws_http_header request_headers_src[] = {
1072 DEFINE_HEADER(":method", "GET"),
1073 DEFINE_HEADER(":scheme", "https"),
1074 DEFINE_HEADER(":path", "/"),
1075 };
1076 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1077
1078 struct client_stream_tester stream_tester;
1079 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1080
1081 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1082 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1083
1084 /* fake peer sends a main-header-block response */
1085 struct aws_http_header response_headers_src[] = {
1086 DEFINE_HEADER(":status", "404"),
1087 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
1088 };
1089
1090 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
1091 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
1092 struct aws_h2_frame *peer_frame =
1093 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
1094 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1095
1096 /* fake peer sends a info-header-block response */
1097 struct aws_http_header info_response_headers_src[] = {
1098 DEFINE_HEADER(":status", "100"),
1099 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:03:49 GMT"),
1100 };
1101
1102 struct aws_http_headers *info_response_headers = aws_http_headers_new(allocator);
1103 aws_http_headers_add_array(
1104 info_response_headers, info_response_headers_src, AWS_ARRAY_SIZE(info_response_headers_src));
1105 peer_frame = aws_h2_frame_new_headers(allocator, stream_id, info_response_headers, false /*end_stream*/, 0, NULL);
1106 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1107
1108 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1109 /* validate the stream completed with error */
1110 ASSERT_TRUE(stream_tester.complete);
1111 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
1112 /* validate the connection is not affected */
1113 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
1114
1115 /* validate that stream sent RST_STREAM */
1116 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1117 struct h2_decoded_frame *rst_stream_frame =
1118 h2_decode_tester_find_stream_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, stream_id, 0, NULL);
1119 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_RST_STREAM, rst_stream_frame->type);
1120 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, rst_stream_frame->error_code);
1121
1122 /* clean up */
1123 aws_http_headers_release(response_headers);
1124 aws_http_headers_release(info_response_headers);
1125 aws_http_message_release(request);
1126 client_stream_tester_clean_up(&stream_tester);
1127 return s_tester_clean_up();
1128 }
1129
1130 TEST_CASE(h2_client_stream_receive_trailing_headers) {
1131 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1132
1133 /* fake peer sends connection preface */
1134 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1135 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1136
1137 /* send request */
1138 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1139 ASSERT_NOT_NULL(request);
1140
1141 struct aws_http_header request_headers_src[] = {
1142 DEFINE_HEADER(":method", "GET"),
1143 DEFINE_HEADER(":scheme", "https"),
1144 DEFINE_HEADER(":path", "/"),
1145 };
1146 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1147
1148 struct client_stream_tester stream_tester;
1149 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1150
1151 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1152 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1153
1154 /* fake peer sends a main-header-block response */
1155 struct aws_http_header response_headers_src[] = {
1156 DEFINE_HEADER(":status", "404"),
1157 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
1158 };
1159
1160 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
1161 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
1162
1163 struct aws_h2_frame *peer_frame =
1164 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
1165 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1166
1167 /* fake peer sends a trailing-header-block response */
1168 struct aws_http_header response_trailer_src[] = {
1169 DEFINE_HEADER("user-agent", "test"),
1170 };
1171
1172 struct aws_http_headers *response_trailer = aws_http_headers_new(allocator);
1173 aws_http_headers_add_array(response_trailer, response_trailer_src, AWS_ARRAY_SIZE(response_trailer_src));
1174
1175 peer_frame = aws_h2_frame_new_headers(allocator, stream_id, response_trailer, true /*end_stream*/, 0, NULL);
1176 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1177
1178 /* validate that client received complete response */
1179 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1180 ASSERT_TRUE(stream_tester.complete);
1181 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
1182 ASSERT_INT_EQUALS(404, stream_tester.response_status);
1183 ASSERT_SUCCESS(s_compare_headers(response_headers, stream_tester.response_headers));
1184 ASSERT_SUCCESS(s_compare_headers(response_trailer, stream_tester.response_trailer));
1185
1186 /* clean up */
1187 aws_http_headers_release(response_headers);
1188 aws_http_headers_release(response_trailer);
1189 aws_http_message_release(request);
1190 client_stream_tester_clean_up(&stream_tester);
1191 return s_tester_clean_up();
1192 }
1193
1194 TEST_CASE(h2_client_stream_err_receive_trailing_before_main) {
1195 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1196
1197 /* fake peer sends connection preface */
1198 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1199 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1200
1201 /* send request */
1202 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1203 ASSERT_NOT_NULL(request);
1204
1205 struct aws_http_header request_headers_src[] = {
1206 DEFINE_HEADER(":method", "GET"),
1207 DEFINE_HEADER(":scheme", "https"),
1208 DEFINE_HEADER(":path", "/"),
1209 };
1210 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1211
1212 struct client_stream_tester stream_tester;
1213 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1214
1215 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1216 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1217
1218 /* fake peer sends a trailing-header-block response */
1219 struct aws_http_header response_trailer_src[] = {
1220 DEFINE_HEADER("user-agent", "test"),
1221 };
1222
1223 struct aws_http_headers *response_trailer = aws_http_headers_new(allocator);
1224 aws_http_headers_add_array(response_trailer, response_trailer_src, AWS_ARRAY_SIZE(response_trailer_src));
1225
1226 struct aws_h2_frame *peer_frame =
1227 aws_h2_frame_new_headers(allocator, stream_id, response_trailer, true /*end_stream*/, 0, NULL);
1228 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1229
1230 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1231 /* validate the stream completed with error */
1232 ASSERT_TRUE(stream_tester.complete);
1233 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
1234 /* validate the connection is not affected */
1235 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
1236
1237 /* validate that stream sent RST_STREAM */
1238 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1239 struct h2_decoded_frame *rst_stream_frame =
1240 h2_decode_tester_find_stream_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, stream_id, 0, NULL);
1241 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_RST_STREAM, rst_stream_frame->type);
1242 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, rst_stream_frame->error_code);
1243
1244 /* clean up */
1245 aws_http_headers_release(response_trailer);
1246 aws_http_message_release(request);
1247 client_stream_tester_clean_up(&stream_tester);
1248 return s_tester_clean_up();
1249 }
1250
1251 /* Peer should not send any frames other than WINDOW_UPDATE and RST_STREAM once they send END_STREAM flag, we will treat
1252 * that as connection error (STREAM_CLOSED) */
1253 TEST_CASE(h2_client_conn_err_stream_frames_received_soon_after_closing) {
1254 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1255
1256 /* fake peer sends connection preface */
1257 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1258 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1259
1260 /* send request */
1261 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1262 ASSERT_NOT_NULL(request);
1263
1264 struct aws_http_header request_headers_src[] = {
1265 DEFINE_HEADER(":method", "GET"),
1266 DEFINE_HEADER(":scheme", "https"),
1267 DEFINE_HEADER(":path", "/"),
1268 };
1269 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1270
1271 struct client_stream_tester stream_tester;
1272 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1273
1274 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1275 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1276
1277 /* fake peer sends complete response */
1278 struct aws_http_header response_headers_src[] = {
1279 DEFINE_HEADER(":status", "404"),
1280 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
1281 };
1282
1283 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
1284 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
1285
1286 struct aws_h2_frame *peer_frame =
1287 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
1288 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1289
1290 /* fake peer try to send data frame */
1291 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, "hello", true /*end_stream*/));
1292
1293 /* validate that connection has closed. */
1294 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1295 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
1296 ASSERT_INT_EQUALS(
1297 AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
1298
1299 /* validate that client sent GOAWAY */
1300 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1301
1302 struct h2_decoded_frame *goaway =
1303 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_GOAWAY, 0, NULL);
1304 ASSERT_NOT_NULL(goaway);
1305 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_STREAM_CLOSED, goaway->error_code);
1306 ASSERT_UINT_EQUALS(0, goaway->goaway_last_stream_id);
1307
1308 /* clean up */
1309 aws_http_headers_release(response_headers);
1310 aws_http_message_release(request);
1311 client_stream_tester_clean_up(&stream_tester);
1312 return s_tester_clean_up();
1313 }
1314
1315 TEST_CASE(h2_client_stream_err_stream_frames_received_soon_after_rst_stream_received) {
1316 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1317
1318 /* fake peer sends connection preface */
1319 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1320 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1321
1322 /* send request */
1323 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1324 ASSERT_NOT_NULL(request);
1325
1326 struct aws_http_header request_headers_src[] = {
1327 DEFINE_HEADER(":method", "GET"),
1328 DEFINE_HEADER(":scheme", "https"),
1329 DEFINE_HEADER(":path", "/"),
1330 };
1331 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1332
1333 struct client_stream_tester stream_tester;
1334 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1335
1336 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1337 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1338
1339 /* fake peer sends RST_STREAM */
1340 struct aws_h2_frame *peer_frame =
1341 aws_h2_frame_new_rst_stream(allocator, stream_id, AWS_HTTP2_ERR_ENHANCE_YOUR_CALM);
1342 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1343 struct aws_http_headers *response_headers;
1344 /* fake peer try sending complete response */
1345 struct aws_http_header response_headers_src[] = {
1346 DEFINE_HEADER(":status", "404"),
1347 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
1348 };
1349
1350 response_headers = aws_http_headers_new(allocator);
1351 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
1352
1353 peer_frame = aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
1354 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1355 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1356 /* validate the stream completed with error */
1357 ASSERT_TRUE(stream_tester.complete);
1358 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_RST_STREAM_RECEIVED, stream_tester.on_complete_error_code);
1359 /* We treat this as a stream error. So, validate the connection is still open and a rst stream is sent by
1360 * client. */
1361 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
1362
1363 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1364 struct h2_decoded_frame *rst_stream_frame =
1365 h2_decode_tester_find_stream_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, stream_id, 0, NULL);
1366 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_STREAM_CLOSED, rst_stream_frame->error_code);
1367 /* clean up */
1368 aws_http_headers_release(response_headers);
1369 aws_http_message_release(request);
1370 client_stream_tester_clean_up(&stream_tester);
1371 return s_tester_clean_up();
1372 }
1373
1374 /* Connection error for frames received on a closed stream we have removed from cache, which may because it closed too
1375 * long ago */
1376 TEST_CASE(h2_client_conn_err_stream_frames_received_after_removed_from_cache) {
1377 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1378
1379 /* fake peer sends connection preface */
1380 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1381 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1382
1383 enum { NUM_STREAMS = AWS_HTTP2_DEFAULT_MAX_CLOSED_STREAMS + 2 };
1384 /* send request */
1385 struct aws_http_message *requests[NUM_STREAMS];
1386
1387 struct aws_http_header request_headers_src[] = {
1388 DEFINE_HEADER(":method", "GET"),
1389 DEFINE_HEADER(":scheme", "https"),
1390 DEFINE_HEADER(":path", "/"),
1391 };
1392 struct client_stream_tester stream_tester[NUM_STREAMS];
1393
1394 /* fill out the cache */
1395 for (size_t i = 0; i < NUM_STREAMS; i++) {
1396 requests[i] = aws_http2_message_new_request(allocator);
1397 aws_http_message_add_header_array(requests[i], request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1398 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester[i], requests[i]));
1399 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1400 /* close the streams immediately */
1401 struct aws_h2_frame *peer_frame = aws_h2_frame_new_rst_stream(
1402 allocator, aws_http_stream_get_id(stream_tester[i].stream), AWS_HTTP2_ERR_ENHANCE_YOUR_CALM);
1403 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1404 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1405 }
1406 uint32_t stream_id = aws_http_stream_get_id(stream_tester[0].stream);
1407
1408 struct aws_http_headers *response_headers;
1409
1410 /* fake peer try sending complete response */
1411 struct aws_http_header response_headers_src[] = {
1412 DEFINE_HEADER(":status", "404"),
1413 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
1414 };
1415
1416 response_headers = aws_http_headers_new(allocator);
1417 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
1418
1419 struct aws_h2_frame *peer_frame =
1420 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
1421 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
1422 /* validate the connection completed with error */
1423 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1424 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
1425 ASSERT_INT_EQUALS(
1426 AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
1427
1428 /* client should send GOAWAY */
1429 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1430 struct h2_decoded_frame *goaway =
1431 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_GOAWAY, 0, NULL);
1432 ASSERT_NOT_NULL(goaway);
1433 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, goaway->error_code);
1434
1435 /* clean up */
1436 aws_http_headers_release(response_headers);
1437 for (size_t i = 0; i < NUM_STREAMS; i++) {
1438 aws_http_message_release(requests[i]);
1439 client_stream_tester_clean_up(&stream_tester[i]);
1440 }
1441 return s_tester_clean_up();
1442 }
1443
1444 /* Test receiving a response with DATA frames */
1445 TEST_CASE(h2_client_stream_receive_data) {
1446 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1447
1448 /* fake peer sends connection preface */
1449 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1450 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1451
1452 /* send request */
1453 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1454 ASSERT_NOT_NULL(request);
1455
1456 struct aws_http_header request_headers_src[] = {
1457 DEFINE_HEADER(":method", "GET"),
1458 DEFINE_HEADER(":scheme", "https"),
1459 DEFINE_HEADER(":path", "/"),
1460 };
1461 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1462
1463 struct client_stream_tester stream_tester;
1464 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1465 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1466
1467 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1468
1469 /* fake peer sends response headers */
1470 struct aws_http_header response_headers_src[] = {
1471 DEFINE_HEADER(":status", "200"),
1472 };
1473
1474 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
1475 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
1476
1477 struct aws_h2_frame *response_frame =
1478 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
1479 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
1480
1481 /* fake peer sends response body */
1482 const char *body_src = "hello";
1483 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, body_src, true /*end_stream*/));
1484
1485 /* validate that client received complete response */
1486 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1487 ASSERT_TRUE(stream_tester.complete);
1488 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
1489 ASSERT_INT_EQUALS(200, stream_tester.response_status);
1490 ASSERT_SUCCESS(s_compare_headers(response_headers, stream_tester.response_headers));
1491 ASSERT_TRUE(aws_byte_buf_eq_c_str(&stream_tester.response_body, body_src));
1492
1493 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
1494
1495 /* clean up */
1496 aws_http_headers_release(response_headers);
1497 aws_http_message_release(request);
1498 client_stream_tester_clean_up(&stream_tester);
1499 return s_tester_clean_up();
1500 }
1501
1502 /* A message is malformed if DATA is received before HEADERS */
1503 TEST_CASE(h2_client_stream_err_receive_data_before_headers) {
1504 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1505
1506 /* fake peer sends connection preface */
1507 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1508 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1509
1510 /* send request */
1511 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1512 ASSERT_NOT_NULL(request);
1513
1514 struct aws_http_header request_headers_src[] = {
1515 DEFINE_HEADER(":method", "GET"),
1516 DEFINE_HEADER(":scheme", "https"),
1517 DEFINE_HEADER(":path", "/"),
1518 };
1519 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1520
1521 struct client_stream_tester stream_tester;
1522 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1523 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1524
1525 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1526
1527 /* fake peer sends response body BEFORE any response headers */
1528 const char *body_src = "hello";
1529 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, body_src, true /*end_stream*/));
1530
1531 /* validate that stream completed with error */
1532 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1533 ASSERT_TRUE(stream_tester.complete);
1534 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
1535
1536 /* a stream error should not affect the connection */
1537 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
1538
1539 /* validate that stream sent RST_STREAM */
1540 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1541 struct h2_decoded_frame *rst_stream_frame =
1542 h2_decode_tester_find_stream_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, stream_id, 0, NULL);
1543 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_RST_STREAM, rst_stream_frame->type);
1544 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, rst_stream_frame->error_code);
1545
1546 /* clean up */
1547 aws_http_message_release(request);
1548 client_stream_tester_clean_up(&stream_tester);
1549 return s_tester_clean_up();
1550 }
1551
1552 /* A message is malformed if DATA is received not match the content_length received */
1553 TEST_CASE(h2_client_stream_err_receive_data_not_match_content_length) {
1554 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1555
1556 /* fake peer sends connection preface */
1557 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1558 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1559
1560 /* send request */
1561 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1562 ASSERT_NOT_NULL(request);
1563
1564 struct aws_http_header request_headers_src[] = {
1565 DEFINE_HEADER(":method", "GET"),
1566 DEFINE_HEADER(":scheme", "https"),
1567 DEFINE_HEADER(":path", "/"),
1568 };
1569 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1570
1571 struct client_stream_tester stream_tester;
1572 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1573 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1574
1575 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1576
1577 /* fake peer sends response headers */
1578 struct aws_http_header response_headers_src[] = {
1579 DEFINE_HEADER(":status", "200"),
1580 DEFINE_HEADER("content-length", "200"),
1581 };
1582
1583 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
1584 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
1585
1586 struct aws_h2_frame *response_frame =
1587 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
1588 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
1589
1590 /* fake peer sends response body */
1591 const char *body_src = "hello";
1592 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, body_src, true /*end_stream*/));
1593
1594 /* validate that stream completed with error */
1595 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1596 ASSERT_TRUE(stream_tester.complete);
1597 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
1598
1599 /* a stream error should not affect the connection */
1600 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
1601
1602 /* validate that stream sent RST_STREAM */
1603 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1604 struct h2_decoded_frame *rst_stream_frame =
1605 h2_decode_tester_find_stream_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, stream_id, 0, NULL);
1606 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_RST_STREAM, rst_stream_frame->type);
1607 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, rst_stream_frame->error_code);
1608
1609 /* clean up */
1610 aws_http_headers_release(response_headers);
1611 aws_http_message_release(request);
1612 client_stream_tester_clean_up(&stream_tester);
1613 return s_tester_clean_up();
1614 }
1615
1616 /* Test sending a request with DATA frames */
1617 TEST_CASE(h2_client_stream_send_data) {
1618 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1619
1620 /* send request */
1621 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1622 ASSERT_NOT_NULL(request);
1623
1624 struct aws_http_header request_headers_src[] = {
1625 DEFINE_HEADER(":method", "POST"),
1626 DEFINE_HEADER(":scheme", "https"),
1627 DEFINE_HEADER(":path", "/"),
1628 DEFINE_HEADER("content-length", "5"),
1629 };
1630 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1631
1632 const char *body_src = "hello";
1633 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
1634 struct aws_input_stream *request_body = aws_input_stream_new_from_cursor(allocator, &body_cursor);
1635 aws_http_message_set_body_stream(request, request_body);
1636
1637 struct client_stream_tester stream_tester;
1638 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1639 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1640
1641 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1642
1643 /* validate sent request (client should have sent SETTINGS, HEADERS, DATA (END_STREAM) */
1644 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1645 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1646 ASSERT_UINT_EQUALS(3, h2_decode_tester_frame_count(&s_tester.peer.decode));
1647
1648 struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, 1);
1649 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
1650 ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(request), sent_headers_frame->headers));
1651 ASSERT_FALSE(sent_headers_frame->end_stream);
1652
1653 struct h2_decoded_frame *sent_data_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, 2);
1654 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_DATA, sent_data_frame->type);
1655 ASSERT_TRUE(sent_data_frame->end_stream);
1656 ASSERT_TRUE(aws_byte_buf_eq_c_str(&sent_data_frame->data, body_src));
1657
1658 /* fake peer sends connection preface */
1659 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1660
1661 /* fake peer sends response headers */
1662 struct aws_http_header response_headers_src[] = {
1663 DEFINE_HEADER(":status", "200"),
1664 };
1665
1666 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
1667 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
1668
1669 struct aws_h2_frame *response_frame =
1670 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
1671 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
1672
1673 /* validate that request completed successfully */
1674 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1675 ASSERT_TRUE(stream_tester.complete);
1676 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
1677 ASSERT_INT_EQUALS(200, stream_tester.response_status);
1678
1679 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
1680
1681 /* clean up */
1682 aws_http_headers_release(response_headers);
1683 aws_http_message_release(request);
1684 client_stream_tester_clean_up(&stream_tester);
1685 aws_input_stream_release(request_body);
1686 return s_tester_clean_up();
1687 }
1688
1689 /* Test sending multiple requests, each with large bodies that must be sent across multiple DATA frames.
1690 * The connection should not let one stream hog the connection, the streams should take turns sending DATA.
1691 * Also, the stream should not send more than one aws_io_message full of frames per event-loop-tick */
1692 TEST_CASE(h2_client_stream_send_lots_of_data) {
1693 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1694
1695 /* bodies must be big enough to span multiple H2-frames and multiple aws_io_messages */
1696 size_t body_size =
1697 aws_max_size(aws_h2_settings_initial[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE], g_aws_channel_max_fragment_size) * 5;
1698
1699 /* get connection preface and acks out of the way */
1700 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1701 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1702 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1703
1704 /* send multiple requests */
1705 enum { NUM_STREAMS = 3 };
1706 struct aws_http_message *requests[NUM_STREAMS];
1707 struct aws_http_header request_headers_src[NUM_STREAMS][3] = {
1708 {
1709 DEFINE_HEADER(":method", "POST"),
1710 DEFINE_HEADER(":scheme", "https"),
1711 DEFINE_HEADER(":path", "/a.txt"),
1712 },
1713 {
1714 DEFINE_HEADER(":method", "POST"),
1715 DEFINE_HEADER(":scheme", "https"),
1716 DEFINE_HEADER(":path", "/b.txt"),
1717 },
1718 {
1719 DEFINE_HEADER(":method", "POST"),
1720 DEFINE_HEADER(":scheme", "https"),
1721 DEFINE_HEADER(":path", "/c.txt"),
1722 },
1723 };
1724
1725 struct aws_byte_buf request_body_bufs[NUM_STREAMS];
1726 struct aws_input_stream *request_bodies[NUM_STREAMS];
1727 struct client_stream_tester stream_testers[NUM_STREAMS];
1728 for (size_t i = 0; i < NUM_STREAMS; ++i) {
1729 requests[i] = aws_http2_message_new_request(allocator);
1730 aws_http_message_add_header_array(requests[i], request_headers_src[i], AWS_ARRAY_SIZE(request_headers_src[i]));
1731
1732 /* fill first body with "aaaa...", second with "bbbb...", etc */
1733 ASSERT_SUCCESS(aws_byte_buf_init(&request_body_bufs[i], allocator, body_size));
1734 ASSERT_TRUE(aws_byte_buf_write_u8_n(&request_body_bufs[i], (uint8_t)('a' + i), body_size));
1735 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&request_body_bufs[i]);
1736
1737 request_bodies[i] = aws_input_stream_new_from_cursor(allocator, &body_cursor);
1738 ASSERT_NOT_NULL(request_bodies[i]);
1739
1740 aws_http_message_set_body_stream(requests[i], request_bodies[i]);
1741
1742 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[i], requests[i]));
1743 }
1744
1745 /* now loop until all requests are done sending.
1746 * 1 aws_io_message should be written with each tick of the event-loop.
1747 * determine when (based on event-loop tick count) each request sent its END_STREAM. */
1748 struct aws_linked_list *written_msg_queue = testing_channel_get_written_message_queue(&s_tester.testing_channel);
1749 size_t tick_i = 0;
1750 size_t end_stream_count = 0;
1751 size_t end_stream_tick[NUM_STREAMS];
1752 while (end_stream_count < NUM_STREAMS) {
1753
1754 /* check that connection sends exactly 1 aws_io_message per event-loop tick */
1755 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
1756
1757 size_t written_msg_queue_len = 0;
1758 for (struct aws_linked_list_node *node = aws_linked_list_begin(written_msg_queue);
1759 node != aws_linked_list_end(written_msg_queue);
1760 node = aws_linked_list_next(node)) {
1761 written_msg_queue_len++;
1762 }
1763 ASSERT_UINT_EQUALS(1, written_msg_queue_len);
1764
1765 /* decode all new frames and examine them to see if any request has finished */
1766 const size_t prev_frame_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
1767 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1768 const size_t frame_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
1769
1770 for (size_t i = prev_frame_count; i < frame_count; ++i) {
1771 struct h2_decoded_frame *frame = h2_decode_tester_get_frame(&s_tester.peer.decode, i);
1772 if (frame->type == AWS_H2_FRAME_T_DATA) {
1773 /* Send a Window update frame back */
1774 struct aws_h2_frame *connection_window_update =
1775 aws_h2_frame_new_window_update(allocator, 0, (uint32_t)frame->data.len);
1776 ASSERT_NOT_NULL(connection_window_update);
1777 h2_fake_peer_send_frame(&s_tester.peer, connection_window_update);
1778 struct aws_h2_frame *stream_window_update =
1779 aws_h2_frame_new_window_update(allocator, frame->stream_id, (uint32_t)frame->data.len);
1780 ASSERT_NOT_NULL(stream_window_update);
1781 h2_fake_peer_send_frame(&s_tester.peer, stream_window_update);
1782 }
1783 if (frame->type == AWS_H2_FRAME_T_DATA && frame->end_stream) {
1784
1785 end_stream_tick[end_stream_count++] = tick_i;
1786 }
1787 }
1788
1789 tick_i++;
1790 }
1791
1792 for (size_t i = 1; i < NUM_STREAMS; ++i) {
1793 /* as a simple fairness test, check that each of the requests finished within 1 event-loop tick of the last. */
1794 size_t streams_finished_n_ticks_apart = end_stream_tick[i] - end_stream_tick[i - 1];
1795 ASSERT_TRUE(streams_finished_n_ticks_apart <= 1);
1796
1797 /* validate that all data sent successfully */
1798 ASSERT_SUCCESS(h2_decode_tester_check_data_across_frames(
1799 &s_tester.peer.decode,
1800 aws_http_stream_get_id(stream_testers[i].stream),
1801 aws_byte_cursor_from_buf(&request_body_bufs[i]),
1802 true /*expect_end_frame*/));
1803 }
1804
1805 /* finally, send responses and ensure all streams complete successfully */
1806 struct aws_http_header response_headers_src[] = {DEFINE_HEADER(":status", "200")};
1807 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
1808 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
1809 for (size_t i = 0; i < NUM_STREAMS; ++i) {
1810 struct aws_h2_frame *response_frame = aws_h2_frame_new_headers(
1811 allocator,
1812 aws_http_stream_get_id(stream_testers[i].stream),
1813 response_headers,
1814 true /* end_stream */,
1815 0,
1816 NULL);
1817 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
1818 }
1819
1820 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1821 for (size_t i = 0; i < NUM_STREAMS; ++i) {
1822 ASSERT_TRUE(stream_testers[i].complete);
1823 ASSERT_INT_EQUALS(200, stream_testers[i].response_status);
1824 }
1825
1826 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
1827
1828 /* clean up */
1829 aws_http_headers_release(response_headers);
1830 for (size_t i = 0; i < NUM_STREAMS; ++i) {
1831 client_stream_tester_clean_up(&stream_testers[i]);
1832 aws_http_message_release(requests[i]);
1833 aws_input_stream_release(request_bodies[i]);
1834 aws_byte_buf_clean_up(&request_body_bufs[i]);
1835 }
1836 return s_tester_clean_up();
1837 }
1838
1839 /* Test sending a request whose aws_input_stream is not providing body data all at once */
1840 TEST_CASE(h2_client_stream_send_stalled_data) {
1841 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1842
1843 /* get connection preface and acks out of the way */
1844 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
1845 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1846 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1847
1848 /* get request ready
1849 * the body_stream will stall and provide no data when we try to read from it */
1850 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1851 ASSERT_NOT_NULL(request);
1852
1853 struct aws_http_header request_headers_src[] = {
1854 DEFINE_HEADER(":method", "POST"),
1855 DEFINE_HEADER(":scheme", "https"),
1856 DEFINE_HEADER(":path", "/"),
1857 };
1858 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1859
1860 const char *body_src = "hello";
1861 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
1862 struct aws_input_stream *request_body = aws_input_stream_new_tester(allocator, body_cursor);
1863 aws_input_stream_tester_set_max_bytes_per_read(request_body, 0);
1864
1865 aws_http_message_set_body_stream(request, request_body);
1866
1867 struct client_stream_tester stream_tester;
1868 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1869
1870 /* Execute 1 event-loop tick. Validate that no DATA frames were written */
1871 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
1872 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1873 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1874 ASSERT_NULL(h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_DATA, 0 /*search_start_idx*/, NULL));
1875
1876 /* Execute a few more event-loop ticks. No more frames should be written */
1877 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
1878 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
1879 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
1880 ASSERT_TRUE(aws_linked_list_empty(testing_channel_get_written_message_queue(&s_tester.testing_channel)));
1881
1882 /* Let aws_input_stream produce just 1 byte. This should result in 1 DATA frame with 1 byte of payload */
1883 aws_input_stream_tester_set_max_bytes_per_read(request_body, 1);
1884 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
1885 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1886
1887 size_t data_frame_idx;
1888 struct h2_decoded_frame *data_frame = h2_decode_tester_find_frame(
1889 &s_tester.peer.decode, AWS_H2_FRAME_T_DATA, 0 /*search_start_idx*/, &data_frame_idx);
1890 ASSERT_NOT_NULL(data_frame);
1891 ASSERT_UINT_EQUALS(1, data_frame->data.len);
1892 ASSERT_FALSE(data_frame->end_stream);
1893
1894 ASSERT_NULL(h2_decode_tester_find_frame(
1895 &s_tester.peer.decode, AWS_H2_FRAME_T_DATA, data_frame_idx + 1 /*search_start_idx*/, NULL));
1896
1897 /* finish up. Let aws_input_stream produce the rest of its data */
1898 aws_input_stream_tester_set_max_bytes_per_read(request_body, SIZE_MAX);
1899 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1900 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1901 ASSERT_SUCCESS(
1902 h2_decode_tester_check_data_str_across_frames(&s_tester.peer.decode, stream_id, body_src, true /*end_stream*/));
1903
1904 /* clean up */
1905 aws_http_message_release(request);
1906 client_stream_tester_clean_up(&stream_tester);
1907 aws_input_stream_release(request_body);
1908 return s_tester_clean_up();
1909 }
1910
1911 static int s_fake_peer_window_update_check(
1912 struct aws_allocator *alloc,
1913 uint32_t stream_id,
1914 uint32_t window_size_increment,
1915 const char *expected_data,
1916 size_t expected_data_len,
1917 bool end_stream,
1918 bool skip_check_data) {
1919
1920 struct aws_h2_frame *stream_window_update = aws_h2_frame_new_window_update(alloc, stream_id, window_size_increment);
1921 ASSERT_NOT_NULL(stream_window_update);
1922 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, stream_window_update));
1923 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1924 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1925 if (expected_data) {
1926 /* DATA should be received now as the last frame, check the result */
1927 struct h2_decoded_frame *latest_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
1928 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_DATA, latest_frame->type);
1929 ASSERT_TRUE(latest_frame->end_stream == end_stream);
1930 if (!skip_check_data) {
1931 ASSERT_BIN_ARRAYS_EQUALS(
1932 latest_frame->data.buffer, latest_frame->data.len, expected_data, expected_data_len);
1933 }
1934 } else {
1935 ASSERT_TRUE(aws_linked_list_empty(testing_channel_get_written_message_queue(&s_tester.testing_channel)));
1936 }
1937 return AWS_OP_SUCCESS;
1938 }
1939
1940 /* Test sending DATA frames is blocked by stream window size, and will resume when we receive window update */
1941 TEST_CASE(h2_client_stream_send_data_controlled_by_stream_window_size) {
1942 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
1943
1944 /* get connection preface and acks out of the way */
1945 /* fake peer sends setting with 5 initial window size */
1946 struct aws_http2_setting settings_array[] = {
1947 {.id = AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE, .value = 5},
1948 };
1949 struct aws_h2_frame *settings =
1950 aws_h2_frame_new_settings(allocator, settings_array, AWS_ARRAY_SIZE(settings_array), false /*ack*/);
1951 ASSERT_NOT_NULL(settings);
1952 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface(&s_tester.peer, settings));
1953 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1954 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1955
1956 size_t frames_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
1957
1958 /* send request */
1959 struct aws_http_message *request = aws_http2_message_new_request(allocator);
1960 ASSERT_NOT_NULL(request);
1961
1962 struct aws_http_header request_headers_src[] = {
1963 DEFINE_HEADER(":method", "GET"),
1964 DEFINE_HEADER(":scheme", "https"),
1965 DEFINE_HEADER(":path", "/"),
1966 };
1967 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1968
1969 const char *body_src = "hello CRT!";
1970
1971 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
1972 struct aws_input_stream *request_body = aws_input_stream_new_from_cursor(allocator, &body_cursor);
1973 aws_http_message_set_body_stream(request, request_body);
1974
1975 struct client_stream_tester stream_tester;
1976 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
1977 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
1978
1979 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
1980
1981 /* validate sent request (client should only have sent HEADERS) */
1982 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
1983 frames_count += 1;
1984 ASSERT_UINT_EQUALS(frames_count, h2_decode_tester_frame_count(&s_tester.peer.decode));
1985
1986 struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frames_count - 1);
1987 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
1988 ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(request), sent_headers_frame->headers));
1989 ASSERT_FALSE(sent_headers_frame->end_stream);
1990
1991 /* fake peer sends a WINDOW_UPDATE on stream to unblock the DATA frame. We need to release the min window size */
1992 ASSERT_SUCCESS(s_fake_peer_window_update_check(
1993 allocator, stream_id, 256, "hello CRT!", 10, true /*end_stream*/, false /*skip_check_data*/));
1994
1995 /* fake peer sends response headers */
1996 struct aws_http_header response_headers_src[] = {
1997 DEFINE_HEADER(":status", "200"),
1998 };
1999
2000 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
2001 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
2002
2003 struct aws_h2_frame *response_frame =
2004 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
2005 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
2006
2007 /* validate that request completed successfully */
2008 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2009 ASSERT_TRUE(stream_tester.complete);
2010 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2011 ASSERT_INT_EQUALS(200, stream_tester.response_status);
2012
2013 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
2014
2015 /* clean up */
2016 aws_http_headers_release(response_headers);
2017 aws_http_message_release(request);
2018 client_stream_tester_clean_up(&stream_tester);
2019 aws_input_stream_release(request_body);
2020 return s_tester_clean_up();
2021 }
2022
2023 /* Test stream window size becomes negative, and will resume only when it back to positive again. */
2024 TEST_CASE(h2_client_stream_send_data_controlled_by_negative_stream_window_size) {
2025 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
2026
2027 /* get connection preface and acks out of the way */
2028 /* fake peer sends setting with 300 initial window size */
2029 struct aws_http2_setting settings_array[] = {
2030 {.id = AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE, .value = 300},
2031 };
2032 struct aws_h2_frame *settings =
2033 aws_h2_frame_new_settings(allocator, settings_array, AWS_ARRAY_SIZE(settings_array), false /*ack*/);
2034 ASSERT_NOT_NULL(settings);
2035 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface(&s_tester.peer, settings));
2036 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2037 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2038
2039 size_t frames_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
2040
2041 /* send request */
2042 struct aws_http_message *request = aws_http2_message_new_request(allocator);
2043 ASSERT_NOT_NULL(request);
2044
2045 struct aws_http_header request_headers_src[] = {
2046 DEFINE_HEADER(":method", "GET"),
2047 DEFINE_HEADER(":scheme", "https"),
2048 DEFINE_HEADER(":path", "/"),
2049 };
2050 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
2051
2052 char body_src[400];
2053 for (int i = 0; i < 400; i++) {
2054 body_src[i] = 'a';
2055 }
2056
2057 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_array(body_src, 400);
2058 struct aws_input_stream *request_body = aws_input_stream_new_from_cursor(allocator, &body_cursor);
2059 aws_http_message_set_body_stream(request, request_body);
2060
2061 struct client_stream_tester stream_tester;
2062 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
2063 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2064
2065 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
2066
2067 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2068 /* validate sent request (client should have sent HEADERS, part of DATA(first 300 bytes) */
2069 frames_count += 2;
2070 ASSERT_UINT_EQUALS(frames_count, h2_decode_tester_frame_count(&s_tester.peer.decode));
2071
2072 struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frames_count - 2);
2073 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
2074 ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(request), sent_headers_frame->headers));
2075 ASSERT_FALSE(sent_headers_frame->end_stream);
2076 struct h2_decoded_frame *sent_data_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frames_count - 1);
2077 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_DATA, sent_data_frame->type);
2078 ASSERT_FALSE(sent_data_frame->end_stream);
2079 ASSERT_BIN_ARRAYS_EQUALS(sent_data_frame->data.buffer, sent_data_frame->data.len, body_src, 300);
2080
2081 /* fake peer set new INITIAL_WINDOW_SIZE to 0 to make stream window size to be negative,which should be -300 */
2082 settings_array[0].value = 0;
2083 settings = aws_h2_frame_new_settings(allocator, settings_array, AWS_ARRAY_SIZE(settings_array), false /*ack*/);
2084 ASSERT_NOT_NULL(settings);
2085 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, settings));
2086 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2087 /* Check for setting ACK */
2088 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2089 frames_count += 1;
2090 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2091 ASSERT_UINT_EQUALS(frames_count, h2_decode_tester_frame_count(&s_tester.peer.decode));
2092 struct h2_decoded_frame *setting_ack_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frames_count - 1);
2093 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_SETTINGS, setting_ack_frame->type);
2094 ASSERT_TRUE(setting_ack_frame->ack);
2095
2096 /* fake peer sends a WINDOW_UPDATE on stream to try unblocking the DATA frame. But just release (300+min window
2097 * size) bytes, it will still be min window size, nothing will be sent */
2098 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2099 allocator, stream_id, 300 + AWS_H2_MIN_WINDOW_SIZE, NULL, 0, false /*end_stream*/, false /*skip_check_data*/));
2100
2101 /* Release one more bytes, rest of the data will be sent */
2102 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2103 allocator, stream_id, 1, body_src, 100, true /*end_stream*/, false /*skip_check_data*/));
2104
2105 /* fake peer sends response headers */
2106 struct aws_http_header response_headers_src[] = {
2107 DEFINE_HEADER(":status", "200"),
2108 };
2109
2110 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
2111 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
2112
2113 struct aws_h2_frame *response_frame =
2114 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
2115 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
2116
2117 /* validate that request completed successfully */
2118 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2119 ASSERT_TRUE(stream_tester.complete);
2120 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2121 ASSERT_INT_EQUALS(200, stream_tester.response_status);
2122
2123 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
2124
2125 /* clean up */
2126 aws_http_headers_release(response_headers);
2127 aws_http_message_release(request);
2128 client_stream_tester_clean_up(&stream_tester);
2129 aws_input_stream_release(request_body);
2130 return s_tester_clean_up();
2131 }
2132
2133 /* Test when connection window size becomes zero, no stream can send data */
2134 TEST_CASE(h2_client_stream_send_data_controlled_by_connection_window_size) {
2135 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
2136
2137 /* bodies must be big enough to span multiple H2-frames and multiple aws_io_messages */
2138 size_t body_size = aws_h2_settings_initial[AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE] - AWS_H2_MIN_WINDOW_SIZE;
2139
2140 /* get connection preface and acks out of the way */
2141 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
2142 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2143 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2144
2145 /* send multiple requests */
2146 enum { NUM_STREAMS = 2 };
2147 struct aws_http_message *requests[NUM_STREAMS];
2148 struct aws_http_header request_headers_src[NUM_STREAMS][3] = {
2149 {
2150 DEFINE_HEADER(":method", "GET"),
2151 DEFINE_HEADER(":scheme", "https"),
2152 DEFINE_HEADER(":path", "/a.txt"),
2153 },
2154 {
2155 DEFINE_HEADER(":method", "GET"),
2156 DEFINE_HEADER(":scheme", "https"),
2157 DEFINE_HEADER(":path", "/b.txt"),
2158 },
2159 };
2160
2161 struct aws_byte_buf request_body_bufs[NUM_STREAMS];
2162 struct aws_input_stream *request_bodies[NUM_STREAMS];
2163 struct client_stream_tester stream_testers[NUM_STREAMS];
2164 for (size_t i = 0; i < NUM_STREAMS; ++i) {
2165 requests[i] = aws_http2_message_new_request(allocator);
2166 aws_http_message_add_header_array(requests[i], request_headers_src[i], AWS_ARRAY_SIZE(request_headers_src[i]));
2167
2168 /* fill first body with "aaaa...", second with "bbbb...", etc */
2169 ASSERT_SUCCESS(aws_byte_buf_init(&request_body_bufs[i], allocator, body_size));
2170 ASSERT_TRUE(aws_byte_buf_write_u8_n(&request_body_bufs[i], (uint8_t)('a' + i), body_size));
2171 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&request_body_bufs[i]);
2172
2173 request_bodies[i] = aws_input_stream_new_from_cursor(allocator, &body_cursor);
2174 ASSERT_NOT_NULL(request_bodies[i]);
2175
2176 aws_http_message_set_body_stream(requests[i], request_bodies[i]);
2177 }
2178 /* Send the first request, which will make the connection window to the min_window_size and stop connection from
2179 * sending more data */
2180 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[0], requests[0]));
2181 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2182
2183 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2184 /* Check the last frame is the end of the stream, if all the data is send. */
2185 struct h2_decoded_frame *latest_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
2186 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_DATA, latest_frame->type);
2187 ASSERT_TRUE(latest_frame->end_stream);
2188 size_t frames_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
2189
2190 /* Send the rest request, and only data frames will be blocked */
2191 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[1], requests[1]));
2192 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2193 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2194 frames_count += 1;
2195 /* Check only the HEADERS frame is received */
2196 ASSERT_UINT_EQUALS(frames_count, h2_decode_tester_frame_count(&s_tester.peer.decode));
2197 struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frames_count - 1);
2198 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
2199 ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(requests[1]), sent_headers_frame->headers));
2200 ASSERT_FALSE(sent_headers_frame->end_stream);
2201
2202 /* WINDOW UPDATE at the second stream will no help */
2203 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2204 allocator,
2205 aws_http_stream_get_id(stream_testers[1].stream),
2206 400,
2207 NULL,
2208 0,
2209 false /*end_stream*/,
2210 false /*skip_check_data*/));
2211
2212 char expected[400];
2213 for (int i = 0; i < 400; i++) {
2214 expected[i] = 'b';
2215 }
2216
2217 /* Connection window update will help, and the rest of the previous request is sent now */
2218 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2219 allocator, 0, 400 - AWS_H2_MIN_WINDOW_SIZE, expected, 400, false /*end_stream*/, false /*skip_check_data*/));
2220
2221 /* Release all the window */
2222 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2223 allocator, 0, (uint32_t)body_size, "", 0, true /*end_stream*/, true /*skip_check_data*/));
2224
2225 /* finally, send responses and ensure all streams complete successfully */
2226 struct aws_http_header response_headers_src[] = {DEFINE_HEADER(":status", "200")};
2227 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
2228 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
2229 for (size_t i = 0; i < NUM_STREAMS; ++i) {
2230 struct aws_h2_frame *response_frame = aws_h2_frame_new_headers(
2231 allocator,
2232 aws_http_stream_get_id(stream_testers[i].stream),
2233 response_headers,
2234 true /* end_stream */,
2235 0,
2236 NULL);
2237 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
2238 }
2239
2240 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2241 for (size_t i = 0; i < NUM_STREAMS; ++i) {
2242 ASSERT_TRUE(stream_testers[i].complete);
2243 ASSERT_INT_EQUALS(200, stream_testers[i].response_status);
2244 }
2245
2246 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
2247
2248 /* clean up */
2249 aws_http_headers_release(response_headers);
2250 for (size_t i = 0; i < NUM_STREAMS; ++i) {
2251 client_stream_tester_clean_up(&stream_testers[i]);
2252 aws_http_message_release(requests[i]);
2253 aws_input_stream_release(request_bodies[i]);
2254 aws_byte_buf_clean_up(&request_body_bufs[i]);
2255 }
2256 return s_tester_clean_up();
2257 }
2258
2259 /* Test when connection window size becomes zero, and stream window size is zero, window_update on connection and stream
2260 * will not affect eachother */
2261 TEST_CASE(h2_client_stream_send_data_controlled_by_connection_and_stream_window_size) {
2262 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
2263
2264 /* bodies must be big enough to span multiple H2-frames and multiple aws_io_messages */
2265 size_t body_size = aws_h2_settings_initial[AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE];
2266
2267 /* get connection preface and acks out of the way */
2268 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
2269 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2270 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2271
2272 /* send multiple requests */
2273 enum { NUM_STREAMS = 3 };
2274 struct aws_http_message *requests[NUM_STREAMS];
2275 struct aws_http_header request_headers_src[NUM_STREAMS][3] = {
2276 {
2277 DEFINE_HEADER(":method", "GET"),
2278 DEFINE_HEADER(":scheme", "https"),
2279 DEFINE_HEADER(":path", "/a.txt"),
2280 },
2281 {
2282 DEFINE_HEADER(":method", "GET"),
2283 DEFINE_HEADER(":scheme", "https"),
2284 DEFINE_HEADER(":path", "/b.txt"),
2285 },
2286 {
2287 DEFINE_HEADER(":method", "GET"),
2288 DEFINE_HEADER(":scheme", "https"),
2289 DEFINE_HEADER(":path", "/c.txt"),
2290 },
2291 };
2292
2293 struct aws_byte_buf request_body_bufs[NUM_STREAMS];
2294 struct aws_input_stream *request_bodies[NUM_STREAMS];
2295 struct client_stream_tester stream_testers[NUM_STREAMS];
2296 for (size_t i = 0; i < NUM_STREAMS; ++i) {
2297 requests[i] = aws_http2_message_new_request(allocator);
2298 aws_http_message_add_header_array(requests[i], request_headers_src[i], AWS_ARRAY_SIZE(request_headers_src[i]));
2299
2300 /* fill first body with "aaaa...", second with "bbbb...", etc */
2301 ASSERT_SUCCESS(aws_byte_buf_init(&request_body_bufs[i], allocator, body_size));
2302 ASSERT_TRUE(aws_byte_buf_write_u8_n(&request_body_bufs[i], (uint8_t)('a' + i), body_size));
2303 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&request_body_bufs[i]);
2304
2305 request_bodies[i] = aws_input_stream_new_from_cursor(allocator, &body_cursor);
2306 ASSERT_NOT_NULL(request_bodies[i]);
2307
2308 aws_http_message_set_body_stream(requests[i], request_bodies[i]);
2309 }
2310 /* Send the first request, which will take all the connection window */
2311 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[0], requests[0]));
2312 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2313
2314 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2315 /* Check the last frame is the end of the stream, if all the data is send. But it's not true in this test. */
2316 /* Since we stop sending data when the connection window size is smaller than 256 bytes, we actually cannot receive
2317 * the end of the stream here. */
2318 struct h2_decoded_frame *latest_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
2319 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_DATA, latest_frame->type);
2320 ASSERT_FALSE(latest_frame->end_stream);
2321 size_t frames_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
2322
2323 /* fake peer set new INITIAL_WINDOW_SIZE to 0 to set window size for rest stream to be 0 */
2324 struct aws_http2_setting settings_array[] = {
2325 {.id = AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE, .value = 0},
2326 };
2327 struct aws_h2_frame *settings =
2328 aws_h2_frame_new_settings(allocator, settings_array, AWS_ARRAY_SIZE(settings_array), false /*ack*/);
2329 ASSERT_NOT_NULL(settings);
2330 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, settings));
2331 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2332 /* Get setting ACK */
2333 frames_count += 1;
2334 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2335 ASSERT_UINT_EQUALS(frames_count, h2_decode_tester_frame_count(&s_tester.peer.decode));
2336 struct h2_decoded_frame *setting_ack_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frames_count - 1);
2337 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_SETTINGS, setting_ack_frame->type);
2338 ASSERT_TRUE(setting_ack_frame->ack);
2339
2340 /* Send the rest requst, which only data frames will be blocked */
2341 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[1], requests[1]));
2342 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[2], requests[2]));
2343 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2344 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2345 /* Check only the HEADERS frames of two streams are received */
2346 frames_count += 2;
2347 ASSERT_UINT_EQUALS(frames_count, h2_decode_tester_frame_count(&s_tester.peer.decode));
2348 /* Header for requests[1] */
2349 struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frames_count - 2);
2350 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
2351 ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(requests[1]), sent_headers_frame->headers));
2352 ASSERT_FALSE(sent_headers_frame->end_stream);
2353 /* Header for requests[2] */
2354 sent_headers_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frames_count - 1);
2355 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
2356 ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(requests[2]), sent_headers_frame->headers));
2357 ASSERT_FALSE(sent_headers_frame->end_stream);
2358
2359 char expected_b[400];
2360 for (int i = 0; i < 400; i++) {
2361 expected_b[i] = 'b';
2362 }
2363 char expected_c[400];
2364 for (int i = 0; i < 400; i++) {
2365 expected_c[i] = 'c';
2366 }
2367 /* WINDOW UPDATE at requests[1] will no help */
2368 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2369 allocator,
2370 aws_http_stream_get_id(stream_testers[1].stream),
2371 400,
2372 NULL,
2373 0,
2374 false /*end_stream*/,
2375 false /*skip_check_data*/));
2376
2377 /* WINDOW UPDATE at the connection to keep connection wide open, but only 10 bytes of requests[1] will be sent */
2378 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2379 allocator, 0, (uint32_t)body_size * 3, expected_b, 400, false /*end_stream*/, false /*skip_check_data*/));
2380
2381 /* WINDOW UPDATE at requests[1] will help requests[1] to send data now */
2382 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2383 allocator,
2384 aws_http_stream_get_id(stream_testers[1].stream),
2385 400,
2386 expected_b,
2387 400,
2388 false /*end_stream*/,
2389 false /*skip_check_data*/));
2390 /* WINDOW UPDATE at requests[2] will help requests[2] to send data now */
2391 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2392 allocator,
2393 aws_http_stream_get_id(stream_testers[2].stream),
2394 400,
2395 expected_c,
2396 400,
2397 false /*end_stream*/,
2398 false /*skip_check_data*/));
2399
2400 /* Release all the window for requests[0] */
2401 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2402 allocator,
2403 aws_http_stream_get_id(stream_testers[0].stream),
2404 (uint32_t)body_size + AWS_H2_MIN_WINDOW_SIZE,
2405 "",
2406 0,
2407 true /*end_stream*/,
2408 true /*skip_check_data*/));
2409 /* Release all the window for requests[1] */
2410 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2411 allocator,
2412 aws_http_stream_get_id(stream_testers[1].stream),
2413 (uint32_t)body_size + AWS_H2_MIN_WINDOW_SIZE,
2414 "",
2415 0,
2416 true /*end_stream*/,
2417 true /*skip_check_data*/));
2418 /* Release all the window for requests[2] */
2419 ASSERT_SUCCESS(s_fake_peer_window_update_check(
2420 allocator,
2421 aws_http_stream_get_id(stream_testers[2].stream),
2422 (uint32_t)body_size + AWS_H2_MIN_WINDOW_SIZE,
2423 "",
2424 0,
2425 true /*end_stream*/,
2426 true /*skip_check_data*/));
2427
2428 /* finally, send responses and ensure all streams complete successfully */
2429 struct aws_http_header response_headers_src[] = {DEFINE_HEADER(":status", "200")};
2430 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
2431 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
2432 for (size_t i = 0; i < NUM_STREAMS; ++i) {
2433 struct aws_h2_frame *response_frame = aws_h2_frame_new_headers(
2434 allocator,
2435 aws_http_stream_get_id(stream_testers[i].stream),
2436 response_headers,
2437 true /* end_stream */,
2438 0,
2439 NULL);
2440 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
2441 }
2442
2443 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2444 for (size_t i = 0; i < NUM_STREAMS; ++i) {
2445 ASSERT_TRUE(stream_testers[i].complete);
2446 ASSERT_INT_EQUALS(200, stream_testers[i].response_status);
2447 }
2448
2449 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
2450
2451 /* clean up */
2452 aws_http_headers_release(response_headers);
2453 for (size_t i = 0; i < NUM_STREAMS; ++i) {
2454 client_stream_tester_clean_up(&stream_testers[i]);
2455 aws_http_message_release(requests[i]);
2456 aws_input_stream_release(request_bodies[i]);
2457 aws_byte_buf_clean_up(&request_body_bufs[i]);
2458 }
2459 return s_tester_clean_up();
2460 }
2461
2462 /* Test receiving a response with DATA frames, the window update frame will be sent */
2463 TEST_CASE(h2_client_stream_send_window_update) {
2464 /* Enable automatic window manager management */
2465 s_tester.no_conn_manual_win_management = true;
2466 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
2467
2468 /* fake peer sends connection preface */
2469 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
2470 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2471 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2472 /* Check the inital window update frame has been sent to maximize the connection window */
2473 size_t initial_window_update_index = 0;
2474 struct h2_decoded_frame *initial_connection_window_update_frame = h2_decode_tester_find_stream_frame(
2475 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, 0 /*stream_id*/, 0 /*idx*/, &initial_window_update_index);
2476 ASSERT_NOT_NULL(initial_connection_window_update_frame);
2477 ASSERT_UINT_EQUALS(
2478 AWS_H2_WINDOW_UPDATE_MAX - AWS_H2_INIT_WINDOW_SIZE,
2479 initial_connection_window_update_frame->window_size_increment);
2480
2481 /* send request */
2482 struct aws_http_message *request = aws_http2_message_new_request(allocator);
2483 ASSERT_NOT_NULL(request);
2484
2485 struct aws_http_header request_headers_src[] = {
2486 DEFINE_HEADER(":method", "GET"),
2487 DEFINE_HEADER(":scheme", "https"),
2488 DEFINE_HEADER(":path", "/"),
2489 };
2490 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
2491
2492 struct client_stream_tester stream_tester;
2493 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
2494 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2495 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
2496
2497 /* fake peer sends response headers */
2498 struct aws_http_header response_headers_src[] = {
2499 DEFINE_HEADER(":status", "200"),
2500 };
2501
2502 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
2503 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
2504
2505 struct aws_h2_frame *response_frame =
2506 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
2507 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
2508
2509 /* fake peer sends 1 DATA frame */
2510 const char *body_src = "hello";
2511 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, body_src, false /*end_stream*/));
2512
2513 /* check that 2 WINDOW_UPDATE frames have been sent.
2514 * 1 for the connection, and 1 for the stream */
2515 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2516 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2517
2518 struct h2_decoded_frame *stream_window_update_frame = h2_decode_tester_find_stream_frame(
2519 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, stream_id, 0 /*idx*/, NULL);
2520 ASSERT_NOT_NULL(stream_window_update_frame);
2521 ASSERT_UINT_EQUALS(5, stream_window_update_frame->window_size_increment);
2522 struct h2_decoded_frame *connection_window_update_frame = h2_decode_tester_find_stream_frame(
2523 &s_tester.peer.decode,
2524 AWS_H2_FRAME_T_WINDOW_UPDATE,
2525 0 /*stream_id*/,
2526 initial_window_update_index + 1 /*idx*/,
2527 NULL);
2528 ASSERT_NOT_NULL(connection_window_update_frame);
2529 ASSERT_UINT_EQUALS(5, connection_window_update_frame->window_size_increment);
2530
2531 /* clean up */
2532 aws_http_headers_release(response_headers);
2533 aws_http_message_release(request);
2534 client_stream_tester_clean_up(&stream_tester);
2535 return s_tester_clean_up();
2536 }
2537
2538 /* Peer sends a frame larger than the window size we had on stream, will result in stream error */
2539 TEST_CASE(h2_client_stream_err_received_data_flow_control) {
2540
2541 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
2542
2543 /* fake peer sends connection preface */
2544 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
2545 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2546
2547 size_t window_size = 10;
2548
2549 /* change the settings of the initial window size for new stream flow-control window */
2550 struct aws_http2_setting settings_array[] = {
2551 {.id = AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE, .value = (uint32_t)window_size},
2552 };
2553
2554 ASSERT_SUCCESS(aws_http2_connection_change_settings(
2555 s_tester.connection,
2556 settings_array,
2557 AWS_ARRAY_SIZE(settings_array),
2558 NULL /*callback function*/,
2559 NULL /*user_data*/));
2560 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2561 /* fake peer sends two settings ack back, one for the initial settings, one for the user settings we just sent */
2562 struct aws_h2_frame *peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
2563 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
2564 peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
2565 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
2566 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2567
2568 /* send request */
2569 struct aws_http_message *request = aws_http2_message_new_request(allocator);
2570 ASSERT_NOT_NULL(request);
2571
2572 struct aws_http_header request_headers_src[] = {
2573 DEFINE_HEADER(":method", "GET"),
2574 DEFINE_HEADER(":scheme", "https"),
2575 DEFINE_HEADER(":path", "/"),
2576 };
2577 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
2578
2579 struct client_stream_tester stream_tester;
2580 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
2581 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2582 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
2583
2584 /* fake peer sends response headers */
2585 struct aws_http_header response_headers_src[] = {
2586 DEFINE_HEADER(":status", "200"),
2587 };
2588
2589 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
2590 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
2591
2592 struct aws_h2_frame *response_frame =
2593 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
2594 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
2595
2596 struct aws_byte_buf response_body_bufs;
2597 /* fake peer sends a DATA frame larger than the window size we have */
2598 ASSERT_SUCCESS(aws_byte_buf_init(&response_body_bufs, allocator, window_size + 1));
2599 ASSERT_TRUE(aws_byte_buf_write_u8_n(&response_body_bufs, (uint8_t)'a', window_size + 1));
2600 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&response_body_bufs);
2601 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, true /*end_stream*/));
2602
2603 /* validate that stream completed with error */
2604 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2605 ASSERT_TRUE(stream_tester.complete);
2606 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
2607
2608 /* a stream error should not affect the connection */
2609 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
2610
2611 /* validate that stream sent RST_STREAM with AWS_HTTP2_ERR_FLOW_CONTROL_ERROR */
2612 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2613 struct h2_decoded_frame *rst_stream_frame =
2614 h2_decode_tester_find_stream_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, stream_id, 0, NULL);
2615 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_FLOW_CONTROL_ERROR, rst_stream_frame->error_code);
2616
2617 /* clean up */
2618 aws_byte_buf_clean_up(&response_body_bufs);
2619 aws_http_headers_release(response_headers);
2620 aws_http_message_release(request);
2621 client_stream_tester_clean_up(&stream_tester);
2622 return s_tester_clean_up();
2623 }
2624
2625 static int s_manual_window_management_tester_init(
2626 struct aws_allocator *alloc,
2627 bool conn,
2628 bool stream,
2629 size_t stream_initial_window_size,
2630 void *ctx) {
2631 (void)ctx;
2632 aws_http_library_init(alloc);
2633
2634 s_tester.alloc = alloc;
2635
2636 struct aws_testing_channel_options options = {.clock_fn = aws_high_res_clock_get_ticks};
2637
2638 ASSERT_SUCCESS(testing_channel_init(&s_tester.testing_channel, alloc, &options));
2639 struct aws_http2_setting settings_array[] = {
2640 {.id = AWS_HTTP2_SETTINGS_ENABLE_PUSH, .value = 0},
2641 };
2642
2643 struct aws_http2_connection_options http2_options = {
2644 .initial_settings_array = settings_array,
2645 .num_initial_settings = AWS_ARRAY_SIZE(settings_array),
2646 .max_closed_streams = AWS_HTTP2_DEFAULT_MAX_CLOSED_STREAMS,
2647 .conn_manual_window_management = conn,
2648 /* Disable the batching for window update in tests */
2649 .conn_window_size_threshold_to_send_update = UINT32_MAX,
2650 .stream_window_size_threshold_to_send_update = UINT32_MAX,
2651 };
2652
2653 s_tester.connection = aws_http_connection_new_http2_client(
2654 alloc, stream /* manual window management */, stream_initial_window_size, &http2_options);
2655 ASSERT_NOT_NULL(s_tester.connection);
2656
2657 { /* re-enact marriage vows of http-connection and channel (handled by http-bootstrap in real world) */
2658 struct aws_channel_slot *slot = aws_channel_slot_new(s_tester.testing_channel.channel);
2659 ASSERT_NOT_NULL(slot);
2660 ASSERT_SUCCESS(aws_channel_slot_insert_end(s_tester.testing_channel.channel, slot));
2661 ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, &s_tester.connection->channel_handler));
2662 s_tester.connection->vtable->on_channel_handler_installed(&s_tester.connection->channel_handler, slot);
2663 }
2664
2665 struct h2_fake_peer_options peer_options = {
2666 .alloc = alloc,
2667 .testing_channel = &s_tester.testing_channel,
2668 .is_server = true,
2669 };
2670 ASSERT_SUCCESS(h2_fake_peer_init(&s_tester.peer, &peer_options));
2671
2672 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2673 return AWS_OP_SUCCESS;
2674 }
2675
2676 /* Peer sends a flow-controlled frame when the connection window-size is not enough for it will result in connection
2677 * flow-control error */
2678 TEST_CASE(h2_client_conn_err_received_data_flow_control) {
2679 /* disable the connection automatic window update */
2680 ASSERT_SUCCESS(s_manual_window_management_tester_init(
2681 allocator, true /*conn*/, false /*stream*/, AWS_H2_INIT_WINDOW_SIZE, ctx));
2682
2683 /* get connection preface and acks out of the way */
2684 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
2685 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2686 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2687
2688 /* send request */
2689 struct aws_http_message *request = aws_http2_message_new_request(allocator);
2690 ASSERT_NOT_NULL(request);
2691
2692 struct aws_http_header request_headers_src[] = {
2693 DEFINE_HEADER(":method", "GET"),
2694 DEFINE_HEADER(":scheme", "https"),
2695 DEFINE_HEADER(":path", "/"),
2696 };
2697 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
2698
2699 struct client_stream_tester stream_tester;
2700 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
2701 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2702 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
2703
2704 /* fake peer sends response headers */
2705 struct aws_http_header response_headers_src[] = {
2706 DEFINE_HEADER(":status", "200"),
2707 };
2708
2709 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
2710 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
2711
2712 struct aws_h2_frame *response_frame =
2713 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
2714 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
2715
2716 struct aws_byte_buf response_body_bufs;
2717
2718 /* The max body size here is limited. So we need to send multiple bodies to get the flow-control error */
2719 size_t body_size =
2720 aws_max_size(aws_h2_settings_initial[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE], g_aws_channel_max_fragment_size) -
2721 AWS_H2_FRAME_PREFIX_SIZE;
2722 /* fake peer sends a DATA frame larger than the window size we have */
2723 ASSERT_SUCCESS(aws_byte_buf_init(&response_body_bufs, allocator, body_size));
2724 ASSERT_TRUE(aws_byte_buf_write_u8_n(&response_body_bufs, (uint8_t)'a', body_size));
2725 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&response_body_bufs);
2726 for (uint32_t i = 0; i < aws_h2_settings_initial[AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE] / body_size; i++) {
2727 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, false /*end_stream*/));
2728 /* manually update the stream flow-control window, ensure that stream window is available all the time */
2729 aws_http_stream_update_window(stream_tester.stream, body_size);
2730 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2731 }
2732 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
2733 /* the last one will result in the connection flow control error */
2734 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, true /*end_stream*/));
2735 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2736 /* validate the connection completed with error */
2737 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
2738 ASSERT_INT_EQUALS(
2739 AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
2740
2741 /* client should send GOAWAY */
2742 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2743 struct h2_decoded_frame *goaway =
2744 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_GOAWAY, 0, NULL);
2745 ASSERT_NOT_NULL(goaway);
2746 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_FLOW_CONTROL_ERROR, goaway->error_code);
2747 ASSERT_UINT_EQUALS(0, goaway->goaway_last_stream_id);
2748
2749 /* clean up */
2750 aws_byte_buf_clean_up(&response_body_bufs);
2751 aws_http_headers_release(response_headers);
2752 aws_http_message_release(request);
2753 client_stream_tester_clean_up(&stream_tester);
2754 return s_tester_clean_up();
2755 }
2756
2757 /* Receiving invalid WINDOW_UPDATE frame of stream should result in a "Stream Error", invalid WINDOW_UPDATE frame of
2758 * connection should result in a "Connection Error". */
2759 static int s_invalid_window_update(
2760 struct aws_allocator *allocator,
2761 void *ctx,
2762 uint32_t window_update_size,
2763 enum aws_http2_error_code h2_error_code) {
2764 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
2765
2766 /* fake peer sends connection preface */
2767 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
2768 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2769
2770 /* send request */
2771 struct aws_http_message *request = aws_http2_message_new_request(allocator);
2772 ASSERT_NOT_NULL(request);
2773
2774 struct aws_http_header request_headers_src[] = {
2775 DEFINE_HEADER(":method", "GET"),
2776 DEFINE_HEADER(":scheme", "https"),
2777 DEFINE_HEADER(":path", "/"),
2778 };
2779 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
2780
2781 struct client_stream_tester stream_tester;
2782 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
2783 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2784
2785 /* Send the largest update on stream, which will cause the flow-control window of stream exceeding the max */
2786 struct aws_h2_frame *stream_window_update =
2787 aws_h2_frame_new_window_update(allocator, aws_http_stream_get_id(stream_tester.stream), window_update_size);
2788 ASSERT_NOT_NULL(stream_window_update);
2789 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, stream_window_update));
2790
2791 /* validate that stream completed with error */
2792 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2793 ASSERT_TRUE(stream_tester.complete);
2794 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
2795
2796 /* a stream error should not affect the connection */
2797 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
2798
2799 /* validate that stream sent RST_STREAM */
2800 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2801 struct h2_decoded_frame *rst_stream_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
2802 ASSERT_INT_EQUALS(AWS_H2_FRAME_T_RST_STREAM, rst_stream_frame->type);
2803 ASSERT_INT_EQUALS(h2_error_code, rst_stream_frame->error_code);
2804
2805 /* Send the largest update on stream, which will cause the flow-control window of stream exceeding the max */
2806 stream_window_update = aws_h2_frame_new_window_update(allocator, 0, window_update_size);
2807 ASSERT_NOT_NULL(stream_window_update);
2808 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, stream_window_update));
2809 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2810
2811 /* validate the connection completed with error */
2812 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
2813 ASSERT_INT_EQUALS(
2814 AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
2815
2816 /* client should send GOAWAY */
2817 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2818 struct h2_decoded_frame *goaway =
2819 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_GOAWAY, 0, NULL);
2820 ASSERT_NOT_NULL(goaway);
2821 ASSERT_UINT_EQUALS(h2_error_code, goaway->error_code);
2822
2823 /* clean up */
2824 aws_http_message_release(request);
2825 client_stream_tester_clean_up(&stream_tester);
2826 return s_tester_clean_up();
2827 }
2828
2829 /* Window update cause window to exceed max size will lead to FLOW_CONTROL_ERROR */
2830 TEST_CASE(h2_client_conn_err_window_update_exceed_max) {
2831 return s_invalid_window_update(allocator, ctx, AWS_H2_WINDOW_UPDATE_MAX, AWS_HTTP2_ERR_FLOW_CONTROL_ERROR);
2832 }
2833
2834 /* Window update with zero update size will lead to PROTOCOL_ERROR */
2835 TEST_CASE(h2_client_conn_err_window_update_size_zero) {
2836 return s_invalid_window_update(allocator, ctx, 0, AWS_HTTP2_ERR_PROTOCOL_ERROR);
2837 }
2838
2839 static int s_compare_settings_array(
2840 const struct aws_http2_setting *expected,
2841 const struct aws_http2_setting *got,
2842 int num_settings) {
2843
2844 for (int i = 0; i < num_settings; ++i) {
2845 struct aws_http2_setting expected_settings = expected[i];
2846 struct aws_http2_setting got_settings = got[i];
2847
2848 ASSERT_INT_EQUALS(expected_settings.id, got_settings.id);
2849 ASSERT_INT_EQUALS(expected_settings.value, got_settings.value);
2850 }
2851
2852 return AWS_OP_SUCCESS;
2853 }
2854
2855 /* SETTINGS_INITIAL_WINDOW_SIZE cause stream window to exceed the max size is a Connection ERROR... */
2856 TEST_CASE(h2_client_conn_err_initial_window_size_settings_cause_window_exceed_max) {
2857 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
2858
2859 /* fake peer sends connection preface */
2860 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
2861 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2862
2863 /* send request */
2864 struct aws_http_message *request = aws_http2_message_new_request(allocator);
2865 ASSERT_NOT_NULL(request);
2866
2867 struct aws_http_header request_headers_src[] = {
2868 DEFINE_HEADER(":method", "GET"),
2869 DEFINE_HEADER(":scheme", "https"),
2870 DEFINE_HEADER(":path", "/"),
2871 };
2872 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
2873
2874 struct client_stream_tester stream_tester;
2875 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
2876 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2877
2878 /* Send a small update on stream */
2879 struct aws_h2_frame *stream_window_update =
2880 aws_h2_frame_new_window_update(allocator, aws_http_stream_get_id(stream_tester.stream), 1);
2881 ASSERT_NOT_NULL(stream_window_update);
2882 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, stream_window_update));
2883
2884 /* Then we set INITIAL_WINDOW_SIZE to largest - 1, which will not lead to any error */
2885 struct aws_http2_setting settings_array[] = {
2886 {.id = AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE, .value = AWS_H2_WINDOW_UPDATE_MAX - 1},
2887 };
2888 struct aws_h2_frame *settings =
2889 aws_h2_frame_new_settings(allocator, settings_array, AWS_ARRAY_SIZE(settings_array), false /*ack*/);
2890 ASSERT_NOT_NULL(settings);
2891 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, settings));
2892 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2893 /* validate connection is still open and callback invoked */
2894 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
2895 ASSERT_INT_EQUALS(s_tester.user_data.num_settings, AWS_ARRAY_SIZE(settings_array));
2896 ASSERT_SUCCESS(s_compare_settings_array(
2897 settings_array, s_tester.user_data.remote_settings_array, AWS_ARRAY_SIZE(settings_array)));
2898 s_tester.user_data.num_settings = 0;
2899
2900 /* Finally we set INITIAL_WINDOW_SIZE to largest, which cause the stream window size to exceed the max size */
2901 settings_array[0].value = AWS_H2_WINDOW_UPDATE_MAX;
2902 settings = aws_h2_frame_new_settings(allocator, settings_array, AWS_ARRAY_SIZE(settings_array), false /*ack*/);
2903 ASSERT_NOT_NULL(settings);
2904 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, settings));
2905 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2906 /* validate callback is not invoked, num_settings is still 0 */
2907 ASSERT_INT_EQUALS(0, s_tester.user_data.num_settings);
2908
2909 /* validate the connection completed with error */
2910 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
2911 ASSERT_INT_EQUALS(
2912 AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
2913
2914 /* client should send GOAWAY */
2915 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2916 struct h2_decoded_frame *goaway =
2917 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_GOAWAY, 0, NULL);
2918 ASSERT_NOT_NULL(goaway);
2919 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_FLOW_CONTROL_ERROR, goaway->error_code);
2920 ASSERT_UINT_EQUALS(0, goaway->goaway_last_stream_id);
2921
2922 /* clean up */
2923 aws_http_message_release(request);
2924 client_stream_tester_clean_up(&stream_tester);
2925 return s_tester_clean_up();
2926 }
2927
2928 /* A server MAY finish the response before client done sending, and client just keep sending the rest of request. */
2929 TEST_CASE(h2_client_stream_receive_end_stream_before_done_sending) {
2930 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
2931
2932 /* get connection preface and acks out of the way */
2933 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
2934 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2935 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2936
2937 /* get request ready */
2938 struct aws_http_message *request = aws_http2_message_new_request(allocator);
2939 ASSERT_NOT_NULL(request);
2940
2941 struct aws_http_header request_headers_src[] = {
2942 DEFINE_HEADER(":method", "POST"),
2943 DEFINE_HEADER(":scheme", "https"),
2944 DEFINE_HEADER(":path", "/"),
2945 };
2946 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
2947
2948 /* use a stalled body-stream so our test can send the response before the request is completely sent */
2949 const char *body_src = "hello";
2950 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
2951 struct aws_input_stream *request_body = aws_input_stream_new_tester(allocator, body_cursor);
2952 aws_http_message_set_body_stream(request, request_body);
2953 aws_input_stream_tester_set_max_bytes_per_read(request_body, 1);
2954
2955 struct client_stream_tester stream_tester;
2956 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
2957
2958 /* execute 1 event-loop tick, 1 byte of the body and header should be written */
2959 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
2960 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
2961 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2962 ASSERT_NOT_NULL(
2963 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_HEADERS, 0 /*search_start_idx*/, NULL));
2964 struct h2_decoded_frame *sent_data_frame =
2965 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_DATA, 0 /*search_start_idx*/, NULL);
2966 ASSERT_FALSE(sent_data_frame->end_stream);
2967 ASSERT_TRUE(aws_byte_buf_eq_c_str(&sent_data_frame->data, "h"));
2968 /* fake peer sends complete response */
2969 struct aws_http_header response_headers_src[] = {
2970 DEFINE_HEADER(":status", "404"),
2971 };
2972 /* stop stalling the input stream */
2973 aws_input_stream_tester_set_max_bytes_per_read(request_body, 5);
2974 size_t frames_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
2975
2976 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
2977 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
2978
2979 struct aws_h2_frame *response_frame =
2980 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
2981 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
2982
2983 /* No rst stream sent, we wait until the client finish sending body */
2984 /* validate the client request completes successfully */
2985 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
2986 ASSERT_TRUE(stream_tester.complete);
2987 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2988 ASSERT_INT_EQUALS(404, stream_tester.response_status);
2989
2990 /* Check the rest of the body received by peer */
2991 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
2992 struct h2_decoded_frame *rest_data_frame = h2_decode_tester_find_frame(
2993 &s_tester.peer.decode, AWS_H2_FRAME_T_DATA, frames_count /*search_start_idx*/, NULL);
2994 ASSERT_TRUE(rest_data_frame->end_stream);
2995 ASSERT_TRUE(aws_byte_buf_eq_c_str(&rest_data_frame->data, "ello"));
2996
2997 /* clean up */
2998 aws_http_headers_release(response_headers);
2999 client_stream_tester_clean_up(&stream_tester);
3000 aws_http_message_release(request);
3001 aws_input_stream_release(request_body);
3002 return s_tester_clean_up();
3003 }
3004
3005 /* A server MAY request that the client abort transmission of a request without error by sending a
3006 * RST_STREAM with an error code of NO_ERROR after sending a complete response. */
3007 TEST_CASE(h2_client_stream_receive_end_stream_and_rst_before_done_sending) {
3008 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
3009
3010 /* get connection preface and acks out of the way */
3011 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3012 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3013 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3014
3015 /* get request ready */
3016 struct aws_http_message *request = aws_http2_message_new_request(allocator);
3017 ASSERT_NOT_NULL(request);
3018
3019 struct aws_http_header request_headers_src[] = {
3020 DEFINE_HEADER(":method", "POST"),
3021 DEFINE_HEADER(":scheme", "https"),
3022 DEFINE_HEADER(":path", "/"),
3023 };
3024 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
3025
3026 /* use a stalled body-stream so our test can send the response before the request is completely sent */
3027 const char *body_src = "hello";
3028 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
3029 struct aws_input_stream *request_body = aws_input_stream_new_tester(allocator, body_cursor);
3030 aws_http_message_set_body_stream(request, request_body);
3031 aws_input_stream_tester_set_max_bytes_per_read(request_body, 0);
3032
3033 struct client_stream_tester stream_tester;
3034 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
3035
3036 /* execute 1 event-loop tick, the HEADERS should be written
3037 * (don't drain task queue or we'll infinite loop waiting for stalled body) */
3038 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
3039 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
3040 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3041 ASSERT_NOT_NULL(
3042 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_HEADERS, 0 /*search_start_idx*/, NULL));
3043
3044 /* fake peer sends complete response */
3045 struct aws_http_header response_headers_src[] = {
3046 DEFINE_HEADER(":status", "404"),
3047 };
3048
3049 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
3050 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
3051
3052 struct aws_h2_frame *response_frame =
3053 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
3054 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
3055
3056 /* fake peer sends RST_STREAM with error-code NO_ERROR */
3057 response_frame = aws_h2_frame_new_rst_stream(allocator, stream_id, AWS_HTTP2_ERR_NO_ERROR);
3058 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
3059
3060 /* validate the client request completes successfully */
3061 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3062 ASSERT_TRUE(stream_tester.complete);
3063 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
3064 ASSERT_INT_EQUALS(404, stream_tester.response_status);
3065 /* Check no data frame received by the peer */
3066 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3067 ASSERT_NULL(h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_DATA, 0 /*search_start_idx*/, NULL));
3068
3069 /* clean up */
3070 aws_http_headers_release(response_headers);
3071 client_stream_tester_clean_up(&stream_tester);
3072 aws_http_message_release(request);
3073 aws_input_stream_release(request_body);
3074 return s_tester_clean_up();
3075 }
3076
3077 TEST_CASE(h2_client_stream_err_input_stream_failure) {
3078 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
3079
3080 /* get connection preface and acks out of the way */
3081 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3082 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3083 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3084
3085 /* get request ready */
3086 struct aws_http_message *request = aws_http2_message_new_request(allocator);
3087 ASSERT_NOT_NULL(request);
3088
3089 struct aws_http_header request_headers_src[] = {
3090 DEFINE_HEADER(":method", "POST"),
3091 DEFINE_HEADER(":scheme", "https"),
3092 DEFINE_HEADER(":path", "/"),
3093 };
3094 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
3095
3096 /* use a stalled body-stream so our test can send the response before the request is completely sent */
3097 const char *body_src = "hello";
3098 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
3099 struct aws_input_stream *request_body = aws_input_stream_new_tester(allocator, body_cursor);
3100 aws_http_message_set_body_stream(request, request_body);
3101 aws_input_stream_tester_set_reading_broken(request_body, true /*is_broken*/);
3102 struct client_stream_tester stream_tester;
3103 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
3104
3105 /* validate that stream completed with error */
3106 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3107 ASSERT_TRUE(stream_tester.complete);
3108 ASSERT_INT_EQUALS(AWS_IO_STREAM_READ_FAILED, stream_tester.on_complete_error_code);
3109 /* a stream error should not affect the connection */
3110 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
3111 /* validate that stream sent RST_STREAM */
3112 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3113 struct h2_decoded_frame *rst_stream_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
3114 ASSERT_INT_EQUALS(AWS_HTTP2_ERR_INTERNAL_ERROR, rst_stream_frame->error_code);
3115 /* clean up */
3116 client_stream_tester_clean_up(&stream_tester);
3117 aws_http_message_release(request);
3118 aws_input_stream_release(request_body);
3119 return s_tester_clean_up();
3120 }
3121
3122 /* A request stream that receives RST_STREAM should terminate */
3123 TEST_CASE(h2_client_stream_err_receive_rst_stream) {
3124 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
3125
3126 /* fake peer sends connection preface */
3127 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3128 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3129
3130 /* send request */
3131 struct aws_http_message *request = aws_http2_message_new_request(allocator);
3132 ASSERT_NOT_NULL(request);
3133
3134 struct aws_http_header request_headers_src[] = {
3135 DEFINE_HEADER(":method", "GET"),
3136 DEFINE_HEADER(":scheme", "https"),
3137 DEFINE_HEADER(":path", "/"),
3138 };
3139 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
3140
3141 struct client_stream_tester stream_tester;
3142 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
3143 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3144
3145 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
3146
3147 /* fake peer sends RST_STREAM */
3148 struct aws_h2_frame *rst_stream =
3149 aws_h2_frame_new_rst_stream(allocator, stream_id, AWS_HTTP2_ERR_HTTP_1_1_REQUIRED);
3150 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, rst_stream));
3151
3152 /* validate that stream completed with error */
3153 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3154 ASSERT_TRUE(stream_tester.complete);
3155 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_RST_STREAM_RECEIVED, stream_tester.on_complete_error_code);
3156
3157 /* a stream error should not affect the connection */
3158 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
3159
3160 /* validate that stream did NOT send RST_STREAM */
3161 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3162 ASSERT_NULL(h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, 0, NULL));
3163
3164 /* clean up */
3165 aws_http_message_release(request);
3166 client_stream_tester_clean_up(&stream_tester);
3167 return s_tester_clean_up();
3168 }
3169
3170 /* We don't fully support PUSH_PROMISE, so we automatically send RST_STREAM to reject any promised streams.
3171 * Why, you ask, don't we simply send SETTINGS_ENABLE_PUSH=0 in the initial SETTINGS frame and call it a day?
3172 * Because it's theoretically possible for a server to start sending PUSH_PROMISE frames in the initial
3173 * response, before sending the ACK to the initial SETTINGS. */
3174 TEST_CASE(h2_client_push_promise_automatically_rejected) {
3175 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
3176
3177 /* fake peer sends connection preface */
3178 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3179 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3180
3181 /* send request */
3182 struct aws_http_message *request = aws_http2_message_new_request(allocator);
3183 ASSERT_NOT_NULL(request);
3184
3185 struct aws_http_header request_headers_src[] = {
3186 DEFINE_HEADER(":method", "GET"),
3187 DEFINE_HEADER(":scheme", "https"),
3188 DEFINE_HEADER(":authority", "veryblackpage.com"),
3189 DEFINE_HEADER(":path", "/"),
3190 };
3191 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
3192
3193 struct client_stream_tester stream_tester;
3194 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
3195
3196 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3197 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
3198
3199 /* fake peer sends push request (PUSH_PROMISE) */
3200 struct aws_http_header push_request_headers_src[] = {
3201 DEFINE_HEADER(":method", "GET"),
3202 DEFINE_HEADER(":scheme", "https"),
3203 DEFINE_HEADER(":authority", "veryblackpage.com"),
3204 DEFINE_HEADER(":path", "/style.css"),
3205 };
3206 struct aws_http_headers *push_request_headers = aws_http_headers_new(allocator);
3207 ASSERT_SUCCESS(aws_http_headers_add_array(
3208 push_request_headers, push_request_headers_src, AWS_ARRAY_SIZE(push_request_headers_src)));
3209
3210 uint32_t promised_stream_id = 2;
3211 struct aws_h2_frame *peer_frame =
3212 aws_h2_frame_new_push_promise(allocator, stream_id, promised_stream_id, push_request_headers, 0);
3213 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3214
3215 /* fake peer sends push response RIGHT AWAY before there's any possibility of receiving RST_STREAM */
3216 struct aws_http_header push_response_headers_src[] = {
3217 DEFINE_HEADER(":status", "200"),
3218 };
3219 struct aws_http_headers *push_response_headers = aws_http_headers_new(allocator);
3220 ASSERT_SUCCESS(aws_http_headers_add_array(
3221 push_response_headers, push_response_headers_src, AWS_ARRAY_SIZE(push_response_headers_src)));
3222
3223 peer_frame =
3224 aws_h2_frame_new_headers(allocator, promised_stream_id, push_response_headers, false /*end_stream*/, 0, NULL);
3225 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3226
3227 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(
3228 &s_tester.peer, promised_stream_id, "body {background-color: black;}", true /*end_stream*/));
3229
3230 /* fake peer sends response to the initial request */
3231 struct aws_http_header response_headers_src[] = {
3232 DEFINE_HEADER(":status", "200"),
3233 };
3234 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
3235 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
3236
3237 peer_frame = aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
3238 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3239
3240 const char *body_src = "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"></head></html>";
3241 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, body_src, true /*end_stream*/));
3242
3243 /* validate that stream completed successfully. */
3244 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3245 ASSERT_TRUE(stream_tester.complete);
3246 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
3247 ASSERT_INT_EQUALS(200, stream_tester.response_status);
3248 ASSERT_BIN_ARRAYS_EQUALS(
3249 body_src, strlen(body_src), stream_tester.response_body.buffer, stream_tester.response_body.len);
3250
3251 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
3252
3253 /* validate that client automatically sent RST_STREAM to reject the promised stream */
3254 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3255 struct h2_decoded_frame *client_sent_rst_stream = h2_decode_tester_find_stream_frame(
3256 &s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, promised_stream_id, 0, NULL);
3257 ASSERT_NOT_NULL(client_sent_rst_stream);
3258
3259 /* clean up */
3260 aws_http_headers_release(push_request_headers);
3261 aws_http_headers_release(push_response_headers);
3262 aws_http_headers_release(response_headers);
3263 aws_http_message_release(request);
3264 client_stream_tester_clean_up(&stream_tester);
3265 return s_tester_clean_up();
3266 }
3267
3268 /* Test client receives the GOAWAY frame, stop creating new stream and complete the streams whose id are higher than the
3269 * last stream id included in GOAWAY frame, and callback invoked */
3270 TEST_CASE(h2_client_conn_receive_goaway) {
3271 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
3272
3273 /* get connection preface and acks out of the way */
3274 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3275 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3276 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3277
3278 /* send multiple requests */
3279 enum { NUM_STREAMS = 3 };
3280 struct aws_http_message *requests[NUM_STREAMS];
3281 struct aws_http_header request_headers_src[NUM_STREAMS][3] = {
3282 {
3283 DEFINE_HEADER(":method", "GET"),
3284 DEFINE_HEADER(":scheme", "https"),
3285 DEFINE_HEADER(":path", "/a.txt"),
3286 },
3287 {
3288 DEFINE_HEADER(":method", "GET"),
3289 DEFINE_HEADER(":scheme", "https"),
3290 DEFINE_HEADER(":path", "/b.txt"),
3291 },
3292 {
3293 DEFINE_HEADER(":method", "GET"),
3294 DEFINE_HEADER(":scheme", "https"),
3295 DEFINE_HEADER(":path", "/c.txt"),
3296 },
3297 };
3298 struct client_stream_tester stream_testers[NUM_STREAMS];
3299 for (size_t i = 0; i < NUM_STREAMS; ++i) {
3300 requests[i] = aws_http2_message_new_request(allocator);
3301 aws_http_message_add_header_array(requests[i], request_headers_src[i], AWS_ARRAY_SIZE(request_headers_src[i]));
3302 }
3303 /* Send the first two requests */
3304 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[0], requests[0]));
3305 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[1], requests[1]));
3306 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3307
3308 /* fake peer send a GOAWAY frame indicating only the first request will be processed */
3309 uint32_t stream_id = aws_http_stream_get_id(stream_testers[0].stream);
3310 struct aws_byte_cursor debug_info;
3311 AWS_ZERO_STRUCT(debug_info);
3312 struct aws_h2_frame *peer_frame = aws_h2_frame_new_goaway(allocator, stream_id, AWS_HTTP2_ERR_NO_ERROR, debug_info);
3313 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3314 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3315
3316 /* validate the callback invoked and the information recorded during callback */
3317 ASSERT_INT_EQUALS(s_tester.user_data.http2_error, AWS_HTTP2_ERR_NO_ERROR);
3318 ASSERT_INT_EQUALS(s_tester.user_data.last_stream_id, stream_id);
3319
3320 /* validate the connection is still open, and the second request finished with GOAWAY_RECEIVED */
3321 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
3322 ASSERT_FALSE(stream_testers[0].complete);
3323 ASSERT_TRUE(stream_testers[1].complete);
3324 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_GOAWAY_RECEIVED, stream_testers[1].on_complete_error_code);
3325
3326 /* validate the new requst will no be accepted */
3327 ASSERT_FAILS(s_stream_tester_init(&stream_testers[2], requests[2]));
3328
3329 /* Try gracefully shutting down the connection */
3330 struct aws_http_header response_headers_src[] = {DEFINE_HEADER(":status", "200")};
3331 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
3332 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
3333 struct aws_h2_frame *response_frame = aws_h2_frame_new_headers(
3334 allocator, aws_http_stream_get_id(stream_testers[0].stream), response_headers, true /* end_stream */, 0, NULL);
3335 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
3336 /* shutdown channel */
3337 aws_channel_shutdown(s_tester.testing_channel.channel, AWS_ERROR_SUCCESS);
3338 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3339 ASSERT_TRUE(testing_channel_is_shutdown_completed(&s_tester.testing_channel));
3340
3341 /* validate the first request finishes successfully */
3342 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3343 ASSERT_TRUE(stream_testers[0].complete);
3344 ASSERT_INT_EQUALS(200, stream_testers[0].response_status);
3345
3346 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
3347
3348 /* clean up */
3349 aws_http_headers_release(response_headers);
3350 for (size_t i = 0; i < NUM_STREAMS; ++i) {
3351 client_stream_tester_clean_up(&stream_testers[i]);
3352 aws_http_message_release(requests[i]);
3353 }
3354 return s_tester_clean_up();
3355 }
3356
3357 /* Test client receives the GOAWAY frame with the debug data correctly */
3358 TEST_CASE(h2_client_conn_receive_goaway_debug_data) {
3359 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
3360
3361 /* get connection preface and acks out of the way */
3362 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3363 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3364 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3365
3366 /* fake peer send a GOAWAY frame indicating only the first request will be processed */
3367 uint32_t stream_id = 1;
3368 const char debug_string[] = "Error, Core Dump 0XFFFFFFFF";
3369 struct aws_byte_cursor debug_info = aws_byte_cursor_from_c_str(debug_string);
3370 struct aws_h2_frame *peer_frame = aws_h2_frame_new_goaway(allocator, stream_id, AWS_HTTP2_ERR_NO_ERROR, debug_info);
3371 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3372 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3373
3374 /* validate the callback invoked and the information recorded during callback */
3375 ASSERT_INT_EQUALS(s_tester.user_data.http2_error, AWS_HTTP2_ERR_NO_ERROR);
3376 ASSERT_INT_EQUALS(s_tester.user_data.last_stream_id, stream_id);
3377 ASSERT_TRUE(aws_byte_buf_eq_c_str(&s_tester.user_data.debug_data, debug_string));
3378
3379 return s_tester_clean_up();
3380 }
3381
3382 /* Test client receives the GOAWAY frame with invalid last stream id and connection error happened, and callback will
3383 * not be invoked for the invalid GOAWAY frame */
3384 TEST_CASE(h2_client_conn_err_invalid_last_stream_id_goaway) {
3385 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
3386
3387 /* get connection preface and acks out of the way */
3388 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3389 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3390 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3391
3392 /* fake peer send multiple GOAWAY frames */
3393 struct aws_byte_cursor debug_info;
3394 AWS_ZERO_STRUCT(debug_info);
3395 /* First on with last_stream_id as AWS_H2_STREAM_ID_MAX */
3396 struct aws_h2_frame *peer_frame =
3397 aws_h2_frame_new_goaway(allocator, AWS_H2_STREAM_ID_MAX, AWS_HTTP2_ERR_NO_ERROR, debug_info);
3398 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3399 /* validate the callback invoked and the information recorded during callback */
3400 ASSERT_INT_EQUALS(s_tester.user_data.http2_error, AWS_HTTP2_ERR_NO_ERROR);
3401 ASSERT_INT_EQUALS(s_tester.user_data.last_stream_id, AWS_H2_STREAM_ID_MAX);
3402
3403 int last_stream_id = 1;
3404 /* Second one with last_stream_id as 1 and some error */
3405 peer_frame = aws_h2_frame_new_goaway(allocator, last_stream_id, AWS_HTTP2_ERR_FLOW_CONTROL_ERROR, debug_info);
3406 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3407 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3408 ASSERT_INT_EQUALS(s_tester.user_data.http2_error, AWS_HTTP2_ERR_FLOW_CONTROL_ERROR);
3409 ASSERT_INT_EQUALS(s_tester.user_data.last_stream_id, last_stream_id);
3410
3411 /* validate the connection is still open, everything is fine */
3412 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
3413
3414 /* Another GOAWAY with higher last stream id will cause connection closed with an error */
3415 peer_frame = aws_h2_frame_new_goaway(allocator, last_stream_id + 1, AWS_HTTP2_ERR_FLOW_CONTROL_ERROR, debug_info);
3416 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3417 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3418 /* validate the callback is not invoked and the information is still the same as the second one */
3419 ASSERT_INT_EQUALS(s_tester.user_data.http2_error, AWS_HTTP2_ERR_FLOW_CONTROL_ERROR);
3420 ASSERT_INT_EQUALS(s_tester.user_data.last_stream_id, last_stream_id);
3421 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
3422 ASSERT_INT_EQUALS(
3423 AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
3424 /* clean up */
3425 return s_tester_clean_up();
3426 }
3427
3428 static void s_on_completed(struct aws_http_connection *connection, int error_code, void *user_data) {
3429 (void)connection;
3430 int *callback_error_code = user_data;
3431 *callback_error_code = error_code;
3432 }
3433
3434 /* Test the user API for changing HTTP/2 connection settings */
3435 TEST_CASE(h2_client_change_settings_succeed) {
3436
3437 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
3438 /* client sent the preface and first settings */
3439 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3440 struct h2_decoded_frame *first_written_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, 0);
3441 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_SETTINGS, first_written_frame->type);
3442 ASSERT_FALSE(first_written_frame->ack);
3443
3444 /* We disabled the push_promise at the initial setting, let's use user API to enable it. */
3445 /* Use user API to change HTTP/2 connection settings */
3446
3447 struct aws_http2_setting settings_array[] = {
3448 {.id = AWS_HTTP2_SETTINGS_ENABLE_PUSH, .value = 1},
3449 };
3450 int callback_error_code = INT32_MAX;
3451
3452 ASSERT_SUCCESS(aws_http2_connection_change_settings(
3453 s_tester.connection, settings_array, AWS_ARRAY_SIZE(settings_array), s_on_completed, &callback_error_code));
3454 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3455 /* check the settings frame is sent */
3456 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3457 struct h2_decoded_frame *second_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, 1);
3458 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_SETTINGS, second_frame->type);
3459 ASSERT_FALSE(second_frame->ack);
3460 ASSERT_INT_EQUALS(1, second_frame->settings.length);
3461 struct aws_http2_setting setting_received;
3462 aws_array_list_front(&second_frame->settings, &setting_received);
3463 ASSERT_INT_EQUALS(AWS_HTTP2_SETTINGS_ENABLE_PUSH, setting_received.id);
3464 ASSERT_INT_EQUALS(1, setting_received.value);
3465
3466 /* fake peer sends connection preface */
3467 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3468 /* fake peer sends two settings ack back, one for the initial settings, one for the user settings we just sent */
3469 struct aws_h2_frame *peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
3470 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3471 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3472 /* Check the callback has NOT fired after the first settings ack frame, the user_data has not changed */
3473 ASSERT_INT_EQUALS(INT32_MAX, callback_error_code);
3474 peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
3475 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3476 /* Check the callback has fired after the second settings ack frame, the error code we got is NO_ERROR(0) */
3477 ASSERT_INT_EQUALS(0, callback_error_code);
3478
3479 /* Check empty settings can be sent */
3480 callback_error_code = INT32_MAX;
3481
3482 ASSERT_SUCCESS(
3483 aws_http2_connection_change_settings(s_tester.connection, NULL, 0, s_on_completed, &callback_error_code));
3484 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3485 /* check the empty settings frame is sent */
3486 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3487 struct h2_decoded_frame *second_settings = h2_decode_tester_latest_frame(&s_tester.peer.decode);
3488 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_SETTINGS, second_settings->type);
3489 ASSERT_FALSE(second_settings->ack);
3490 ASSERT_INT_EQUALS(0, second_settings->settings.length);
3491 peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
3492 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3493 /* Check the callback has fired after the second settings ack frame, the error code we got is NO_ERROR(0) */
3494 ASSERT_INT_EQUALS(0, callback_error_code);
3495
3496 struct aws_http_message *request = aws_http2_message_new_request(allocator);
3497 ASSERT_NOT_NULL(request);
3498
3499 struct aws_http_header request_headers_src[] = {
3500 DEFINE_HEADER(":method", "GET"),
3501 DEFINE_HEADER(":scheme", "https"),
3502 DEFINE_HEADER(":path", "/"),
3503 };
3504 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
3505
3506 struct client_stream_tester stream_tester;
3507 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
3508 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3509 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
3510
3511 /* fake peer sends push request (PUSH_PROMISE) */
3512 struct aws_http_header push_request_headers_src[] = {
3513 DEFINE_HEADER(":method", "GET"),
3514 DEFINE_HEADER(":scheme", "https"),
3515 DEFINE_HEADER(":authority", "veryblackpage.com"),
3516 DEFINE_HEADER(":path", "/style.css"),
3517 };
3518 struct aws_http_headers *push_request_headers = aws_http_headers_new(allocator);
3519 ASSERT_SUCCESS(aws_http_headers_add_array(
3520 push_request_headers, push_request_headers_src, AWS_ARRAY_SIZE(push_request_headers_src)));
3521
3522 uint32_t promised_stream_id = 2;
3523 peer_frame = aws_h2_frame_new_push_promise(allocator, stream_id, promised_stream_id, push_request_headers, 0);
3524 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3525 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3526 /* validate the connection is still open */
3527 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
3528
3529 /* clean up */
3530 aws_http_headers_release(push_request_headers);
3531 aws_http_message_release(request);
3532 client_stream_tester_clean_up(&stream_tester);
3533 return s_tester_clean_up();
3534 }
3535
3536 /* Test the user API for changing HTTP/2 connection settings and no settings ACK received from peer */
3537 TEST_CASE(h2_client_change_settings_failed_no_ack_received) {
3538
3539 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
3540 /* client sent the preface and first settings */
3541 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3542 struct h2_decoded_frame *first_written_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, 0);
3543 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_SETTINGS, first_written_frame->type);
3544 ASSERT_FALSE(first_written_frame->ack);
3545
3546 /* request changing setting */
3547 struct aws_http2_setting settings_array[] = {
3548 {.id = AWS_HTTP2_SETTINGS_ENABLE_PUSH, .value = 1},
3549 };
3550 int callback_error_code = INT32_MAX;
3551 ASSERT_SUCCESS(aws_http2_connection_change_settings(
3552 s_tester.connection, settings_array, AWS_ARRAY_SIZE(settings_array), s_on_completed, &callback_error_code));
3553 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3554 /* fake peer sends connection preface */
3555 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3556 /* fake peer sends one settings ack back the initial settings */
3557 struct aws_h2_frame *peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
3558 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3559 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3560 /* Check the callback has NOT fired after the first settings ack frame, the user_data has not changed */
3561 ASSERT_INT_EQUALS(INT32_MAX, callback_error_code);
3562
3563 /* shutdown the connection */
3564 h2_fake_peer_clean_up(&s_tester.peer);
3565 aws_http_connection_release(s_tester.connection);
3566 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3567 /* Check the callback has fired with error, after connection shutdown */
3568 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, callback_error_code);
3569 ASSERT_SUCCESS(testing_channel_clean_up(&s_tester.testing_channel));
3570 /* clean up */
3571 aws_http_library_clean_up();
3572 return AWS_OP_SUCCESS;
3573 }
3574
3575 /* Test manual window management for stream successfully disabled the automatically window update */
3576 TEST_CASE(h2_client_manual_window_management_disabled_auto_window_update) {
3577 size_t window_size = 10;
3578 ASSERT_SUCCESS(
3579 s_manual_window_management_tester_init(allocator, false /*conn*/, true /*stream*/, window_size, ctx));
3580 /* fake peer sends connection preface */
3581 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3582 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3583 /* fake peer sends settings ack back for the initial settings */
3584 struct aws_h2_frame *peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
3585 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3586 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3587
3588 /* send request */
3589 struct aws_http_message *request = aws_http2_message_new_request(allocator);
3590 ASSERT_NOT_NULL(request);
3591
3592 struct aws_http_header request_headers_src[] = {
3593 DEFINE_HEADER(":method", "GET"),
3594 DEFINE_HEADER(":scheme", "https"),
3595 DEFINE_HEADER(":path", "/"),
3596 };
3597 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
3598
3599 struct client_stream_tester stream_tester;
3600 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
3601 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3602 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
3603
3604 /* fake peer sends response headers */
3605 struct aws_http_header response_headers_src[] = {
3606 DEFINE_HEADER(":status", "200"),
3607 };
3608
3609 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
3610 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
3611
3612 struct aws_h2_frame *response_frame =
3613 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
3614 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
3615
3616 struct aws_byte_buf response_body_bufs;
3617 /* fake peer sends a DATA frame take all the window size we have */
3618 ASSERT_SUCCESS(aws_byte_buf_init(&response_body_bufs, allocator, window_size));
3619 ASSERT_TRUE(aws_byte_buf_write_u8_n(&response_body_bufs, (uint8_t)'a', window_size));
3620 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&response_body_bufs);
3621 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, false /*end_stream*/));
3622
3623 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3624 /* validate no window_update for stream frame sent automatically */
3625 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3626 ASSERT_NULL(h2_decode_tester_find_stream_frame(
3627 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, stream_id, 0 /*idx*/, NULL));
3628
3629 /* validate that stream is still open */
3630 ASSERT_FALSE(stream_tester.complete);
3631 /* peer send another flow-controlled frame will result in stream flow control error */
3632 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, true /*end_stream*/));
3633
3634 /* validate that stream completed with error */
3635 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3636 ASSERT_TRUE(stream_tester.complete);
3637 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
3638
3639 /* a stream error should not affect the connection */
3640 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
3641
3642 /* validate that stream sent RST_STREAM with AWS_HTTP2_ERR_FLOW_CONTROL_ERROR */
3643 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3644 struct h2_decoded_frame *rst_stream_frame =
3645 h2_decode_tester_find_stream_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, stream_id, 0, NULL);
3646 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_FLOW_CONTROL_ERROR, rst_stream_frame->error_code);
3647
3648 /* clean up */
3649 aws_byte_buf_clean_up(&response_body_bufs);
3650 aws_http_headers_release(response_headers);
3651 aws_http_message_release(request);
3652 client_stream_tester_clean_up(&stream_tester);
3653
3654 return s_tester_clean_up();
3655 }
3656
3657 /* Test manual window management for stream with an invalid initial window size. */
3658 TEST_CASE(h2_client_manual_window_management_invalid_initial_window_size) {
3659 /* The initial window size must be less than or equal to 2^31 - 1. */
3660 ASSERT_FAILS(
3661 s_manual_window_management_tester_init(allocator, false /*conn*/, true /*stream*/, (size_t)INT32_MAX + 1, ctx));
3662 ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error());
3663 return s_tester_clean_up();
3664 }
3665 /* Test manual window management for stream with the max initial window size. */
3666 TEST_CASE(h2_client_manual_window_management_max_initial_window_size) {
3667 ASSERT_SUCCESS(
3668 s_manual_window_management_tester_init(allocator, false /*conn*/, true /*stream*/, (size_t)INT32_MAX, ctx));
3669 return s_tester_clean_up();
3670 }
3671
3672 TEST_CASE(h2_client_manual_window_management_user_send_stream_window_update) {
3673
3674 ASSERT_SUCCESS(s_manual_window_management_tester_init(
3675 allocator, false /*conn*/, true /*stream*/, AWS_H2_INIT_WINDOW_SIZE, ctx));
3676 /* fake peer sends connection preface */
3677 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3678 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3679
3680 size_t window_size = 10;
3681
3682 /* change the settings of the initial window size for new stream flow-control window */
3683 struct aws_http2_setting settings_array[] = {
3684 {.id = AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE, .value = (uint32_t)window_size},
3685 };
3686
3687 ASSERT_SUCCESS(aws_http2_connection_change_settings(
3688 s_tester.connection,
3689 settings_array,
3690 AWS_ARRAY_SIZE(settings_array),
3691 NULL /*callback function*/,
3692 NULL /*user_data*/));
3693 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3694 /* fake peer sends two settings ack back, one for the initial settings, one for the user settings we just sent */
3695 struct aws_h2_frame *peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
3696 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3697 peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
3698 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3699 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3700
3701 /* send request */
3702 struct aws_http_message *request = aws_http2_message_new_request(allocator);
3703 ASSERT_NOT_NULL(request);
3704
3705 struct aws_http_header request_headers_src[] = {
3706 DEFINE_HEADER(":method", "GET"),
3707 DEFINE_HEADER(":scheme", "https"),
3708 DEFINE_HEADER(":path", "/"),
3709 };
3710 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
3711
3712 struct client_stream_tester stream_tester;
3713 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
3714 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3715 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
3716
3717 /* fake peer sends response headers */
3718 struct aws_http_header response_headers_src[] = {
3719 DEFINE_HEADER(":status", "200"),
3720 };
3721
3722 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
3723 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
3724
3725 struct aws_h2_frame *response_frame =
3726 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
3727 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
3728
3729 struct aws_byte_buf response_body_bufs;
3730 /* fake peer sends a DATA frame take all the window size we have */
3731 ASSERT_SUCCESS(aws_byte_buf_init(&response_body_bufs, allocator, window_size));
3732 ASSERT_TRUE(aws_byte_buf_write_u8_n(&response_body_bufs, (uint8_t)'a', window_size));
3733 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&response_body_bufs);
3734 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, false /*end_stream*/));
3735
3736 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3737 /* validate no window_update frame for stream sent automatically */
3738 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3739 ASSERT_NULL(h2_decode_tester_find_stream_frame(
3740 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, stream_id, 0 /*idx*/, NULL));
3741
3742 /* call API to update the stream window */
3743 aws_http_stream_update_window(stream_tester.stream, window_size);
3744 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3745 /* validate stream window_update frame was sent */
3746 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3747 struct h2_decoded_frame *stream_window_update_frame = h2_decode_tester_find_stream_frame(
3748 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, stream_id, 0 /*idx*/, NULL);
3749 ASSERT_NOT_NULL(stream_window_update_frame);
3750 ASSERT_UINT_EQUALS(window_size, stream_window_update_frame->window_size_increment);
3751
3752 /* validate that stream is still open */
3753 ASSERT_FALSE(stream_tester.complete);
3754 /* peer send another flow-controlled frame will success */
3755 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, true /*end_stream*/));
3756
3757 /* validate that stream received complete response */
3758 struct aws_byte_buf expected_body;
3759 ASSERT_SUCCESS(aws_byte_buf_init(&expected_body, allocator, 2 * window_size));
3760 ASSERT_TRUE(aws_byte_buf_write_u8_n(&expected_body, (uint8_t)'a', 2 * window_size));
3761 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3762 ASSERT_TRUE(stream_tester.complete);
3763 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
3764 ASSERT_INT_EQUALS(200, stream_tester.response_status);
3765 ASSERT_SUCCESS(s_compare_headers(response_headers, stream_tester.response_headers));
3766 ASSERT_TRUE(aws_byte_buf_eq(&stream_tester.response_body, &expected_body));
3767
3768 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
3769
3770 /* clean up */
3771 aws_byte_buf_clean_up(&response_body_bufs);
3772 aws_byte_buf_clean_up(&expected_body);
3773 aws_http_headers_release(response_headers);
3774 aws_http_message_release(request);
3775 client_stream_tester_clean_up(&stream_tester);
3776
3777 return s_tester_clean_up();
3778 }
3779
3780 TEST_CASE(h2_client_manual_window_management_user_send_stream_window_update_with_padding) {
3781
3782 size_t window_size = 20;
3783 size_t padding_length = 10;
3784 size_t data_length = window_size - padding_length - 1;
3785 ASSERT_SUCCESS(
3786 s_manual_window_management_tester_init(allocator, false /*conn*/, true /*stream*/, window_size, ctx));
3787 /* fake peer sends connection preface */
3788 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3789 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3790
3791 /* fake peer sends settings ack back for the initial settings */
3792 struct aws_h2_frame *peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
3793 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
3794 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3795
3796 /* send request */
3797 struct aws_http_message *request = aws_http2_message_new_request(allocator);
3798 ASSERT_NOT_NULL(request);
3799
3800 struct aws_http_header request_headers_src[] = {
3801 DEFINE_HEADER(":method", "GET"),
3802 DEFINE_HEADER(":scheme", "https"),
3803 DEFINE_HEADER(":path", "/"),
3804 };
3805 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
3806
3807 struct client_stream_tester stream_tester;
3808 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
3809 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3810 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
3811
3812 /* fake peer sends response headers */
3813 struct aws_http_header response_headers_src[] = {
3814 DEFINE_HEADER(":status", "200"),
3815 };
3816
3817 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
3818 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
3819
3820 struct aws_h2_frame *response_frame =
3821 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
3822 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
3823
3824 struct aws_byte_buf response_body_bufs;
3825 /* fake peer sends a DATA frame take all the window size we have */
3826 ASSERT_SUCCESS(aws_byte_buf_init(&response_body_bufs, allocator, data_length));
3827 ASSERT_TRUE(aws_byte_buf_write_u8_n(&response_body_bufs, (uint8_t)'a', data_length));
3828 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&response_body_bufs);
3829 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_with_padding_length(
3830 &s_tester.peer, stream_id, body_cursor, false /*end_stream*/, (uint8_t)padding_length));
3831
3832 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3833 /* validate no window_update frame for stream sent automatically */
3834 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3835 /* padding and padding length should be updated automatically */
3836 size_t end_index = 0;
3837 struct h2_decoded_frame *stream_window_update_frame = h2_decode_tester_find_stream_frame(
3838 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, stream_id, 0 /*idx*/, &end_index);
3839 ASSERT_NOT_NULL(stream_window_update_frame);
3840 ASSERT_UINT_EQUALS(
3841 padding_length + 1 /*one byte for padding length*/, stream_window_update_frame->window_size_increment);
3842
3843 /* call API to update the stream window */
3844 aws_http_stream_update_window(stream_tester.stream, data_length);
3845 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3846 /* validate stream window_update frame from user was sent */
3847 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3848 stream_window_update_frame = h2_decode_tester_find_stream_frame(
3849 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, stream_id, end_index + 1, NULL);
3850 ASSERT_NOT_NULL(stream_window_update_frame);
3851 ASSERT_UINT_EQUALS(data_length, stream_window_update_frame->window_size_increment);
3852
3853 /* validate that stream is still open */
3854 ASSERT_FALSE(stream_tester.complete);
3855 /* peer send another flow-controlled frame will success */
3856 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, true /*end_stream*/));
3857
3858 /* validate that stream received complete response */
3859 struct aws_byte_buf expected_body;
3860 ASSERT_SUCCESS(aws_byte_buf_init(&expected_body, allocator, 2 * data_length));
3861 ASSERT_TRUE(aws_byte_buf_write_u8_n(&expected_body, (uint8_t)'a', 2 * data_length));
3862 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3863 ASSERT_TRUE(stream_tester.complete);
3864 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
3865 ASSERT_INT_EQUALS(200, stream_tester.response_status);
3866 ASSERT_SUCCESS(s_compare_headers(response_headers, stream_tester.response_headers));
3867 ASSERT_TRUE(aws_byte_buf_eq(&stream_tester.response_body, &expected_body));
3868
3869 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
3870
3871 /* clean up */
3872 aws_byte_buf_clean_up(&response_body_bufs);
3873 aws_byte_buf_clean_up(&expected_body);
3874 aws_http_headers_release(response_headers);
3875 aws_http_message_release(request);
3876 client_stream_tester_clean_up(&stream_tester);
3877
3878 return s_tester_clean_up();
3879 }
3880
3881 TEST_CASE(h2_client_manual_window_management_user_send_stream_window_update_overflow_capped) {
3882
3883 ASSERT_SUCCESS(s_manual_window_management_tester_init(
3884 allocator, false /*conn*/, true /*stream*/, AWS_H2_INIT_WINDOW_SIZE, ctx));
3885 /* fake peer sends connection preface */
3886 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3887 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3888
3889 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3890
3891 /* send request */
3892 struct aws_http_message *request = aws_http2_message_new_request(allocator);
3893 ASSERT_NOT_NULL(request);
3894
3895 struct aws_http_header request_headers_src[] = {
3896 DEFINE_HEADER(":method", "GET"),
3897 DEFINE_HEADER(":scheme", "https"),
3898 DEFINE_HEADER(":path", "/"),
3899 };
3900 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
3901
3902 struct client_stream_tester stream_tester;
3903 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
3904 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3905
3906 /* call API to update the stream window and cause a overflow */
3907 aws_http_stream_update_window(stream_tester.stream, INT32_MAX);
3908 aws_http_stream_update_window(stream_tester.stream, INT32_MAX);
3909 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3910 /* The overflowed update window will be capped to the allowed max */
3911 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
3912 struct aws_h2_stream *h2_stream = AWS_CONTAINER_OF(stream_tester.stream, struct aws_h2_stream, base);
3913 ASSERT_TRUE(h2_stream->thread_data.window_size_self == AWS_H2_WINDOW_UPDATE_MAX);
3914 /* clean up */
3915 aws_http_message_release(request);
3916 client_stream_tester_clean_up(&stream_tester);
3917
3918 return s_tester_clean_up();
3919 }
3920
3921 /* Peer sends a flow-controlled frame when the connection window-size is not enough for it will result in connection
3922 * flow-control error */
3923 TEST_CASE(h2_client_manual_window_management_user_send_conn_window_update) {
3924
3925 ASSERT_SUCCESS(s_manual_window_management_tester_init(allocator, true /*conn*/, false /*stream*/, 0, ctx));
3926
3927 /* get connection preface and acks out of the way */
3928 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
3929 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3930 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3931
3932 /* send request */
3933 struct aws_http_message *request = aws_http2_message_new_request(allocator);
3934 ASSERT_NOT_NULL(request);
3935
3936 struct aws_http_header request_headers_src[] = {
3937 DEFINE_HEADER(":method", "GET"),
3938 DEFINE_HEADER(":scheme", "https"),
3939 DEFINE_HEADER(":path", "/"),
3940 };
3941 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
3942
3943 struct client_stream_tester stream_tester;
3944 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
3945 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3946 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
3947
3948 /* fake peer sends response headers */
3949 struct aws_http_header response_headers_src[] = {
3950 DEFINE_HEADER(":status", "200"),
3951 };
3952
3953 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
3954 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
3955
3956 struct aws_h2_frame *response_frame =
3957 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
3958 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
3959
3960 struct aws_byte_buf response_body_bufs;
3961
3962 /* The max body size here is limited. So we need to send multiple bodies to get the flow-control error */
3963 size_t body_size =
3964 aws_min_size(aws_h2_settings_initial[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE], g_aws_channel_max_fragment_size) -
3965 AWS_H2_FRAME_PREFIX_SIZE;
3966 /* fake peer sends a DATA frame larger than the window size we have */
3967 ASSERT_SUCCESS(aws_byte_buf_init(&response_body_bufs, allocator, body_size));
3968 ASSERT_TRUE(aws_byte_buf_write_u8_n(&response_body_bufs, (uint8_t)'a', body_size));
3969 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&response_body_bufs);
3970 /* number of bodies peer will send, just to ensure the connection flow-control window will not be blocked when we
3971 * manually update it */
3972 size_t body_number = 2 * aws_h2_settings_initial[AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE] / body_size;
3973 for (size_t i = 0; i < body_number; i++) {
3974 if (i == body_number - 1) {
3975 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, true /*end_stream*/));
3976 } else {
3977 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, false /*end_stream*/));
3978 }
3979 /* manually update connection flow-control window. */
3980 aws_http2_connection_update_window(s_tester.connection, (uint32_t)body_size);
3981 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3982 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
3983
3984 struct h2_decoded_frame *connection_window_update_frame = h2_decode_tester_find_stream_frame(
3985 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, 0 /*stream_id*/, 0 /*idx*/, NULL);
3986 ASSERT_NOT_NULL(connection_window_update_frame);
3987 ASSERT_UINT_EQUALS(body_size, connection_window_update_frame->window_size_increment);
3988 }
3989 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
3990 /* validate that stream received complete response */
3991 struct aws_byte_buf expected_body;
3992 ASSERT_SUCCESS(aws_byte_buf_init(&expected_body, allocator, body_number * body_size));
3993 ASSERT_TRUE(aws_byte_buf_write_u8_n(&expected_body, (uint8_t)'a', body_number * body_size));
3994 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
3995 ASSERT_TRUE(stream_tester.complete);
3996 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
3997 ASSERT_INT_EQUALS(200, stream_tester.response_status);
3998 ASSERT_SUCCESS(s_compare_headers(response_headers, stream_tester.response_headers));
3999 ASSERT_TRUE(aws_byte_buf_eq(&stream_tester.response_body, &expected_body));
4000
4001 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
4002
4003 /* clean up */
4004 aws_byte_buf_clean_up(&response_body_bufs);
4005 aws_byte_buf_clean_up(&expected_body);
4006 aws_http_headers_release(response_headers);
4007 aws_http_message_release(request);
4008 client_stream_tester_clean_up(&stream_tester);
4009 return s_tester_clean_up();
4010 }
4011
4012 TEST_CASE(h2_client_manual_window_management_user_send_conn_window_update_with_padding) {
4013
4014 ASSERT_SUCCESS(s_manual_window_management_tester_init(
4015 allocator, true /*conn*/, false /*stream*/, AWS_H2_INIT_WINDOW_SIZE, ctx));
4016
4017 /* get connection preface and acks out of the way */
4018 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4019 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4020 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4021
4022 /* send request */
4023 struct aws_http_message *request = aws_http2_message_new_request(allocator);
4024 ASSERT_NOT_NULL(request);
4025
4026 struct aws_http_header request_headers_src[] = {
4027 DEFINE_HEADER(":method", "GET"),
4028 DEFINE_HEADER(":scheme", "https"),
4029 DEFINE_HEADER(":path", "/"),
4030 };
4031 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
4032
4033 struct client_stream_tester stream_tester;
4034 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
4035 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4036 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
4037
4038 /* fake peer sends response headers */
4039 struct aws_http_header response_headers_src[] = {
4040 DEFINE_HEADER(":status", "200"),
4041 };
4042
4043 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
4044 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
4045
4046 struct aws_h2_frame *response_frame =
4047 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
4048 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
4049
4050 struct aws_byte_buf response_body_bufs;
4051
4052 /* The max body size here is limited. So we need to send multiple bodies to get the flow-control error */
4053 size_t padding_size = 10;
4054 size_t body_size =
4055 aws_min_size(aws_h2_settings_initial[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE], g_aws_channel_max_fragment_size) -
4056 AWS_H2_FRAME_PREFIX_SIZE - padding_size - 1;
4057 /* fake peer sends a DATA frame larger than the window size we have */
4058 ASSERT_SUCCESS(aws_byte_buf_init(&response_body_bufs, allocator, body_size));
4059 ASSERT_TRUE(aws_byte_buf_write_u8_n(&response_body_bufs, (uint8_t)'a', body_size));
4060 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&response_body_bufs);
4061 /* number of bodies peer will send, just to ensure the connection flow-control window will not be blocked when we
4062 * manually update it */
4063 size_t body_number = 2 * aws_h2_settings_initial[AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE] / body_size;
4064 for (size_t i = 0; i < body_number; i++) {
4065 if (i == body_number - 1) {
4066 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_with_padding_length(
4067 &s_tester.peer, stream_id, body_cursor, true /*end_stream*/, (uint8_t)padding_size));
4068 } else {
4069 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_with_padding_length(
4070 &s_tester.peer, stream_id, body_cursor, false /*end_stream*/, (uint8_t)padding_size));
4071 }
4072 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4073 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4074 size_t out_index = 0;
4075 /* The update for padding and padding length should be sent */
4076 struct h2_decoded_frame *connection_window_update_frame = h2_decode_tester_find_stream_frame(
4077 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, 0 /*stream_id*/, 0 /*idx*/, &out_index);
4078 ASSERT_NOT_NULL(connection_window_update_frame);
4079 ASSERT_UINT_EQUALS(
4080 padding_size + 1 /* one byte for padding length */, connection_window_update_frame->window_size_increment);
4081 /* manually update the stream and connection flow-control window. */
4082 aws_http_stream_update_window(stream_tester.stream, body_size);
4083 aws_http2_connection_update_window(s_tester.connection, (uint32_t)body_size);
4084 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4085 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4086
4087 connection_window_update_frame = h2_decode_tester_find_stream_frame(
4088 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, 0 /*stream_id*/, out_index + 1 /*idx*/, &out_index);
4089 ASSERT_NOT_NULL(connection_window_update_frame);
4090 ASSERT_UINT_EQUALS(body_size, connection_window_update_frame->window_size_increment);
4091 }
4092 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
4093 /* validate that stream received complete response */
4094 struct aws_byte_buf expected_body;
4095 ASSERT_SUCCESS(aws_byte_buf_init(&expected_body, allocator, body_number * body_size));
4096 ASSERT_TRUE(aws_byte_buf_write_u8_n(&expected_body, (uint8_t)'a', body_number * body_size));
4097 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4098 ASSERT_TRUE(stream_tester.complete);
4099 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
4100 ASSERT_INT_EQUALS(200, stream_tester.response_status);
4101 ASSERT_SUCCESS(s_compare_headers(response_headers, stream_tester.response_headers));
4102 ASSERT_TRUE(aws_byte_buf_eq(&stream_tester.response_body, &expected_body));
4103
4104 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
4105
4106 /* clean up */
4107 aws_byte_buf_clean_up(&response_body_bufs);
4108 aws_byte_buf_clean_up(&expected_body);
4109 aws_http_headers_release(response_headers);
4110 aws_http_message_release(request);
4111 client_stream_tester_clean_up(&stream_tester);
4112 return s_tester_clean_up();
4113 }
4114
4115 TEST_CASE(h2_client_manual_window_management_user_send_connection_window_update_overflow_capped) {
4116
4117 ASSERT_SUCCESS(s_manual_window_management_tester_init(allocator, true /*conn*/, false /*stream*/, 0, ctx));
4118 /* fake peer sends connection preface */
4119 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4120 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4121
4122 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4123
4124 /* update the connection window to cause an overflow */
4125 aws_http2_connection_update_window(s_tester.connection, INT32_MAX);
4126 aws_http2_connection_update_window(s_tester.connection, INT32_MAX);
4127 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4128 /* The overflowed update window will be capped to the allowed max */
4129 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
4130 struct aws_h2_connection *h2_connection = AWS_CONTAINER_OF(s_tester.connection, struct aws_h2_connection, base);
4131 ASSERT_TRUE(h2_connection->thread_data.window_size_self == AWS_H2_WINDOW_UPDATE_MAX);
4132
4133 /* clean up */
4134 return s_tester_clean_up();
4135 }
4136
4137 struct ping_user_data {
4138 uint64_t rtt_ns;
4139 int error_code;
4140 };
4141
4142 static void on_ping_complete(
4143 struct aws_http_connection *connection,
4144 uint64_t round_trip_time_ns,
4145 int error_code,
4146 void *user_data) {
4147
4148 (void)connection;
4149 struct ping_user_data *data = user_data;
4150 data->error_code = error_code;
4151 data->rtt_ns = round_trip_time_ns;
4152 }
4153
4154 /* Test the user API for PING successfully get the round trip time */
4155 TEST_CASE(h2_client_send_ping_successfully_receive_ack) {
4156
4157 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4158 /* get connection preface and acks out of the way */
4159 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4160 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4161
4162 struct aws_byte_cursor opaque_data = aws_byte_cursor_from_c_str("12345678");
4163 struct ping_user_data data = {.rtt_ns = 0, .error_code = INT32_MAX};
4164 /* client request a PING */
4165 ASSERT_SUCCESS(aws_http2_connection_ping(s_tester.connection, &opaque_data, on_ping_complete, &data));
4166 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4167 /* check ping frame received */
4168 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4169 struct h2_decoded_frame *ping_frame =
4170 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_PING, 0, NULL);
4171 ASSERT_BIN_ARRAYS_EQUALS(
4172 opaque_data.ptr, AWS_HTTP2_PING_DATA_SIZE, ping_frame->ping_opaque_data, AWS_HTTP2_PING_DATA_SIZE);
4173 ASSERT_FALSE(ping_frame->ack);
4174
4175 /* fake peer send PING ACK */
4176 struct aws_h2_frame *peer_frame = aws_h2_frame_new_ping(allocator, true /*ACK*/, ping_frame->ping_opaque_data);
4177 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
4178 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4179 /* check callback fired, and succeed */
4180 ASSERT_INT_EQUALS(0, data.error_code);
4181 ASSERT_FALSE(data.rtt_ns == 0);
4182 /* clean up */
4183 return s_tester_clean_up();
4184 }
4185
4186 /* Test the user request a PING, but peer never sends PING ACK back */
4187 TEST_CASE(h2_client_send_ping_no_ack_received) {
4188
4189 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4190 /* get connection preface and acks out of the way */
4191 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4192 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4193
4194 struct ping_user_data data = {.rtt_ns = 0, .error_code = INT32_MAX};
4195 /* client request a PING */
4196 ASSERT_SUCCESS(aws_http2_connection_ping(s_tester.connection, NULL, on_ping_complete, &data));
4197 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4198 /* check ping frame received */
4199 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4200 struct h2_decoded_frame *ping_frame =
4201 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_PING, 0, NULL);
4202 uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE];
4203 AWS_ZERO_ARRAY(opaque_data);
4204 /* Zeroed 8 bytes data received */
4205 ASSERT_BIN_ARRAYS_EQUALS(
4206 opaque_data, AWS_HTTP2_PING_DATA_SIZE, ping_frame->ping_opaque_data, AWS_HTTP2_PING_DATA_SIZE);
4207 ASSERT_FALSE(ping_frame->ack);
4208
4209 /* shutdown the connection */
4210 h2_fake_peer_clean_up(&s_tester.peer);
4211 aws_http_connection_release(s_tester.connection);
4212 ASSERT_SUCCESS(testing_channel_clean_up(&s_tester.testing_channel));
4213 /* Check the callback has fired with error */
4214 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, data.error_code);
4215 ASSERT_TRUE(data.rtt_ns == 0);
4216 /* clean up */
4217 aws_http_library_clean_up();
4218 return AWS_OP_SUCCESS;
4219 }
4220
4221 /* Test the user request a PING, but peer sends an extra PING ACK */
4222 TEST_CASE(h2_client_conn_err_extraneous_ping_ack_received) {
4223
4224 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4225 /* get connection preface and acks out of the way */
4226 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4227 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4228
4229 struct aws_byte_cursor opaque_data = aws_byte_cursor_from_c_str("12345678");
4230 /* client request a PING */
4231 ASSERT_SUCCESS(aws_http2_connection_ping(s_tester.connection, &opaque_data, NULL, NULL));
4232 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4233
4234 struct aws_h2_frame *peer_frame = aws_h2_frame_new_ping(allocator, true /*ACK*/, opaque_data.ptr);
4235 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
4236 /* fake peer send an extra PING ACK */
4237 peer_frame = aws_h2_frame_new_ping(allocator, true /*ACK*/, opaque_data.ptr);
4238 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
4239 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4240
4241 /* validate the connection completed with error */
4242 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
4243 ASSERT_INT_EQUALS(
4244 AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
4245
4246 /* client should send GOAWAY */
4247 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4248 struct h2_decoded_frame *goaway =
4249 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_GOAWAY, 0, NULL);
4250 ASSERT_NOT_NULL(goaway);
4251 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, goaway->error_code);
4252 ASSERT_UINT_EQUALS(0, goaway->goaway_last_stream_id);
4253
4254 /* clean up */
4255 return s_tester_clean_up();
4256 }
4257
4258 /* Test the user request a PING, but peer sends the PING ACK with mismatched opaque_data */
4259 TEST_CASE(h2_client_conn_err_mismatched_ping_ack_received) {
4260
4261 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4262 /* get connection preface and acks out of the way */
4263 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4264 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4265
4266 struct aws_byte_cursor opaque_data = aws_byte_cursor_from_c_str("12345678");
4267 /* client request a PING with all zero opaque_data */
4268 ASSERT_SUCCESS(aws_http2_connection_ping(s_tester.connection, NULL, NULL, NULL));
4269 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4270 /* peer sends PING ACK with numbers in payload */
4271 struct aws_h2_frame *peer_frame = aws_h2_frame_new_ping(allocator, true /*ACK*/, opaque_data.ptr);
4272 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
4273 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4274
4275 /* validate the connection completed with error */
4276 ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
4277 ASSERT_INT_EQUALS(
4278 AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
4279
4280 /* client should send GOAWAY */
4281 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4282 struct h2_decoded_frame *goaway =
4283 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_GOAWAY, 0, NULL);
4284 ASSERT_NOT_NULL(goaway);
4285 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, goaway->error_code);
4286 ASSERT_UINT_EQUALS(0, goaway->goaway_last_stream_id);
4287
4288 /* clean up */
4289 return s_tester_clean_up();
4290 }
4291
4292 TEST_CASE(h2_client_empty_initial_settings) {
4293 (void)ctx;
4294 aws_http_library_init(allocator);
4295
4296 s_tester.alloc = allocator;
4297
4298 struct aws_testing_channel_options options = {.clock_fn = aws_high_res_clock_get_ticks};
4299
4300 ASSERT_SUCCESS(testing_channel_init(&s_tester.testing_channel, allocator, &options));
4301
4302 /* empty initial settings */
4303 struct aws_http2_connection_options http2_options = {
4304 .on_initial_settings_completed = s_on_initial_settings_completed,
4305 .max_closed_streams = AWS_HTTP2_DEFAULT_MAX_CLOSED_STREAMS,
4306 .on_remote_settings_change = s_on_remote_settings_change,
4307 };
4308
4309 s_tester.connection = aws_http_connection_new_http2_client(
4310 allocator, false /* manual window management */, AWS_H2_INIT_WINDOW_SIZE, &http2_options);
4311 ASSERT_NOT_NULL(s_tester.connection);
4312
4313 {
4314 /* set connection user_data (handled by http-bootstrap in real world) */
4315 s_tester.connection->user_data = &s_tester.user_data;
4316 /* re-enact marriage vows of http-connection and channel (handled by http-bootstrap in real world) */
4317 struct aws_channel_slot *slot = aws_channel_slot_new(s_tester.testing_channel.channel);
4318 ASSERT_NOT_NULL(slot);
4319 ASSERT_SUCCESS(aws_channel_slot_insert_end(s_tester.testing_channel.channel, slot));
4320 ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, &s_tester.connection->channel_handler));
4321 s_tester.connection->vtable->on_channel_handler_installed(&s_tester.connection->channel_handler, slot);
4322 }
4323
4324 struct h2_fake_peer_options peer_options = {
4325 .alloc = allocator,
4326 .testing_channel = &s_tester.testing_channel,
4327 .is_server = true,
4328 };
4329 ASSERT_SUCCESS(h2_fake_peer_init(&s_tester.peer, &peer_options));
4330
4331 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4332
4333 /* shutdown the connection */
4334 h2_fake_peer_clean_up(&s_tester.peer);
4335 aws_http_connection_release(s_tester.connection);
4336 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4337 /* Check the callback has fired with error, after connection shutdown */
4338 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, s_tester.user_data.initial_settings_error_code);
4339 ASSERT_SUCCESS(testing_channel_clean_up(&s_tester.testing_channel));
4340 /* clean up */
4341 aws_http_library_clean_up();
4342 return AWS_OP_SUCCESS;
4343 }
4344
4345 TEST_CASE(h2_client_conn_failed_initial_settings_completed_not_invoked) {
4346 (void)ctx;
4347 aws_http_library_init(allocator);
4348
4349 s_tester.alloc = allocator;
4350 struct aws_http2_setting settings_array[] = {
4351 {.id = AWS_HTTP2_SETTINGS_ENABLE_PUSH, .value = 0},
4352 };
4353
4354 struct aws_http2_connection_options http2_options = {
4355 .initial_settings_array = settings_array,
4356 .num_initial_settings = AWS_ARRAY_SIZE(settings_array),
4357 .on_initial_settings_completed = s_on_initial_settings_completed,
4358 .max_closed_streams = AWS_HTTP2_DEFAULT_MAX_CLOSED_STREAMS,
4359 .on_remote_settings_change = s_on_remote_settings_change,
4360 };
4361 s_tester.connection = aws_http_connection_new_http2_client(
4362 allocator, false /* manual window management */, AWS_H2_INIT_WINDOW_SIZE, &http2_options);
4363 ASSERT_NOT_NULL(s_tester.connection);
4364 s_tester.user_data.initial_settings_error_code = INT32_MAX;
4365 {
4366 /* set connection user_data (handled by http-bootstrap in real world) */
4367 s_tester.connection->user_data = &s_tester.user_data;
4368 /* pretent the connection failed, and destroy the handler (handled by http-bootstrap in real world) */
4369 aws_channel_handler_destroy(&s_tester.connection->channel_handler);
4370 }
4371 /* Check callback has not fired and the error code is still INT32_MAX */
4372 ASSERT_INT_EQUALS(INT32_MAX, s_tester.user_data.initial_settings_error_code);
4373 /* clean up */
4374 aws_http_library_clean_up();
4375 return AWS_OP_SUCCESS;
4376 }
4377
4378 TEST_CASE(h2_client_stream_reset_stream) {
4379
4380 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4381
4382 /* get connection preface and acks out of the way */
4383 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4384 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4385 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4386
4387 /* send request */
4388 struct aws_http_message *request = aws_http2_message_new_request(allocator);
4389 ASSERT_NOT_NULL(request);
4390
4391 struct aws_http_header request_headers_src[] = {
4392 DEFINE_HEADER(":method", "GET"),
4393 DEFINE_HEADER(":scheme", "https"),
4394 DEFINE_HEADER(":path", "/"),
4395 };
4396 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
4397
4398 struct client_stream_tester stream_tester;
4399 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
4400 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4401 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4402 size_t frames_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
4403
4404 /* reset stream with no error */
4405 ASSERT_SUCCESS(aws_http2_stream_reset(stream_tester.stream, AWS_HTTP2_ERR_NO_ERROR));
4406 /* stream can only be reset once, the second reset will not fail but will be ignored */
4407 ASSERT_SUCCESS(aws_http2_stream_reset(stream_tester.stream, AWS_HTTP2_ERR_CANCEL));
4408
4409 /* validate that stream completed with error. */
4410 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4411 ASSERT_TRUE(stream_tester.complete);
4412 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_RST_STREAM_SENT, stream_tester.on_complete_error_code);
4413 /* a stream error should not affect the connection */
4414 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
4415 /* validate that stream sent only the first RST_STREAM */
4416 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4417 ASSERT_TRUE(frames_count + 1 == h2_decode_tester_frame_count(&s_tester.peer.decode));
4418 struct h2_decoded_frame *rst_stream_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
4419 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_RST_STREAM, rst_stream_frame->type);
4420 ASSERT_INT_EQUALS(AWS_HTTP2_ERR_NO_ERROR, rst_stream_frame->error_code);
4421
4422 /* clean up */
4423 aws_http_message_release(request);
4424 client_stream_tester_clean_up(&stream_tester);
4425 return s_tester_clean_up();
4426 }
4427
4428 TEST_CASE(h2_client_stream_reset_ignored_stream_closed) {
4429
4430 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4431
4432 /* get connection preface and acks out of the way */
4433 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4434 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4435 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4436
4437 /* send request */
4438 struct aws_http_message *request = aws_http2_message_new_request(allocator);
4439 ASSERT_NOT_NULL(request);
4440
4441 struct aws_http_header request_headers_src[] = {
4442 DEFINE_HEADER(":method", "GET"),
4443 DEFINE_HEADER(":scheme", "https"),
4444 DEFINE_HEADER(":path", "/"),
4445 };
4446 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
4447
4448 struct client_stream_tester stream_tester;
4449 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
4450 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4451 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
4452
4453 /* Request to reset stream after error has happened, still get success back, but the error code will be ignored */
4454 ASSERT_SUCCESS(aws_http2_stream_reset(stream_tester.stream, AWS_HTTP2_ERR_CANCEL));
4455 /* Before the async call finishes, an error happens and stream closed because of it */
4456 /* fake peer sends response body BEFORE any response headers, which leads to a error and stream will close */
4457 const char *body_src = "hello";
4458 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, body_src, true /*end_stream*/));
4459 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4460
4461 /* A possible race condition between "real error" and user request to reset stream in real life, which will lead to
4462 * possible different error code in rst_stream. User can aws_http2_stream_get_sent_reset_error_code to query the
4463 * error code we sent to peer. */
4464
4465 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
4466 ASSERT_TRUE(stream_tester.complete);
4467 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
4468 /* validate that stream sent RST_STREAM */
4469 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4470 struct h2_decoded_frame *rst_stream_frame =
4471 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, 0, NULL);
4472 /* But the error code is not the same as user was trying to send */
4473 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, rst_stream_frame->error_code);
4474
4475 /* clean up */
4476 aws_http_message_release(request);
4477 client_stream_tester_clean_up(&stream_tester);
4478 return s_tester_clean_up();
4479 }
4480
4481 TEST_CASE(h2_client_stream_reset_failed_before_activate_called) {
4482 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4483 /* get connection preface and acks out of the way */
4484 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4485 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4486 struct aws_http_message *request = aws_http2_message_new_request(allocator);
4487 ASSERT_NOT_NULL(request);
4488
4489 struct aws_http_header request_headers_src[] = {
4490 DEFINE_HEADER(":method", "GET"),
4491 DEFINE_HEADER(":scheme", "https"),
4492 DEFINE_HEADER(":path", "/"),
4493 };
4494 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
4495 struct aws_http_make_request_options request_options = {
4496 .self_size = sizeof(request_options),
4497 .request = request,
4498 };
4499 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
4500 ASSERT_NOT_NULL(stream);
4501
4502 /* reset will fail before activate called */
4503 ASSERT_FAILS(aws_http2_stream_reset(stream, AWS_HTTP2_ERR_NO_ERROR));
4504
4505 /* Once you activate the stream, you are able to reset it */
4506 ASSERT_SUCCESS(aws_http_stream_activate(stream));
4507 ASSERT_SUCCESS(aws_http2_stream_reset(stream, AWS_HTTP2_ERR_ENHANCE_YOUR_CALM));
4508
4509 /* validate rst_stream is sent */
4510 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4511 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4512 struct h2_decoded_frame *rst_stream_frame =
4513 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, 0, NULL);
4514 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_ENHANCE_YOUR_CALM, rst_stream_frame->error_code);
4515 /* clean up */
4516 aws_http_message_release(request);
4517 aws_http_stream_release(stream);
4518 return s_tester_clean_up();
4519 }
4520
4521 TEST_CASE(h2_client_stream_cancel_stream) {
4522 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4523 /* get connection preface and acks out of the way */
4524 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4525 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4526 struct aws_http_message *request = aws_http2_message_new_request(allocator);
4527 ASSERT_NOT_NULL(request);
4528
4529 struct aws_http_header request_headers_src[] = {
4530 DEFINE_HEADER(":method", "GET"),
4531 DEFINE_HEADER(":scheme", "https"),
4532 DEFINE_HEADER(":path", "/"),
4533 };
4534 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
4535 struct aws_http_make_request_options request_options = {
4536 .self_size = sizeof(request_options),
4537 .request = request,
4538 };
4539
4540 struct client_stream_tester stream_tester;
4541 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
4542 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4543
4544 /* Cancel the request */
4545 aws_http_stream_cancel_default_error(stream_tester.stream);
4546 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4547
4548 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
4549 ASSERT_TRUE(stream_tester.complete);
4550 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_STREAM_CANCELLED, stream_tester.on_complete_error_code);
4551 /* validate that stream sent RST_STREAM */
4552 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4553 struct h2_decoded_frame *rst_stream_frame =
4554 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, 0, NULL);
4555 /* But the error code is not the same as user was trying to send */
4556 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_CANCEL, rst_stream_frame->error_code);
4557
4558 /* clean up */
4559 aws_http_message_release(request);
4560 client_stream_tester_clean_up(&stream_tester);
4561 return s_tester_clean_up();
4562 }
4563
4564 TEST_CASE(h2_client_stream_keeps_alive_for_cross_thread_task) {
4565
4566 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4567
4568 /* get connection preface and acks out of the way */
4569 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4570 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4571 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4572
4573 /* send request */
4574 struct aws_http_message *request = aws_http2_message_new_request(allocator);
4575 ASSERT_NOT_NULL(request);
4576
4577 struct aws_http_header request_headers_src[] = {
4578 DEFINE_HEADER(":method", "GET"),
4579 DEFINE_HEADER(":scheme", "https"),
4580 DEFINE_HEADER(":path", "/"),
4581 };
4582 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
4583
4584 struct client_stream_tester stream_tester;
4585 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
4586 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4587 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
4588
4589 /* fake peer sends response */
4590 struct aws_http_header response_headers_src[] = {
4591 DEFINE_HEADER(":status", "404"),
4592 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
4593 };
4594
4595 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
4596 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
4597
4598 struct aws_h2_frame *response_frame =
4599 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
4600
4601 /* User reset the stream */
4602 ASSERT_SUCCESS(aws_http2_stream_reset(stream_tester.stream, AWS_HTTP2_ERR_ENHANCE_YOUR_CALM));
4603 /* Before the async call finishes, the stream completes */
4604 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
4605 /* And user releases the stream */
4606 aws_http_stream_release(stream_tester.stream);
4607
4608 /* Task should finish without error */
4609 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4610 ASSERT_TRUE(stream_tester.complete);
4611 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
4612 /* validate that no RST_STREAM sent */
4613 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4614 ASSERT_NULL(h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, 0, NULL));
4615
4616 /* clean up */
4617 aws_http_message_release(request);
4618 aws_http_headers_release(response_headers);
4619
4620 /* clean up stream_tester */
4621 for (size_t i = 0; i < stream_tester.num_info_responses; ++i) {
4622 aws_http_message_release(stream_tester.info_responses[i]);
4623 }
4624
4625 aws_http_headers_release(stream_tester.current_info_headers);
4626 aws_http_headers_release(stream_tester.response_headers);
4627 aws_http_headers_release(stream_tester.response_trailer);
4628 aws_byte_buf_clean_up(&stream_tester.response_body);
4629 return s_tester_clean_up();
4630 }
4631
4632 TEST_CASE(h2_client_stream_get_received_reset_error_code) {
4633
4634 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4635
4636 /* get connection preface and acks out of the way */
4637 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4638 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4639 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4640
4641 /* send request */
4642 struct aws_http_message *request = aws_http2_message_new_request(allocator);
4643 ASSERT_NOT_NULL(request);
4644
4645 struct aws_http_header request_headers_src[] = {
4646 DEFINE_HEADER(":method", "GET"),
4647 DEFINE_HEADER(":scheme", "https"),
4648 DEFINE_HEADER(":path", "/"),
4649 };
4650 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
4651
4652 struct client_stream_tester stream_tester;
4653 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
4654 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4655
4656 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
4657 uint32_t http2_error;
4658 /* Before rst_stream received, get function will fail */
4659 ASSERT_FAILS(aws_http2_stream_get_received_reset_error_code(stream_tester.stream, &http2_error));
4660
4661 /* fake peer sends RST_STREAM */
4662 struct aws_h2_frame *rst_stream =
4663 aws_h2_frame_new_rst_stream(allocator, stream_id, AWS_HTTP2_ERR_ENHANCE_YOUR_CALM);
4664 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, rst_stream));
4665 /* validate that stream completed with error */
4666 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4667 ASSERT_TRUE(stream_tester.complete);
4668 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_RST_STREAM_RECEIVED, stream_tester.on_complete_error_code);
4669
4670 /* After rst_stream received, and stream completed with RST_STREAM_RECEIVED, get function will get the error_code
4671 * received in rst_stream */
4672 ASSERT_SUCCESS(aws_http2_stream_get_received_reset_error_code(stream_tester.stream, &http2_error));
4673 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_ENHANCE_YOUR_CALM, http2_error);
4674 /* clean up */
4675 aws_http_message_release(request);
4676 client_stream_tester_clean_up(&stream_tester);
4677 return s_tester_clean_up();
4678 }
4679
4680 TEST_CASE(h2_client_stream_get_sent_reset_error_code) {
4681
4682 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4683
4684 /* get connection preface and acks out of the way */
4685 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4686 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4687 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4688
4689 /* send request */
4690 struct aws_http_message *request = aws_http2_message_new_request(allocator);
4691 ASSERT_NOT_NULL(request);
4692
4693 struct aws_http_header request_headers_src[] = {
4694 DEFINE_HEADER(":method", "GET"),
4695 DEFINE_HEADER(":scheme", "https"),
4696 DEFINE_HEADER(":path", "/"),
4697 };
4698 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
4699
4700 struct client_stream_tester stream_tester;
4701 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
4702 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4703 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
4704 uint32_t http2_error;
4705
4706 /* Before rst_stream sent, get function will fail */
4707 ASSERT_FAILS(aws_http2_stream_get_sent_reset_error_code(stream_tester.stream, &http2_error));
4708
4709 /* fake peer sends response body BEFORE any response headers, which leads to a error and stream will close */
4710 const char *body_src = "hello";
4711 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, body_src, true /*end_stream*/));
4712 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4713
4714 /* validate that stream completed with protocol error */
4715 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4716 ASSERT_TRUE(stream_tester.complete);
4717 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
4718
4719 /* Stream completed with error code, it's time to get what we sent */
4720 ASSERT_SUCCESS(aws_http2_stream_get_sent_reset_error_code(stream_tester.stream, &http2_error));
4721 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, http2_error);
4722
4723 /* validate that stream sent RST_STREAM */
4724 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4725 struct h2_decoded_frame *rst_stream_frame =
4726 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, 0, NULL);
4727 ASSERT_NOT_NULL(rst_stream_frame);
4728 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, rst_stream_frame->error_code);
4729
4730 /* clean up */
4731 aws_http_message_release(request);
4732 client_stream_tester_clean_up(&stream_tester);
4733 return s_tester_clean_up();
4734 }
4735
4736 TEST_CASE(h2_client_new_request_allowed) {
4737 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4738
4739 /* get connection preface and acks out of the way */
4740 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4741 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4742 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4743
4744 /* prepare request */
4745 struct aws_http_message *request = aws_http2_message_new_request(allocator);
4746 ASSERT_NOT_NULL(request);
4747
4748 struct aws_http_header headers[] = {
4749 DEFINE_HEADER(":method", "GET"),
4750 DEFINE_HEADER(":scheme", "https"),
4751 DEFINE_HEADER(":authority", "veryblackpage.com"),
4752 DEFINE_HEADER(":path", "/"),
4753 };
4754 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
4755
4756 struct aws_http_make_request_options options = {
4757 .self_size = sizeof(options),
4758 .request = request,
4759 };
4760
4761 /* validate the new request is allowed for now */
4762 ASSERT_TRUE(aws_http_connection_new_requests_allowed(s_tester.connection));
4763
4764 /* fake peer send a GOAWAY frame */
4765 uint32_t stream_id = 0;
4766 struct aws_byte_cursor debug_info;
4767 AWS_ZERO_STRUCT(debug_info);
4768 struct aws_h2_frame *peer_frame = aws_h2_frame_new_goaway(allocator, stream_id, AWS_HTTP2_ERR_NO_ERROR, debug_info);
4769 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
4770 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4771
4772 /* validate the new request is not allowed anymore when goaway received */
4773 ASSERT_FALSE(aws_http_connection_new_requests_allowed(s_tester.connection));
4774 /* Make new request will fail */
4775 ASSERT_NULL(aws_http_connection_make_request(s_tester.connection, &options));
4776 ASSERT_UINT_EQUALS(AWS_ERROR_HTTP_GOAWAY_RECEIVED, aws_last_error());
4777
4778 /* close connection */
4779 aws_http_connection_close(s_tester.connection);
4780 /* Make new request will fail */
4781 ASSERT_NULL(aws_http_connection_make_request(s_tester.connection, &options));
4782 ASSERT_UINT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, aws_last_error());
4783
4784 /* clean up */
4785 aws_http_message_release(request);
4786 return s_tester_clean_up();
4787 }
4788
4789 static void s_default_settings(struct aws_http2_setting settings[AWS_HTTP2_SETTINGS_COUNT]) {
4790 for (int i = AWS_HTTP2_SETTINGS_BEGIN_RANGE; i < AWS_HTTP2_SETTINGS_END_RANGE; i++) {
4791 /* settings range begin with 1, store them into 0-based array of aws_http2_setting */
4792 settings[i - 1].id = i;
4793 settings[i - 1].value = aws_h2_settings_initial[i];
4794 }
4795 }
4796
4797 static int s_apply_changed_settings(
4798 struct aws_http2_setting settings[AWS_HTTP2_SETTINGS_COUNT],
4799 struct aws_http2_setting *settings_to_change,
4800 int number_settings_to_change) {
4801
4802 for (int i = 0; i < number_settings_to_change; i++) {
4803 struct aws_http2_setting setting = settings_to_change[i];
4804 ASSERT_UINT_EQUALS(settings[setting.id - 1].id, setting.id);
4805 settings[setting.id - 1].value = setting.value;
4806 }
4807 return AWS_OP_SUCCESS;
4808 }
4809
4810 TEST_CASE(h2_client_send_multiple_goaway) {
4811
4812 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4813 /* get connection preface and acks out of the way */
4814 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4815 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4816
4817 struct aws_byte_buf info_buf = aws_byte_buf_from_c_str("this is a debug info");
4818 struct aws_byte_cursor debug_info = aws_byte_cursor_from_buf(&info_buf);
4819
4820 /* First graceful shutdown warning */
4821 aws_http2_connection_send_goaway(
4822 s_tester.connection, AWS_HTTP2_ERR_NO_ERROR, true /*allow_more_streams*/, &debug_info /*debug_data*/);
4823 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4824 /* Check the goaway frame received */
4825 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4826 struct h2_decoded_frame *latest_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
4827 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_NO_ERROR, latest_frame->error_code);
4828 ASSERT_UINT_EQUALS(AWS_H2_STREAM_ID_MAX, latest_frame->goaway_last_stream_id);
4829 ASSERT_TRUE(aws_byte_buf_eq_c_str(&latest_frame->data, "this is a debug info"));
4830
4831 /* Real GOAWAY */
4832 aws_http2_connection_send_goaway(
4833 s_tester.connection, AWS_HTTP2_ERR_PROTOCOL_ERROR, false /*allow_more_streams*/, &debug_info);
4834 /* It is fine to free the buffer right after the call, since we keep it in the connection's memory */
4835 aws_byte_buf_clean_up(&info_buf);
4836 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4837 /* Check the goaway frame received */
4838 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4839 latest_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
4840 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, latest_frame->error_code);
4841 ASSERT_UINT_EQUALS(0, latest_frame->goaway_last_stream_id);
4842 ASSERT_TRUE(aws_byte_buf_eq_c_str(&latest_frame->data, "this is a debug info"));
4843 size_t frames_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
4844
4845 /* Graceful shutdown warning after real GOAWAY will be ignored */
4846 aws_http2_connection_send_goaway(
4847 s_tester.connection, AWS_HTTP2_ERR_NO_ERROR, true /*allow_more_streams*/, NULL /*debug_data*/);
4848 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4849 /* Check the goaway frame received */
4850 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4851 ASSERT_UINT_EQUALS(frames_count, h2_decode_tester_frame_count(&s_tester.peer.decode));
4852
4853 /* clean up */
4854 return s_tester_clean_up();
4855 }
4856
4857 TEST_CASE(h2_client_get_sent_goaway) {
4858
4859 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4860 /* get connection preface and acks out of the way */
4861 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4862 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4863
4864 uint32_t last_stream_id;
4865 uint32_t http2_error;
4866 ASSERT_FAILS(aws_http2_connection_get_sent_goaway(s_tester.connection, &http2_error, &last_stream_id));
4867
4868 /* First graceful shutdown warning */
4869 aws_http2_connection_send_goaway(
4870 s_tester.connection, AWS_HTTP2_ERR_NO_ERROR, true /*allow_more_streams*/, NULL /*debug_data*/);
4871 /* User send goaway asynchronously, you are not able to get the sent goaway right after the call */
4872 ASSERT_FAILS(aws_http2_connection_get_sent_goaway(s_tester.connection, &http2_error, &last_stream_id));
4873
4874 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4875 ASSERT_SUCCESS(aws_http2_connection_get_sent_goaway(s_tester.connection, &http2_error, &last_stream_id));
4876 ASSERT_UINT_EQUALS(AWS_H2_STREAM_ID_MAX, last_stream_id);
4877 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_NO_ERROR, http2_error);
4878
4879 /* Second graceful shutdown warning, with non-zero error. Well it's not against the law, just do what user wants */
4880 aws_http2_connection_send_goaway(
4881 s_tester.connection, AWS_HTTP2_ERR_ENHANCE_YOUR_CALM, true /*allow_more_streams*/, NULL /*debug_data*/);
4882 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4883 ASSERT_SUCCESS(aws_http2_connection_get_sent_goaway(s_tester.connection, &http2_error, &last_stream_id));
4884 ASSERT_UINT_EQUALS(AWS_H2_STREAM_ID_MAX, last_stream_id);
4885 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_ENHANCE_YOUR_CALM, http2_error);
4886
4887 struct aws_byte_cursor opaque_data = aws_byte_cursor_from_c_str("12345678");
4888 /* peer send extra ping ack will lead to a connection error and goaway will be sent */
4889 struct aws_h2_frame *peer_frame = aws_h2_frame_new_ping(allocator, true /*ACK*/, opaque_data.ptr);
4890 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
4891 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4892 /* Check the sent goaway */
4893 ASSERT_SUCCESS(aws_http2_connection_get_sent_goaway(s_tester.connection, &http2_error, &last_stream_id));
4894 ASSERT_UINT_EQUALS(0, last_stream_id);
4895 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, http2_error);
4896
4897 /* clean up */
4898 return s_tester_clean_up();
4899 }
4900
4901 TEST_CASE(h2_client_get_received_goaway) {
4902
4903 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4904 /* get connection preface and acks out of the way */
4905 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4906 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
4907
4908 uint32_t last_stream_id;
4909 uint32_t http2_error;
4910
4911 /* you are not able to get the received goaway if no GOAWAY received */
4912 ASSERT_FAILS(aws_http2_connection_get_received_goaway(s_tester.connection, &http2_error, &last_stream_id));
4913
4914 /* fake peer send goaway */
4915 const char debug_string[] = "Error, Core Dump 0XFFFFFFFF";
4916 struct aws_byte_cursor debug_info = aws_byte_cursor_from_c_str(debug_string);
4917 struct aws_h2_frame *peer_frame =
4918 aws_h2_frame_new_goaway(allocator, AWS_H2_STREAM_ID_MAX, AWS_HTTP2_ERR_NO_ERROR, debug_info);
4919 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
4920 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4921 /* Try to get the received goaway */
4922 ASSERT_SUCCESS(aws_http2_connection_get_received_goaway(s_tester.connection, &http2_error, &last_stream_id));
4923 ASSERT_UINT_EQUALS(AWS_H2_STREAM_ID_MAX, last_stream_id);
4924 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_NO_ERROR, http2_error);
4925
4926 /* clean up */
4927 return s_tester_clean_up();
4928 }
4929
4930 TEST_CASE(h2_client_get_local_settings) {
4931
4932 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4933 struct aws_http2_setting settings_get[AWS_HTTP2_SETTINGS_COUNT];
4934 struct aws_http2_setting settings_expected[AWS_HTTP2_SETTINGS_COUNT];
4935 s_default_settings(settings_expected);
4936 aws_http2_connection_get_local_settings(s_tester.connection, settings_get);
4937 /* Altough we disabled the push_promise at the initial settings, but without the settings ACK from peer, the
4938 * settings we are using locally is still the default settings */
4939 ASSERT_SUCCESS(s_compare_settings_array(settings_expected, settings_get, AWS_HTTP2_SETTINGS_COUNT));
4940
4941 /* fake peer sends connection preface */
4942 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4943 /* fake peer sends settings ack back for the initial settings. */
4944 struct aws_h2_frame *peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
4945 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
4946 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4947 /* Set expected setting */
4948 settings_expected[AWS_HTTP2_SETTINGS_ENABLE_PUSH - 1].value = false;
4949 /* Initial settings got ACK by peer, know we will get the settings with push_promise disabled */
4950 aws_http2_connection_get_local_settings(s_tester.connection, settings_get);
4951 ASSERT_SUCCESS(s_compare_settings_array(settings_expected, settings_get, AWS_HTTP2_SETTINGS_COUNT));
4952
4953 /* Request to change the local settings */
4954 struct aws_http2_setting settings_array[] = {
4955 {.id = AWS_HTTP2_SETTINGS_ENABLE_PUSH, .value = 1},
4956 {.id = AWS_HTTP2_SETTINGS_HEADER_TABLE_SIZE, .value = 0},
4957 {.id = AWS_HTTP2_SETTINGS_HEADER_TABLE_SIZE, .value = 1000},
4958 {.id = AWS_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, .value = 1},
4959 {.id = AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE, .value = 1},
4960 {.id = AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE, .value = AWS_H2_PAYLOAD_MAX},
4961 {.id = AWS_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE, .value = 1},
4962 };
4963
4964 ASSERT_SUCCESS(aws_http2_connection_change_settings(
4965 s_tester.connection, settings_array, AWS_ARRAY_SIZE(settings_array), NULL /*call_back*/, NULL /*user_data*/));
4966 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4967 /* Settings sent, but not ACKed, still the same settings, we will get */
4968 aws_http2_connection_get_local_settings(s_tester.connection, settings_get);
4969 ASSERT_SUCCESS(s_compare_settings_array(settings_expected, settings_get, AWS_HTTP2_SETTINGS_COUNT));
4970
4971 /* Peer ACKed the settings */
4972 peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
4973 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
4974 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
4975 /* Set expected setting */
4976 ASSERT_SUCCESS(s_apply_changed_settings(settings_expected, settings_array, AWS_ARRAY_SIZE(settings_array)));
4977 aws_http2_connection_get_local_settings(s_tester.connection, settings_get);
4978 ASSERT_SUCCESS(s_compare_settings_array(settings_expected, settings_get, AWS_HTTP2_SETTINGS_COUNT));
4979 /* clean up */
4980 return s_tester_clean_up();
4981 }
4982
4983 TEST_CASE(h2_client_get_remote_settings) {
4984
4985 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
4986 struct aws_http2_setting settings_get[AWS_HTTP2_SETTINGS_COUNT];
4987 struct aws_http2_setting settings_expected[AWS_HTTP2_SETTINGS_COUNT];
4988 s_default_settings(settings_expected);
4989 /* Once connection setup and no settings from peer, remote settings will be default init settings */
4990 aws_http2_connection_get_remote_settings(s_tester.connection, settings_get);
4991 ASSERT_SUCCESS(s_compare_settings_array(settings_expected, settings_get, AWS_HTTP2_SETTINGS_COUNT));
4992
4993 /* fake peer sends connection preface */
4994 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
4995 /* fake peer sends settings and change the remote settings */
4996 struct aws_http2_setting settings_array[] = {
4997 {.id = AWS_HTTP2_SETTINGS_ENABLE_PUSH, .value = 1},
4998 {.id = AWS_HTTP2_SETTINGS_HEADER_TABLE_SIZE, .value = 0},
4999 {.id = AWS_HTTP2_SETTINGS_HEADER_TABLE_SIZE, .value = 1000},
5000 {.id = AWS_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, .value = 1},
5001 {.id = AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE, .value = 1},
5002 {.id = AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE, .value = AWS_H2_PAYLOAD_MAX},
5003 {.id = AWS_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE, .value = 1},
5004 };
5005
5006 struct aws_h2_frame *settings_frame =
5007 aws_h2_frame_new_settings(allocator, settings_array, AWS_ARRAY_SIZE(settings_array), false /*ack*/);
5008 ASSERT_NOT_NULL(settings_frame);
5009 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, settings_frame));
5010 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5011 /* Set expected setting */
5012 ASSERT_SUCCESS(s_apply_changed_settings(settings_expected, settings_array, AWS_ARRAY_SIZE(settings_array)));
5013 aws_http2_connection_get_remote_settings(s_tester.connection, settings_get);
5014 ASSERT_SUCCESS(s_compare_settings_array(settings_expected, settings_get, AWS_HTTP2_SETTINGS_COUNT));
5015
5016 /* clean up */
5017 return s_tester_clean_up();
5018 }
5019
5020 /* User apis that want to add stuff into connection.synced_data will fail after connection shutdown starts */
5021 TEST_CASE(h2_client_request_apis_failed_after_connection_begin_shutdown) {
5022
5023 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
5024 /* get connection preface and acks out of the way */
5025 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
5026 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5027 struct aws_http_message *request = aws_http2_message_new_request(allocator);
5028 ASSERT_NOT_NULL(request);
5029
5030 struct aws_http_header request_headers_src[] = {
5031 DEFINE_HEADER(":method", "GET"),
5032 DEFINE_HEADER(":scheme", "https"),
5033 DEFINE_HEADER(":path", "/"),
5034 };
5035 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
5036 struct aws_http_make_request_options request_options = {
5037 .self_size = sizeof(request_options),
5038 .request = request,
5039 };
5040 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
5041 ASSERT_NOT_NULL(stream);
5042 /* close the connection */
5043 aws_http_connection_close(s_tester.connection);
5044
5045 /* Send goaway will silently do nothing as the connection already closed */
5046 aws_http2_connection_send_goaway(
5047 s_tester.connection, AWS_HTTP2_ERR_NO_ERROR, false /*allow_more_streams*/, NULL /*debug_data*/);
5048 /* validate all those user apis to add stuff into synced data will fail */
5049 ASSERT_FAILS(aws_http_stream_activate(stream));
5050 ASSERT_FAILS(aws_http2_connection_change_settings(
5051 s_tester.connection, NULL, 0, NULL /*callback function*/, NULL /*user_data*/));
5052 ASSERT_FAILS(aws_http2_connection_ping(s_tester.connection, NULL, NULL /*callback function*/, NULL /*user_data*/));
5053
5054 /* clean up */
5055 aws_http_message_release(request);
5056 aws_http_stream_release(stream);
5057 return s_tester_clean_up();
5058 }
5059
5060 enum request_callback {
5061 REQUEST_CALLBACK_OUTGOING_BODY,
5062 REQUEST_CALLBACK_INCOMING_HEADERS,
5063 REQUEST_CALLBACK_INCOMING_HEADERS_DONE,
5064 REQUEST_CALLBACK_INCOMING_BODY,
5065 REQUEST_CALLBACK_COMPLETE,
5066 REQUEST_CALLBACK_COUNT,
5067 };
5068
5069 struct error_from_callback_tester {
5070 struct aws_input_stream base;
5071 enum request_callback error_at;
5072 int callback_counts[REQUEST_CALLBACK_COUNT];
5073 bool has_errored;
5074 struct aws_stream_status status;
5075 int on_complete_error_code;
5076 };
5077
5078 static const int ERROR_FROM_CALLBACK_ERROR_CODE = (int)0xBEEFCAFE;
5079
5080 static int s_error_from_callback_common(
5081 struct error_from_callback_tester *error_tester,
5082 enum request_callback current_callback) {
5083
5084 error_tester->callback_counts[current_callback]++;
5085
5086 /* After error code returned, no more callbacks should fire (except for on_complete) */
5087 AWS_FATAL_ASSERT(!error_tester->has_errored);
5088 AWS_FATAL_ASSERT(current_callback <= error_tester->error_at);
5089 if (current_callback == error_tester->error_at) {
5090 error_tester->has_errored = true;
5091 return aws_raise_error(ERROR_FROM_CALLBACK_ERROR_CODE);
5092 }
5093
5094 return AWS_OP_SUCCESS;
5095 }
5096
5097 static int s_error_from_outgoing_body_read(struct aws_input_stream *body, struct aws_byte_buf *dest) {
5098
5099 (void)dest;
5100
5101 struct error_from_callback_tester *error_tester = AWS_CONTAINER_OF(body, struct error_from_callback_tester, base);
5102 if (s_error_from_callback_common(error_tester, REQUEST_CALLBACK_OUTGOING_BODY)) {
5103 return AWS_OP_ERR;
5104 }
5105
5106 /* If the common fn was successful, write out some data and end the stream */
5107 ASSERT_TRUE(aws_byte_buf_write(dest, (const uint8_t *)"abcd", 4));
5108 error_tester->status.is_end_of_stream = true;
5109 return AWS_OP_SUCCESS;
5110 }
5111
5112 static int s_error_from_outgoing_body_get_status(struct aws_input_stream *body, struct aws_stream_status *status) {
5113 struct error_from_callback_tester *error_tester = AWS_CONTAINER_OF(body, struct error_from_callback_tester, base);
5114 *status = error_tester->status;
5115 return AWS_OP_SUCCESS;
5116 }
5117
5118 static void s_error_from_outgoing_body_destroy(void *stream) {
5119 /* allocated from stack, nothing to do */
5120 (void)stream;
5121 }
5122 static struct aws_input_stream_vtable s_error_from_outgoing_body_vtable = {
5123 .seek = NULL,
5124 .read = s_error_from_outgoing_body_read,
5125 .get_status = s_error_from_outgoing_body_get_status,
5126 .get_length = NULL,
5127 };
5128
5129 static int s_error_from_incoming_headers(
5130 struct aws_http_stream *stream,
5131 enum aws_http_header_block header_block,
5132 const struct aws_http_header *header_array,
5133 size_t num_headers,
5134 void *user_data) {
5135
5136 (void)stream;
5137 (void)header_block;
5138 (void)header_array;
5139 (void)num_headers;
5140 return s_error_from_callback_common(user_data, REQUEST_CALLBACK_INCOMING_HEADERS);
5141 }
5142
5143 static int s_error_from_incoming_headers_done(
5144 struct aws_http_stream *stream,
5145 enum aws_http_header_block header_block,
5146 void *user_data) {
5147 (void)stream;
5148 (void)header_block;
5149 return s_error_from_callback_common(user_data, REQUEST_CALLBACK_INCOMING_HEADERS_DONE);
5150 }
5151
5152 static int s_error_from_incoming_body(
5153 struct aws_http_stream *stream,
5154 const struct aws_byte_cursor *data,
5155 void *user_data) {
5156
5157 (void)stream;
5158 (void)data;
5159 return s_error_from_callback_common(user_data, REQUEST_CALLBACK_INCOMING_BODY);
5160 }
5161
5162 static void s_error_tester_on_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
5163 (void)stream;
5164 struct error_from_callback_tester *error_tester = user_data;
5165 error_tester->callback_counts[REQUEST_CALLBACK_COMPLETE]++;
5166 error_tester->on_complete_error_code = error_code;
5167 }
5168
5169 static int s_test_error_from_callback(struct aws_allocator *allocator, void *ctx, enum request_callback error_at) {
5170
5171 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
5172 /* get connection preface and acks out of the way */
5173 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
5174 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5175 /* send request */
5176 struct aws_http_message *request = aws_http2_message_new_request(allocator);
5177 ASSERT_NOT_NULL(request);
5178
5179 struct aws_http_header request_headers_src[] = {
5180 DEFINE_HEADER(":method", "GET"),
5181 DEFINE_HEADER(":scheme", "https"),
5182 DEFINE_HEADER(":path", "/"),
5183 };
5184 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
5185
5186 struct error_from_callback_tester error_tester = {
5187 .error_at = error_at,
5188 .status =
5189 {
5190 .is_valid = true,
5191 .is_end_of_stream = false,
5192 },
5193 };
5194 error_tester.base.vtable = &s_error_from_outgoing_body_vtable;
5195 aws_ref_count_init(&error_tester.base.ref_count, &error_tester, s_error_from_outgoing_body_destroy);
5196
5197 aws_http_message_set_body_stream(request, &error_tester.base);
5198
5199 struct aws_http_make_request_options opt = {
5200 .self_size = sizeof(opt),
5201 .request = request,
5202 .on_response_headers = s_error_from_incoming_headers,
5203 .on_response_header_block_done = s_error_from_incoming_headers_done,
5204 .on_response_body = s_error_from_incoming_body,
5205 .on_complete = s_error_tester_on_stream_complete,
5206 .user_data = &error_tester,
5207 };
5208 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5209
5210 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &opt);
5211 ASSERT_NOT_NULL(stream);
5212 ASSERT_SUCCESS(aws_http_stream_activate(stream));
5213
5214 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5215
5216 /* Ensure the request can be destroyed after request is sent */
5217 aws_http_message_release(opt.request);
5218
5219 /* fake peer sends response headers */
5220 struct aws_http_header response_headers_src[] = {
5221 DEFINE_HEADER(":status", "200"),
5222 DEFINE_HEADER("date", "Fri, 01 Mar 2019 17:18:55 GMT"),
5223 };
5224
5225 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
5226 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
5227 uint32_t stream_id = aws_http_stream_get_id(stream);
5228 struct aws_h2_frame *response_frame =
5229 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
5230 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
5231
5232 struct aws_byte_buf response_body_bufs;
5233 size_t body_length = 5;
5234
5235 /* fake peer sends a DATA frame larger than the window size we have */
5236 ASSERT_SUCCESS(aws_byte_buf_init(&response_body_bufs, allocator, body_length));
5237 ASSERT_TRUE(aws_byte_buf_write_u8_n(&response_body_bufs, (uint8_t)'a', body_length));
5238 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&response_body_bufs);
5239 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, true /*end_stream*/));
5240
5241 /* validate that stream completed with error */
5242 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5243
5244 /* check that callbacks were invoked before error_at, but not after */
5245 for (int i = 0; i < REQUEST_CALLBACK_COMPLETE; ++i) {
5246 if (i <= error_at) {
5247 ASSERT_TRUE(error_tester.callback_counts[i] > 0);
5248 } else {
5249 ASSERT_INT_EQUALS(0, error_tester.callback_counts[i]);
5250 }
5251 }
5252
5253 /* validate the RST_STREAM sent and connection is still open */
5254 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5255 struct h2_decoded_frame *rst_stream_frame =
5256 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, 0, NULL);
5257 ASSERT_NOT_NULL(rst_stream_frame);
5258 ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_INTERNAL_ERROR, rst_stream_frame->error_code);
5259 ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
5260
5261 /* the on_complete callback should always fire though, and should receive the proper error_code */
5262 ASSERT_INT_EQUALS(1, error_tester.callback_counts[REQUEST_CALLBACK_COMPLETE]);
5263 ASSERT_INT_EQUALS(ERROR_FROM_CALLBACK_ERROR_CODE, error_tester.on_complete_error_code);
5264
5265 aws_http_headers_release(response_headers);
5266 aws_byte_buf_clean_up(&response_body_bufs);
5267 aws_http_stream_release(stream);
5268 return s_tester_clean_up();
5269 }
5270
5271 TEST_CASE(h2_client_error_from_outgoing_body_callback_reset_stream) {
5272 (void)ctx;
5273 ASSERT_SUCCESS(s_test_error_from_callback(allocator, ctx, REQUEST_CALLBACK_OUTGOING_BODY));
5274 return AWS_OP_SUCCESS;
5275 }
5276
5277 TEST_CASE(h2_client_error_from_incoming_headers_callback_reset_stream) {
5278 (void)ctx;
5279 ASSERT_SUCCESS(s_test_error_from_callback(allocator, ctx, REQUEST_CALLBACK_INCOMING_HEADERS));
5280 return AWS_OP_SUCCESS;
5281 }
5282
5283 TEST_CASE(h2_client_error_from_incoming_headers_done_callback_reset_stream) {
5284 (void)ctx;
5285 ASSERT_SUCCESS(s_test_error_from_callback(allocator, ctx, REQUEST_CALLBACK_INCOMING_HEADERS_DONE));
5286 return AWS_OP_SUCCESS;
5287 }
5288
5289 TEST_CASE(h2_client_error_from_incoming_body_callback_reset_stream) {
5290 (void)ctx;
5291 ASSERT_SUCCESS(s_test_error_from_callback(allocator, ctx, REQUEST_CALLBACK_INCOMING_BODY));
5292 return AWS_OP_SUCCESS;
5293 }
5294
5295 struct h2_client_manual_data_write_ctx {
5296 struct aws_allocator *allocator;
5297 struct aws_byte_buf data;
5298 int complete_error_code;
5299 };
5300
5301 static struct aws_input_stream *s_h2_client_manual_data_write_generate_data(
5302 struct h2_client_manual_data_write_ctx *ctx) {
5303 struct aws_byte_cursor data = aws_byte_cursor_from_buf(&ctx->data);
5304 data.len = aws_max_size(rand() % ctx->data.capacity, 1);
5305 return aws_input_stream_new_from_cursor(ctx->allocator, &data);
5306 }
5307
5308 TEST_CASE(h2_client_manual_data_write) {
5309
5310 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
5311 /* get connection preface and acks out of the way */
5312 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
5313 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5314 size_t frame_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
5315
5316 struct aws_http_message *request = aws_http2_message_new_request(allocator);
5317 ASSERT_NOT_NULL(request);
5318
5319 struct aws_http_header request_headers_src[] = {
5320 DEFINE_HEADER(":method", "GET"),
5321 DEFINE_HEADER(":scheme", "https"),
5322 DEFINE_HEADER(":path", "/"),
5323 };
5324 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
5325 struct aws_http_make_request_options request_options = {
5326 .self_size = sizeof(request_options),
5327 .request = request,
5328 .http2_use_manual_data_writes = true,
5329 };
5330 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
5331 ASSERT_NOT_NULL(stream);
5332
5333 aws_http_stream_activate(stream);
5334 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5335 uint32_t stream_id = aws_http_stream_get_id(stream);
5336
5337 struct aws_byte_buf payload;
5338 aws_byte_buf_init(&payload, allocator, 1024);
5339
5340 struct h2_client_manual_data_write_ctx test_ctx = {
5341 .allocator = allocator,
5342 .data = payload,
5343 };
5344 size_t total_length = 0;
5345
5346 /* Simulate writes coming in over time */
5347 for (int idx = 0; idx < 1000; ++idx) {
5348 struct aws_input_stream *data_stream = s_h2_client_manual_data_write_generate_data(&test_ctx);
5349 int64_t stream_length = 0;
5350 ASSERT_SUCCESS(aws_input_stream_get_length(data_stream, &stream_length));
5351 total_length += (size_t)stream_length;
5352 struct aws_http2_stream_write_data_options write = {
5353 .data = data_stream,
5354 .on_complete = NULL,
5355 .user_data = NULL,
5356 };
5357 ASSERT_SUCCESS(aws_http2_stream_write_data(stream, &write));
5358 /* fake peer sends WINDOW_UPDATE */
5359 struct aws_h2_frame *peer_frame = aws_h2_frame_new_window_update(allocator, stream_id, (uint32_t)stream_length);
5360 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
5361 /* Connection level window update */
5362 peer_frame = aws_h2_frame_new_window_update(allocator, 0, (uint32_t)stream_length);
5363 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
5364 if (idx % 10 == 0) {
5365 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5366 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5367 }
5368 aws_input_stream_release(data_stream);
5369 }
5370 struct aws_http2_stream_write_data_options last_write = {.end_stream = true};
5371
5372 ASSERT_SUCCESS(aws_http2_stream_write_data(stream, &last_write));
5373
5374 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5375 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5376 size_t frame_count2 = h2_decode_tester_frame_count(&s_tester.peer.decode);
5377 /* Peer should received header frame without end_stream and mutiple data frames and combined payload length should
5378 * be the same as total length sent. */
5379 struct h2_decoded_frame *header_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frame_count);
5380 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_HEADERS, header_frame->type);
5381 ASSERT_FALSE(header_frame->end_stream);
5382 size_t received_length = 0;
5383 for (size_t i = frame_count + 1; i < frame_count2; i++) {
5384 struct h2_decoded_frame *data_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, i);
5385 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_DATA, data_frame->type);
5386 received_length += data_frame->data_payload_len;
5387 if (i == frame_count2 - 1) {
5388 ASSERT_TRUE(data_frame->end_stream);
5389 } else {
5390 ASSERT_FALSE(data_frame->end_stream);
5391 }
5392 }
5393 ASSERT_UINT_EQUALS(received_length, total_length);
5394
5395 aws_http_message_release(request);
5396 aws_http_stream_release(stream);
5397
5398 /* close the connection */
5399 aws_http_connection_close(s_tester.connection);
5400
5401 aws_byte_buf_clean_up(&test_ctx.data);
5402
5403 /* clean up */
5404 return s_tester_clean_up();
5405 }
5406
5407 static void s_http_stream_write_complete_fn(struct aws_http_stream *stream, int error_code, void *user_data) {
5408 (void)stream;
5409 int *ctx = (int *)user_data;
5410 *ctx = error_code;
5411 }
5412
5413 TEST_CASE(h2_client_manual_data_write_read_broken) {
5414
5415 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
5416 /* get connection preface and acks out of the way */
5417 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
5418 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5419
5420 struct aws_http_message *request = aws_http2_message_new_request(allocator);
5421 ASSERT_NOT_NULL(request);
5422
5423 struct aws_http_header request_headers_src[] = {
5424 DEFINE_HEADER(":method", "POST"),
5425 DEFINE_HEADER(":scheme", "https"),
5426 DEFINE_HEADER(":path", "/"),
5427 };
5428 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
5429 struct client_stream_tester stream_tester;
5430
5431 struct client_stream_tester_options options = {
5432 .request = request,
5433 .connection = s_tester.connection,
5434 .http2_manual_write = true,
5435 };
5436 ASSERT_SUCCESS(client_stream_tester_init(&stream_tester, s_tester.alloc, &options));
5437 struct aws_http_stream *stream = stream_tester.stream;
5438 ASSERT_NOT_NULL(stream);
5439
5440 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5441
5442 struct aws_input_stream *data_stream = aws_input_stream_new_tester(allocator, aws_byte_cursor_from_c_str("abcd"));
5443 aws_input_stream_tester_set_reading_broken(data_stream, true);
5444 int error_code = 0;
5445 struct aws_http2_stream_write_data_options write = {
5446 .data = data_stream,
5447 .on_complete = s_http_stream_write_complete_fn,
5448 .user_data = &error_code,
5449 };
5450
5451 ASSERT_SUCCESS(aws_http2_stream_write_data(stream, &write));
5452 aws_input_stream_release(data_stream);
5453
5454 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5455 ASSERT_TRUE(stream_tester.complete);
5456 /* The stream complete will get the error code from the input stream read. */
5457 ASSERT_UINT_EQUALS(stream_tester.on_complete_error_code, AWS_IO_STREAM_READ_FAILED);
5458 /* The write triggers the error, which should be reported to the write complete */
5459 ASSERT_UINT_EQUALS(error_code, AWS_IO_STREAM_READ_FAILED);
5460
5461 aws_http_message_release(request);
5462
5463 /* close the connection */
5464 aws_http_connection_close(s_tester.connection);
5465 client_stream_tester_clean_up(&stream_tester);
5466
5467 /* clean up */
5468 return s_tester_clean_up();
5469 }
5470
5471 TEST_CASE(h2_client_manual_data_write_not_enabled) {
5472
5473 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
5474
5475 struct aws_http_message *request = aws_http2_message_new_request(allocator);
5476 ASSERT_NOT_NULL(request);
5477
5478 struct aws_http_header request_headers_src[] = {
5479 DEFINE_HEADER(":method", "GET"),
5480 DEFINE_HEADER(":scheme", "https"),
5481 DEFINE_HEADER(":path", "/"),
5482 };
5483 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
5484 struct aws_http_make_request_options request_options = {
5485 .self_size = sizeof(request_options),
5486 .request = request,
5487 .http2_use_manual_data_writes = false,
5488 };
5489 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
5490 ASSERT_NOT_NULL(stream);
5491
5492 aws_http_stream_activate(stream);
5493
5494 struct aws_byte_buf payload;
5495 aws_byte_buf_init(&payload, allocator, 1024);
5496
5497 struct h2_client_manual_data_write_ctx test_ctx = {
5498 .allocator = allocator,
5499 .data = payload,
5500 };
5501
5502 /* Try writing the data */
5503 struct aws_input_stream *data_stream = s_h2_client_manual_data_write_generate_data(&test_ctx);
5504 int64_t stream_length = 0;
5505 ASSERT_SUCCESS(aws_input_stream_get_length(data_stream, &stream_length));
5506 struct aws_http2_stream_write_data_options write_options = {
5507 .data = data_stream,
5508 };
5509 ASSERT_ERROR(AWS_ERROR_HTTP_MANUAL_WRITE_NOT_ENABLED, aws_http2_stream_write_data(stream, &write_options));
5510 aws_input_stream_release(data_stream);
5511 aws_http_message_release(request);
5512 aws_http_stream_release(stream);
5513
5514 /* close the connection */
5515 aws_http_connection_close(s_tester.connection);
5516
5517 aws_byte_buf_clean_up(&test_ctx.data);
5518
5519 /* clean up */
5520 return s_tester_clean_up();
5521 }
5522
5523 TEST_CASE(h2_client_manual_data_write_with_body) {
5524
5525 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
5526 /* get connection preface and acks out of the way */
5527 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
5528 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5529 size_t frame_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
5530
5531 struct aws_http_message *request = aws_http2_message_new_request(allocator);
5532 ASSERT_NOT_NULL(request);
5533
5534 struct aws_http_header request_headers_src[] = {
5535 DEFINE_HEADER(":method", "GET"),
5536 DEFINE_HEADER(":scheme", "https"),
5537 DEFINE_HEADER(":path", "/"),
5538 };
5539 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
5540 struct aws_http_make_request_options request_options = {
5541 .self_size = sizeof(request_options),
5542 .request = request,
5543 .http2_use_manual_data_writes = true,
5544 };
5545 size_t total_length = 0;
5546
5547 /* set request body */
5548 const char *body_src = "hello";
5549 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
5550 struct aws_input_stream *request_body = aws_input_stream_new_from_cursor(allocator, &body_cursor);
5551 aws_http_message_set_body_stream(request, request_body);
5552 int64_t body_length = 0;
5553 ASSERT_SUCCESS(aws_input_stream_get_length(request_body, &body_length));
5554 total_length += (size_t)body_length;
5555 aws_input_stream_release(request_body);
5556
5557 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
5558 ASSERT_NOT_NULL(stream);
5559
5560 aws_http_stream_activate(stream);
5561 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5562 uint32_t stream_id = aws_http_stream_get_id(stream);
5563
5564 struct aws_byte_buf payload;
5565 aws_byte_buf_init(&payload, allocator, 1024);
5566
5567 struct h2_client_manual_data_write_ctx test_ctx = {
5568 .allocator = allocator,
5569 .data = payload,
5570 };
5571
5572 /* Simulate writes coming in over time */
5573 for (int idx = 0; idx < 1000; ++idx) {
5574 struct aws_input_stream *data_stream = s_h2_client_manual_data_write_generate_data(&test_ctx);
5575 int64_t stream_length = 0;
5576 ASSERT_SUCCESS(aws_input_stream_get_length(data_stream, &stream_length));
5577 total_length += (size_t)stream_length;
5578 struct aws_http2_stream_write_data_options write = {
5579 .data = data_stream,
5580 .on_complete = NULL,
5581 .user_data = NULL,
5582 };
5583 ASSERT_SUCCESS(aws_http2_stream_write_data(stream, &write));
5584 /* fake peer sends WINDOW_UPDATE */
5585 struct aws_h2_frame *peer_frame = aws_h2_frame_new_window_update(allocator, stream_id, (uint32_t)stream_length);
5586 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
5587 /* Connection level window update */
5588 peer_frame = aws_h2_frame_new_window_update(allocator, 0, (uint32_t)stream_length);
5589 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
5590 if (idx % 10 == 0) {
5591 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5592 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5593 }
5594 aws_input_stream_release(data_stream);
5595 }
5596 struct aws_http2_stream_write_data_options last_write = {.end_stream = true};
5597
5598 ASSERT_SUCCESS(aws_http2_stream_write_data(stream, &last_write));
5599
5600 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5601 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5602 size_t frame_count2 = h2_decode_tester_frame_count(&s_tester.peer.decode);
5603 /* Peer should received header frame without end_stream and mutiple data frames and combined payload length should
5604 * be the same as total length sent. */
5605 struct h2_decoded_frame *header_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frame_count);
5606 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_HEADERS, header_frame->type);
5607 ASSERT_FALSE(header_frame->end_stream);
5608 size_t received_length = 0;
5609 for (size_t i = frame_count + 1; i < frame_count2; i++) {
5610 struct h2_decoded_frame *data_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, i);
5611 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_DATA, data_frame->type);
5612 received_length += data_frame->data_payload_len;
5613 if (i == frame_count2 - 1) {
5614 ASSERT_TRUE(data_frame->end_stream);
5615 } else {
5616 ASSERT_FALSE(data_frame->end_stream);
5617 }
5618 }
5619 ASSERT_UINT_EQUALS(received_length, total_length);
5620
5621 aws_http_message_release(request);
5622 aws_http_stream_release(stream);
5623
5624 /* close the connection */
5625 aws_http_connection_close(s_tester.connection);
5626
5627 aws_byte_buf_clean_up(&test_ctx.data);
5628
5629 /* clean up */
5630 return s_tester_clean_up();
5631 }
5632
5633 TEST_CASE(h2_client_manual_data_write_no_data) {
5634
5635 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
5636 /* get connection preface and acks out of the way */
5637 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
5638 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5639 size_t frame_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
5640
5641 struct aws_http_message *request = aws_http2_message_new_request(allocator);
5642 ASSERT_NOT_NULL(request);
5643
5644 struct aws_http_header request_headers_src[] = {
5645 DEFINE_HEADER(":method", "GET"),
5646 DEFINE_HEADER(":scheme", "https"),
5647 DEFINE_HEADER(":path", "/"),
5648 };
5649 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
5650 struct aws_http_make_request_options request_options = {
5651 .self_size = sizeof(request_options),
5652 .request = request,
5653 .http2_use_manual_data_writes = true,
5654 };
5655 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
5656 ASSERT_NOT_NULL(stream);
5657
5658 aws_http_stream_activate(stream);
5659
5660 struct aws_http2_stream_write_data_options last_write = {.end_stream = true};
5661 ASSERT_SUCCESS(aws_http2_stream_write_data(stream, &last_write));
5662
5663 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5664 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5665 size_t frame_count_2 = h2_decode_tester_frame_count(&s_tester.peer.decode);
5666 /* Peer should received header frame without end_stream and empty data frame with end_stream */
5667 ASSERT_UINT_EQUALS(frame_count + 2, frame_count_2);
5668 struct h2_decoded_frame *header_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frame_count);
5669 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_HEADERS, header_frame->type);
5670 ASSERT_FALSE(header_frame->end_stream);
5671 struct h2_decoded_frame *empty_data_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frame_count + 1);
5672 ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_DATA, empty_data_frame->type);
5673 ASSERT_UINT_EQUALS(0, empty_data_frame->data_payload_len);
5674 ASSERT_TRUE(empty_data_frame->end_stream);
5675 aws_http_message_release(request);
5676 aws_http_stream_release(stream);
5677
5678 /* close the connection */
5679 aws_http_connection_close(s_tester.connection);
5680
5681 /* clean up */
5682 return s_tester_clean_up();
5683 }
5684
5685 static void s_on_manual_data_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
5686 (void)stream;
5687 struct h2_client_manual_data_write_ctx *test_ctx = (struct h2_client_manual_data_write_ctx *)user_data;
5688 test_ctx->complete_error_code = error_code;
5689 }
5690
5691 /* Close the connection before finishes writing data */
5692 TEST_CASE(h2_client_manual_data_write_connection_close) {
5693
5694 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
5695 /* get connection preface and acks out of the way */
5696 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
5697 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5698
5699 struct aws_http_message *request = aws_http2_message_new_request(allocator);
5700 ASSERT_NOT_NULL(request);
5701
5702 struct aws_http_header request_headers_src[] = {
5703 DEFINE_HEADER(":method", "GET"),
5704 DEFINE_HEADER(":scheme", "https"),
5705 DEFINE_HEADER(":path", "/"),
5706 };
5707 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
5708
5709 struct aws_byte_buf payload;
5710 aws_byte_buf_init(&payload, allocator, 1024);
5711
5712 struct h2_client_manual_data_write_ctx test_ctx = {
5713 .allocator = allocator,
5714 .data = payload,
5715 };
5716
5717 struct aws_http_make_request_options request_options = {
5718 .self_size = sizeof(request_options),
5719 .request = request,
5720 .http2_use_manual_data_writes = true,
5721 .on_complete = s_on_manual_data_stream_complete,
5722 .user_data = &test_ctx,
5723 };
5724 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
5725 ASSERT_NOT_NULL(stream);
5726
5727 struct aws_input_stream *data_stream = s_h2_client_manual_data_write_generate_data(&test_ctx);
5728 struct aws_http2_stream_write_data_options write = {
5729 .data = data_stream,
5730 .on_complete = NULL,
5731 .user_data = NULL,
5732 };
5733 /* Cannot write before activate the stream */
5734 ASSERT_FAILS(aws_http2_stream_write_data(stream, &write));
5735 aws_http_stream_activate(stream);
5736 ASSERT_SUCCESS(aws_http2_stream_write_data(stream, &write));
5737
5738 /* close connection */
5739 aws_http_connection_close(s_tester.connection);
5740
5741 ASSERT_SUCCESS(aws_http2_stream_write_data(stream, &write));
5742
5743 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5744 /* Cannot write after stream closed */
5745 ASSERT_FAILS(aws_http2_stream_write_data(stream, &write));
5746
5747 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, test_ctx.complete_error_code);
5748
5749 aws_http_message_release(request);
5750 aws_http_stream_release(stream);
5751
5752 /* clean up */
5753 aws_byte_buf_clean_up(&test_ctx.data);
5754 aws_input_stream_release(data_stream);
5755 return s_tester_clean_up();
5756 }
5757
5758 /* Initialize the tester to test on the behavior that window update will be batched */
5759 static int s_batch_window_management_tester_init(
5760 struct aws_allocator *alloc,
5761 bool manual_conn,
5762 bool manual_stream,
5763 uint32_t conn_threshold,
5764 uint32_t stream_threshold,
5765 uint32_t initial_window_size,
5766 void *ctx) {
5767 (void)ctx;
5768 aws_http_library_init(alloc);
5769
5770 s_tester.alloc = alloc;
5771
5772 struct aws_testing_channel_options options = {.clock_fn = aws_high_res_clock_get_ticks};
5773
5774 ASSERT_SUCCESS(testing_channel_init(&s_tester.testing_channel, alloc, &options));
5775 struct aws_http2_setting settings_array[] = {
5776 {.id = AWS_HTTP2_SETTINGS_ENABLE_PUSH, .value = 0},
5777 };
5778
5779 struct aws_http2_connection_options http2_options = {
5780 .initial_settings_array = settings_array,
5781 .num_initial_settings = AWS_ARRAY_SIZE(settings_array),
5782 .max_closed_streams = AWS_HTTP2_DEFAULT_MAX_CLOSED_STREAMS,
5783 .conn_manual_window_management = manual_conn,
5784 .conn_window_size_threshold_to_send_update = conn_threshold,
5785 .stream_window_size_threshold_to_send_update = stream_threshold,
5786 };
5787
5788 s_tester.connection = aws_http_connection_new_http2_client(
5789 alloc, manual_stream /* manual window management */, initial_window_size, &http2_options);
5790 ASSERT_NOT_NULL(s_tester.connection);
5791
5792 { /* re-enact marriage vows of http-connection and channel (handled by http-bootstrap in real world) */
5793 struct aws_channel_slot *slot = aws_channel_slot_new(s_tester.testing_channel.channel);
5794 ASSERT_NOT_NULL(slot);
5795 ASSERT_SUCCESS(aws_channel_slot_insert_end(s_tester.testing_channel.channel, slot));
5796 ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, &s_tester.connection->channel_handler));
5797 s_tester.connection->vtable->on_channel_handler_installed(&s_tester.connection->channel_handler, slot);
5798 }
5799
5800 struct h2_fake_peer_options peer_options = {
5801 .alloc = alloc,
5802 .testing_channel = &s_tester.testing_channel,
5803 .is_server = true,
5804 };
5805 ASSERT_SUCCESS(h2_fake_peer_init(&s_tester.peer, &peer_options));
5806
5807 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5808 return AWS_OP_SUCCESS;
5809 }
5810
5811 TEST_CASE(h2_client_batch_auto_window_update) {
5812 /* Automated and default threshold */
5813 ASSERT_SUCCESS(s_batch_window_management_tester_init(
5814 allocator,
5815 false /*manual_conn*/,
5816 false /*manual_stream*/,
5817 0 /*conn_threshold*/,
5818 0 /*stream_threshold*/,
5819 0 /*initial_window_size*/,
5820 ctx));
5821 /* fake peer sends connection preface */
5822 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
5823 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5824 struct aws_h2_frame *peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
5825 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
5826 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5827 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5828 /* Get the initial frames out of the way. */
5829 size_t start_index = h2_decode_tester_frame_count(&s_tester.peer.decode);
5830
5831 /* send request */
5832 struct aws_http_message *request = aws_http2_message_new_request(allocator);
5833 ASSERT_NOT_NULL(request);
5834
5835 struct aws_http_header request_headers_src[] = {
5836 DEFINE_HEADER(":method", "GET"),
5837 DEFINE_HEADER(":scheme", "https"),
5838 DEFINE_HEADER(":path", "/"),
5839 };
5840 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
5841
5842 struct client_stream_tester stream_tester;
5843 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
5844 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5845 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
5846
5847 /* fake peer sends response headers */
5848 struct aws_http_header response_headers_src[] = {
5849 DEFINE_HEADER(":status", "200"),
5850 };
5851
5852 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
5853 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
5854
5855 struct aws_h2_frame *response_frame =
5856 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
5857 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
5858
5859 /* The other end sents half the initial window size to get the window below he threshold and trigger the window
5860 * update from client. */
5861 /* The threshold is floor(AWS_H2_INIT_WINDOW_SIZE/2), so we need the window size to drop below the threshold.
5862 * So, we need at least AWS_H2_INIT_WINDOW_SIZE - AWS_H2_INIT_WINDOW_SIZE/2 + 1 */
5863 size_t total_data_size = AWS_H2_INIT_WINDOW_SIZE - AWS_H2_INIT_WINDOW_SIZE / 2 + 1;
5864
5865 /* The frame size has the limit from channel and the protocol level. */
5866 size_t max_frame_data_size =
5867 aws_min_size(aws_h2_settings_initial[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE], g_aws_channel_max_fragment_size) -
5868 AWS_H2_FRAME_PREFIX_SIZE - 1;
5869 /* number of bodies peer will send, just to ensure the connection flow-control window will not be blocked when we
5870 * manually update it */
5871 size_t sent_data = 0;
5872
5873 /* Make sure we have multiple data frames. */
5874 ASSERT_TRUE(total_data_size > max_frame_data_size);
5875 while (sent_data < total_data_size) {
5876 size_t body_size = aws_min_size(total_data_size - sent_data, max_frame_data_size);
5877 sent_data += body_size;
5878 struct aws_byte_buf body_buf;
5879 ASSERT_SUCCESS(aws_byte_buf_init(&body_buf, allocator, body_size));
5880 ASSERT_TRUE(aws_byte_buf_write_u8_n(&body_buf, (uint8_t)'a', body_size));
5881 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&body_buf);
5882 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, false /*end_stream*/));
5883 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5884 aws_byte_buf_clean_up(&body_buf);
5885 }
5886
5887 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5888 /* Check that only one window update frame sent for the stream and no window update frame sent for the connection,
5889 * since client batches the window update to skip the small increment. */
5890 struct h2_decoded_frame *connection_window_update_frame = h2_decode_tester_find_stream_frame(
5891 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, 0 /*stream_id*/, start_index /*idx*/, NULL);
5892 ASSERT_NULL(connection_window_update_frame);
5893
5894 size_t stream_window_update_index = 0;
5895 struct h2_decoded_frame *stream_window_update_frame = h2_decode_tester_find_stream_frame(
5896 &s_tester.peer.decode,
5897 AWS_H2_FRAME_T_WINDOW_UPDATE,
5898 1 /*stream_id*/,
5899 start_index /*idx*/,
5900 &stream_window_update_index);
5901 ASSERT_NOT_NULL(stream_window_update_frame);
5902 ASSERT_UINT_EQUALS(total_data_size, stream_window_update_frame->window_size_increment);
5903
5904 stream_window_update_frame = h2_decode_tester_find_stream_frame(
5905 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, 1 /*stream_id*/, stream_window_update_index + 1, NULL);
5906 /* No more found */
5907 ASSERT_NULL(stream_window_update_frame);
5908
5909 aws_http_message_release(request);
5910 aws_http_headers_release(response_headers);
5911 client_stream_tester_clean_up(&stream_tester);
5912 return s_tester_clean_up();
5913 }
5914
5915 TEST_CASE(h2_client_batch_manual_window_update) {
5916 /* Automated and default threshold */
5917 uint32_t stream_initial_window_size = 100;
5918 ASSERT_SUCCESS(s_batch_window_management_tester_init(
5919 allocator,
5920 true /*manual_conn*/,
5921 true /*manual_stream*/,
5922 UINT32_MAX /*conn_threshold, make sure every connection level update will be sent*/,
5923 0 /*stream_threshold, use the default as half of the initial window size*/,
5924 stream_initial_window_size /*initial_window_size*/,
5925 ctx));
5926 /* fake peer sends connection preface */
5927 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
5928 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5929 struct aws_h2_frame *peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
5930 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
5931 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5932 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5933 /* Get the initial frames out of the way. */
5934 size_t start_index = h2_decode_tester_frame_count(&s_tester.peer.decode);
5935
5936 /* send request */
5937 struct aws_http_message *request = aws_http2_message_new_request(allocator);
5938 ASSERT_NOT_NULL(request);
5939
5940 struct aws_http_header request_headers_src[] = {
5941 DEFINE_HEADER(":method", "GET"),
5942 DEFINE_HEADER(":scheme", "https"),
5943 DEFINE_HEADER(":path", "/"),
5944 };
5945 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
5946
5947 struct client_stream_tester stream_tester;
5948 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
5949 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5950 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
5951
5952 /* fake peer sends response headers */
5953 struct aws_http_header response_headers_src[] = {
5954 DEFINE_HEADER(":status", "200"),
5955 };
5956
5957 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
5958 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
5959
5960 struct aws_h2_frame *response_frame =
5961 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
5962 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
5963
5964 /* Sent 3 data frame with manual window update from user */
5965 size_t body_size = stream_initial_window_size / 4;
5966 /* number of bodies peer will send, just to ensure the connection flow-control window will not be blocked when we
5967 * manually update it */
5968
5969 /* Make sure we have multiple data frames. */
5970 size_t num_data_frames = 3;
5971 for (size_t i = 0; i < num_data_frames; i++) {
5972 /* Client update the window manually before receive the data, since once the data received, the client will
5973 * start to send out window updates if needed. */
5974 aws_http_stream_update_window(stream_tester.stream, body_size);
5975 aws_http2_connection_update_window(s_tester.connection, (uint32_t)body_size);
5976 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5977 struct aws_byte_buf body_buf;
5978 ASSERT_SUCCESS(aws_byte_buf_init(&body_buf, allocator, body_size));
5979 ASSERT_TRUE(aws_byte_buf_write_u8_n(&body_buf, (uint8_t)'a', body_size));
5980 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&body_buf);
5981 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, false /*end_stream*/));
5982 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5983 aws_byte_buf_clean_up(&body_buf);
5984 }
5985
5986 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
5987 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
5988 /* Check that only one window update frame sent for the stream and 3 window update frame sent for the connection */
5989 size_t conn_window_update_index = start_index;
5990 struct h2_decoded_frame *connection_window_update_frame;
5991 for (size_t i = 0; i < num_data_frames; i++) {
5992 connection_window_update_frame = h2_decode_tester_find_stream_frame(
5993 &s_tester.peer.decode,
5994 AWS_H2_FRAME_T_WINDOW_UPDATE,
5995 0 /*stream_id*/,
5996 conn_window_update_index + 1 /*idx*/,
5997 &conn_window_update_index);
5998 ASSERT_NOT_NULL(connection_window_update_frame);
5999 ASSERT_UINT_EQUALS(body_size, connection_window_update_frame->window_size_increment);
6000 }
6001 /* There shall be no more */
6002 connection_window_update_frame = h2_decode_tester_find_stream_frame(
6003 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, 0 /*stream_id*/, conn_window_update_index + 1, NULL);
6004 ASSERT_NULL(connection_window_update_frame);
6005
6006 /* Only one stream window update should be found. */
6007 size_t stream_window_update_index = 0;
6008 struct h2_decoded_frame *stream_window_update_frame = h2_decode_tester_find_stream_frame(
6009 &s_tester.peer.decode,
6010 AWS_H2_FRAME_T_WINDOW_UPDATE,
6011 1 /*stream_id*/,
6012 start_index /*idx*/,
6013 &stream_window_update_index);
6014 ASSERT_NOT_NULL(stream_window_update_frame);
6015 ASSERT_UINT_EQUALS(num_data_frames * body_size, stream_window_update_frame->window_size_increment);
6016
6017 stream_window_update_frame = h2_decode_tester_find_stream_frame(
6018 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, 1 /*stream_id*/, stream_window_update_index + 1, NULL);
6019 /* No more found */
6020 ASSERT_NULL(stream_window_update_frame);
6021
6022 aws_http_message_release(request);
6023 aws_http_headers_release(response_headers);
6024 client_stream_tester_clean_up(&stream_tester);
6025 return s_tester_clean_up();
6026 }
6027
6028 /* Test that on_h2_remote_end_stream fires when remote peer sends END_STREAM */
6029 TEST_CASE(h2_client_on_h2_remote_end_stream_fires) {
6030 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
6031
6032 /* get connection preface and acks out of the way */
6033 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
6034 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6035
6036 /* send request */
6037 struct aws_http_message *request = aws_http2_message_new_request(allocator);
6038 ASSERT_NOT_NULL(request);
6039
6040 struct aws_http_header request_headers_src[] = {
6041 DEFINE_HEADER(":method", "GET"),
6042 DEFINE_HEADER(":scheme", "https"),
6043 DEFINE_HEADER(":path", "/"),
6044 };
6045 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
6046
6047 bool track_on_h2_remote_end_stream = true;
6048 struct client_stream_tester_options tester_options = {
6049 .request = request,
6050 .connection = s_tester.connection,
6051 .on_h2_remote_end_stream = &track_on_h2_remote_end_stream,
6052 };
6053 struct client_stream_tester stream_tester;
6054 ASSERT_SUCCESS(client_stream_tester_init(&stream_tester, allocator, &tester_options));
6055
6056 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6057 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
6058
6059 /* fake peer sends response with END_STREAM */
6060 struct aws_http_header response_headers_src[] = {
6061 DEFINE_HEADER(":status", "200"),
6062 };
6063
6064 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
6065 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
6066
6067 struct aws_h2_frame *response_frame =
6068 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
6069 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
6070
6071 /* validate that both callbacks fired */
6072 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6073 ASSERT_TRUE(stream_tester.on_h2_remote_end_stream_invoked);
6074 ASSERT_TRUE(stream_tester.complete);
6075 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
6076
6077 /* clean up */
6078 aws_http_headers_release(response_headers);
6079 aws_http_message_release(request);
6080 client_stream_tester_clean_up(&stream_tester);
6081 return s_tester_clean_up();
6082 }
6083
6084 /* Test that on_h2_remote_end_stream does NOT fire when RST_STREAM is received */
6085 TEST_CASE(h2_client_on_h2_remote_end_stream_not_fired_on_rst_stream) {
6086 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
6087
6088 /* get connection preface and acks out of the way */
6089 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
6090 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6091
6092 /* send request */
6093 struct aws_http_message *request = aws_http2_message_new_request(allocator);
6094 ASSERT_NOT_NULL(request);
6095
6096 struct aws_http_header request_headers_src[] = {
6097 DEFINE_HEADER(":method", "GET"),
6098 DEFINE_HEADER(":scheme", "https"),
6099 DEFINE_HEADER(":path", "/"),
6100 };
6101 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
6102
6103 bool track_on_h2_remote_end_stream = true;
6104 struct client_stream_tester_options tester_options = {
6105 .request = request,
6106 .connection = s_tester.connection,
6107 .on_h2_remote_end_stream = &track_on_h2_remote_end_stream,
6108 };
6109 struct client_stream_tester stream_tester;
6110 ASSERT_SUCCESS(client_stream_tester_init(&stream_tester, allocator, &tester_options));
6111
6112 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6113 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
6114
6115 /* fake peer sends RST_STREAM instead of END_STREAM */
6116 struct aws_h2_frame *rst_frame = aws_h2_frame_new_rst_stream(allocator, stream_id, AWS_HTTP2_ERR_INTERNAL_ERROR);
6117 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, rst_frame));
6118
6119 /* validate that on_h2_remote_end_stream did NOT fire, but on_complete did */
6120 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6121 ASSERT_FALSE(stream_tester.on_h2_remote_end_stream_invoked);
6122 ASSERT_TRUE(stream_tester.complete);
6123 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_RST_STREAM_RECEIVED, stream_tester.on_complete_error_code);
6124
6125 /* clean up */
6126 aws_http_message_release(request);
6127 client_stream_tester_clean_up(&stream_tester);
6128 return s_tester_clean_up();
6129 }
6130
6131 /* Test early server response (server sends END_STREAM before client finishes) */
6132 TEST_CASE(h2_client_on_h2_remote_end_stream_early_server_response) {
6133 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
6134
6135 /* get connection preface and acks out of the way */
6136 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
6137 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6138
6139 /* create request with body that will stall */
6140 struct aws_http_message *request = aws_http2_message_new_request(allocator);
6141 ASSERT_NOT_NULL(request);
6142
6143 struct aws_http_header request_headers_src[] = {
6144 DEFINE_HEADER(":method", "POST"),
6145 DEFINE_HEADER(":scheme", "https"),
6146 DEFINE_HEADER(":path", "/upload"),
6147 };
6148 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
6149
6150 const char *body = "request body data";
6151 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body);
6152 struct aws_input_stream *body_stream = aws_input_stream_new_tester(allocator, body_cursor);
6153 aws_input_stream_tester_set_max_bytes_per_read(body_stream, 1); /* Stall the body */
6154 aws_http_message_set_body_stream(request, body_stream);
6155
6156 bool track_on_h2_remote_end_stream = true;
6157 struct client_stream_tester_options tester_options = {
6158 .request = request,
6159 .connection = s_tester.connection,
6160 .on_h2_remote_end_stream = &track_on_h2_remote_end_stream,
6161 };
6162 struct client_stream_tester stream_tester;
6163 ASSERT_SUCCESS(client_stream_tester_init(&stream_tester, allocator, &tester_options));
6164
6165 /* Execute 1 event-loop tick, HEADERS should be sent but body stalled */
6166 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
6167 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
6168
6169 /* fake peer sends complete response while client is still sending */
6170 struct aws_http_header response_headers_src[] = {
6171 DEFINE_HEADER(":status", "200"),
6172 };
6173
6174 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
6175 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
6176
6177 struct aws_h2_frame *response_frame =
6178 aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
6179 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
6180
6181 testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
6182
6183 /* At this point, on_h2_remote_end_stream should have fired, but on_complete should NOT (client still sending) */
6184 ASSERT_TRUE(stream_tester.on_h2_remote_end_stream_invoked);
6185 ASSERT_FALSE(stream_tester.complete);
6186
6187 /* Now let client finish sending */
6188 aws_input_stream_tester_set_max_bytes_per_read(body_stream, SIZE_MAX);
6189 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6190
6191 /* Now on_complete should have fired */
6192 ASSERT_TRUE(stream_tester.complete);
6193 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
6194
6195 /* clean up */
6196 aws_http_headers_release(response_headers);
6197 aws_http_message_release(request);
6198 aws_input_stream_release(body_stream);
6199 client_stream_tester_clean_up(&stream_tester);
6200 return s_tester_clean_up();
6201 }
6202
6203 /* Test that on_h2_remote_end_stream works with body data */
6204 TEST_CASE(h2_client_on_h2_remote_end_stream_with_body_data) {
6205 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
6206
6207 /* get connection preface and acks out of the way */
6208 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
6209 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6210
6211 /* send request */
6212 struct aws_http_message *request = aws_http2_message_new_request(allocator);
6213 ASSERT_NOT_NULL(request);
6214
6215 struct aws_http_header request_headers_src[] = {
6216 DEFINE_HEADER(":method", "GET"),
6217 DEFINE_HEADER(":scheme", "https"),
6218 DEFINE_HEADER(":path", "/"),
6219 };
6220 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
6221
6222 bool track_on_h2_remote_end_stream = true;
6223 struct client_stream_tester_options tester_options = {
6224 .request = request,
6225 .connection = s_tester.connection,
6226 .on_h2_remote_end_stream = &track_on_h2_remote_end_stream,
6227 };
6228 struct client_stream_tester stream_tester;
6229 ASSERT_SUCCESS(client_stream_tester_init(&stream_tester, allocator, &tester_options));
6230
6231 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6232 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
6233
6234 /* fake peer sends response headers */
6235 struct aws_http_header response_headers_src[] = {
6236 DEFINE_HEADER(":status", "200"),
6237 };
6238
6239 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
6240 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
6241
6242 struct aws_h2_frame *headers_frame =
6243 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
6244 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, headers_frame));
6245
6246 /* fake peer sends body data */
6247 const char *body = "response body";
6248 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, body, false /*end_stream*/));
6249
6250 /* fake peer sends final data frame with END_STREAM */
6251 ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, " more data", true /*end_stream*/));
6252
6253 /* validate that both callbacks fired */
6254 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6255 ASSERT_TRUE(stream_tester.on_h2_remote_end_stream_invoked);
6256 ASSERT_TRUE(stream_tester.complete);
6257 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
6258
6259 /* clean up */
6260 aws_http_headers_release(response_headers);
6261 aws_http_message_release(request);
6262 client_stream_tester_clean_up(&stream_tester);
6263 return s_tester_clean_up();
6264 }
6265
6266 /* The overflow window update will be capped to the allowed max to be sent. */
6267 TEST_CASE(h2_client_cap_manual_window_update) {
6268 /* Automated and default threshold */
6269 uint32_t stream_initial_window_size = 100;
6270 ASSERT_SUCCESS(s_batch_window_management_tester_init(
6271 allocator,
6272 true /*manual_conn*/,
6273 true /*manual_stream*/,
6274 UINT32_MAX /*conn_threshold, make sure every connection level update will be sent*/,
6275 0 /*stream_threshold, use the default as half of the initial window size*/,
6276 stream_initial_window_size /*initial_window_size*/,
6277 ctx));
6278 /* fake peer sends connection preface */
6279 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
6280 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6281 struct aws_h2_frame *peer_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
6282 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
6283 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6284 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
6285 /* Get the initial frames out of the way. */
6286 size_t start_index = h2_decode_tester_frame_count(&s_tester.peer.decode);
6287
6288 /* send request */
6289 struct aws_http_message *request = aws_http2_message_new_request(allocator);
6290 ASSERT_NOT_NULL(request);
6291
6292 struct aws_http_header request_headers_src[] = {
6293 DEFINE_HEADER(":method", "GET"),
6294 DEFINE_HEADER(":scheme", "https"),
6295 DEFINE_HEADER(":path", "/"),
6296 };
6297 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
6298
6299 struct client_stream_tester stream_tester;
6300 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
6301 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6302 uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
6303
6304 /* fake peer sends response headers */
6305 struct aws_http_header response_headers_src[] = {
6306 DEFINE_HEADER(":status", "200"),
6307 };
6308
6309 struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
6310 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
6311
6312 struct aws_h2_frame *response_frame =
6313 aws_h2_frame_new_headers(allocator, stream_id, response_headers, false /*end_stream*/, 0, NULL);
6314 ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
6315
6316 /* User wants to update the connection window to overflow */
6317 size_t allowed_max_window_update = INT32_MAX - AWS_H2_INIT_WINDOW_SIZE;
6318 aws_http2_connection_update_window(s_tester.connection, (uint32_t)allowed_max_window_update + 100);
6319 /* update stream window to max */
6320 aws_http_stream_update_window(stream_tester.stream, SIZE_MAX);
6321 /* The other side should received the allowed max for the connection, since it's below the threshold */
6322 /* But no window update received for the stream, since the threshold is not hit. */
6323 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6324 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
6325
6326 size_t conn_window_update_index = start_index;
6327 struct h2_decoded_frame *connection_window_update_frame = h2_decode_tester_find_stream_frame(
6328 &s_tester.peer.decode,
6329 AWS_H2_FRAME_T_WINDOW_UPDATE,
6330 0 /*stream_id*/,
6331 conn_window_update_index + 1 /*idx*/,
6332 &conn_window_update_index);
6333 ASSERT_NOT_NULL(connection_window_update_frame);
6334 ASSERT_UINT_EQUALS(allowed_max_window_update, connection_window_update_frame->window_size_increment);
6335 /* The user requested window update still has more left, so everytime the window shrink, it will update the window
6336 * automatically, until all the requested window update has been sent */
6337 size_t stream_window_update_index = 0;
6338 struct h2_decoded_frame *stream_window_update_frame = h2_decode_tester_find_stream_frame(
6339 &s_tester.peer.decode,
6340 AWS_H2_FRAME_T_WINDOW_UPDATE,
6341 1 /*stream_id*/,
6342 start_index /*idx*/,
6343 &stream_window_update_index);
6344 ASSERT_NULL(stream_window_update_frame);
6345
6346 /* Sent 3 data frame with manual window update from user */
6347 size_t body_size = 50;
6348 size_t num_data_frames = 3;
6349 for (size_t i = 0; i < num_data_frames; i++) {
6350 struct aws_byte_buf body_buf;
6351 ASSERT_SUCCESS(aws_byte_buf_init(&body_buf, allocator, body_size));
6352 ASSERT_TRUE(aws_byte_buf_write_u8_n(&body_buf, (uint8_t)'a', body_size));
6353 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&body_buf);
6354 ASSERT_SUCCESS(h2_fake_peer_send_data_frame(&s_tester.peer, stream_id, body_cursor, false /*end_stream*/));
6355 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6356 aws_byte_buf_clean_up(&body_buf);
6357 /* No more window update invoked */
6358 }
6359
6360 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6361 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
6362 /* Check that only one window update frame sent for the stream and 2 more window update frame sent for the
6363 * connection to fit the overflow.
6364 */
6365 for (size_t i = 0; i < 2; i++) {
6366 connection_window_update_frame = h2_decode_tester_find_stream_frame(
6367 &s_tester.peer.decode,
6368 AWS_H2_FRAME_T_WINDOW_UPDATE,
6369 0 /*stream_id*/,
6370 conn_window_update_index + 1 /*idx*/,
6371 &conn_window_update_index);
6372 ASSERT_NOT_NULL(connection_window_update_frame);
6373 ASSERT_UINT_EQUALS(body_size, connection_window_update_frame->window_size_increment);
6374 }
6375 /* There shall be no more */
6376 connection_window_update_frame = h2_decode_tester_find_stream_frame(
6377 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, 0 /*stream_id*/, conn_window_update_index + 1, NULL);
6378 ASSERT_NULL(connection_window_update_frame);
6379
6380 /* Only one stream window update should be found. */
6381 stream_window_update_frame = h2_decode_tester_find_stream_frame(
6382 &s_tester.peer.decode,
6383 AWS_H2_FRAME_T_WINDOW_UPDATE,
6384 1 /*stream_id*/,
6385 start_index /*idx*/,
6386 &stream_window_update_index);
6387 ASSERT_NOT_NULL(stream_window_update_frame);
6388 /* The max allowed size should be max - current and the current there should be zero. */
6389 ASSERT_UINT_EQUALS(INT32_MAX, stream_window_update_frame->window_size_increment);
6390
6391 stream_window_update_frame = h2_decode_tester_find_stream_frame(
6392 &s_tester.peer.decode, AWS_H2_FRAME_T_WINDOW_UPDATE, 1 /*stream_id*/, stream_window_update_index + 1, NULL);
6393 /* No more found */
6394 ASSERT_NULL(stream_window_update_frame);
6395
6396 aws_http_message_release(request);
6397 aws_http_headers_release(response_headers);
6398 client_stream_tester_clean_up(&stream_tester);
6399 return s_tester_clean_up();
6400 }
6401
6402 /* Test unified API works for H2 */
6403 TEST_CASE(h2_client_unified_write_data_api) {
6404 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
6405 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
6406 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
6407
6408 struct aws_http_message *request = aws_http2_message_new_request(allocator);
6409 ASSERT_NOT_NULL(request);
6410
6411 struct aws_http_header request_headers_src[] = {
6412 DEFINE_HEADER(":method", "POST"),
6413 DEFINE_HEADER(":scheme", "https"),
6414 DEFINE_HEADER(":path", "/upload"),
6415 };
6416 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
6417
6418 struct aws_http_make_request_options request_options = {
6419 .self_size = sizeof(request_options),
6420 .request = request,
6421 .http2_use_manual_data_writes = true,
6422 };
6423 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
6424 ASSERT_NOT_NULL(stream);
6425
6426 aws_http_stream_activate(stream);
6427 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6428
6429 struct aws_byte_cursor data = aws_byte_cursor_from_c_str("hello");
6430 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &data);
6431 ASSERT_NOT_NULL(input_stream);
6432
6433 struct aws_http_stream_write_data_options write_options = {
6434 .data = input_stream,
6435 .end_stream = true,
6436 };
6437
6438 ASSERT_SUCCESS(aws_http_stream_write_data(stream, &write_options));
6439 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6440
6441 uint32_t stream_id = aws_http_stream_get_id(stream);
6442 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
6443 ASSERT_NOT_NULL(
6444 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_HEADERS, 0 /*search_start_idx*/, NULL));
6445 ASSERT_SUCCESS(
6446 h2_decode_tester_check_data_str_across_frames(&s_tester.peer.decode, stream_id, "hello", true /*end_stream*/));
6447
6448 aws_input_stream_release(input_stream);
6449 aws_http_message_release(request);
6450 aws_http_stream_release(stream);
6451 aws_http_connection_close(s_tester.connection);
6452
6453 return s_tester_clean_up();
6454 }
6455
6456 /* Test: H2 write_data using the new use_manual_data_writes field (not the deprecated http2_use_manual_data_writes) */
6457 TEST_CASE(h2_client_unified_write_data_api_new_field) {
6458 ASSERT_SUCCESS(s_tester_init(allocator, ctx));
6459 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
6460 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
6461
6462 struct aws_http_message *request = aws_http2_message_new_request(allocator);
6463 ASSERT_NOT_NULL(request);
6464
6465 struct aws_http_header request_headers_src[] = {
6466 DEFINE_HEADER(":method", "POST"),
6467 DEFINE_HEADER(":scheme", "https"),
6468 DEFINE_HEADER(":path", "/upload"),
6469 };
6470 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
6471
6472 struct aws_http_make_request_options request_options = {
6473 .self_size = sizeof(request_options),
6474 .request = request,
6475 .use_manual_data_writes = true,
6476 };
6477 struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &request_options);
6478 ASSERT_NOT_NULL(stream);
6479
6480 aws_http_stream_activate(stream);
6481 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6482
6483 struct aws_byte_cursor data = aws_byte_cursor_from_c_str("hello");
6484 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &data);
6485 ASSERT_NOT_NULL(input_stream);
6486
6487 struct aws_http_stream_write_data_options write_options = {
6488 .data = input_stream,
6489 .end_stream = true,
6490 };
6491
6492 ASSERT_SUCCESS(aws_http_stream_write_data(stream, &write_options));
6493 testing_channel_drain_queued_tasks(&s_tester.testing_channel);
6494
6495 uint32_t stream_id = aws_http_stream_get_id(stream);
6496 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
6497 ASSERT_NOT_NULL(
6498 h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_HEADERS, 0 /*search_start_idx*/, NULL));
6499 ASSERT_SUCCESS(
6500 h2_decode_tester_check_data_str_across_frames(&s_tester.peer.decode, stream_id, "hello", true /*end_stream*/));
6501
6502 aws_input_stream_release(input_stream);
6503 aws_http_message_release(request);
6504 aws_http_stream_release(stream);
6505 aws_http_connection_close(s_tester.connection);
6506
6507 return s_tester_clean_up();
6508 }