From: "flichtenheld (Code Review)" Date: Mon, 22 Jan 2024 09:51:36 +0000 In-Reply-To: References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, gert@...1296... Message-ID: <7686d55a9bde78c9adddf4dbe95497fc9a4fc484-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="QxKbhQJNuH0="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: test_user_pass: new UT for get_user_pass List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , cron2 Cc: openvpn-devel --QxKbhQJNuH0= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, flichtenheld, plaisthos. Hello cron2, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/468?usp=3Demail to look at the new patch set (#8). The following approvals got outdated and were removed: Code-Review+2 by plaisthos, Code-Review-1 by cron2 The change is no longer submittable: Code-Review and checks~ChecksSubmitRul= e are unsatisfied now. Change subject: test_user_pass: new UT for get_user_pass ...................................................................... test_user_pass: new UT for get_user_pass UTs for basic functionality, without management functions. v2: - add CMake support - add GHA support for both MSVC and mingw v3: - fix distcheck by adding input/ directory to dist Change-Id: I193aef06912f01426dd4ac298aadfab97dd75a35 Signed-off-by: Frank Lichtenheld --- M .github/workflows/build.yaml M CMakeLists.txt M src/openvpn/misc.h M src/openvpn/syshead.h M tests/unit_tests/openvpn/Makefile.am A tests/unit_tests/openvpn/input/user_only.txt A tests/unit_tests/openvpn/input/user_pass.txt A tests/unit_tests/openvpn/test_user_pass.c 8 files changed, 327 insertions(+), 2 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/68/468/8 diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index cc98813..bc937e5 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -85,11 +85,13 @@ fail-fast: false matrix: arch: [x86, x64] - test: [argv, auth_token, buffer, cryptoapi, crypto, misc, ncp, pac= ket_id, pkt, provider, ssl, tls_crypt] + test: [argv, auth_token, buffer, cryptoapi, crypto, misc, ncp, pac= ket_id, pkt, provider, ssl, tls_crypt, user_pass] runs-on: windows-latest name: "mingw unittest ${{ matrix.test }} - ${{ matrix.arch }} - OSSL" steps: + - name: Checkout OpenVPN + uses: actions/checkout@...2689... - name: Retrieve mingw unittest uses: actions/download-artifact@...2689... with: @@ -97,6 +99,8 @@ path: unittests - name: Run ${{ matrix.test }} unit test run: ./unittests/test_${{ matrix.test }}.exe + env: + srcdir: "${{ github.workspace }}/tests/unit_tests/openvpn" ubuntu: strategy: @@ -279,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 5e07d50..be55c60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -604,6 +604,7 @@ "test_pkt" "test_provider" "test_ssl" + "test_user_pass" ) if (WIN32) @@ -662,6 +663,10 @@ # test_networking needs special environment 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${_UT_SOURCE_DIR}") endif () add_executable(${test_name} tests/unit_tests/openvpn/${test_name}.c @@ -782,6 +787,15 @@ src/openvpn/base64.c ) + target_sources(test_user_pass PRIVATE + tests/unit_tests/openvpn/mock_get_random.c + tests/unit_tests/openvpn/mock_win32_execve.c + src/openvpn/base64.c + src/openvpn/console.c + src/openvpn/env_set.c + src/openvpn/run_command.c + ) + if (TARGET test_argv) target_link_options(test_argv PRIVATE -Wl,--wrap=3Dparse_line) target_sources(test_argv PRIVATE diff --git a/src/openvpn/misc.h b/src/openvpn/misc.h index d51609d..5ae61b5 100644 --- a/src/openvpn/misc.h +++ b/src/openvpn/misc.h @@ -118,12 +118,31 @@ #define GET_USER_PASS_INLINE_CREDS (1<<10) /* indicates that auth_file is= actually inline creds */ +/** + * Retrieves the user credentials from various sources depending on the fl= ags. + * + * @param up The user_pass structure to store the retrieved credentials. + * @param auth_file The path to the authentication file. Might be NULL. + * @param prefix The prefix to prepend to user prompts. + * @param flags Additional flags to control the behavior of the function. + * @param auth_challenge The authentication challenge string. + * @return true if the user credentials were successfully retrieved, false= otherwise. + */ bool get_user_pass_cr(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags, const char *auth_challenge); +/** + * Retrieves the user credentials from various sources depending on the fl= ags. + * + * @param up The user_pass structure to store the retrieved credentials. + * @param auth_file The path to the authentication file. Might be NULL. + * @param prefix The prefix to prepend to user prompts. + * @param flags Additional flags to control the behavior of the function. + * @return true if the user credentials were successfully retrieved, false= otherwise. + */ static inline bool get_user_pass(struct user_pass *up, const char *auth_file, diff --git a/src/openvpn/syshead.h b/src/openvpn/syshead.h index a021c91..d2859fe 100644 --- a/src/openvpn/syshead.h +++ b/src/openvpn/syshead.h @@ -524,4 +524,10 @@ #define ENABLE_MEMSTATS #endif +#ifdef _MSC_VER +#ifndef PATH_MAX +#define PATH_MAX MAX_PATH +#endif +#endif + #endif /* ifndef SYSHEAD_H */ diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvp= n/Makefile.am index f81a10f..88a694d 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -1,5 +1,7 @@ AUTOMAKE_OPTIONS =3D foreign +EXTRA_DIST =3D input + AM_TESTSUITE_SUMMARY_HEADER =3D ' for $(PACKAGE_STRING) Unit-Tests' test_binaries=3D @@ -9,7 +11,7 @@ endif test_binaries +=3D crypto_testdriver packet_id_testdriver auth_token_testd= river ncp_testdriver misc_testdriver \ - pkt_testdriver ssl_testdriver + pkt_testdriver ssl_testdriver user_pass_testdriver if HAVE_LD_WRAP_SUPPORT if !WIN32 @@ -251,6 +253,22 @@ $(top_srcdir)/src/openvpn/base64.c +user_pass_testdriver_CFLAGS =3D @TEST_CFLAGS@ \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/op= envpn +user_pass_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ + +user_pass_testdriver_SOURCES =3D test_user_pass.c mock_msg.c \ + $(top_srcdir)/src/openvpn/buffer.c \ + $(top_srcdir)/src/openvpn/console.c \ + $(top_srcdir)/src/openvpn/env_set.c \ + mock_win32_execve.c \ + $(top_srcdir)/src/openvpn/run_command.c \ + mock_get_random.c \ + $(top_srcdir)/src/openvpn/platform.c \ + $(top_srcdir)/src/openvpn/win32-util.c \ + $(top_srcdir)/src/openvpn/base64.c + + ncp_testdriver_CFLAGS =3D @TEST_CFLAGS@ \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/op= envpn \ $(OPTIONAL_CRYPTO_CFLAGS) diff --git a/tests/unit_tests/openvpn/input/user_only.txt b/tests/unit_test= s/openvpn/input/user_only.txt new file mode 100644 index 0000000..3ca0db3 --- /dev/null +++ b/tests/unit_tests/openvpn/input/user_only.txt @@ -0,0 +1 @@ +fuser diff --git a/tests/unit_tests/openvpn/input/user_pass.txt b/tests/unit_test= s/openvpn/input/user_pass.txt new file mode 100644 index 0000000..31b38ef --- /dev/null +++ b/tests/unit_tests/openvpn/input/user_pass.txt @@ -0,0 +1,2 @@ +fuser +fpassword diff --git a/tests/unit_tests/openvpn/test_user_pass.c b/tests/unit_tests/o= penvpn/test_user_pass.c new file mode 100644 index 0000000..ab4dfe4 --- /dev/null +++ b/tests/unit_tests/openvpn/test_user_pass.c @@ -0,0 +1,260 @@ +/* + * 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) 2023 OpenVPN Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by t= he + * Free Software Foundation, either version 2 of the License, + * or (at your option) any later version. + * + * 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#undef ENABLE_SYSTEMD + +#include "syshead.h" +#include "manage.h" + +#include +#include +#include +#include + +#include "misc.c" + +struct management *management; /* global */ + +/* mocking */ +bool +query_user_exec_builtin(void) +{ + /* Loop through configured query_user slots */ + for (int i =3D 0; i < QUERY_USER_NUMSLOTS && query_user[i].response != =3D NULL; i++) + { + check_expected(query_user[i].prompt); + strncpy(query_user[i].response, mock_ptr_type(char *), query_user[= i].response_len); + } + + return mock(); +} +void +management_auth_failure(struct management *man, const char *type, const ch= ar *reason) +{ + assert_true(0); +} +bool +management_query_user_pass(struct management *man, + struct user_pass *up, + const char *type, + const unsigned int flags, + const char *static_challenge) +{ + assert_true(0); + return false; +} +/* stubs for some unused functions instead of pulling in too many dependen= cies */ +int +parse_line(const char *line, char **p, const int n, const char *file, + const int line_num, int msglevel, struct gc_arena *gc) +{ + assert_true(0); + return 0; +} + +/* tooling */ +static void +reset_user_pass(struct user_pass *up) +{ + up->defined =3D false; + up->token_defined =3D false; + up->nocache =3D false; + strcpy(up->username, "user"); + strcpy(up->password, "password"); +} + +static void +test_get_user_pass_defined(void **state) +{ + struct user_pass up =3D { 0 }; + reset_user_pass(&up); + up.defined =3D true; + assert_true(get_user_pass_cr(&up, NULL, "UT", 0, NULL)); +} + +static void +test_get_user_pass_needok(void **state) +{ + struct user_pass up =3D { 0 }; + reset_user_pass(&up); + unsigned int flags =3D GET_USER_PASS_NEED_OK; + + expect_string(query_user_exec_builtin, query_user[i].prompt, "NEED-OK|= UT|user:"); + will_return(query_user_exec_builtin, ""); + will_return(query_user_exec_builtin, true); + /*FIXME: query_user_exec() called even though nothing queued */ + will_return(query_user_exec_builtin, true); + assert_true(get_user_pass_cr(&up, NULL, "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.password, "ok"); + + reset_user_pass(&up); + + expect_string(query_user_exec_builtin, query_user[i].prompt, "NEED-OK|= UT|user:"); + will_return(query_user_exec_builtin, "cancel"); + will_return(query_user_exec_builtin, true); + /*FIXME: query_user_exec() called even though nothing queued */ + will_return(query_user_exec_builtin, true); + assert_true(get_user_pass_cr(&up, NULL, "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.password, "cancel"); +} + +static void +test_get_user_pass_inline_creds(void **state) +{ + struct user_pass up =3D { 0 }; + reset_user_pass(&up); + unsigned int flags =3D GET_USER_PASS_INLINE_CREDS; + + /*FIXME: query_user_exec() called even though nothing queued */ + will_return(query_user_exec_builtin, true); + assert_true(get_user_pass_cr(&up, "iuser\nipassword", "UT", flags, NUL= L)); + assert_true(up.defined); + assert_string_equal(up.username, "iuser"); + assert_string_equal(up.password, "ipassword"); + + 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); + /* will try to retrieve missing password from stdin */ + assert_true(get_user_pass_cr(&up, "iuser", "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.username, "iuser"); + assert_string_equal(up.password, "cpassword"); + + reset_user_pass(&up); + + flags |=3D GET_USER_PASS_PASSWORD_ONLY; + /*FIXME: query_user_exec() called even though nothing queued */ + will_return(query_user_exec_builtin, true); + assert_true(get_user_pass_cr(&up, "ipassword\n", "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.username, "user"); + assert_string_equal(up.password, "ipassword"); + + 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, "cpassword"); + will_return(query_user_exec_builtin, true); + /* will try to retrieve missing password from stdin */ + assert_true(get_user_pass_cr(&up, "", "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.username, "user"); + assert_string_equal(up.password, "cpassword"); +} + +static void +test_get_user_pass_authfile_stdin(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, "cuser"); + will_return(query_user_exec_builtin, "cpassword"); + will_return(query_user_exec_builtin, true); + assert_true(get_user_pass_cr(&up, "stdin", "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.username, "cuser"); + 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, "cpassword"); + will_return(query_user_exec_builtin, true); + 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, "cpassword"); +} + +static void +test_get_user_pass_authfile_file(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/user_pass.txt"); + /*FIXME: query_user_exec() called even though nothing queued */ + will_return(query_user_exec_builtin, true); + assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.username, "fuser"); + assert_string_equal(up.password, "fpassword"); + + 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"); + will_return(query_user_exec_builtin, true); + /* will try to retrieve missing password from stdin */ + assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.username, "fuser"); + assert_string_equal(up.password, "cpassword"); + + reset_user_pass(&up); + + flags |=3D GET_USER_PASS_PASSWORD_ONLY; + snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt"); + /*FIXME: query_user_exec() called even though nothing queued */ + will_return(query_user_exec_builtin, true); + assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL)); + assert_true(up.defined); + assert_string_equal(up.username, "user"); + assert_string_equal(up.password, "fuser"); +} + +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_authfile_stdin), + cmocka_unit_test(test_get_user_pass_authfile_file), +}; + +int +main(void) +{ + return cmocka_run_group_tests(user_pass_tests, NULL, NULL); +} -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/468?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: I193aef06912f01426dd4ac298aadfab97dd75a35 Gerrit-Change-Number: 468 Gerrit-PatchSet: 8 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --QxKbhQJNuH0= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, flichtenheld, plaisthos. <= /p>

flichtenheld uploaded patch set #8 to this change.

<= p>View Ch= ange

The following approvals got outdated and were removed: Code-Review+2 by plaisthos, Code-Review-1 by cron2

The change is no l= onger submittable: Code-Review and checks~ChecksSubmitRule are unsatisfied = now.

test_user_pass: new UT for get_user_pass

UTs for basic function= ality, without management functions.

v2:
- add CMake support
= - add GHA support for both MSVC and mingw
v3:
- fix distcheck by ad= ding input/ directory to dist

Change-Id: I193aef06912f01426dd4ac298a= adfab97dd75a35
Signed-off-by: Frank Lichtenheld <frank@...2641...>=
---
M .github/workflows/build.yaml
M CMakeLists.txt
M src/open= vpn/misc.h
M src/openvpn/syshead.h
M tests/unit_tests/openvpn/Makefil= e.am
A tests/unit_tests/openvpn/input/user_only.txt
A tests/unit_test= s/openvpn/input/user_pass.txt
A tests/unit_tests/openvpn/test_user_pass.= c
8 files changed, 327 insertions(+), 2 deletions(-)

git pull =
ssh://gerrit.openvpn.net:29418/openvpn refs/changes/68/468/8
diff -=
-git a/.github/workflows/build.yaml b/.github/workflows/build.yamlindex cc98813..bc937e5 100644
--- a/.github/workflo= ws/build.yaml
+++ b/.github/workflows/build.yaml
= @@ -85,11 +85,13 @@
fail-fast: false matrix:
arch: [x86, x64]
<= span style=3D"color: hsl(0, 100%, 40%);">- test: [argv, auth_token, = buffer, cryptoapi, crypto, misc, ncp, packet_id, pkt, provider, ssl, tls_cr= ypt]
+ test: [= argv, auth_token, buffer, cryptoapi, crypto, misc, ncp, packet_id, pkt, pro= vider, ssl, tls_crypt, user_pass]

ru= ns-on: windows-latest
name: "mingw unittest ${{ m= atrix.test }} - ${{ matrix.arch }} - OSSL"
steps:=
+ - name: Check= out OpenVPN
+ = uses: actions/checkout@...2689...
- name: Retrieve m= ingw unittest
uses: actions/download-artifact@...1665...= 689...
with:
@@ -97,6 +99,8 @@
path: unittests
- name: Run = ${{ matrix.test }} unit test
run: ./unittests/test= _${{ matrix.test }}.exe
+ env:
+ = srcdir: "${{ github.workspace }}/tests/unit_tests/openvpn"=

ubuntu:
strategy= :
@@ -279,6 +283,7 @@
configureP= reset: win-${{ matrix.arch }}-release
buildPrese= t: win-${{ matrix.arch }}-release
testPreset: wi= n-${{ matrix.arch }}-release
+ testPresetAdditionalArgs: "['--output-on-failur= e']"

- uses: actions/uplo= ad-artifact@...2689...
with:
diff = --git a/CMakeLists.txt b/CMakeLists.txt
index 5e07d50..be55= c60 100644
--- a/CMakeLists.txt
+++ b/CMake= Lists.txt
@@ -604,6 +604,7 @@
&quo= t;test_pkt"
"test_provider"<= br> "test_ssl"
+ "test_user_pass"
= )

