Skip to content

Commit

Permalink
Add more tests for zero len items in cbor_serialize_alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
PJK committed Dec 30, 2022
1 parent e308674 commit ecabc8c
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions test/cbor_serialize_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,52 @@ static void test_auto_serialize_zero_len_string_chunk(
_cbor_free(output);
}

static void test_auto_serialize_zero_len_array(void **_CBOR_UNUSED(_state)) {
cbor_item_t *item = cbor_new_definite_array(0);

unsigned char *output;
assert_int_equal(cbor_serialize_alloc(item, &output, NULL), 1);
assert_memory_equal(output, ((unsigned char[]){0x80}), 1);
assert_int_equal(cbor_serialized_size(item), 1);
cbor_decref(&item);
_cbor_free(output);
}

static void test_auto_serialize_zero_len_indef_array(
void **_CBOR_UNUSED(_state)) {
cbor_item_t *item = cbor_new_indefinite_array();

unsigned char *output;
assert_int_equal(cbor_serialize_alloc(item, &output, NULL), 2);
assert_memory_equal(output, ((unsigned char[]){0x9f, 0xff}), 2);
assert_int_equal(cbor_serialized_size(item), 2);
cbor_decref(&item);
_cbor_free(output);
}

static void test_auto_serialize_zero_len_map(void **_CBOR_UNUSED(_state)) {
cbor_item_t *item = cbor_new_definite_map(0);

unsigned char *output;
assert_int_equal(cbor_serialize_alloc(item, &output, NULL), 1);
assert_memory_equal(output, ((unsigned char[]){0xa0}), 1);
assert_int_equal(cbor_serialized_size(item), 1);
cbor_decref(&item);
_cbor_free(output);
}

static void test_auto_serialize_zero_len_indef_map(
void **_CBOR_UNUSED(_state)) {
cbor_item_t *item = cbor_new_indefinite_map();

unsigned char *output;
assert_int_equal(cbor_serialize_alloc(item, &output, NULL), 2);
assert_memory_equal(output, ((unsigned char[]){0xbf, 0xff}), 2);
assert_int_equal(cbor_serialized_size(item), 2);
cbor_decref(&item);
_cbor_free(output);
}

int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_serialize_uint8_embed),
Expand Down Expand Up @@ -627,6 +673,10 @@ int main(void) {
cmocka_unit_test(test_auto_serialize_zero_len_string),
cmocka_unit_test(test_auto_serialize_zero_len_bytestring_chunk),
cmocka_unit_test(test_auto_serialize_zero_len_string_chunk),
cmocka_unit_test(test_auto_serialize_zero_len_array),
cmocka_unit_test(test_auto_serialize_zero_len_indef_array),
cmocka_unit_test(test_auto_serialize_zero_len_map),
cmocka_unit_test(test_auto_serialize_zero_len_indef_map),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

0 comments on commit ecabc8c

Please sign in to comment.