From: "cron2 (Code Review)" Date: Thu, 15 Jan 2026 09:23:22 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <513be09303830e7fd34d4e3812c706dc50daa9ca-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="zp4LcWWrAx4="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[release/2.6]: tests/unit_tests: Port to cmocka 2.0.0 API List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld , plaisthos Cc: openvpn-devel --zp4LcWWrAx4= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#2) to the change originally created by= flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1462?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 Change subject: tests/unit_tests: Port to cmocka 2.0.0 API ...................................................................... tests/unit_tests: Port to cmocka 2.0.0 API 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. This backport commit additionally includes the fix for test_tls_crypt from commit 6246f2113f6e1fda13bca8de863dd5cc396ab6ef since the other test fixes from the original commit are not relevant to release/2.6. Change-Id: Ifb6594700db71d219643a29c581099c778bcbbc6 Signed-off-by: Frank Lichtenheld Signed-off-by: Gert Doering Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1462 (cherry picked from commit 6db186e0b1d9783ea96e8a945a47fd23b45e4778) Message-Id: <20260115082817.18618-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg35= 273.html Signed-off-by: Gert Doering --- M CMakeLists.txt M config.h.cmake.in M configure.ac M tests/unit_tests/openvpn/test_common.h M tests/unit_tests/openvpn/test_tls_crypt.c 5 files changed, 49 insertions(+), 12 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/62/1462/2 diff --git a/CMakeLists.txt b/CMakeLists.txt index f7cb5a7..6983d111 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -291,6 +291,18 @@ ) 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 @@ -541,14 +553,6 @@ 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_auth_token" "test_buffer" diff --git a/config.h.cmake.in b/config.h.cmake.in index cedc5be..bf60d39 100644 --- a/config.h.cmake.in +++ b/config.h.cmake.in @@ -86,6 +86,9 @@ /* git version information in config-version.h */ #cmakedefine HAVE_CONFIG_VERSION_H +/* cmocka version information available in cmocka_version.h (>=3D 2.0.0) */ +#cmakedefine HAVE_CMOCKA_VERSION_H + /* Define to 1 if you have the `daemon' function. */ #cmakedefine HAVE_DAEMON diff --git a/configure.ac b/configure.ac index e77fd47..fb8d530 100644 --- a/configure.ac +++ b/configure.ac @@ -1525,7 +1525,16 @@ [have_cmocka=3D"yes"], [AC_MSG_WARN([cmocka.pc not found on the system using pkg-config ${pkg_co= nfig_found}. Unit tests disabled])] ) -AM_CONDITIONAL([ENABLE_UNITTESTS], [test "${enable_unit_tests}" =3D "yes" = -a "${have_cmocka}" =3D "yes" ]) +AM_CONDITIONAL([ENABLE_UNITTESTS], [false]) +if test "${enable_unit_tests}" =3D "yes" -a "${have_cmocka}" =3D "yes"; th= en + AM_CONDITIONAL([ENABLE_UNITTESTS], [true]) + + saved_CFLAGS=3D"${CFLAGS}" + CFLAGS=3D"${CFLAGS} ${CMOCKA_CFLAGS}" + # detect cmocka < 2.0.0 that had no cmocka_version.h + AC_CHECK_HEADERS([cmocka_version.h]) + CFLAGS=3D"${saved_CFLAGS}" +fi AC_SUBST([ENABLE_UNITTESTS]) TEST_LDFLAGS=3D"${OPTIONAL_CRYPTO_LIBS} ${OPTIONAL_PKCS11_HELPER_LIBS}" diff --git a/tests/unit_tests/openvpn/test_common.h b/tests/unit_tests/open= vpn/test_common.h index 50e16d6..e5e4420 100644 --- a/tests/unit_tests/openvpn/test_common.h +++ b/tests/unit_tests/openvpn/test_common.h @@ -25,6 +25,27 @@ #include #include +/* 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 stdo= ut * non-buffered to avoid messages getting lost if the program exits early. diff --git a/tests/unit_tests/openvpn/test_tls_crypt.c b/tests/unit_tests/o= penvpn/test_tls_crypt.c index 5eb864e..bf5a8ce 100644 --- a/tests/unit_tests/openvpn/test_tls_crypt.c +++ b/tests/unit_tests/openvpn/test_tls_crypt.c @@ -112,8 +112,8 @@ __wrap_buffer_write_file(const char *filename, const struct buffer *buf) { const char *pem =3D BSTR(buf); - check_expected(filename); - check_expected(pem); + check_expected_ptr(filename); + check_expected_ptr(pem); return mock_type(bool); } @@ -121,7 +121,7 @@ struct buffer __wrap_buffer_read_from_file(const char *filename, struct gc_arena *gc) { - check_expected(filename); + check_expected_ptr(filename); const char *pem_str =3D mock_ptr_type(const char *); struct buffer ret =3D alloc_buf_gc(strlen(pem_str) + 1, gc); -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1462?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: release/2.6 Gerrit-Change-Id: Ifb6594700db71d219643a29c581099c778bcbbc6 Gerrit-Change-Number: 1462 Gerrit-PatchSet: 2 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --zp4LcWWrAx4= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 upl= oaded patch set #2 to the change originally created by flichtenhel= d.

