From 66cef0fe4754cc33fa3b9f8ace3e3ef4e7f662c7 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 31 Jan 2025 20:21:32 +0100 Subject: Test the testable aspects of libenv_get_complete_list and print the variable list for human verification MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 1 + Makefile | 14 +++++--- libenv_get_complete_list.c | 2 +- test-variables.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 test-variables.c diff --git a/.gitignore b/.gitignore index c2c489c..ce38fe3 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ *.gcno *.gcda /test +/test-variables diff --git a/Makefile b/Makefile index e94edb7..f7db511 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.POSIX: +>.POSIX: CONFIGFILE = config.mk include $(CONFIGFILE) @@ -65,10 +65,11 @@ MAN7 =\ LOBJ = $(OBJ:.o=.lo) -all: libenv.a libenv.$(LIBEXT) test +all: libenv.a libenv.$(LIBEXT) test test-variables $(OBJ): $(HDR) $(LOBJ): $(HDR) test.o: libenv_get_complete_list.c $(HDR) +test-variables.o: libenv.h .c.o: $(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS) @@ -87,8 +88,12 @@ libenv.$(LIBEXT): $(LOBJ) test: test.o $(OBJ_TESTABLE) $(CC) -o $@ test.o $(OBJ_TESTABLE) $(LDFLAGS) -check: test +test-variables: test-variables.o libenv.a + $(CC) -o $@ test-variables.o libenv.a $(LDFLAGS) + +check: test test-variables $(CHECK_PREFIX) ./test + $(CHECK_PREFIX) ./test-variables > /dev/null install: libenv.a libenv.$(LIBEXT) mkdir -p -- "$(DESTDIR)$(PREFIX)/lib" @@ -115,7 +120,8 @@ uninstall: clean: -rm -f -- *.o *.a *.lo *.su *.so *.so.* *.dll *.dylib - -rm -f -- *.gch *.gcov *.gcno *.gcda *.$(LIBEXT) test + -rm -f -- *.gch *.gcov *.gcno *.gcda *.$(LIBEXT) + -rm -f -- test test-variables .SUFFIXES: .SUFFIXES: .lo .o .c diff --git a/libenv_get_complete_list.c b/libenv_get_complete_list.c index e40ed91..f2d63c0 100644 --- a/libenv_get_complete_list.c +++ b/libenv_get_complete_list.c @@ -51,7 +51,7 @@ DECLARE(classes__exec__path, _(LIBENV_EXEC) | SU_SAFE | LOGIN_UNSAFE | PATH); #define V(S) {S, CLASSES} -static struct libenv_variable list[] = { /* TODO validate */ +static struct libenv_variable list[] = { #define CLASSES classes__display V("DISPLAY"), diff --git a/test-variables.c b/test-variables.c new file mode 100644 index 0000000..3d5b7c5 --- /dev/null +++ b/test-variables.c @@ -0,0 +1,84 @@ +/* See LICENSE file for copyright and license details. */ +#include "libenv.h" +#include /* TODO improve output */ +#include + + +#define ELEMSOF(A) (sizeof(A) / sizeof(*(A))) + + +static const char *class_names[] = { + "LIBENV_END", + "display", + "display-config", + "desktop", + "terminal-caps", + "local-terminal", + "locale", + "timezone", + "session", + "theme-config", + "theme-plugin", + "internet", + "audio", + "user", + "applications", + "preferences", + "system-paths", + "user-paths", + "pwd", + "exec", + "path", + "remote-unsafe", + "remote-safe", + "login-unsafe", + "login-safe", + "su-unsafe", + "su-safe" +}; + + +int +main(void) +{ + const struct libenv_variable *vars; + size_t nclasswords1 = 0, nclasswords2 = 1, nclasswords; + size_t count1 = 0, count2 = 1, count; + size_t i, j; + + assert(vars = libenv_get_complete_list(NULL, NULL)); + assert(vars == libenv_get_complete_list(&nclasswords1, NULL)); + assert(vars == libenv_get_complete_list(NULL, &count1)); + assert(vars == libenv_get_complete_list(&nclasswords2, &count2)); + assert(vars == libenv_get_complete_list(&nclasswords, &count)); + + assert(nclasswords1 == nclasswords2); + assert(nclasswords1 == nclasswords); + assert(count1 == count2); + assert(count1 == count); + + assert(!vars[count].name); + assert(!vars[count].classes); + + assert(nclasswords == (ELEMSOF(class_names) + 63) / 64); + + for (i = 0; i < count; i++) { + const char *prefix = " "; + + assert(vars[i].name); + assert(*vars[i].name); + assert(vars[i].classes); + assert(!(vars[i].classes[0] & 1)); /* LIBENV_END */ + + printf("%s:", vars[i].name); + for (j = 0; j < ELEMSOF(class_names); j++) { + if ((vars[i].classes[j / 64] >> (j % 64)) & 1) { + printf("%s%s", prefix, class_names[j]); + prefix = ", "; + } + } + printf("\n"); + } + + return 0; +} -- cgit v1.2.3-70-g09d2