From: Baptiste Daroussin Date: Wed, 5 Jul 2023 12:49:50 +0000 (+0200) Subject: moderation: add regression tests X-Git-Tag: RELEASE_1_4_0rc1~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=936db40ad98a4092f9b6c713d48d4f428764eceb;p=thirdparty%2Fmlmmj.git moderation: add regression tests --- diff --git a/src/subscriberfuncs.c b/src/subscriberfuncs.c index 4390476c..30f71f7b 100644 --- a/src/subscriberfuncs.c +++ b/src/subscriberfuncs.c @@ -412,9 +412,8 @@ void mod_get_addr_and_type(struct ml *ml, const char *modstr, char **addrptr, enum subtype *subtypeptr) { int fd; - char *readaddr, *readtype, *modfilename, *line = NULL; - FILE *f; - size_t linecap = 0; + char *readaddr, *readtype, *modfilename; + char *buf, *walk; if (strncmp(modstr, "subscribe", 9) == 0) modstr += 9; @@ -426,22 +425,13 @@ mod_get_addr_and_type(struct ml *ml, const char *modstr, char **addrptr, enum su log_error(LOG_ARGS, "Could not open %s/%s", ml->dir, modfilename); exit(EXIT_FAILURE); } - f = fdopen(fd, "r"); - - if (getline(&line, &linecap, f) <= 0) { - log_error(LOG_ARGS, "Could not parse %s/%s", ml->dir, modfilename); - exit(EXIT_FAILURE); - } - chomp(line); - readaddr = xstrdup(line); - if (getline(&line, &linecap, f) <= 0) { - log_error(LOG_ARGS, "Could not parse %s/%s", ml->dir, modfilename); + walk = buf = readlf(fd, false); + if (buf == NULL) { + log_error(LOG_ARGS, "Could not open %s/%s", ml->dir, modfilename); exit(EXIT_FAILURE); } - chomp(line); - readtype = xstrdup(line); - fclose(f); - + readaddr = xstrdup(strsep(&walk, "\n")); + readtype = strsep(&walk, "\n"); *addrptr = readaddr; for (size_t i = 0; i < NELEM(subtypes); i++) { @@ -455,9 +445,10 @@ mod_get_addr_and_type(struct ml *ml, const char *modstr, char **addrptr, enum su log_error(LOG_ARGS, "Type %s not valid in %s/%s", readtype, ml->dir, modfilename); + exit(EXIT_FAILURE); freedone: - free(readtype); + free(buf); unlinkat(ml->fd, modfilename, 0); free(modfilename); } diff --git a/tests/mlmmj.c b/tests/mlmmj.c index c2b4a697..ff8ddb89 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -150,6 +150,7 @@ ATF_TC_WITHOUT_HEAD(send_help); ATF_TC_WITHOUT_HEAD(requeuemail); ATF_TC_WITHOUT_HEAD(gethdrline); ATF_TC_WITHOUT_HEAD(readlf); +ATF_TC_WITHOUT_HEAD(mod_get_addr_type); ATF_TC_BODY(random_int, tc) { @@ -2601,6 +2602,70 @@ ATF_TC_BODY(readlf, tc) ATF_REQUIRE(readlf(-1, true) == NULL); } +ATF_TC_BODY(mod_get_addr_type, tc) +{ + mkdir("moderation/", 0775); + atf_utils_create_file("moderation/subscribeinvalid", ""); + atf_utils_create_file("moderation/subscribevalid", "user\nSUB_BOTH\n"); + atf_utils_create_file("moderation/subscribeinvalid2", "user\n"); + atf_utils_create_file("moderation/subscribeinvalid3", "user\ninvalid3\n"); + + pid_t p = atf_utils_fork(); + if (p == 0) { + struct ml ml; + enum subtype subtype; + char *addr; + ml.fd = open(".", O_DIRECTORY); + mod_get_addr_and_type(&ml, "modstr", &addr, &subtype); + } + atf_utils_wait(p, 1, "", ""); + + p = atf_utils_fork(); + if (p == 0) { + struct ml ml; + enum subtype subtype; + char *addr; + ml.fd = open(".", O_DIRECTORY); + mod_get_addr_and_type(&ml, "invalid", &addr, &subtype); + } + atf_utils_wait(p, 1, "", ""); + + p = atf_utils_fork(); + if (p == 0) { + struct ml ml; + enum subtype subtype = 0; + char *addr; + ml.fd = open(".", O_DIRECTORY); + mod_get_addr_and_type(&ml, "subscribevalid", &addr, &subtype); + ATF_REQUIRE_STREQ(addr, "user"); + ATF_REQUIRE_EQ(subtype, SUB_BOTH); + exit(EXIT_SUCCESS); + } + atf_utils_wait(p, 0, "", ""); + + p = atf_utils_fork(); + if (p == 0) { + struct ml ml; + enum subtype subtype = 0; + char *addr; + ml.fd = open(".", O_DIRECTORY); + mod_get_addr_and_type(&ml, "subscribeinvalid2", &addr, &subtype); + exit(EXIT_SUCCESS); + } + atf_utils_wait(p, 1, "", ""); + + p = atf_utils_fork(); + if (p == 0) { + struct ml ml; + enum subtype subtype = 0; + char *addr; + ml.fd = open(".", O_DIRECTORY); + mod_get_addr_and_type(&ml, "subscribeinvalid3", &addr, &subtype); + exit(EXIT_SUCCESS); + } + atf_utils_wait(p, 1, "", ""); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, random_int); @@ -2685,6 +2750,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, requeuemail); ATF_TP_ADD_TC(tp, gethdrline); ATF_TP_ADD_TC(tp, readlf); + ATF_TP_ADD_TC(tp, mod_get_addr_type); return (atf_no_error()); }