From: "cron2 (Code Review)" Date: Fri, 12 Dec 2025 12:53:03 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <26b063e1705d511e73d52264dfe7be61dd9ec6d0-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="EOoGleg+3z0="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: mbuf: Add unit tests List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: plaisthos , openvpn-devel --EOoGleg+3z0= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/14= 32?usp=3Demail ) Change subject: mbuf: Add unit tests ...................................................................... mbuf: Add unit tests While fixing the conversion warning I was somewhat confused how this works, so added UTs to verify I understood it. v2: - disable assert test for MS VS - add define for memory-intensive UTs and only enable it by default for CMake builds, so we do not break a lot of builds out there due to memory allocation failures Change-Id: Icab68a5fd1b6288955f0073179f1ddde1468d951 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1432 Message-Id: <20251212120352.17402-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg35= 050.html Signed-off-by: Gert Doering --- M CMakeLists.txt M src/openvpn/mbuf.c M src/openvpn/mbuf.h M src/openvpn/options.c M tests/unit_tests/openvpn/Makefile.am M tests/unit_tests/openvpn/test_buffer.c A tests/unit_tests/openvpn/test_mbuf.c 7 files changed, 209 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 906fa04..bdb1904 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -633,6 +633,7 @@ endif () endif () +option(UT_ALLOW_BIG_ALLOC "Allow unit-tests to use > 1 GB of memory" ON) if (BUILD_TESTING) find_package(cmocka CONFIG) @@ -649,6 +650,7 @@ "test_buffer" "test_crypto" "test_dhcp" + "test_mbuf" "test_misc" "test_ncp" "test_options_parse" @@ -739,6 +741,9 @@ # for compat with IDEs like Clion that ignore the tests properties # for the environment variable srcdir when running tests as fallba= ck target_compile_definitions(${test_name} PRIVATE "UNIT_TEST_SOURCED= IR=3D\"${_UT_SOURCE_DIR}\"") + if (UT_ALLOW_BIG_ALLOC) + target_compile_definitions(${test_name} PRIVATE UNIT_TEST_ALLO= W_BIG_ALLOC) + endif () if (NOT ${test_name} STREQUAL "test_buffer") target_sources(${test_name} PRIVATE @@ -800,6 +805,12 @@ src/openvpn/xkey_provider.c ) + target_sources(test_mbuf PRIVATE + tests/unit_tests/openvpn/mock_get_random.c + src/openvpn/buffer.c + src/openvpn/mbuf.c + ) + target_sources(test_misc PRIVATE tests/unit_tests/openvpn/mock_get_random.c src/openvpn/options_util.c diff --git a/src/openvpn/mbuf.c b/src/openvpn/mbuf.c index 448124c..42858ec 100644 --- a/src/openvpn/mbuf.c +++ b/src/openvpn/mbuf.c @@ -42,6 +42,8 @@ struct mbuf_set * mbuf_init(unsigned int size) { + ASSERT(size <=3D MBUF_SIZE_MAX); + struct mbuf_set *ret; ALLOC_OBJ_CLEAR(ret, struct mbuf_set); ret->capacity =3D adjust_power_of_2(size); @@ -121,6 +123,7 @@ bool ret =3D false; if (ms) { + ASSERT(item); while (ms->len) { *item =3D ms->array[ms->head]; diff --git a/src/openvpn/mbuf.h b/src/openvpn/mbuf.h index 7f8c1b7..f741e21 100644 --- a/src/openvpn/mbuf.h +++ b/src/openvpn/mbuf.h @@ -36,6 +36,8 @@ struct multi_instance; #define MBUF_INDEX(head, offset, size) (((head) + (offset)) & ((size) - 1)) +/* limited by adjust_power_of_2 and array_mult_safe */ +#define MBUF_SIZE_MAX (ALLOC_SIZE_MAX / sizeof(struct mbu= f_item)) struct mbuf_buffer { diff --git a/src/openvpn/options.c b/src/openvpn/options.c index d01ec47..24c3e92 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -62,6 +62,7 @@ #include "options_util.h" #include "tun_afunix.h" #include "domain_helper.h" +#include "mbuf.h" #include @@ -7552,7 +7553,7 @@ else if (streq(p[0], "bcast-buffers") && p[1] && !p[2]) { VERIFY_PERMISSION(OPT_P_GENERAL); - atoi_constrained(p[1], &options->n_bcast_buf, p[0], 1, INT_MAX, ms= glevel); + atoi_constrained(p[1], &options->n_bcast_buf, p[0], 1, MBUF_SIZE_M= AX, msglevel); } else if (streq(p[0], "tcp-queue-limit") && p[1] && !p[2]) { diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvp= n/Makefile.am index 0f13172..7aeea47 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -6,9 +6,22 @@ AM_TESTS_ENVIRONMENT =3D export LSAN_OPTIONS=3Dsuppressions=3D$(srcdir)/in= put/leak_suppr.txt; -test_binaries =3D argv_testdriver buffer_testdriver crypto_testdriver dhcp= _testdriver packet_id_testdriver auth_token_testdriver \ - ncp_testdriver misc_testdriver options_parse_testdriver pkt_testdriver ss= l_testdriver \ - user_pass_testdriver push_update_msg_testdriver provider_testdriver socke= t_testdriver +test_binaries =3D argv_testdriver \ + auth_token_testdriver \ + buffer_testdriver \ + crypto_testdriver \ + dhcp_testdriver \ + ncp_testdriver \ + mbuf_testdriver \ + misc_testdriver \ + options_parse_testdriver \ + packet_id_testdriver \ + pkt_testdriver \ + provider_testdriver \ + push_update_msg_testdriver \ + socket_testdriver \ + ssl_testdriver \ + user_pass_testdriver if HAVE_LD_WRAP_SUPPORT if !WIN32 @@ -327,6 +340,20 @@ $(top_srcdir)/src/compat/compat-strsep.c \ $(top_srcdir)/src/openvpn/ssl_util.c =20 +mbuf_testdriver_CFLAGS =3D \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/op= envpn \ + -DSOURCEDIR=3D\"$(top_srcdir)\" @TEST_CFLAGS@ + +mbuf_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ + +mbuf_testdriver_SOURCES =3D test_mbuf.c \ + mock_msg.c test_common.h \ + mock_get_random.c \ + $(top_srcdir)/src/openvpn/buffer.c \ + $(top_srcdir)/src/openvpn/win32-util.c \ + $(top_srcdir)/src/openvpn/platform.c \ + $(top_srcdir)/src/openvpn/mbuf.c + misc_testdriver_CFLAGS =3D \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/op= envpn \ -DSOURCEDIR=3D\"$(top_srcdir)\" @TEST_CFLAGS@ diff --git a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/open= vpn/test_buffer.c index 25a8def..8855e5b 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -386,7 +386,7 @@ /* Instead of trying to trick the compiler here, disable the warnings * for this unit test. We know that the results will be truncated - * and we want to test that. Not we need the clang as clang-cl (msvc) = does + * and we want to test that. Note we need the clang as clang-cl (msvc)= does * not define __GNUC__ like it does under UNIX(-like) platforms */ #if defined(__GNUC__) || defined(__clang__) /* some clang version do not understand -Wformat-truncation, so ignore the diff --git a/tests/unit_tests/openvpn/test_mbuf.c b/tests/unit_tests/openvp= n/test_mbuf.c new file mode 100644 index 0000000..cba4da7 --- /dev/null +++ b/tests/unit_tests/openvpn/test_mbuf.c @@ -0,0 +1,160 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2025 OpenVPN Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" + +#include +#include + +#include "buffer.h" +#include "multi.h" +#include "mbuf.h" +#include "test_common.h" + +static void +test_mbuf_init(void **state) +{ + struct mbuf_set *ms =3D mbuf_init(256); + assert_int_equal(ms->capacity, 256); + assert_false(mbuf_defined(ms)); + assert_non_null(ms->array); + mbuf_free(ms); + + ms =3D mbuf_init(257); + assert_int_equal(ms->capacity, 512); + mbuf_free(ms); + +#ifdef UNIT_TEST_ALLOW_BIG_ALLOC /* allocates up to 2GB of memory */ + ms =3D mbuf_init(MBUF_SIZE_MAX); + assert_int_equal(ms->capacity, MBUF_SIZE_MAX); + mbuf_free(ms); + +/* NOTE: expect_assert_failure does not seem to work with MSVC */ +#ifndef _MSC_VER + expect_assert_failure(mbuf_init(MBUF_SIZE_MAX + 1)); +#endif +#endif +} + +static void +test_mbuf_add_remove(void **state) +{ + struct mbuf_set *ms =3D mbuf_init(4); + assert_int_equal(ms->capacity, 4); + assert_false(mbuf_defined(ms)); + assert_non_null(ms->array); + + /* instances */ + struct multi_instance mi =3D { 0 }; + struct multi_instance mi2 =3D { 0 }; + /* buffers */ + struct buffer buf =3D alloc_buf(16); + struct mbuf_buffer *mbuf_buf =3D mbuf_alloc_buf(&buf); + assert_int_equal(mbuf_buf->refcount, 1); + struct mbuf_buffer *mbuf_buf2 =3D mbuf_alloc_buf(&buf); + assert_int_equal(mbuf_buf2->refcount, 1); + free_buf(&buf); + /* items */ + struct mbuf_item mb_item =3D { .buffer =3D mbuf_buf, .instance =3D &mi= }; + struct mbuf_item mb_item2 =3D { .buffer =3D mbuf_buf2, .instance =3D &= mi2 }; + + mbuf_add_item(ms, &mb_item); + assert_int_equal(mbuf_buf->refcount, 2); + assert_int_equal(mbuf_buf2->refcount, 1); + assert_int_equal(mbuf_len(ms), 1); + assert_int_equal(mbuf_maximum_queued(ms), 1); + assert_int_equal(ms->head, 0); + assert_ptr_equal(mbuf_peek(ms), &mi); + + mbuf_add_item(ms, &mb_item2); + assert_int_equal(mbuf_buf->refcount, 2); + assert_int_equal(mbuf_buf2->refcount, 2); + assert_int_equal(mbuf_len(ms), 2); + assert_int_equal(mbuf_maximum_queued(ms), 2); + assert_int_equal(ms->head, 0); + assert_ptr_equal(mbuf_peek(ms), &mi); + + mbuf_add_item(ms, &mb_item2); + assert_int_equal(mbuf_buf->refcount, 2); + assert_int_equal(mbuf_buf2->refcount, 3); + assert_int_equal(mbuf_len(ms), 3); + assert_int_equal(mbuf_maximum_queued(ms), 3); + assert_int_equal(ms->head, 0); + assert_ptr_equal(mbuf_peek(ms), &mi); + + mbuf_add_item(ms, &mb_item2); + mbuf_add_item(ms, &mb_item2); /* overflow, first item gets removed */ + assert_int_equal(mbuf_buf->refcount, 1); + assert_int_equal(mbuf_buf2->refcount, 5); + assert_int_equal(mbuf_len(ms), 4); + assert_int_equal(mbuf_maximum_queued(ms), 4); + assert_int_equal(ms->head, 1); + assert_ptr_equal(mbuf_peek(ms), &mi2); + + mbuf_add_item(ms, &mb_item); + assert_int_equal(mbuf_buf->refcount, 2); + assert_int_equal(mbuf_buf2->refcount, 4); + assert_int_equal(mbuf_len(ms), 4); + assert_int_equal(mbuf_maximum_queued(ms), 4); + assert_int_equal(ms->head, 2); + assert_ptr_equal(mbuf_peek(ms), &mi2); + + struct mbuf_item out_item; + assert_true(mbuf_extract_item(ms, &out_item)); + assert_ptr_equal(out_item.instance, mb_item2.instance); + assert_int_equal(mbuf_buf->refcount, 2); + assert_int_equal(mbuf_buf2->refcount, 4); + assert_int_equal(mbuf_len(ms), 3); + assert_int_equal(mbuf_maximum_queued(ms), 4); + assert_int_equal(ms->head, 3); + assert_ptr_equal(mbuf_peek(ms), &mi2); + mbuf_free_buf(out_item.buffer); + + mbuf_dereference_instance(ms, &mi2); + assert_int_equal(mbuf_buf->refcount, 2); + assert_int_equal(mbuf_buf2->refcount, 1); + assert_int_equal(mbuf_len(ms), 3); + assert_int_equal(mbuf_maximum_queued(ms), 4); + assert_int_equal(ms->head, 3); + assert_ptr_equal(mbuf_peek(ms), &mi); + + mbuf_free(ms); + assert_int_equal(mbuf_buf->refcount, 1); + mbuf_free_buf(mbuf_buf); + assert_int_equal(mbuf_buf2->refcount, 1); + mbuf_free_buf(mbuf_buf2); +} + +int +main(void) +{ + const struct CMUnitTest tests[] =3D { + cmocka_unit_test(test_mbuf_init), + cmocka_unit_test(test_mbuf_add_remove), + }; + + return cmocka_run_group_tests_name("mbuf", tests, NULL, NULL); +} -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1432?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: merged Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Icab68a5fd1b6288955f0073179f1ddde1468d951 Gerrit-Change-Number: 1432 Gerrit-PatchSet: 4 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --EOoGleg+3z0= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 sub= mitted this change.

