*/
#include <atf-c.h>
-#include <wrappers.h>
#include <inttypes.h>
#include <sys/limits.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <err.h>
+#include <stdio.h>
+
+#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)
{
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());
}