From: "flichtenheld (Code Review)" Date: Fri, 17 May 2024 16:10:22 +0000 In-Reply-To: References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="4p910CUIloU="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: test_user_pass: Check fatal errors for empty username/password List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: openvpn-devel --4p910CUIloU= 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/+/474?usp=3Demail to look at the new patch set (#11). Change subject: test_user_pass: Check fatal errors for empty username/passw= ord ...................................................................... test_user_pass: Check fatal errors for empty username/password Required a fix to mock_msg to make tests of M_FATAL possible at all. This also tests some cases which arguably should throw a fatal error but do not. v2: - Suppress LeakSanitizer errors for fatal error tests. Due to aborting the function, the memory will not be cleaned up, but that is expected. v3: - Disable assert tests with MSVC. Does not seem to catch the error correctly. - Rebase on top of parallel-tests series to get AM_TESTS_ENVIRONMENT. v8: - Update srcdir handling according to master. v10: - Update mock_msg.c fatal handling to be compatible with NO_CMOCKA. Change-Id: Icabc8acf75638c86c8c395e9ffecba7a7226cd97 Signed-off-by: Frank Lichtenheld --- M CMakeLists.txt M tests/unit_tests/openvpn/Makefile.am A tests/unit_tests/openvpn/input/appears_empty.txt A tests/unit_tests/openvpn/input/empty.txt A tests/unit_tests/openvpn/input/leak_suppr.txt M tests/unit_tests/openvpn/mock_msg.c M tests/unit_tests/openvpn/test_user_pass.c 7 files changed, 112 insertions(+), 3 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/74/474/11 diff --git a/CMakeLists.txt b/CMakeLists.txt index f8b37a9..e32c99c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -668,7 +668,7 @@ # for compat with autotools make check set(_UT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests/unit_test= s/openvpn) set_tests_properties(${test_name} PROPERTIES - ENVIRONMENT "srcdir=3D${_UT_SOURCE_DIR}") + ENVIRONMENT "srcdir=3D${_UT_SOURCE_DIR};LSAN_OPTIONS=3Dsup= pressions=3D${_UT_SOURCE_DIR}/input/leak_suppr.txt") endif () add_executable(${test_name} tests/unit_tests/openvpn/${test_name}.c diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvp= n/Makefile.am index a4e6235..22dc075 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -4,6 +4,8 @@ AM_TESTSUITE_SUMMARY_HEADER =3D ' for $(PACKAGE_STRING) Unit-Tests' +AM_TESTS_ENVIRONMENT =3D export LSAN_OPTIONS=3Dsuppressions=3D$(srcdir)/in= put/leak_suppr.txt; + test_binaries=3D if HAVE_LD_WRAP_SUPPORT diff --git a/tests/unit_tests/openvpn/input/appears_empty.txt b/tests/unit_= tests/openvpn/input/appears_empty.txt new file mode 100644 index 0000000..ffb749a --- /dev/null +++ b/tests/unit_tests/openvpn/input/appears_empty.txt @@ -0,0 +1,3 @@ + + +(contains \t\n\t\n) diff --git a/tests/unit_tests/openvpn/input/empty.txt b/tests/unit_tests/op= envpn/input/empty.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/unit_tests/openvpn/input/empty.txt diff --git a/tests/unit_tests/openvpn/input/leak_suppr.txt b/tests/unit_tes= ts/openvpn/input/leak_suppr.txt new file mode 100644 index 0000000..72ebfe0 --- /dev/null +++ b/tests/unit_tests/openvpn/input/leak_suppr.txt @@ -0,0 +1 @@ +leak:_assertions$ diff --git a/tests/unit_tests/openvpn/mock_msg.c b/tests/unit_tests/openvpn= /mock_msg.c index a291f8f..098f56b 100644 --- a/tests/unit_tests/openvpn/mock_msg.c +++ b/tests/unit_tests/openvpn/mock_msg.c @@ -39,7 +39,6 @@ #include "error.h" unsigned int x_debug_level =3D 0; /* Default to (almost) no debugging outp= ut */ -bool fatal_error_triggered =3D false; void mock_set_debug_level(int level) @@ -59,11 +58,16 @@ { if (flags & M_FATAL) { - fatal_error_triggered =3D true; printf("FATAL ERROR:"); } vprintf(format, arglist); printf("\n"); +#ifndef NO_CMOCKA + if (flags & M_FATAL) + { + mock_assert(false, "FATAL ERROR", __FILE__, __LINE__); + } +#endif } void diff --git a/tests/unit_tests/openvpn/test_user_pass.c b/tests/unit_tests/o= penvpn/test_user_pass.c index ac84654..d6e464f 100644 --- a/tests/unit_tests/openvpn/test_user_pass.c +++ b/tests/unit_tests/openvpn/test_user_pass.c @@ -165,6 +165,16 @@ reset_user_pass(&up); + /*FIXME: query_user_exec() called even though nothing queued */ + will_return(query_user_exec_builtin, true); + /*FIXME: silently removes control characters but does not error out */ + assert_true(get_user_pass_cr(&up, "\t\n\t", "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.username, ""); + assert_string_equal(up.password, ""); + + reset_user_pass(&up); + expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT= Password:"); will_return(query_user_exec_builtin, "cpassword"); will_return(query_user_exec_builtin, true); @@ -197,6 +207,25 @@ assert_string_equal(up.password, "cpassword"); } +/* NOTE: expect_assert_failure does not seem to work with MSVC */ +#ifndef _MSC_VER +/* NOTE: leaks gc memory */ +static void +test_get_user_pass_inline_creds_assertions(void **state) +{ + struct user_pass up =3D { 0 }; + reset_user_pass(&up); + unsigned int flags =3D GET_USER_PASS_INLINE_CREDS; + + reset_user_pass(&up); + + /*FIXME: query_user_exec() called even though nothing queued */ + /*FIXME? username error thrown very late in stdin handling */ + will_return(query_user_exec_builtin, true); + expect_assert_failure(get_user_pass_cr(&up, "\nipassword\n", "UT", fla= gs, NULL)); +} +#endif + static void test_get_user_pass_authfile_stdin(void **state) { @@ -224,8 +253,39 @@ assert_true(up.defined); assert_string_equal(up.username, "user"); assert_string_equal(up.password, "cpassword"); + + reset_user_pass(&up); + + flags |=3D GET_USER_PASS_PASSWORD_ONLY; + expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT= Password:"); + will_return(query_user_exec_builtin, ""); + will_return(query_user_exec_builtin, true); + /*FIXME? does not error out on empty password */ + assert_true(get_user_pass_cr(&up, "stdin", "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.username, "user"); + assert_string_equal(up.password, ""); } +/* NOTE: expect_assert_failure does not seem to work with MSVC */ +#ifndef _MSC_VER +/* NOTE: leaks gc memory */ +static void +test_get_user_pass_authfile_stdin_assertions(void **state) +{ + struct user_pass up =3D { 0 }; + reset_user_pass(&up); + unsigned int flags =3D 0; + + expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT= Username:"); + expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT= Password:"); + will_return(query_user_exec_builtin, ""); + will_return(query_user_exec_builtin, "cpassword"); + will_return(query_user_exec_builtin, true); + expect_assert_failure(get_user_pass_cr(&up, "stdin", "UT", flags, NULL= )); +} +#endif + static void test_get_user_pass_authfile_file(void **state) { @@ -245,6 +305,17 @@ reset_user_pass(&up); + openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/appears_empty.t= xt"); + /*FIXME: query_user_exec() called even though nothing queued */ + will_return(query_user_exec_builtin, true); + /*FIXME? does not error out */ + assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.username, ""); + assert_string_equal(up.password, ""); + + reset_user_pass(&up); + openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt"); expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT= Password:"); will_return(query_user_exec_builtin, "cpassword"); @@ -324,6 +395,29 @@ } #endif /* ENABLE_MANAGEMENT */ +/* NOTE: expect_assert_failure does not seem to work with MSVC */ +#ifndef _MSC_VER +/* NOTE: leaks gc memory */ +static void +test_get_user_pass_authfile_file_assertions(void **state) +{ + struct user_pass up =3D { 0 }; + reset_user_pass(&up); + unsigned int flags =3D 0; + + char authfile[PATH_MAX] =3D { 0 }; + + openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/empty.txt"); + expect_assert_failure(get_user_pass_cr(&up, authfile, "UT", flags, NUL= L)); + + reset_user_pass(&up); + + flags |=3D GET_USER_PASS_PASSWORD_ONLY; + openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/empty.txt"); + expect_assert_failure(get_user_pass_cr(&up, authfile, "UT", flags, NUL= L)); +} +#endif /* ifndef _MSC_VER */ + const struct CMUnitTest user_pass_tests[] =3D { cmocka_unit_test(test_get_user_pass_defined), cmocka_unit_test(test_get_user_pass_needok), @@ -334,6 +428,11 @@ cmocka_unit_test(test_get_user_pass_dynamic_challenge), cmocka_unit_test(test_get_user_pass_static_challenge), #endif /* ENABLE_MANAGEMENT */ +#ifndef _MSC_VER + cmocka_unit_test(test_get_user_pass_inline_creds_assertions), + cmocka_unit_test(test_get_user_pass_authfile_stdin_assertions), + cmocka_unit_test(test_get_user_pass_authfile_file_assertions), +#endif }; int -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/474?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: Icabc8acf75638c86c8c395e9ffecba7a7226cd97 Gerrit-Change-Number: 474 Gerrit-PatchSet: 11 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-MessageType: newpatchset --4p910CUIloU= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: plaisthos.

flichtenheld uploaded patch set #11 to this change.

=

View C= hange

test_user_pass: Check fatal errors for empty username/password
Required a fix to mock_msg to make tests of M_FATAL
possible at all= .
This also tests some cases which arguably should throw
a fatal erro= r but do not.

v2:
- Suppress LeakSanitizer errors for fatal erro= r tests.
Due to aborting the function, the memory will not be
c= leaned up, but that is expected.
v3:
- Disable assert tests with MSV= C. Does not seem to catch
the error correctly.
- Rebase on top of= parallel-tests series to get
AM_TESTS_ENVIRONMENT.
v8:
- Upda= te srcdir handling according to master.
v10:
- Update mock_msg.c fat= al handling to be compatible
with NO_CMOCKA.

Change-Id: Icabc8= acf75638c86c8c395e9ffecba7a7226cd97
Signed-off-by: Frank Lichtenheld <= ;frank@...2641...>
---
M CMakeLists.txt
M tests/unit_tests/open= vpn/Makefile.am
A tests/unit_tests/openvpn/input/appears_empty.txt
A = tests/unit_tests/openvpn/input/empty.txt
A tests/unit_tests/openvpn/inpu= t/leak_suppr.txt
M tests/unit_tests/openvpn/mock_msg.c
M tests/unit_t= ests/openvpn/test_user_pass.c
7 files changed, 112 insertions(+), 3 dele= tions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/cha=
nges/74/474/11
diff --git a/CMakeLists.txt b/CMakeLists.txt<=
br>index f8b37a9..e32c99c 100644
--- a/CMakeLists.txt=
+++ b/CMakeLists.txt
@@ -668,7 +668,7 @@
# for compat with autotools make check set(_UT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests/u= nit_tests/openvpn)
set_tests_properties(${test= _name} PROPERTIES
- = ENVIRONMENT "srcdir=3D${_UT_SOURCE_DIR}")
<= span style=3D"color: hsl(120, 100%, 40%);">+ ENVIRONMENT &qu= ot;srcdir=3D${_UT_SOURCE_DIR};LSAN_OPTIONS=3Dsuppressions=3D${_UT_SOURCE_DI= R}/input/leak_suppr.txt")

endif ()
= add_executable(${test_name}
tes= ts/unit_tests/openvpn/${test_name}.c
diff --git a/tests/uni= t_tests/openvpn/Makefile.am b/tests/unit_tests/openvpn/Makefile.amindex a4e6235..22dc075 100644
--- a/tests/unit_test= s/openvpn/Makefile.am
+++ b/tests/unit_tests/openvpn/Makefi= le.am
@@ -4,6 +4,8 @@

AM= _TESTSUITE_SUMMARY_HEADER =3D ' for $(PACKAGE_STRING) Unit-Tests'

+AM_= TESTS_ENVIRONMENT =3D export LSAN_OPTIONS=3Dsuppressions=3D$(srcdir)/input/= leak_suppr.txt;
+
test_binaries=3D

if HAVE_L= D_WRAP_SUPPORT
diff --git a/tests/unit_tests/openvpn/input/= appears_empty.txt b/tests/unit_tests/openvpn/input/appears_empty.txt=
new file mode 100644
index 0000000..ffb749a
--- /dev/null
+++ b/tests/unit_tests/openvpn/inp= ut/appears_empty.txt
@@ -0,0 +1,3 @@
+
+
+(c= ontains \t\n\t\n)
diff --git a/tests/unit_tests/openvpn/inp= ut/empty.txt b/tests/unit_tests/openvpn/input/empty.txt
new= file mode 100644
index 0000000..e69de29
--= - /dev/null
+++ b/tests/unit_tests/openvpn/input/empty.txt<= /span>
diff --git a/tests/unit_tests/openvpn/input/leak_suppr.txt = b/tests/unit_tests/openvpn/input/leak_suppr.txt
new file mo= de 100644
index 0000000..72ebfe0
--- /dev/n= ull
+++ b/tests/unit_tests/openvpn/input/leak_suppr.txt
@@ -0,0 +1 @@
+leak:_assertions$
diff --git a/tests/unit_tests/open= vpn/mock_msg.c b/tests/unit_tests/openvpn/mock_msg.c
index = a291f8f..098f56b 100644
--- a/tests/unit_tests/openvpn/mock= _msg.c
+++ b/tests/unit_tests/openvpn/mock_msg.c
= @@ -39,7 +39,6 @@
#include "error.h"

unsigned int x_debug_level =3D 0; /* Default = to (almost) no debugging output */
-bool fatal_error_triggered =3D false;
=
void
mock_set_debug_level(int level)
= @@ -59,11 +58,16 @@
{
if (flags= & M_FATAL)
{
- fatal_error_triggered =3D true;
= printf("FATAL ERROR:");
}
<= span> vprintf(format, arglist);

printf("\n&qu= ot;);
+#ifndef NO_CMO= CKA
+ if (flags &a= mp; M_FATAL)
+ {
+ mock_assert(f= alse, "FATAL ERROR", __FILE__, __LINE__);
+ }
+#endif
}

<= span> void

diff --git a/tests/unit_tests/openvpn/test_user_= pass.c b/tests/unit_tests/openvpn/test_user_pass.c
index ac= 84654..d6e464f 100644
--- a/tests/unit_tests/openvpn/test_u= ser_pass.c
+++ b/tests/unit_tests/openvpn/test_user_pass.c<= /span>
@@ -165,6 +165,16 @@

= reset_user_pass(&up);

+ /*FIXME: query_user_exec() called even though = nothing queued */
+ = will_return(query_user_exec_builtin, true);
+ /*FIXME: silently removes control characters = but does not error out */
+ assert_true(get_user_pass_cr(&up, "\t\n\t", "U= T", flags, NULL));
+ assert_true(up.defined);
+ assert_string_equal(up.username, "");
= + assert_string_equal(up.pas= sword, "");
+
+ reset_user_pa= ss(&up);
+=
expect_string(query_user_exec_builtin, query_user[i].prompt,= "Enter UT Password:");
will_return(query_us= er_exec_builtin, "cpassword");
will_return(q= uery_user_exec_builtin, true);
@@ -197,6 +207,25 @@<= br> assert_string_equal(up.password, "cpassword");
}

+/* NOTE: expect_assert_failure does not seem to work with MSVC= */
+#ifndef _MSC_VER=
+/* NOTE: leaks gc m= emory */
+static void=
+test_get_user_pass_= inline_creds_assertions(void **state)
+{
+= struct user_pass up =3D { 0 };
+ reset_user_pass(&up);
+ unsigned int flags =3D GET_USER_PASS_INLINE_CR= EDS;
+
+ reset_user_pass(&up);
+
+ /*FIXME: query_user_exec() called eve= n though nothing queued */
+ /*FIXME? username error thrown very late in stdin handling */
+ will_return(query_= user_exec_builtin, true);
+ expect_assert_failure(get_user_pass_cr(&up, "\nipassword= \n", "UT", flags, NULL));
+}
+ static void
test_get_user_pass_authfile_stdin(void= **state)
{
@@ -224,8 +253,39 @@ assert_true(up.defined);
assert_string_equ= al(up.username, "user");
assert_string_equal= (up.password, "cpassword");
+
+ = reset_user_pass(&up);
+
+ flags = |=3D GET_USER_PASS_PASSWORD_ONLY;
+ expect_string(query_user_exec_builtin, query_user[i].prom= pt, "Enter UT Password:");
+ will_return(query_user_exec_builtin, "");
+ will_return(query_= user_exec_builtin, true);
+ /*FIXME? does not error out on empty password */
+ assert_true(get_user_pass_cr(&am= p;up, "stdin", "UT", flags, NULL));
+ assert_true(up.defined);
= + assert_string_equal(up.use= rname, "user");
+ assert_string_equal(up.password, "");
= }

+= /* NOTE: expect_assert_failure does not seem to work with MSVC */+#ifndef _MSC_VER
+/* NOTE: leaks gc memory */
+static void
+test_get_user_pass_authfile_stdi= n_assertions(void **state)

+{
+ struct = user_pass up =3D { 0 };
+ reset_user_pass(&up);
+ unsigned int flags =3D 0;
+
+ expect_string(query_user_exec_builtin, query_user[i].prompt, "= ;Enter UT Username:");
+ expect_string(query_user_exec_builtin, query_user[i].prompt, &q= uot;Enter UT Password:");
+ will_return(query_user_exec_builtin, "");+ will_return(query_user_e= xec_builtin, "cpassword");
+ will_return(query_user_exec_builtin, true);
= + expect_assert_failure(get_= user_pass_cr(&up, "stdin", "UT", flags, NULL));
+}
+#endif
+
static void
test_ge= t_user_pass_authfile_file(void **state)
{
= @@ -245,6 +305,17 @@

reset_user_pass= (&up);

+ openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/ap= pears_empty.txt");
+ /*FIXME: query_user_exec() called even though nothing queued */
+ will_return(query_u= ser_exec_builtin, true);
+ /*FIXME? does not error out */
+ assert_true(get_user_pass_cr(&up, authfile, &qu= ot;UT", flags, NULL));
+ assert_true(up.defined);
+ assert_string_equal(up.username, "");=
+ assert_string_equal(up= .password, "");
+
+ reset_use= r_pass(&up);
+
openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "in= put/user_only.txt");
expect_string(query_user_exe= c_builtin, query_user[i].prompt, "Enter UT Password:"); will_return(query_user_exec_builtin, "cpassword");
@@ -324,6 +395,29 @@
}
#end= if /* ENABLE_MANAGEMENT */

+/* NOTE: expect_assert_failure does not seem to wo= rk with MSVC */
+#ifn= def _MSC_VER
+/* NOTE= : leaks gc memory */
= +static void
+test_ge= t_user_pass_authfile_file_assertions(void **state)
+{
+ struct user_pass up =3D { 0 };
+ reset_user_pass(&up);
+ unsigned int flags =3D 0;<= br>+
+ char authfile[PATH_MAX] =3D { 0 };<= br>+
+ openvpn_test_get_srcdir_dir(authfile, PATH= _MAX, "input/empty.txt");
+ expect_assert_failure(get_user_pass_cr(&up, authfil= e, "UT", flags, NULL));
+
+ r= eset_user_pass(&up);
+
+ flags |=3D= GET_USER_PASS_PASSWORD_ONLY;
+ openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/e= mpty.txt");
+ = expect_assert_failure(get_user_pass_cr(&up, authfile, "UT", = flags, NULL));
+}
+#endif /* ifndef _MSC_V= ER */
+
const struct CMUnitTest user_pass_tests[] =3D {

cm= ocka_unit_test(test_get_user_pass_defined),
cmocka_uni= t_test(test_get_user_pass_needok),
@@ -334,6 +428,11 @@
cmocka_unit_test(test_get_user_pass_dynamic_challenge),
cmocka_unit_test(test_get_user_pass_static_challenge),<= /span>
#endif /* ENABLE_MANAGEMENT */
+#ifndef _MSC_VER
+ cmocka_unit_test(test_get_user_pass_inline_cre= ds_assertions),
+ = cmocka_unit_test(test_get_user_pass_authfile_stdin_assertions),
<= span style=3D"color: hsl(120, 100%, 40%);">+ cmocka_unit_test(test_get_u= ser_pass_authfile_file_assertions),

+#endif
};

int


To view, visit change 474. To unsubsc= ribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Icabc8acf75638c86c8c395e9ffe= cba7a7226cd97
Gerrit-Change-Number: 474
Gerrit-PatchSet: 11
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
--4p910CUIloU=--