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;
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++) {
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);
}
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)
{
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);
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());
}