xref: /freebsd-src/contrib/libcbor/test/memory_utils_test.c (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
1*5d3e7166SEd Maste /*
2*5d3e7166SEd Maste  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3*5d3e7166SEd Maste  *
4*5d3e7166SEd Maste  * libcbor is free software; you can redistribute it and/or modify
5*5d3e7166SEd Maste  * it under the terms of the MIT license. See LICENSE for details.
6*5d3e7166SEd Maste  */
7*5d3e7166SEd Maste 
8*5d3e7166SEd Maste #include "cbor/internal/memory_utils.h"
9*5d3e7166SEd Maste #include <string.h>
10*5d3e7166SEd Maste #include "assertions.h"
11*5d3e7166SEd Maste 
test_safe_multiply(void ** _CBOR_UNUSED (_state))12*5d3e7166SEd Maste static void test_safe_multiply(void **_CBOR_UNUSED(_state)) {
13*5d3e7166SEd Maste   assert_true(_cbor_safe_to_multiply(1, 1));
14*5d3e7166SEd Maste   assert_true(_cbor_safe_to_multiply(SIZE_MAX, 0));
15*5d3e7166SEd Maste   assert_true(_cbor_safe_to_multiply(SIZE_MAX, 1));
16*5d3e7166SEd Maste   assert_false(_cbor_safe_to_multiply(SIZE_MAX, 2));
17*5d3e7166SEd Maste   assert_false(_cbor_safe_to_multiply(SIZE_MAX, SIZE_MAX));
18*5d3e7166SEd Maste }
19*5d3e7166SEd Maste 
test_safe_add(void ** _CBOR_UNUSED (_state))20*5d3e7166SEd Maste static void test_safe_add(void **_CBOR_UNUSED(_state)) {
21*5d3e7166SEd Maste   assert_true(_cbor_safe_to_add(1, 1));
22*5d3e7166SEd Maste   assert_true(_cbor_safe_to_add(SIZE_MAX - 1, 1));
23*5d3e7166SEd Maste   assert_true(_cbor_safe_to_add(SIZE_MAX, 0));
24*5d3e7166SEd Maste   assert_false(_cbor_safe_to_add(SIZE_MAX, 1));
25*5d3e7166SEd Maste   assert_false(_cbor_safe_to_add(SIZE_MAX, 2));
26*5d3e7166SEd Maste   assert_false(_cbor_safe_to_add(SIZE_MAX, SIZE_MAX));
27*5d3e7166SEd Maste   assert_false(_cbor_safe_to_add(SIZE_MAX - 1, SIZE_MAX));
28*5d3e7166SEd Maste   assert_false(_cbor_safe_to_add(SIZE_MAX - 1, SIZE_MAX - 1));
29*5d3e7166SEd Maste }
30*5d3e7166SEd Maste 
test_safe_signalling_add(void ** _CBOR_UNUSED (_state))31*5d3e7166SEd Maste static void test_safe_signalling_add(void **_CBOR_UNUSED(_state)) {
32*5d3e7166SEd Maste   assert_size_equal(_cbor_safe_signaling_add(1, 2), 3);
33*5d3e7166SEd Maste   assert_size_equal(_cbor_safe_signaling_add(0, 1), 0);
34*5d3e7166SEd Maste   assert_size_equal(_cbor_safe_signaling_add(0, SIZE_MAX), 0);
35*5d3e7166SEd Maste   assert_size_equal(_cbor_safe_signaling_add(1, SIZE_MAX), 0);
36*5d3e7166SEd Maste   assert_size_equal(_cbor_safe_signaling_add(1, SIZE_MAX - 1), SIZE_MAX);
37*5d3e7166SEd Maste }
38*5d3e7166SEd Maste 
test_realloc_multiple(void ** _CBOR_UNUSED (_state))39*5d3e7166SEd Maste static void test_realloc_multiple(void **_CBOR_UNUSED(_state)) {
40*5d3e7166SEd Maste   unsigned char *data = malloc(1);
41*5d3e7166SEd Maste   data[0] = 0x2a;
42*5d3e7166SEd Maste 
43*5d3e7166SEd Maste   data = _cbor_realloc_multiple(data, /*item_size=*/1, /*item_count=*/10);
44*5d3e7166SEd Maste   assert_size_equal(data[0], 0x2a);
45*5d3e7166SEd Maste   data[9] = 0x2b;  // Sanitizer will stop us if not ok
46*5d3e7166SEd Maste   free(data);
47*5d3e7166SEd Maste 
48*5d3e7166SEd Maste   assert_null(_cbor_realloc_multiple(NULL, SIZE_MAX, 2));
49*5d3e7166SEd Maste }
50*5d3e7166SEd Maste 
main(void)51*5d3e7166SEd Maste int main(void) {
52*5d3e7166SEd Maste   const struct CMUnitTest tests[] = {
53*5d3e7166SEd Maste       cmocka_unit_test(test_safe_multiply),
54*5d3e7166SEd Maste       cmocka_unit_test(test_safe_add),
55*5d3e7166SEd Maste       cmocka_unit_test(test_safe_signalling_add),
56*5d3e7166SEd Maste       cmocka_unit_test(test_realloc_multiple),
57*5d3e7166SEd Maste   };
58*5d3e7166SEd Maste   return cmocka_run_group_tests(tests, NULL, NULL);
59*5d3e7166SEd Maste }
60