From: "flichtenheld (Code Review)"
Date: Fri, 29 Aug 2025 13:43:46 +0000
In-Reply-To:
References:
Reply-To: frank@...2641..., arne-openvpn@...1227...,
openvpn-devel@lists.sourceforge.net
Message-ID: <692bcd9686b5ba673b7b3fb912aaacce12f1baee-EmailReplacePatchSet-HTML@...2715...>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Disposition: inline
Content-Type: multipart/alternative; boundary="p+qmkd9Nzn4="; charset=UTF-8
Subject: [Openvpn-devel] [M] Change in openvpn[master]: Add new unit test
module test_socket
List-Id:
List-Unsubscribe: ,
List-Archive:
List-Post:
List-Help:
List-Subscribe: ,
To: plaisthos
Cc: openvpn-devel
--p+qmkd9Nzn4=
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Attention is currently required from: plaisthos.
Hello plaisthos,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/1160?usp=3Demail
to look at the new patch set (#3).
Change subject: Add new unit test module test_socket
......................................................................
Add new unit test module test_socket
With a first UT that tests add_in6_addr() (and
print_in6_addr implicitly).
Change-Id: If546f64a4554b292623bfcfe9ee53bac17dfa803
Signed-off-by: Frank Lichtenheld
---
M CMakeLists.txt
M tests/unit_tests/openvpn/Makefile.am
M tests/unit_tests/openvpn/mock_management.c
A tests/unit_tests/openvpn/test_socket.c
4 files changed, 157 insertions(+), 10 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/60/1160/3
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 45044af..6ca08fb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -653,6 +653,7 @@
"test_packet_id"
"test_pkt"
"test_provider"
+ "test_socket"
"test_ssl"
"test_user_pass"
"test_push_update_msg"
@@ -849,6 +850,15 @@
src/openvpn/base64.c
)
+ target_sources(test_socket PRIVATE
+ tests/unit_tests/openvpn/mock_get_random.c
+ tests/unit_tests/openvpn/mock_management.c
+ tests/unit_tests/openvpn/mock_win32_execve.c
+ src/openvpn/env_set.c
+ src/openvpn/run_command.c
+ src/openvpn/socket_util.c
+ )
+
target_sources(test_user_pass PRIVATE
tests/unit_tests/openvpn/mock_get_random.c
tests/unit_tests/openvpn/mock_win32_execve.c
diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvp=
n/Makefile.am
index b24e03c..d030185 100644
--- a/tests/unit_tests/openvpn/Makefile.am
+++ b/tests/unit_tests/openvpn/Makefile.am
@@ -4,23 +4,17 @@
AM_TESTSUITE_SUMMARY_HEADER =3D ' for $(PACKAGE_STRING) Unit-Tests'
-test_binaries=3D
+test_binaries =3D crypto_testdriver packet_id_testdriver auth_token_testdr=
iver \
+ ncp_testdriver misc_testdriver pkt_testdriver ssl_testdriver \
+ user_pass_testdriver push_update_msg_testdriver provider_testdriver socke=
t_testdriver
if HAVE_LD_WRAP_SUPPORT
test_binaries +=3D argv_testdriver buffer_testdriver
-endif
-
-test_binaries +=3D crypto_testdriver packet_id_testdriver auth_token_testd=
river ncp_testdriver misc_testdriver \
- pkt_testdriver ssl_testdriver user_pass_testdriver push_update_msg_testdr=
iver
-
-if HAVE_LD_WRAP_SUPPORT
if !WIN32
test_binaries +=3D tls_crypt_testdriver
endif
endif
-test_binaries +=3D provider_testdriver
-
if WIN32
test_binaries +=3D cryptoapi_testdriver
LDADD =3D -lws2_32
@@ -343,4 +337,22 @@
$(top_srcdir)/src/openvpn/platform.c \
$(top_srcdir)/src/openvpn/push_util.c \
$(top_srcdir)/src/openvpn/options_util.c \
- $(top_srcdir)/src/openvpn/otime.c
\ No newline at end of file
+ $(top_srcdir)/src/openvpn/otime.c
+
+socket_testdriver_CFLAGS =3D \
+ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/op=
envpn \
+ -DSOURCEDIR=3D\"$(top_srcdir)\" @TEST_CFLAGS@
+
+socket_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ $(SOCKETS_LIBS)
+
+socket_testdriver_SOURCES =3D test_socket.c \
+ mock_msg.c test_common.h \
+ mock_get_random.c \
+ mock_management.c \
+ $(top_srcdir)/src/openvpn/buffer.c \
+ $(top_srcdir)/src/openvpn/win32-util.c \
+ $(top_srcdir)/src/openvpn/platform.c \
+ $(top_srcdir)/src/openvpn/env_set.c \
+ $(top_srcdir)/src/openvpn/run_command.c \
+ $(top_srcdir)/src/openvpn/socket_util.c
+
diff --git a/tests/unit_tests/openvpn/mock_management.c b/tests/unit_tests/=
openvpn/mock_management.c
index b24e4c4..28e541c 100644
--- a/tests/unit_tests/openvpn/mock_management.c
+++ b/tests/unit_tests/openvpn/mock_management.c
@@ -46,4 +46,18 @@
{
return NULL;
}
+
+void
+management_set_state(struct management *man, const int state, const char *=
detail,
+ const in_addr_t *tun_local_ip, const struct in6_addr =
*tun_local_ip6,
+ const struct openvpn_sockaddr *local_addr,
+ const struct openvpn_sockaddr *remote_addr)
+{
+}
+
#endif
+
+void
+management_sleep(const int n)
+{
+}
diff --git a/tests/unit_tests/openvpn/test_socket.c b/tests/unit_tests/open=
vpn/test_socket.c
new file mode 100644
index 0000000..a73c53a
--- /dev/null
+++ b/tests/unit_tests/openvpn/test_socket.c
@@ -0,0 +1,111 @@
+/*
+ * 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) 2021-2025 Arne Schwabe
+ *
+ * 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
+#include
+#include
+#include
+
+#include "socket.h"
+#include "win32.h"
+
+/* stubs for some unused functions instead of pulling in too many dependen=
cies */
+struct signal_info siginfo_static; /* GLOBAL */
+
+int
+signal_reset(struct signal_info *si, int signum)
+{
+ assert_true(0);
+ return 0;
+}
+
+#ifdef _WIN32
+struct win32_signal win32_signal; /* GLOBAL */
+
+int
+win32_signal_get(struct win32_signal *ws)
+{
+ assert_true(0);
+ return 0;
+}
+#endif
+
+int
+parse_line(const char *line, char **p, const int n, const char *file, cons=
t int line_num,
+ int msglevel, struct gc_arena *gc)
+{
+ assert_true(0);
+ return 0;
+}
+
+static void
+test_add_in6_addr_tc(const char *orig_str, uint32_t add, const char *expec=
t_str)
+{
+ struct in6_addr orig, result, expected;
+ struct gc_arena gc =3D gc_new();
+ assert_int_equal(inet_pton(AF_INET6, orig_str, &orig), 1);
+ assert_int_equal(inet_pton(AF_INET6, expect_str, &expected), 1);
+ result =3D add_in6_addr(orig, add);
+ const char *result_str =3D print_in6_addr(result, 0, &gc);
+ assert_string_equal(result_str, expect_str);
+ assert_memory_equal(&result, &expected, sizeof(struct in6_addr));
+ gc_free(&gc);
+}
+
+static void
+test_add_in6_addr(void **state)
+{
+ /* Note that some of the result strings need to account for
+ print_in6_addr formatting the addresses as IPv4 */
+ test_add_in6_addr_tc("::", 1, "::1");
+ test_add_in6_addr_tc("::ff", 1, "::100");
+ test_add_in6_addr_tc("::ffff", 1, "::0.1.0.0");
+ test_add_in6_addr_tc("ffff::ffff", 1, "ffff::1:0");
+ test_add_in6_addr_tc("::ffff:ffff", 1, "::1:0:0");
+ test_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 1, "::=
");
+ test_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 2, "::=
1");
+
+ test_add_in6_addr_tc("::", UINT32_MAX, "::255.255.255.255");
+ test_add_in6_addr_tc("::1", UINT32_MAX, "::1:0:0");
+ test_add_in6_addr_tc("::ffff", UINT32_MAX, "::1:0:fffe");
+ test_add_in6_addr_tc("ffff::ffff", UINT32_MAX, "ffff::1:0:fffe");
+ test_add_in6_addr_tc("::ffff:ffff", UINT32_MAX, "::1:ffff:fffe");
+ test_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", UINT32=
_MAX, "::255.255.255.254");
+}
+
+const struct CMUnitTest socket_tests[] =3D {
+ cmocka_unit_test(test_add_in6_addr)
+};
+
+int
+main(void)
+{
+ return cmocka_run_group_tests(socket_tests, NULL, NULL);
+}
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1160?usp=3Demail
To unsubscribe, or for help writing mail filters, visit http://gerrit.openv=
pn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If546f64a4554b292623bfcfe9ee53bac17dfa803
Gerrit-Change-Number: 1160
Gerrit-PatchSet: 3
Gerrit-Owner: flichtenheld
Gerrit-Reviewer: plaisthos
Gerrit-CC: openvpn-devel
Gerrit-Attention: plaisthos
Gerrit-MessageType: newpatchset
--p+qmkd9Nzn4=
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Attention is currently required from: plaisthos.
flichtenheld uploaded patch set #3 to this change.
<=
p>View C=
hange
Add new unit test module test_socket
With a first UT that =
tests add_in6_addr() (and
print_in6_addr implicitly).
Change-Id: =
If546f64a4554b292623bfcfe9ee53bac17dfa803
Signed-off-by: Frank Lichtenhe=
ld <frank@...2641...>
---
M CMakeLists.txt
M tests/unit_test=
s/openvpn/Makefile.am
M tests/unit_tests/openvpn/mock_management.c
A =
tests/unit_tests/openvpn/test_socket.c
4 files changed, 157 insertions(+=
), 10 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvp=
n refs/changes/60/1160/3
diff --git a/CMakeLists.txt b/CMakeLists.t=
xt
index 45044af..6ca08fb 100644
--- a/CMak=
eLists.txt
+++ b/CMakeLists.txt
@@ -653,6 +=
653,7 @@
"test_packet_id"
"test_pkt"
"test_provide=
r"
+ &quo=
t;test_socket"
"test_ssl"
"test_user_pass"
"t=
est_push_update_msg"
@@ -849,6 +850,15 @@
src/openvpn/base64.c
)
+ target_sourc=
es(test_socket PRIVATE
+ tests/unit_tests/openvpn/mock_get_random.c
+ tests/unit_tests/openvpn/mock_man=
agement.c
+ te=
sts/unit_tests/openvpn/mock_win32_execve.c
+ src/openvpn/env_set.c
+ src/openvpn/run_command.c<=
br>+ src/openvpn/socket_=
util.c
+ )
+
t=
arget_sources(test_user_pass PRIVATE
tests/unit_te=
sts/openvpn/mock_get_random.c
tests/unit_tests/ope=
nvpn/mock_win32_execve.c
diff --git a/tests/unit_tests/open=
vpn/Makefile.am b/tests/unit_tests/openvpn/Makefile.am
inde=
x b24e03c..d030185 100644
--- a/tests/unit_tests/openvpn/Ma=
kefile.am
+++ b/tests/unit_tests/openvpn/Makefile.am=
@@ -4,23 +4,17 @@
AM_TESTSUITE=
_SUMMARY_HEADER =3D ' for $(PACKAGE_STRING) Unit-Tests'
<=
span>
-test_binaries=
=3D
+test_binaries =
=3D crypto_testdriver packet_id_testdriver auth_token_testdriver \+ ncp_testdriver misc_testdri=
ver pkt_testdriver ssl_testdriver \
+ user_pass_testdriver push_update_msg_testdriver provider_t=
estdriver socket_testdriver
if HAVE_LD_W=
RAP_SUPPORT
test_binaries +=3D argv_testdriver buffer_test=
driver
-endif
-
-test_binaries +=3D crypto_testdriver packet_id_testd=
river auth_token_testdriver ncp_testdriver misc_testdriver \
- pkt_testdriver ssl_testdriver user_=
pass_testdriver push_update_msg_testdriver
-
-=
if HAVE_LD_WRAP_SUPPORT
if !WIN32
test_bi=
naries +=3D tls_crypt_testdriver
endif
en=
dif
-=
test_binaries +=3D provider_testdriver
-
if WIN32
test_binaries =
+=3D cryptoapi_testdriver
LDADD =3D -lws2_32
@@ -343,4 +337,22 @@
$(top_srcdir)/src/openvpn/platfor=
m.c \
$(top_srcdir)/src/openvpn/push_util.c \
<=
span> $(top_srcdir)/src/openvpn/options_util.c \
- $(top_srcdir)/src/openvpn/otime.c
\ No newline at end of file
+ $(top_srcdir)/src/openvpn/otime.c
+
+socket_testdriver_CFLAGS =3D \
+ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$=
(top_srcdir)/src/openvpn \
+ -DSOURCEDIR=3D\"$(top_srcdir)\" @TEST_CFLAGS@
<=
span style=3D"color: hsl(120, 100%, 40%);">+
+socket_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ $(SOC=
KETS_LIBS)
++socket_testdriver_SOURCES =
=3D test_socket.c \
+=
mock_msg.c test_common.h \
+ mock_get_random.c \
+ mock_management.c \
+ $(top_srcdir)/src/openvpn/buffer.c \
+ $(top_srcdir)/src/openvpn/win32-util.c \
+ $(top_srcdir)/src/openv=
pn/platform.c \
+ $(t=
op_srcdir)/src/openvpn/env_set.c \
+ $(top_srcdir)/src/openvpn/run_command.c \
+ $(top_srcdir)/src/openvpn/socket_uti=
l.c
+
diff --git a/tests/unit_tests/openvpn/mock_management.c b/tests/unit_tests=
/openvpn/mock_management.c
index b24e4c4..28e541c 100644
--- a/tests/unit_tests/openvpn/mock_management.c
+++ b/tests/unit_tests/openvpn/mock_management.c
@@ -46=
,4 +46,18 @@
{
return NULL;
}
+
+void
+management_set_state(struct management *man,=
const int state, const char *detail,
+ const in_addr_t *tun_local_ip, const=
struct in6_addr *tun_local_ip6,
+ const struct openvpn_sockaddr *local_addr=
,
+ =
const struct openvpn_sockaddr *remote_addr)
+{
+}
+ #endif
+
+void
+management_sleep(const int n)+{
+}
diff --git a/tests/unit_test=
s/openvpn/test_socket.c b/tests/unit_tests/openvpn/test_socket.c
=
new file mode 100644
index 0000000..a73c53a
--- /dev/null
+++ b/tests/unit_tests/openvpn/test_so=
cket.c
@@ -0,0 +1,111 @@
+/*
+ * OpenVPN -- An application to securely tunnel IP networks
=
+ * over a single U=
DP port, with support for SSL/TLS-based
+ * session authentication and key exchange,=
+ * pack=
et encryption, packet authentication, and
+ * packet compression.
+ *
+ * Copyright (C) 2021-2025 Arne Schwabe <arne@...1711...=
27...>
+ *<=
br>+ * This program is free so=
ftware; 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 usef=
ul,
+ * but WITHOUT =
ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICU=
LAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public=
License along
+ * w=
ith this program; if not, see <https://www.gnu.org/licenses/>.=
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#inclu=
de "syshead.h"
+
+#include <s=
tdio.h>
+#include =
<stdlib.h>
+#in=
clude <stdarg.h>
+#include <string.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include "socket.h"
+#include "win32.h"
+
+/* stubs for some unused functions instead of pulling in too many depende=
ncies */
+struct sign=
al_info siginfo_static; /* GLOBAL */
+
+in=
t
+signal_reset(struc=
t signal_info *si, int signum)
+{
+ ass=
ert_true(0);
+ ret=
urn 0;
+}
<=
span style=3D"color: hsl(120, 100%, 40%);">+
+#ifdef _WIN32
+struct win32_signal win32_signal; /* GLOBAL */
+
+int
+win32_signal_get(struct win32_signal *ws)
+{
+ assert_true(0);
+ return 0;
+}
+#endif=
span>
+
+int
+parse_line(const char *line, char **p, const int n, cons=
t char *file, const int line_num,
+ int msglevel, struct gc_arena *gc)
+{
+ assert_true(0);
+ return 0;
+}
+=
span>
+static void
+test_add_in6_addr_tc(const ch=
ar *orig_str, uint32_t add, const char *expect_str)
+{
+ struct in6_addr orig, result, expected;
+ struct gc_arena gc =3D gc_new()=
;
+ assert_int_equ=
al(inet_pton(AF_INET6, orig_str, &orig), 1);
+ assert_int_equal(inet_pton(AF_INET6, expec=
t_str, &expected), 1);
+ result =3D add_in6_addr(orig, add);
+ const char *result_str =3D print_in6_addr(re=
sult, 0, &gc);
+ =
assert_string_equal(result_str, expect_str);
+ assert_memory_equal(&result, &expec=
ted, sizeof(struct in6_addr));
+ gc_free(&gc);
+}
+
+static void
+test_add_in6_addr(void **state)<=
/span>
+{
+ /* Note that some of the result st=
rings need to account for
+ print_in6_addr formatting the addresses as IPv4 */
<=
span style=3D"color: hsl(120, 100%, 40%);">+ test_add_in6_addr_tc("=
::", 1, "::1");
+ test_add_in6_addr_tc("::ff", 1, "::100")=
;
+ test_add_in6_a=
ddr_tc("::ffff", 1, "::0.1.0.0");
+ test_add_in6_addr_tc("ffff::ffff=
", 1, "ffff::1:0");
+ test_add_in6_addr_tc("::ffff:ffff", 1, ":=
:1:0:0");
+ t=
est_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 1,=
"::");
+ =
test_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",=
2, "::1");
+
+ test_add_in6_=
addr_tc("::", UINT32_MAX, "::255.255.255.255");<=
br>+ test_add_in6_addr_tc(&q=
uot;::1", UINT32_MAX, "::1:0:0");
+ test_add_in6_addr_tc("::ffff", U=
INT32_MAX, "::1:0:fffe");
+ test_add_in6_addr_tc("ffff::ffff", UINT32_MAX=
, "ffff::1:0:fffe");
+ test_add_in6_addr_tc("::ffff:ffff", UINT32_MAX, &q=
uot;::1:ffff:fffe");
+ test_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff=
f", UINT32_MAX, "::255.255.255.254");
+}
+
+con=
st struct CMUnitTest socket_tests[] =3D {
+ cmocka_unit_test(test_add_in6_addr)
+};
+
+int
+main(void)=
+{
+ return cmocka_run_group_tests(soc=
ket_tests, NULL, NULL);
+}
To view, visit change 1160. To unsubs=
cribe, or for help writing mail filters, visit settings.
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If546f64a4554b292623bfcfe9ee=
53bac17dfa803
Gerrit-Change-Number: 1160
Gerrit-PatchSet: 3
Gerrit-Owner: flichtenheld <frank@...2723...=
.>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...=
.1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...=
ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...=
..1227...>
Gerrit-MessageType: newpatchset
--p+qmkd9Nzn4=--