Commit Graph

770 Commits

Author SHA1 Message Date
Friedrich Schwedler 2453c23475 Alias ptr asserts to assert_msg with no message
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>
2024-04-17 09:05:34 +02:00
Friedrich Schwedler dadc4c7e0c Add tests for asserting pointers with message
Signed-off-by: Friedrich Schwedler <fschwedler@emlix.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2024-04-17 09:05:34 +02:00
Friedrich Schwedler 614175f145 Implement assert macros with user provided error messages
Signed-off-by: Friedrich Schwedler <fschwedler@emlix.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2024-04-17 09:05:34 +02:00
Friedrich Schwedler 9fd057cc71 add a function to print an aditional message
with automatic formating depending on the output format

Signed-off-by: Friedrich Schwedler <fschwedler@emlix.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2024-04-17 09:05:34 +02:00
Jakub Czapiga f9e5b1fa3b treewide: Use bool intead of int wherever possible
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-12 15:57:02 +01:00
Andreas Schneider 48df48211c Add initial CONTRIBUTING.md 2024-02-12 15:56:37 +01:00
Andreas Schneider 563df866d7 clang-format: Update to reflect coding style 2024-02-06 11:07:06 +01:00
Andreas Schneider 5fee42e0ba cmocka: Implement better memory_equal_display_error()
Reviewed-by: Joseph Sutton <jsutton@samba.org>
2024-02-05 09:20:41 +01:00
Andreas Schneider 327c359586 cmocka: Use bool and uint8_t for memory_equal_display_error()
Reviewed-by: Joseph Sutton <jsutton@samba.org>
2024-02-05 09:20:41 +01:00
Andreas Schneider e48a664e75 cmocka: Add all_zero() function and test
Reviewed-by: Joseph Sutton <jsutton@samba.org>
2024-02-05 09:20:41 +01:00
Andreas Schneider 371497b419 tests: Add tests for assert_memory_equal()
Reviewed-by: Joseph Sutton <jsutton@samba.org>
2024-02-05 09:17:45 +01:00
Joseph Sutton 2206c22515 cmocka: Fix assert_memory_equal() display
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-04 11:08:17 +01:00
Jakub Czapiga 90315882d6 Sanitize XML strings.
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-03 10:27:03 +01:00
Jakub Czapiga 54b3af4631 Improve c_strreplace implementation
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-03 10:27:03 +01:00
Andreas Schneider 390b9c1895 cmocka: Get rid of _expect_in_set() 2024-02-02 11:21:34 +01:00
Andreas Schneider 722c7ad5ef tests: Add test for expect_uint_in_set_count() 2024-02-02 11:21:34 +01:00
Andreas Schneider a9e34d885a cmocka: Implement expect_uint_in_set() 2024-02-02 11:21:34 +01:00
Andreas Schneider 669b05fef6 cmocka: Add expect_uint_in_set_count() 2024-02-02 11:21:34 +01:00
Andreas Schneider 6d93495050 cmocka: Implement __expect_uint_in_set() 2024-02-02 11:21:34 +01:00
Andreas Schneider 59b61c72b1 cmocka: Implement expect_int_in_set_count() 2024-02-02 11:21:34 +01:00
Andreas Schneider 241c933413 tests: Add test for expect_int_in_set_count() 2024-02-02 11:21:34 +01:00
Andreas Schneider c999637585 cmocka: Add expect_int_in_set_count() 2024-02-02 11:21:34 +01:00
Andreas Schneider 4b2dafe85d cmocka: Implement _expect_int_in_set() 2024-02-02 11:21:34 +01:00
Andreas Schneider de511ab272 cmocka: Add missing null check in _expect_check() 2024-02-02 11:21:34 +01:00
Andreas Schneider 9e6ab12ae2 cmocka: Use uint_value_in_set_display_error() in _assert_not_in_set() 2024-02-02 11:21:34 +01:00
Andreas Schneider 83ed06d9aa cmocka: Reformat expect_check_count() define 2024-02-02 11:21:34 +01:00
Andreas Schneider 4cb63191de cmocka: Reformat expect_check() 2024-02-02 11:21:34 +01:00
Andreas Schneider 6f187f610b test: Add a uint64_t test for will_return_uint/mock_uint
See #93
2024-02-02 11:16:31 +01:00
Eshan Kelkar a5cb5c3354 Add unit tests for assert_false()
Signed-off-by: Eshan Kelkar <eshankelkar@galorithm.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2024-01-05 07:51:51 +01:00
Eshan Kelkar 5740aff1a1 Add unit tests for assert_true()
Signed-off-by: Eshan Kelkar <eshankelkar@galorithm.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2024-01-05 07:51:51 +01:00
Eshan Kelkar ad567f8d85 Make assert_true(), assert_false() more verbose
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>
2024-01-05 07:51:51 +01:00
Andreas Schneider 109645a20f include: Remove old MVSC prototype
We do not support such old versions anymore.
2023-08-01 11:47:11 +02:00
Alois Klink 323e87dacc include: avoid GCC -Wuninitialized in assert_ptr*
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-01 11:12:07 +02:00
wdlkmpx 6698413af7 update meson tests
33 tests: 21 ok, 12 expected fail, 0 fail, 0 skipped

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-08-01 11:06:07 +02:00
wdlkmpx e813179555 meson.build: install library if not a subproject
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-08-01 11:05:44 +02:00
Alois Klink ad446d96d3 cmake: enable WINDOWS_EXPORT_ALL_SYMBOLS
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-01 10:58:03 +02:00
Alois Klink 1442e46446 include: add CMOCKA_DLLEXTERN for MSVC DLLs
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-08-01 10:58:03 +02:00
Alexander Courtis 1b019882ba 89 add deprecated cm_print_error
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-05-07 17:41:50 +02:00
Thomas Bong 309ee5e402 Allow macros as input for convenience macros
Nested macros like the cmocka macros that make use of # or ## are not expanded.
The `cmocka_tostring` macro makes sure that you can use for example macros
redefining symbols like functions.

