From: "flichtenheld (Code Review)" Date: Fri, 8 Dec 2023 17:30:49 +0000 In-Reply-To: References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <609706ccaf71fe5629d64c1a81585860c997bbac-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="7id6BMoqJjE="; 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 --7id6BMoqJjE= 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 (#2). 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. Change-Id: Icabc8acf75638c86c8c395e9ffecba7a7226cd97 Signed-off-by: Frank Lichtenheld --- M .github/workflows/build.yaml 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 8 files changed, 100 insertions(+), 3 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/74/474/2 diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index d7cc01c..784a844 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -283,6 +283,7 @@ configurePreset: win-${{ matrix.arch }}-release buildPreset: win-${{ matrix.arch }}-release testPreset: win-${{ matrix.arch }}-release + testPresetAdditionalArgs: "['--output-on-failure']" - uses: actions/upload-artifact@...2689... with: diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f96354..142bde4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -621,8 +621,9 @@ if (NOT ${test_name} STREQUAL "test_networking") add_test(${test_name} ${test_name}) # 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${CMAKE_CURRENT_SOURCE_DIR}/tests/un= it_tests/openvpn") + 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 6199b4a..ac85aca 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -2,6 +2,8 @@ EXTRA_DIST =3D input +export LSAN_OPTIONS=3Dsuppressions=3D$(srcdir)/input/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 d74efaa..2fcad9d 100644 --- a/tests/unit_tests/openvpn/mock_msg.c +++ b/tests/unit_tests/openvpn/mock_msg.c @@ -38,7 +38,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) @@ -58,11 +57,14 @@ { if (flags & M_FATAL) { - fatal_error_triggered =3D true; printf("FATAL ERROR:"); } vprintf(format, arglist); printf("\n"); + if (flags & M_FATAL) + { + mock_assert(false, "FATAL ERROR", __FILE__, __LINE__); + } } void diff --git a/tests/unit_tests/openvpn/test_user_pass.c b/tests/unit_tests/o= penvpn/test_user_pass.c index e468d3f..ed65fdb 100644 --- a/tests/unit_tests/openvpn/test_user_pass.c +++ b/tests/unit_tests/openvpn/test_user_pass.c @@ -172,6 +172,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); @@ -205,6 +215,21 @@ } 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)); +} + +static void test_get_user_pass_authfile_stdin(void **state) { struct user_pass up =3D { 0 }; @@ -231,6 +256,33 @@ 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, ""); +} + +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= )); } static void @@ -254,6 +306,17 @@ reset_user_pass(&up); + snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/appears_empty.txt= "); + /*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); + snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt"); expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT= Password:"); will_return(query_user_exec_builtin, "cpassword"); @@ -276,12 +339,36 @@ assert_string_equal(up.password, "fuser"); } +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; + + const char *srcdir =3D getenv("srcdir"); + assert_non_null(srcdir); + char authfile[PATH_MAX] =3D { 0 }; + + snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "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; + snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/empty.txt"); + expect_assert_failure(get_user_pass_cr(&up, authfile, "UT", flags, NUL= L)); +} + const struct CMUnitTest user_pass_tests[] =3D { cmocka_unit_test(test_get_user_pass_defined), cmocka_unit_test(test_get_user_pass_needok), cmocka_unit_test(test_get_user_pass_inline_creds), + cmocka_unit_test(test_get_user_pass_inline_creds_assertions), cmocka_unit_test(test_get_user_pass_authfile_stdin), + cmocka_unit_test(test_get_user_pass_authfile_stdin_assertions), cmocka_unit_test(test_get_user_pass_authfile_file), + cmocka_unit_test(test_get_user_pass_authfile_file_assertions), }; 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: 2 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-MessageType: newpatchset --7id6BMoqJjE= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: plaisthos.

flichtenheld uploaded patch set #2 to this change.

<= p>View Ch= ange

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
cl= eaned up, but that is expected.

Change-Id: Icabc8acf75638c86c8c395e9= ffecba7a7226cd97
Signed-off-by: Frank Lichtenheld <frank@...2641...&g= t;
---
M .github/workflows/build.yaml
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_tes= ts/openvpn/input/leak_suppr.txt
M tests/unit_tests/openvpn/mock_msg.cM tests/unit_tests/openvpn/test_user_pass.c
8 files changed, 100 insert= ions(+), 3 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/o=
penvpn refs/changes/74/474/2
diff --git a/.github/workflows/build.y=
aml b/.github/workflows/build.yaml
index d7cc01c..784a844 1= 00644
--- a/.github/workflows/build.yaml
++= + b/.github/workflows/build.yaml
@@ -283,6 +283,7 @@=
configurePreset: win-${{ matrix.arch }}-release=
buildPreset: win-${{ matrix.arch }}-release
= testPreset: win-${{ matrix.arch }}-release
+ testPresetAdditionalArgs:= "['--output-on-failure']"

- uses: actions/upload-artifact@...2689...
= with:
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3f96354..142bde4 100644
--- a/CMakeLists.= txt
+++ b/CMakeLists.txt
@@ -621,8 +621,9 @= @
if (NOT ${test_name} STREQUAL "test_network= ing")
add_test(${test_name} ${test_name})=
# for compat with autotools make check=
+ set(_UT_SOURCE= _DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests/unit_tests/openvpn)
= set_tests_properties(${test_name} PROPERTIES
- ENVIRONMENT "srcdi= r=3D${CMAKE_CURRENT_SOURCE_DIR}/tests/unit_tests/openvpn")
<= 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 6199b4a..ac85aca 100644
--- a/tests/unit_test= s/openvpn/Makefile.am
+++ b/tests/unit_tests/openvpn/Makefi= le.am
@@ -2,6 +2,8 @@

EX= TRA_DIST =3D input

+export LSAN_OPTIONS=3Dsuppressions=3D$(srcdir)/input/leak_= suppr.txt
+ test_binaries=3D

if HAVE_LD_WRAP= _SUPPORT
diff --git a/tests/unit_tests/openvpn/input/appear= s_empty.txt b/tests/unit_tests/openvpn/input/appears_empty.txt
new file mode 100644
index 0000000..ffb749a
<= span>--- /dev/null
+++ b/tests/unit_tests/openvpn/input/app= ears_empty.txt
@@ -0,0 +1,3 @@
+
+
+(contains= \t\n\t\n)
diff --git a/tests/unit_tests/openvpn/input/empt= y.txt b/tests/unit_tests/openvpn/input/empty.txt
new file m= ode 100644
index 0000000..e69de29
--- /dev/= null
+++ b/tests/unit_tests/openvpn/input/empty.txt<= br>diff --git a/tests/unit_tests/openvpn/input/leak_suppr.txt b/tests= /unit_tests/openvpn/input/leak_suppr.txt
new file mode 1006= 44
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/moc= k_msg.c b/tests/unit_tests/openvpn/mock_msg.c
index d74efaa= ..2fcad9d 100644
--- a/tests/unit_tests/openvpn/mock_msg.c<= /span>
+++ b/tests/unit_tests/openvpn/mock_msg.c
@= @ -38,7 +38,6 @@
#include "error.h"

unsigned int x_debug_level =3D 0; /* Default to (alm= ost) no debugging output */
-bool fatal_error_triggered =3D false;

void

mock_set_debug_level(int level)
@= @ -58,11 +57,14 @@
{
if (flags & = M_FATAL)
{
- fatal_error_triggered =3D true;
= printf("FATAL ERROR:");
}
= vprintf(format, arglist);
printf("\n");
+ if (flags & M= _FATAL)
+ {=
+ mock_assert(false,= "FATAL ERROR", __FILE__, __LINE__);
+ }
}

void
diff --git a/tests/unit_tests/openvpn/test= _user_pass.c b/tests/unit_tests/openvpn/test_user_pass.c
in= dex e468d3f..ed65fdb 100644
--- a/tests/unit_tests/openvpn/= test_user_pass.c
+++ b/tests/unit_tests/openvpn/test_user_p= ass.c
@@ -172,6 +172,16 @@

reset_user_pass(&up);

+ /*FIXME: query_user_exec() called even t= hough nothing queued */
+ will_return(query_user_exec_builtin, true);
+ /*FIXME: silently removes control cha= racters 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_equa= l(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);
@@ -205,6 +215,21 @@=
}

static void+test_get_user_pass_inline_cr= eds_assertions(void **state)
+{
+ struc= t user_pass up =3D { 0 };
+ reset_user_pass(&up);
+ unsigned int flags =3D GET_USER_PASS_INLINE_CREDS;

+
+ reset_user_pass(&up);
<= span style=3D"color: hsl(120, 100%, 40%);">+
+ /*FIXME: query_user_exec() called even though = nothing queued */
+ = /*FIXME? username error thrown very late in stdin handling */
<= span style=3D"color: hsl(120, 100%, 40%);">+ 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)

