Browse code

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>

Arne Schwabe authored on 2024/02/01 23:48:17
Showing 3 changed files
... ...
@@ -682,6 +682,10 @@ if (BUILD_TESTING)
682 682
 
683 683
         target_include_directories(${test_name} PRIVATE src/openvpn)
684 684
 
685
+        # for compat with IDEs like Clion that ignore the tests properties
686
+        # for the environment variable srcdir when running tests as fallback
687
+        target_compile_definitions(${test_name} PRIVATE "-DUNIT_TEST_SOURCEDIR=\"${CMAKE_SOURCE_DIR}/tests/unit_tests/openvpn\"")
688
+
685 689
         if (NOT ${test_name} STREQUAL "test_buffer")
686 690
             target_sources(${test_name} PRIVATE
687 691
                 src/openvpn/buffer.c
... ...
@@ -32,9 +32,36 @@
32 32
  * This has a openvpn prefix to avoid confusion with cmocka's unit_test_setup_*
33 33
  * methods
34 34
  */
35
-void
35
+static inline void
36 36
 openvpn_unit_test_setup()
37 37
 {
38 38
     assert_int_equal(setvbuf(stdout, NULL, _IONBF, BUFSIZ), 0);
39 39
     assert_int_equal(setvbuf(stderr, NULL, _IONBF, BUFSIZ), 0);
40 40
 }
41
+
42
+/**
43
+ * Helper function to get a file path from the unit test directory to open it
44
+ * or pass its path to another function. This function will first look for
45
+ * an environment variable or if failing that, will fall back to a hardcoded
46
+ * value from compile time if compiled with CMake.
47
+ *
48
+ * @param buf           buffer holding the path to the file
49
+ * @param bufsize       size of buf
50
+ * @param filename      name of the filename to retrieve relative to the
51
+ *                      unit test source directory
52
+ */
53
+void
54
+openvpn_test_get_srcdir_dir(char *buf, size_t bufsize, const char *filename)
55
+{
56
+    const char *srcdir = getenv("srcdir");
57
+
58
+#if defined(UNIT_TEST_SOURCEDIR)
59
+    if (!srcdir)
60
+    {
61
+        srcdir = UNIT_TEST_SOURCEDIR;
62
+    }
63
+#endif
64
+    assert_non_null(srcdir);
65
+
66
+    snprintf(buf, bufsize, "%s/%s", srcdir, filename);
67
+}
... ...
@@ -35,6 +35,7 @@
35 35
 #include <string.h>
36 36
 #include <setjmp.h>
37 37
 #include <cmocka.h>
38
+#include "test_common.h"
38 39
 
39 40
 #include "misc.c"
40 41
 
... ...
@@ -232,11 +233,9 @@ test_get_user_pass_authfile_file(void **state)
232 232
     reset_user_pass(&up);
233 233
     unsigned int flags = 0;
234 234
 
235
-    const char *srcdir = getenv("srcdir");
236
-    assert_non_null(srcdir);
237 235
     char authfile[PATH_MAX] = { 0 };
236
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_pass.txt" );
238 237
 
239
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_pass.txt");
240 238
     /*FIXME: query_user_exec() called even though nothing queued */
241 239
     will_return(query_user_exec_builtin, true);
242 240
     assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
... ...
@@ -246,7 +245,7 @@ test_get_user_pass_authfile_file(void **state)
246 246
 
247 247
     reset_user_pass(&up);
248 248
 
249
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
249
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");
250 250
     expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT Password:");
251 251
     will_return(query_user_exec_builtin, "cpassword");
252 252
     will_return(query_user_exec_builtin, true);
... ...
@@ -259,7 +258,7 @@ test_get_user_pass_authfile_file(void **state)
259 259
     reset_user_pass(&up);
260 260
 
261 261
     flags |= GET_USER_PASS_PASSWORD_ONLY;
262
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
262
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");
263 263
     /*FIXME: query_user_exec() called even though nothing queued */
264 264
     will_return(query_user_exec_builtin, true);
265 265
     assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
... ...
@@ -279,5 +278,6 @@ const struct CMUnitTest user_pass_tests[] = {
279 279
 int
280 280
 main(void)
281 281
 {
282
+    openvpn_unit_test_setup();
282 283
     return cmocka_run_group_tests(user_pass_tests, NULL, NULL);
283 284
 }