| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/checksums/crc.h> |
| 7 |
#include <aws/common/array_list.h> |
| 8 |
#include <aws/event-stream/event_stream.h> |
| 9 |
#include <aws/testing/aws_test_harness.h> |
| 10 |
|
| 11 |
struct test_decoder_data { |
| 12 |
struct aws_event_stream_message_prelude latest_prelude; |
| 13 |
struct aws_array_list headers_list; |
| 14 |
uint8_t *latest_payload; |
| 15 |
size_t written; |
| 16 |
struct aws_allocator *alloc; |
| 17 |
int latest_error; |
| 18 |
uint32_t message_crc; |
| 19 |
}; |
| 20 |
|
| 21 |
static void s_decoder_test_on_payload_segment( |
| 22 |
struct aws_event_stream_streaming_decoder *decoder, |
| 23 |
struct aws_byte_buf *data, |
| 24 |
int8_t final_segment, |
| 25 |
void *user_data) { |
| 26 |
(void)final_segment; |
| 27 |
(void)decoder; |
| 28 |
struct test_decoder_data *decoder_data = (struct test_decoder_data *)user_data; |
| 29 |
memcpy(decoder_data->latest_payload + decoder_data->written, data->buffer, data->len); |
| 30 |
decoder_data->written += data->len; |
| 31 |
} |
| 32 |
|
| 33 |
static void s_decoder_test_on_prelude_received( |
| 34 |
struct aws_event_stream_streaming_decoder *decoder, |
| 35 |
struct aws_event_stream_message_prelude *prelude, |
| 36 |
void *user_data) { |
| 37 |
|
| 38 |
(void)decoder; |
| 39 |
struct test_decoder_data *decoder_data = (struct test_decoder_data *)user_data; |
| 40 |
decoder_data->latest_prelude = *prelude; |
| 41 |
|
| 42 |
if (decoder_data->latest_payload) { |
| 43 |
aws_mem_release(decoder_data->alloc, decoder_data->latest_payload); |
| 44 |
} |
| 45 |
|
| 46 |
const size_t payload_size = decoder_data->latest_prelude.total_len - AWS_EVENT_STREAM_PRELUDE_LENGTH - |
| 47 |
AWS_EVENT_STREAM_TRAILER_LENGTH - decoder_data->latest_prelude.headers_len; |
| 48 |
|
| 49 |
if (payload_size) { |
| 50 |
decoder_data->latest_payload = aws_mem_acquire(decoder_data->alloc, payload_size); |
| 51 |
} else { |
| 52 |
decoder_data->latest_payload = NULL; |
| 53 |
} |
| 54 |
decoder_data->written = 0; |
| 55 |
} |
| 56 |
|
| 57 |
static void s_decoder_test_header_received( |
| 58 |
struct aws_event_stream_streaming_decoder *decoder, |
| 59 |
struct aws_event_stream_message_prelude *prelude, |
| 60 |
struct aws_event_stream_header_value_pair *header, |
| 61 |
void *user_data) { |
| 62 |
(void)decoder; |
| 63 |
(void)prelude; |
| 64 |
struct test_decoder_data *decoder_data = (struct test_decoder_data *)user_data; |
| 65 |
aws_event_stream_add_header(&decoder_data->headers_list, header); |
| 66 |
} |
| 67 |
|
| 68 |
static void s_decoder_test_on_complete( |
| 69 |
struct aws_event_stream_streaming_decoder *decoder, |
| 70 |
uint32_t message_crc, |
| 71 |
void *user_data) { |
| 72 |
(void)decoder; |
| 73 |
struct test_decoder_data *decoder_data = (struct test_decoder_data *)user_data; |
| 74 |
decoder_data->message_crc = message_crc; |
| 75 |
} |
| 76 |
|
| 77 |
static void s_decoder_test_on_error( |
| 78 |
struct aws_event_stream_streaming_decoder *decoder, |
| 79 |
struct aws_event_stream_message_prelude *prelude, |
| 80 |
int error_code, |
| 81 |
const char *message, |
| 82 |
void *user_data) { |
| 83 |
|
| 84 |
(void)decoder; |
| 85 |
(void)prelude; |
| 86 |
(void)message; |
| 87 |
struct test_decoder_data *decoder_data = (struct test_decoder_data *)user_data; |
| 88 |
decoder_data->latest_error = error_code; |
| 89 |
} |
| 90 |
|
| 91 |
static int s_test_streaming_decoder_incoming_no_op_valid_single_message_fn(struct aws_allocator *allocator, void *ctx) { |
| 92 |
uint8_t test_data[] = { |
| 93 |
0x00, |
| 94 |
0x00, |
| 95 |
0x00, |
| 96 |
0x10, |
| 97 |
0x00, |
| 98 |
0x00, |
| 99 |
0x00, |
| 100 |
0x00, |
| 101 |
0x05, |
| 102 |
0xc2, |
| 103 |
0x48, |
| 104 |
0xeb, |
| 105 |
0x7d, |
| 106 |
0x98, |
| 107 |
0xc8, |
| 108 |
0xff, |
| 109 |
}; |
| 110 |
|
| 111 |
(void)ctx; |
| 112 |
struct test_decoder_data decoder_data = {.latest_payload = 0, .written = 0, .alloc = allocator, .latest_error = 0}; |
| 113 |
|
| 114 |
struct aws_event_stream_streaming_decoder_options decoder_options = { |
| 115 |
.on_payload_segment = s_decoder_test_on_payload_segment, |
| 116 |
.on_prelude = s_decoder_test_on_prelude_received, |
| 117 |
.on_header = s_decoder_test_header_received, |
| 118 |
.on_complete = s_decoder_test_on_complete, |
| 119 |
.on_error = s_decoder_test_on_error, |
| 120 |
.user_data = &decoder_data}; |
| 121 |
|
| 122 |
struct aws_event_stream_streaming_decoder decoder; |
| 123 |
aws_event_stream_streaming_decoder_init_from_options(&decoder, allocator, &decoder_options); |
| 124 |
|
| 125 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data)); |
| 126 |
ASSERT_SUCCESS( |
| 127 |
aws_event_stream_streaming_decoder_pump(&decoder, &test_buf), "Message validation should have succeeded"); |
| 128 |
ASSERT_SUCCESS(decoder_data.latest_error, "No Error callback shouldn't have been called"); |
| 129 |
|
| 130 |
ASSERT_INT_EQUALS(0x00000010, decoder_data.latest_prelude.total_len, "Message length should have been 0x10"); |
| 131 |
ASSERT_INT_EQUALS(0x00000000, decoder_data.latest_prelude.headers_len, "Headers Length should have been 0x00"); |
| 132 |
ASSERT_INT_EQUALS(0x05c248eb, decoder_data.latest_prelude.prelude_crc, "Prelude CRC should have been 0x8c335472"); |
| 133 |
ASSERT_INT_EQUALS(0, decoder_data.written, "No payload data should have been written"); |
| 134 |
|
| 135 |
if (decoder_data.latest_payload) { |
| 136 |
aws_mem_release(allocator, decoder_data.latest_payload); |
| 137 |
} |
| 138 |
ASSERT_UINT_EQUALS(0x7D98C8FF, decoder_data.message_crc); |
| 139 |
|
| 140 |
aws_event_stream_streaming_decoder_clean_up(&decoder); |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
AWS_TEST_CASE( |
| 146 |
test_streaming_decoder_incoming_no_op_valid_single_message, |
| 147 |
s_test_streaming_decoder_incoming_no_op_valid_single_message_fn) |
| 148 |
|
| 149 |
static int s_test_streaming_decoder_incoming_application_no_headers_fn(struct aws_allocator *allocator, void *ctx) { |
| 150 |
uint8_t test_data[] = { |
| 151 |
0x00, 0x00, 0x00, 0x1D, 0x00, 0x00, 0x00, 0x00, 0xfd, 0x52, 0x8c, 0x5a, 0x7b, 0x27, 0x66, |
| 152 |
0x6f, 0x6f, 0x27, 0x3a, 0x27, 0x62, 0x61, 0x72, 0x27, 0x7d, 0xc3, 0x65, 0x39, 0x36, |
| 153 |
}; |
| 154 |
|
| 155 |
(void)ctx; |
| 156 |
struct test_decoder_data decoder_data = {.latest_payload = 0, .written = 0, .alloc = allocator, .latest_error = 0}; |
| 157 |
|
| 158 |
struct aws_event_stream_streaming_decoder_options decoder_options = { |
| 159 |
.on_payload_segment = s_decoder_test_on_payload_segment, |
| 160 |
.on_prelude = s_decoder_test_on_prelude_received, |
| 161 |
.on_header = s_decoder_test_header_received, |
| 162 |
.on_complete = s_decoder_test_on_complete, |
| 163 |
.on_error = s_decoder_test_on_error, |
| 164 |
.user_data = &decoder_data}; |
| 165 |
|
| 166 |
struct aws_event_stream_streaming_decoder decoder; |
| 167 |
aws_event_stream_streaming_decoder_init_from_options(&decoder, allocator, &decoder_options); |
| 168 |
|
| 169 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data)); |
| 170 |
|
| 171 |
ASSERT_SUCCESS( |
| 172 |
aws_event_stream_streaming_decoder_pump(&decoder, &test_buf), "Message validation should have succeeded"); |
| 173 |
ASSERT_SUCCESS(decoder_data.latest_error, "No Error callback shouldn't have been called"); |
| 174 |
|
| 175 |
ASSERT_INT_EQUALS(0x0000001D, decoder_data.latest_prelude.total_len, "Message length should have been 0x1D"); |
| 176 |
ASSERT_INT_EQUALS(0x00000000, decoder_data.latest_prelude.headers_len, "Headers Length should have been 0x00"); |
| 177 |
ASSERT_INT_EQUALS(0xfd528c5a, decoder_data.latest_prelude.prelude_crc, "Prelude CRC should have been 0xfd528c5a"); |
| 178 |
|
| 179 |
const char *expected_str = "{'foo':'bar'}"; |
| 180 |
size_t payload_len = decoder_data.latest_prelude.total_len - AWS_EVENT_STREAM_PRELUDE_LENGTH - |
| 181 |
AWS_EVENT_STREAM_TRAILER_LENGTH - decoder_data.latest_prelude.headers_len; |
| 182 |
ASSERT_INT_EQUALS( |
| 183 |
strlen(expected_str), payload_len, "payload length should have been %d", (int)(strlen(expected_str))); |
| 184 |
|
| 185 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 186 |
expected_str, |
| 187 |
strlen(expected_str), |
| 188 |
decoder_data.latest_payload, |
| 189 |
payload_len, |
| 190 |
"payload should have been %s", |
| 191 |
expected_str); |
| 192 |
|
| 193 |
if (decoder_data.latest_payload) { |
| 194 |
aws_mem_release(allocator, decoder_data.latest_payload); |
| 195 |
} |
| 196 |
ASSERT_UINT_EQUALS(0xC3653936, decoder_data.message_crc); |
| 197 |
|
| 198 |
aws_event_stream_streaming_decoder_clean_up(&decoder); |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
AWS_TEST_CASE( |
| 204 |
test_streaming_decoder_incoming_application_no_headers, |
| 205 |
s_test_streaming_decoder_incoming_application_no_headers_fn) |
| 206 |
|
| 207 |
static int s_test_streaming_decoder_incoming_application_one_compressed_header_pair_valid_fn( |
| 208 |
struct aws_allocator *allocator, |
| 209 |
void *ctx) { |
| 210 |
(void)ctx; |
| 211 |
uint8_t test_data[] = { |
| 212 |
0x00, 0x00, 0x00, 0x3D, 0x00, 0x00, 0x00, 0x20, 0x07, 0xFD, 0x83, 0x96, 0x0C, 'c', 'o', 'n', |
| 213 |
't', 'e', 'n', 't', '-', 't', 'y', 'p', 'e', 0x07, 0x00, 0x10, 'a', 'p', 'p', 'l', |
| 214 |
'i', 'c', 'a', 't', 'i', 'o', 'n', '/', 'j', 's', 'o', 'n', 0x7b, 0x27, 0x66, 0x6f, |
| 215 |
0x6f, 0x27, 0x3a, 0x27, 0x62, 0x61, 0x72, 0x27, 0x7d, 0x8D, 0x9C, 0x08, 0xB1, |
| 216 |
}; |
| 217 |
|
| 218 |
struct test_decoder_data decoder_data = { |
| 219 |
.latest_payload = 0, |
| 220 |
.written = 0, |
| 221 |
.alloc = allocator, |
| 222 |
.latest_error = 0, |
| 223 |
}; |
| 224 |
aws_event_stream_headers_list_init(&decoder_data.headers_list, allocator); |
| 225 |
|
| 226 |
struct aws_event_stream_streaming_decoder_options decoder_options = { |
| 227 |
.on_payload_segment = s_decoder_test_on_payload_segment, |
| 228 |
.on_prelude = s_decoder_test_on_prelude_received, |
| 229 |
.on_header = s_decoder_test_header_received, |
| 230 |
.on_complete = s_decoder_test_on_complete, |
| 231 |
.on_error = s_decoder_test_on_error, |
| 232 |
.user_data = &decoder_data}; |
| 233 |
|
| 234 |
struct aws_event_stream_streaming_decoder decoder; |
| 235 |
aws_event_stream_streaming_decoder_init_from_options(&decoder, allocator, &decoder_options); |
| 236 |
|
| 237 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data)); |
| 238 |
|
| 239 |
ASSERT_SUCCESS( |
| 240 |
aws_event_stream_streaming_decoder_pump(&decoder, &test_buf), "Message validation should have succeeded"); |
| 241 |
ASSERT_SUCCESS(decoder_data.latest_error, "No Error callback shouldn't have been called"); |
| 242 |
|
| 243 |
ASSERT_INT_EQUALS(0x0000003D, decoder_data.latest_prelude.total_len, "Message length should have been 0x3D"); |
| 244 |
ASSERT_INT_EQUALS(0x00000020, decoder_data.latest_prelude.headers_len, "Headers Length should have been 0x20"); |
| 245 |
ASSERT_INT_EQUALS(0x07FD8396, decoder_data.latest_prelude.prelude_crc, "Prelude CRC should have been 0x07FD8396"); |
| 246 |
|
| 247 |
const char *content_type = "content-type"; |
| 248 |
const char *content_type_value = "application/json"; |
| 249 |
|
| 250 |
struct aws_event_stream_header_value_pair latest_header; |
| 251 |
aws_array_list_get_at(&decoder_data.headers_list, &latest_header, 0); |
| 252 |
struct aws_byte_buf latest_header_value = aws_event_stream_header_value_as_string(&latest_header); |
| 253 |
|
| 254 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 255 |
content_type, |
| 256 |
strlen(content_type), |
| 257 |
latest_header.header_name, |
| 258 |
latest_header.header_name_len, |
| 259 |
"header name should have been %s", |
| 260 |
content_type); |
| 261 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 262 |
content_type_value, |
| 263 |
strlen(content_type_value), |
| 264 |
latest_header_value.buffer, |
| 265 |
latest_header_value.len, |
| 266 |
"header value should have been %s", |
| 267 |
content_type_value); |
| 268 |
|
| 269 |
const char *expected_str = "{'foo':'bar'}"; |
| 270 |
size_t payload_len = decoder_data.latest_prelude.total_len - AWS_EVENT_STREAM_PRELUDE_LENGTH - |
| 271 |
AWS_EVENT_STREAM_TRAILER_LENGTH - decoder_data.latest_prelude.headers_len; |
| 272 |
ASSERT_INT_EQUALS( |
| 273 |
strlen(expected_str), payload_len, "payload length should have been %d", (int)(strlen(expected_str))); |
| 274 |
|
| 275 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 276 |
expected_str, |
| 277 |
strlen(expected_str), |
| 278 |
decoder_data.latest_payload, |
| 279 |
payload_len, |
| 280 |
"payload should have been %s", |
| 281 |
expected_str); |
| 282 |
|
| 283 |
if (decoder_data.latest_payload) { |
| 284 |
aws_mem_release(allocator, decoder_data.latest_payload); |
| 285 |
} |
| 286 |
ASSERT_UINT_EQUALS(0x8D9C08B1, decoder_data.message_crc); |
| 287 |
|
| 288 |
aws_event_stream_headers_list_cleanup(&decoder_data.headers_list); |
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
AWS_TEST_CASE( |
| 293 |
test_streaming_decoder_incoming_application_one_compressed_header_pair_valid, |
| 294 |
s_test_streaming_decoder_incoming_application_one_compressed_header_pair_valid_fn) |
| 295 |
|
| 296 |
static int s_test_streaming_decoder_incoming_application_one_int32_header_pair_valid_fn( |
| 297 |
struct aws_allocator *allocator, |
| 298 |
void *ctx) { |
| 299 |
(void)ctx; |
| 300 |
|
| 301 |
uint8_t test_data[] = { |
| 302 |
0x00, 0x00, 0x00, 0x1b, |
| 303 |
0x00, 0x00, 0x00, 0x0b, |
| 304 |
0xe5, 0xc0, 0xa0, 0x72, |
| 305 |
0x05, |
| 306 |
'e', 'v', 'e', 'n', 't', |
| 307 |
0x04, |
| 308 |
0x00, 0x00, |
| 309 |
0x00, 0x20, |
| 310 |
0x04, 0xa1, 0xd4, 0x7c |
| 311 |
}; |
| 312 |
|
| 313 |
|
| 314 |
struct test_decoder_data decoder_data = { |
| 315 |
.latest_payload = 0, |
| 316 |
.written = 0, |
| 317 |
.alloc = allocator, |
| 318 |
.latest_error = 0, |
| 319 |
}; |
| 320 |
aws_event_stream_headers_list_init(&decoder_data.headers_list, allocator); |
| 321 |
|
| 322 |
struct aws_event_stream_streaming_decoder_options decoder_options = { |
| 323 |
.on_payload_segment = s_decoder_test_on_payload_segment, |
| 324 |
.on_prelude = s_decoder_test_on_prelude_received, |
| 325 |
.on_header = s_decoder_test_header_received, |
| 326 |
.on_complete = s_decoder_test_on_complete, |
| 327 |
.on_error = s_decoder_test_on_error, |
| 328 |
.user_data = &decoder_data}; |
| 329 |
|
| 330 |
struct aws_event_stream_streaming_decoder decoder; |
| 331 |
aws_event_stream_streaming_decoder_init_from_options(&decoder, allocator, &decoder_options); |
| 332 |
|
| 333 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data)); |
| 334 |
|
| 335 |
ASSERT_SUCCESS( |
| 336 |
aws_event_stream_streaming_decoder_pump(&decoder, &test_buf), "Message validation should have succeeded"); |
| 337 |
ASSERT_SUCCESS(decoder_data.latest_error, "No Error callback shouldn't have been called"); |
| 338 |
|
| 339 |
ASSERT_INT_EQUALS(0x0000001B, decoder_data.latest_prelude.total_len, "Message length should have been 0x1B"); |
| 340 |
ASSERT_INT_EQUALS(0x0000000B, decoder_data.latest_prelude.headers_len, "Headers Length should have been 0xB"); |
| 341 |
ASSERT_INT_EQUALS(0xE5C0A072, decoder_data.latest_prelude.prelude_crc, "Prelude CRC should have been 0xE5C0A072"); |
| 342 |
|
| 343 |
const char *expected_header_name = "event"; |
| 344 |
struct aws_event_stream_header_value_pair latest_header; |
| 345 |
aws_array_list_get_at(&decoder_data.headers_list, &latest_header, 0); |
| 346 |
|
| 347 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 348 |
expected_header_name, |
| 349 |
strlen(expected_header_name), |
| 350 |
latest_header.header_name, |
| 351 |
latest_header.header_name_len, |
| 352 |
"header name should have been %s", |
| 353 |
expected_header_name); |
| 354 |
|
| 355 |
int32_t latest_header_value = aws_event_stream_header_value_as_int32(&latest_header); |
| 356 |
ASSERT_INT_EQUALS(0x00000020, latest_header_value, "Header value should have been 0x00000020"); |
| 357 |
ASSERT_UINT_EQUALS(0x04A1D47C, decoder_data.message_crc); |
| 358 |
|
| 359 |
aws_event_stream_headers_list_cleanup(&decoder_data.headers_list); |
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
AWS_TEST_CASE( |
| 364 |
test_streaming_decoder_incoming_application_one_int32_header_pair_valid, |
| 365 |
s_test_streaming_decoder_incoming_application_one_int32_header_pair_valid_fn) |
| 366 |
|
| 367 |
static int s_test_streaming_decoder_incoming_application_variable_headers_with_empty_length_pair_valid_fn( |
| 368 |
struct aws_allocator *allocator, |
| 369 |
void *ctx) { |
| 370 |
(void)ctx; |
| 371 |
|
| 372 |
uint8_t test_data[] = { |
| 373 |
0x00, 0x00, 0x00, 0x22, |
| 374 |
0x00, 0x00, 0x00, 0x12, |
| 375 |
0x2D, 0x9A, 0xD2, 0x45, |
| 376 |
0x04, |
| 377 |
'b', 'u', 'f', 'f', |
| 378 |
0x06, |
| 379 |
0x00, 0x00, |
| 380 |
0x06, |
| 381 |
's', 't', 'r', 'i','n','g', |
| 382 |
0x07, |
| 383 |
0x00, 0x00, |
| 384 |
0xC8, 0x4C, 0xF8, 0x53 |
| 385 |
}; |
| 386 |
|
| 387 |
struct test_decoder_data decoder_data = { |
| 388 |
.latest_payload = 0, |
| 389 |
.written = 0, |
| 390 |
.alloc = allocator, |
| 391 |
.latest_error = 0, |
| 392 |
}; |
| 393 |
aws_event_stream_headers_list_init(&decoder_data.headers_list, allocator); |
| 394 |
struct aws_event_stream_streaming_decoder_options decoder_options = { |
| 395 |
.on_payload_segment = s_decoder_test_on_payload_segment, |
| 396 |
.on_prelude = s_decoder_test_on_prelude_received, |
| 397 |
.on_header = s_decoder_test_header_received, |
| 398 |
.on_complete = s_decoder_test_on_complete, |
| 399 |
.on_error = s_decoder_test_on_error, |
| 400 |
.user_data = &decoder_data}; |
| 401 |
|
| 402 |
struct aws_event_stream_streaming_decoder decoder; |
| 403 |
aws_event_stream_streaming_decoder_init_from_options(&decoder, allocator, &decoder_options); |
| 404 |
|
| 405 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data)); |
| 406 |
ASSERT_SUCCESS( |
| 407 |
aws_event_stream_streaming_decoder_pump(&decoder, &test_buf), "Message validation should have succeeded"); |
| 408 |
ASSERT_SUCCESS(decoder_data.latest_error, "No Error callback shouldn't have been called"); |
| 409 |
|
| 410 |
ASSERT_INT_EQUALS(0x00000022, decoder_data.latest_prelude.total_len); |
| 411 |
ASSERT_INT_EQUALS(0x00000012, decoder_data.latest_prelude.headers_len); |
| 412 |
ASSERT_INT_EQUALS(0x2D9AD245, decoder_data.latest_prelude.prelude_crc); |
| 413 |
ASSERT_UINT_EQUALS(0xC84CF853, decoder_data.message_crc); |
| 414 |
|
| 415 |
const char *expected_header_name = "buff"; |
| 416 |
struct aws_event_stream_header_value_pair latest_header; |
| 417 |
aws_array_list_get_at(&decoder_data.headers_list, &latest_header, 0); |
| 418 |
|
| 419 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 420 |
expected_header_name, |
| 421 |
strlen(expected_header_name), |
| 422 |
latest_header.header_name, |
| 423 |
latest_header.header_name_len, |
| 424 |
"header name should have been %s", |
| 425 |
expected_header_name); |
| 426 |
|
| 427 |
struct aws_byte_buf latest_header_value = aws_event_stream_header_value_as_bytebuf(&latest_header); |
| 428 |
ASSERT_INT_EQUALS(0, latest_header_value.len); |
| 429 |
ASSERT_NULL(latest_header_value.buffer); |
| 430 |
|
| 431 |
const char *expected_string_header_name = "string"; |
| 432 |
aws_array_list_get_at(&decoder_data.headers_list, &latest_header, 1); |
| 433 |
|
| 434 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 435 |
expected_string_header_name, |
| 436 |
strlen(expected_string_header_name), |
| 437 |
latest_header.header_name, |
| 438 |
latest_header.header_name_len, |
| 439 |
"header name should have been %s", |
| 440 |
expected_header_name); |
| 441 |
|
| 442 |
latest_header_value = aws_event_stream_header_value_as_bytebuf(&latest_header); |
| 443 |
ASSERT_INT_EQUALS(0, latest_header_value.len); |
| 444 |
ASSERT_NULL(latest_header_value.buffer); |
| 445 |
|
| 446 |
aws_event_stream_headers_list_cleanup(&decoder_data.headers_list); |
| 447 |
return 0; |
| 448 |
} |
| 449 |
|
| 450 |
AWS_TEST_CASE( |
| 451 |
test_streaming_decoder_incoming_application_variable_headers_with_empty_length_pair_valid, |
| 452 |
s_test_streaming_decoder_incoming_application_variable_headers_with_empty_length_pair_valid_fn) |
| 453 |
|
| 454 |
static int s_test_streaming_decoder_incoming_application_one_bool_header_pair_valid_fn( |
| 455 |
struct aws_allocator *allocator, |
| 456 |
void *ctx) { |
| 457 |
(void)ctx; |
| 458 |
|
| 459 |
uint8_t test_data[] = { |
| 460 |
0x00, 0x00, 0x00, 0x17, |
| 461 |
0x00, 0x00, 0x00, 0x07, |
| 462 |
0x29, 0x86, 0x01, 0x58, |
| 463 |
0x05, |
| 464 |
'e', 'v', 'e', 'n', 't', |
| 465 |
0x00, |
| 466 |
0x4b, 0x4d, 0x2b, 0xe7 |
| 467 |
}; |
| 468 |
|
| 469 |
|
| 470 |
struct test_decoder_data decoder_data = { |
| 471 |
.latest_payload = 0, |
| 472 |
.written = 0, |
| 473 |
.alloc = allocator, |
| 474 |
.latest_error = 0, |
| 475 |
}; |
| 476 |
aws_event_stream_headers_list_init(&decoder_data.headers_list, allocator); |
| 477 |
|
| 478 |
struct aws_event_stream_streaming_decoder_options decoder_options = { |
| 479 |
.on_payload_segment = s_decoder_test_on_payload_segment, |
| 480 |
.on_prelude = s_decoder_test_on_prelude_received, |
| 481 |
.on_header = s_decoder_test_header_received, |
| 482 |
.on_complete = s_decoder_test_on_complete, |
| 483 |
.on_error = s_decoder_test_on_error, |
| 484 |
.user_data = &decoder_data}; |
| 485 |
|
| 486 |
struct aws_event_stream_streaming_decoder decoder; |
| 487 |
aws_event_stream_streaming_decoder_init_from_options(&decoder, allocator, &decoder_options); |
| 488 |
|
| 489 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data)); |
| 490 |
|
| 491 |
ASSERT_SUCCESS( |
| 492 |
aws_event_stream_streaming_decoder_pump(&decoder, &test_buf), "Message validation should have succeeded"); |
| 493 |
ASSERT_SUCCESS(decoder_data.latest_error, "No Error callback shouldn't have been called"); |
| 494 |
|
| 495 |
ASSERT_INT_EQUALS(0x00000017, decoder_data.latest_prelude.total_len, "Message length should have been 0x17"); |
| 496 |
ASSERT_INT_EQUALS(0x00000007, decoder_data.latest_prelude.headers_len, "Headers Length should have been 0x7"); |
| 497 |
ASSERT_INT_EQUALS(0x29860158, decoder_data.latest_prelude.prelude_crc, "Prelude CRC should have been 0x29860158"); |
| 498 |
|
| 499 |
const char *expected_header_name = "event"; |
| 500 |
struct aws_event_stream_header_value_pair latest_header; |
| 501 |
aws_array_list_get_at(&decoder_data.headers_list, &latest_header, 0); |
| 502 |
|
| 503 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 504 |
expected_header_name, |
| 505 |
strlen(expected_header_name), |
| 506 |
latest_header.header_name, |
| 507 |
latest_header.header_name_len, |
| 508 |
"header name should have been %s", |
| 509 |
expected_header_name); |
| 510 |
|
| 511 |
int8_t latest_header_value = aws_event_stream_header_value_as_bool(&latest_header); |
| 512 |
ASSERT_INT_EQUALS(1, latest_header_value, "Header value should have been true"); |
| 513 |
ASSERT_UINT_EQUALS(0x4B4D2BE7, decoder_data.message_crc); |
| 514 |
|
| 515 |
aws_event_stream_headers_list_cleanup(&decoder_data.headers_list); |
| 516 |
return 0; |
| 517 |
} |
| 518 |
|
| 519 |
AWS_TEST_CASE( |
| 520 |
test_streaming_decoder_incoming_application_one_bool_header_pair_valid, |
| 521 |
s_test_streaming_decoder_incoming_application_one_bool_header_pair_valid_fn) |
| 522 |
|
| 523 |
static int s_test_streaming_decoder_incoming_multiple_messages_fn(struct aws_allocator *allocator, void *ctx) { |
| 524 |
(void)ctx; |
| 525 |
uint8_t test_data[] = { |
| 526 |
|
| 527 |
0x00, |
| 528 |
0x00, |
| 529 |
0x00, |
| 530 |
0x10, |
| 531 |
0x00, |
| 532 |
0x00, |
| 533 |
0x00, |
| 534 |
0x00, |
| 535 |
0x05, |
| 536 |
0xc2, |
| 537 |
0x48, |
| 538 |
0xeb, |
| 539 |
0x7d, |
| 540 |
0x98, |
| 541 |
0xc8, |
| 542 |
0xff, |
| 543 |
|
| 544 |
0x00, |
| 545 |
0x00, |
| 546 |
0x00, |
| 547 |
0x3D, |
| 548 |
0x00, |
| 549 |
0x00, |
| 550 |
0x00, |
| 551 |
0x20, |
| 552 |
0x07, |
| 553 |
0xFD, |
| 554 |
0x83, |
| 555 |
0x96, |
| 556 |
0x0C, |
| 557 |
'c', |
| 558 |
'o', |
| 559 |
'n', |
| 560 |
't', |
| 561 |
'e', |
| 562 |
'n', |
| 563 |
't', |
| 564 |
'-', |
| 565 |
't', |
| 566 |
'y', |
| 567 |
'p', |
| 568 |
'e', |
| 569 |
0x07, |
| 570 |
0x00, |
| 571 |
0x10, |
| 572 |
'a', |
| 573 |
'p', |
| 574 |
'p', |
| 575 |
'l', |
| 576 |
'i', |
| 577 |
'c', |
| 578 |
'a', |
| 579 |
't', |
| 580 |
'i', |
| 581 |
'o', |
| 582 |
'n', |
| 583 |
'/', |
| 584 |
'j', |
| 585 |
's', |
| 586 |
'o', |
| 587 |
'n', |
| 588 |
0x7b, |
| 589 |
0x27, |
| 590 |
0x66, |
| 591 |
0x6f, |
| 592 |
0x6f, |
| 593 |
0x27, |
| 594 |
0x3a, |
| 595 |
0x27, |
| 596 |
0x62, |
| 597 |
0x61, |
| 598 |
0x72, |
| 599 |
0x27, |
| 600 |
0x7d, |
| 601 |
0x8D, |
| 602 |
0x9C, |
| 603 |
0x08, |
| 604 |
0xB1, |
| 605 |
}; |
| 606 |
|
| 607 |
size_t first_message_size = 0x10; |
| 608 |
size_t read_size = 7; |
| 609 |
|
| 610 |
|
| 611 |
struct test_decoder_data decoder_data = {.latest_payload = 0, .written = 0, .alloc = allocator, .latest_error = 0}; |
| 612 |
aws_event_stream_headers_list_init(&decoder_data.headers_list, allocator); |
| 613 |
|
| 614 |
struct aws_event_stream_streaming_decoder_options decoder_options = { |
| 615 |
.on_payload_segment = s_decoder_test_on_payload_segment, |
| 616 |
.on_prelude = s_decoder_test_on_prelude_received, |
| 617 |
.on_header = s_decoder_test_header_received, |
| 618 |
.on_complete = s_decoder_test_on_complete, |
| 619 |
.on_error = s_decoder_test_on_error, |
| 620 |
.user_data = &decoder_data}; |
| 621 |
|
| 622 |
struct aws_event_stream_streaming_decoder decoder; |
| 623 |
aws_event_stream_streaming_decoder_init_from_options(&decoder, allocator, &decoder_options); |
| 624 |
|
| 625 |
size_t current_written = 0; |
| 626 |
int err_code = 0; |
| 627 |
while (current_written < first_message_size && !err_code) { |
| 628 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data + current_written, read_size); |
| 629 |
err_code = aws_event_stream_streaming_decoder_pump(&decoder, &test_buf); |
| 630 |
current_written += read_size; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
ASSERT_SUCCESS(err_code, "Message validation should have succeeded"); |
| 636 |
ASSERT_SUCCESS(decoder_data.latest_error, "No Error callback shouldn't have been called"); |
| 637 |
|
| 638 |
ASSERT_INT_EQUALS(0x00000010, decoder_data.latest_prelude.total_len, "Message length should have been 0x10"); |
| 639 |
ASSERT_INT_EQUALS(0x00000000, decoder_data.latest_prelude.headers_len, "Headers Length should have been 0x00"); |
| 640 |
ASSERT_INT_EQUALS(0x05c248eb, decoder_data.latest_prelude.prelude_crc, "Prelude CRC should have been 0x8c335472"); |
| 641 |
ASSERT_INT_EQUALS(0, decoder_data.written, "No payload data should have been written"); |
| 642 |
ASSERT_UINT_EQUALS(0x7D98C8FF, decoder_data.message_crc); |
| 643 |
|
| 644 |
while (current_written < sizeof(test_data) && !err_code) { |
| 645 |
size_t to_write = |
| 646 |
current_written + read_size < sizeof(test_data) ? read_size : sizeof(test_data) - current_written; |
| 647 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data + current_written, to_write); |
| 648 |
err_code = aws_event_stream_streaming_decoder_pump(&decoder, &test_buf); |
| 649 |
current_written += to_write; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
ASSERT_SUCCESS(err_code, "Message validation should have succeeded"); |
| 654 |
ASSERT_SUCCESS(decoder_data.latest_error, "No Error callback shouldn't have been called"); |
| 655 |
|
| 656 |
ASSERT_INT_EQUALS(0x0000003D, decoder_data.latest_prelude.total_len, "Message length should have been 0x3D"); |
| 657 |
ASSERT_INT_EQUALS(0x00000020, decoder_data.latest_prelude.headers_len, "Headers Length should have been 0x20"); |
| 658 |
ASSERT_INT_EQUALS(0x07FD8396, decoder_data.latest_prelude.prelude_crc, "Prelude CRC should have been 0x07FD8396"); |
| 659 |
|
| 660 |
const char *content_type = "content-type"; |
| 661 |
const char *content_type_value = "application/json"; |
| 662 |
|
| 663 |
struct aws_event_stream_header_value_pair latest_header; |
| 664 |
aws_array_list_get_at(&decoder_data.headers_list, &latest_header, 0); |
| 665 |
struct aws_byte_buf latest_header_value = aws_event_stream_header_value_as_string(&latest_header); |
| 666 |
|
| 667 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 668 |
content_type, |
| 669 |
strlen(content_type), |
| 670 |
latest_header.header_name, |
| 671 |
latest_header.header_name_len, |
| 672 |
"header name should have been %s", |
| 673 |
content_type); |
| 674 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 675 |
content_type_value, |
| 676 |
strlen(content_type_value), |
| 677 |
latest_header_value.buffer, |
| 678 |
latest_header_value.len, |
| 679 |
"header value should have been %s", |
| 680 |
content_type_value); |
| 681 |
|
| 682 |
const char *expected_str = "{'foo':'bar'}"; |
| 683 |
size_t payload_len = decoder_data.latest_prelude.total_len - AWS_EVENT_STREAM_PRELUDE_LENGTH - |
| 684 |
AWS_EVENT_STREAM_TRAILER_LENGTH - decoder_data.latest_prelude.headers_len; |
| 685 |
ASSERT_INT_EQUALS( |
| 686 |
strlen(expected_str), payload_len, "payload length should have been %d", (int)(strlen(expected_str))); |
| 687 |
|
| 688 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 689 |
expected_str, |
| 690 |
strlen(expected_str), |
| 691 |
decoder_data.latest_payload, |
| 692 |
payload_len, |
| 693 |
"payload should have been %s", |
| 694 |
expected_str); |
| 695 |
|
| 696 |
if (decoder_data.latest_payload) { |
| 697 |
aws_mem_release(allocator, decoder_data.latest_payload); |
| 698 |
} |
| 699 |
ASSERT_UINT_EQUALS(0x8D9C08B1, decoder_data.message_crc); |
| 700 |
|
| 701 |
aws_event_stream_streaming_decoder_clean_up(&decoder); |
| 702 |
aws_event_stream_headers_list_cleanup(&decoder_data.headers_list); |
| 703 |
|
| 704 |
return 0; |
| 705 |
} |
| 706 |
|
| 707 |
AWS_TEST_CASE(test_streaming_decoder_incoming_multiple_messages, s_test_streaming_decoder_incoming_multiple_messages_fn) |
| 708 |
|
| 709 |
static int s_test_streaming_decoder_incoming_application_large_size_header_name_valid_fn( |
| 710 |
struct aws_allocator *allocator, |
| 711 |
void *ctx) { |
| 712 |
(void)ctx; |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
uint8_t name_len = 255; |
| 721 |
size_t name_bytes = 255; |
| 722 |
uint32_t headers_len = (uint32_t)(1 + name_bytes + 1); |
| 723 |
uint32_t total_len = (uint32_t)(AWS_EVENT_STREAM_PRELUDE_LENGTH + headers_len + AWS_EVENT_STREAM_TRAILER_LENGTH); |
| 724 |
|
| 725 |
|
| 726 |
uint8_t *test_data = aws_mem_acquire(allocator, total_len); |
| 727 |
memset(test_data, 0, total_len); |
| 728 |
|
| 729 |
|
| 730 |
test_data[0] = (total_len >> 24) & 0xFF; |
| 731 |
test_data[1] = (total_len >> 16) & 0xFF; |
| 732 |
test_data[2] = (total_len >> 8) & 0xFF; |
| 733 |
test_data[3] = total_len & 0xFF; |
| 734 |
|
| 735 |
|
| 736 |
test_data[4] = (headers_len >> 24) & 0xFF; |
| 737 |
test_data[5] = (headers_len >> 16) & 0xFF; |
| 738 |
test_data[6] = (headers_len >> 8) & 0xFF; |
| 739 |
test_data[7] = headers_len & 0xFF; |
| 740 |
|
| 741 |
|
| 742 |
uint32_t prelude_crc = aws_checksums_crc32(test_data, 8, 0); |
| 743 |
test_data[8] = (prelude_crc >> 24) & 0xFF; |
| 744 |
test_data[9] = (prelude_crc >> 16) & 0xFF; |
| 745 |
test_data[10] = (prelude_crc >> 8) & 0xFF; |
| 746 |
test_data[11] = prelude_crc & 0xFF; |
| 747 |
|
| 748 |
|
| 749 |
test_data[12] = name_len; |
| 750 |
|
| 751 |
|
| 752 |
memset(test_data + 13, 'A', name_bytes); |
| 753 |
|
| 754 |
|
| 755 |
test_data[13 + name_bytes] = 0x00; |
| 756 |
|
| 757 |
|
| 758 |
uint32_t message_crc = aws_checksums_crc32(test_data, total_len - 4, 0); |
| 759 |
size_t crc_offset = total_len - 4; |
| 760 |
test_data[crc_offset] = (message_crc >> 24) & 0xFF; |
| 761 |
test_data[crc_offset + 1] = (message_crc >> 16) & 0xFF; |
| 762 |
test_data[crc_offset + 2] = (message_crc >> 8) & 0xFF; |
| 763 |
test_data[crc_offset + 3] = message_crc & 0xFF; |
| 764 |
|
| 765 |
struct test_decoder_data decoder_data = { |
| 766 |
.latest_payload = 0, |
| 767 |
.written = 0, |
| 768 |
.alloc = allocator, |
| 769 |
.latest_error = 0, |
| 770 |
}; |
| 771 |
aws_event_stream_headers_list_init(&decoder_data.headers_list, allocator); |
| 772 |
|
| 773 |
struct aws_event_stream_streaming_decoder_options decoder_options = { |
| 774 |
.on_payload_segment = s_decoder_test_on_payload_segment, |
| 775 |
.on_prelude = s_decoder_test_on_prelude_received, |
| 776 |
.on_header = s_decoder_test_header_received, |
| 777 |
.on_complete = s_decoder_test_on_complete, |
| 778 |
.on_error = s_decoder_test_on_error, |
| 779 |
.user_data = &decoder_data}; |
| 780 |
|
| 781 |
struct aws_event_stream_streaming_decoder decoder; |
| 782 |
aws_event_stream_streaming_decoder_init_from_options(&decoder, allocator, &decoder_options); |
| 783 |
|
| 784 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, total_len); |
| 785 |
|
| 786 |
ASSERT_SUCCESS( |
| 787 |
aws_event_stream_streaming_decoder_pump(&decoder, &test_buf), "Message validation should have succeeded"); |
| 788 |
ASSERT_SUCCESS(decoder_data.latest_error, "No Error callback shouldn't have been called"); |
| 789 |
|
| 790 |
ASSERT_INT_EQUALS(total_len, decoder_data.latest_prelude.total_len, "Message length mismatch"); |
| 791 |
ASSERT_INT_EQUALS(headers_len, decoder_data.latest_prelude.headers_len, "Headers length mismatch"); |
| 792 |
ASSERT_INT_EQUALS(prelude_crc, decoder_data.latest_prelude.prelude_crc, "Prelude CRC mismatch"); |
| 793 |
|
| 794 |
|
| 795 |
ASSERT_TRUE(aws_array_list_length(&decoder_data.headers_list) > 0, "Should have at least one header"); |
| 796 |
struct aws_event_stream_header_value_pair latest_header; |
| 797 |
aws_array_list_get_at(&decoder_data.headers_list, &latest_header, 0); |
| 798 |
ASSERT_INT_EQUALS(name_len, latest_header.header_name_len, "Header name length should be 255"); |
| 799 |
|
| 800 |
|
| 801 |
for (size_t i = 0; i < name_len; i++) { |
| 802 |
ASSERT_INT_EQUALS('A', latest_header.header_name[i], "Header name byte should be 'A'"); |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
int8_t header_value = aws_event_stream_header_value_as_bool(&latest_header); |
| 807 |
ASSERT_INT_EQUALS(1, header_value, "Header value should be true"); |
| 808 |
|
| 809 |
ASSERT_UINT_EQUALS(message_crc, decoder_data.message_crc, "Message CRC mismatch"); |
| 810 |
|
| 811 |
if (decoder_data.latest_payload) { |
| 812 |
aws_mem_release(allocator, decoder_data.latest_payload); |
| 813 |
} |
| 814 |
|
| 815 |
aws_event_stream_streaming_decoder_clean_up(&decoder); |
| 816 |
aws_event_stream_headers_list_cleanup(&decoder_data.headers_list); |
| 817 |
aws_mem_release(allocator, test_data); |
| 818 |
|
| 819 |
return 0; |
| 820 |
} |
| 821 |
|
| 822 |
AWS_TEST_CASE( |
| 823 |
test_streaming_decoder_incoming_application_large_size_header_name_valid, |
| 824 |
s_test_streaming_decoder_incoming_application_large_size_header_name_valid_fn) |
| 825 |
|
| 826 |
static int s_test_streaming_decoder_incoming_illegal_header_length_relationship_fails_fn( |
| 827 |
struct aws_allocator *allocator, |
| 828 |
void *ctx) { |
| 829 |
uint8_t test_data[] = { |
| 830 |
0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x08, 0xfe, 0x99, 0x66, |
| 831 |
0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 832 |
}; |
| 833 |
|
| 834 |
(void)ctx; |
| 835 |
struct test_decoder_data decoder_data = {.latest_payload = 0, .written = 0, .alloc = allocator, .latest_error = 0}; |
| 836 |
aws_event_stream_headers_list_init(&decoder_data.headers_list, allocator); |
| 837 |
|
| 838 |
struct aws_event_stream_streaming_decoder_options decoder_options = { |
| 839 |
.on_payload_segment = s_decoder_test_on_payload_segment, |
| 840 |
.on_prelude = s_decoder_test_on_prelude_received, |
| 841 |
.on_header = s_decoder_test_header_received, |
| 842 |
.on_complete = s_decoder_test_on_complete, |
| 843 |
.on_error = s_decoder_test_on_error, |
| 844 |
.user_data = &decoder_data}; |
| 845 |
|
| 846 |
struct aws_event_stream_streaming_decoder decoder; |
| 847 |
aws_event_stream_streaming_decoder_init_from_options(&decoder, allocator, &decoder_options); |
| 848 |
|
| 849 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data)); |
| 850 |
ASSERT_FAILS(aws_event_stream_streaming_decoder_pump(&decoder, &test_buf), "Message validation should not succeed"); |
| 851 |
ASSERT_INT_EQUALS( |
| 852 |
AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN, |
| 853 |
decoder_data.latest_error, |
| 854 |
"Error should be invalid headers length"); |
| 855 |
|
| 856 |
if (decoder_data.latest_payload) { |
| 857 |
aws_mem_release(allocator, decoder_data.latest_payload); |
| 858 |
} |
| 859 |
|
| 860 |
aws_event_stream_streaming_decoder_clean_up(&decoder); |
| 861 |
|
| 862 |
aws_event_stream_headers_list_cleanup(&decoder_data.headers_list); |
| 863 |
|
| 864 |
return 0; |
| 865 |
} |
| 866 |
|
| 867 |
AWS_TEST_CASE( |
| 868 |
test_streaming_decoder_incoming_illegal_header_length_relationship_fails, |
| 869 |
s_test_streaming_decoder_incoming_illegal_header_length_relationship_fails_fn) |
| 870 |
|
| 871 |
static int s_test_streaming_decoder_incoming_message_too_short_fn(struct aws_allocator *allocator, void *ctx) { |
| 872 |
uint8_t test_data[] = { |
| 873 |
0x00, |
| 874 |
0x00, |
| 875 |
0x00, |
| 876 |
0x0c, |
| 877 |
0x00, |
| 878 |
0x00, |
| 879 |
0x00, |
| 880 |
0x00, |
| 881 |
0xa0, |
| 882 |
0xd2, |
| 883 |
0x32, |
| 884 |
0x68, |
| 885 |
0x00, |
| 886 |
}; |
| 887 |
|
| 888 |
(void)ctx; |
| 889 |
struct test_decoder_data decoder_data = { |
| 890 |
.latest_payload = 0, |
| 891 |
.written = 0, |
| 892 |
.alloc = allocator, |
| 893 |
.latest_error = 0, |
| 894 |
}; |
| 895 |
aws_event_stream_headers_list_init(&decoder_data.headers_list, allocator); |
| 896 |
|
| 897 |
struct aws_event_stream_streaming_decoder_options decoder_options = { |
| 898 |
.on_payload_segment = s_decoder_test_on_payload_segment, |
| 899 |
.on_prelude = s_decoder_test_on_prelude_received, |
| 900 |
.on_header = s_decoder_test_header_received, |
| 901 |
.on_complete = s_decoder_test_on_complete, |
| 902 |
.on_error = s_decoder_test_on_error, |
| 903 |
.user_data = &decoder_data}; |
| 904 |
|
| 905 |
struct aws_event_stream_streaming_decoder decoder; |
| 906 |
aws_event_stream_streaming_decoder_init_from_options(&decoder, allocator, &decoder_options); |
| 907 |
|
| 908 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data)); |
| 909 |
ASSERT_FAILS(aws_event_stream_streaming_decoder_pump(&decoder, &test_buf), "Message validation should not succeed"); |
| 910 |
ASSERT_INT_EQUALS( |
| 911 |
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH, decoder_data.latest_error, "Error should be length mismatch"); |
| 912 |
|
| 913 |
if (decoder_data.latest_payload) { |
| 914 |
aws_mem_release(allocator, decoder_data.latest_payload); |
| 915 |
} |
| 916 |
|
| 917 |
aws_event_stream_streaming_decoder_clean_up(&decoder); |
| 918 |
|
| 919 |
aws_event_stream_headers_list_cleanup(&decoder_data.headers_list); |
| 920 |
|
| 921 |
return 0; |
| 922 |
} |
| 923 |
|
| 924 |
AWS_TEST_CASE(test_streaming_decoder_incoming_message_too_short, s_test_streaming_decoder_incoming_message_too_short_fn) |