if (WIN32)
@@ = -662,6 +663,10 @@
# test_networking needs special = environment
if (NOT ${test_name} STREQUAL "te= st_networking")
add_test(${test_name} ${t= est_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=
+ ENVIRONMEN= T "srcdir=3D${_UT_SOURCE_DIR}")
endif ()=
add_executable(${test_name}
= tests/unit_tests/openvpn/${test_name}.c
@@ -782,6 += 787,15 @@
src/openvpn/base64.c
= )

+ target_sources(test_user_pass PRIVATE
+ tests/unit_tests/openvpn/mock_get_rando= m.c
+ tests/un= it_tests/openvpn/mock_win32_execve.c
+ src/openvpn/base64.c
+ src/openvpn/console.c
+ src/openvpn/env_set.c+ src/openvpn/run_comma= nd.c
+ )
+
if = (TARGET test_argv)
target_link_options(test_argv P= RIVATE -Wl,--wrap=3Dparse_line)
target_sources(tes= t_argv PRIVATE
diff --git a/src/openvpn/misc.h b/src/openvp= n/misc.h
index d51609d..5ae61b5 100644
--- = a/src/openvpn/misc.h
+++ b/src/openvpn/misc.h
@@ -118,12 +118,31 @@


#define GET_USE= R_PASS_INLINE_CREDS (1<<10) /* indicates that auth_file is actually = inline creds */

+/**
+ *= Retrieves the user credentials from various sources depending on the flags= .
+ *
+ * @param up The user_pass structur= e to store the retrieved credentials.

+ * @param auth_file The path to the authentication file. = Might be NULL.
+ * @p= aram prefix The prefix to prepend to user prompts.
+ * @param flags Additional flags to control = the behavior of the function.
+ * @param auth_challenge The authentication challenge string.
+ * @return true if the = user credentials were successfully retrieved, false otherwise.
+ */

bool get_us= er_pass_cr(struct user_pass *up,
con= st char *auth_file,
const char *pref= ix,
const unsigned int flags,=
const char *auth_challenge);

+/**
<= span style=3D"color: hsl(120, 100%, 40%);">+ * Retrieves the user credentia= ls from various sources depending on the flags.
+ *
+ * @param up The user_pass structure to store the retrieved cred= entials.
+ * @param a= uth_file The path to the authentication file. Might be NULL.
+ * @param prefix The prefix to pre= pend to user prompts.
+ * @param flags Additional flags to control the behavior of the function.=
+ * @return true if = the user credentials were successfully retrieved, false otherwise.+ */
static = inline bool
get_user_pass(struct user_pass *up,
= const char *auth_file,
diff --git a/sr= c/openvpn/syshead.h b/src/openvpn/syshead.h
index a021c91..= d2859fe 100644
--- a/src/openvpn/syshead.h
= +++ b/src/openvpn/syshead.h
@@ -524,4 +524,10 @@
= #define ENABLE_MEMSTATS
#endif

+#ifdef _MSC_VER=
+#ifndef PATH_MAX+#define PATH_MAX MAX_PATH

+#endif
+#endif
+
#endif /* ifndef SYSHEAD_H */
diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/un= it_tests/openvpn/Makefile.am
index f81a10f..88a694d 100644<= /span>
--- a/tests/unit_tests/openvpn/Makefile.am
= +++ b/tests/unit_tests/openvpn/Makefile.am
@@ -1,5 +1,7 @@<= /span>
AUTOMAKE_OPTIONS =3D foreign

<= span style=3D"color: hsl(120, 100%, 40%);">+EXTRA_DIST =3D input

= +
AM_TESTSUITE= _SUMMARY_HEADER =3D ' for $(PACKAGE_STRING) Unit-Tests'
<= span>

test_binaries=3D
@@ -9,7 +11,7 @@
endif

test_binaries +=3D= crypto_testdriver packet_id_testdriver auth_token_testdriver ncp_testdrive= r misc_testdriver \
- p= kt_testdriver ssl_testdriver
+ pkt_testdriver ssl_testdriver user_pass_testdriver

if HAVE_LD_WRAP_SUPPORT
if !WIN32
@@ -251,6 +253,22 @@
$(top_srcdir)/src/openv= pn/base64.c


+user_pass_testdriver_CFLAGS =3D @TEST_CFLAGS@ = \
+ -I$(top_srcdir)/i= nclude -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn
+user_pass_testdriver_LDFLAGS =3D = @TEST_LDFLAGS@

+
+user_pass_testdriver_SOU= RCES =3D test_user_pass.c mock_msg.c \
+ $(top_srcdir)/src/openvpn/buffer.c \
+ $(top_srcdir)/src/openvpn/console.c \=
+ $(top_srcdir)/src/= openvpn/env_set.c \
+= mock_win32_execve.c \
+ mock_get_random.c \
+ $(top_srcdir)/src/openvpn/platform.c \+ $(top_srcdir)/src/openvpn/w= in32-util.c \
+ $(top= _srcdir)/src/openvpn/base64.c
+
+ ncp_testdriver_CFLAGS =3D @TEST_CFLAGS@ \
-I$(t= op_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \=
$(OPTIONAL_CRYPTO_CFLAGS)
diff --git a/t= ests/unit_tests/openvpn/input/user_only.txt b/tests/unit_tests/openvpn/inpu= t/user_only.txt
new file mode 100644
index = 0000000..3ca0db3
--- /dev/null
+++ b/tests/= unit_tests/openvpn/input/user_only.txt
@@ -0,0 +1 @@=
+fuser
diff= --git a/tests/unit_tests/openvpn/input/user_pass.txt b/tests/unit_tests/op= envpn/input/user_pass.txt
new file mode 100644
index 0000000..31b38ef

--- /dev/null
++= + b/tests/unit_tests/openvpn/input/user_pass.txt
@@ -0,0 +1= ,2 @@
+fuser+fpassword
di= ff --git a/tests/unit_tests/openvpn/test_user_pass.c b/tests/unit_tests/ope= nvpn/test_user_pass.c
new file mode 100644
= index 0000000..ab4dfe4
--- /dev/null
+++ b/= tests/unit_tests/openvpn/test_user_pass.c
@@ -0,0 +1,260 @@=
+/*
+ * OpenVPN -- An application to sec= urely tunnel IP networks
+ * over a single UDP port, with support for SSL/TLS-based<= /span>
+ * sessi= on authentication and key exchange,
+ * packet encryption, packet authentication, an= d
+ * pac= ket compression.
+ *<= /span>
+ * Copyright (C) 20= 23 OpenVPN Inc <sales@...515...>
+ *
+ * This program is free software; you can redistribute it and/or modify<= /span>
+ * it under the ter= ms of the GNU General Public License as published by the
+ * Free Software Foundation, either v= ersion 2 of the License,
+ * or (at your option) any later version.
+ *
+ * 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, = Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef HAVE_CON= FIG_H
+#include "= ;config.h"
+#end= if
+
+#undef ENABLE_SYSTEMD
+

+#include "syshead.h"
+#include "manage.h"
<= span style=3D"color: hsl(120, 100%, 40%);">+

+#include <stdlib.h>
+#include <string.h>
+#include <setjmp.h>
+#include <cmocka.h>
=
+
+#include "misc.c"
+
+struct management *management; /* global */
+

+/* mocking */
+bool
+{
+ /= * Loop through configured query_user slots */
+ for (int i =3D 0; i < QUERY_USER_NUMSLOTS = && query_user[i].response !=3D NULL; i++)
+ {
+ check_expected(query_user[i].prompt);
+ strncpy(query_user[i].res= ponse, mock_ptr_type(char *), query_user[i].response_len);

+ }
+
+ return mock();
+}
+void+management_auth_failure(stru= ct management *man, const char *type, const char *reason)
+{
+ assert_true(0);
+}
+bool
+management_qu= ery_user_pass(struct management *man,
+ struct user_pass *up,+ c= onst char *type,
+ = const unsigned int flags,
+ const char *static= _challenge)
+{=
+ assert_true(0);=
+ return false;+}
+/* stubs for some unused functions instead of = pulling in too many dependencies */
+int
+= parse_line(const char *line, char **p, const int n, const char *file,
+ const int line= _num, int msglevel, struct gc_arena *gc)
+{
+}
+
+/* tooling */
+static void
+reset_user_pass(struct user_pass *up)
+{
+ up->defined =3D false;
+ up->token_defined =3D false;+ up->nocache =3D false= ;
+ strcpy(up->= username, "user");
+ strcpy(up->password, "password");
+}

+
+test_= get_user_pass_defined(void **state)
+{
+ = struct user_pass up =3D { 0 };
+ reset_user_pass(&up);
+ up.defined =3D true;
+ assert_true(get_user_pass_cr(&up, NULL,= "UT", 0, NULL));
+}
++static void
+test_get_user_pass_needok(void **stat= e)
+{
+ struct user_pass up =3D { 0 };<= /span>
+ reset_user_pass(= &up);
+ unsign= ed int flags =3D GET_USER_PASS_NEED_OK;
+
= + expect_string(query_user_exec_builtin, query_user[i].prompt, "NEE= D-OK|UT|user:");
+ will_return(query_user_exec_builtin, "");
+ will_return(query_user_exec_built= in, true);
+ /*FIX= ME: query_user_exec() called even though nothing queued */
+ will_return(query_user_exec_buil= tin, true);
+ asse= rt_true(get_user_pass_cr(&up, NULL, "UT", flags, NULL));
+ assert_true(up.defin= ed);
+ assert_stri= ng_equal(up.password, "ok");
+
+= reset_user_pass(&up);
+
+ expec= t_string(query_user_exec_builtin, query_user[i].prompt, "NEED-OK|UT|us= er:");
+ will= _return(query_user_exec_builtin, "cancel");
+ will_return(query_user_exec_builtin, = true);
+ /*FIXME: = query_user_exec() called even though nothing queued */
+ will_return(query_user_exec_builtin,= true);
+ assert_t= rue(get_user_pass_cr(&up, NULL, "UT", flags, NULL));+ assert_true(up.defined);=
+ assert_string_e= qual(up.password, "cancel");
+}
= +
+static void=
+test_get_user_pass_inline_= creds(void **state)
+= {
+ struct user_pa= ss up =3D { 0 };
+ = reset_user_pass(&up);
+ unsigned int flags =3D GET_USER_PASS_INLINE_CREDS;
+

+ /*FIXME: query_user_exec() called even though no= thing queued */
+ = will_return(query_user_exec_builtin, true);
+ assert_true(get_user_pass_cr(&up, "ius= er\nipassword", "UT", flags, NULL));
+ assert_true(up.defined);
+ assert_string_equal(up.usern= ame, "iuser");

+ assert_string_equal(up.password, "ipassword");+
+ reset_user_pass(&up);
+
+ expect_string(query_user_exec_builtin, query_user[i].= prompt, "Enter UT Password:");
+ will_return(query_user_exec_builtin, "cpasswo= rd");
+ will_= return(query_user_exec_builtin, true);
+ /* will try to retrieve missing password from stdin = */
+ assert_true(g= et_user_pass_cr(&up, "iuser", "UT", flags, NULL));<= /span>
+ assert_true(up.d= efined);
+ assert_= string_equal(up.username, "iuser");
+ assert_string_equal(up.password, "cpassw= ord");
+<= br>+ reset_user_pass(&up= );
+
+ flags |=3D GET_USER_PASS_PASSWOR= D_ONLY;
+ /*FIXME:= query_user_exec() called even though nothing queued */
+ will_return(query_user_exec_builtin= , true);
+ assert_= true(get_user_pass_cr(&up, "ipassword\n", "UT", fla= gs, NULL));
+ asse= rt_true(up.defined);
= + assert_string_equal(up.username, "user");
+ assert_string_equal(up.password, &= quot;ipassword");
+ reset_user_p= ass(&up);
+
+ flags |=3D GET_USER_P= ASS_PASSWORD_ONLY;
+ = expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter= UT Password:");
+ will_return(query_user_exec_builtin, "cpassword");+ will_return(query_user_e= xec_builtin, true);
+= /* will try to retrieve missing password from stdin */
+ assert_true(get_user_pass_cr(&am= p;up, "", "UT", flags, NULL));
+ assert_true(up.defined);
+ assert_string_equal(up.username= , "user");

= + assert_string_equal(up.password, "cpassword");
+}

+
+static void
+tes= t_get_user_pass_authfile_stdin(void **state)
+{
+ struct user_pass up =3D { 0 };
+ reset_user_pass(&up);
+ unsigned int flags =3D 0;
<= span style=3D"color: hsl(120, 100%, 40%);">+
+ expect_string(query_user_exec_builtin, query_u= ser[i].prompt, "Enter UT Username:");
+ expect_string(query_user_exec_builtin, quer= y_user[i].prompt, "Enter UT Password:");
+ will_return(query_user_exec_builtin, &qu= ot;cuser");
+ = will_return(query_user_exec_builtin, "cpassword");
+ will_return(query_user_exec_b= uiltin, true);
+ a= ssert_true(get_user_pass_cr(&up, "stdin", "UT", fla= gs, NULL));
+ asse= rt_true(up.defined);
= + assert_string_equal(up.username, "cuser");
+ 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, "Ente= r UT Password:");
+ will_return(query_user_= exec_builtin, true);
= + 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.passw= ord, "cpassword");

+}
++static void
+test_get_user_pass_authfile_file(voi= d **state)
+{<= br>+ struct user_pass up =3D= { 0 };
+ reset_us= er_pass(&up);
+ = unsigned int flags =3D 0;
+
+ const c= har *srcdir =3D getenv("srcdir");
+ assert_non_null(srcdir);
+ char authfile[PATH_MAX] =3D { 0 };
+
+ snprintf(authfile, PATH_MAX, "%s= /%s", srcdir, "input/user_pass.txt");
+ /*FIXME: query_user_exec() called eve= n though nothing queued */
+ will_return(query_user_exec_builtin, true);
+ assert_true(get_user_pass_cr(&up= , authfile, "UT", flags, NULL));
+ assert_true(up.defined);
+ assert_string_equal(up.username, &quo= t;fuser");
+ = assert_string_equal(up.password, "fpassword");
+
+ 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, &q= uot;Enter UT Password:");
+ will_return(query_user_exec_builtin, "cpassword");=
+ will_return(que= ry_user_exec_builtin, true);
+ /* will try to retrieve missing password from stdin */<= br>+ assert_true(get_user_pa= ss_cr(&up, authfile, "UT", flags, NULL));
+ assert_true(up.defined);
= + assert_string_equal(up.use= rname, "fuser");
+ assert_string_equal(up.password, "cpassword");<= br>+
+ reset_user_pass(&up);
+
+ flags |=3D GET_USER_PASS_PASSWORD_ONLY;
<= span style=3D"color: hsl(120, 100%, 40%);">+ snprintf(authfile, PATH_MAX= , "%s/%s", srcdir, "input/user_only.txt");

+ /*FIXME: query_user_exec() c= alled even though nothing queued */
+ will_return(query_user_exec_builtin, true);
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_true(get_user_pass_c= r(&up, authfile, "UT", flags, NULL));
+ assert_true(up.defined);
+ assert_string_equal(up.usern= ame, "user");
+ assert_string_equal(up.password, "fuser");
+}
+
+const struct CMUnitTest user_pass_tests[] =3D {
+ cmocka_unit_test(test_get_user_pass_d= efined),
+ cmocka_= unit_test(test_get_user_pass_needok),
+ cmocka_unit_test(test_get_user_pass_inline_creds),

+ cmocka_unit_test(t= est_get_user_pass_authfile_stdin),
+ cmocka_unit_test(test_get_user_pass_authfile_file),
+};
+
+int
+m= ain(void)
+{+ return cmocka_run_group_= tests(user_pass_tests, NULL, NULL);
+}

To view, visit change 468.= To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I193aef06912f01426dd4ac298aa= dfab97dd75a35
Gerrit-Change-Number: 468
Gerrit-PatchSet: 8
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
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-Attention: cron2 <gert@...1296...>= ;
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--QxKbhQJNuH0=--