Skip to content

Commit

Permalink
added testcase for run directory
Browse files Browse the repository at this point in the history
  • Loading branch information
schubi2 committed Oct 4, 2023
1 parent 0d203f7 commit 2ed14c1
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ set(TESTS tst-filedoesnotexit1
tst-getconfdirs6
tst-getconfdirs7
tst-getconfdirs8
tst-getconfdirs9
tst-without-suffix
tst-econf_errstring1
tst-setgetvalues1
Expand Down
2 changes: 2 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/tst-getconfdirs9-data/run/getconfdir.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KEY1=etc
ETC=true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KEY1=etcconfd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OVERRIDE=true
1 change: 1 addition & 0 deletions tests/tst-getconfdirs9-data/run/getconfdir.conf.d/9-broken
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OVERRIDE=broken
2 changes: 2 additions & 0 deletions tests/tst-getconfdirs9-data/usr/etc/getconfdir.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KEY1=usretc
USRETC=true
79 changes: 79 additions & 0 deletions tests/tst-getconfdirs9.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include "libeconf.h"

/* Test case:
Test the systemd like behavior:
/usr/etc/getconfdir.conf exists
/run/getconfdir.conf exists
/etc/getconfidr.conf.d/<files>.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;
}

0 comments on commit 2ed14c1

Please sign in to comment.