| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/common/encoding.h> |
| 6 |
#include <aws/event-stream/event_stream.h> |
| 7 |
#include <aws/testing/aws_test_harness.h> |
| 8 |
|
| 9 |
static int s_test_incoming_no_op_valid_fn(struct aws_allocator *allocator, void *ctx) { |
| 10 |
(void)ctx; |
| 11 |
uint8_t expected_data[] = { |
| 12 |
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x05, 0xc2, 0x48, 0xeb, 0x7d, 0x98, 0xc8, 0xff}; |
| 13 |
|
| 14 |
struct aws_event_stream_message message; |
| 15 |
ASSERT_SUCCESS( |
| 16 |
aws_event_stream_message_init(&message, allocator, NULL, NULL), "Message validation should have succeeded"); |
| 17 |
|
| 18 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 19 |
expected_data, |
| 20 |
sizeof(expected_data), |
| 21 |
aws_event_stream_message_buffer(&message), |
| 22 |
aws_event_stream_message_total_length(&message), |
| 23 |
"buffers didn't match"); |
| 24 |
|
| 25 |
aws_event_stream_message_clean_up(&message); |
| 26 |
|
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
AWS_TEST_CASE(test_incoming_no_op_valid, s_test_incoming_no_op_valid_fn) |
| 31 |
|
| 32 |
static int s_test_incoming_application_data_no_headers_valid_fn(struct aws_allocator *allocator, void *ctx) { |
| 33 |
(void)ctx; |
| 34 |
uint8_t expected_data[] = {0x00, 0x00, 0x00, 0x1D, 0x00, 0x00, 0x00, 0x00, 0xfd, 0x52, 0x8c, 0x5a, 0x7b, 0x27, 0x66, |
| 35 |
0x6f, 0x6f, 0x27, 0x3a, 0x27, 0x62, 0x61, 0x72, 0x27, 0x7d, 0xc3, 0x65, 0x39, 0x36}; |
| 36 |
|
| 37 |
const char *test_str = "{'foo':'bar'}"; |
| 38 |
struct aws_event_stream_message message; |
| 39 |
struct aws_byte_buf test_buf = aws_byte_buf_from_c_str(test_str); |
| 40 |
ASSERT_SUCCESS( |
| 41 |
aws_event_stream_message_init(&message, allocator, NULL, &test_buf), |
| 42 |
"Message validation should have succeeded"); |
| 43 |
|
| 44 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 45 |
expected_data, |
| 46 |
sizeof(expected_data), |
| 47 |
aws_event_stream_message_buffer(&message), |
| 48 |
aws_event_stream_message_total_length(&message), |
| 49 |
"buffers didn't match"); |
| 50 |
|
| 51 |
aws_event_stream_message_clean_up(&message); |
| 52 |
|
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
AWS_TEST_CASE(test_incoming_application_data_no_headers_valid, s_test_incoming_application_data_no_headers_valid_fn) |
| 57 |
|
| 58 |
static int s_test_incoming_application_one_compressed_header_pair_valid_fn(struct aws_allocator *allocator, void *ctx) { |
| 59 |
(void)ctx; |
| 60 |
uint8_t expected_data[] = {0x00, 0x00, 0x00, 0x3D, 0x00, 0x00, 0x00, 0x20, 0x07, 0xFD, 0x83, 0x96, 0x0C, |
| 61 |
'c', 'o', 'n', 't', 'e', 'n', 't', '-', 't', 'y', 'p', 'e', 0x07, |
| 62 |
0x00, 0x10, 'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', |
| 63 |
'/', 'j', 's', 'o', 'n', 0x7b, 0x27, 0x66, 0x6f, 0x6f, 0x27, 0x3a, 0x27, |
| 64 |
0x62, 0x61, 0x72, 0x27, 0x7d, 0x8D, 0x9C, 0x08, 0xB1}; |
| 65 |
|
| 66 |
const char *test_str = "{'foo':'bar'}"; |
| 67 |
struct aws_event_stream_message message; |
| 68 |
|
| 69 |
struct aws_array_list headers; |
| 70 |
ASSERT_SUCCESS(aws_event_stream_headers_list_init(&headers, allocator), "Header initialization failed"); |
| 71 |
|
| 72 |
const char *header_name = "content-type"; |
| 73 |
const char *header_value = "application/json"; |
| 74 |
|
| 75 |
ASSERT_SUCCESS( |
| 76 |
aws_event_stream_add_string_header( |
| 77 |
&headers, header_name, (int8_t)strlen(header_name), header_value, (uint16_t)strlen(header_value), 0), |
| 78 |
"Adding a header should have succeeded."); |
| 79 |
|
| 80 |
struct aws_byte_buf test_buf = aws_byte_buf_from_c_str(test_str); |
| 81 |
|
| 82 |
ASSERT_SUCCESS( |
| 83 |
aws_event_stream_message_init(&message, allocator, &headers, &test_buf), |
| 84 |
"Message validation should have succeeded"); |
| 85 |
|
| 86 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 87 |
expected_data, |
| 88 |
sizeof(expected_data), |
| 89 |
aws_event_stream_message_buffer(&message), |
| 90 |
aws_event_stream_message_total_length(&message), |
| 91 |
"buffers didn't match"); |
| 92 |
|
| 93 |
aws_event_stream_headers_list_cleanup(&headers); |
| 94 |
aws_event_stream_message_clean_up(&message); |
| 95 |
|
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
AWS_TEST_CASE( |
| 100 |
test_incoming_application_one_compressed_header_pair_valid, |
| 101 |
s_test_incoming_application_one_compressed_header_pair_valid_fn) |
| 102 |
|
| 103 |
static int s_test_incoming_application_int32_header_valid_fn(struct aws_allocator *allocator, void *ctx) { |
| 104 |
(void)ctx; |
| 105 |
uint8_t expected_data[] = {0x00, 0x00, 0x00, 0x2B, 0x00, 0x00, 0x00, 0x0E, 0x34, 0x8B, 0xEC, 0x7B, 0x08, 'e', 'v', |
| 106 |
'e', 'n', 't', '-', 'i', 'd', 0x04, 0x00, 0x00, 0xA0, 0x0C, 0x7b, 0x27, 0x66, 0x6f, |
| 107 |
0x6f, 0x27, 0x3a, 0x27, 0x62, 0x61, 0x72, 0x27, 0x7d, 0xD3, 0x89, 0x02, 0x85}; |
| 108 |
|
| 109 |
const char *test_str = "{'foo':'bar'}"; |
| 110 |
struct aws_event_stream_message message; |
| 111 |
|
| 112 |
struct aws_array_list headers; |
| 113 |
ASSERT_SUCCESS(aws_event_stream_headers_list_init(&headers, allocator), "Header initialization failed"); |
| 114 |
|
| 115 |
const char *header_name = "event-id"; |
| 116 |
|
| 117 |
ASSERT_SUCCESS( |
| 118 |
aws_event_stream_add_int32_header(&headers, header_name, (int8_t)strlen(header_name), 0x0000A00c), |
| 119 |
"Adding a header should have succeeded."); |
| 120 |
|
| 121 |
struct aws_byte_buf test_buf = aws_byte_buf_from_c_str(test_str); |
| 122 |
|
| 123 |
ASSERT_SUCCESS(aws_event_stream_message_init(&message, allocator, &headers, &test_buf), "buffers didn't match"); |
| 124 |
|
| 125 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 126 |
expected_data, |
| 127 |
sizeof(expected_data), |
| 128 |
aws_event_stream_message_buffer(&message), |
| 129 |
aws_event_stream_message_total_length(&message), |
| 130 |
"buffers didn't match"); |
| 131 |
|
| 132 |
aws_array_list_clean_up(&headers); |
| 133 |
aws_event_stream_message_clean_up(&message); |
| 134 |
|
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
AWS_TEST_CASE(test_incoming_application_int32_header_valid, s_test_incoming_application_int32_header_valid_fn) |