| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/cbor.h> |
| 7 |
#include <float.h> |
| 8 |
#include <math.h> |
| 9 |
|
| 10 |
#include <aws/testing/aws_test_harness.h> |
| 11 |
|
| 12 |
#define CBOR_TEST_CASE(NAME) \ |
| 13 |
AWS_TEST_CASE(NAME, s_test_##NAME); \ |
| 14 |
static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) |
| 15 |
|
| 16 |
CBOR_TEST_CASE(cbor_encode_decode_int_test) { |
| 17 |
(void)allocator; |
| 18 |
(void)ctx; |
| 19 |
aws_common_library_init(allocator); |
| 20 |
enum { VALUE_NUM = 6 }; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
uint64_t values[VALUE_NUM] = {23, 24, UINT8_MAX + 1, UINT16_MAX + 1U, UINT32_MAX + 1LLU, UINT64_MAX}; |
| 30 |
uint64_t expected_encoded_len[VALUE_NUM] = {1, 2, 3, 5, 9, 9}; |
| 31 |
|
| 32 |
size_t encoded_len = 0; |
| 33 |
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); |
| 34 |
|
| 35 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 36 |
aws_cbor_encoder_write_uint(encoder, values[i]); |
| 37 |
struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 38 |
ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); |
| 39 |
encoded_len = cursor.len; |
| 40 |
} |
| 41 |
|
| 42 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 43 |
aws_cbor_encoder_write_negint(encoder, values[i]); |
| 44 |
struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 45 |
ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); |
| 46 |
encoded_len = cursor.len; |
| 47 |
} |
| 48 |
struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 49 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); |
| 50 |
|
| 51 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 52 |
uint64_t result; |
| 53 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_unsigned_int_val(decoder, &result)); |
| 54 |
ASSERT_UINT_EQUALS(values[i], result); |
| 55 |
} |
| 56 |
|
| 57 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 58 |
uint64_t result; |
| 59 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_negative_int_val(decoder, &result)); |
| 60 |
ASSERT_UINT_EQUALS(values[i], result); |
| 61 |
} |
| 62 |
|
| 63 |
ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); |
| 64 |
|
| 65 |
aws_cbor_encoder_destroy(encoder); |
| 66 |
aws_cbor_decoder_destroy(decoder); |
| 67 |
aws_common_library_clean_up(); |
| 68 |
return AWS_OP_SUCCESS; |
| 69 |
} |
| 70 |
|
| 71 |
CBOR_TEST_CASE(cbor_encode_decode_double_test) { |
| 72 |
(void)allocator; |
| 73 |
(void)ctx; |
| 74 |
aws_common_library_init(allocator); |
| 75 |
enum { VALUE_NUM = 10 }; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
double values[VALUE_NUM] = {1.0, -1.0, 1.1, 1.1f, -1.1f, INFINITY, FLT_MAX, DBL_MAX, DBL_MIN, HUGE_VAL}; |
| 90 |
uint64_t expected_encoded_len[VALUE_NUM] = {1, 1, 9, 5, 5, 5, 5, 9, 9, 5}; |
| 91 |
|
| 92 |
int expected_encoded_type[VALUE_NUM] = { |
| 93 |
AWS_CBOR_TYPE_UINT, |
| 94 |
AWS_CBOR_TYPE_NEGINT, |
| 95 |
AWS_CBOR_TYPE_FLOAT, |
| 96 |
AWS_CBOR_TYPE_FLOAT, |
| 97 |
AWS_CBOR_TYPE_FLOAT, |
| 98 |
AWS_CBOR_TYPE_FLOAT, |
| 99 |
AWS_CBOR_TYPE_FLOAT, |
| 100 |
AWS_CBOR_TYPE_FLOAT, |
| 101 |
AWS_CBOR_TYPE_FLOAT, |
| 102 |
AWS_CBOR_TYPE_FLOAT, |
| 103 |
}; |
| 104 |
|
| 105 |
size_t encoded_len = 0; |
| 106 |
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); |
| 107 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 108 |
aws_cbor_encoder_write_float(encoder, values[i]); |
| 109 |
struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 110 |
ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); |
| 111 |
encoded_len = cursor.len; |
| 112 |
} |
| 113 |
struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 114 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); |
| 115 |
|
| 116 |
size_t index = 0; |
| 117 |
uint64_t result = 0; |
| 118 |
enum aws_cbor_type out_type = AWS_CBOR_TYPE_UNKNOWN; |
| 119 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 120 |
ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); |
| 121 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_unsigned_int_val(decoder, &result)); |
| 122 |
ASSERT_TRUE(values[index++] == result); |
| 123 |
|
| 124 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 125 |
ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); |
| 126 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_negative_int_val(decoder, &result)); |
| 127 |
|
| 128 |
ASSERT_TRUE((-1 - values[index++]) == result); |
| 129 |
|
| 130 |
double double_result = 0; |
| 131 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 132 |
ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); |
| 133 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); |
| 134 |
ASSERT_TRUE(values[index++] == double_result); |
| 135 |
|
| 136 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 137 |
ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); |
| 138 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); |
| 139 |
ASSERT_TRUE(values[index++] == double_result); |
| 140 |
|
| 141 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 142 |
ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); |
| 143 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); |
| 144 |
ASSERT_TRUE(values[index++] == double_result); |
| 145 |
|
| 146 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 147 |
ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); |
| 148 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); |
| 149 |
ASSERT_TRUE(values[index++] == double_result); |
| 150 |
|
| 151 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 152 |
ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); |
| 153 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); |
| 154 |
ASSERT_TRUE(values[index++] == double_result); |
| 155 |
|
| 156 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 157 |
ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); |
| 158 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); |
| 159 |
ASSERT_TRUE(values[index++] == double_result); |
| 160 |
|
| 161 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 162 |
ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); |
| 163 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); |
| 164 |
ASSERT_TRUE(values[index++] == double_result); |
| 165 |
|
| 166 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 167 |
ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); |
| 168 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); |
| 169 |
ASSERT_TRUE(values[index++] == double_result); |
| 170 |
|
| 171 |
ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); |
| 172 |
|
| 173 |
aws_cbor_encoder_destroy(encoder); |
| 174 |
aws_cbor_decoder_destroy(decoder); |
| 175 |
aws_common_library_clean_up(); |
| 176 |
return AWS_OP_SUCCESS; |
| 177 |
} |
| 178 |
|
| 179 |
CBOR_TEST_CASE(cbor_encode_decode_bool_test) { |
| 180 |
(void)allocator; |
| 181 |
(void)ctx; |
| 182 |
aws_common_library_init(allocator); |
| 183 |
enum { VALUE_NUM = 2 }; |
| 184 |
bool values[VALUE_NUM] = {true, false}; |
| 185 |
uint64_t expected_encoded_len[VALUE_NUM] = {1, 1}; |
| 186 |
|
| 187 |
size_t encoded_len = 0; |
| 188 |
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); |
| 189 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 190 |
aws_cbor_encoder_write_bool(encoder, values[i]); |
| 191 |
struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 192 |
ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); |
| 193 |
encoded_len = cursor.len; |
| 194 |
} |
| 195 |
struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 196 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); |
| 197 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 198 |
bool result; |
| 199 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_boolean_val(decoder, &result)); |
| 200 |
ASSERT_UINT_EQUALS(values[i], result); |
| 201 |
} |
| 202 |
|
| 203 |
ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); |
| 204 |
|
| 205 |
aws_cbor_encoder_destroy(encoder); |
| 206 |
aws_cbor_decoder_destroy(decoder); |
| 207 |
aws_common_library_clean_up(); |
| 208 |
return AWS_OP_SUCCESS; |
| 209 |
} |
| 210 |
|
| 211 |
CBOR_TEST_CASE(cbor_encode_decode_bytesstr_str_test) { |
| 212 |
(void)allocator; |
| 213 |
(void)ctx; |
| 214 |
aws_common_library_init(allocator); |
| 215 |
struct aws_byte_cursor val_1 = aws_byte_cursor_from_c_str("my test"); |
| 216 |
struct aws_byte_cursor val_2 = aws_byte_cursor_from_c_str("write more tests"); |
| 217 |
|
| 218 |
enum { VALUE_NUM = 2 }; |
| 219 |
struct aws_byte_cursor values[VALUE_NUM] = {val_1, val_2}; |
| 220 |
uint64_t expected_encoded_len[VALUE_NUM] = {1 + val_1.len, 1 + val_2.len}; |
| 221 |
|
| 222 |
size_t encoded_len = 0; |
| 223 |
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); |
| 224 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 225 |
aws_cbor_encoder_write_text(encoder, values[i]); |
| 226 |
struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 227 |
ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); |
| 228 |
encoded_len = cursor.len; |
| 229 |
} |
| 230 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 231 |
aws_cbor_encoder_write_bytes(encoder, values[i]); |
| 232 |
struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 233 |
ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); |
| 234 |
encoded_len = cursor.len; |
| 235 |
} |
| 236 |
struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 237 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); |
| 238 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 239 |
struct aws_byte_cursor result; |
| 240 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_text_val(decoder, &result)); |
| 241 |
ASSERT_TRUE(aws_byte_cursor_eq(&result, &values[i])); |
| 242 |
} |
| 243 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 244 |
struct aws_byte_cursor result; |
| 245 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_bytes_val(decoder, &result)); |
| 246 |
ASSERT_TRUE(aws_byte_cursor_eq(&result, &values[i])); |
| 247 |
} |
| 248 |
|
| 249 |
ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); |
| 250 |
|
| 251 |
aws_cbor_encoder_destroy(encoder); |
| 252 |
aws_cbor_decoder_destroy(decoder); |
| 253 |
aws_common_library_clean_up(); |
| 254 |
return AWS_OP_SUCCESS; |
| 255 |
} |
| 256 |
|
| 257 |
CBOR_TEST_CASE(cbor_encode_decode_array_map_test) { |
| 258 |
(void)allocator; |
| 259 |
(void)ctx; |
| 260 |
aws_common_library_init(allocator); |
| 261 |
struct aws_byte_cursor val_1 = aws_byte_cursor_from_c_str("my test"); |
| 262 |
struct aws_byte_cursor val_2 = aws_byte_cursor_from_c_str("write more tests"); |
| 263 |
|
| 264 |
enum { VALUE_NUM = 2 }; |
| 265 |
struct aws_byte_cursor values[VALUE_NUM] = {val_1, val_2}; |
| 266 |
uint64_t expected_encoded_len[VALUE_NUM] = {1 + val_1.len, 1 + val_2.len}; |
| 267 |
|
| 268 |
size_t encoded_len = 0; |
| 269 |
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); |
| 270 |
|
| 271 |
|
| 272 |
aws_cbor_encoder_write_array_start(encoder, 2); |
| 273 |
struct aws_byte_cursor encoded_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 274 |
|
| 275 |
ASSERT_UINT_EQUALS(encoded_len + 1, encoded_cursor.len); |
| 276 |
encoded_len = encoded_cursor.len; |
| 277 |
|
| 278 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 279 |
aws_cbor_encoder_write_text(encoder, values[i]); |
| 280 |
struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 281 |
ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); |
| 282 |
encoded_len = cursor.len; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
aws_cbor_encoder_write_map_start(encoder, 1); |
| 287 |
encoded_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 288 |
|
| 289 |
ASSERT_UINT_EQUALS(encoded_len + 1, encoded_cursor.len); |
| 290 |
encoded_len = encoded_cursor.len; |
| 291 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 292 |
aws_cbor_encoder_write_bytes(encoder, values[i]); |
| 293 |
struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 294 |
ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); |
| 295 |
encoded_len = cursor.len; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
aws_cbor_encoder_write_array_start(encoder, UINT16_MAX + 1); |
| 300 |
encoded_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 301 |
|
| 302 |
ASSERT_UINT_EQUALS(encoded_len + 5, encoded_cursor.len); |
| 303 |
encoded_len = encoded_cursor.len; |
| 304 |
|
| 305 |
aws_cbor_encoder_write_map_start(encoder, UINT16_MAX + 1); |
| 306 |
encoded_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 307 |
|
| 308 |
ASSERT_UINT_EQUALS(encoded_len + 5, encoded_cursor.len); |
| 309 |
encoded_len = encoded_cursor.len; |
| 310 |
|
| 311 |
struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 312 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); |
| 313 |
|
| 314 |
uint64_t element_size = 0; |
| 315 |
aws_cbor_decoder_pop_next_array_start(decoder, &element_size); |
| 316 |
ASSERT_UINT_EQUALS(element_size, 2); |
| 317 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 318 |
struct aws_byte_cursor result; |
| 319 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_text_val(decoder, &result)); |
| 320 |
ASSERT_TRUE(aws_byte_cursor_eq(&result, &values[i])); |
| 321 |
} |
| 322 |
aws_cbor_decoder_pop_next_map_start(decoder, &element_size); |
| 323 |
ASSERT_UINT_EQUALS(element_size, 1); |
| 324 |
for (size_t i = 0; i < VALUE_NUM; i++) { |
| 325 |
struct aws_byte_cursor result; |
| 326 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_bytes_val(decoder, &result)); |
| 327 |
ASSERT_TRUE(aws_byte_cursor_eq(&result, &values[i])); |
| 328 |
} |
| 329 |
|
| 330 |
ASSERT_FAILS(aws_cbor_decoder_pop_next_array_start(decoder, &element_size)); |
| 331 |
ASSERT_INT_EQUALS(AWS_ERROR_CBOR_RESOURCE_LIMIT_EXCEEDED, aws_last_error()); |
| 332 |
|
| 333 |
aws_cbor_decoder_reset_src(decoder, (struct aws_byte_cursor){0}); |
| 334 |
|
| 335 |
ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); |
| 336 |
|
| 337 |
aws_cbor_encoder_destroy(encoder); |
| 338 |
aws_cbor_decoder_destroy(decoder); |
| 339 |
aws_common_library_clean_up(); |
| 340 |
return AWS_OP_SUCCESS; |
| 341 |
} |
| 342 |
|
| 343 |
CBOR_TEST_CASE(cbor_encode_decode_simple_value_test) { |
| 344 |
(void)allocator; |
| 345 |
(void)ctx; |
| 346 |
aws_common_library_init(allocator); |
| 347 |
|
| 348 |
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); |
| 349 |
aws_cbor_encoder_write_null(encoder); |
| 350 |
aws_cbor_encoder_write_undefined(encoder); |
| 351 |
struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 352 |
|
| 353 |
ASSERT_UINT_EQUALS(2, final_cursor.len); |
| 354 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); |
| 355 |
|
| 356 |
enum aws_cbor_type out_type = AWS_CBOR_TYPE_UNKNOWN; |
| 357 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 358 |
ASSERT_UINT_EQUALS(out_type, AWS_CBOR_TYPE_NULL); |
| 359 |
ASSERT_SUCCESS(aws_cbor_decoder_consume_next_single_element(decoder)); |
| 360 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 361 |
ASSERT_UINT_EQUALS(out_type, AWS_CBOR_TYPE_UNDEFINED); |
| 362 |
ASSERT_SUCCESS(aws_cbor_decoder_consume_next_single_element(decoder)); |
| 363 |
ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); |
| 364 |
|
| 365 |
aws_cbor_encoder_destroy(encoder); |
| 366 |
aws_cbor_decoder_destroy(decoder); |
| 367 |
aws_common_library_clean_up(); |
| 368 |
return AWS_OP_SUCCESS; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
CBOR_TEST_CASE(cbor_encode_decode_indef_test) { |
| 373 |
(void)allocator; |
| 374 |
(void)ctx; |
| 375 |
aws_common_library_init(allocator); |
| 376 |
struct aws_byte_cursor val_1 = aws_byte_cursor_from_c_str("my test"); |
| 377 |
struct aws_byte_cursor val_2 = aws_byte_cursor_from_c_str("write more tests"); |
| 378 |
|
| 379 |
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); |
| 380 |
|
| 381 |
|
| 382 |
aws_cbor_encoder_write_indef_map_start(encoder); |
| 383 |
|
| 384 |
aws_cbor_encoder_write_text(encoder, val_1); |
| 385 |
|
| 386 |
aws_cbor_encoder_write_indef_array_start(encoder); |
| 387 |
|
| 388 |
aws_cbor_encoder_write_indef_text_start(encoder); |
| 389 |
aws_cbor_encoder_write_text(encoder, val_1); |
| 390 |
aws_cbor_encoder_write_text(encoder, val_2); |
| 391 |
aws_cbor_encoder_write_break(encoder); |
| 392 |
|
| 393 |
aws_cbor_encoder_write_indef_bytes_start(encoder); |
| 394 |
aws_cbor_encoder_write_bytes(encoder, val_1); |
| 395 |
aws_cbor_encoder_write_bytes(encoder, val_2); |
| 396 |
aws_cbor_encoder_write_break(encoder); |
| 397 |
|
| 398 |
aws_cbor_encoder_write_tag(encoder, AWS_CBOR_TAG_DECIMAL_FRACTION); |
| 399 |
aws_cbor_encoder_write_indef_array_start(encoder); |
| 400 |
aws_cbor_encoder_write_indef_bytes_start(encoder); |
| 401 |
aws_cbor_encoder_write_bytes(encoder, val_1); |
| 402 |
aws_cbor_encoder_write_bytes(encoder, val_2); |
| 403 |
aws_cbor_encoder_write_break(encoder); |
| 404 |
aws_cbor_encoder_write_break(encoder); |
| 405 |
|
| 406 |
aws_cbor_encoder_write_break(encoder); |
| 407 |
|
| 408 |
aws_cbor_encoder_write_break(encoder); |
| 409 |
|
| 410 |
struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 411 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); |
| 412 |
|
| 413 |
enum aws_cbor_type out_type = AWS_CBOR_TYPE_UNKNOWN; |
| 414 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 415 |
ASSERT_UINT_EQUALS(out_type, AWS_CBOR_TYPE_INDEF_MAP_START); |
| 416 |
|
| 417 |
|
| 418 |
ASSERT_SUCCESS(aws_cbor_decoder_consume_next_whole_data_item(decoder)); |
| 419 |
|
| 420 |
ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); |
| 421 |
|
| 422 |
aws_cbor_encoder_destroy(encoder); |
| 423 |
aws_cbor_decoder_destroy(decoder); |
| 424 |
aws_common_library_clean_up(); |
| 425 |
return AWS_OP_SUCCESS; |
| 426 |
} |
| 427 |
|
| 428 |
CBOR_TEST_CASE(cbor_decode_error_handling_test) { |
| 429 |
(void)allocator; |
| 430 |
(void)ctx; |
| 431 |
aws_common_library_init(allocator); |
| 432 |
|
| 433 |
uint8_t invalid_data[] = {0xFE}; |
| 434 |
struct aws_byte_cursor invalid_cbor = aws_byte_cursor_from_array(invalid_data, sizeof(invalid_data)); |
| 435 |
|
| 436 |
enum aws_cbor_type out_type = AWS_CBOR_TYPE_UNKNOWN; |
| 437 |
|
| 438 |
|
| 439 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, invalid_cbor); |
| 440 |
ASSERT_FAILS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 441 |
ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_CBOR, aws_last_error()); |
| 442 |
|
| 443 |
|
| 444 |
struct aws_byte_cursor empty = {0}; |
| 445 |
aws_cbor_decoder_reset_src(decoder, empty); |
| 446 |
ASSERT_FAILS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 447 |
ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_CBOR, aws_last_error()); |
| 448 |
|
| 449 |
|
| 450 |
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); |
| 451 |
uint64_t val = 1; |
| 452 |
aws_cbor_encoder_write_uint(encoder, val); |
| 453 |
struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 454 |
aws_cbor_decoder_reset_src(decoder, final_cursor); |
| 455 |
uint64_t out = 0; |
| 456 |
ASSERT_FAILS(aws_cbor_decoder_pop_next_array_start(decoder, &out)); |
| 457 |
ASSERT_UINT_EQUALS(AWS_ERROR_CBOR_UNEXPECTED_TYPE, aws_last_error()); |
| 458 |
|
| 459 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_unsigned_int_val(decoder, &out)); |
| 460 |
ASSERT_UINT_EQUALS(val, out); |
| 461 |
|
| 462 |
ASSERT_FAILS(aws_cbor_decoder_consume_next_whole_data_item(decoder)); |
| 463 |
ASSERT_FAILS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 464 |
ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_CBOR, aws_last_error()); |
| 465 |
|
| 466 |
|
| 467 |
struct aws_byte_cursor val_1 = aws_byte_cursor_from_c_str("my test"); |
| 468 |
aws_cbor_encoder_reset(encoder); |
| 469 |
aws_cbor_encoder_write_map_start(encoder, 1); |
| 470 |
|
| 471 |
aws_cbor_encoder_write_text(encoder, val_1); |
| 472 |
|
| 473 |
aws_cbor_encoder_write_array_start(encoder, 1); |
| 474 |
aws_cbor_encoder_write_tag(encoder, AWS_CBOR_TAG_NEGATIVE_BIGNUM); |
| 475 |
aws_cbor_encoder_write_bytes(encoder, val_1); |
| 476 |
final_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 477 |
aws_cbor_decoder_reset_src(decoder, final_cursor); |
| 478 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 479 |
ASSERT_UINT_EQUALS(AWS_CBOR_TYPE_MAP_START, out_type); |
| 480 |
ASSERT_SUCCESS(aws_cbor_decoder_consume_next_whole_data_item(decoder)); |
| 481 |
ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); |
| 482 |
|
| 483 |
aws_cbor_decoder_destroy(decoder); |
| 484 |
aws_cbor_encoder_destroy(encoder); |
| 485 |
aws_common_library_clean_up(); |
| 486 |
return AWS_OP_SUCCESS; |
| 487 |
} |
| 488 |
|
| 489 |
CBOR_TEST_CASE(cbor_decode_resource_limit_container_size_test) { |
| 490 |
(void)ctx; |
| 491 |
aws_common_library_init(allocator); |
| 492 |
|
| 493 |
|
| 494 |
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); |
| 495 |
aws_cbor_encoder_write_array_start(encoder, 10001); |
| 496 |
struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 497 |
|
| 498 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, cursor); |
| 499 |
uint64_t out_size = 0; |
| 500 |
ASSERT_FAILS(aws_cbor_decoder_pop_next_array_start(decoder, &out_size)); |
| 501 |
ASSERT_INT_EQUALS(AWS_ERROR_CBOR_RESOURCE_LIMIT_EXCEEDED, aws_last_error()); |
| 502 |
|
| 503 |
|
| 504 |
aws_cbor_encoder_reset(encoder); |
| 505 |
aws_cbor_encoder_write_map_start(encoder, 10001); |
| 506 |
cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 507 |
aws_cbor_decoder_reset_src(decoder, cursor); |
| 508 |
ASSERT_FAILS(aws_cbor_decoder_pop_next_map_start(decoder, &out_size)); |
| 509 |
ASSERT_INT_EQUALS(AWS_ERROR_CBOR_RESOURCE_LIMIT_EXCEEDED, aws_last_error()); |
| 510 |
|
| 511 |
|
| 512 |
aws_cbor_encoder_reset(encoder); |
| 513 |
aws_cbor_encoder_write_array_start(encoder, 10000); |
| 514 |
cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 515 |
aws_cbor_decoder_reset_src(decoder, cursor); |
| 516 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_array_start(decoder, &out_size)); |
| 517 |
ASSERT_UINT_EQUALS(10000, out_size); |
| 518 |
|
| 519 |
aws_cbor_decoder_destroy(decoder); |
| 520 |
aws_cbor_encoder_destroy(encoder); |
| 521 |
aws_common_library_clean_up(); |
| 522 |
return AWS_OP_SUCCESS; |
| 523 |
} |
| 524 |
|
| 525 |
CBOR_TEST_CASE(cbor_decode_resource_limit_depth_test) { |
| 526 |
(void)ctx; |
| 527 |
aws_common_library_init(allocator); |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
uint8_t data[130]; |
| 532 |
memset(data, 0x81, 129); |
| 533 |
data[129] = 0x01; |
| 534 |
struct aws_byte_cursor cursor = aws_byte_cursor_from_array(data, sizeof(data)); |
| 535 |
|
| 536 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, cursor); |
| 537 |
ASSERT_FAILS(aws_cbor_decoder_consume_next_whole_data_item(decoder)); |
| 538 |
ASSERT_INT_EQUALS(AWS_ERROR_CBOR_RESOURCE_LIMIT_EXCEEDED, aws_last_error()); |
| 539 |
|
| 540 |
|
| 541 |
uint8_t ok_data[128]; |
| 542 |
memset(ok_data, 0x81, 127); |
| 543 |
ok_data[127] = 0x01; |
| 544 |
cursor = aws_byte_cursor_from_array(ok_data, sizeof(ok_data)); |
| 545 |
aws_cbor_decoder_reset_src(decoder, cursor); |
| 546 |
ASSERT_SUCCESS(aws_cbor_decoder_consume_next_whole_data_item(decoder)); |
| 547 |
|
| 548 |
aws_cbor_decoder_destroy(decoder); |
| 549 |
aws_common_library_clean_up(); |
| 550 |
return AWS_OP_SUCCESS; |
| 551 |
} |
| 552 |
|
| 553 |
CBOR_TEST_CASE(cbor_decode_remaining_length_after_peek_test) { |
| 554 |
(void)allocator; |
| 555 |
(void)ctx; |
| 556 |
aws_common_library_init(allocator); |
| 557 |
|
| 558 |
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); |
| 559 |
aws_cbor_encoder_write_uint(encoder, 42); |
| 560 |
aws_cbor_encoder_write_uint(encoder, 100); |
| 561 |
struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 562 |
size_t total_len = cursor.len; |
| 563 |
|
| 564 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, cursor); |
| 565 |
ASSERT_UINT_EQUALS(total_len, aws_cbor_decoder_get_remaining_length(decoder)); |
| 566 |
ASSERT_UINT_EQUALS(total_len, aws_cbor_decoder_get_unconsumed_length(decoder)); |
| 567 |
|
| 568 |
|
| 569 |
enum aws_cbor_type out_type; |
| 570 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 571 |
ASSERT_TRUE(aws_cbor_decoder_get_remaining_length(decoder) < total_len); |
| 572 |
ASSERT_UINT_EQUALS(total_len, aws_cbor_decoder_get_unconsumed_length(decoder)); |
| 573 |
|
| 574 |
|
| 575 |
uint64_t val; |
| 576 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_unsigned_int_val(decoder, &val)); |
| 577 |
ASSERT_UINT_EQUALS(42, val); |
| 578 |
size_t after_first_pop = aws_cbor_decoder_get_unconsumed_length(decoder); |
| 579 |
ASSERT_TRUE(after_first_pop < total_len); |
| 580 |
ASSERT_UINT_EQUALS(after_first_pop, aws_cbor_decoder_get_remaining_length(decoder)); |
| 581 |
|
| 582 |
|
| 583 |
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); |
| 584 |
ASSERT_UINT_EQUALS(after_first_pop, aws_cbor_decoder_get_unconsumed_length(decoder)); |
| 585 |
ASSERT_TRUE(aws_cbor_decoder_get_remaining_length(decoder) < after_first_pop); |
| 586 |
|
| 587 |
|
| 588 |
ASSERT_SUCCESS(aws_cbor_decoder_pop_next_unsigned_int_val(decoder, &val)); |
| 589 |
ASSERT_UINT_EQUALS(100, val); |
| 590 |
ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); |
| 591 |
ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_unconsumed_length(decoder)); |
| 592 |
|
| 593 |
aws_cbor_encoder_destroy(encoder); |
| 594 |
aws_cbor_decoder_destroy(decoder); |
| 595 |
aws_common_library_clean_up(); |
| 596 |
return AWS_OP_SUCCESS; |
| 597 |
} |