]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
moderation: add regression tests
authorBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 5 Jul 2023 12:49:50 +0000 (14:49 +0200)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 5 Jul 2023 12:49:50 +0000 (14:49 +0200)
src/subscriberfuncs.c
tests/mlmmj.c

index 4390476c1a33985ed2f30cc8edd3a1034a30a20b..30f71f7b093737198d5d5acbdc596700289b9f5b 100644 (file)
@@ -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);
 }
index c2b4a697d0ce84eada71a17a5fea388024a008f3..ff8ddb89b32240db64eb7f1ea3414252d9500464 100644 (file)
@@ -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());
 }