1a8db8ba30bcb60acfaced395ce4336a6464a30f
[apps/pfixtools.git] / postlicyd / tst-filters.c
1 /******************************************************************************/
2 /*          pfixtools: a collection of postfix related tools                  */
3 /*          ~~~~~~~~~                                                         */
4 /*  ________________________________________________________________________  */
5 /*                                                                            */
6 /*  Redistribution and use in source and binary forms, with or without        */
7 /*  modification, are permitted provided that the following conditions        */
8 /*  are met:                                                                  */
9 /*                                                                            */
10 /*  1. Redistributions of source code must retain the above copyright         */
11 /*     notice, this list of conditions and the following disclaimer.          */
12 /*  2. Redistributions in binary form must reproduce the above copyright      */
13 /*     notice, this list of conditions and the following disclaimer in the    */
14 /*     documentation and/or other materials provided with the distribution.   */
15 /*  3. The names of its contributors may not be used to endorse or promote    */
16 /*     products derived from this software without specific prior written     */
17 /*     permission.                                                            */
18 /*                                                                            */
19 /*  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   */
20 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     */
21 /*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        */
22 /*  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    */
23 /*  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    */
24 /*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      */
25 /*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  */
26 /*  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   */
27 /*  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   */
28 /*  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    */
29 /*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
30 /******************************************************************************/
31
32 /*
33  * Copyright © 2008 Florent Bruneau
34  */
35
36 #include "str.h"
37 #include "config.h"
38 #include "file.h"
39 #include <dirent.h>
40
41 #define DAEMON_NAME "tst-filters"
42
43 DECLARE_MAIN
44
45 static char *read_query(const char *basepath, const char *filename,
46                         char *buff, char **end, query_t *q)
47 {
48     char path[FILENAME_MAX];
49     snprintf(path, FILENAME_MAX, "%s%s", basepath, filename);
50     {
51         file_map_t map;
52         if (!file_map_open(&map, path, false)) {
53             UNIXERR("open");
54             return NULL;
55         }
56         if (map.end - map.map >= BUFSIZ) {
57             syslog(LOG_ERR, "File too large for a testcase: %s", path);
58             file_map_close(&map);
59             return NULL;
60         }
61         memcpy(buff, map.map, map.end - map.map);
62         if (end != NULL) {
63             *end = buff + (map.end - map.map);
64             **end = '\0';
65         }
66         file_map_close(&map);
67     }
68
69     char *eoq = strstr(buff, "\n\n");
70     if (eoq == NULL) {
71         return NULL;
72     }
73     if (!query_parse(q, buff)) {
74         syslog(LOG_ERR, "Cannot parse query from file %s", filename);
75         return NULL;
76     }
77     return eoq + 2;
78 }
79
80 static bool run_testcase(const config_t *config, const char *basepath,
81                          const char *filename)
82 {
83     char buff[BUFSIZ];
84     char *end;
85     query_t query;
86     const char *eol = read_query(basepath, filename, buff, &end, &query);
87     if (eol == NULL) {
88         return false;
89     }
90
91     bool ok = true;
92     while (eol < end) {
93         char *neol = memchr(eol, '\n', end - eol);
94         if (neol == NULL) {
95             neol = end;
96         }
97         *neol = '\0';
98         char *sep = memchr(eol, '=', neol - eol);
99         if (sep == NULL) {
100             eol = neol + 1;
101             syslog(LOG_ERR, "missing separator");
102             continue;
103         }
104         *sep = '\0';
105
106         int pos = filter_find_with_name(&config->filters, eol);
107         if (pos == -1) {
108             syslog(LOG_ERR, "Unknown filter %s", eol);
109             eol = neol + 1;
110             continue;
111         }
112         ++sep;
113         filter_result_t result = hook_tokenize(sep, neol - sep);
114         if (result == HTK_UNKNOWN) {
115             syslog(LOG_ERR, "Unknown filter result %.*s", neol - sep, sep);
116             eol = neol + 1;
117             continue;
118         }
119         filter_t *filter = array_ptr(config->filters, pos);
120
121 #define TEST(Name, Run)                                                        \
122         do {                                                                   \
123           bool __test = (Run);                                                 \
124           printf("  test %s: %s\n", Name, __test ? "SUCCESS" : "FAILED");      \
125           ok = ok && __test;                                                   \
126         } while (0)
127         TEST(filter->name, filter_test(filter, &query, result));
128         eol = neol + 1;
129     }
130     return ok;
131 }
132
133 static bool run_greylisttest(const config_t *config, const char *basepath)
134 {
135     char buff_q1[BUFSIZ];
136     char buff_q2[BUFSIZ];
137     char buff_q3[BUFSIZ];
138     query_t q1;
139     query_t q2;
140     query_t q3;
141     bool ok = true;
142
143     filter_t *greylist1;
144 //    filter_t *greylist2;
145
146 #define QUERY(Q)                                                               \
147     if (read_query(basepath, "greylist_" STR(Q), buff_##Q, NULL, &Q) == NULL) {    \
148         return false;                                                          \
149     }
150     QUERY(q1);
151     QUERY(q2);
152     QUERY(q3);
153 #undef QUERY
154
155 #define FILTER(F)                                                              \
156     do {                                                                       \
157       int __p = filter_find_with_name(&config->filters, STR(F));               \
158       if (__p < 0) {                                                           \
159           return false;                                                        \
160       }                                                                        \
161       F = array_ptr(config->filters, __p);                                     \
162     } while (0)
163     FILTER(greylist1);
164 //    FILTER(greylist2);
165 #undef FILTER
166
167     /* Test greylist */
168     TEST("greylisted", filter_test(greylist1, &q1, HTK_GREYLIST));
169     TEST("too_fast", filter_test(greylist1, &q1, HTK_GREYLIST));
170     sleep(5);
171     TEST("too_slow", filter_test(greylist1, &q1, HTK_GREYLIST));
172     sleep(2);
173     TEST("whitelisted", filter_test(greylist1, &q1, HTK_WHITELIST));
174     TEST("other_greylisted", filter_test(greylist1, &q2, HTK_GREYLIST));
175     TEST("auto_whitelisted", filter_test(greylist1, &q1, HTK_WHITELIST));
176     TEST("other_auto_whitelisted", filter_test(greylist1, &q2, HTK_WHITELIST));
177     TEST("greylisted", filter_test(greylist1, &q3, HTK_GREYLIST));
178     sleep(10);
179     TEST("cleanup", filter_test(greylist1, &q1, HTK_GREYLIST));
180
181     return ok;
182 }
183
184 int main(int argc, char *argv[])
185 {
186     char basepath[FILENAME_MAX];
187     char path[FILENAME_MAX];
188     char *p;
189
190     p = strrchr(argv[0], '/');
191     if (p == NULL) {
192         p = argv[0];
193     } else {
194         ++p;
195     }
196     snprintf(basepath, FILENAME_MAX, "%.*sdata/", p - argv[0], argv[0]);
197
198     /* Cleanup */
199     {
200 #define RM(File)                                                               \
201       snprintf(path, FILENAME_MAX, "%s/%s", basepath, File);                   \
202       unlink(path);
203       RM("test1_greylist.db");
204       RM("test1_whitelist.db");
205       RM("test2_greylist.db");
206       RM("test2_whitelist.db");
207 #undef RM
208     }
209
210     snprintf(path, FILENAME_MAX, "%s/test.conf", basepath);
211
212     config_t *config = config_read(path);
213     if (config == NULL) {
214         return 1;
215     }
216
217
218 #define RUN(Name, Test, ...)                                                   \
219     printf("Running %s:\n", Name);                                             \
220     printf("%s\n", run_##Test(config, basepath, ##__VA_ARGS__) ? "SUCCESS"     \
221                                                                : "FAILED");
222
223     /* Test stateless filters */
224     DIR *dir = opendir(basepath);
225     if (dir == NULL) {
226         UNIXERR("opendir");
227         return 1;
228     }
229     struct dirent *ent;
230     while ((ent = readdir(dir)) != NULL) {
231         if (strncmp("testcase_", ent->d_name, 9) == 0) {
232             RUN(ent->d_name, testcase, ent->d_name);
233         }
234     }
235     closedir(dir);
236
237     /* Test greylist */
238     RUN("greylist", greylisttest);
239
240
241 #undef RUN
242     config_delete(&config);
243     return 0;
244 }