| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/event-stream/event_stream.h> |
| 7 |
#include <aws/testing/aws_test_harness.h> |
| 8 |
|
| 9 |
static int s_test_outgoing_no_op_valid_fn(struct aws_allocator *allocator, void *ctx) { |
| 10 |
(void)allocator; |
| 11 |
(void)ctx; |
| 12 |
uint8_t test_data[] = { |
| 13 |
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x05, 0xc2, 0x48, 0xeb, 0x7d, 0x98, 0xc8, 0xff}; |
| 14 |
|
| 15 |
struct aws_event_stream_message message; |
| 16 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data)); |
| 17 |
ASSERT_SUCCESS( |
| 18 |
aws_event_stream_message_from_buffer(&message, allocator, &test_buf), |
| 19 |
"Message validation should have succeeded"); |
| 20 |
|
| 21 |
ASSERT_INT_EQUALS( |
| 22 |
0x00000010, aws_event_stream_message_total_length(&message), "Message length should have been 0x10"); |
| 23 |
ASSERT_INT_EQUALS( |
| 24 |
0x00000000, aws_event_stream_message_headers_len(&message), "Headers Length should have been 0x00"); |
| 25 |
ASSERT_INT_EQUALS( |
| 26 |
0x05c248eb, aws_event_stream_message_prelude_crc(&message), "Prelude CRC should have been 0x05c248eb"); |
| 27 |
ASSERT_INT_EQUALS( |
| 28 |
0x7d98c8ff, aws_event_stream_message_message_crc(&message), "Message CRC should have been 0x7d98c8ff"); |
| 29 |
|
| 30 |
aws_event_stream_message_clean_up(&message); |
| 31 |
|
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
AWS_TEST_CASE(test_outgoing_no_op_valid, s_test_outgoing_no_op_valid_fn) |
| 36 |
|
| 37 |
static int s_test_outgoing_application_data_no_headers_valid_fn(struct aws_allocator *allocator, void *ctx) { |
| 38 |
(void)allocator; |
| 39 |
(void)ctx; |
| 40 |
uint8_t test_data[] = {0x00, 0x00, 0x00, 0x1D, 0x00, 0x00, 0x00, 0x00, 0xfd, 0x52, 0x8c, 0x5a, 0x7b, 0x27, 0x66, |
| 41 |
0x6f, 0x6f, 0x27, 0x3a, 0x27, 0x62, 0x61, 0x72, 0x27, 0x7d, 0xc3, 0x65, 0x39, 0x36}; |
| 42 |
|
| 43 |
struct aws_event_stream_message message; |
| 44 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data)); |
| 45 |
|
| 46 |
ASSERT_SUCCESS( |
| 47 |
aws_event_stream_message_from_buffer(&message, allocator, &test_buf), |
| 48 |
"Message validation should have succeeded"); |
| 49 |
|
| 50 |
ASSERT_INT_EQUALS( |
| 51 |
0x0000001D, aws_event_stream_message_total_length(&message), "Message length should have been 0x0000001D"); |
| 52 |
ASSERT_INT_EQUALS( |
| 53 |
0x00000000, aws_event_stream_message_headers_len(&message), "Headers Length should have been 0x00"); |
| 54 |
ASSERT_INT_EQUALS( |
| 55 |
0xfd528c5a, aws_event_stream_message_prelude_crc(&message), "Prelude CRC should have been 0xfd528c5a"); |
| 56 |
|
| 57 |
const char *expected_str = "{'foo':'bar'}"; |
| 58 |
ASSERT_INT_EQUALS( |
| 59 |
strlen(expected_str), |
| 60 |
aws_event_stream_message_payload_len(&message), |
| 61 |
"payload length should have been %d", |
| 62 |
(int)(strlen(expected_str))); |
| 63 |
|
| 64 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 65 |
expected_str, |
| 66 |
strlen(expected_str), |
| 67 |
aws_event_stream_message_payload(&message), |
| 68 |
aws_event_stream_message_payload_len(&message), |
| 69 |
"payload should have been %s", |
| 70 |
expected_str); |
| 71 |
ASSERT_INT_EQUALS( |
| 72 |
0xc3653936, aws_event_stream_message_message_crc(&message), "Message CRC should have been 0xc3653936"); |
| 73 |
|
| 74 |
aws_event_stream_message_clean_up(&message); |
| 75 |
|
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
AWS_TEST_CASE(test_outgoing_application_data_no_headers_valid, s_test_outgoing_application_data_no_headers_valid_fn) |
| 80 |
|
| 81 |
static int s_test_outgoing_application_one_compressed_header_pair_valid_fn(struct aws_allocator *allocator, void *ctx) { |
| 82 |
(void)allocator; |
| 83 |
(void)ctx; |
| 84 |
uint8_t test_data[] = {0x00, 0x00, 0x00, 0x3D, 0x00, 0x00, 0x00, 0x20, 0x07, 0xFD, 0x83, 0x96, 0x0C, |
| 85 |
'c', 'o', 'n', 't', 'e', 'n', 't', '-', 't', 'y', 'p', 'e', 0x07, |
| 86 |
0x00, 0x10, 'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', |
| 87 |
'/', 'j', 's', 'o', 'n', 0x7b, 0x27, 0x66, 0x6f, 0x6f, 0x27, 0x3a, 0x27, |
| 88 |
0x62, 0x61, 0x72, 0x27, 0x7d, 0x8D, 0x9C, 0x08, 0xB1}; |
| 89 |
|
| 90 |
struct aws_event_stream_message message; |
| 91 |
struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data)); |
| 92 |
|
| 93 |
ASSERT_SUCCESS( |
| 94 |
aws_event_stream_message_from_buffer(&message, allocator, &test_buf), |
| 95 |
"Message validation should have succeeded"); |
| 96 |
|
| 97 |
ASSERT_INT_EQUALS( |
| 98 |
0x0000003D, aws_event_stream_message_total_length(&message), "Message length should have been 0x0000003D"); |
| 99 |
ASSERT_INT_EQUALS( |
| 100 |
0x00000020, aws_event_stream_message_headers_len(&message), "Headers Length should have been 0x00000020"); |
| 101 |
ASSERT_INT_EQUALS( |
| 102 |
0x07FD8396, aws_event_stream_message_prelude_crc(&message), "Prelude CRC should have been 0x07FD8396"); |
| 103 |
|
| 104 |
const char *expected_str = "{'foo':'bar'}"; |
| 105 |
ASSERT_INT_EQUALS( |
| 106 |
strlen(expected_str), |
| 107 |
aws_event_stream_message_payload_len(&message), |
| 108 |
"payload length should have been %d", |
| 109 |
(int)(strlen(expected_str))); |
| 110 |
|
| 111 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 112 |
expected_str, |
| 113 |
strlen(expected_str), |
| 114 |
aws_event_stream_message_payload(&message), |
| 115 |
aws_event_stream_message_payload_len(&message), |
| 116 |
"payload should have been %s", |
| 117 |
expected_str); |
| 118 |
|
| 119 |
struct aws_array_list headers; |
| 120 |
ASSERT_SUCCESS(aws_event_stream_headers_list_init(&headers, allocator), "Header initialization failed"); |
| 121 |
|
| 122 |
ASSERT_SUCCESS(aws_event_stream_message_headers(&message, &headers), "Header parsing should have succeeded"); |
| 123 |
ASSERT_INT_EQUALS(1, headers.length, "There should be exactly one header found"); |
| 124 |
struct aws_event_stream_header_value_pair header; |
| 125 |
ASSERT_SUCCESS( |
| 126 |
aws_array_list_front(&headers, &header), |
| 127 |
"accessing the first element of an array of size 1 should have succeeded"); |
| 128 |
|
| 129 |
const char *content_type = "content-type"; |
| 130 |
const char *content_type_value = "application/json"; |
| 131 |
|
| 132 |
struct aws_byte_buf header_name_buf = aws_event_stream_header_name(&header); |
| 133 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 134 |
content_type, |
| 135 |
strlen(content_type), |
| 136 |
header_name_buf.buffer, |
| 137 |
header_name_buf.len, |
| 138 |
"header name should have been %s", |
| 139 |
content_type); |
| 140 |
|
| 141 |
struct aws_byte_buf header_value_buf = aws_event_stream_header_value_as_string(&header); |
| 142 |
|
| 143 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 144 |
content_type_value, |
| 145 |
strlen(content_type_value), |
| 146 |
header_value_buf.buffer, |
| 147 |
header_value_buf.len, |
| 148 |
"header value should have been %s", |
| 149 |
content_type_value); |
| 150 |
|
| 151 |
ASSERT_INT_EQUALS( |
| 152 |
0x8D9C08B1, aws_event_stream_message_message_crc(&message), "Message CRC should have been 0x8D9C08B1"); |
| 153 |
|
| 154 |
aws_event_stream_headers_list_cleanup(&headers); |
| 155 |
aws_event_stream_message_clean_up(&message); |
| 156 |
|
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
AWS_TEST_CASE( |
| 161 |
test_outgoing_application_one_compressed_header_pair_valid, |
| 162 |
s_test_outgoing_application_one_compressed_header_pair_valid_fn) |
| 163 |
|
| 164 |
static const size_t TOO_MANY_HEADERS_BUFFER_SIZE = 1024 * 1024 - 16; |
| 165 |
|
| 166 |
static int s_test_read_message_headers_too_many_fn(struct aws_allocator *allocator, void *ctx) { |
| 167 |
(void)allocator; |
| 168 |
(void)ctx; |
| 169 |
|
| 170 |
struct aws_byte_buf header_buffer; |
| 171 |
aws_byte_buf_init(&header_buffer, allocator, TOO_MANY_HEADERS_BUFFER_SIZE); |
| 172 |
|
| 173 |
|
| 174 |
aws_secure_zero(header_buffer.buffer, TOO_MANY_HEADERS_BUFFER_SIZE); |
| 175 |
|
| 176 |
struct aws_array_list headers; |
| 177 |
aws_array_list_init_dynamic(&headers, allocator, 8, sizeof(struct aws_event_stream_header_value_pair)); |
| 178 |
|
| 179 |
int result = |
| 180 |
aws_event_stream_read_headers_from_buffer(&headers, header_buffer.buffer, TOO_MANY_HEADERS_BUFFER_SIZE); |
| 181 |
int last_error = aws_last_error(); |
| 182 |
ASSERT_FAILS(result); |
| 183 |
ASSERT_INT_EQUALS(AWS_ERROR_EVENT_STREAM_MESSAGE_TOO_MANY_HEADERS, last_error); |
| 184 |
ASSERT_TRUE(aws_array_list_length(&headers) <= AWS_EVENT_STREAM_MESSAGE_MAX_HEADERS); |
| 185 |
|
| 186 |
aws_array_list_clean_up(&headers); |
| 187 |
aws_byte_buf_clean_up(&header_buffer); |
| 188 |
|
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
AWS_TEST_CASE(test_read_message_headers_too_many, s_test_read_message_headers_too_many_fn) |