aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests
diff options
context:
space:
mode:
authorArne Schwabe2024-02-01 15:48:17 +0100
committerGert Doering2024-02-01 18:22:02 +0100
commitbb0849db20747dc4acea394c4db807a47cbc526b (patch)
tree5045a90eb3442128716cc673f4d72991a1535de1 /tests/unit_tests
parente1f8c599aeb840909f5ea8e9ae0bc4dab5bc7deb (diff)
downloadopenvpn-bb0849db20747dc4acea394c4db807a47cbc526b.zip
openvpn-bb0849db20747dc4acea394c4db807a47cbc526b.tar.gz
Allow unit tests to fall back to hard coded location
Settings the environment variable required for running unit tests is tiresome in my IDE (Clion). So allow unit tests to fall back to a hard coded location in case the environment variable is not set. Change-Id: Ide72b81f497088dd0fd2cdcfff83cbce5b48f145 Acked-by: Frank Lichtenheld <frank@lichtenheld.com> Message-Id: <20240201144817.188884-1-frank@lichtenheld.com> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg28161.html Signed-off-by: Gert Doering <gert@greenie.muc.de>
Diffstat (limited to 'tests/unit_tests')
-rw-r--r--tests/unit_tests/openvpn/test_common.h29
-rw-r--r--tests/unit_tests/openvpn/test_user_pass.c10
2 files changed, 33 insertions, 6 deletions
diff --git a/tests/unit_tests/openvpn/test_common.h b/tests/unit_tests/openvpn/test_common.h
index 50e16d6..f219e93 100644
--- a/tests/unit_tests/openvpn/test_common.h
+++ b/tests/unit_tests/openvpn/test_common.h
@@ -32,9 +32,36 @@
* This has a openvpn prefix to avoid confusion with cmocka's unit_test_setup_*
* methods
*/
-void
+static inline void
openvpn_unit_test_setup()
{
assert_int_equal(setvbuf(stdout, NULL, _IONBF, BUFSIZ), 0);
assert_int_equal(setvbuf(stderr, NULL, _IONBF, BUFSIZ), 0);
}
+
+/**
+ * Helper function to get a file path from the unit test directory to open it
+ * or pass its path to another function. This function will first look for
+ * an environment variable or if failing that, will fall back to a hardcoded
+ * value from compile time if compiled with CMake.
+ *
+ * @param buf buffer holding the path to the file
+ * @param bufsize size of buf
+ * @param filename name of the filename to retrieve relative to the
+ * unit test source directory
+ */
+void
+openvpn_test_get_srcdir_dir(char *buf, size_t bufsize, const char *filename)
+{
+ const char *srcdir = getenv("srcdir");
+
+#if defined(UNIT_TEST_SOURCEDIR)
+ if (!srcdir)
+ {
+ srcdir = UNIT_TEST_SOURCEDIR;
+ }
+#endif
+ assert_non_null(srcdir);
+
+ snprintf(buf, bufsize, "%s/%s", srcdir, filename);
+}
diff --git a/tests/unit_tests/openvpn/test_user_pass.c b/tests/unit_tests/openvpn/test_user_pass.c
index 277cb1d..bd4eb1f 100644
--- a/tests/unit_tests/openvpn/test_user_pass.c
+++ b/tests/unit_tests/openvpn/test_user_pass.c
@@ -35,6 +35,7 @@
#include <string.h>
#include <setjmp.h>
#include <cmocka.h>
+#include "test_common.h"
#include "misc.c"
@@ -232,11 +233,9 @@ test_get_user_pass_authfile_file(void **state)
reset_user_pass(&up);
unsigned int flags = 0;
- const char *srcdir = getenv("srcdir");
- assert_non_null(srcdir);
char authfile[PATH_MAX] = { 0 };
+ openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_pass.txt" );
- 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));
@@ -246,7 +245,7 @@ test_get_user_pass_authfile_file(void **state)
reset_user_pass(&up);
- snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
+ 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");
will_return(query_user_exec_builtin, true);
@@ -259,7 +258,7 @@ test_get_user_pass_authfile_file(void **state)
reset_user_pass(&up);
flags |= GET_USER_PASS_PASSWORD_ONLY;
- snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
+ openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "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));
@@ -279,5 +278,6 @@ const struct CMUnitTest user_pass_tests[] = {
int
main(void)
{
+ openvpn_unit_test_setup();
return cmocka_run_group_tests(user_pass_tests, NULL, NULL);
}