aboutsummaryrefslogtreecommitdiff
path: root/src
AgeCommit message (Collapse)AuthorFilesLines
3 daysAlias ptr asserts to assert_msg with no messageHEADmasterFriedrich Schwedler1-24/+7
Implements: assert_ptr_equal with assert_ptr_equal_msg assert_ptr_not_equal with assert_ptr_not_equal_msg assert_null with assert_null_msg assert_non_null with assert_non_null_msg Signed-off-by: Friedrich Schwedler <fschwedler@emlix.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
3 daysImplement assert macros with user provided error messagesFriedrich Schwedler1-0/+25
Signed-off-by: Friedrich Schwedler <fschwedler@emlix.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
3 daysadd a function to print an aditional messageFriedrich Schwedler1-0/+12
with automatic formating depending on the output format Signed-off-by: Friedrich Schwedler <fschwedler@emlix.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2024-02-12treewide: Use bool intead of int wherever possibleJakub Czapiga1-116/+114
Use boolean type instead of integer to return logical values. This ensures correct values handling and reduction in confusion for developers. TEST=mkdir obj ; ( cd obj && cmake --DCMAKE_BUILD_TYPE=RelWithDebInfo \ -DPICKY_DEVELOPER=ON DUNIT_TESTING=ON .. && make -j \ && ctest --output-on-failure ) Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2024-02-05cmocka: Implement better memory_equal_display_error()Andreas Schneider1-22/+122
Reviewed-by: Joseph Sutton <jsutton@samba.org>
2024-02-05cmocka: Use bool and uint8_t for memory_equal_display_error()Andreas Schneider1-8/+9
Reviewed-by: Joseph Sutton <jsutton@samba.org>
2024-02-05cmocka: Add all_zero() function and testAndreas Schneider1-0/+8
Reviewed-by: Joseph Sutton <jsutton@samba.org>
2024-02-04cmocka: Fix assert_memory_equal() displayJoseph Sutton1-3/+3
The %x specifier expects an unsigned argument. If char is signed, cmocka_print_error() may incorrectly display values sign-extended. To fix this, use an unsigned char and the corresponding format specifier (%hhx). Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2024-02-03Sanitize XML strings.Jakub Czapiga1-2/+23
Replace characters with their XML string equivalents to allow for them in the tests and groups names. Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2024-02-03Improve c_strreplace implementationJakub Czapiga1-9/+18
Return successfuly if there is nothing to replace. Allow for replacement with superset pattern. Simplified example without fix: c_strreplace("ABCD", "A", "AX") -> "AAAAAA...AAAXBCD" Simplified example with fix: c_strreplace("ABCD", "A", "AX") -> "AXBCD" Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2024-02-02cmocka: Get rid of _expect_in_set()Andreas Schneider1-16/+0
2024-02-02cmocka: Implement __expect_uint_in_set()Andreas Schneider1-0/+61
2024-02-02cmocka: Implement expect_int_in_set_count()Andreas Schneider1-16/+16
2024-02-02cmocka: Implement _expect_int_in_set()Andreas Schneider1-0/+59
2024-02-02cmocka: Add missing null check in _expect_check()Andreas Schneider1-0/+1
2024-02-02cmocka: Use uint_value_in_set_display_error() in _assert_not_in_set()Andreas Schneider1-4/+8
2024-01-05Make assert_true(), assert_false() more verboseEshan Kelkar1-1/+11
Both assert_true(expression) and assert_false(expression) print the expression when the assertion fails. Its not very clear on seeing the expression that what exactly is the error, whether its the expression being true or it being false. This commit changes the assert_true() and assert_false() such that on failure of assertion: - assert_true(expression) prints : expression is not true - assert_false(expression) prints : expression is not false Signed-off-by: Eshan Kelkar <eshankelkar@galorithm.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-08-01include: avoid GCC -Wuninitialized in assert_ptr*Alois Klink1-0/+2
In some cases, using the `assert_ptr_*()` macros causes GCC to throw a `-Wmaybe-unitialized` warning. For example: ```c void * my_ptr = malloc(1); // throws a `-Wmaybe-unitialized` warning assert_non_null(my_ptr); ``` This is because GCC assumes that a function accepting a `const void *` (or other constant pointer type) tries to read the pointer value. See [the `-Wmaybe-unitialized` docs][1]. We can tell GCC that the `_assert_ptr_equal`/`_assert_ptr_not_equal` functions only try to read the pointer address, not the pointer value, by using the [`access` function attribute][2]. Since CMocka supports C99, we can't use the ISO C23 attribute syntax. However, we can use the IBM/GCC/Clang `__attribute__` syntax hidden behind `#ifdef` guards, so that they're ignored on compilers that don't support `__attribute__`. [1]: https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#Warning-Options [2]: https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Common-Function-Attributes.html#Common-Function-Attributes on-behalf-of: @nqminds <info@nqminds.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-08-01cmake: enable WINDOWS_EXPORT_ALL_SYMBOLSAlois Klink2-69/+2
Enable the [`WINDOWS_EXPORT_ALL_SYMBOLS` CMake property][1], which automatically exports all symbols into the `.dll` file. This feature is available since [CMake 3.4][2], which is fine, since the minimum version of CMake CMocka supports is CMake 3.5. It is a bit slower, but it does mean that we no longer need to manually create a `.def` file, as CMake will automatically create one for us. [1]: https://cmake.org/cmake/help/latest/prop_tgt/WINDOWS_EXPORT_ALL_SYMBOLS.html [2]: https://www.kitware.com//create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/ Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-08-01include: add CMOCKA_DLLEXTERN for MSVC DLLsAlois Klink2-5/+12
Add a new macro called `CMOCKA_DLLEXTERN` that represents Windows DLL storage class specifiers. Essentially, in order to publically export some data in a DLL, you must either declare the function/data with `__declspec(dllexport)`, or list the function/data in a `.def` file (which is what the CMocka project does). In order to import functions from a DLL, `__declspec(dllimport)` may be used for a performance increase. However, **`__declspec(dllimport)` is required for importing data from a DLL**! The new `CMOCKA_DLLEXTERN` macro takes care of this for us, when we're using MSVC. See https://github.com/MicrosoftDocs/cpp-docs/blob/main/docs/cpp/definitions-and-declarations-cpp.md Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-03-23cmocka: Add overflow check for test_calloc()Alexander Dahl1-1/+10
Makes the implementation behave the same like libc calloc() and not fail with unpredictable errors in test_malloc() anymore. Signed-off-by: Alexander Dahl <ada@thorsis.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-03-22cmake: Set CMOCKA_LIBRARIES in package config for backwards compatibilityAlexander Dahl1-2/+2
Projects using cmocka could have done this successfully from version 1.0 to 1.1.5 to build against cmocka: ``` find_package(cmocka 1.0 REQUIRED CONFIG) ``` and later ``` target_link_libraries(myapp ${CMOCKA_LIBRARIES} ) ``` Modern apps should just "link" against 'cmocka::cmocka' instead like it's done in examples already. To not break old builds (as it is the case with cmocka release 1.1.7) we can put that modern target string into the variable CMOCKA_LIBRARIES and thus trick old projects to just use that, keeping compatibility until those projects explicitly use the modern version. Link: https://cmake.org/cmake/help/latest/command/install.html#install-export Link: https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#package-configuration-file Link: https://cmake.org/cmake/help/latest/module/CMakePackageConfigHelpers.html#module:CMakePackageConfigHelpers Fixes: #87 Signed-off-by: Alexander Dahl <ada@thorsis.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-02-01cmake: Use C99 and define GNU and POSIX flags directly at source filesAndreas Schneider1-0/+12
Fixes #50
2023-01-29cmocka: Add some kind of type safety for will_return and mockAndreas Schneider1-8/+43
Fixes #75
2023-01-29cmocka: Improve pointer assert functionsAlois Klink1-4/+42
Use pointer types for assert_* pointer functions, instead of casting to uintmax_t. On some platforms, casting pointers to uintmax_t loses information. As an additional benefit, we can now use the `%p` specifier to print pointers. On GCC, this will print (nil) for NULL pointers. BREAKING CHANGE: The `assert_{not_,}null` and `assert_ptr_{not_,}equal` functions may now throw `Wint-conversion` warnings when used with non-pointer types. on-behalf-of: @nqminds <info@nqminds.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29cmocka: Change `_mock` to return CMockaValueDataAlois Klink1-6/+7
Change the `_mock()` function to return CMockaValueData. Additionally, the `will_return_ptr*` functions now must be used for pointer types (we can't use C11 _Generic since CMocka still uses C99). BREAKING CHANGE: `will_return*` may throw compiler errors/warnings when used with pointers. Please use the new `will_return_ptr*` macros instead. on-behalf-of: @nqminds <info@nqminds.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29cmocka: Make expect_check to use CMockaValueDataAlois Klink1-52/+51
Change all `expect_check*()` macros to use CMockaValueData internally, instead of `uintmax_t`. This breaks ABI compatibility on some platforms. The API for most macros is the same, but the `expect_check()` and `expect_check_count()` macros now need to be called with the new `CMockaValueData` type. BREAKING CHANGE: `expect_check()` and `expect_check_count()` now use the type CMockaValueData in the check function and check data. on-behalf-of: @nqminds <info@nqminds.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29src: disable casting integers to (void *)Alois Klink1-9/+16
Disable casting integers to and from (void *) in free_symbol_map_value. Instead, we can just use a pointer to an integer. Casting an integer to (void *) is implementation-defined, unless that integer was previously casted from `*intptr_t` from a valid pointer. on-behalf-of: @nqminds <info@nqminds.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29src: set MALLOC_ALIGNMENT to `16`Alois Klink1-1/+6
Hard-code the default `MALLOC_ALIGNMENT` to `16`. On most non-Windows x86_64 platforms, the default alignment should be 16-bytes, as `long double` is an 80-bit large floating point number. C11 has a built-in that's perfect for this, `alignof(max_align_t)`, but unfortunately, cmocka only supports C99. Unfortunately, we can't use `sizeof(long double)`, because on some platforms, it returns 12, which is not a power of 2. A hard-coded value of 16 may be over-aligned on some platforms, but there's no downside to this, other than using up more RAM than normal. on-behalf-of: @nqminds <info@nqminds.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-09cmocka.c: Reduce the call stack consumption of printfXiang Xiao1-7/+7
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com> Change-Id: Idf179ad5eb81bbc63eea4f71980857831668e7a8 Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2022-12-23src: use __builtin_align_down, if availableAlois Klink1-4/+12
In _test_malloc, we manually align a pointer to MALLOC_ALIGNMENT using `& ~(MALLOC_ALIGNMENT - 1)`. However, this has two problems: 1. We're casting the pointer to `size_t`, which isn't guaranteed to hold a pointer. Instead, we should cast to `uintptr_t`. 2. Modifying a pointer as a integer is undefined behavior, and on some platforms (e.g. CHERI), this does not work. C++11 has std::align that does this for us, but unfortunately, there isn't a way to do this in ISO C that is guaranteed to work, except for in Clang v10+, which has a builtin extension called __builtin_align_down that can align pointers safely for us. See: https://clang.llvm.org/docs/LanguageExtensions.html#alignment-builtins on-behalf-of: @nqminds <info@nqminds.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2022-12-23src: use value=`1` when calling longjmp()Alois Klink1-1/+1
Currently, `longjmp()` is always called with value=`0`. According to the ISO C standard, calling longjmp with value 0 just acts like it is called with value 1. However, some BSD platforms are buggy, and instead make setjmp return 0 and cause an infinite loop. See [NetBSD PR/56066][1]. Calling `longjmp` with value 1 makes the meaning more clear as well. [1]: https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=56066 on-behalf-of: @nqminds <info@nqminds.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2022-12-01cmocka: Only handle exceptions on platforms supporting itAndreas Schneider1-15/+26
This also issues a warning during compile time.
2022-11-30cmake: build cmocka with C_EXTENSIONS set to ONAlois Klink1-0/+2
When compiling code with CMake [`C_EXTENSIONS` `OFF`][1], (e.g. using `-std=c11` instead of `-std=gnu11`), only standard ISO C is available in C headers like `<setjmp.h>`. Because cmocka uses POSIX extension functions, we need to set `_POSIX_C_SOURCE` to the appropriate level before including these headers. On gcc/clang, this is enabled by default when C_EXTENSIONS ON. Fixes https://gitlab.com/cmocka/cmocka/-/issues/50, e.g. using cmocka from a project that has C_EXTENSIONS OFF. [1]: https://cmake.org/cmake/help/latest/prop_tgt/C_EXTENSIONS.html Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2022-11-28cmocka: Deprecate assert_in_setAndreas Schneider2-13/+0
2022-11-28cmocka: Add assert_uint_not_in_set()Andreas Schneider2-0/+19
2022-11-28cmocka: Add assert_uint_in_set()Andreas Schneider2-0/+65
2022-11-28cmocka: Add assert_int_not_in_set()Andreas Schneider2-0/+19
2022-11-28cmocka: Add assert_int_in_set()Andreas Schneider2-0/+64
2022-11-28cmocka: Deprecate assert_not_in_range()Andreas Schneider2-10/+0
2022-11-28cmocka: Add assert_uint_not_in_range()Andreas Schneider2-0/+29
2022-11-28cmocka: Add assert_int_not_in_range()Andreas Schneider2-0/+27
2022-11-28cmocka: Deprecate assert_in_range()Andreas Schneider2-10/+0
2022-07-11cmocka: Add assert_int_in_range()Andreas Schneider2-0/+13
2022-06-13tests: Add tests for assert_int_in_range()Andreas Schneider1-0/+1
2022-06-13cmocka: Add assert_int_in_range()Andreas Schneider1-0/+32
2022-06-13cmocka: Rename integer_in_range_display_error for uintAndreas Schneider1-10/+13
2022-06-13cmocka: Return bool for integer_in_range_display_error()Andreas Schneider1-3/+3
2022-06-13cmocka: Export _assert_uint_(not_)equalAndreas Schneider1-0/+2
2022-03-31src: Silence a possible null pointer dereference warningAndreas Schneider1-1/+2
src/cmocka.c:1050:28: warning[core.NullDereference]: Access to field 'function' results in a dereference of a null pointer (loaded from variable 'expected_call')