-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathtest_h2_client.c
More file actions
6400 lines (5314 loc) · 297 KB
/
test_h2_client.c
File metadata and controls
6400 lines (5314 loc) · 297 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include "h2_test_helper.h"
#include "stream_test_helper.h"
#include <aws/http/private/h2_connection.h>
#include <aws/http/private/h2_stream.h>
#include <aws/http/private/request_response_impl.h>
#include <aws/http/request_response.h>
#include <aws/io/stream.h>
#include <aws/testing/io_testing_channel.h>
#define TEST_CASE(NAME) \
AWS_TEST_CASE(NAME, s_test_##NAME); \
static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
#define DEFINE_HEADER(NAME, VALUE) \
{ \
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \
}
struct connection_user_data {
struct aws_allocator *allocator;
int initial_settings_error_code;
uint32_t last_stream_id;
uint32_t http2_error;
struct aws_http2_setting remote_settings_array[10];
struct aws_byte_buf debug_data;
size_t num_settings;
};
static void s_on_initial_settings_completed(
struct aws_http_connection *http2_connection,
int error_code,
void *user_data) {
(void)http2_connection;
struct connection_user_data *data = user_data;
data->initial_settings_error_code = error_code;
}
static void s_on_goaway_received(
struct aws_http_connection *http2_connection,
uint32_t last_stream_id,
uint32_t http2_error,
const struct aws_byte_cursor debug_data,
void *user_data) {
(void)http2_connection;
struct connection_user_data *data = user_data;
data->last_stream_id = last_stream_id;
data->http2_error = http2_error;
if (data->debug_data.capacity != 0) {
/* If multiple goaway received, clean up the previous one */
aws_byte_buf_clean_up(&data->debug_data);
}
aws_byte_buf_init_copy_from_cursor(&data->debug_data, data->allocator, debug_data);
}
static void s_on_remote_settings_change(
struct aws_http_connection *http2_connection,
const struct aws_http2_setting *settings_array,
size_t num_settings,
void *user_data) {
(void)http2_connection;
struct connection_user_data *data = user_data;
if (num_settings) {
memcpy(data->remote_settings_array, settings_array, num_settings * sizeof(struct aws_http2_setting));
}
data->num_settings = num_settings;
}
/* Singleton used by tests in this file */
static struct tester {
struct aws_allocator *alloc;
struct aws_http_connection *connection;
struct testing_channel testing_channel;
struct h2_fake_peer peer;
struct connection_user_data user_data;
bool no_conn_manual_win_management;
} s_tester;
static int s_tester_init(struct aws_allocator *alloc, void *ctx) {
(void)ctx;
aws_http_library_init(alloc);
s_tester.alloc = alloc;
AWS_ZERO_STRUCT(s_tester.user_data.debug_data);
struct aws_testing_channel_options options = {.clock_fn = aws_high_res_clock_get_ticks};
ASSERT_SUCCESS(testing_channel_init(&s_tester.testing_channel, alloc, &options));
struct aws_http2_setting settings_array[] = {
{.id = AWS_HTTP2_SETTINGS_ENABLE_PUSH, .value = 0},
};
struct aws_http2_connection_options http2_options = {
.initial_settings_array = settings_array,
.num_initial_settings = AWS_ARRAY_SIZE(settings_array),
.on_initial_settings_completed = s_on_initial_settings_completed,
.max_closed_streams = AWS_HTTP2_DEFAULT_MAX_CLOSED_STREAMS,
.on_goaway_received = s_on_goaway_received,
.on_remote_settings_change = s_on_remote_settings_change,
.conn_manual_window_management = !s_tester.no_conn_manual_win_management,
/* Disable the batching for window update in tests */
.conn_window_size_threshold_to_send_update = UINT32_MAX,
.stream_window_size_threshold_to_send_update = UINT32_MAX,
};
s_tester.connection =
aws_http_connection_new_http2_client(alloc, false /* manual window management */, 0, &http2_options);
ASSERT_NOT_NULL(s_tester.connection);
{
s_tester.user_data.allocator = s_tester.alloc;
/* set connection user_data (handled by http-bootstrap in real world) */
s_tester.connection->user_data = &s_tester.user_data;
/* re-enact marriage vows of http-connection and channel (handled by http-bootstrap in real world) */
struct aws_channel_slot *slot = aws_channel_slot_new(s_tester.testing_channel.channel);
ASSERT_NOT_NULL(slot);
ASSERT_SUCCESS(aws_channel_slot_insert_end(s_tester.testing_channel.channel, slot));
ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, &s_tester.connection->channel_handler));
s_tester.connection->vtable->on_channel_handler_installed(&s_tester.connection->channel_handler, slot);
}
struct h2_fake_peer_options peer_options = {
.alloc = alloc,
.testing_channel = &s_tester.testing_channel,
.is_server = true,
};
ASSERT_SUCCESS(h2_fake_peer_init(&s_tester.peer, &peer_options));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
return AWS_OP_SUCCESS;
}
static int s_tester_clean_up(void) {
aws_byte_buf_clean_up(&s_tester.user_data.debug_data);
h2_fake_peer_clean_up(&s_tester.peer);
aws_http_connection_release(s_tester.connection);
ASSERT_SUCCESS(testing_channel_clean_up(&s_tester.testing_channel));
aws_http_library_clean_up();
return AWS_OP_SUCCESS;
}
/* Test the common setup/teardown used by all tests in this file */
TEST_CASE(h2_client_sanity_check) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
return s_tester_clean_up();
}
/* Test that a stream can be created and destroyed. */
TEST_CASE(h2_client_stream_create) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* create request */
struct aws_http_message *request = aws_http2_message_new_request(allocator);
ASSERT_NOT_NULL(request);
struct aws_http_header headers[] = {
DEFINE_HEADER(":method", "GET"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":path", "/"),
};
ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
struct aws_http_make_request_options options = {
.self_size = sizeof(options),
.request = request,
};
struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &options);
ASSERT_NOT_NULL(stream);
aws_http_stream_activate(stream);
/* shutdown channel so request can be released */
aws_channel_shutdown(s_tester.testing_channel.channel, AWS_ERROR_SUCCESS);
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_TRUE(testing_channel_is_shutdown_completed(&s_tester.testing_channel));
/* release request */
aws_http_stream_release(stream);
aws_http_message_release(request);
return s_tester_clean_up();
}
static void s_stream_cleans_up_on_destroy(void *data) {
bool *destroyed = data;
*destroyed = true;
}
TEST_CASE(h2_client_stream_release_after_complete) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* create request */
struct aws_http_message *request = aws_http2_message_new_request(allocator);
ASSERT_NOT_NULL(request);
struct aws_http_header headers[] = {
DEFINE_HEADER(":method", "GET"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":path", "/"),
};
ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
bool destroyed = false;
struct aws_http_make_request_options options = {
.self_size = sizeof(options),
.request = request,
.on_destroy = s_stream_cleans_up_on_destroy,
.user_data = &destroyed,
};
struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &options);
ASSERT_NOT_NULL(stream);
aws_http_stream_activate(stream);
/* shutdown channel so request can be released */
aws_channel_shutdown(s_tester.testing_channel.channel, AWS_ERROR_SUCCESS);
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_TRUE(testing_channel_is_shutdown_completed(&s_tester.testing_channel));
/* release request */
ASSERT_FALSE(destroyed);
aws_http_stream_release(stream);
ASSERT_TRUE(destroyed);
aws_http_message_release(request);
return s_tester_clean_up();
}
struct s_callback_invoked {
bool destroy_invoked;
bool complete_invoked;
};
static void s_unactivated_stream_cleans_up_on_destroy(void *data) {
struct s_callback_invoked *callback_data = data;
callback_data->destroy_invoked = true;
}
static void s_unactivated_stream_complete(struct aws_http_stream *stream, int error_code, void *data) {
(void)stream;
(void)error_code;
struct s_callback_invoked *callback_data = data;
callback_data->complete_invoked = true;
}
TEST_CASE(h2_client_unactivated_stream_cleans_up) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* create request */
struct aws_http_message *request = aws_http2_message_new_request(allocator);
ASSERT_NOT_NULL(request);
struct aws_http_header headers[] = {
DEFINE_HEADER(":method", "GET"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":path", "/"),
};
ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
struct s_callback_invoked callback_data = {0};
struct aws_http_make_request_options options = {
.self_size = sizeof(options),
.request = request,
.on_destroy = s_unactivated_stream_cleans_up_on_destroy,
.on_complete = s_unactivated_stream_complete,
.user_data = &callback_data,
};
struct aws_http_stream *stream = aws_http_connection_make_request(s_tester.connection, &options);
ASSERT_NOT_NULL(stream);
/* do not activate the stream, that's the test. */
ASSERT_FALSE(callback_data.destroy_invoked);
ASSERT_FALSE(callback_data.complete_invoked);
/* shutdown channel so request can be released */
aws_channel_shutdown(s_tester.testing_channel.channel, AWS_ERROR_SUCCESS);
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_TRUE(testing_channel_is_shutdown_completed(&s_tester.testing_channel));
aws_http_stream_release(stream);
ASSERT_TRUE(callback_data.destroy_invoked);
ASSERT_FALSE(callback_data.complete_invoked);
aws_http_message_release(request);
return s_tester_clean_up();
}
/* Test that client automatically sends the HTTP/2 Connection Preface (magic string, followed by SETTINGS frame) */
TEST_CASE(h2_client_connection_preface_sent) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* Have the fake peer to run its decoder on what the client has written.
* The decoder will raise an error if it doesn't receive the "client connection preface string" first. */
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
/* Now check that client sent SETTINGS frame */
struct h2_decoded_frame *first_written_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, 0);
ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_SETTINGS, first_written_frame->type);
ASSERT_FALSE(first_written_frame->ack);
return s_tester_clean_up();
}
static int s_stream_tester_init(struct client_stream_tester *stream_tester, struct aws_http_message *request) {
struct client_stream_tester_options options = {
.request = request,
.connection = s_tester.connection,
};
return client_stream_tester_init(stream_tester, s_tester.alloc, &options);
}
/* Test that client will automatically send the PING ACK frame back, when the PING frame is received */
TEST_CASE(h2_client_auto_ping_ack) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* Connection preface requires that SETTINGS be sent first (RFC-7540 3.5). */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7};
struct aws_h2_frame *frame = aws_h2_frame_new_ping(allocator, false /*ack*/, opaque_data);
ASSERT_NOT_NULL(frame);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, frame));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
/* Now check that client sent PING ACK frame, it should be the latest frame received by peer
* The last frame should be a ping type with ack on, and identical payload */
struct h2_decoded_frame *latest_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_PING, latest_frame->type);
ASSERT_TRUE(latest_frame->ack);
ASSERT_BIN_ARRAYS_EQUALS(
opaque_data, AWS_HTTP2_PING_DATA_SIZE, latest_frame->ping_opaque_data, AWS_HTTP2_PING_DATA_SIZE);
return s_tester_clean_up();
}
TEST_CASE(h2_client_auto_ping_ack_higher_priority) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* get connection preface and acks out of the way */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
size_t frames_count = h2_decode_tester_frame_count(&s_tester.peer.decode);
/* send request */
struct aws_http_message *request = aws_http2_message_new_request(allocator);
ASSERT_NOT_NULL(request);
struct aws_http_header request_headers_src[] = {
DEFINE_HEADER(":method", "POST"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":path", "/"),
};
aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
const char *body_src = "hello";
struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
struct aws_input_stream *request_body = aws_input_stream_new_from_cursor(allocator, &body_cursor);
aws_http_message_set_body_stream(request, request_body);
struct client_stream_tester stream_tester;
ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
/* Frames for the request are activated. Fake peer send PING frame now */
uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7};
struct aws_h2_frame *frame = aws_h2_frame_new_ping(allocator, false /*ack*/, opaque_data);
ASSERT_NOT_NULL(frame);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, frame));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* validate PING ACK frame has higher priority than the normal request frames, and be received earliest */
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
struct h2_decoded_frame *fastest_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, frames_count);
ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_PING, fastest_frame->type);
ASSERT_TRUE(fastest_frame->ack);
ASSERT_BIN_ARRAYS_EQUALS(
opaque_data, AWS_HTTP2_PING_DATA_SIZE, fastest_frame->ping_opaque_data, AWS_HTTP2_PING_DATA_SIZE);
/* clean up */
aws_http_message_release(request);
client_stream_tester_clean_up(&stream_tester);
aws_input_stream_release(request_body);
return s_tester_clean_up();
}
/* Test client can automatically send SETTINGs ACK */
TEST_CASE(h2_client_auto_settings_ack) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* Connection preface requires that SETTINGS be sent first (RFC-7540 3.5). */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* Have the fake peer to run its decoder on what the client has written. */
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
/* The Setting ACK frame should be sent back */
struct h2_decoded_frame *latest_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
ASSERT_UINT_EQUALS(AWS_H2_FRAME_T_SETTINGS, latest_frame->type);
ASSERT_TRUE(latest_frame->ack);
return s_tester_clean_up();
}
static int s_compare_headers(const struct aws_http_headers *expected, const struct aws_http_headers *got) {
ASSERT_UINT_EQUALS(aws_http_headers_count(expected), aws_http_headers_count(got));
for (size_t i = 0; i < aws_http_headers_count(expected); ++i) {
struct aws_http_header expected_field;
aws_http_headers_get_index(expected, i, &expected_field);
struct aws_http_header got_field;
aws_http_headers_get_index(got, i, &got_field);
ASSERT_TRUE(aws_byte_cursor_eq(&expected_field.name, &got_field.name));
ASSERT_TRUE(aws_byte_cursor_eq(&expected_field.value, &got_field.value));
ASSERT_INT_EQUALS(expected_field.compression, got_field.compression);
}
return AWS_OP_SUCCESS;
}
/* Test that a simple request/response can be carried to completion.
* The request consists of a single HEADERS frame and the response consists of a single HEADERS frame. */
TEST_CASE(h2_client_stream_complete) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* fake peer sends connection preface */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* send request */
struct aws_http_message *request = aws_http2_message_new_request(allocator);
ASSERT_NOT_NULL(request);
struct aws_http_header request_headers_src[] = {
DEFINE_HEADER(":method", "GET"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":path", "/"),
};
aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
struct client_stream_tester stream_tester;
ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
/* validate sent request, */
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
ASSERT_TRUE(sent_headers_frame->end_stream);
ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(request), sent_headers_frame->headers));
/* fake peer sends response */
struct aws_http_header response_headers_src[] = {
DEFINE_HEADER(":status", "404"),
DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
};
struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
struct aws_h2_frame *response_frame = aws_h2_frame_new_headers(
allocator, aws_http_stream_get_id(stream_tester.stream), response_headers, true /*end_stream*/, 0, NULL);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
/* validate that client received complete response */
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_TRUE(stream_tester.complete);
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
ASSERT_INT_EQUALS(404, stream_tester.response_status);
ASSERT_SUCCESS(s_compare_headers(response_headers, stream_tester.response_headers));
ASSERT_TRUE(stream_tester.metrics.receive_end_timestamp_ns > 0);
ASSERT_TRUE(stream_tester.metrics.receive_start_timestamp_ns > 0);
ASSERT_TRUE(stream_tester.metrics.receive_end_timestamp_ns > stream_tester.metrics.receive_start_timestamp_ns);
ASSERT_TRUE(
stream_tester.metrics.receiving_duration_ns ==
stream_tester.metrics.receive_end_timestamp_ns - stream_tester.metrics.receive_start_timestamp_ns);
ASSERT_TRUE(stream_tester.metrics.send_start_timestamp_ns > 0);
ASSERT_TRUE(stream_tester.metrics.send_end_timestamp_ns > 0);
ASSERT_TRUE(stream_tester.metrics.send_end_timestamp_ns > stream_tester.metrics.send_start_timestamp_ns);
ASSERT_TRUE(
stream_tester.metrics.sending_duration_ns ==
stream_tester.metrics.send_end_timestamp_ns - stream_tester.metrics.send_start_timestamp_ns);
ASSERT_TRUE(stream_tester.metrics.stream_id == stream_tester.stream->id);
ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
/* clean up */
aws_http_headers_release(response_headers);
aws_http_message_release(request);
client_stream_tester_clean_up(&stream_tester);
return s_tester_clean_up();
}
/* Calling aws_http_connection_close() should cleanly shut down connection */
TEST_CASE(h2_client_close) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* fake peer sends connection preface */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* send request */
struct aws_http_message *request = aws_http2_message_new_request(allocator);
ASSERT_NOT_NULL(request);
struct aws_http_header request_headers_src[] = {
DEFINE_HEADER(":method", "GET"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":path", "/"),
};
aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
struct client_stream_tester stream_tester;
ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
/* close connection */
aws_http_connection_close(s_tester.connection);
/* connection should immediately lose "open" status */
ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
/* finish shutting down */
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
/* validate that pending streams complete with error */
ASSERT_TRUE(stream_tester.complete);
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, stream_tester.on_complete_error_code);
/* validate that GOAWAY sent */
struct h2_decoded_frame *goaway =
h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_GOAWAY, 0, NULL);
ASSERT_NOT_NULL(goaway);
ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_NO_ERROR, goaway->error_code);
ASSERT_UINT_EQUALS(0, goaway->goaway_last_stream_id);
/* clean up */
aws_http_message_release(request);
client_stream_tester_clean_up(&stream_tester);
return s_tester_clean_up();
}
/* Test that client automatically sends the HTTP/2 Connection Preface (magic string, followed by initial SETTINGS frame,
* which we disabled the push_promise) And it will not be applied until the SETTINGS ack is received. Once SETTINGS ack
* received, the initial settings will be applied and callback will be invoked */
TEST_CASE(h2_client_connection_init_settings_applied_after_ack_by_peer) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* send request */
struct aws_http_message *request = aws_http2_message_new_request(allocator);
ASSERT_NOT_NULL(request);
struct aws_http_header request_headers_src[] = {
DEFINE_HEADER(":method", "GET"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":path", "/"),
};
aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
struct client_stream_tester stream_tester;
ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
/* validate sent request, */
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
ASSERT_TRUE(sent_headers_frame->end_stream);
ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(request), sent_headers_frame->headers));
/* fake peer sends connection preface */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* fake peer sends push_promise */
uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
/* fake peer sends push request (PUSH_PROMISE) */
struct aws_http_header push_request_headers_src[] = {
DEFINE_HEADER(":method", "GET"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":authority", "veryblackpage.com"),
DEFINE_HEADER(":path", "/style.css"),
};
struct aws_http_headers *push_request_headers = aws_http_headers_new(allocator);
ASSERT_SUCCESS(aws_http_headers_add_array(
push_request_headers, push_request_headers_src, AWS_ARRAY_SIZE(push_request_headers_src)));
uint32_t promised_stream_id = 2;
struct aws_h2_frame *peer_frame =
aws_h2_frame_new_push_promise(allocator, stream_id, promised_stream_id, push_request_headers, 0);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* validate the connection is still open */
ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
/* set initial_settings_error_code as AWS_ERROR_UNKNOWN to make sure callback invoked later */
s_tester.user_data.initial_settings_error_code = AWS_ERROR_UNKNOWN;
/* fake peer sends setting ack */
struct aws_h2_frame *settings_ack_frame = aws_h2_frame_new_settings(allocator, NULL, 0, true);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, settings_ack_frame));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* validate the callback invoked */
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, s_tester.user_data.initial_settings_error_code);
/* fake peer sends another push_promise again, after setting applied, connection will be closed */
peer_frame = aws_h2_frame_new_push_promise(allocator, stream_id, promised_stream_id + 2, push_request_headers, 0);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* validate the connection completed with error */
ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
ASSERT_INT_EQUALS(
AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
/* clean up */
aws_http_headers_release(push_request_headers);
aws_http_message_release(request);
client_stream_tester_clean_up(&stream_tester);
return s_tester_clean_up();
}
/* Test that h2 stream can take a h1 request massega and transfrom it to h2 style to send it. */
TEST_CASE(h2_client_stream_with_h1_request_message) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* fake peer sends connection preface */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* send an h1 request */
struct aws_http_message *request = aws_http_message_new_request(allocator);
ASSERT_NOT_NULL(request);
AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_method(request, aws_http_method_post));
AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
struct aws_http_header request_headers_src[] = {
DEFINE_HEADER("Accept", "*/*"),
DEFINE_HEADER("Host", "example.com"),
DEFINE_HEADER("Content-Length", "5"),
DEFINE_HEADER("Upgrade", "HTTP/2.0"), /* Connection-specific header should be skiped */
};
aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
/* body */
const char *body_src = "hello";
struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
struct aws_input_stream *request_body = aws_input_stream_new_from_cursor(allocator, &body_cursor);
aws_http_message_set_body_stream(request, request_body);
struct client_stream_tester stream_tester;
ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
/* validate sent request (client should have sent SETTINGS, SETTINGS ACK, HEADERS, DATA (END_STREAM) */
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
ASSERT_UINT_EQUALS(4, h2_decode_tester_frame_count(&s_tester.peer.decode));
/* set expected h2 style headers */
struct aws_http_header expected_headers_src[] = {
DEFINE_HEADER(":method", "POST"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":authority", "example.com"),
DEFINE_HEADER(":path", "/"),
DEFINE_HEADER("accept", "*/*"),
DEFINE_HEADER("content-length", "5"),
};
struct aws_http_headers *expected_headers = aws_http_headers_new(allocator);
ASSERT_SUCCESS(
aws_http_headers_add_array(expected_headers, expected_headers_src, AWS_ARRAY_SIZE(expected_headers_src)));
struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, 2);
ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
ASSERT_SUCCESS(s_compare_headers(expected_headers, sent_headers_frame->headers));
struct h2_decoded_frame *sent_data_frame = h2_decode_tester_get_frame(&s_tester.peer.decode, 3);
ASSERT_INT_EQUALS(AWS_H2_FRAME_T_DATA, sent_data_frame->type);
ASSERT_TRUE(sent_data_frame->end_stream);
ASSERT_TRUE(aws_byte_buf_eq_c_str(&sent_data_frame->data, body_src));
/* clean up */
aws_http_headers_release(expected_headers);
aws_http_message_release(request);
client_stream_tester_clean_up(&stream_tester);
aws_input_stream_destroy(request_body);
return s_tester_clean_up();
}
/* Test that h2 stream can split the cookies header correctly */
TEST_CASE(h2_client_stream_with_cookies_headers) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* fake peer sends connection preface */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* send an h1 request */
struct aws_http_message *request = aws_http_message_new_request(allocator);
ASSERT_NOT_NULL(request);
AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_method(request, aws_http_method_get));
AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
struct aws_http_header request_headers_src[] = {
DEFINE_HEADER("Accept", "*/*"),
DEFINE_HEADER("Host", "example.com"),
DEFINE_HEADER("cookie", "a=b; c=d; e=f"),
};
aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
struct client_stream_tester stream_tester;
ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
/* set expected h2 style headers */
struct aws_http_header expected_headers_src[] = {
DEFINE_HEADER(":method", "GET"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":authority", "example.com"),
DEFINE_HEADER(":path", "/"),
DEFINE_HEADER("accept", "*/*"),
DEFINE_HEADER("cookie", "a=b; c=d; e=f"),
};
struct aws_http_headers *expected_headers = aws_http_headers_new(allocator);
ASSERT_SUCCESS(
aws_http_headers_add_array(expected_headers, expected_headers_src, AWS_ARRAY_SIZE(expected_headers_src)));
/* validate sent request, */
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
ASSERT_TRUE(sent_headers_frame->end_stream);
ASSERT_SUCCESS(s_compare_headers(expected_headers, sent_headers_frame->headers));
/* clean up */
aws_http_headers_release(expected_headers);
aws_http_message_release(request);
client_stream_tester_clean_up(&stream_tester);
return s_tester_clean_up();
}
/* Receiving malformed headers should result in a "Stream Error", not a "Connection Error". */
TEST_CASE(h2_client_stream_err_malformed_header) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* fake peer sends connection preface */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* send request */
struct aws_http_message *request = aws_http2_message_new_request(allocator);
ASSERT_NOT_NULL(request);
struct aws_http_header request_headers_src[] = {
DEFINE_HEADER(":method", "GET"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":path", "/"),
};
aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
struct client_stream_tester stream_tester;
ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* fake peer sends response with malformed header */
struct aws_http_header response_headers_src[] = {
DEFINE_HEADER(":STATUS", "404"), /* uppercase name forbidden in h2 */
};
struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
struct aws_h2_frame *response_frame = aws_h2_frame_new_headers(
allocator, aws_http_stream_get_id(stream_tester.stream), response_headers, true /*end_stream*/, 0, NULL);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
/* validate that stream completed with error */
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_TRUE(stream_tester.complete);
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
/* a stream error should not affect the connection */
ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
/* validate that stream sent RST_STREAM */
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
struct h2_decoded_frame *rst_stream_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
ASSERT_INT_EQUALS(AWS_H2_FRAME_T_RST_STREAM, rst_stream_frame->type);
ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, rst_stream_frame->error_code);
/* clean up */
aws_http_headers_release(response_headers);
aws_http_message_release(request);
client_stream_tester_clean_up(&stream_tester);
return s_tester_clean_up();
}
TEST_CASE(h2_client_stream_err_state_forbids_frame) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* fake peer sends connection preface */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* send request */
struct aws_http_message *request = aws_http2_message_new_request(allocator);
ASSERT_NOT_NULL(request);
struct aws_http_header request_headers_src[] = {
DEFINE_HEADER(":method", "PUT"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":path", "/"),
};
aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
const char *body_src = "hello";
struct aws_byte_cursor body_cursor = aws_byte_cursor_from_c_str(body_src);
struct aws_input_stream *request_body = aws_input_stream_new_tester(allocator, body_cursor);
/* Prevent END_STREAM from being sent */
aws_input_stream_tester_set_max_bytes_per_read(request_body, 0);
aws_http_message_set_body_stream(request, request_body);
struct client_stream_tester stream_tester;
ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
/* Execute 1 event-loop tick. Request is sent, but no end_stream received */
testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
struct h2_decoded_frame *sent_headers_frame = h2_decode_tester_latest_frame(&s_tester.peer.decode);
ASSERT_INT_EQUALS(AWS_H2_FRAME_T_HEADERS, sent_headers_frame->type);
ASSERT_FALSE(sent_headers_frame->end_stream);
ASSERT_SUCCESS(s_compare_headers(aws_http_message_get_headers(request), sent_headers_frame->headers));
/* fake peer sends response */
struct aws_http_header response_headers_src[] = {
DEFINE_HEADER(":status", "404"),
DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
};
struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
/* fake peer sends response headers with end_stream set, which cause the stream to be
* AWS_H2_STREAM_STATE_HALF_CLOSED_REMOTE */
struct aws_h2_frame *response_frame =
aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
/* AWS_H2_STREAM_STATE_HALF_CLOSED_REMOTE will reject body frame */
ASSERT_SUCCESS(h2_fake_peer_send_data_frame_str(&s_tester.peer, stream_id, body_src, true /*end_stream*/));
/* validate that stream completed with error */
testing_channel_run_currently_queued_tasks(&s_tester.testing_channel);
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
/* a stream error should not affect the connection */
ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
/* validate that stream sent RST_STREAM */
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
struct h2_decoded_frame *rst_stream_frame =
h2_decode_tester_find_stream_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_RST_STREAM, stream_id, 0, NULL);
ASSERT_INT_EQUALS(AWS_H2_FRAME_T_RST_STREAM, rst_stream_frame->type);
ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_STREAM_CLOSED, rst_stream_frame->error_code);
/* clean up */
aws_http_headers_release(response_headers);
aws_http_message_release(request);
client_stream_tester_clean_up(&stream_tester);
aws_input_stream_release(request_body);
return s_tester_clean_up();
}
TEST_CASE(h2_client_conn_err_stream_frames_received_for_idle_stream) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* fake peer sends connection preface */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* fake peer sends response to "idle" (aka doesn't exist yet) stream 99 */
struct aws_http_header response_headers_src[] = {
DEFINE_HEADER(":status", "200"),
};
struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
struct aws_h2_frame *response_frame =
aws_h2_frame_new_headers(allocator, 99 /*stream_id*/, response_headers, true /* end_stream */, 0, NULL);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, response_frame));
/* validate that connection has closed due to PROTOCOL_ERROR */
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_FALSE(aws_http_connection_is_open(s_tester.connection));
ASSERT_INT_EQUALS(
AWS_ERROR_HTTP_PROTOCOL_ERROR, testing_channel_get_shutdown_error_code(&s_tester.testing_channel));
/* validate that client sent GOAWAY */
ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&s_tester.peer));
struct h2_decoded_frame *goaway =
h2_decode_tester_find_frame(&s_tester.peer.decode, AWS_H2_FRAME_T_GOAWAY, 0, NULL);
ASSERT_NOT_NULL(goaway);
ASSERT_UINT_EQUALS(AWS_HTTP2_ERR_PROTOCOL_ERROR, goaway->error_code);
ASSERT_UINT_EQUALS(0, goaway->goaway_last_stream_id);
/* clean up */
aws_http_headers_release(response_headers);
return s_tester_clean_up();
}
/* Peer may have sent certain frames (WINDOW_UPDATE and RST_STREAM) before realizing
* that we have closed the stream. These frames should be ignored. */
TEST_CASE(h2_client_stream_ignores_some_frames_received_soon_after_closing) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* fake peer sends connection preface */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* send request */
struct aws_http_message *request = aws_http2_message_new_request(allocator);
ASSERT_NOT_NULL(request);
struct aws_http_header request_headers_src[] = {
DEFINE_HEADER(":method", "GET"),
DEFINE_HEADER(":scheme", "https"),
DEFINE_HEADER(":path", "/"),
};
aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
struct client_stream_tester stream_tester;
ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, request));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
uint32_t stream_id = aws_http_stream_get_id(stream_tester.stream);
/* fake peer sends complete response */
struct aws_http_header response_headers_src[] = {
DEFINE_HEADER(":status", "404"),
DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
};
struct aws_http_headers *response_headers = aws_http_headers_new(allocator);
aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
struct aws_h2_frame *peer_frame =
aws_h2_frame_new_headers(allocator, stream_id, response_headers, true /*end_stream*/, 0, NULL);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
/* fake peer sends WINDOW_UPDATE */
peer_frame = aws_h2_frame_new_window_update(allocator, stream_id, 99);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
/* fake peer sends RST_STREAM */
peer_frame = aws_h2_frame_new_rst_stream(allocator, stream_id, AWS_HTTP2_ERR_ENHANCE_YOUR_CALM);
ASSERT_SUCCESS(h2_fake_peer_send_frame(&s_tester.peer, peer_frame));
/* validate that stream completed successfully.
* the WINDOW_UPDATE and RST_STREAM should be ignored because
* they arrived soon after the client had sent END_STREAM */
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
ASSERT_TRUE(stream_tester.complete);
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
ASSERT_TRUE(aws_http_connection_is_open(s_tester.connection));
/* clean up */
aws_http_headers_release(response_headers);
aws_http_message_release(request);
client_stream_tester_clean_up(&stream_tester);
return s_tester_clean_up();
}
TEST_CASE(h2_client_stream_receive_info_headers) {
ASSERT_SUCCESS(s_tester_init(allocator, ctx));
/* fake peer sends connection preface */
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&s_tester.peer));
testing_channel_drain_queued_tasks(&s_tester.testing_channel);
/* send request */