View Change

The following approvals got outdated and were remove= d: Code-Review+2 by cron2

tests/unit_tests: Port to cmocka 2.=
0.0 API

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

Thi= s backport commit additionally includes the fix
for test_tls_crypt from = commit
6246f2113f6e1fda13bca8de863dd5cc396ab6ef since the
other test = fixes from the original commit are not
relevant to release/2.6.

C= hange-Id: Ifb6594700db71d219643a29c581099c778bcbbc6
Signed-off-by: Frank= Lichtenheld <frank@...2641...>
Signed-off-by: Gert Doering <ge= rt@...1296...>
Acked-by: Gert Doering <gert@...1296...>
Gerr= it URL: https://gerrit.openvpn.net/c/openvpn/+/1462
(cherry picked from = commit 6db186e0b1d9783ea96e8a945a47fd23b45e4778)
Message-Id: <2026011= 5082817.18618-1-gert@...1296...>
URL: https://www.mail-archive.com/op= envpn-devel@lists.sourceforge.net/msg35273.html
Signed-off-by: Gert Doer= ing <gert@...1296...>
---
M CMakeLists.txt
M config.h.cmake.= in
M configure.ac
M tests/unit_tests/openvpn/test_common.h
M tests= /unit_tests/openvpn/test_tls_crypt.c
5 files changed, 49 insertions(+), = 12 deletions(-)

git pull ssh://gerrit.openvpn.ne=
t:29418/openvpn refs/changes/62/1462/2
diff --git a/CMakeLists.txt =
b/CMakeLists.txt
index f7cb5a7..6983d111 100644
<= span>--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -291,6 +291,18 @@
)
set(HAVE_C= ONFIG_VERSION_H YES)