When using cmocka on embedded devices, it may happen that the original function
is necessary for it to boot correctly as well as a mocked version for unittests
with a prefix `unittest_`. To simplify this, the compiler can be given
definitions via the `-D` argument. These replacements are now also possible for
the invocations of the cmocka convenience macros like "will_return",
"expect_value", ...

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-03-23 10:27:02 +01:00
Alexander Dahl 6ff1378b6f cmocka: Add overflow check for test_calloc()
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-23 10:02:55 +01:00
Alexander Dahl d87c470017 tests: Add test for test_calloc()
calloc() is prone to integer overflow on multiplication of its arguments.
glibc, musl, and uclibc test for that in its implementations and return NULL in
that case with errno set to ENOMEM.  cmocka lacks such a check and passes all
kinds of overflown values to test_malloc() with different ways to fail.

Signed-off-by: Alexander Dahl <ada@thorsis.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-03-23 10:02:55 +01:00
Andreas Schneider a41ae23be3 gitlab-ci: Add code coverage 2023-03-22 17:58:18 +01:00
Andreas Schneider dddce043e0 gitlab-ci: Do not install CMake
This has been added to windows runners by default now.
2023-03-22 15:40:57 +01:00
Alexander Dahl de67675842 cmake: Set CMOCKA_LIBRARIES in package config for backwards compatibility
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-03-22 15:38:32 +01:00
Andreas Schneider 408ab76759 cpack: Update ignore files for source package generation
Fixes #86
2023-02-19 09:36:28 +01:00
Andreas Schneider a79b97cb92 coverity: Add missing functions to model file 2023-02-03 12:47:42 +01:00
Andreas Schneider 4fccbc3466 gitlab-ci: Build also the coverity model files 2023-02-03 11:49:13 +01:00
Andreas Schneider 776cc1f5eb coverity: Add makefile to build/test the model files 2023-02-03 11:43:04 +01:00
Andreas Schneider fcb8a031b1 coverity: Add URL to modelling description 2023-02-02 17:44:17 +01:00
Andreas Schneider bd266eb406 coverity: Add missing type definitions 2023-02-02 17:33:30 +01:00