Debian Codegraph @

crt/aws-c-http/tests/h2_test_helper.h

raw symbols SCIP
1 #ifndef AWS_HTTP_H2_TEST_HELPER_H
2 #define AWS_HTTP_H2_TEST_HELPER_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7
8 #include <aws/common/array_list.h>
9 #include <aws/http/private/h2_frames.h>
10 #include <aws/http/request_response.h>
11 #include <aws/testing/aws_test_harness.h>
12
13 struct aws_input_stream;
14
15 #define ASSERT_H2ERR_SUCCESS(condition, ...) \
16 do { \
17 struct aws_h2err assert_rv = (condition); \
18 if (!aws_h2err_success(assert_rv)) { \
19 if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \
20 PRINT_FAIL_INTERNAL0( \
21 "Expected success at %s; got aws_h2err{%s, %s}\n", \
22 #condition, \
23 aws_http2_error_code_to_str(assert_rv.h2_code), \
24 aws_error_name(assert_rv.aws_code)); \
25 } \
26 POSTFAIL_INTERNAL(); \
27 } \
28 } while (0)
29
30 #define ASSERT_H2ERR_FAILS(condition, ...) \
31 do { \
32 struct aws_h2err assert_rv = (condition); \
33 if (!aws_h2err_failed(assert_rv)) { \
34 if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \
35 PRINT_FAIL_INTERNAL0("Expected failure at %s; got AWS_H2ERR_SUCCESS\n", #condition); \
36 } \
37 POSTFAIL_INTERNAL(); \
38 } \
39 } while (0)
40
41 #define ASSERT_H2ERR_ERROR(h2_error, condition, ...) \
42 do { \
43 struct aws_h2err assert_rv = (condition); \
44 if (!aws_h2err_failed(assert_rv)) { \
45 if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \
46 PRINT_FAIL_INTERNAL0( \
47 "Expected %s failure at %s; got AWS_H2ERR_SUCCESS\n", \
48 aws_http2_error_code_to_str(h2_error), \
49 #condition); \
50 } \
51 POSTFAIL_INTERNAL(); \
52 } \
53 if (assert_rv.h2_code != h2_error) { \
54 PRINT_FAIL_INTERNAL0( \
55 "Expected %s failure at %s; got aws_h2err{%s, %s}\n", \
56 aws_http2_error_code_to_str(h2_error), \
57 #condition, \
58 aws_http2_error_code_to_str(assert_rv.h2_code), \
59 aws_error_name(assert_rv.aws_code)); \
60 } \
61 } while (0)
62
63 /**
64 * Information gathered about a given frame from decoder callbacks.
65 * These aren't 1:1 with literal H2 frames:
66 * - The decoder hides the existence of CONTINUATION frames,
67 * their data continues the preceding HEADERS or PUSH_PROMISE frame.
68 *
69 * - A DATA frame could appear as N on_data callbacks.
70 *
71 * - The on_end_stream callback fires after all other callbacks for that frame,
72 * so we count it as part of the preceding "finished" frame.
73 */
74 struct h2_decoded_frame {
75 /* If true, we expect no further callbacks regarding this frame */
76 bool finished;
77
78 enum aws_h2_frame_type type; /* All frame types have this */
79 uint32_t stream_id; /* All frame types have this */
80
81 /*
82 * Everything else is only found in certain frame types
83 */
84
85 bool end_stream; /* HEADERS and DATA might have this */
86 bool ack; /* PING and SETTINGS might have this */
87
88 uint32_t error_code; /* RST_STREAM and GOAWAY have this */
89 uint32_t promised_stream_id; /* PUSH_PROMISE has this */
90 uint32_t goaway_last_stream_id; /* GOAWAY has this */
91 uint8_t ping_opaque_data[AWS_HTTP2_PING_DATA_SIZE]; /* PING has this */
92 uint32_t window_size_increment; /* WINDOW_UPDATE has this */
93
94 struct aws_http_headers *headers; /* HEADERS and PUSH_PROMISE have this */
95 bool headers_malformed; /* HEADERS and PUSH_PROMISE have this */
96 enum aws_http_header_block header_block_type; /* HEADERS have this */
97 struct aws_array_list settings; /* contains aws_http2_setting, SETTINGS has this */
98 struct aws_byte_buf data; /* DATA and GOAWAY have this */
99 uint32_t data_payload_len; /* DATA has this */
100 bool data_end_stream; /* DATA has this */
101 };
102
103 /**
104 * Check that:
105 * - frame finished (ex: if HEADERS frame, then on_headers_end() fired)
106 * - frame was in fact using the expected type and stream_id.
107 */
108 int h2_decoded_frame_check_finished(
109 const struct h2_decoded_frame *frame,
110 enum aws_h2_frame_type expected_type,
111 uint32_t expected_stream_id);
112
113 /******************************************************************************/
114
115 /**
116 * Translates decoder callbacks into an array-list of h2_decoded_frames.
117 */
118 struct h2_decode_tester {
119 struct aws_allocator *alloc;
120 struct aws_h2_decoder *decoder;
121 struct aws_array_list frames; /* contains h2_decoded_frame */
122 };
123
124 struct h2_decode_tester_options {
125 struct aws_allocator *alloc;
126 bool is_server;
127 bool skip_connection_preface;
128 };
129
130 int h2_decode_tester_init(struct h2_decode_tester *decode_tester, const struct h2_decode_tester_options *options);
131 void h2_decode_tester_clean_up(struct h2_decode_tester *decode_tester);
132
133 size_t h2_decode_tester_frame_count(const struct h2_decode_tester *decode_tester);
134 struct h2_decoded_frame *h2_decode_tester_get_frame(const struct h2_decode_tester *decode_tester, size_t i);
135 struct h2_decoded_frame *h2_decode_tester_latest_frame(const struct h2_decode_tester *decode_tester);
136
137 /**
138 * Search for frame of a given type, starting at specified index.
139 * To search for the next frame, pass search_start_idx = prev_idx + 1
140 */
141 struct h2_decoded_frame *h2_decode_tester_find_frame(
142 const struct h2_decode_tester *decode_tester,
143 enum aws_h2_frame_type type,
144 size_t search_start_idx,
145 size_t *out_idx);
146
147 /**
148 * Search for frame of a given stream-id, starting at specified index.
149 * To search for the next frame, pass search_start_idx = prev_idx + 1
150 */
151 struct h2_decoded_frame *h2_decode_tester_find_stream_frame_any_type(
152 const struct h2_decode_tester *decode_tester,
153 uint32_t stream_id,
154 size_t search_start_idx,
155 size_t *out_idx);
156
157 /**
158 * Search for frame of a given type and stream-id, starting at specified index.
159 * To search for the next frame, pass search_start_idx = prev_idx + 1
160 */
161 struct h2_decoded_frame *h2_decode_tester_find_stream_frame(
162 const struct h2_decode_tester *decode_tester,
163 enum aws_h2_frame_type type,
164 uint32_t stream_id,
165 size_t search_start_idx,
166 size_t *out_idx);
167
168 /**
169 * Compare data (which may be split across N frames) against expected
170 */
171 int h2_decode_tester_check_data_across_frames(
172 const struct h2_decode_tester *decode_tester,
173 uint32_t stream_id,
174 struct aws_byte_cursor expected,
175 bool expect_end_stream);
176
177 /**
178 * Compare data (which may be split across N frames) against expected
179 */
180 int h2_decode_tester_check_data_str_across_frames(
181 const struct h2_decode_tester *decode_tester,
182 uint32_t stream_id,
183 const char *expected,
184 bool expect_end_stream);
185
186 /******************************************************************************/
187
188 /**
189 * Fake HTTP/2 peer.
190 * Can decode H2 frames that are are written to the testing channel.
191 * Can encode H2 frames and push it into the channel in the read direction.
192 */
193 struct h2_fake_peer {
194 struct aws_allocator *alloc;
195 struct testing_channel *testing_channel;
196
197 struct aws_h2_frame_encoder encoder;
198 struct h2_decode_tester decode;
199 bool is_server;
200 };
201
202 struct h2_fake_peer_options {
203 struct aws_allocator *alloc;
204 struct testing_channel *testing_channel;
205 bool is_server;
206 };
207
208 int h2_fake_peer_init(struct h2_fake_peer *peer, const struct h2_fake_peer_options *options);
209 void h2_fake_peer_clean_up(struct h2_fake_peer *peer);
210
211 /**
212 * Pop all written messages off the testing-channel and run them through the peer's decode-tester
213 */
214 int h2_fake_peer_decode_messages_from_testing_channel(struct h2_fake_peer *peer);
215
216 /**
217 * Encode frame and push it into the testing-channel in the read-direction.
218 * Takes ownership of frame and destroys after sending.
219 */
220 int h2_fake_peer_send_frame(struct h2_fake_peer *peer, struct aws_h2_frame *frame);
221
222 /**
223 * Encode the entire byte cursor into a single DATA frame.
224 * Fails if the cursor is too large for this to work.
225 */
226 int h2_fake_peer_send_data_frame(
227 struct h2_fake_peer *peer,
228 uint32_t stream_id,
229 struct aws_byte_cursor data,
230 bool end_stream);
231
232 /**
233 * Encode the entire byte cursor into a single DATA frame.
234 * Fails if the cursor is too large for this to work.
235 */
236 int h2_fake_peer_send_data_frame_with_padding_length(
237 struct h2_fake_peer *peer,
238 uint32_t stream_id,
239 struct aws_byte_cursor data,
240 bool end_stream,
241 uint8_t padding_length);
242
243 /**
244 * Encode the entire string into a single DATA frame.
245 * Fails if the string is too large for this to work.
246 */
247 int h2_fake_peer_send_data_frame_str(struct h2_fake_peer *peer, uint32_t stream_id, const char *data, bool end_stream);
248
249 /**
250 * Peer sends the connection preface with specified settings.
251 * Takes ownership of frame and destroys after sending
252 */
253 int h2_fake_peer_send_connection_preface(struct h2_fake_peer *peer, struct aws_h2_frame *settings);
254
255 /**
256 * Peer sends the connection preface with default settings.
257 */
258 int h2_fake_peer_send_connection_preface_default_settings(struct h2_fake_peer *peer);
259
260 /******************************************************************************/
261
262 /**
263 * Create input stream that can do weird stuff in tests
264 */
265 struct aws_input_stream *aws_input_stream_new_tester(struct aws_allocator *alloc, struct aws_byte_cursor cursor);
266
267 void aws_input_stream_tester_set_max_bytes_per_read(struct aws_input_stream *input_stream, size_t max_bytes);
268
269 void aws_input_stream_tester_set_reading_broken(struct aws_input_stream *input_stream, bool is_broken);
270
271 /**
272 * Create input stream that can upload a certain length of stuff
273 */
274 struct aws_input_stream *aws_input_stream_tester_upload_new(struct aws_allocator *alloc, size_t length);
275 size_t aws_input_stream_tester_upload_get_num_sentence_sent(struct aws_input_stream *stream);
276
277 #endif /* AWS_HTTP_H2_TEST_HELPER_H */