| 1 |
#ifndef AWS_HTTP_H2_TEST_HELPER_H |
| 2 |
#define AWS_HTTP_H2_TEST_HELPER_H |
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
struct h2_decoded_frame { |
| 75 |
|
| 76 |
bool finished; |
| 77 |
|
| 78 |
enum aws_h2_frame_type type; |
| 79 |
uint32_t stream_id; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
bool end_stream; |
| 86 |
bool ack; |
| 87 |
|
| 88 |
uint32_t error_code; |
| 89 |
uint32_t promised_stream_id; |
| 90 |
uint32_t goaway_last_stream_id; |
| 91 |
uint8_t ping_opaque_data[AWS_HTTP2_PING_DATA_SIZE]; |
| 92 |
uint32_t window_size_increment; |
| 93 |
|
| 94 |
struct aws_http_headers *headers; |
| 95 |
bool headers_malformed; |
| 96 |
enum aws_http_header_block header_block_type; |
| 97 |
struct aws_array_list settings; |
| 98 |
struct aws_byte_buf data; |
| 99 |
uint32_t data_payload_len; |
| 100 |
bool data_end_stream; |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 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 |
|
| 117 |
|
| 118 |
struct h2_decode_tester { |
| 119 |
struct aws_allocator *alloc; |
| 120 |
struct aws_h2_decoder *decoder; |
| 121 |
struct aws_array_list frames; |
| 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 |
|
| 139 |
|
| 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 |
|
| 149 |
|
| 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 |
|
| 159 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 190 |
|
| 191 |
|
| 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 |
|
| 213 |
|
| 214 |
int h2_fake_peer_decode_messages_from_testing_channel(struct h2_fake_peer *peer); |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
int h2_fake_peer_send_frame(struct h2_fake_peer *peer, struct aws_h2_frame *frame); |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 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 |
|
| 234 |
|
| 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 |
|
| 245 |
|
| 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 |
|
| 251 |
|
| 252 |
|
| 253 |
int h2_fake_peer_send_connection_preface(struct h2_fake_peer *peer, struct aws_h2_frame *settings); |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
int h2_fake_peer_send_connection_preface_default_settings(struct h2_fake_peer *peer); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 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 |
|
| 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 |