View Change

mbuf: Add unit tests

While fixing the con= version warning I was
somewhat confused how this works, so added
UTs = to verify I understood it.

v2:
- disable assert test for MS VS - add define for memory-intensive UTs and
only enable it by defaul= t for CMake builds,
so we do not break a lot of builds out there
= due to memory allocation failures

Change-Id: Icab68a5fd1b6288955f0= 073179f1ddde1468d951
Signed-off-by: Frank Lichtenheld <frank@...2724...= ..>
Acked-by: Gert Doering <gert@...1296...>
Gerrit URL: htt= ps://gerrit.openvpn.net/c/openvpn/+/1432
Message-Id: <20251212120352.= 17402-1-gert@...1296...>
URL: https://www.mail-archive.com/openvpn-de= vel@lists.sourceforge.net/msg35050.html
Signed-off-by: Gert Doering <= gert@...1296...>
---
M CMakeLists.txt
M src/openvpn/mbuf.c
M= src/openvpn/mbuf.h
M src/openvpn/options.c
M tests/unit_tests/openvp= n/Makefile.am
M tests/unit_tests/openvpn/test_buffer.c
A tests/unit_t= ests/openvpn/test_mbuf.c
7 files changed, 209 insertions(+), 5 deletions= (-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 906fa= 04..bdb1904 100644
--- a/CMakeLists.txt
+++= b/CMakeLists.txt
@@ -633,6 +633,7 @@
= endif ()
endif ()

+option(UT_ALLOW_BIG_ALLOC "Allow uni= t-tests to use > 1 GB of memory" ON)

if (BUILD_TESTING)
find_package(cmocka CONFIG)

@@ -649,6 +650,7 @@
"test_buffe= r"
"test_crypto"
= "test_dhcp"
+ "test_mbuf"
"test_= misc"
"test_ncp"
= "test_options_parse"
@@ -739,6 +741,9 @@
# for compat with IDEs like Clion that ignore the t= ests properties
# for the environment variable src= dir when running tests as fallback
target_compile_= definitions(${test_name} PRIVATE "UNIT_TEST_SOURCEDIR=3D\"${_UT_S= OURCE_DIR}\"")
+ if (UT_ALLOW_BIG_ALLOC)
+ target_compile_definitions(${test_name} PRIVAT= E UNIT_TEST_ALLOW_BIG_ALLOC)
+ endif ()

if (NO= T ${test_name} STREQUAL "test_buffer")
= target_sources(${test_name} PRIVATE
@@ -800,6 +805,12 @@=
src/openvpn/xkey_provider.c
= )

+ target_sources(test_mbuf PRIVATE
+ tests/unit_tests/openvpn/mock_get_random.c

+ src/openvpn/buf= fer.c
+ src/op= envpn/mbuf.c
+ = )
+
= target_sources(test_misc PRIVATE
tests/unit_t= ests/openvpn/mock_get_random.c
src/openvpn/options= _util.c
diff --git a/src/openvpn/mbuf.c b/src/openvpn/mbuf.= c
index 448124c..42858ec 100644
--- a/src/o= penvpn/mbuf.c
+++ b/src/openvpn/mbuf.c
@@ -= 42,6 +42,8 @@
struct mbuf_set *
mbuf_init= (unsigned int size)
{
+ ASSERT(size <=3D MBUF_SIZE_MAX);
+
struct mbuf_se= t *ret;
ALLOC_OBJ_CLEAR(ret, struct mbuf_set);<= br> ret->capacity =3D adjust_power_of_2(size);
@@ -121,6 +123,7 @@
bool ret =3D false;
if (ms)
{
+ ASSERT(item);
while (ms= ->len)
{
*item =3D= ms->array[ms->head];
diff --git a/src/openvpn/mbuf.h= b/src/openvpn/mbuf.h
index 7f8c1b7..f741e21 100644<= br>--- a/src/openvpn/mbuf.h
+++ b/src/openvpn/mbuf.h<= /span>
@@ -36,6 +36,8 @@
struct multi_instance;

#define MBUF_INDEX(head, offset, size) ((= (head) + (offset)) & ((size) - 1))
+/* limited by adjust_power_of_2 and array_mult_safe */
+#define MBUF_SIZE_MAX= (ALLOC_SIZE_MAX / sizeof(struct mbuf_item))

struct mbuf_buffer
{
diff --git a/src/openvpn/options.c b/src/openvpn/options.c

index d01ec47..24c3e92 100644

--- a/src/openvpn/options.c<= /span>
+++ b/src/openvpn/options.c
@@ -62,6 +62,7 = @@
#include "options_util.h"
#i= nclude "tun_afunix.h"
#include "domain_help= er.h"
+#include = "mbuf.h"

#include <ctype.h&= gt;

@@ -7552,7 +7553,7 @@
else if (streq(p[0], "bcast-buffers") && p[1] &= & !p[2])

{
VERIFY_PERMISS= ION(OPT_P_GENERAL);
- = atoi_constrained(p[1], &options->n_bcast_buf, p[0], 1, INT_MAX= , msglevel);
+ = atoi_constrained(p[1], &options->n_bcast_buf, p[0], 1, MBUF_SIZE_MA= X, msglevel);
}
else if (streq(p[= 0], "tcp-queue-limit") && p[1] && !p[2]) {
diff --git a/tests/unit_tests/openvpn/Makefi= le.am b/tests/unit_tests/openvpn/Makefile.am
index 0f13172.= .7aeea47 100644
--- a/tests/unit_tests/openvpn/Makefile.am<= /span>
+++ b/tests/unit_tests/openvpn/Makefile.am
= @@ -6,9 +6,22 @@

AM_TESTS_ENVIRONMENT = =3D export LSAN_OPTIONS=3Dsuppressions=3D$(srcdir)/input/leak_suppr.txt;

-test_b= inaries =3D argv_testdriver buffer_testdriver crypto_testdriver dhcp_testdr= iver packet_id_testdriver auth_token_testdriver \
- ncp_testdriver misc_testdriver options_parse_t= estdriver pkt_testdriver ssl_testdriver \
- user_pass_testdriver push_update_msg_testdriver provid= er_testdriver socket_testdriver
+test_binaries =3D argv_testdriver \
+ auth_token_testdriver \
+ buffer_testdriver \
+ crypto_testdriver \
+ dhcp_testdriver \
+ ncp_testdriver \
+ mbuf_testdriver \
+ misc_testdriver \
+ options_parse_testdriver \
+ packet_id_testdriver \
+ pkt_testdriver \
<= span style=3D"color: hsl(120, 100%, 40%);">+ provider_testdriver \
+ push_update_msg_testdriver = \
+ socket_testdriver= \
+ ssl_testdriver \=
+ user_pass_testdriv= er

if HAVE_LD_WRAP_SUPPORT
if !WIN32
@@ -327,6 +340,20 @@
$(top_= srcdir)/src/compat/compat-strsep.c \
$(top_srcdir)/src/op= envpn/ssl_util.c

+mbuf_testdriver_CFLAGS =3D \
+ -I$(top_srcdir)/include -I$(top_srcdir)/src/compa= t -I$(top_srcdir)/src/openvpn \
+ -DSOURCEDIR=3D\"$(top_srcdir)\" @TEST_CFLAGS@=
+
+mbuf_testdriver_LDFLAGS =3D @TEST_LDFLAGS@
+
+mbuf_testdriver_SOURCES =3D test_mbuf.c \=
+ mock_msg.c test_co= mmon.h \
+ mock_get_= random.c \
+ $(top_sr= cdir)/src/openvpn/buffer.c \
+ $(top_srcdir)/src/openvpn/win32-util.c \
+ $(top_srcdir)/src/openvpn/platform.c \
+ $(top_srcdir)/src/openv= pn/mbuf.c
+ misc_testdriver_CFLAGS =3D \
-I$(top_srcdir)/inc= lude -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \
-DSOURCEDIR=3D\"$(top_srcdir)\" @TEST_CFLAGS@

diff --git a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/op= envpn/test_buffer.c

index 25a8def..8855e5b 100644--- a/tests/unit_tests/openvpn/test_buffer.c
+++ b/t= ests/unit_tests/openvpn/test_buffer.c
@@ -386,7 +386,7 @@

/* Instead of trying to trick the com= piler here, disable the warnings
* for this unit test= . We know that the results will be truncated
- * and we want to test that. Not we need the cla= ng as clang-cl (msvc) does
+ * and we want to test that. Note we need the clang as clang-cl = (msvc) does
* not define __GNUC__ like it does under = UNIX(-like) platforms */
#if defined(__GNUC__) || defined(= __clang__)
/* some clang version do not understand -Wforma= t-truncation, so ignore the
diff --git a/tests/unit_tests/o= penvpn/test_mbuf.c b/tests/unit_tests/openvpn/test_mbuf.c
n= ew file mode 100644
index 0000000..cba4da7
= --- /dev/null
+++ b/tests/unit_tests/openvpn/test_mbuf.c
@@ -0,0 +1,160 @@
+/*
+ * O= penVPN -- An application to securely tunnel IP networks
+ * over a single UDP port, = with support for SSL/TLS-based
+ * session authentication and key exchange,+ * packet encryp= tion, packet authentication, and
+ * packet compression.
+ *
+ * Copyright (C) 2025 OpenVPN Inc. <sales@...2722...>

+ *
+ * This program is free software; you c= an redistribute it and/or modify
+ * it under the terms of the GNU General Public License versi= on 2
+ * as publishe= d by the Free Software Foundation.
+ *
+ *= This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY;= without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. = See the
+ * GNU Gen= eral Public License for more details.
+ *
= + * You should have received a copy of the GNU General Public License alon= g
+ * with this prog= ram; if not, see <https://www.gnu.org/licenses/>.
+ */
+
= +#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+<= /span>
+#include "syshe= ad.h"
++#include <setjmp.h>
+#include <cmocka.h&= gt;
+
+#include "buffer.h"
+#include "multi.h&qu= ot;
+#include "m= buf.h"
+#include= "test_common.h"
+
+static void<= /span>
+test_mbuf_init(void = **state)
+{+ struct mbuf_set *ms =3D m= buf_init(256);
+ a= ssert_int_equal(ms->capacity, 256);
+ assert_false(mbuf_defined(ms));
+ assert_non_null(ms->array);

+ mbuf_free(ms);=
+
+ ms =3D mbuf_init(257);
+ assert_int_equal(ms->capacity, = 512);
+ mbuf_free(= ms);
+
+#ifdef UNIT_TEST_ALLOW_BIG_ALLOC /= * allocates up to 2GB of memory */
+ ms =3D mbuf_init(MBUF_SIZE_MAX);
+ assert_int_equal(ms->capacity, MBU= F_SIZE_MAX);
+ mbu= f_free(ms);
+<= br>+/* NOTE: expect_assert_fail= ure does not seem to work with MSVC */
+#ifndef _MSC_VER
+ expect_assert_failure(mbuf_init(MBUF_SIZE_MAX + 1));
+#endif
+#endif

+}
+
+static voi= d
+test_mbuf_add_remo= ve(void **state)
+{
+ struct mbuf_set *= ms =3D mbuf_init(4);
= + assert_int_equal(ms->capacity, 4);
+ assert_false(mbuf_defined(ms));
+ assert_non_null(ms->array);
+
+ /* instances */
+ struct multi_instance mi =3D { 0 };<= /span>
+ struct multi_ins= tance mi2 =3D { 0 };
= + /* buffers */
+ = struct buffer buf =3D alloc_buf(16);
+ struct mbuf_buffer *mbuf_buf =3D mbuf_alloc_buf(&am= p;buf);
+ assert_i= nt_equal(mbuf_buf->refcount, 1);
+ struct mbuf_buffer *mbuf_buf2 =3D mbuf_alloc_buf(&b= uf);
+ assert_int_= equal(mbuf_buf2->refcount, 1);
+ free_buf(&buf);
+ /* items */
+ struct mbuf_item mb_item =3D { .buffer =3D mbuf_buf, .ins= tance =3D &mi };
= + struct mbuf_item mb_item2 =3D { .buffer =3D mbuf_buf2, .instance =3D &= amp;mi2 };
++ mbuf_add_item(ms, &m= b_item);
+ assert_= int_equal(mbuf_buf->refcount, 2);
+ assert_int_equal(mbuf_buf2->refcount, 1);+ assert_int_equal(mbuf_len= (ms), 1);
+ assert= _int_equal(mbuf_maximum_queued(ms), 1);
+ assert_int_equal(ms->head, 0);
+ assert_ptr_equal(mbuf_peek(ms), &= amp;mi);
+
= + mbuf_add_item(ms, &mb_= item2);
+ assert_i= nt_equal(mbuf_buf->refcount, 2);
+ assert_int_equal(mbuf_buf2->refcount, 2);
= + assert_int_equal(mbuf_len(= ms), 2);
+ assert_= int_equal(mbuf_maximum_queued(ms), 2);
+ assert_int_equal(ms->head, 0);
+ assert_ptr_equal(mbuf_peek(ms), &a= mp;mi);
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ mbuf_add_item(ms, &mb_i= tem2);

+ assert_in= t_equal(mbuf_buf->refcount, 2);
+ assert_int_equal(mbuf_buf2->refcount, 3);
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_int_equal(mbuf_len(m= s), 3);

+ assert_i= nt_equal(mbuf_maximum_queued(ms), 3);
+ assert_int_equal(ms->head, 0);
+ assert_ptr_equal(mbuf_peek(ms), &am= p;mi);
+
+ mbuf_add_item(ms, &mb_it= em2);

+ mbuf_add_i= tem(ms, &mb_item2); /* overflow, first item gets removed */
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_int_equal(mbuf_buf-&= gt;refcount, 1);
+ = assert_int_equal(mbuf_buf2->refcount, 5);
+ assert_int_equal(mbuf_len(ms), 4);
= + assert_int_equal(mbuf_maxi= mum_queued(ms), 4);
+= assert_int_equal(ms->head, 1);
+ assert_ptr_equal(mbuf_peek(ms), &mi2);
= +
+ mbuf_add_item(ms, &mb_item);
+ assert_int_equal(mbuf_buf-&g= t;refcount, 2);
+ = assert_int_equal(mbuf_buf2->refcount, 4);
+ assert_int_equal(mbuf_len(ms), 4);
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_int_equal(mbuf_maxim= um_queued(ms), 4);
+ = assert_int_equal(ms->head, 2);
+ assert_ptr_equal(mbuf_peek(ms), &mi2);
<= span style=3D"color: hsl(120, 100%, 40%);">+
+ struct mbuf_item out_item;
+ assert_true(mbuf_extract_item(ms, &= amp;out_item));
+ = assert_ptr_equal(out_item.instance, mb_item2.instance);
+ assert_int_equal(mbuf_buf->refco= unt, 2);
+ assert_= int_equal(mbuf_buf2->refcount, 4);
+ assert_int_equal(mbuf_len(ms), 3);
+ assert_int_equal(mbuf_maximum_queu= ed(ms), 4);
+ asse= rt_int_equal(ms->head, 3);
+ assert_ptr_equal(mbuf_peek(ms), &mi2);
+ mbuf_free_buf(out_item.buffer);

+
+ mbuf_dereference_instance(ms, &mi= 2);
+ assert_int_e= qual(mbuf_buf->refcount, 2);
+ assert_int_equal(mbuf_buf2->refcount, 1);
+ assert_int_equal(mbuf_len(ms),= 3);
+ assert_int_= equal(mbuf_maximum_queued(ms), 4);
+ assert_int_equal(ms->head, 3);
+ assert_ptr_equal(mbuf_peek(ms), &= mi);
+
+ mbuf_free(ms);
+ assert_int_equal(mbuf_buf->r= efcount, 1);
+ mbu= f_free_buf(mbuf_buf);
+ assert_int_equal(mbuf_buf2->refcount, 1);
+ mbuf_free_buf(mbuf_buf2);
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] =3D= {
+ cmocka_un= it_test(test_mbuf_init),
+ cmocka_unit_test(test_mbuf_add_remove),
+ };
+
+ return cmocka_run_group_tests_name("mbuf", tests, NULL, NUL= L);
+}

To view, visit change 1432. To unsubscribe, or for help w= riting mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Icab68a5fd1b6288955f0073179f= 1ddde1468d951
Gerrit-Change-Number: 1432
Gerrit-PatchSet: 4
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>
= --EOoGleg+3z0=--