Skip to content

Commit

Permalink
Merge pull request #265 from PJK/ints32b
Browse files Browse the repository at this point in the history
Fix test assertions on size_t
  • Loading branch information
PJK committed Jan 8, 2023
2 parents 4997d26 + d865db9 commit 98dd1c7
Show file tree
Hide file tree
Showing 29 changed files with 375 additions and 505 deletions.
18 changes: 6 additions & 12 deletions test/array_encoders_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,25 @@
* it under the terms of the MIT license. See LICENSE for details.
*/

#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>

#include <cmocka.h>

#include "assertions.h"
#include "cbor.h"

unsigned char buffer[512];

static void test_embedded_array_start(void **_CBOR_UNUSED(_state)) {
assert_int_equal(1, cbor_encode_array_start(1, buffer, 512));
assert_size_equal(1, cbor_encode_array_start(1, buffer, 512));
assert_memory_equal(buffer, ((unsigned char[]){0x81}), 1);
}

static void test_array_start(void **_CBOR_UNUSED(_state)) {
assert_int_equal(5, cbor_encode_array_start(1000000, buffer, 512));
assert_size_equal(5, cbor_encode_array_start(1000000, buffer, 512));
assert_memory_equal(buffer, ((unsigned char[]){0x9A, 0x00, 0x0F, 0x42, 0x40}),
5);
}

static void test_indef_array_start(void **_CBOR_UNUSED(_state)) {
assert_int_equal(1, cbor_encode_indef_array_start(buffer, 512));
assert_int_equal(0, cbor_encode_indef_array_start(buffer, 0));
assert_size_equal(1, cbor_encode_indef_array_start(buffer, 512));
assert_size_equal(0, cbor_encode_indef_array_start(buffer, 0));
assert_memory_equal(buffer, ((unsigned char[]){0x9F}), 1);
}

