|
cmocka 2.0.1
Unit testing library with mock support
|
Detect memory leaks, buffer overflows, and allocation errors. More...
Functions | |
| void * | test_malloc (size_t size) |
| Test function overriding malloc. | |
| void * | test_calloc (size_t nmemb, size_t size) |
| Test function overriding calloc. | |
| void * | test_realloc (void *ptr, size_t size) |
| Test function overriding realloc which detects buffer overruns and memory leaks. | |
| void | test_free (void *ptr) |
| Test function overriding free(3). | |
Detect memory leaks, buffer overflows, and allocation errors.
To test for memory leaks, buffer overflows and underflows a module being tested by cmocka should replace calls to malloc(), calloc() and free() to test_malloc(), test_calloc() and test_free() respectively. Each time a block is deallocated using test_free() it is checked for corruption, if a corrupt block is found a test failure is signalled. All blocks allocated using the test_*() allocation functions are tracked by the cmocka library. When a test completes if any allocated blocks (memory leaks) remain they are reported and a test failure is signalled.
For simplicity cmocka currently executes all tests in one process. Therefore all test cases in a test application share a single address space which means memory corruption from a single test case could potentially cause the test application to exit prematurely.
Automatic Allocation Redirection (Deprecated):
When both UNIT_TESTING and ALLOCATION_TESTING are defined, the standard C library allocation functions (malloc, calloc, realloc, free) are automatically redirected to cmocka's test allocators. This enables automatic memory leak detection and helps ensure proper memory management in tested code.
To enable allocation testing, define both macros before including cmocka.h:
With ALLOCATION_TESTING enabled:
| void * test_calloc | ( | size_t | nmemb, |
| size_t | size ) |
Test function overriding calloc.
The memory is set to zero.
| [in] | nmemb | The number of elements for an array to be allocated. |
| [in] | size | The size in bytes of each array element to allocate. |
| void test_free | ( | void * | ptr | ) |
Test function overriding free(3).
| [in] | ptr | The pointer to the memory space to free. |
| void * test_malloc | ( | size_t | size | ) |
Test function overriding malloc.
| [in] | size | The bytes which should be allocated. |
| void * test_realloc | ( | void * | ptr, |
| size_t | size ) |
Test function overriding realloc which detects buffer overruns and memory leaks.
| [in] | ptr | The memory block which should be changed. |
| [in] | size | The bytes which should be allocated. |