]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
mlmmj-process: add tests for the function which matches the ml name
authorBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 8 Feb 2023 15:56:26 +0000 (16:56 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 8 Feb 2023 15:56:26 +0000 (16:56 +0100)
While here use xstrndup where appropriate

include/mlmmj-process.h
src/mlmmj-process.c
src/utils.c
tests/mlmmj.c

index b817db6ad2683ce0e93cb6092cd33b87c2f27b81..40c33575bd4866414725bc79e3e6ae8a7a94f505 100644 (file)
@@ -24,3 +24,4 @@
 #pragma once
 
 char *get_recipextra_from_env(int ctrlfd);
+bool addrmatch(const char *listaddr, const char *addr, const char *listdelim, char **recipextra);
index 7306b1d93fe350b02ae1369d12300191e1c13438..6dbbda86ac6c15f56ee6d0c3852ad49eabfa404a 100644 (file)
@@ -353,53 +353,6 @@ static enum action do_access(strlist *rule_strs, strlist *hdrs,
 }
 
 
-static int addrmatch(const char *listaddr, const char *addr,
-               const char *listdelim, char **recipextra)
-{
-       char *delim, *atsign;
-       size_t len;
-
-       if (!addr)
-               return 0;
-
-       if(strcasecmp(listaddr, addr) == 0) {
-               if (recipextra)
-                       *recipextra = NULL;
-               return 1;
-       }
-
-       if (!listdelim)
-               return 0;
-
-       delim = strstr(addr, listdelim);
-       if (!delim)
-               return 0;
-
-       len = delim - addr;
-       if(strncasecmp(listaddr, addr, len) != 0)
-               return 0;
-       if(*(listaddr + len) != '@')
-               return 0;
-
-       delim += strlen(listdelim);
-
-       atsign = strrchr(delim, '@');
-       if (!atsign)
-               return 0;
-
-       if(strcasecmp(listaddr + len + 1, atsign + 1) != 0)
-               return 0;
-
-       if (recipextra) {
-               len = atsign - delim;
-               *recipextra = (char *)xmalloc(len + 1);
-               strncpy(*recipextra, delim, len);
-               (*recipextra)[len] = '\0';
-       }
-
-       return 1;
-}
-
 
 static void print_help(const char *prg)
 {
@@ -592,13 +545,12 @@ int main(int argc, char **argv)
        }
 
        recipextra = get_recipextra_from_env(ctrlfd);
-       if (recipextra == NULL)
+       if (recipextra == NULL) {
                findaddress = true;
-       addr_in_to_or_cc = !statctrl(ctrlfd, "tocc");
-
-       if (findaddress) {
                listdelim = getlistdelim(ctrlfd);
        }
+       addr_in_to_or_cc = !statctrl(ctrlfd, "tocc");
+
        if(addr_in_to_or_cc || findaddress) {
                listaddrs = ctrlvalues(ctrlfd, "listaddress");
                tll_foreach(dtemails, it) {
index 7d4c9bc4d55018c8aa934892f48574aa55834023..87f64a232b703272f01c27b47afd6e5c0df7ebd9 100644 (file)
@@ -232,3 +232,49 @@ get_recipextra_from_env(int ctrlfd)
        }
        return (NULL);
 }
+
+bool
+addrmatch(const char *listaddr, const char *addr,
+    const char *listdelim, char **recipextra)
+{
+       char *delim, *atsign;
+       size_t len;
+
+       if (recipextra)
+               *recipextra = NULL;
+
+       if (addr == NULL)
+               return (false);
+
+       if(strcasecmp(listaddr, addr) == 0)
+               return (true);
+
+       if (listdelim == NULL)
+               return (false);
+
+       delim = strstr(addr, listdelim);
+       if (delim == NULL)
+               return (false);
+
+       len = delim - addr;
+       if(strncasecmp(listaddr, addr, len) != 0)
+               return (false);
+       if(*(listaddr + len) != '@')
+               return (false);
+
+       delim += strlen(listdelim);
+
+       atsign = strrchr(delim, '@');
+       if (!atsign)
+               return (false);
+
+       if(strcasecmp(listaddr + len + 1, atsign + 1) != 0)
+               return (false);
+
+       if (recipextra) {
+               len = atsign - delim;
+               *recipextra = xstrndup(delim, len);
+       }
+
+       return (true);
+}
index 5562862febfb39eda1ca259364d74932e4ee9f63..96fc89337c30e330ccbb740a4e35a81642d3c26b 100644 (file)
@@ -135,6 +135,7 @@ ATF_TC_WITHOUT_HEAD(get_recipextra_from_env_none);
 ATF_TC_WITHOUT_HEAD(get_recipextra_from_env_qmail);
 ATF_TC_WITHOUT_HEAD(get_recipextra_from_env_postfix);
 ATF_TC_WITHOUT_HEAD(get_recipextra_from_env_exim);
+ATF_TC_WITHOUT_HEAD(addrmatch);
 
 #ifndef NELEM
 #define NELEM(array)    (sizeof(array) / sizeof((array)[0]))
@@ -2037,6 +2038,27 @@ ATF_TC_BODY(get_recipextra_from_env_exim, tc)
        free(str);
 }
 
+ATF_TC_BODY(addrmatch, tc)
+{
+       char *extra;
+
+       ATF_REQUIRE(addrmatch("lists@test.org", "lists@test.org", "+", &extra));
+       ATF_REQUIRE(extra == NULL);
+
+       ATF_REQUIRE(!addrmatch("lists@test.org", NULL, "+", &extra));
+       ATF_REQUIRE(!addrmatch("lists@test.org", "nope@test.org", "+", &extra));
+       ATF_REQUIRE(!addrmatch("lists@test.org", "nope@test.org", NULL, &extra));
+       ATF_REQUIRE(addrmatch("lists@test.org", "lists+@test.org", "+", &extra));
+       ATF_REQUIRE_STREQ(extra, "");
+       ATF_REQUIRE(!addrmatch("lists@test.org", "list+@test.org", "+", &extra));
+       ATF_REQUIRE(!addrmatch("lists@test.org", "lists+@bla.org", "+", &extra));
+       ATF_REQUIRE(!addrmatch("lists@test.org", "bla+@test.org", "+", &extra));
+       ATF_REQUIRE(!addrmatch("lists@test.org", "list+@bla.org", "+", &extra));
+       ATF_REQUIRE(!addrmatch("lists@test.org", "list+test.org", "+", &extra));
+       ATF_REQUIRE(addrmatch("lists@test.org", "lists+value@test.org", "+", &extra));
+       ATF_REQUIRE_STREQ(extra, "value");
+}
+
 ATF_TP_ADD_TCS(tp)
 {
        ATF_TP_ADD_TC(tp, random_int);
@@ -2111,6 +2133,7 @@ ATF_TP_ADD_TCS(tp)
        ATF_TP_ADD_TC(tp, get_recipextra_from_env_qmail);
        ATF_TP_ADD_TC(tp, get_recipextra_from_env_postfix);
        ATF_TP_ADD_TC(tp, get_recipextra_from_env_exim);
+       ATF_TP_ADD_TC(tp, addrmatch);
 
        return (atf_no_error());
 }