{
struct user_pass up =3D { 0 };
@@ -231,6 += 256,33 @@
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_builti= n, "");
+ = 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, ""= );
+}
+

+static void
+test_get_user_pass_authfile_stdin_assertions(void **state)
+{
+ struct user_pass up =3D { 0 };=
+ reset_user_pass(&u= p);
+ unsigned int= flags =3D 0;
+
+ expect_string(query_u= ser_exec_builtin, query_user[i].prompt, "Enter UT Username:");
+ expect_string(quer= y_user_exec_builtin, query_user[i].prompt, "Enter UT Password:");=
+ will_return(que= ry_user_exec_builtin, "");
+ will_return(query_user_exec_builtin, "cpassword&q= uot;);
+ will_retu= rn(query_user_exec_builtin, true);
+ expect_assert_failure(get_user_pass_cr(&up, "st= din", "UT", flags, NULL));
}

static void
@@ -254,6 +306,17 @@

reset_user_pass(&up);

+ snprintf(auth= file, PATH_MAX, "%s/%s", srcdir, "input/appears_empty.txt&qu= ot;);
+ /*FIXME: q= uery_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, "&= quot;);
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ reset_user_pass(&up);
+
= snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_o= nly.txt");
expect_string(query_user_exec_builtin,= query_user[i].prompt, "Enter UT Password:");
= will_return(query_user_exec_builtin, "cpassword");
@@ -276,12 +339,36 @@

assert_string_equal(up.passw= ord, "fuser");
}

+static void

+test_get_user_pass_authfile_file_assertio= ns(void **state)
+{
+ struct user_pass = up =3D { 0 };
+ re= set_user_pass(&up);
+ unsigned int flags =3D 0;
+
+ c= onst char *srcdir =3D getenv("srcdir");
+ assert_non_null(srcdir);
+ char authfile[PATH_MAX] =3D { 0 = };
+
+ snprintf(authfile, PATH_MAX, &qu= ot;%s/%s", srcdir, "input/empty.txt");
+ expect_assert_failure(get_user_pass_c= r(&up, authfile, "UT", flags, NULL));
+
+ reset_user_pass(&up);
+
+ flags |=3D GET_USER_PASS_PASSWORD_ONLY;
+ snprintf(authfile, PATH_MAX, "%s/%s&= quot;, srcdir, "input/empty.txt");
+ expect_assert_failure(get_user_pass_cr(&up= , authfile, "UT", flags, NULL));
+}
+
const struct CMUnitTest user_pass_tests[] =3D {
cmocka_unit_test(test_get_user_pass_defined),
cmocka_unit_test(test_get_user_pass_needok),

= cmocka_unit_test(test_get_user_pass_inline_creds),
+ cmocka_unit_test(test_get_user_pass_inli= ne_creds_assertions),
cmocka_unit_test(test_get_user_p= ass_authfile_stdin),
= + cmocka_unit_test(test_get_user_pass_authfile_stdin_assertions),=
cmocka_unit_test(test_get_user_pass_authfile_file),+ cmocka_unit_test(test_ge= t_user_pass_authfile_file_assertions),
};
=
int

To view, visit <= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/474?usp=3Demail">change 474= . To unsubscribe, 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: 2
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
--7id6BMoqJjE=--