From: "cron2 (Code Review)" cron2 upl=
oaded patch set #4 to the change originally created by flichtenhel=
d. The following approvals got outdated and were remove=
d:
Code-Review+2 by cron2 git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/44=
/1244/4test_options_parse: Start new UT fo=
r options_parse.c
For now contains one test case for parse_line.
=
Change-Id: I95032d2539d994abf69fc17319ed1a429c3bb948
Signed-off-by: =
Frank Lichtenheld <frank@...2641...>
Acked-by: Gert Doering <ge=
rt@...1296...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/124=
4
Message-Id: <20251008101014.5691-1-gert@...1296...>
URL: http=
s://sourceforge.net/p/openvpn/mailman/message/59243816/
Signed-off-by: G=
ert Doering <gert@...1296...>
---
M CMakeLists.txt
M tests/u=
nit_tests/openvpn/Makefile.am
A tests/unit_tests/openvpn/test_options_pa=
rse.c
3 files changed, 227 insertions(+), 0 deletions(-)diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt=
+++ b/CMakeLists.txt
@@ -685,6 +685,7 @@ # Clang-cl (which is also MSVC) is wrongly detected to support=
wrap
if (NOT MSVC AND "${LD_SUPPORTS_WRAP}"=
)
list(APPEND unit_tests
+ "test_options_parse"
"test_tls_crypt"
=
)
endif ()
@@ -826,6 +827,20 @=
@
src/compat/compat-strsep.c
=
)
+ if (TARGET test_options_parse)
+ target_link_options(test_options_parse PRIVATE=
span>
+ -Wl,--wra=
p=3Dadd_option
+ =
-Wl,--wrap=3Dremove_option
+ -Wl,--wrap=3Dupdate_option
+ -Wl,--wrap=3Dusage
+ )
+ target_sources(test_options_parse PRIVA=
TE
+ tests=
/unit_tests/openvpn/mock_get_random.c
+ src/openvpn/options_parse.c
+ src/openvpn/options_util.=
c
+ )+ endif ()
+
target_source=
s(test_packet_id PRIVATE
tests/unit_tests/openvpn/=
mock_get_random.c
src/openvpn/otime.cdiff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/op=
envpn/Makefile.am
index f7b1bc8..8e94665 100644
<=
span>--- a/tests/unit_tests/openvpn/Makefile.am
+++ b/tests=
/unit_tests/openvpn/Makefile.am
@@ -9,6 +9,7 @@
<=
span> user_pass_testdriver push_update_msg_testdriver provider_testdriver =
socket_testdriver
if HAVE_LD_WRAP_SUPPOR=
T
+test_binaries +=3D=
options_parse_testdriver
if !WIN32
test_=
binaries +=3D tls_crypt_testdriver
endif
@=
@ -190,6 +191,21 @@
$(top_srcdir)/src/openvpn/platform.c<=
/span>
endif
+options_parse_testdriver_CFLAGS =3D -I$(top_srcdir)/s=
rc/openvpn -I$(top_srcdir)/src/compat @TEST_CFLAGS@
+options_parse_testdriver_LDFLAGS =3D @TES=
T_LDFLAGS@ -L$(top_srcdir)/src/openvpn \
+ -Wl,--wrap=3Dadd_option \
+ -Wl,--wrap=3Dupdate_option \
+ -Wl,--wrap=3Dremove_option \<=
br>+ -Wl,--wrap=3Dusage<=
br>+options_parse_testdriver_SO=
URCES =3D test_options_parse.c \
+ mock_msg.c mock_msg.h test_common.h \
+ mock_get_random.c \
+ $(top_srcdir)/src/openvpn/options_parse=
.c \
+ $(top_srcdir)/=
src/openvpn/options_util.c \
+ $(top_srcdir)/src/openvpn/buffer.c \
+ $(top_srcdir)/src/openvpn/win32-util.c \=
+ $(top_srcdir)/src/openvpn=
/platform.c
+<=
br> provider_testdriver_CFLAGS =3D \
-I$(top_srcdi=
r)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \<=
br> @TEST_CFLAGS@ $(OPTIONAL_CRYPTO_CFLAGS)
diff --g=
it a/tests/unit_tests/openvpn/test_options_parse.c b/tests/unit_tests/openv=
pn/test_options_parse.c
new file mode 100644
--- /dev/null
+++ =
b/tests/unit_tests/openvpn/test_options_parse.c
@@ -0,0 +1,=
196 @@
+/*
=
+ * 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 authenticati=
on, and
+ * =
packet compression.
+ *
+ * Copyright =
(C) 2025 OpenVPN Inc <sales@...515...>
+ *
+ * This program is free software; you can redistribute it and/or m=
odify
+ * it under t=
he terms of the GNU General Public License version 2
+ * as published by the Free Software Fou=
ndation.
+ *+ * This program is distribu=
ted in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied =
warranty of
+ * MERC=
HANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for mo=
re details.
+ *
+ * You should have recei=
ved a copy of the GNU General Public License along
+ * with this program; if not, see <https=
://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h&=
quot;
+#endif<=
br>+
+#include "syshead.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <setjmp.h>
+#include <cmocka.h&g=
t;
+
+#include "options.h"
+#include "test_commo=
n.h"
+#include &=
quot;mock_msg.h"
+
+void+__wrap_add_option(struct options=
*options, char *p[], bool is_inline, const char *file,
+ int line, const int l=
evel, const msglvl_t msglevel,
+ const unsigned int permission_mask, unsigned i=
nt *option_types_found,
+ struct env_set *es)
+{
+}
+
+__wrap_remove_option(struct context *c, struct o=
ptions *options, char *p[], bool is_inline,
+ const char *file, int line, co=
nst msglvl_t msglevel,
+ const unsigned int permission_mask, unsigned int *o=
ption_types_found,
+ =
struct env_set *es)
+{
+}
+
+void
+__wrap_update_option(struct context *c, struct opt=
ions *options, char *p[], bool is_inline,
+ const char *file, int line, cons=
t int level, const msglvl_t msglevel,
+ const unsigned int permission_mask, =
unsigned int *option_types_found,
+ struct env_set *es, unsigned int *update=
_options_found)
+{
+}
+
+void
=
+__wrap_usage(void)
+=
{
+}
+
+/* for building long texts */
+#define A_TIMES_256 "AAAAAAAAAAAAAAAAAAA=
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
AAAAAAAAAAAO"
+<=
/span>
+static void+test_parse_line(void **state=
)
+{
+ char *p[MAX_PARMS + 1] =3D { 0 }=
;
+ struct gc_aren=
a gc =3D gc_new();
+ =
int res =3D 0;
+=
span>
+#define PARSE_LINE_TS=
T(string) \=
+ do =
\
<=
span style=3D"color: hsl(120, 100%, 40%);">+ { =
\
+ CLEAR(p); =
\
+ res =3D parse_line(string, p, SIZ=
E(p) - 1, "test_options_parse", 1, M_INFO, &gc); \
=
+ } while (0);
+ /* basic example */
+ PARSE_LINE_TST("some-opt firstparm seco=
nd-parm");
+ =
assert_int_equal(res, 3);
+ assert_string_equal(p[0], "some-opt");
+ assert_string_equal(p[1], "=
firstparm");
+ =
assert_string_equal(p[2], "second-parm");
+ assert_null(p[res]);
+
+ /* basic quoting, -- is not handled special */
+ PARSE_LINE_TST("=
;--some-opt 'first parm' \"second' 'parm\"")=
;
+ assert_int_equ=
al(res, 3);
+ asse=
rt_string_equal(p[0], "--some-opt");
+ assert_string_equal(p[1], "first parm&q=
uot;);
+ assert_st=
ring_equal(p[2], "second' 'parm");
+ assert_null(p[res]);
+
+ /* escaped quotes */
+ PARSE_LINE_TST("\"some opt\" =
39;first\" \"parm' \"second\\\" \\\"parm\"=
;");
+ assert=
_int_equal(res, 3);
+=
assert_string_equal(p[0], "some opt");
+ assert_string_equal(p[1], "first=
\" \"parm");
+ assert_string_equal(p[2], "second\" \"parm"=
);
+ assert_null(p=
[res]);
+
<=
span style=3D"color: hsl(120, 100%, 40%);">+ /* missing closing quote */=
+ PARSE_LINE_TST(=
"--some-opt 'first parm \"second parm\"");+ assert_int_equal(res, 0)=
;
+
+ /* escaped backslash */
+ PARSE_LINE_TST("some=
\\\\opt C:\\\\directory\\\\file");
+ assert_int_equal(res, 2);
+ assert_string_equal(p[0], "some\\opt=
");
+ assert_=
string_equal(p[1], "C:\\directory\\file");
+ assert_null(p[res]);
+
+ /* comment chars are not special inside parameter *=
/
+ PARSE_LINE_TST=
("some-opt firstparm; second#parm");
+ assert_int_equal(res, 3);
+ assert_string_equal(p[0], "so=
me-opt");
+ a=
ssert_string_equal(p[1], "firstparm;");
+ assert_string_equal(p[2], "second#pa=
rm");
+ asser=
t_null(p[res]);
+
+ /* comment */
+ PARSE_LINE_TST("=
some-opt firstparm # secondparm");
+ assert_int_equal(res, 2);
+ assert_string_equal(p[0], "some-opt&=
quot;);
+ assert_s=
tring_equal(p[1], "firstparm");
+ assert_null(p[res]);
+
+ /* parameter just long enough */
+ PARSE_LINE_TST("opt " A_TIMES_256);<=
/span>
+ assert_int_equal=
(res, 2);
+ assert=
_string_equal(p[0], "opt");
+ assert_string_equal(p[1], A_TIMES_256);
+ /* quoting doesn't count for parameter =
length */
+ PARSE_=
LINE_TST("opt \"" A_TIMES_256 "\"");+ assert_int_equal(res, 2)=
;
+ assert_string_=
equal(p[0], "opt");
+ assert_string_equal(p[1], A_TIMES_256);
+ assert_null(p[res]);
+
+ /* very long line */
+ PARSE_LINE_TST("opt " A_TIMES_256 &q=
uot; " A_TIMES_256 " " A_TIMES_256 " " A_TIMES_256=
);
+ assert_int_eq=
ual(res, 5);
+ ass=
ert_string_equal(p[0], "opt");
+ assert_string_equal(p[1], A_TIMES_256);
=
+ assert_string_equal(p[2], =
A_TIMES_256);
+ as=
sert_string_equal(p[3], A_TIMES_256);
+ assert_string_equal(p[4], A_TIMES_256);
+ /* parameter too long */
+ PARSE_LINE_TST("opt " A_=
TIMES_256 "B");
+ assert_int_equal(res, 0);
+
+ =
/* max parameters */
+ PARSE_LINE_TST("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15");
+ assert_int_equal(r=
es, MAX_PARMS);
+ =
char num[3];
+ for=
(int i =3D 0; i < MAX_PARMS; i++)
+ {
+ assert_true(snprintf(num, 3, "%d", i) < 3);=
+ assert_string_equa=
l(p[i], num);
+ }<=
/span>
+ assert_null(p[re=
s]);
+
+ =
PARSE_LINE_TST("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16");
+ assert_int_equal(res=
, MAX_PARMS);
+ fo=
r (int i =3D 0; i < MAX_PARMS; i++)
+ {
+ assert_true(snprintf(num, 3, "%d", i) < 3);
+ assert_string_equ=
al(p[i], num);
+ }=
+ assert_null(p[r=
es]);
+
<=
span style=3D"color: hsl(120, 100%, 40%);">+}
+
+int
+main(void=
)
+{
+ const struct CMUnitTest tests[] =
=3D {
+ cmocka=
_unit_test(test_parse_line),
+ };
+
+ return cmocka_run_g=
roup_tests_name("options_parse", tests, NULL, NULL);+}
To view, visit change 1244. To unsubscribe, or for help writing mail filt= ers, visit setti= ngs.