From: "cron2 (Code Review)" Date: Thu, 1 Feb 2024 19:25:46 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <10da90b3335c45bb02ce44140d6a53d5fc256da2-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="uJs0lbkoitA="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: Allow unit tests to fall back to hard coded location List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --uJs0lbkoitA= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#2) to the change originally created by= plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/509?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by flichtenheld Change subject: Allow unit tests to fall back to hard coded location ...................................................................... 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 Message-Id: <20240201144817.188884-1-frank@...2641...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg28= 161.html Signed-off-by: Gert Doering --- M CMakeLists.txt M tests/unit_tests/openvpn/test_common.h M tests/unit_tests/openvpn/test_user_pass.c 3 files changed, 37 insertions(+), 6 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/09/509/2 diff --git a/CMakeLists.txt b/CMakeLists.txt index be55c60..fdd2b01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -682,6 +682,10 @@ target_include_directories(${test_name} PRIVATE src/openvpn) + # for compat with IDEs like Clion that ignore the tests properties + # for the environment variable srcdir when running tests as fallba= ck + target_compile_definitions(${test_name} PRIVATE "-DUNIT_TEST_SOURC= EDIR=3D\"${CMAKE_SOURCE_DIR}/tests/unit_tests/openvpn\"") + if (NOT ${test_name} STREQUAL "test_buffer") target_sources(${test_name} PRIVATE src/openvpn/buffer.c diff --git a/tests/unit_tests/openvpn/test_common.h b/tests/unit_tests/open= vpn/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_se= tup_* * 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 hardcod= ed + * 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 *filenam= e) +{ + const char *srcdir =3D getenv("srcdir"); + +#if defined(UNIT_TEST_SOURCEDIR) + if (!srcdir) + { + srcdir =3D 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/o= penvpn/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 #include #include +#include "test_common.h" #include "misc.c" @@ -232,11 +233,9 @@ 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 }; + 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 @@ 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 @@ reset_user_pass(&up); flags |=3D 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 @@ int main(void) { + openvpn_unit_test_setup(); return cmocka_run_group_tests(user_pass_tests, NULL, NULL); } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/509?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: Ide72b81f497088dd0fd2cdcfff83cbce5b48f145 Gerrit-Change-Number: 509 Gerrit-PatchSet: 2 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --uJs0lbkoitA= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #2 to the change originally cr= eated by plaisthos.

View Change

The following approvals got outdated= and were removed: Code-Review+2 by flichtenheld

Allow unit tests to fall back to hard coded l=
ocation

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.
<= br>Change-Id: Ide72b81f497088dd0fd2cdcfff83cbce5b48f145
Acked-by: Frank = Lichtenheld <frank@...2641...>
Message-Id: <20240201144817.1888= 84-1-frank@...2641...>
URL: https://www.mail-archive.com/openvpn-deve= l@lists.sourceforge.net/msg28161.html
Signed-off-by: Gert Doering <ge= rt@...1296...>
---
M CMakeLists.txt
M tests/unit_tests/openvpn/= test_common.h
M tests/unit_tests/openvpn/test_user_pass.c
3 files cha= nged, 37 insertions(+), 6 deletions(-)

git pull ssh://gerrit.open=
vpn.net:29418/openvpn refs/changes/09/509/2
diff --git a/CMakeLists=
.txt b/CMakeLists.txt
index be55c60..fdd2b01 100644<= br>--- a/CMakeLists.txt
+++ b/CMakeLists.txt@@ -682,6 +682,10 @@

tar= get_include_directories(${test_name} PRIVATE src/openvpn)
=
+ # for compa= t with IDEs like Clion that ignore the tests properties
+ # for the environment variable = srcdir when running tests as fallback
+ target_compile_definitions(${test_name} PRIVATE &= quot;-DUNIT_TEST_SOURCEDIR=3D\"${CMAKE_SOURCE_DIR}/tests/unit_tests/op= envpn\"")
+=
if (NOT ${test_name} STREQUAL "test_buffer&q= uot;)
target_sources(${test_name} PRIVATE
src/openvpn/buffer.c
diff --gi= t 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_t= ests/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_t= est_setup()
{
assert_int_equal(setvbu= f(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 t= o 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 CMak= e.
+ *
+ * @param buf buffer hol= ding the path to the file
+ * @param bufsize size of buf
+ * @param filename name of the filename to retri= eve relative to the
+= * unit test source directory
+ */
+void
+= openvpn_test_get_srcdir_dir(char *buf, size_t bufsize, const char *filename= )
+{
+ const char *srcdir =3D getenv(&q= uot;srcdir");
+<= /span>
+#if defined(UNIT_TES= T_SOURCEDIR)
+ if = (!srcdir)
+ {

+ srcdir =3D UNIT_= TEST_SOURCEDIR;
+ = }
+#endif
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_non_null(srcdir);
+
+ snprintf(buf, bufsize, "%s/%s&qu= ot;, 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_p= ass.c
+++ b/tests/unit_tests/openvpn/test_user_pass.c
@@ -35,6 +35,7 @@
#include <string.h>

#include <setjmp.h>
#include <cmock= a.h>
+#include &qu= ot;test_common.h"

#include "mi= sc.c"

@@ -232,11 +233,9 @@ reset_user_pass(&up);
unsigned int fla= gs =3D 0;

- const char *srcdir =3D getenv("srcdir");
- assert_non_null(srcdir);<= br> char authfile[PATH_MAX] =3D { 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, NU= LL));
@@ -246,7 +245,7 @@

reset_user_pass(&up);

- snprintf(authfile, PATH_MAX, "%s/%s&qu= ot;, srcdir, "input/user_only.txt");
+ openvpn_test_get_srcdir_dir(authfile, PATH_M= AX, "input/user_only.txt");
expect_string(qu= ery_user_exec_builtin, query_user[i].prompt, "Enter UT Password:"= );
will_return(query_user_exec_builtin, "cpasswor= d");
will_return(query_user_exec_builtin, true);<= /span>
@@ -259,7 +258,7 @@
reset_user_pass(&a= mp;up);

flags |=3D GET_USER_PASS_PAS= SWORD_ONLY;
- snprin= tf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt= ");
+ openvpn= _test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");<= /span>
/*FIXME: query_user_exec() called even though nothing = queued */
will_return(query_user_exec_builtin, true);<= /span>
assert_true(get_user_pass_cr(&up, authfile, "= UT", flags, NULL));
@@ -279,5 +278,6 @@
int

main(void)
{
+ openvpn_unit_test_setup();
= return cmocka_run_group_tests(user_pass_tests, NULL, NULL);

}

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

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ide72b81f497088dd0fd2cdcfff8= 3cbce5b48f145
Gerrit-Change-Number: 509
Gerrit-PatchSet: 2
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: newpatchset
--uJs0lbkoitA=--