From 2ed14c126456fbd2e109116b96129e544e7f72af Mon Sep 17 00:00:00 2001 From: Stefan Schubert Date: Wed, 4 Oct 2023 18:10:49 +0200 Subject: [PATCH] added testcase for run directory --- tests/CMakeLists.txt | 1 + tests/meson.build | 2 + .../tst-getconfdirs9-data/run/getconfdir.conf | 2 + .../run/getconfdir.conf.d/1-override.conf | 1 + .../run/getconfdir.conf.d/5-override.conf | 1 + .../run/getconfdir.conf.d/9-broken | 1 + .../usr/etc/getconfdir.conf | 2 + tests/tst-getconfdirs9.c | 79 +++++++++++++++++++ 8 files changed, 89 insertions(+) create mode 100644 tests/tst-getconfdirs9-data/run/getconfdir.conf create mode 100644 tests/tst-getconfdirs9-data/run/getconfdir.conf.d/1-override.conf create mode 100644 tests/tst-getconfdirs9-data/run/getconfdir.conf.d/5-override.conf create mode 100644 tests/tst-getconfdirs9-data/run/getconfdir.conf.d/9-broken create mode 100644 tests/tst-getconfdirs9-data/usr/etc/getconfdir.conf create mode 100644 tests/tst-getconfdirs9.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9f92074..19ddc88 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -50,6 +50,7 @@ set(TESTS tst-filedoesnotexit1 tst-getconfdirs6 tst-getconfdirs7 tst-getconfdirs8 + tst-getconfdirs9 tst-without-suffix tst-econf_errstring1 tst-setgetvalues1 diff --git a/tests/meson.build b/tests/meson.build index d4e6ac8..205c9d1 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -70,6 +70,8 @@ tst_getconfdirs7_exe = executable('tst-getconfdirs7', 'tst-getconfdirs7.c', c_ar test('tst-getconfdirs7', tst_getconfdirs7_exe) tst_getconfdirs8_exe = executable('tst-getconfdirs8', 'tst-getconfdirs8.c', c_args: ['-DSUFFIX="conf"', test_args], dependencies : libeconf_dep) test('tst-getconfdirs8', tst_getconfdirs8_exe) +tst_getconfdirs9_exe = executable('tst-getconfdirs9', 'tst-getconfdirs9.c', c_args: test_args, dependencies : libeconf_dep) +test('tst-getconfdirs9', tst_getconfdirs9_exe) tst_parse_error_exe = executable('tst-parse-error', 'tst-parse-error.c', c_args: test_args, dependencies : libeconf_dep) test('tst-parse-error', tst_parse_error_exe) diff --git a/tests/tst-getconfdirs9-data/run/getconfdir.conf b/tests/tst-getconfdirs9-data/run/getconfdir.conf new file mode 100644 index 0000000..bb93891 --- /dev/null +++ b/tests/tst-getconfdirs9-data/run/getconfdir.conf @@ -0,0 +1,2 @@ +KEY1=etc +ETC=true diff --git a/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/1-override.conf b/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/1-override.conf new file mode 100644 index 0000000..f6e4831 --- /dev/null +++ b/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/1-override.conf @@ -0,0 +1 @@ +KEY1=etcconfd diff --git a/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/5-override.conf b/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/5-override.conf new file mode 100644 index 0000000..8678608 --- /dev/null +++ b/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/5-override.conf @@ -0,0 +1 @@ +OVERRIDE=true diff --git a/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/9-broken b/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/9-broken new file mode 100644 index 0000000..50f8464 --- /dev/null +++ b/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/9-broken @@ -0,0 +1 @@ +OVERRIDE=broken diff --git a/tests/tst-getconfdirs9-data/usr/etc/getconfdir.conf b/tests/tst-getconfdirs9-data/usr/etc/getconfdir.conf new file mode 100644 index 0000000..dce43a4 --- /dev/null +++ b/tests/tst-getconfdirs9-data/usr/etc/getconfdir.conf @@ -0,0 +1,2 @@ +KEY1=usretc +USRETC=true diff --git a/tests/tst-getconfdirs9.c b/tests/tst-getconfdirs9.c new file mode 100644 index 0000000..86bbde7 --- /dev/null +++ b/tests/tst-getconfdirs9.c @@ -0,0 +1,79 @@ +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include + +#include "libeconf.h" + +/* Test case: + Test the systemd like behavior: + /usr/etc/getconfdir.conf exists + /run/getconfdir.conf exists + /etc/getconfidr.conf.d/.conf exists + + libeconf should ignore /usr/etc/getconfdir.conf, as this contains +*/ + +static int +check_key(econf_file *key_file, char *key, char *expected_val) +{ + char *val = NULL; + econf_err error = econf_getStringValue (key_file, "", key, &val); + if (expected_val == NULL) + { + if (val == NULL) + return 0; + + fprintf (stderr, "ERROR: %s has value \"%s\"\n", key, val); + return 1; + } + if (val == NULL || strlen(val) == 0) + { + fprintf (stderr, "ERROR: %s returns nothing! (%s)\n", key, + econf_errString(error)); + return 1; + } + if (strcmp (val, expected_val) != 0) + { + fprintf (stderr, "ERROR: %s is not \"%s\"\n", key, expected_val); + return 1; + } + + printf("Ok: %s=%s\n", key, val); + free (val); + return 0; +} + +int +main(void) +{ + econf_file *key_file = NULL; + int retval = 0; + econf_err error; + + error = econf_readDirs (&key_file, + TESTSDIR"tst-getconfdirs1-data/usr/etc", + TESTSDIR"tst-getconfdirs1-data/etc", + "getconfdir", "conf", "=", "#"); + if (error) + { + fprintf (stderr, "ERROR: econf_readDirs: %s\n", + econf_errString(error)); + return 1; + } + + if (check_key(key_file, "KEY1", "etcconfd") != 0) + retval = 1; + if (check_key(key_file, "USRETC", NULL) != 0) + retval = 1; + if (check_key(key_file, "ETC", "true") != 0) + retval = 1; + if (check_key(key_file, "OVERRIDE", "true") != 0) + retval = 1; + + econf_free (key_file); + + return retval; +}