From patchwork Thu Dec 18 10:40:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 4691 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:8f0c:b0:7b1:439f:bdf with SMTP id mq12csp290067mab; Thu, 18 Dec 2025 02:40:59 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUCh9NTcMCo3yVQPR1m/RsFcKS8H/rWUtdK6aM3fSyHTYqKAPBY4IrkF6gJeiRdur2945ZAgO0Ekm4=@openvpn.net X-Google-Smtp-Source: AGHT+IHYEjbMMGm1EcMq60oHiSzqNHX2i5gcp0u0EE87lLqdOjULnYR8hQ0ZWhym0laB8BVEGGax X-Received: by 2002:a05:6820:1892:b0:659:9a49:9067 with SMTP id 006d021491bc7-65b4523b4e1mr9288114eaf.50.1766054459086; Thu, 18 Dec 2025 02:40:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1766054459; cv=none; d=google.com; s=arc-20240605; b=lLRa7jWfzMpzrtnUVL56+GeRve2otZjuwd3FjvRqnLRJ3EPCmq7acoa855CoeDzA+X dQiY1qPGneY4R0fGSykLd3pYOWRTKivOtFMzfKqnT0/3X+9WPn6Ho+HpGvWdVUBfEo64 vXNPqhD4FTN2TDCUu3fEtrd61TmabNKdN5ERMmQMleQh2ji9XFnnLkmDgD2XhFN1K9ie QoyrCCvplvykWnw7UxB6UKpwuTdq7asxCYt5tKt2kiTLXv/bzoEENZH6AGoHYTpDKeq8 +uEswzzMqzRieNOZN4RW95IEtOZrXm2VMXdPP13Lmhlhz0g3Rep0TgW6U70amgNysGvY y3RQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=YCkpuoaFPiWJ/gwzIPY1HWGaT3rx3WwsdQLpuCyHdUg=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=FUCshvFcNhPdq9d7YuIVc1IZnZL42+uFT5ILCCHRaxQsfOMIY6jcWmXBe0L0FJ5++a 9R97GDEad6JbF1/ts8c5S4XRCFdB0p4tbUSjUopa3Bqo6SJp+YQgGCYU2Gk0vFiCH51Z E1H2nENNlSzEDr5KcBdPpYyhAwYWaIfCqTOC7GyilxPaFGc+LIPs+odhLIUjoKSphwv+ uYyFTDQ1QVx02Bo9zDQOliChsVyzL18mm6l/DoglWsToEtPdkQJlWQ3DVdQ+fis0MtE3 1gYnay6fV0iRYHzc1pL+85e3HcculeRJOKMvxg/yz0ygMlO3UONo9+E8gFUho3f6DFyM 3Bmw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=A+VJPfku; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=bVOr6UHt; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=eDCc64+b; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id 006d021491bc7-65cff340e7bsi1182989eaf.82.2025.12.18.02.40.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Dec 2025 02:40:59 -0800 (PST) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=A+VJPfku; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=bVOr6UHt; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=eDCc64+b; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:References:In-Reply-To:Message-ID:Date:To:From:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=YCkpuoaFPiWJ/gwzIPY1HWGaT3rx3WwsdQLpuCyHdUg=; b=A+VJPfkuDLcPL1ADv+5fcuKivH eZKVUPd2ED/Ni5+t5KqlUNUklfqLAo2YE0WIOPf0IjBSFXVL0OHyDFszEmeyMv6o8lgDYjPsmZZck /KgrKHmbBKr423bcMsl/XylCI7iSwq+hryMp3Lpxo4b9tmiKDd4i2QwRsQyGLslI2Sqc=; Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1vWBRD-0008IN-Rk; Thu, 18 Dec 2025 10:40:52 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1vWBRC-0008I8-D8 for openvpn-devel@lists.sourceforge.net; Thu, 18 Dec 2025 10:40:51 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Of9inqpUWJuS3vZ/c6U4bVd0JilOZLoF+EQaH7vwISU=; b=bVOr6UHtbA+CsPMf2rhzZNlj/d d5ydlVKn/XDZ+7SX6mFVPAaYwIf18EaBvFxC9JkCx9SoQ7IyvXdmgv3M9QJrTLDr/iROkZmAADSIM u65EzqJAas/j+Vm2NMCsHhP2qnuHVJueNc/peW+FhdAfQRh5nUsj3Q7mR4hUsi2EZ1DA=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=Of9inqpUWJuS3vZ/c6U4bVd0JilOZLoF+EQaH7vwISU=; b=eDCc64+bCb2MNkW2j5m/mc0UCJ MoHerAkW8RsTOT0NuJzemdGQbmwtAXoVl38tnKE7eI4LmzC6u4CgrDqSofxiOm4u3K8jfVqCgmJws 6xLyMaEI0DFJt1/pU05wXIHErhwfeFRLlHZ+AMO5wdBoeumt9ofDO+r6/JmvvGzPC+rw=; Received: from [193.149.48.134] (helo=blue.greenie.muc.de) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1vWBRB-0004WR-VI for openvpn-devel@lists.sourceforge.net; Thu, 18 Dec 2025 10:40:51 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.18.1/8.18.1) with ESMTP id 5BIAehkB005980 for ; Thu, 18 Dec 2025 11:40:43 +0100 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.1/8.18.1/Submit) id 5BIAehqi005979 for openvpn-devel@lists.sourceforge.net; Thu, 18 Dec 2025 11:40:43 +0100 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Thu, 18 Dec 2025 11:40:32 +0100 Message-ID: <20251218104042.5961-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.51.2 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: 1.3 (+) X-Spam-Report: Spam detection software, running on the system "sfi-spamd-2.hosts.colo.sdot.me", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Frank Lichtenheld 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. Content analysis details: (1.3 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS X-Headers-End: 1vWBRB-0004WR-VI Subject: [Openvpn-devel] [PATCH v3] tests/unit_tests: Port to cmocka 2.0.0 API X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1851842320275170834?= X-GMAIL-MSGID: =?utf-8?q?1851842320275170834?= From: Frank Lichtenheld 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 Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1449 --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1449 This mail reflects revision 3 of this Change. Acked-by according to Gerrit (reflected above): Gert Doering diff --git a/CMakeLists.txt b/CMakeLists.txt index bdb1904..fc5eaf1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -379,6 +379,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 @@ -636,14 +648,6 @@ 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" diff --git a/config.h.cmake.in b/config.h.cmake.in index 53c3598..01bbadc 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 (>= 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 d5a0c70..f363e0f 100644 --- a/configure.ac +++ b/configure.ac @@ -1415,7 +1415,16 @@ [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}" diff --git a/tests/unit_tests/openvpn/test_common.h b/tests/unit_tests/openvpn/test_common.h index da98531..3a0598d 100644 --- a/tests/unit_tests/openvpn/test_common.h +++ b/tests/unit_tests/openvpn/test_common.h @@ -24,6 +24,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 stdout * non-buffered to avoid messages getting lost if the program exits early. diff --git a/tests/unit_tests/openvpn/test_options_parse.c b/tests/unit_tests/openvpn/test_options_parse.c index adf3e5b..5af84f5 100644 --- a/tests/unit_tests/openvpn/test_options_parse.c +++ b/tests/unit_tests/openvpn/test_options_parse.c @@ -44,8 +44,8 @@ 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 @@ &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 @@ /* 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, "\nsome text\nother text\n"); 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, "\n" A_TIMES_256 A_TIMES_256 A_TIMES_256 A_TIMES_256 A_TIMES_256 "\n"); gc_free(&o.gc); diff --git a/tests/unit_tests/openvpn/test_push_update_msg.c b/tests/unit_tests/openvpn/test_push_update_msg.c index 5c16001..9b7978e 100644 --- a/tests/unit_tests/openvpn/test_push_update_msg.c +++ b/tests/unit_tests/openvpn/test_push_update_msg.c @@ -171,7 +171,7 @@ bool send_control_channel_string(struct context *c, const char *str, msglvl_t msglevel) { - check_expected(str); + check_expected_ptr(str); return true; } diff --git a/tests/unit_tests/openvpn/test_user_pass.c b/tests/unit_tests/openvpn/test_user_pass.c index abe223c..a109201 100644 --- a/tests/unit_tests/openvpn/test_user_pass.c +++ b/tests/unit_tests/openvpn/test_user_pass.c @@ -52,7 +52,7 @@ /* 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); }