Change in openvpn[master]: mbuf: Add unit tests
A message that is not in the archive (sent off-list or not yet received): gerrit.1765383841000.Icab68a5fd1b6288955f0073179f1ddde1468d951@...2715...
Attention is currently required from: plaisthos.
Hello plaisthos,
I'd like you to do a code review.
Please visit
http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email
to review the following change.
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.
---
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
A tests/unit_tests/openvpn/test_mbuf.c
6 files changed, 200 insertions(+), 4 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/32/1432/1
CMakeLists.txt +7 -0
@@ -649,6 +649,7 @@ "test_buffer" "test_crypto" "test_dhcp" + "test_mbuf" "test_misc" "test_ncp" "test_options_parse"
@@ -800,6 +801,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
src/openvpn/mbuf.c +3 -0
@@ -42,6 +42,8 @@struct mbuf_set *mbuf_init (unsigned int size) { +ASSERT (size <=MBUF_MAX_SIZE ); +struct mbuf_set *ret; ALLOC_OBJ_CLEAR(ret, struct mbuf_set); ret->capacity =adjust_power_of_2 (size);
@@ -121,6 +123,7 @@bool ret = false;if (ms) { +ASSERT (item);while (ms->len) { *item = ms->array[ms->head];
src/openvpn/mbuf.h +3 -0
@@ -36,6 +36,9 @@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_MAX_POW2 (1u << ((sizeof(unsigned int) * 8) - 1)) +#define MBUF_MAX_SIZE (MBUF_MAX_POW2 / sizeof(struct mbuf_item))struct mbuf_buffer {
src/openvpn/options.c +2 -1
@@ -62,6 +62,7 @@#include "options_util.h" #include "tun_afunix.h" #include "domain_helper.h" +#include "mbuf.h" #include <ctype.h>
@@ -7555,7 +7556,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 , msglevel); +atoi_constrained (p[1 ], &options->n_bcast_buf, p[0 ],1 ,MBUF_MAX_SIZE , msglevel); }else if (streq (p[0 ],"tcp-queue-limit" ) && p[1 ] && !p[2 ]) {
tests/unit_tests/openvpn/Makefile.am +30 -3
@@ -6,9 +6,22 @@ AM_TESTS_ENVIRONMENT = export LSAN_OPTIONS=suppressions=$(srcdir)/input/leak_suppr.txt; -test_binaries = argv_testdriver buffer_testdriver crypto_testdriver dhcp_testdriver packet_id_testdriver auth_token_testdriver \ - ncp_testdriver misc_testdriver options_parse_testdriver pkt_testdriver ssl_testdriver \ - user_pass_testdriver push_update_msg_testdriver provider_testdriver socket_testdriver +test_binaries = 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 +mbuf_testdriver_CFLAGS = \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ + -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@ + +mbuf_testdriver_LDFLAGS = @TEST_LDFLAGS@ + +mbuf_testdriver_SOURCES = 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 = \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@
tests/unit_tests/openvpn/test_mbuf.c new file +155 -0
155 lines not on this page · raw message
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Icab68a5fd1b6288955f0073179f1ddde1468d951
Gerrit-Change-Number: 1432
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Attention is currently required from: plaisthos.
Hello plaisthos,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email
to look at the new patch set (#2).
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
---
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, 210 insertions(+), 5 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/32/1432/2
CMakeLists.txt +11 -0
@@ -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 fallback target_compile_definitions(${test_name} PRIVATE "UNIT_TEST_SOURCEDIR=\"${_UT_SOURCE_DIR}\"") + if (UT_ALLOW_BIG_ALLOC) + target_compile_definitions(${test_name} PRIVATE UNIT_TEST_ALLOW_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
src/openvpn/mbuf.c +3 -0
@@ -42,6 +42,8 @@struct mbuf_set *mbuf_init (unsigned int size) { +ASSERT (size <=MBUF_MAX_SIZE ); +struct mbuf_set *ret; ALLOC_OBJ_CLEAR(ret, struct mbuf_set); ret->capacity =adjust_power_of_2 (size);
@@ -121,6 +123,7 @@bool ret = false;if (ms) { +ASSERT (item);while (ms->len) { *item = ms->array[ms->head];
src/openvpn/mbuf.h +3 -0
@@ -36,6 +36,9 @@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_MAX_POW2 (1u << ((sizeof(unsigned int) * 8) - 1)) +#define MBUF_MAX_SIZE (MBUF_MAX_POW2 / sizeof(struct mbuf_item))struct mbuf_buffer {
src/openvpn/options.c +2 -1
@@ -62,6 +62,7 @@#include "options_util.h" #include "tun_afunix.h" #include "domain_helper.h" +#include "mbuf.h" #include <ctype.h>
@@ -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 , msglevel); +atoi_constrained (p[1 ], &options->n_bcast_buf, p[0 ],1 ,MBUF_MAX_SIZE , msglevel); }else if (streq (p[0 ],"tcp-queue-limit" ) && p[1 ] && !p[2 ]) {
tests/unit_tests/openvpn/Makefile.am +30 -3
@@ -6,9 +6,22 @@ AM_TESTS_ENVIRONMENT = export LSAN_OPTIONS=suppressions=$(srcdir)/input/leak_suppr.txt; -test_binaries = argv_testdriver buffer_testdriver crypto_testdriver dhcp_testdriver packet_id_testdriver auth_token_testdriver \ - ncp_testdriver misc_testdriver options_parse_testdriver pkt_testdriver ssl_testdriver \ - user_pass_testdriver push_update_msg_testdriver provider_testdriver socket_testdriver +test_binaries = 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 +mbuf_testdriver_CFLAGS = \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ + -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@ + +mbuf_testdriver_LDFLAGS = @TEST_LDFLAGS@ + +mbuf_testdriver_SOURCES = 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 = \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@
tests/unit_tests/openvpn/test_buffer.c +1 -1
@@ -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
tests/unit_tests/openvpn/test_mbuf.c new file +160 -0
160 lines not on this page · raw message
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Icab68a5fd1b6288955f0073179f1ddde1468d951
Gerrit-Change-Number: 1432
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Attention is currently required from: plaisthos.
Hello plaisthos,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email
to look at the new patch set (#3).
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
---
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(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/32/1432/3
CMakeLists.txt +11 -0
@@ -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 fallback target_compile_definitions(${test_name} PRIVATE "UNIT_TEST_SOURCEDIR=\"${_UT_SOURCE_DIR}\"") + if (UT_ALLOW_BIG_ALLOC) + target_compile_definitions(${test_name} PRIVATE UNIT_TEST_ALLOW_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
src/openvpn/mbuf.c +3 -0
@@ -42,6 +42,8 @@struct mbuf_set *mbuf_init (unsigned int size) { +ASSERT (size <=MBUF_SIZE_MAX ); +struct mbuf_set *ret; ALLOC_OBJ_CLEAR(ret, struct mbuf_set); ret->capacity =adjust_power_of_2 (size);
@@ -121,6 +123,7 @@bool ret = false;if (ms) { +ASSERT (item);while (ms->len) { *item = ms->array[ms->head];
src/openvpn/mbuf.h +2 -0
@@ -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 {
src/openvpn/options.c +2 -1
@@ -62,6 +62,7 @@#include "options_util.h" #include "tun_afunix.h" #include "domain_helper.h" +#include "mbuf.h" #include <ctype.h>
@@ -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 , msglevel); +atoi_constrained (p[1 ], &options->n_bcast_buf, p[0 ],1 ,MBUF_SIZE_MAX , msglevel); }else if (streq (p[0 ],"tcp-queue-limit" ) && p[1 ] && !p[2 ]) {
tests/unit_tests/openvpn/Makefile.am +30 -3
@@ -6,9 +6,22 @@ AM_TESTS_ENVIRONMENT = export LSAN_OPTIONS=suppressions=$(srcdir)/input/leak_suppr.txt; -test_binaries = argv_testdriver buffer_testdriver crypto_testdriver dhcp_testdriver packet_id_testdriver auth_token_testdriver \ - ncp_testdriver misc_testdriver options_parse_testdriver pkt_testdriver ssl_testdriver \ - user_pass_testdriver push_update_msg_testdriver provider_testdriver socket_testdriver +test_binaries = 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 +mbuf_testdriver_CFLAGS = \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ + -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@ + +mbuf_testdriver_LDFLAGS = @TEST_LDFLAGS@ + +mbuf_testdriver_SOURCES = 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 = \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@
tests/unit_tests/openvpn/test_buffer.c +1 -1
@@ -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
tests/unit_tests/openvpn/test_mbuf.c new file +160 -0
160 lines not on this page · raw message
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Icab68a5fd1b6288955f0073179f1ddde1468d951
Gerrit-Change-Number: 1432
Gerrit-PatchSet: 3
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Attention is currently required from: flichtenheld, plaisthos.
cron2 has posted comments on this change by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email )
Change subject: mbuf: Add unit tests
......................................................................
Patch Set 3: Code-Review+2
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Icab68a5fd1b6288955f0073179f1ddde1468d951
Gerrit-Change-Number: 1432
Gerrit-PatchSet: 3
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Fri, 12 Dec 2025 12:03:36 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
PATCH v3 1/1 mbuf: Add unit tests
In reply to a message not in the archiveFrom: Frank Lichtenheld <[email protected]...>
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
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1432
---
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/+/1432
This mail reflects revision 3 of this Change.
Acked-by according to Gerrit (reflected above):
Gert Doering <[email protected]...>
CMakeLists.txt +11 -0
@@ -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 fallback target_compile_definitions(${test_name} PRIVATE "UNIT_TEST_SOURCEDIR=\"${_UT_SOURCE_DIR}\"") + if (UT_ALLOW_BIG_ALLOC) + target_compile_definitions(${test_name} PRIVATE UNIT_TEST_ALLOW_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
src/openvpn/mbuf.c +3 -0
@@ -42,6 +42,8 @@struct mbuf_set *mbuf_init (unsigned int size) { +ASSERT (size <=MBUF_SIZE_MAX ); +struct mbuf_set *ret; ALLOC_OBJ_CLEAR(ret, struct mbuf_set); ret->capacity =adjust_power_of_2 (size);
@@ -121,6 +123,7 @@bool ret = false;if (ms) { +ASSERT (item);while (ms->len) { *item = ms->array[ms->head];
src/openvpn/mbuf.h +2 -0
@@ -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 {
src/openvpn/options.c +2 -1
@@ -62,6 +62,7 @@#include "options_util.h" #include "tun_afunix.h" #include "domain_helper.h" +#include "mbuf.h" #include <ctype.h>
@@ -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 , msglevel); +atoi_constrained (p[1 ], &options->n_bcast_buf, p[0 ],1 ,MBUF_SIZE_MAX , msglevel); }else if (streq (p[0 ],"tcp-queue-limit" ) && p[1 ] && !p[2 ]) {
tests/unit_tests/openvpn/Makefile.am +30 -3
@@ -6,9 +6,22 @@ AM_TESTS_ENVIRONMENT = export LSAN_OPTIONS=suppressions=$(srcdir)/input/leak_suppr.txt; -test_binaries = argv_testdriver buffer_testdriver crypto_testdriver dhcp_testdriver packet_id_testdriver auth_token_testdriver \ - ncp_testdriver misc_testdriver options_parse_testdriver pkt_testdriver ssl_testdriver \ - user_pass_testdriver push_update_msg_testdriver provider_testdriver socket_testdriver +test_binaries = 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 +mbuf_testdriver_CFLAGS = \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ + -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@ + +mbuf_testdriver_LDFLAGS = @TEST_LDFLAGS@ + +mbuf_testdriver_SOURCES = 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 = \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@
tests/unit_tests/openvpn/test_buffer.c +1 -1
@@ -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
tests/unit_tests/openvpn/test_mbuf.c new file +160 -0
160 lines not on this page · raw message
[Openvpn-devel] [PATCH applied] Re: mbuf: Add unit tests
Additional unit tests are good, especially when we have no real test coverage for this important data structure yet.
The approach of having memory-intensive tests be disabled by default (and be disabled by the standard unix build) is also useful - smaller systems would have spurious failures that scare people, with little gain.
I had some reservations about the ASSERT()s being added - but I have come to the conclusion that they are fine. One is needed to test the maximum allocation size, and the other one can never fire in the current source (because both callers pass in a pointer to a local variable).
A quick local (autoconf) test passes, BB is also all green.
Your patch has been applied to the master branch.
commit 75cc34eccca990a2efbaa02639311def6b2cd70f
Author: Frank Lichtenheld Date: Fri Dec 12 13:03:46 2025 +0100
mbuf: Add unit tests
Signed-off-by: Frank Lichtenheld <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1432
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35050.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
cron2 has uploaded a new patch set (#4) to the change originally created by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email )
The following approvals got outdated and were removed:
Code-Review+2 by cron2
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
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1432
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35050.html
---
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(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/32/1432/4
CMakeLists.txt +11 -0
@@ -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 fallback target_compile_definitions(${test_name} PRIVATE "UNIT_TEST_SOURCEDIR=\"${_UT_SOURCE_DIR}\"") + if (UT_ALLOW_BIG_ALLOC) + target_compile_definitions(${test_name} PRIVATE UNIT_TEST_ALLOW_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
src/openvpn/mbuf.c +3 -0
@@ -42,6 +42,8 @@struct mbuf_set *mbuf_init (unsigned int size) { +ASSERT (size <=MBUF_SIZE_MAX ); +struct mbuf_set *ret; ALLOC_OBJ_CLEAR(ret, struct mbuf_set); ret->capacity =adjust_power_of_2 (size);
@@ -121,6 +123,7 @@bool ret = false;if (ms) { +ASSERT (item);while (ms->len) { *item = ms->array[ms->head];
src/openvpn/mbuf.h +2 -0
@@ -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 {
src/openvpn/options.c +2 -1
@@ -62,6 +62,7 @@#include "options_util.h" #include "tun_afunix.h" #include "domain_helper.h" +#include "mbuf.h" #include <ctype.h>
@@ -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 , msglevel); +atoi_constrained (p[1 ], &options->n_bcast_buf, p[0 ],1 ,MBUF_SIZE_MAX , msglevel); }else if (streq (p[0 ],"tcp-queue-limit" ) && p[1 ] && !p[2 ]) {
tests/unit_tests/openvpn/Makefile.am +30 -3
@@ -6,9 +6,22 @@ AM_TESTS_ENVIRONMENT = export LSAN_OPTIONS=suppressions=$(srcdir)/input/leak_suppr.txt; -test_binaries = argv_testdriver buffer_testdriver crypto_testdriver dhcp_testdriver packet_id_testdriver auth_token_testdriver \ - ncp_testdriver misc_testdriver options_parse_testdriver pkt_testdriver ssl_testdriver \ - user_pass_testdriver push_update_msg_testdriver provider_testdriver socket_testdriver +test_binaries = 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 +mbuf_testdriver_CFLAGS = \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ + -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@ + +mbuf_testdriver_LDFLAGS = @TEST_LDFLAGS@ + +mbuf_testdriver_SOURCES = 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 = \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@
tests/unit_tests/openvpn/test_buffer.c +1 -1
@@ -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
tests/unit_tests/openvpn/test_mbuf.c new file +160 -0
160 lines not on this page · raw message
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Icab68a5fd1b6288955f0073179f1ddde1468d951
Gerrit-Change-Number: 1432
Gerrit-PatchSet: 4
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email )
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
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1432
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35050.html
---
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(-)
CMakeLists.txt +11 -0
@@ -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 fallback target_compile_definitions(${test_name} PRIVATE "UNIT_TEST_SOURCEDIR=\"${_UT_SOURCE_DIR}\"") + if (UT_ALLOW_BIG_ALLOC) + target_compile_definitions(${test_name} PRIVATE UNIT_TEST_ALLOW_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
src/openvpn/mbuf.c +3 -0
@@ -42,6 +42,8 @@struct mbuf_set *mbuf_init (unsigned int size) { +ASSERT (size <=MBUF_SIZE_MAX ); +struct mbuf_set *ret; ALLOC_OBJ_CLEAR(ret, struct mbuf_set); ret->capacity =adjust_power_of_2 (size);
@@ -121,6 +123,7 @@bool ret = false;if (ms) { +ASSERT (item);while (ms->len) { *item = ms->array[ms->head];
src/openvpn/mbuf.h +2 -0
@@ -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 {
src/openvpn/options.c +2 -1
@@ -62,6 +62,7 @@#include "options_util.h" #include "tun_afunix.h" #include "domain_helper.h" +#include "mbuf.h" #include <ctype.h>
@@ -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 , msglevel); +atoi_constrained (p[1 ], &options->n_bcast_buf, p[0 ],1 ,MBUF_SIZE_MAX , msglevel); }else if (streq (p[0 ],"tcp-queue-limit" ) && p[1 ] && !p[2 ]) {
tests/unit_tests/openvpn/Makefile.am +30 -3
@@ -6,9 +6,22 @@ AM_TESTS_ENVIRONMENT = export LSAN_OPTIONS=suppressions=$(srcdir)/input/leak_suppr.txt; -test_binaries = argv_testdriver buffer_testdriver crypto_testdriver dhcp_testdriver packet_id_testdriver auth_token_testdriver \ - ncp_testdriver misc_testdriver options_parse_testdriver pkt_testdriver ssl_testdriver \ - user_pass_testdriver push_update_msg_testdriver provider_testdriver socket_testdriver +test_binaries = 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 +mbuf_testdriver_CFLAGS = \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ + -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@ + +mbuf_testdriver_LDFLAGS = @TEST_LDFLAGS@ + +mbuf_testdriver_SOURCES = 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 = \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@
tests/unit_tests/openvpn/test_buffer.c +1 -1
@@ -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
tests/unit_tests/openvpn/test_mbuf.c new file +160 -0
160 lines not on this page · raw message
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1432?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Icab68a5fd1b6288955f0073179f1ddde1468d951
Gerrit-Change-Number: 1432
Gerrit-PatchSet: 4
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>