From: Baptiste Daroussin Date: Thu, 16 Dec 2021 16:25:06 +0000 (+0100) Subject: subscriber functions: add unit tests X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a80773c0f00ee716324cd0b7f6587668007839ef;p=thirdparty%2Fmlmmj.git subscriber functions: add unit tests --- diff --git a/include/subscriberfuncs.h b/include/subscriberfuncs.h index 6e7e4939..8a4743c5 100644 --- a/include/subscriberfuncs.h +++ b/include/subscriberfuncs.h @@ -33,6 +33,6 @@ enum subtype is_subbed(struct mlmmj_list *list, const char *address, int both); int open_subscriber_directory(struct mlmmj_list *list, enum subtype typesub, const char **subdir); void change_uid(struct mlmmj_list *list); char *lowercase(const char *address); -void unsubscribe(struct mlmmj_list *list, const char *address, enum subtype typesub, enum subreason reasonsub); +void unsubscribe(struct mlmmj_list *list, const char *address, enum subtype typesub); #endif /* SUBSCRIBERFUNC_H */ diff --git a/src/subscriberfuncs.c b/src/subscriberfuncs.c index 07fffaea..0eb8df1f 100644 --- a/src/subscriberfuncs.c +++ b/src/subscriberfuncs.c @@ -65,6 +65,7 @@ off_t find_subscriber(int fd, const char *address) char *start, *cur, *next; struct stat st; size_t len; + off_t out = -1; if(fstat(fd, &st) < 0) { log_error(LOG_ARGS, "Could not stat fd"); @@ -91,8 +92,8 @@ off_t find_subscriber(int fd, const char *address) len = next - cur; if((strlen(address) == len) && (strncasecmp(address, cur, len) == 0)) { - munmap(start, st.st_size); - return (off_t)(cur - start); + out = (off_t)(cur - start); + goto out; } cur = next + 1; } @@ -102,13 +103,13 @@ off_t find_subscriber(int fd, const char *address) len = next - cur; if((strlen(address) == len) && (strncasecmp(address, cur, len) == 0)) { - munmap(start, st.st_size); - return (off_t)(cur - start); + out = (off_t)(cur - start); + goto out; } } - +out: munmap(start, st.st_size); - return (off_t)-1; + return out; } bool @@ -278,6 +279,7 @@ real_unsubscribe(int subreadfd, int subwritefd, const char *address) return written; } + void unsubscribe(struct mlmmj_list *list, const char *address, enum subtype typesub) { @@ -293,6 +295,13 @@ unsubscribe(struct mlmmj_list *list, const char *address, enum subtype typesub) off_t suboff; int subdirfd; + if (typesub == SUB_ALL) { + unsubscribe(list, address, SUB_NORMAL); + unsubscribe(list, address, SUB_DIGEST); + unsubscribe(list, address, SUB_NOMAIL); + return; + } + subdirfd = open_subscriber_directory(list, typesub, &subdir); if (fstat(subdirfd, &st) == 0) { if(st.st_mode & S_IWGRP) { diff --git a/tests/Makefile.am b/tests/Makefile.am index 1afccb9d..3b52f97b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -6,7 +6,8 @@ mlmmj_SOURCES = mlmmj.c $(top_srcdir)/src/prepstdreply.c $(top_srcdir)/include/p $(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/mlmmj.c $(top_srcdir)/src/mail-functions.c + $(top_srcdir)/src/mlmmj.c $(top_srcdir)/src/mail-functions.c \ + $(top_srcdir)/src/subscriberfuncs.c mlmmj_CFLAGS = @ATF_CFLAGS@ -g -Wall -pedantic -Wsign-compare -DDEFAULTTEXTDIR='"@textlibdir@"' -I$(top_srcdir)/include -DSRCDIR='"@abs_top_srcdir@"' mlmmj_LDADD = @ATF_LIBS@ diff --git a/tests/mlmmj.c b/tests/mlmmj.c index 05a42f4d..63c7f900 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -41,6 +41,7 @@ #include "memory.h" #include "mail-functions.h" #include "mygetline.h" +#include "subscriberfuncs.h" ATF_TC(random_int); ATF_TC(statctrl); @@ -59,6 +60,9 @@ ATF_TC(write_quit); ATF_TC(write_rset); ATF_TC(write_replyto); ATF_TC(mygetline); +ATF_TC(unsubscribe); +ATF_TC(lowercase); +ATF_TC(subbed); ATF_TC_HEAD(random_int, tc) { } ATF_TC_HEAD(statctrl, tc) { } @@ -77,6 +81,9 @@ ATF_TC_HEAD(write_quit, tc) {} ATF_TC_HEAD(write_rset, tc) {} ATF_TC_HEAD(write_replyto, tc) {} ATF_TC_HEAD(mygetline, tc) {} +ATF_TC_HEAD(unsubscribe, tc) {} +ATF_TC_HEAD(lowercase, tc) {} +ATF_TC_HEAD(subbed, tc) {} #ifndef NELEM #define NELEM(array) (sizeof(array) / sizeof((array)[0])) @@ -416,6 +423,90 @@ ATF_TC_BODY(mygetline, tc) close(fd); } +ATF_TC_BODY(unsubscribe, tc) +{ + struct stat st; + struct mlmmj_list list; + init_ml(true); + mlmmj_list_init(&list); + list.dir = "list"; + + ATF_CHECK(mlmmj_list_open(&list)); + + FILE *f = fopen("list/subscribers.d/j", "w"); + fputs("john@doe.org", f); + fclose(f); + + f = fopen("list/nomailsubs.d/j", "w"); + fputs("john@doe.org\n", f); + fputs("jane@doe.org\n", f); + fclose(f); + + f = fopen("list/digesters.d/j", "w"); + fputs("john@doe.org\n", f); + fclose(f); + + unsubscribe(&list, "john@doe.org", SUB_ALL); + ATF_REQUIRE_EQ_MSG(stat("list/subscribers.d/j", &st), -1, "Not unsubscribed"); + ATF_REQUIRE_EQ_MSG(stat("list/digesters.d/j", &st), -1, "Not unsubscribed from digest"); + if (!atf_utils_compare_file("list/nomailsubs.d/j", "jane@doe.org\n")) + atf_tc_fail("Not unsubscribed from nomail"); + + unsubscribe(&list, "jane@doe.org", SUB_NORMAL); + if (!atf_utils_compare_file("list/nomailsubs.d/j", "jane@doe.org\n")) + atf_tc_fail("Has been unsubscribed from nomail"); + + unsubscribe(&list, "jane@doe.org", SUB_DIGEST); + if (!atf_utils_compare_file("list/nomailsubs.d/j", "jane@doe.org\n")) + atf_tc_fail("Has been unsubscribed from nomail"); + + unsubscribe(&list, "jane@doe.org", SUB_NOMAIL); + ATF_REQUIRE_EQ_MSG(stat("list/nomailsubs.d/j", &st), -1, "Not unsubscribed from nomail"); + + rmdir("list/nomailsubs.d"); + pid_t p = atf_utils_fork(); + if (p == 0) { + unsubscribe(&list, "jane@doe.org", SUB_NOMAIL); + } + atf_utils_wait(p, 1, "", "mlmmj: Unable to open list/nomailsubs.d: No such file or directory\n"); +} + +ATF_TC_BODY(lowercase, tc) +{ + ATF_REQUIRE_STREQ(lowercase("MAR23anC"), "mar23anc"); +} + +ATF_TC_BODY(subbed, tc) +{ + struct mlmmj_list list; + init_ml(true); + mlmmj_list_init(&list); + list.dir = "list"; + + ATF_CHECK(mlmmj_list_open(&list)); + + FILE *f = fopen("list/subscribers.d/j", "w"); + fputs("john@doe.org", f); + fclose(f); + + ATF_CHECK_EQ(is_subbed(&list, "john@doe.org", false), SUB_NORMAL); + + f = fopen("list/digesters.d/j", "w"); + fputs("john@doe.org", f); + fclose(f); + ATF_CHECK_EQ(is_subbed(&list, "john@doe.org", false), SUB_NORMAL); + ATF_CHECK_EQ(is_subbed(&list, "john@doe.org", true), SUB_BOTH); + unlink("list/subscribers.d/j"); + ATF_CHECK_EQ(is_subbed(&list, "john@doe.org", true), SUB_DIGEST); + ATF_CHECK_EQ(is_subbed(&list, "john@doe.org", false), SUB_DIGEST); + unlink("list/digesters.d/j"); + ATF_CHECK_EQ(is_subbed(&list, "john@doe.org", false), SUB_NONE); + f = fopen("list/nomailsubs.d/j", "w"); + fputs("john@doe.org", f); + fclose(f); + ATF_CHECK_EQ(is_subbed(&list, "john@doe.org", false), SUB_NOMAIL); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, random_int); @@ -435,6 +526,9 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, write_rset); ATF_TP_ADD_TC(tp, write_replyto); ATF_TP_ADD_TC(tp, mygetline); + ATF_TP_ADD_TC(tp, unsubscribe); + ATF_TP_ADD_TC(tp, lowercase); + ATF_TP_ADD_TC(tp, subbed); return (atf_no_error()); }