From: Baptiste Daroussin Date: Wed, 3 Nov 2021 10:51:10 +0000 (+0100) Subject: testsuite: add tests for statctrl and mlmmj_list* X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1dfab72cbcc6b4dc54fdebf3f1668f413cdb0b85;p=thirdparty%2Fmlmmj.git testsuite: add tests for statctrl and mlmmj_list* --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 49bc19b5..24d0fcb8 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -5,7 +5,8 @@ check_PROGRAMS = mlmmj mlmmj_SOURCES = mlmmj.c $(top_srcdir)/src/prepstdreply.c $(top_srcdir)/include/prepstdreply.h \ $(top_srcdir)/src/memory.c $(top_srcdir)/src/controls.c $(top_srcdir)/src/utils.c \ $(top_srcdir)/src/strgen.c $(top_srcdir)/src/random-int.c $(top_srcdir)/src/readn.c \ - $(top_srcdir)/src/log_error.c $(top_srcdir)/src/chomp.c $(top_srcdir)/src/mygetline.c $(top_srcdir)/src/unistr.c + $(top_srcdir)/src/log_error.c $(top_srcdir)/src/chomp.c $(top_srcdir)/src/mygetline.c $(top_srcdir)/src/unistr.c \ + $(top_srcdir)/src/mlmmj.c $(top_srcdir)/src/getlistdelim.c mlmmj_CFLAGS = @ATF_CFLAGS@ -g -Wall -pedantic -Wsign-compare -DDEFAULTTEXTDIR='"@textlibdir@"' -I$(top_srcdir)/include mlmmj_LDADD = @ATF_LIBS@ diff --git a/tests/mlmmj.c b/tests/mlmmj.c index 7eb9bde5..7797ebb8 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -21,13 +21,66 @@ */ #include -#include #include #include +#include +#include +#include +#include +#include + +#include "mlmmj.h" +#include "wrappers.h" +#include "controls.h" ATF_TC(random_int); +ATF_TC(statctrl); +ATF_TC(mlmmj_list); ATF_TC_HEAD(random_int, tc) { } +ATF_TC_HEAD(statctrl, tc) { } +ATF_TC_HEAD(mlmmj_list, tc) { } + +#ifndef NELEM +#define NELEM(array) (sizeof(array) / sizeof((array)[0])) +#endif + +const char *listdir[] = { + "incoming", + "queue", + "queue/discarded", + "archive", + "text", + "subconf", + "unsubconf", + "bounce", + "control", + "moderation", + "subscribers.d", + "digesters.d", + "requeue", + "nomailsubs.d" +}; + +void +init_ml(bool complete) +{ + int fd; + ATF_CHECK(mkdir("list", 00755) != -1); + fd = open("list", O_DIRECTORY); + ATF_CHECK(fd != -1); + + for (uintmax_t i = 0; i < NELEM(listdir); i++) { + ATF_CHECK(mkdirat(fd, listdir[i], 00755) != -1); + } + + if (complete) { + FILE *f = fopen("list/control/listaddress", "w+"); + ATF_CHECK(f != NULL); + fputs("test@test", f); + fclose(f); + } +} ATF_TC_BODY(random_int, tc) { @@ -36,8 +89,77 @@ ATF_TC_BODY(random_int, tc) ATF_CHECK_MSG(val < INT_MAX, "Invalid integer"); } +ATF_TC_BODY(statctrl, tc) +{ + struct mlmmj_list list; + + init_ml(true); + mlmmj_list_init(&list); + list.dir = "list"; + ATF_CHECK(mlmmj_list_open(&list)); + ATF_CHECK(!statctrl(&list, "nonexistent")); + fclose(fopen("list/control/existent", "w")); + ATF_CHECK(statctrl(&list, "existent")); +} + +ATF_TC_BODY(mlmmj_list, tc) +{ + struct mlmmj_list list; + FILE *f = fopen("err.log", "w+"); + ATF_CHECK(f != NULL); + mlmmj_list_init(&list); + list.dir = "list"; + err_set_file(f); + ATF_CHECK(mlmmj_list_open(&list) == false); + fflush(f); + if (!atf_utils_compare_file("err.log", "mlmmj: Unable to open 'list'\n")) { + atf_utils_cat_file("err.log", "err.log> "); + atf_tc_fail("Missing error message"); + } + + rewind(f); + ftruncate(fileno(f), 0); + init_ml(false); + mlmmj_list_init(&list); + list.dir = "list"; + ATF_CHECK(mlmmj_list_open(&list) == false); + fflush(f); + if (!atf_utils_compare_file("err.log", "mlmmj: Missing list address\n")) { + atf_utils_cat_file("err.log", "err.log> "); + atf_tc_fail("Missing error message for missing listaddress"); + } + + rewind(f); + ftruncate(fileno(f), 0); + FILE *fp = fopen("list/control/listaddress", "w+"); + ATF_CHECK(fp != NULL); + fputs("test", fp); + fflush(fp); + mlmmj_list_init(&list); + list.dir = "list"; + ATF_CHECK(mlmmj_list_open(&list) == false); + fflush(f); + if (!atf_utils_compare_file("err.log", "mlmmj: test: is not a valid mailing list address, missing '@'\n")) { + atf_utils_cat_file("err.log", "err.log> "); + atf_tc_fail("Missing invalid address error"); + } + + rewind(fp); + ftruncate(fileno(fp), 0); + fputs("test@test", fp); + fclose(fp); + rewind(f); + ftruncate(fileno(f), 0); + mlmmj_list_init(&list); + list.dir = "list"; + ATF_CHECK(mlmmj_list_open(&list) == true); + fclose(f); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, random_int); + ATF_TP_ADD_TC(tp, mlmmj_list); + ATF_TP_ADD_TC(tp, statctrl); return (atf_no_error()); }