| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/event-stream/event_stream.h> |
| 7 |
|
| 8 |
#include <aws/common/encoding.h> |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#ifdef _WIN32 |
| 15 |
# define DELIM "\\" |
| 16 |
#else |
| 17 |
# define DELIM "/" |
| 18 |
#endif |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifdef _MSC_VER |
| 25 |
#pragma warning (disable: 4996 4310) |
| 26 |
#endif |
| 27 |
|
| 28 |
static void write_negative_test_case( |
| 29 |
const char *root_dir, |
| 30 |
const char *test_name, |
| 31 |
const uint8_t *buffer, |
| 32 |
size_t buffer_size, |
| 33 |
const char *err_msg) { |
| 34 |
size_t dir_len = strlen(root_dir); |
| 35 |
size_t encoded_len = strlen("encoded") + strlen("negative") + strlen(test_name) + 2; |
| 36 |
size_t decoded_len = strlen("decoded") + strlen("negative") + strlen(test_name) + 2; |
| 37 |
|
| 38 |
size_t enc_output_file_size = dir_len + 1 + encoded_len + 1; |
| 39 |
char *enc_output_file = (char *)malloc(enc_output_file_size); |
| 40 |
snprintf(enc_output_file, enc_output_file_size, "%s%s%s%s%s%s%s", root_dir, DELIM, "encoded", DELIM, "negative", DELIM, test_name); |
| 41 |
|
| 42 |
size_t dec_output_file_size = dir_len + 1 + decoded_len + 1; |
| 43 |
char *dec_output_file = (char *)malloc(dec_output_file_size); |
| 44 |
snprintf(dec_output_file, dec_output_file_size, "%s%s%s%s%s%s%s", root_dir, DELIM, "decoded", DELIM, "negative", DELIM, test_name); |
| 45 |
|
| 46 |
FILE *enc = fopen(enc_output_file, "w"); |
| 47 |
if (!enc) { |
| 48 |
fprintf(stderr, "couldn't write to %s\n", enc_output_file); |
| 49 |
exit(-1); |
| 50 |
} |
| 51 |
|
| 52 |
fwrite(buffer, sizeof(uint8_t), buffer_size, enc); |
| 53 |
|
| 54 |
fflush(enc); |
| 55 |
fclose(enc); |
| 56 |
|
| 57 |
FILE *dec = fopen(dec_output_file, "w"); |
| 58 |
if (!dec) { |
| 59 |
fprintf(stderr, "couldn't write to %s\n", dec_output_file); |
| 60 |
exit(-1); |
| 61 |
} |
| 62 |
|
| 63 |
fwrite(err_msg, sizeof(char), strlen(err_msg), dec); |
| 64 |
|
| 65 |
fflush(dec); |
| 66 |
fclose(dec); |
| 67 |
|
| 68 |
free(enc_output_file); |
| 69 |
free(dec_output_file); |
| 70 |
} |
| 71 |
|
| 72 |
static void write_positive_test_case( |
| 73 |
const char *root_dir, |
| 74 |
const char *test_name, |
| 75 |
struct aws_event_stream_message *message) { |
| 76 |
size_t dir_len = strlen(root_dir); |
| 77 |
size_t encoded_len = strlen("encoded") + strlen("positive") + strlen(test_name) + 2; |
| 78 |
size_t decoded_len = strlen("decoded") + strlen("positive") + strlen(test_name) + 2; |
| 79 |
|
| 80 |
size_t enc_output_file_size = dir_len + 1 + encoded_len + 1; |
| 81 |
char *enc_output_file = (char *)malloc(enc_output_file_size); |
| 82 |
snprintf(enc_output_file, enc_output_file_size, "%s%s%s%s%s%s%s", root_dir, DELIM, "encoded", DELIM, "positive", DELIM, test_name); |
| 83 |
|
| 84 |
size_t dec_output_file_size = dir_len + 1 + decoded_len + 1; |
| 85 |
char *dec_output_file = (char *)malloc(dec_output_file_size); |
| 86 |
snprintf(dec_output_file, dec_output_file_size, "%s%s%s%s%s%s%s", root_dir, DELIM, "decoded", DELIM, "positive", DELIM, test_name); |
| 87 |
|
| 88 |
FILE *enc = fopen(enc_output_file, "w"); |
| 89 |
if (!enc) { |
| 90 |
fprintf(stderr, "couldn't write to %s\n", enc_output_file); |
| 91 |
exit(-1); |
| 92 |
} |
| 93 |
|
| 94 |
fwrite( |
| 95 |
aws_event_stream_message_buffer(message), sizeof(uint8_t), aws_event_stream_message_total_length(message), enc); |
| 96 |
|
| 97 |
fflush(enc); |
| 98 |
fclose(enc); |
| 99 |
|
| 100 |
FILE *dec = fopen(dec_output_file, "w"); |
| 101 |
if (!dec) { |
| 102 |
fprintf(stderr, "couldn't write to %s\n", dec_output_file); |
| 103 |
exit(-1); |
| 104 |
} |
| 105 |
|
| 106 |
aws_event_stream_message_to_debug_str(dec, message); |
| 107 |
|
| 108 |
fflush(dec); |
| 109 |
fclose(dec); |
| 110 |
free(enc_output_file); |
| 111 |
free(dec_output_file); |
| 112 |
} |
| 113 |
|
| 114 |
int main(void) { |
| 115 |
struct aws_array_list headers; |
| 116 |
struct aws_allocator *alloc = aws_default_allocator(); |
| 117 |
aws_event_stream_headers_list_init(&headers, alloc); |
| 118 |
|
| 119 |
struct aws_event_stream_message msg; |
| 120 |
aws_event_stream_message_init(&msg, alloc, &headers, NULL); |
| 121 |
|
| 122 |
write_positive_test_case(".", "empty_message", &msg); |
| 123 |
|
| 124 |
struct aws_byte_buf payload = aws_byte_buf_from_c_str("{'foo':'bar'}"); |
| 125 |
|
| 126 |
aws_event_stream_message_clean_up(&msg); |
| 127 |
aws_event_stream_message_init(&msg, alloc, &headers, &payload); |
| 128 |
|
| 129 |
write_positive_test_case(".", "payload_no_headers", &msg); |
| 130 |
aws_event_stream_message_clean_up(&msg); |
| 131 |
|
| 132 |
static const char content_type[] = "content-type"; |
| 133 |
static const char json[] = "application/json"; |
| 134 |
|
| 135 |
aws_event_stream_add_string_header(&headers, content_type, sizeof(content_type) - 1, json, sizeof(json) - 1, 0); |
| 136 |
aws_event_stream_message_init(&msg, alloc, &headers, &payload); |
| 137 |
|
| 138 |
write_positive_test_case(".", "payload_one_str_header", &msg); |
| 139 |
|
| 140 |
|
| 141 |
uint32_t original_length = aws_event_stream_message_total_length(&msg); |
| 142 |
uint8_t *buffer_cpy = aws_mem_acquire(alloc, original_length); |
| 143 |
memcpy(buffer_cpy, aws_event_stream_message_buffer(&msg), original_length); |
| 144 |
aws_write_u32(original_length + 1, buffer_cpy); |
| 145 |
|
| 146 |
write_negative_test_case(".", "corrupted_length", buffer_cpy, original_length, "Prelude checksum mismatch"); |
| 147 |
aws_mem_release(alloc, buffer_cpy); |
| 148 |
|
| 149 |
|
| 150 |
buffer_cpy = aws_mem_acquire(alloc, original_length); |
| 151 |
memcpy(buffer_cpy, aws_event_stream_message_buffer(&msg), original_length); |
| 152 |
|
| 153 |
uint32_t original_hdr_len = aws_event_stream_message_headers_len(&msg); |
| 154 |
|
| 155 |
aws_write_u32(original_hdr_len + 1, buffer_cpy + 4); |
| 156 |
|
| 157 |
write_negative_test_case(".", "corrupted_header_len", buffer_cpy, original_length, "Prelude checksum mismatch"); |
| 158 |
aws_mem_release(alloc, buffer_cpy); |
| 159 |
|
| 160 |
|
| 161 |
buffer_cpy = aws_mem_acquire(alloc, original_length); |
| 162 |
memcpy(buffer_cpy, aws_event_stream_message_buffer(&msg), original_length); |
| 163 |
|
| 164 |
uint32_t hdr_len = aws_event_stream_message_headers_len(&msg); |
| 165 |
buffer_cpy[hdr_len + AWS_EVENT_STREAM_PRELUDE_LENGTH + 1] = 'a'; |
| 166 |
|
| 167 |
write_negative_test_case(".", "corrupted_headers", buffer_cpy, original_length, "Message checksum mismatch"); |
| 168 |
aws_mem_release(alloc, buffer_cpy); |
| 169 |
|
| 170 |
buffer_cpy = aws_mem_acquire(alloc, original_length); |
| 171 |
memcpy(buffer_cpy, aws_event_stream_message_buffer(&msg), original_length); |
| 172 |
aws_event_stream_message_clean_up(&msg); |
| 173 |
|
| 174 |
|
| 175 |
aws_event_stream_message_init(&msg, alloc, NULL, &payload); |
| 176 |
((uint8_t *)aws_event_stream_message_payload(&msg))[0] = '['; |
| 177 |
write_negative_test_case( |
| 178 |
".", |
| 179 |
"corrupted_payload", |
| 180 |
aws_event_stream_message_buffer(&msg), |
| 181 |
aws_event_stream_message_total_length(&msg), |
| 182 |
"Message checksum mismatch"); |
| 183 |
|
| 184 |
aws_event_stream_message_clean_up(&msg); |
| 185 |
|
| 186 |
|
| 187 |
static const char event_type[] = "event-type"; |
| 188 |
|
| 189 |
aws_array_list_clear(&headers); |
| 190 |
aws_event_stream_add_int32_header(&headers, event_type, sizeof(event_type) - 1, 0x0000A00C); |
| 191 |
aws_event_stream_message_init(&msg, alloc, &headers, &payload); |
| 192 |
|
| 193 |
write_positive_test_case(".", "int32_header", &msg); |
| 194 |
aws_event_stream_message_clean_up(&msg); |
| 195 |
|
| 196 |
aws_array_list_clear(&headers); |
| 197 |
|
| 198 |
|
| 199 |
aws_event_stream_add_int32_header(&headers, event_type, sizeof(event_type) - 1, 0x0000A00C); |
| 200 |
aws_event_stream_add_string_header(&headers, content_type, sizeof(content_type) - 1, json, sizeof(json) - 1, 0); |
| 201 |
|
| 202 |
static const char bool_false[] = "bool false"; |
| 203 |
aws_event_stream_add_bool_header(&headers, bool_false, sizeof(bool_false) - 1, 0); |
| 204 |
|
| 205 |
static const char bool_true[] = "bool true"; |
| 206 |
aws_event_stream_add_bool_header(&headers, bool_true, sizeof(bool_true) - 1, 1); |
| 207 |
|
| 208 |
static const char byte_hdr[] = "byte"; |
| 209 |
aws_event_stream_add_byte_header(&headers, byte_hdr, sizeof(byte_hdr) - 1, (int8_t)0xcf); |
| 210 |
|
| 211 |
static const char byte_buf_hdr[] = "byte buf"; |
| 212 |
static const char byte_buf[] = "I'm a little teapot!"; |
| 213 |
|
| 214 |
aws_event_stream_add_bytebuf_header( |
| 215 |
&headers, byte_buf_hdr, sizeof(byte_buf_hdr) - 1, (uint8_t *)byte_buf, sizeof(byte_buf) - 1, 0); |
| 216 |
|
| 217 |
static const char timestamp_hdr[] = "timestamp"; |
| 218 |
aws_event_stream_add_timestamp_header(&headers, timestamp_hdr, sizeof(timestamp_hdr) - 1, 8675309); |
| 219 |
|
| 220 |
static const char int16_hdr[] = "int16"; |
| 221 |
aws_event_stream_add_int16_header(&headers, int16_hdr, sizeof(int16_hdr) - 1, 42); |
| 222 |
|
| 223 |
static const char int64_hdr[] = "int64"; |
| 224 |
aws_event_stream_add_int64_header(&headers, int64_hdr, sizeof(int64_hdr) - 1, 42424242); |
| 225 |
|
| 226 |
static const char uuid_hdr[] = "uuid"; |
| 227 |
static const uint8_t uuid[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; |
| 228 |
|
| 229 |
aws_event_stream_add_uuid_header(&headers, uuid_hdr, sizeof(uuid_hdr) - 1, (uint8_t *)uuid); |
| 230 |
aws_event_stream_message_init(&msg, alloc, &headers, &payload); |
| 231 |
|
| 232 |
struct aws_event_stream_message sanity_check_message; |
| 233 |
struct aws_byte_buf message_buffer = |
| 234 |
aws_byte_buf_from_array(aws_event_stream_message_buffer(&msg), aws_event_stream_message_total_length(&msg)); |
| 235 |
|
| 236 |
int err = aws_event_stream_message_from_buffer(&sanity_check_message, alloc, &message_buffer); |
| 237 |
|
| 238 |
if (err) { |
| 239 |
fprintf(stderr, "failed to parse what should have been a valid message\n"); |
| 240 |
exit(-1); |
| 241 |
} |
| 242 |
|
| 243 |
write_positive_test_case(".", "all_headers", &msg); |
| 244 |
aws_event_stream_message_clean_up(&msg); |
| 245 |
aws_event_stream_headers_list_cleanup(&headers); |
| 246 |
|
| 247 |
return 0; |
| 248 |
} |