0c08f6964037e456ccf1cfe958687936864b5ec7
[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     printf("Reading greylist_" STR(Q) "\n");                                   \
148     if (read_query(basepath, "greylist_" STR(Q), buff_##Q, NULL, &Q) == NULL) {    \
149         return false;                                                          \
150     }
151     QUERY(q1);
152     QUERY(q2);
153     QUERY(q3);
154 #undef QUERY
155
156 #define FILTER(F)                                                              \
157     do {                                                                       \
158       int __p = filter_find_with_name(&config->filters, STR(F));               \
159       if (__p < 0) {                                                           \
160           return false;                                                        \
161       }                                                                        \
162       F = array_ptr(config->filters, __p);                                     \
163     } while (0)
164     FILTER(greylist1);
165 //    FILTER(greylist2);
166 #undef FILTER
167
168     /* Test greylist */
169     TEST("greylisted", filter_test(greylist1, &q1, HTK_GREYLIST));
170     TEST("greylisted", filter_test(greylist1, &q1, HTK_GREYLIST));
171     sleep(2);
172     TEST("whitelisted", filter_test(greylist1, &q1, HTK_WHITELIST));
173     TEST("other_greylisted", filter_test(greylist1, &q2, HTK_GREYLIST));
174     TEST("auto_whitelisted", filter_test(greylist1, &q1, HTK_WHITELIST));
175     TEST("other_auto_whitelisted", filter_test(greylist1, &q2, HTK_WHITELIST));
176     TEST("greylisted", filter_test(greylist1, &q3, HTK_GREYLIST));
177
178     return ok;
179 }
180
181 int main(int argc, char *argv[])
182 {
183     char basepath[FILENAME_MAX];
184     char path[FILENAME_MAX];
185     char *p;
186
187     p = strrchr(argv[0], '/');
188     if (p == NULL) {
189         p = argv[0];
190     } else {
191         ++p;
192     }
193     snprintf(basepath, FILENAME_MAX, "%.*sdata/", p - argv[0], argv[0]);
194
195     /* Cleanup */
196     {
197 #define RM(File)                                                               \
198       snprintf(path, FILENAME_MAX, "%s/%s", basepath, File);                   \
199       unlink(path);
200       RM("test1_greylist.db");
201       RM("test1_whitelist.db");
202       RM("test2_greylist.db");
203       RM("test2_whitelist.db");
204 #undef RM
205     }
206
207     snprintf(path, FILENAME_MAX, "%s/test.conf", basepath);
208
209     config_t *config = config_read(path);
210     if (config == NULL) {
211         return 1;
212     }
213
214
215 #define RUN(Name, Test, ...)                                                   \
216     printf("Running %s:\n", Name);                                             \
217     printf("%s\n", run_##Test(config, basepath, ##__VA_ARGS__) ? "SUCCESS"     \
218                                                                : "FAILED");
219
220     /* Test stateless filters */
221     DIR *dir = opendir(basepath);
222     if (dir == NULL) {
223         UNIXERR("opendir");
224         return 1;
225     }
226     struct dirent *ent;
227     while ((ent = readdir(dir)) != NULL) {
228         if (strncmp("testcase_", ent->d_name, 9) == 0) {
229             RUN(ent->d_name, testcase, ent->d_name);
230         }
231     }
232     closedir(dir);
233
234     /* Test greylist */
235     RUN("greylist", greylisttest);
236
237
238 #undef RUN
239     config_delete(&config);
240     return 0;
241 }