Skip to content

Commit

Permalink
Fix string testing todo
Browse files Browse the repository at this point in the history
  • Loading branch information
PJK committed Dec 28, 2022
1 parent 873c949 commit 49f929c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/cbor/strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk) {
struct cbor_indefinite_string_data *data =
(struct cbor_indefinite_string_data *)item->data;
if (data->chunk_count == data->chunk_capacity) {
// TODO: Add a test for this
if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) {
return false;
}
Expand Down
20 changes: 19 additions & 1 deletion test/string_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,24 @@ static void test_string_add_chunk(void **_CBOR_UNUSED(_state)) {
5, MALLOC, MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
}

static void test_add_chunk_reallocation_overflow(void **_CBOR_UNUSED(_state)) {
string = cbor_new_indefinite_string();
cbor_item_t *chunk = cbor_build_string("Hello!");
struct cbor_indefinite_string_data *metadata =
(struct cbor_indefinite_string_data *)string->data;
// Pretend we already have many chunks allocated
metadata->chunk_count = SIZE_MAX;
metadata->chunk_capacity = SIZE_MAX;

assert_false(cbor_string_add_chunk(string, chunk));
assert_int_equal(cbor_refcount(chunk), 1);

metadata->chunk_count = 0;
metadata->chunk_capacity = 0;
cbor_decref(&chunk);
cbor_decref(&string);
}

int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_empty_string),
Expand All @@ -275,7 +293,7 @@ int main(void) {
cmocka_unit_test(test_inline_creation),
cmocka_unit_test(test_string_creation),
cmocka_unit_test(test_string_add_chunk),
// TODO: string chunks realloc test
cmocka_unit_test(test_add_chunk_reallocation_overflow),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

0 comments on commit 49f929c

Please sign in to comment.