Skip to content

Commit

Permalink
test_user_pass: new UT for get_user_pass
Browse files Browse the repository at this point in the history
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 <[email protected]>
Acked-by: Arne Schwabe <[email protected]>
Message-Id: <[email protected]>
URL: https://www.mail-archive.com/[email protected]/msg28138.html
Signed-off-by: Gert Doering <[email protected]>
  • Loading branch information
flichtenheld authored and cron2 committed Jan 29, 2024
1 parent 7869617 commit b9696ff
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 2 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,22 @@ jobs:
fail-fast: false
matrix:
arch: [x86, x64]
test: [argv, auth_token, buffer, cryptoapi, crypto, misc, ncp, packet_id, pkt, provider, ssl, tls_crypt]
test: [argv, auth_token, buffer, cryptoapi, crypto, misc, ncp, packet_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@v3
- name: Retrieve mingw unittest
uses: actions/download-artifact@v3
with:
name: openvpn-mingw-${{ matrix.arch }}-tests
path: unittests
- name: Run ${{ matrix.test }} unit test
run: ./unittests/test_${{ matrix.test }}.exe
env:
srcdir: "${{ github.workspace }}/tests/unit_tests/openvpn"

ubuntu:
strategy:
Expand Down Expand Up @@ -279,6 +283,7 @@ jobs:
configurePreset: win-${{ matrix.arch }}-release
buildPreset: win-${{ matrix.arch }}-release
testPreset: win-${{ matrix.arch }}-release
testPresetAdditionalArgs: "['--output-on-failure']"

- uses: actions/upload-artifact@v3
with:
Expand Down
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ if (BUILD_TESTING)
"test_pkt"
"test_provider"
"test_ssl"
"test_user_pass"
)

if (WIN32)
Expand Down Expand Up @@ -662,6 +663,10 @@ if (BUILD_TESTING)
# 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_tests/openvpn)
set_tests_properties(${test_name} PROPERTIES
ENVIRONMENT "srcdir=${_UT_SOURCE_DIR}")
endif ()
add_executable(${test_name}
tests/unit_tests/openvpn/${test_name}.c
Expand Down Expand Up @@ -782,6 +787,15 @@ if (BUILD_TESTING)
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=parse_line)
target_sources(test_argv PRIVATE
Expand Down
19 changes: 19 additions & 0 deletions src/openvpn/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,31 @@ struct static_challenge_info {};

#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 flags.
*
* @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 flags.
*
* @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,
Expand Down
6 changes: 6 additions & 0 deletions src/openvpn/syshead.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,10 @@ socket_defined(const socket_descriptor_t sd)
#define ENABLE_MEMSTATS
#endif

#ifdef _MSC_VER
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#endif

#endif /* ifndef SYSHEAD_H */
20 changes: 19 additions & 1 deletion tests/unit_tests/openvpn/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
AUTOMAKE_OPTIONS = foreign

EXTRA_DIST = input

AM_TESTSUITE_SUMMARY_HEADER = ' for $(PACKAGE_STRING) Unit-Tests'

test_binaries=
Expand All @@ -9,7 +11,7 @@ test_binaries += argv_testdriver buffer_testdriver
endif

test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver ncp_testdriver misc_testdriver \
pkt_testdriver ssl_testdriver
pkt_testdriver ssl_testdriver user_pass_testdriver

if HAVE_LD_WRAP_SUPPORT
if !WIN32
Expand Down Expand Up @@ -251,6 +253,22 @@ auth_token_testdriver_SOURCES = test_auth_token.c mock_msg.c \
$(top_srcdir)/src/openvpn/base64.c


user_pass_testdriver_CFLAGS = @TEST_CFLAGS@ \
-I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn
user_pass_testdriver_LDFLAGS = @TEST_LDFLAGS@

user_pass_testdriver_SOURCES = 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 = @TEST_CFLAGS@ \
-I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \
$(OPTIONAL_CRYPTO_CFLAGS)
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/openvpn/input/user_only.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fuser
2 changes: 2 additions & 0 deletions tests/unit_tests/openvpn/input/user_pass.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fuser
fpassword
Loading

0 comments on commit b9696ff

Please sign in to comment.