Skip to content

Commit

Permalink
Merge pull request #287 from PJK/testtweaks
Browse files Browse the repository at this point in the history
Test coverage improvements
  • Loading branch information
PJK committed Jun 3, 2023
2 parents 5fe9656 + ae5210c commit d2ac21a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/cbor/internal/builder_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ void _cbor_builder_append(cbor_item_t *item,
// Note: We use 0 and 1 subitems to distinguish between keys and values in
// indefinite items
if (ctx->stack->top->subitems % 2) {
/* Odd record, this is a value */
if (!_cbor_map_add_value(ctx->stack->top->item, item)) {
ctx->creation_failed = true;
cbor_decref(&item);
break;
}
// Odd record, this is a value.
ctx->creation_failed =
!_cbor_map_add_value(ctx->stack->top->item, item);
// Adding a value never fails since the memory is allocated when the
// key is added
CBOR_ASSERT(!ctx->creation_failed);
} else {
/* Even record, this is a key */
// Even record, this is a key.
if (!_cbor_map_add_key(ctx->stack->top->item, item)) {
ctx->creation_failed = true;
cbor_decref(&item);
Expand Down Expand Up @@ -344,6 +344,8 @@ bool _cbor_is_indefinite(cbor_item_t *item) {
case CBOR_TYPE_MAP:
return cbor_map_is_indefinite(item);
default:
// Should never happen since a non-nested item cannot be on top of the
// stack.
return false;
}
}
Expand Down
25 changes: 25 additions & 0 deletions test/callbacks_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,30 @@ static void test_invalid_indef_break(void** _CBOR_UNUSED(_state)) {
assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
}

static void test_invalid_state_indef_break(void** _CBOR_UNUSED(_state)) {
struct _cbor_stack stack = _cbor_stack_init();
assert_non_null(_cbor_stack_push(&stack, cbor_new_int8(), /*subitems=*/0));
struct _cbor_decoder_context context = {
.creation_failed = false,
.syntax_error = false,
.root = NULL,
.stack = &stack,
};

cbor_builder_indef_break_callback(&context);

assert_false(context.creation_failed);
assert_true(context.syntax_error);
assert_size_equal(context.stack->size, 1);
// The stack remains unchanged
cbor_item_t* small_int = stack.top->item;
assert_size_equal(cbor_refcount(small_int), 1);
assert_true(cbor_isa_uint(small_int));

cbor_decref(&small_int);
_cbor_stack_pop(&stack);
}

int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_default_callbacks),
Expand All @@ -388,6 +412,7 @@ int main(void) {
cmocka_unit_test(test_append_array_failure),
cmocka_unit_test(test_append_map_failure),
cmocka_unit_test(test_invalid_indef_break),
cmocka_unit_test(test_invalid_state_indef_break),
};

cmocka_run_group_tests(tests, NULL, NULL);
Expand Down
30 changes: 30 additions & 0 deletions test/cbor_serialize_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,34 @@ static void test_serialize_definite_string(void **_CBOR_UNUSED(_state)) {
cbor_decref(&item);
}

static void test_serialize_definite_string_4b_header(
void **_CBOR_UNUSED(_state)) {
#if SIZE_MAX > UINT16_MAX
cbor_item_t *item = cbor_new_definite_string();
const size_t size = (size_t)UINT16_MAX + 1;
unsigned char *data = malloc(size);
memset(data, 0, size);
cbor_string_set_handle(item, data, size);
assert_size_equal(cbor_serialized_size(item), 1 + 4 + size);
cbor_decref(&item);
#endif
}

static void test_serialize_definite_string_8b_header(
void **_CBOR_UNUSED(_state)) {
#if SIZE_MAX > UINT32_MAX
cbor_item_t *item = cbor_new_definite_string();
const size_t size = (size_t)UINT32_MAX + 1;
unsigned char *data = malloc(1);
data[0] = '\0';
cbor_string_set_handle(item, data, 1);
// Pretend that we have a big item to avoid the huge malloc
item->metadata.string_metadata.length = size;
assert_size_equal(cbor_serialized_size(item), 1 + 8 + size);
cbor_decref(&item);
#endif
}

static void test_serialize_indefinite_string(void **_CBOR_UNUSED(_state)) {
cbor_item_t *item = cbor_new_indefinite_string();
cbor_item_t *chunk = cbor_new_definite_string();
Expand Down Expand Up @@ -640,6 +668,8 @@ int main(void) {
cmocka_unit_test(test_serialize_bytestring_no_space),
cmocka_unit_test(test_serialize_indefinite_bytestring_no_space),
cmocka_unit_test(test_serialize_definite_string),
cmocka_unit_test(test_serialize_definite_string_4b_header),
cmocka_unit_test(test_serialize_definite_string_8b_header),
cmocka_unit_test(test_serialize_indefinite_string),
cmocka_unit_test(test_serialize_string_no_space),
cmocka_unit_test(test_serialize_indefinite_string_no_space),
Expand Down
6 changes: 4 additions & 2 deletions test/float_ctrl_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static void test_float2(void **_CBOR_UNUSED(_state)) {
assert_true(cbor_is_float(float_ctrl));
assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_16);
assert_true(cbor_float_get_float2(float_ctrl) == 65504.0F);
assert_true(fabs(cbor_float_get_float(float_ctrl) - 65504.0F) < eps);
assert_float_equal(cbor_float_get_float(float_ctrl), 65504.0F, eps);
cbor_decref(&float_ctrl);
assert_null(float_ctrl);
}
Expand All @@ -43,7 +43,7 @@ static void test_float4(void **_CBOR_UNUSED(_state)) {
assert_true(cbor_is_float(float_ctrl));
assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_32);
assert_true(cbor_float_get_float4(float_ctrl) == 100000.0F);
assert_true(fabs(cbor_float_get_float(float_ctrl) - 100000.0F) < eps);
assert_float_equal(cbor_float_get_float(float_ctrl), 100000.0F, eps);
cbor_decref(&float_ctrl);
assert_null(float_ctrl);
}
Expand All @@ -58,6 +58,8 @@ static void test_float8(void **_CBOR_UNUSED(_state)) {
assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_64);
// XXX: the cast prevents promotion to 80-bit floats on 32-bit x86
assert_true(cbor_float_get_float8(float_ctrl) == (double)1.0e+300);
// Not using `assert_double_equal` since CI has an old version of cmocka
assert_true(fabs(cbor_float_get_float(float_ctrl) - (double)1.0e+300) < eps);
cbor_decref(&float_ctrl);
assert_null(float_ctrl);
}
Expand Down

0 comments on commit d2ac21a

Please sign in to comment.