+if (BUILD_TESTING)
+ find_package(cmocka CONFIG)
+ if (TARGET cmocka::cmocka)
+ set(CMOCKA_LIBRARIES cmock= a::cmocka)
+ else = ()
+ pkg_searc= h_module(cmocka cmocka REQUIRED IMPORTED_TARGET)
+ set(CMOCKA_LIBRARIES PkgConfig::cmocka= )
+ endif ()
+ set(CMAKE_REQUIRED_LI= BRARIES ${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
@@ -541,14 +553= ,6 @@


if (BUILD_TESTIN= G)
- find_package(cm= ocka CONFIG)
- if (T= ARGET cmocka::cmocka)
-= set(CMOCKA_LIBRARIES cmocka::cmocka)
- else ()
- pkg_search_module(cmocka cmocka REQUIRED IMPORTED_TARG= ET)
- set(CMOCKA= _LIBRARIES PkgConfig::cmocka)
- endif ()
-<= /span>
set(unit_tests
"test_aut= h_token"
"test_buffer"
diff --git a/config.h.cmake.in b/config.h.cmake.in

inde= x cedc5be..bf60d39 100644
--- a/config.h.cmake.in+++ b/config.h.cmake.in
@@ -86,6 +86,9 @@
= /* git version information in config-version.h */
#= cmakedefine HAVE_CONFIG_VERSION_H

+/* cmocka version information available i= n cmocka_version.h (>=3D 2.0.0) */
+#cmakedefine HAVE_CMOCKA_VERSION_H
+
/* Define to 1 if you h= ave the `daemon' function. */
#cmakedefine HAVE_DAEMON=

diff --git a/configure.ac b/configure.ac=
index e77fd47..fb8d530 100644
--- a/config= ure.ac
+++ b/configure.ac
@@ -1525,7 +1525,= 16 @@
[have_cmocka=3D"yes"],
= [AC_MSG_WARN([cmocka.pc not found on the system using pkg-config ${pkg_conf= ig_found}. Unit tests disabled])]
)
-AM_CONDITIONAL([ENABLE_UNITTESTS], [test &q= uot;${enable_unit_tests}" =3D "yes" -a "${have_cmocka}&= quot; =3D "yes" ])
+AM_CONDITIONAL([ENABLE_UNITTESTS], [false])
+if test "${enable_unit_tests}" = =3D "yes" -a "${have_cmocka}" =3D "yes"; then=
+ AM_CONDITIONAL([= ENABLE_UNITTESTS], [true])
+
+ saved_CFL= AGS=3D"${CFLAGS}"
+ CFLAGS=3D"${CFLAGS} ${CMOCKA_CFLAGS}"
+ # detect cmocka < 2.0.0 that h= ad no cmocka_version.h
+ CFLAGS=3D"${saved_CFLAGS}"
+fi
AC_SUBST([EN= ABLE_UNITTESTS])

TEST_LDFLAGS=3D"${= OPTIONAL_CRYPTO_LIBS} ${OPTIONAL_PKCS11_HELPER_LIBS}"
= diff --git a/tests/unit_tests/openvpn/test_common.h b/tests/unit_tests/open= vpn/test_common.h
index 50e16d6..e5e4420 100644
<= span>--- a/tests/unit_tests/openvpn/test_common.h
+++ b/tes= ts/unit_tests/openvpn/test_common.h
@@ -25,6 +25,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 ve= rsions have LargestIntegralType. Newer
+ * versions use uintmax_t. But LargestIntegralType
+ * is not guaranteed to b= e equal to uintmax_t, so
+ * we can't use that unconditionally. So we only use
+ * it if cmocka.h does not define= LargestIntegralType.

+ */
+#ifndef Larges= tIntegralType
+#defin= e LargestIntegralType uintmax_t
+#endif
+/= * redefine 2.x API in terms of 1.x API */
+#define CMockaValueData LargestIntegralTy= pe
+#define check_exp= ected_uint check_expected
+#define expect_uint_value expect_value
+#define expect_check_data = expect_check
+#de= fine cast_ptr_to_cmocka_value(x) (x)
+#endif
+
/**
* Sets up the environment for u= nit tests like making both stderr and stdout
* non-buffer= ed to avoid messages getting lost if the program exits early.
diff --git a/tests/unit_tests/openvpn/test_tls_crypt.c b/tests/unit_test= s/openvpn/test_tls_crypt.c
index 5eb864e..bf5a8ce 100644

--- a/tests/unit_tests/openvpn/test_tls_crypt.c
+++ b/tests/unit_tests/openvpn/test_tls_crypt.c
@@ -112,= 8 +112,8 @@
__wrap_buffer_write_file(const char *filename,= const struct buffer *buf)
{
const ch= ar *pem =3D BSTR(buf);
= - check_expected(filename);
- check_expected(pem);
+ check_expected_ptr(filename);
+ check_expected_ptr(pem);
<= /span>
return mock_type(bool);
}
<= span>@@ -121,7 +121,7 @@

struct buffer
__= wrap_buffer_read_from_file(const char *filename, struct gc_arena *gc)
{
- chec= k_expected(filename);
+ check_expected_ptr(filename);

= const char *pem_str =3D mock_ptr_type(const char *);
s= truct buffer ret =3D alloc_buf_gc(strlen(pem_str) + 1, gc);

To view, visit change 1462. To unsubscribe, or for help wr= iting mail filters, visit settings.

=
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: release/2.6
Gerrit-Change-Id: Ifb6594700db71d219643a29c581= 099c778bcbbc6
Gerrit-Change-Number: 1462
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --zp4LcWWrAx4=--