tests/unit_tests: Port to cmocka 2.0.0 API
Some checks failed
Build / Check code style with clang-format (push) Has been cancelled
Build / Android - arm64-v8a (push) Has been cancelled
Build / gcc-mingw - x64 - Debug - OSSL (push) Has been cancelled
Build / gcc-mingw - x64 - Release - OSSL (push) Has been cancelled
Build / gcc-mingw - x86 - Debug - OSSL (push) Has been cancelled
Build / gcc-mingw - x86 - Release - OSSL (push) Has been cancelled
Build / gcc - ubuntu-24.04 - OpenSSL 3.0.13 --enable-pkcs11 (push) Has been cancelled
Build / gcc - ubuntu-22.04 - OpenSSL 3.0.2 --enable-pkcs11 (push) Has been cancelled
Build / clang-asan - ubuntu-22.04 - openssl (push) Has been cancelled
Build / clang-asan - ubuntu-24.04 - openssl (push) Has been cancelled
Build / macos-14 - libressl - asan (push) Has been cancelled
Build / macos-14 - openssl@3 - asan (push) Has been cancelled
Build / macos-15 - libressl - asan (push) Has been cancelled
Build / macos-15 - openssl@3 - asan (push) Has been cancelled
Build / macos-26 - libressl - asan (push) Has been cancelled
Build / macos-26 - openssl@3 - asan (push) Has been cancelled
Build / macos-14 - libressl - normal (push) Has been cancelled
Build / macos-14 - openssl@3 - normal (push) Has been cancelled
Build / macos-15 - libressl - normal (push) Has been cancelled
Build / macos-15 - openssl@3 - normal (push) Has been cancelled
Build / macos-26 - libressl - normal (push) Has been cancelled
Build / macos-26 - openssl@3 - normal (push) Has been cancelled
Build / msbuild - amd64 - openssl (push) Has been cancelled
Build / msbuild - amd64-clang - openssl (push) Has been cancelled
Build / msbuild - arm64 - openssl (push) Has been cancelled
Build / msbuild - x86 - openssl (push) Has been cancelled
Build / msbuild - x86-clang - openssl (push) Has been cancelled
Build / clang asan - ubuntu-22.04 - libressl (push) Has been cancelled
Build / gcc normal - ubuntu-22.04 - libressl (push) Has been cancelled
Build / clang asan - ubuntu-22.04 - mbedtls3 (push) Has been cancelled
Build / gcc normal - ubuntu-22.04 - mbedtls3 (push) Has been cancelled
Build / clang asan - ubuntu-24.04 - awslc (push) Has been cancelled
Build / gcc normal - ubuntu-24.04 - awslc (push) Has been cancelled
Deploy Doxygen documentation to Pages / build (push) Has been cancelled
Build / mingw unittest argv - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest auth_token - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest buffer - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest crypto - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest cryptoapi - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest misc - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest ncp - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest options_parse - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest packet_id - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest pkt - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest provider - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest ssl - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest tls_crypt - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest user_pass - x64 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest argv - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest auth_token - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest buffer - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest crypto - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest cryptoapi - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest misc - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest ncp - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest options_parse - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest packet_id - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest pkt - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest provider - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest ssl - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest tls_crypt - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest user_pass - x64 - Release - OSSL (push) Has been cancelled
Build / mingw unittest argv - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest auth_token - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest buffer - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest crypto - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest cryptoapi - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest misc - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest ncp - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest options_parse - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest packet_id - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest pkt - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest provider - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest ssl - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest tls_crypt - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest user_pass - x86 - Debug - OSSL (push) Has been cancelled
Build / mingw unittest argv - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest auth_token - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest buffer - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest crypto - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest cryptoapi - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest misc - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest ncp - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest options_parse - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest packet_id - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest pkt - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest provider - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest ssl - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest tls_crypt - x86 - Release - OSSL (push) Has been cancelled
Build / mingw unittest user_pass - x86 - Release - OSSL (push) Has been cancelled
Deploy Doxygen documentation to Pages / deploy (push) Has been cancelled

But add compat layer so that we can still build
against older versions of cmocka. Mostly this is
trivial but the custom check function changed its
prototype, so that requires some more work.

Change-Id: Ifb6594700db71d219643a29c581099c778bcbbc6
Signed-off-by: Frank Lichtenheld <frank@lichtenheld.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1449
Message-Id: <20251218104042.5961-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg35144.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
This commit is contained in:
Frank Lichtenheld 2025-12-18 11:40:32 +01:00 committed by Gert Doering
parent ab5887cfbd
commit 6db186e0b1
7 changed files with 79 additions and 46 deletions

