Can remove the sender and/or the recipient from the key of the greylister.
[apps/pfixtools.git] / postlicyd / tst-filters.c
index 7bcaa41..63ca7e6 100644 (file)
 #include "file.h"
 #include <dirent.h>
 
-#define DAEMON_NAME "tst-filters"
-
-DECLARE_MAIN
-
 static char *read_query(const char *basepath, const char *filename,
                         char *buff, char **end, query_t *q)
 {
@@ -54,7 +50,7 @@ static char *read_query(const char *basepath, const char *filename,
             return NULL;
         }
         if (map.end - map.map >= BUFSIZ) {
-            syslog(LOG_ERR, "File too large for a testcase: %s", path);
+            err("File too large for a testcase: %s", path);
             file_map_close(&map);
             return NULL;
         }
@@ -62,6 +58,8 @@ static char *read_query(const char *basepath, const char *filename,
         if (end != NULL) {
             *end = buff + (map.end - map.map);
             **end = '\0';
+        } else {
+            buff[map.end - map.map] = '\0';
         }
         file_map_close(&map);
     }
@@ -71,7 +69,7 @@ static char *read_query(const char *basepath, const char *filename,
         return NULL;
     }
     if (!query_parse(q, buff)) {
-        syslog(LOG_ERR, "Cannot parse query from file %s", filename);
+        err("Cannot parse query from file %s", filename);
         return NULL;
     }
     return eoq + 2;
@@ -89,6 +87,9 @@ static bool run_testcase(const config_t *config, const char *basepath,
     }
 
     bool ok = true;
+    filter_context_t context;
+    filter_context_prepare(&context, NULL);
+
     while (eol < end) {
         char *neol = memchr(eol, '\n', end - eol);
         if (neol == NULL) {
@@ -98,21 +99,21 @@ static bool run_testcase(const config_t *config, const char *basepath,
         char *sep = memchr(eol, '=', neol - eol);
         if (sep == NULL) {
             eol = neol + 1;
-            syslog(LOG_ERR, "missing separator");
+            err("missing separator");
             continue;
         }
         *sep = '\0';
 
         int pos = filter_find_with_name(&config->filters, eol);
         if (pos == -1) {
-            syslog(LOG_ERR, "Unknown filter %s", eol);
+            err("Unknown filter %s", eol);
             eol = neol + 1;
             continue;
         }
         ++sep;
         filter_result_t result = hook_tokenize(sep, neol - sep);
         if (result == HTK_UNKNOWN) {
-            syslog(LOG_ERR, "Unknown filter result %.*s", neol - sep, sep);
+            err("Unknown filter result %.*s", neol - sep, sep);
             eol = neol + 1;
             continue;
         }
@@ -124,9 +125,11 @@ static bool run_testcase(const config_t *config, const char *basepath,
           printf("  test %s: %s\n", Name, __test ? "SUCCESS" : "FAILED");      \
           ok = ok && __test;                                                   \
         } while (0)
-        TEST(filter->name, filter_test(filter, &query, result));
+        TEST(filter->name, filter_test(filter, &query, &context, result));
         eol = neol + 1;
+
     }
+    filter_context_wipe(&context);
     return ok;
 }
 
@@ -164,18 +167,24 @@ static bool run_greylisttest(const config_t *config, const char *basepath)
 //    FILTER(greylist2);
 #undef FILTER
 
+    filter_context_t context;
+    filter_context_prepare(&context, NULL);
+
     /* Test greylist */
-    TEST("greylisted", filter_test(greylist1, &q1, HTK_GREYLIST));
-    TEST("too_fast", filter_test(greylist1, &q1, HTK_GREYLIST));
+    TEST("greylisted", filter_test(greylist1, &q1, &context, HTK_GREYLIST));
+    TEST("too_fast", filter_test(greylist1, &q1, &context, HTK_GREYLIST));
     sleep(5);
-    TEST("too_slow", filter_test(greylist1, &q1, HTK_GREYLIST));
+    TEST("too_slow", filter_test(greylist1, &q1, &context, HTK_GREYLIST));
     sleep(2);
-    TEST("whitelisted", filter_test(greylist1, &q1, HTK_WHITELIST));
-    TEST("other_greylisted", filter_test(greylist1, &q2, HTK_GREYLIST));
-    TEST("auto_whitelisted", filter_test(greylist1, &q1, HTK_WHITELIST));
-    TEST("other_auto_whitelisted", filter_test(greylist1, &q2, HTK_WHITELIST));
-    TEST("greylisted", filter_test(greylist1, &q3, HTK_GREYLIST));
-
+    TEST("whitelisted", filter_test(greylist1, &q1, &context, HTK_WHITELIST));
+    TEST("other_greylisted", filter_test(greylist1, &q2, &context, HTK_GREYLIST));
+    TEST("auto_whitelisted", filter_test(greylist1, &q1, &context, HTK_WHITELIST));
+    TEST("other_auto_whitelisted", filter_test(greylist1, &q2, &context, HTK_WHITELIST));
+    TEST("greylisted", filter_test(greylist1, &q3, &context, HTK_GREYLIST));
+    sleep(10);
+    TEST("cleanup", filter_test(greylist1, &q1, &context, HTK_GREYLIST));
+
+    filter_context_wipe(&context);
     return ok;
 }
 
@@ -185,6 +194,7 @@ int main(int argc, char *argv[])
     char path[FILENAME_MAX];
     char *p;
 
+    common_startup();
     p = strrchr(argv[0], '/');
     if (p == NULL) {
         p = argv[0];
@@ -198,8 +208,8 @@ int main(int argc, char *argv[])
 #define RM(File)                                                               \
       snprintf(path, FILENAME_MAX, "%s/%s", basepath, File);                   \
       unlink(path);
-      RM("test1_greylist.db");
-      RM("test1_whitelist.db");
+//      RM("test1_greylist.db");
+//      RM("test1_whitelist.db");
       RM("test2_greylist.db");
       RM("test2_whitelist.db");
 #undef RM
@@ -237,6 +247,5 @@ int main(int argc, char *argv[])
 
 
 #undef RUN
-    config_delete(&config);
     return 0;
 }