Expand All @@ -40,7 +34,7 @@ static void test_indef_array_encoding(void **_CBOR_UNUSED(_state)) {
assert_true(cbor_array_push(array, one));
assert_true(cbor_array_push(array, two));

assert_int_equal(4, cbor_serialize_array(array, buffer, 512));
assert_size_equal(4, cbor_serialize_array(array, buffer, 512));
assert_memory_equal(buffer, ((unsigned char[]){0x9F, 0x01, 0x02, 0xFF}), 4);

cbor_decref(&array);
Expand Down
39 changes: 16 additions & 23 deletions test/array_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
* it under the terms of the MIT license. See LICENSE for details.
*/

#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>

#include <cmocka.h>

#include "assertions.h"
#include "cbor.h"
#include "test_allocator.h"
Expand Down Expand Up @@ -39,9 +32,9 @@ static void test_simple_array(void **_CBOR_UNUSED(_state)) {
assert_non_null(arr);
assert_true(cbor_typeof(arr) == CBOR_TYPE_ARRAY);
assert_true(cbor_isa_array(arr));
assert_int_equal(cbor_array_size(arr), 1);
assert_size_equal(cbor_array_size(arr), 1);
assert_true(res.read == 2);
assert_int_equal(cbor_array_allocated(arr), 1);
assert_size_equal(cbor_array_allocated(arr), 1);
/* Check the values */
assert_uint8(cbor_array_handle(arr)[0], 1);
cbor_item_t *intermediate = cbor_array_get(arr, 0);
Expand Down Expand Up @@ -106,7 +99,7 @@ static void test_nested_indef_arrays(void **_CBOR_UNUSED(_state)) {
assert_non_null(arr);
assert_true(cbor_typeof(arr) == CBOR_TYPE_ARRAY);
assert_true(cbor_isa_array(arr));
assert_int_equal(cbor_array_size(arr), 3);
assert_size_equal(cbor_array_size(arr), 3);
assert_true(res.read == 7);
/* Check the values */
assert_uint8(cbor_array_handle(arr)[0], 1);
Expand All @@ -122,30 +115,30 @@ static void test_nested_indef_arrays(void **_CBOR_UNUSED(_state)) {

static void test_array_replace(void **_CBOR_UNUSED(_state)) {
cbor_item_t *array = cbor_new_definite_array(2);
assert_int_equal(cbor_array_size(array), 0);
assert_size_equal(cbor_array_size(array), 0);
cbor_item_t *one = cbor_build_uint8(1);
cbor_item_t *three = cbor_build_uint8(3);
assert_int_equal(cbor_refcount(one), 1);
assert_int_equal(cbor_refcount(three), 1);
assert_size_equal(cbor_refcount(one), 1);
assert_size_equal(cbor_refcount(three), 1);

// No item to replace
assert_false(cbor_array_replace(array, 0, three));
assert_int_equal(cbor_refcount(three), 1);
assert_size_equal(cbor_refcount(three), 1);

// Add items [1, 2]
assert_true(cbor_array_push(array, one));
assert_true(cbor_array_push(array, cbor_move(cbor_build_uint8(2))));
assert_int_equal(cbor_refcount(one), 2);
assert_int_equal(cbor_array_size(array), 2);
assert_size_equal(cbor_refcount(one), 2);
assert_size_equal(cbor_array_size(array), 2);

// Array has only two items
assert_false(cbor_array_replace(array, 2, three));
assert_int_equal(cbor_refcount(three), 1);
assert_size_equal(cbor_refcount(three), 1);

// Change [1, 2] to [3, 2]
assert_true(cbor_array_replace(array, 0, three));
assert_int_equal(cbor_refcount(one), 1);
assert_int_equal(cbor_refcount(three), 2);
assert_size_equal(cbor_refcount(one), 1);
assert_size_equal(cbor_refcount(three), 2);
assert_uint8(cbor_move(cbor_array_get(array, 0)), 3);
assert_uint8(cbor_move(cbor_array_get(array, 1)), 2);

Expand All @@ -164,7 +157,7 @@ static void test_array_push_overflow(void **_CBOR_UNUSED(_state)) {
metadata->end_ptr = SIZE_MAX;

assert_false(cbor_array_push(array, one));
assert_int_equal(cbor_refcount(one), 1);
assert_size_equal(cbor_refcount(one), 1);

cbor_decref(&one);
metadata->allocated = 0;
Expand All @@ -187,9 +180,9 @@ static void test_array_push(void **_CBOR_UNUSED(_state)) {
cbor_item_t *string = cbor_build_string("Hello!");

assert_false(cbor_array_push(array, string));
assert_int_equal(cbor_array_allocated(array), 0);
assert_size_equal(cbor_array_allocated(array), 0);
assert_null(array->data);
assert_int_equal(array->metadata.array_metadata.end_ptr, 0);
assert_size_equal(array->metadata.array_metadata.end_ptr, 0);

cbor_decref(&string);
cbor_decref(&array);
Expand All @@ -206,7 +199,7 @@ static void test_indef_array_decode(void **_CBOR_UNUSED(_state)) {
array = cbor_load(simple_indef_array, 4, &res);

assert_null(array);
assert_int_equal(res.error.code, CBOR_ERR_MEMERROR);
assert_size_equal(res.error.code, CBOR_ERR_MEMERROR);
},
4, MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
}
Expand Down
11 changes: 11 additions & 0 deletions test/assertions.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ void assert_minimum_input_size(size_t expected, cbor_data data) {
assert_decoder_result_nedata(expected, decode(data, 1));
}
}

void _assert_size_equal(size_t actual, size_t expected, const char* src_file,
int src_line) {
if (actual == expected) return;
// Not using `fail_msg` since it mishandles variadic macro arguments, which
// causes compiler warnings/
// TODO file bug
printf("(%s:%d) assert_size_equal: Expected %zu to equal %zu\n", src_file,
src_line, actual, expected);
fail();
}
8 changes: 8 additions & 0 deletions test/assertions.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Headers needed by cmocka -- must be imported first
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
Expand All @@ -15,6 +16,7 @@ void assert_uint16(cbor_item_t* item, uint16_t num);
void assert_uint32(cbor_item_t* item, uint32_t num);
void assert_uint64(cbor_item_t* item, uint64_t num);

// TODO: Fix "Yoda" parameter ordering of asserts
/** Verify the `actual_result.status` and `actual_result.status`. */
void assert_decoder_result(size_t expected_bytes_read,
enum cbor_decoder_status expected_status,
Expand All @@ -33,4 +35,10 @@ void assert_decoder_result_nedata(size_t expected_bytes_required,
*/
void assert_minimum_input_size(size_t expected, cbor_data data);

/** Check the equality of two `size_t`s. */
void _assert_size_equal(size_t actual, size_t expected, const char* src_file,
int src_line);
#define assert_size_equal(actual, expected) \
_assert_size_equal(actual, expected, __FILE__, __LINE__)

#endif
26 changes: 10 additions & 16 deletions test/bad_inputs_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
* it under the terms of the MIT license. See LICENSE for details.
*/

#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>

#include <cmocka.h>

#include "assertions.h"
#include "cbor.h"

/* These tests verify behavior on interesting randomly generated inputs from the
Expand All @@ -26,23 +20,23 @@ static void test_1(void **_CBOR_UNUSED(_state)) {
item = cbor_load(data1, 2, &res);
assert_null(item);
assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
assert_int_equal(res.error.position, 2);
assert_size_equal(res.error.position, 2);
}

unsigned char data2[] = {0x9D};
static void test_2(void **_CBOR_UNUSED(_state)) {
item = cbor_load(data2, 1, &res);
assert_null(item);
assert_true(res.error.code == CBOR_ERR_MALFORMATED);
assert_int_equal(res.error.position, 0);
assert_size_equal(res.error.position, 0);
}

unsigned char data3[] = {0xD6};
static void test_3(void **_CBOR_UNUSED(_state)) {
item = cbor_load(data3, 1, &res);
assert_null(item);
assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
assert_int_equal(res.error.position, 1);
assert_size_equal(res.error.position, 1);
}

#ifdef SANE_MALLOC
Expand All @@ -51,15 +45,15 @@ static void test_4(void **_CBOR_UNUSED(_state)) {
item = cbor_load(data4, 7, &res);
assert_null(item);
assert_true(res.error.code == CBOR_ERR_MEMERROR);
assert_int_equal(res.error.position, 5);
assert_size_equal(res.error.position, 5);
}

unsigned char data5[] = {0x9A, 0xDA, 0x3A, 0xB2, 0x7F, 0x29};
static void test_5(void **_CBOR_UNUSED(_state)) {
assert_true(res.error.code == CBOR_ERR_MEMERROR);
item = cbor_load(data5, 6, &res);
assert_null(item);
assert_int_equal(res.error.position, 5);
assert_size_equal(res.error.position, 5);
/* Indef string expectation mismatch */
}
#endif
Expand All @@ -69,7 +63,7 @@ static void test_6(void **_CBOR_UNUSED(_state)) {
item = cbor_load(data6, 5, &res);
assert_null(item);
assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
assert_int_equal(res.error.position, 2);
assert_size_equal(res.error.position, 2);
}

#ifdef EIGHT_BYTE_SIZE_T
Expand All @@ -81,7 +75,7 @@ static void test_7(void **_CBOR_UNUSED(_state)) {
item = cbor_load(data7, 16, &res);
assert_null(item);
assert_true(res.error.code == CBOR_ERR_MEMERROR);
assert_int_equal(res.error.position, 10);
assert_size_equal(res.error.position, 10);
}
#endif

Expand All @@ -94,15 +88,15 @@ static void test_8(void **_CBOR_UNUSED(_state)) {
item = cbor_load(data8, 39, &res);
assert_null(item);
assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
assert_int_equal(res.error.position, 7);
assert_size_equal(res.error.position, 7);
}

unsigned char data9[] = {0xBF, 0x05, 0xFF, 0x00, 0x00, 0x00, 0x10, 0x04};
static void test_9(void **_CBOR_UNUSED(_state)) {
item = cbor_load(data9, 8, &res);
assert_null(item);
assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
assert_int_equal(res.error.position, 3);
assert_size_equal(res.error.position, 3);
}

int main(void) {
Expand Down
15 changes: 5 additions & 10 deletions test/bytestring_encoders_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,26 @@
* it under the terms of the MIT license. See LICENSE for details.
*/

#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>

#include <cmocka.h>
#include "assertions.h"

#include "cbor.h"

unsigned char buffer[512];

static void test_embedded_bytestring_start(void **_CBOR_UNUSED(_state)) {
assert_int_equal(1, cbor_encode_bytestring_start(1, buffer, 512));
assert_size_equal(1, cbor_encode_bytestring_start(1, buffer, 512));
assert_memory_equal(buffer, ((unsigned char[]){0x41}), 1);
}

static void test_bytestring_start(void **_CBOR_UNUSED(_state)) {
assert_int_equal(5, cbor_encode_bytestring_start(1000000, buffer, 512));
assert_size_equal(5, cbor_encode_bytestring_start(1000000, buffer, 512));
assert_memory_equal(buffer, ((unsigned char[]){0x5A, 0x00, 0x0F, 0x42, 0x40}),
5);
}

static void test_indef_bytestring_start(void **_CBOR_UNUSED(_state)) {
assert_int_equal(0, cbor_encode_indef_bytestring_start(buffer, 0));
assert_int_equal(1, cbor_encode_indef_bytestring_start(buffer, 512));
assert_size_equal(0, cbor_encode_indef_bytestring_start(buffer, 0));
assert_size_equal(1, cbor_encode_indef_bytestring_start(buffer, 512));
assert_memory_equal(buffer, ((unsigned char[]){0x5F}), 1);
}

Expand Down
18 changes: 6 additions & 12 deletions test/bytestring_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
* it under the terms of the MIT license. See LICENSE for details.
*/

#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>

#include <cmocka.h>

#include "assertions.h"
#include "cbor.h"
#include "test_allocator.h"

Expand Down Expand Up @@ -144,7 +138,7 @@ static void test_empty_bs(void **_CBOR_UNUSED(_state)) {
assert_non_null(bs);
assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
assert_true(cbor_isa_bytestring(bs));
assert_int_equal(cbor_bytestring_length(bs), 0);
assert_size_equal(cbor_bytestring_length(bs), 0);
assert_true(res.read == 1);
cbor_decref(&bs);
assert_null(bs);
Expand Down Expand Up @@ -274,7 +268,7 @@ unsigned char data11[] = {0x5F, 0x58, 0x01, 0xA1, 0x58, 0x01, 0xA2, 0xFF, 0xFF};
static void test_two_indef(void **_CBOR_UNUSED(_state)) {
bs = cbor_load(data11, 9, &res);
assert_non_null(bs);
assert_int_equal(1, cbor_refcount(bs));
assert_size_equal(1, cbor_refcount(bs));
assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
assert_true(cbor_isa_bytestring(bs));
assert_true(cbor_bytestring_length(bs) == 0);
Expand Down Expand Up @@ -321,7 +315,7 @@ static void test_add_chunk_reallocation_overflow(void **_CBOR_UNUSED(_state)) {
metadata->chunk_capacity = SIZE_MAX;

assert_false(cbor_bytestring_add_chunk(bs, chunk));
assert_int_equal(cbor_refcount(chunk), 1);
assert_size_equal(cbor_refcount(chunk), 1);

metadata->chunk_count = 0;
metadata->chunk_capacity = 0;
Expand Down Expand Up @@ -352,8 +346,8 @@ static void test_bytestring_add_chunk(void **_CBOR_UNUSED(_state)) {

assert_false(cbor_bytestring_add_chunk(bytestring, chunk));

assert_int_equal(cbor_bytestring_chunk_count(bytestring), 0);
assert_int_equal(
assert_size_equal(cbor_bytestring_chunk_count(bytestring), 0);
assert_size_equal(
((struct cbor_indefinite_string_data *)bytestring->data)
->chunk_capacity,
0);
Expand Down

0 comments on commit 98dd1c7

Please sign in to comment.