View file

@ -379,6 +379,18 @@ add_custom_command(
)
set(HAVE_CONFIG_VERSION_H YES)
if (BUILD_TESTING)
find_package(cmocka CONFIG)
if (TARGET cmocka::cmocka)
set(CMOCKA_LIBRARIES cmocka::cmocka)
else ()
pkg_search_module(cmocka cmocka REQUIRED IMPORTED_TARGET)
set(CMOCKA_LIBRARIES PkgConfig::cmocka)
endif ()
set(CMAKE_REQUIRED_LIBRARIES ${CMOCKA_LIBRARIES})
check_include_files(cmocka_version.h HAVE_CMOCKA_VERSION_H)
endif ()
configure_file(config.h.cmake.in config.h)
configure_file(include/openvpn-plugin.h.in openvpn-plugin.h)
# TODO we should remove the need for this, and always include config.h
@ -636,14 +648,6 @@ endif ()
option(UT_ALLOW_BIG_ALLOC "Allow unit-tests to use > 1 GB of memory" ON)
if (BUILD_TESTING)
find_package(cmocka CONFIG)
if (TARGET cmocka::cmocka)
set(CMOCKA_LIBRARIES cmocka::cmocka)
else ()
pkg_search_module(cmocka cmocka REQUIRED IMPORTED_TARGET)
set(CMOCKA_LIBRARIES PkgConfig::cmocka)
endif ()
set(unit_tests
"test_argv"
"test_auth_token"

View file

@ -86,6 +86,9 @@
/* git version information in config-version.h */
#cmakedefine HAVE_CONFIG_VERSION_H
/* cmocka version information available in cmocka_version.h (>= 2.0.0) */
#cmakedefine HAVE_CMOCKA_VERSION_H
/* Define to 1 if you have the `daemon' function. */
#cmakedefine HAVE_DAEMON

View file

@ -1415,7 +1415,16 @@ PKG_CHECK_MODULES(
[have_cmocka="yes"],
[AC_MSG_WARN([cmocka.pc not found on the system using pkg-config ${pkg_config_found}. Unit tests disabled])]
)
AM_CONDITIONAL([ENABLE_UNITTESTS], [test "${enable_unit_tests}" = "yes" -a "${have_cmocka}" = "yes" ])
AM_CONDITIONAL([ENABLE_UNITTESTS], [false])
if test "${enable_unit_tests}" = "yes" -a "${have_cmocka}" = "yes"; then
AM_CONDITIONAL([ENABLE_UNITTESTS], [true])
saved_CFLAGS="${CFLAGS}"
CFLAGS="${CFLAGS} ${CMOCKA_CFLAGS}"
# detect cmocka < 2.0.0 that had no cmocka_version.h
AC_CHECK_HEADERS([cmocka_version.h])
CFLAGS="${saved_CFLAGS}"
fi
AC_SUBST([ENABLE_UNITTESTS])
TEST_LDFLAGS="${OPTIONAL_CRYPTO_LIBS} ${OPTIONAL_PKCS11_HELPER_LIBS} ${OPTIONAL_LIBCAPNG_LIBS}"

View file

@ -24,6 +24,27 @@
#include <setjmp.h>
#include <cmocka.h>
/* Do we use cmocka < 2.0.0? */
#ifndef HAVE_CMOCKA_VERSION_H
#define HAVE_OLD_CMOCKA_API 1
/* compat with various versions of cmocka.h
* Older versions have LargestIntegralType. Newer
* versions use uintmax_t. But LargestIntegralType
* is not guaranteed to be equal to uintmax_t, so
* we can't use that unconditionally. So we only use
* it if cmocka.h does not define LargestIntegralType.
*/
#ifndef LargestIntegralType
#define LargestIntegralType uintmax_t
#endif
/* redefine 2.x API in terms of 1.x API */
#define CMockaValueData LargestIntegralType
#define check_expected_uint check_expected
#define expect_uint_value expect_value
#define expect_check_data expect_check
#define cast_ptr_to_cmocka_value(x) (x)
#endif
/**
* Sets up the environment for unit tests like making both stderr and stdout
* non-buffered to avoid messages getting lost if the program exits early.

View file

@ -44,8 +44,8 @@ add_option(struct options *options, char *p[], bool is_inline, const char *file,
struct env_set *es)
{
function_called();
check_expected(p);
check_expected(is_inline);
check_expected_ptr(p);
check_expected_uint(is_inline);
}
void
@ -198,31 +198,27 @@ read_single_config(struct options *options, const char *config)
&option_types_found, &es);
}
/* compat with various versions of cmocka.h
* Older versions have LargestIntegralType. Newer
* versions use uintmax_t. But LargestIntegralType
* is not guaranteed to be equal to uintmax_t, so
* we can't use that unconditionally. So we only use
* it if cmocka.h does not define LargestIntegralType.
*/
#ifndef LargestIntegralType
#define LargestIntegralType uintmax_t
#if HAVE_OLD_CMOCKA_API
union token_parameter
{
LargestIntegralType int_val;
void *ptr;
};
#endif
union tokens_parameter
{
LargestIntegralType as_int;
void *as_pointer;
};
static int
check_tokens(const LargestIntegralType value, const LargestIntegralType expected)
check_tokens(const CMockaValueData value, const CMockaValueData expected)
{
union tokens_parameter temp;
temp.as_int = value;
const char **p = (const char **)temp.as_pointer;
temp.as_int = expected;
const char **expected_p = (const char **)temp.as_pointer;
#if HAVE_OLD_CMOCKA_API
union token_parameter temp;
temp.int_val = value;
const char **p = (const char **)temp.ptr;
temp.int_val = expected;
const char **expected_p = (const char **)temp.ptr;
#else
const char **p = (const char **)value.ptr;
const char **expected_p = (const char **)expected.ptr;
#endif
for (int i = 0; i < MAX_PARMS; i++)
{
if (!p[i] && !expected_p[i])
@ -271,33 +267,33 @@ test_read_config(void **state)
/* basic test */
expect_function_call(add_option);
expect_check(add_option, p, check_tokens, p_expect_someopt);
expect_value(add_option, is_inline, 0);
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_someopt));
expect_uint_value(add_option, is_inline, 0);
expect_function_call(add_option);
expect_check(add_option, p, check_tokens, p_expect_otheropt);
expect_value(add_option, is_inline, 0);
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_otheropt));
expect_uint_value(add_option, is_inline, 0);
read_single_config(&o, "someopt parm1 parm2\n otheropt 1 2");
/* -- gets stripped */
expect_function_call(add_option);
expect_check(add_option, p, check_tokens, p_expect_someopt);
expect_value(add_option, is_inline, 0);
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_someopt));
expect_uint_value(add_option, is_inline, 0);
expect_function_call(add_option);
expect_check(add_option, p, check_tokens, p_expect_otheropt);
expect_value(add_option, is_inline, 0);
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_otheropt));
expect_uint_value(add_option, is_inline, 0);
read_single_config(&o, "someopt parm1 parm2\n\t--otheropt 1 2");
/* inline options */
expect_function_call(add_option);
expect_check(add_option, p, check_tokens, p_expect_inlineopt);
expect_value(add_option, is_inline, 1);
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_inlineopt));
expect_uint_value(add_option, is_inline, 1);
read_single_config(&o, "<inlineopt>\nsome text\nother text\n</inlineopt>");
p_expect_inlineopt[0] = "inlineopt";
p_expect_inlineopt[1] = A_TIMES_256 A_TIMES_256 A_TIMES_256 A_TIMES_256 A_TIMES_256 "\n";
expect_function_call(add_option);
expect_check(add_option, p, check_tokens, p_expect_inlineopt);
expect_value(add_option, is_inline, 1);
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_inlineopt));
expect_uint_value(add_option, is_inline, 1);
read_single_config(&o, "<inlineopt>\n" A_TIMES_256 A_TIMES_256 A_TIMES_256 A_TIMES_256 A_TIMES_256 "\n</inlineopt>");
gc_free(&o.gc);

View file

@ -171,7 +171,7 @@ send_control_channel_string(struct context *c, const char *str, msglvl_t msgleve
bool
send_control_channel_string(struct context *c, const char *str, msglvl_t msglevel)
{
check_expected(str);
check_expected_ptr(str);
return true;
}

View file

@ -52,7 +52,7 @@ query_user_exec_builtin(void)
/* Loop through configured query_user slots */
for (int i = 0; i < QUERY_USER_NUMSLOTS && query_user[i].response != NULL; i++)
{
check_expected(query_user[i].prompt);
check_expected_ptr(query_user[i].prompt);
strncpy(query_user[i].response, mock_ptr_type(char *), query_user[i].response_len);
}